/includes/Libraries/Session/wp-session.php

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