You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

140 lines
3.0 KiB

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