[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-user-meta-session-tokens.php (source)

   1  <?php
   2  /**
   3   * Session API: WP_User_Meta_Session_Tokens class
   4   *
   5   * @package WordPress
   6   * @subpackage Session
   7   * @since 4.7.0
   8   */
   9  
  10  /**
  11   * Meta-based user sessions token manager.
  12   *
  13   * @since 4.0.0
  14   *
  15   * @see WP_Session_Tokens
  16   */
  17  class WP_User_Meta_Session_Tokens extends WP_Session_Tokens {
  18  
  19      /**
  20       * Retrieves all sessions of the user.
  21       *
  22       * @since 4.0.0
  23       *
  24       * @return array Sessions of the user.
  25       */
  26  	protected function get_sessions() {
  27          $sessions = get_user_meta( $this->user_id, 'session_tokens', true );
  28  
  29          if ( ! is_array( $sessions ) ) {
  30              return array();
  31          }
  32  
  33          $sessions = array_map( array( $this, 'prepare_session' ), $sessions );
  34          return array_filter( $sessions, array( $this, 'is_still_valid' ) );
  35      }
  36  
  37      /**
  38       * Converts an expiration to an array of session information.
  39       *
  40       * @param mixed $session Session or expiration.
  41       * @return array Session.
  42       */
  43  	protected function prepare_session( $session ) {
  44          if ( is_int( $session ) ) {
  45              return array( 'expiration' => $session );
  46          }
  47  
  48          return $session;
  49      }
  50  
  51      /**
  52       * Retrieves a session based on its verifier (token hash).
  53       *
  54       * @since 4.0.0
  55       *
  56       * @param string $verifier Verifier for the session to retrieve.
  57       * @return array|null The session, or null if it does not exist
  58       */
  59  	protected function get_session( $verifier ) {
  60          $sessions = $this->get_sessions();
  61  
  62          if ( isset( $sessions[ $verifier ] ) ) {
  63              return $sessions[ $verifier ];
  64          }
  65  
  66          return null;
  67      }
  68  
  69      /**
  70       * Updates a session based on its verifier (token hash).
  71       *
  72       * @since 4.0.0
  73       *
  74       * @param string $verifier Verifier for the session to update.
  75       * @param array  $session  Optional. Session. Omitting this argument destroys the session.
  76       */
  77  	protected function update_session( $verifier, $session = null ) {
  78          $sessions = $this->get_sessions();
  79  
  80          if ( $session ) {
  81              $sessions[ $verifier ] = $session;
  82          } else {
  83              unset( $sessions[ $verifier ] );
  84          }
  85  
  86          $this->update_sessions( $sessions );
  87      }
  88  
  89      /**
  90       * Updates the user's sessions in the usermeta table.
  91       *
  92       * @since 4.0.0
  93       *
  94       * @param array $sessions Sessions.
  95       */
  96  	protected function update_sessions( $sessions ) {
  97          if ( $sessions ) {
  98              update_user_meta( $this->user_id, 'session_tokens', $sessions );
  99          } else {
 100              delete_user_meta( $this->user_id, 'session_tokens' );
 101          }
 102      }
 103  
 104      /**
 105       * Destroys all sessions for this user, except the single session with the given verifier.
 106       *
 107       * @since 4.0.0
 108       *
 109       * @param string $verifier Verifier of the session to keep.
 110       */
 111  	protected function destroy_other_sessions( $verifier ) {
 112          $session = $this->get_session( $verifier );
 113          $this->update_sessions( array( $verifier => $session ) );
 114      }
 115  
 116      /**
 117       * Destroys all session tokens for the user.
 118       *
 119       * @since 4.0.0
 120       */
 121  	protected function destroy_all_sessions() {
 122          $this->update_sessions( array() );
 123      }
 124  
 125      /**
 126       * Destroys all sessions for all users.
 127       *
 128       * @since 4.0.0
 129       */
 130  	public static function drop_sessions() {
 131          delete_metadata( 'user', 0, 'session_tokens', false, true );
 132      }
 133  }


Generated: Wed Nov 6 01:00:02 2024 Cross-referenced by PHPXref 0.7.1