wp/wp-includes/class-wp-user-meta-session-tokens.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     9 
     9 
    10 /**
    10 /**
    11  * Meta-based user sessions token manager.
    11  * Meta-based user sessions token manager.
    12  *
    12  *
    13  * @since 4.0.0
    13  * @since 4.0.0
       
    14  *
       
    15  * @see WP_Session_Tokens
    14  */
    16  */
    15 class WP_User_Meta_Session_Tokens extends WP_Session_Tokens {
    17 class WP_User_Meta_Session_Tokens extends WP_Session_Tokens {
    16 
    18 
    17 	/**
    19 	/**
    18 	 * Get all sessions of a user.
    20 	 * Retrieves all sessions of the user.
    19 	 *
    21 	 *
    20 	 * @since 4.0.0
    22 	 * @since 4.0.0
    21 	 *
    23 	 *
    22 	 * @return array Sessions of a user.
    24 	 * @return array Sessions of the user.
    23 	 */
    25 	 */
    24 	protected function get_sessions() {
    26 	protected function get_sessions() {
    25 		$sessions = get_user_meta( $this->user_id, 'session_tokens', true );
    27 		$sessions = get_user_meta( $this->user_id, 'session_tokens', true );
    26 
    28 
    27 		if ( ! is_array( $sessions ) ) {
    29 		if ( ! is_array( $sessions ) ) {
    45 
    47 
    46 		return $session;
    48 		return $session;
    47 	}
    49 	}
    48 
    50 
    49 	/**
    51 	/**
    50 	 * Retrieve a session by its verifier (token hash).
    52 	 * Retrieves a session based on its verifier (token hash).
    51 	 *
    53 	 *
    52 	 * @since 4.0.0
    54 	 * @since 4.0.0
    53 	 *
    55 	 *
    54 	 * @param string $verifier Verifier of the session to retrieve.
    56 	 * @param string $verifier Verifier for the session to retrieve.
    55 	 * @return array|null The session, or null if it does not exist
    57 	 * @return array|null The session, or null if it does not exist
    56 	 */
    58 	 */
    57 	protected function get_session( $verifier ) {
    59 	protected function get_session( $verifier ) {
    58 		$sessions = $this->get_sessions();
    60 		$sessions = $this->get_sessions();
    59 
    61 
    63 
    65 
    64 		return null;
    66 		return null;
    65 	}
    67 	}
    66 
    68 
    67 	/**
    69 	/**
    68 	 * Update a session by its verifier.
    70 	 * Updates a session based on its verifier (token hash).
    69 	 *
    71 	 *
    70 	 * @since 4.0.0
    72 	 * @since 4.0.0
    71 	 *
    73 	 *
    72 	 * @param string $verifier Verifier of the session to update.
    74 	 * @param string $verifier Verifier for the session to update.
    73 	 * @param array  $session  Optional. Session. Omitting this argument destroys the session.
    75 	 * @param array  $session  Optional. Session. Omitting this argument destroys the session.
    74 	 */
    76 	 */
    75 	protected function update_session( $verifier, $session = null ) {
    77 	protected function update_session( $verifier, $session = null ) {
    76 		$sessions = $this->get_sessions();
    78 		$sessions = $this->get_sessions();
    77 
    79 
    83 
    85 
    84 		$this->update_sessions( $sessions );
    86 		$this->update_sessions( $sessions );
    85 	}
    87 	}
    86 
    88 
    87 	/**
    89 	/**
    88 	 * Update a user's sessions in the usermeta table.
    90 	 * Updates the user's sessions in the usermeta table.
    89 	 *
    91 	 *
    90 	 * @since 4.0.0
    92 	 * @since 4.0.0
    91 	 *
    93 	 *
    92 	 * @param array $sessions Sessions.
    94 	 * @param array $sessions Sessions.
    93 	 */
    95 	 */
    98 			delete_user_meta( $this->user_id, 'session_tokens' );
   100 			delete_user_meta( $this->user_id, 'session_tokens' );
    99 		}
   101 		}
   100 	}
   102 	}
   101 
   103 
   102 	/**
   104 	/**
   103 	 * Destroy all session tokens for a user, except a single session passed.
   105 	 * Destroys all sessions for this user, except the single session with the given verifier.
   104 	 *
   106 	 *
   105 	 * @since 4.0.0
   107 	 * @since 4.0.0
   106 	 *
   108 	 *
   107 	 * @param string $verifier Verifier of the session to keep.
   109 	 * @param string $verifier Verifier of the session to keep.
   108 	 */
   110 	 */
   110 		$session = $this->get_session( $verifier );
   112 		$session = $this->get_session( $verifier );
   111 		$this->update_sessions( array( $verifier => $session ) );
   113 		$this->update_sessions( array( $verifier => $session ) );
   112 	}
   114 	}
   113 
   115 
   114 	/**
   116 	/**
   115 	 * Destroy all session tokens for a user.
   117 	 * Destroys all session tokens for the user.
   116 	 *
   118 	 *
   117 	 * @since 4.0.0
   119 	 * @since 4.0.0
   118 	 */
   120 	 */
   119 	protected function destroy_all_sessions() {
   121 	protected function destroy_all_sessions() {
   120 		$this->update_sessions( array() );
   122 		$this->update_sessions( array() );
   121 	}
   123 	}
   122 
   124 
   123 	/**
   125 	/**
   124 	 * Destroy all session tokens for all users.
   126 	 * Destroys all sessions for all users.
   125 	 *
   127 	 *
   126 	 * @since 4.0.0
   128 	 * @since 4.0.0
   127 	 * @static
       
   128 	 */
   129 	 */
   129 	public static function drop_sessions() {
   130 	public static function drop_sessions() {
   130 		delete_metadata( 'user', 0, 'session_tokens', false, true );
   131 		delete_metadata( 'user', 0, 'session_tokens', false, true );
   131 	}
   132 	}
   132 }
   133 }