wp-session.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * WordPress session managment.
  4. *
  5. * Standardizes WordPress session data and uses either database transients or in-memory caching
  6. * for storing user session information.
  7. *
  8. * @package WordPress
  9. * @subpackage Session
  10. * @since 3.7.0
  11. */
  12. // Exit if accessed directly
  13. if ( ! defined( 'ABSPATH' ) ) exit;
  14. /**
  15. * Return the current cache expire setting.
  16. *
  17. * @return int
  18. */
  19. function wp_session_cache_expire() {
  20. $wp_session = WP_Session::get_instance();
  21. return $wp_session->cache_expiration();
  22. }
  23. /**
  24. * Alias of wp_session_write_close()
  25. */
  26. function wp_session_commit() {
  27. wp_session_write_close();
  28. }
  29. /**
  30. * Load a JSON-encoded string into the current session.
  31. *
  32. * @param string $data
  33. */
  34. function wp_session_decode( $data ) {
  35. $wp_session = WP_Session::get_instance();
  36. return $wp_session->json_in( $data );
  37. }
  38. /**
  39. * Encode the current session's data as a JSON string.
  40. *
  41. * @return string
  42. */
  43. function wp_session_encode() {
  44. $wp_session = WP_Session::get_instance();
  45. return $wp_session->json_out();
  46. }
  47. /**
  48. * Regenerate the session ID.
  49. *
  50. * @param bool $delete_old_session
  51. *
  52. * @return bool
  53. */
  54. function wp_session_regenerate_id( $delete_old_session = false ) {
  55. $wp_session = WP_Session::get_instance();
  56. $wp_session->regenerate_id( $delete_old_session );
  57. return true;
  58. }
  59. /**
  60. * Start new or resume existing session.
  61. *
  62. * Resumes an existing session based on a value sent by the _wp_session cookie.
  63. *
  64. * @return bool
  65. */
  66. function wp_session_start() {
  67. $wp_session = WP_Session::get_instance();
  68. do_action( 'wp_session_start' );
  69. return $wp_session->session_started();
  70. }
  71. add_action( 'plugins_loaded', 'wp_session_start' );
  72. /**
  73. * Return the current session status.
  74. *
  75. * @return int
  76. */
  77. function wp_session_status() {
  78. $wp_session = WP_Session::get_instance();
  79. if ( $wp_session->session_started() ) {
  80. return PHP_SESSION_ACTIVE;
  81. }
  82. return PHP_SESSION_NONE;
  83. }
  84. /**
  85. * Unset all session variables.
  86. */
  87. function wp_session_unset() {
  88. $wp_session = WP_Session::get_instance();
  89. $wp_session->reset();
  90. }
  91. /**
  92. * Write session data and end session
  93. */
  94. function wp_session_write_close() {
  95. $wp_session = WP_Session::get_instance();
  96. $wp_session->write_data();
  97. do_action( 'wp_session_commit' );
  98. }
  99. add_action( 'shutdown', 'wp_session_write_close' );
  100. /**
  101. * Clean up expired sessions by removing data and their expiration entries from
  102. * the WordPress options table.
  103. *
  104. * This method should never be called directly and should instead be triggered as part
  105. * of a scheduled task or cron job.
  106. */
  107. function wp_session_cleanup() {
  108. global $wpdb;
  109. if ( defined( 'WP_SETUP_CONFIG' ) ) {
  110. return;
  111. }
  112. if ( ! defined( 'WP_INSTALLING' ) ) {
  113. $expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" );
  114. $now = current_time( 'timestamp' );
  115. $expired_sessions = array();
  116. foreach( $expiration_keys as $expiration ) {
  117. // If the session has expired
  118. if ( $now > intval( $expiration->option_value ) ) {
  119. // Get the session ID by parsing the option_name
  120. $session_id = substr( $expiration->option_name, 20 );
  121. if( (int) -1 === (int) $session_id || ! preg_match( '/^[a-f0-9]{32}$/', $session_id ) ) {
  122. continue;
  123. }
  124. $expired_sessions[] = $expiration->option_name;
  125. $expired_sessions[] = esc_sql( "_wp_session_$session_id" );
  126. }
  127. }
  128. // Delete all expired sessions in a single query
  129. if ( ! empty( $expired_sessions ) ) {
  130. $option_names = implode( "','", $expired_sessions );
  131. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" );
  132. }
  133. }
  134. // Allow other plugins to hook in to the garbage collection process.
  135. do_action( 'wp_session_cleanup' );
  136. }
  137. add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' );
  138. /**
  139. * Register the garbage collector as a twice daily event.
  140. */
  141. function wp_session_register_garbage_collection() {
  142. if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) {
  143. wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'wp_session_garbage_collection' );
  144. }
  145. }
  146. add_action( 'wp', 'wp_session_register_garbage_collection' );