|
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
|
|
|
/** |
|
14
|
|
|
* Return the current cache expire setting. |
|
15
|
|
|
* |
|
16
|
|
|
* @return int |
|
17
|
|
|
*/ |
|
18
|
|
|
function wp_session_cache_expire() { |
|
19
|
|
|
$wp_session = WP_Session::get_instance(); |
|
20
|
|
|
|
|
21
|
|
|
return $wp_session->cache_expiration(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Alias of wp_session_write_close() |
|
26
|
|
|
*/ |
|
27
|
|
|
function wp_session_commit() { |
|
28
|
|
|
wp_session_write_close(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Load a JSON-encoded string into the current session. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $data |
|
35
|
|
|
*/ |
|
36
|
|
|
function wp_session_decode( $data ) { |
|
37
|
|
|
$wp_session = WP_Session::get_instance(); |
|
38
|
|
|
|
|
39
|
|
|
return $wp_session->json_in( $data ); |
|
40
|
|
|
} |
|
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
|
|
|
/** |
|
54
|
|
|
* Regenerate the session ID. |
|
55
|
|
|
* |
|
56
|
|
|
* @param bool $delete_old_session |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
function wp_session_regenerate_id( $delete_old_session = false ) { |
|
61
|
|
|
$wp_session = WP_Session::get_instance(); |
|
62
|
|
|
|
|
63
|
|
|
$wp_session->regenerate_id( $delete_old_session ); |
|
64
|
|
|
|
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Start new or resume existing session. |
|
70
|
|
|
* |
|
71
|
|
|
* Resumes an existing session based on a value sent by the _wp_session cookie. |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
function wp_session_start() { |
|
76
|
|
|
$wp_session = WP_Session::get_instance(); |
|
77
|
|
|
do_action( 'wp_session_start' ); |
|
78
|
|
|
|
|
79
|
|
|
return $wp_session->session_started(); |
|
80
|
|
|
} |
|
81
|
|
|
if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
|
82
|
|
|
add_action( 'plugins_loaded', 'wp_session_start' ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return the current session status. |
|
87
|
|
|
* |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
|
|
function wp_session_status() { |
|
91
|
|
|
$wp_session = WP_Session::get_instance(); |
|
92
|
|
|
|
|
93
|
|
|
if ( $wp_session->session_started() ) { |
|
94
|
|
|
return PHP_SESSION_ACTIVE; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return PHP_SESSION_NONE; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Unset all session variables. |
|
102
|
|
|
*/ |
|
103
|
|
|
function wp_session_unset() { |
|
104
|
|
|
$wp_session = WP_Session::get_instance(); |
|
105
|
|
|
|
|
106
|
|
|
$wp_session->reset(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Write session data and end session |
|
111
|
|
|
*/ |
|
112
|
|
|
function wp_session_write_close() { |
|
113
|
|
|
$wp_session = WP_Session::get_instance(); |
|
114
|
|
|
|
|
115
|
|
|
$wp_session->write_data(); |
|
116
|
|
|
do_action( 'wp_session_commit' ); |
|
117
|
|
|
} |
|
118
|
|
|
if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
|
119
|
|
|
add_action( 'shutdown', 'wp_session_write_close' ); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Clean up expired sessions by removing data and their expiration entries from |
|
124
|
|
|
* the WordPress options table. |
|
125
|
|
|
* |
|
126
|
|
|
* This method should never be called directly and should instead be triggered as part |
|
127
|
|
|
* of a scheduled task or cron job. |
|
128
|
|
|
*/ |
|
129
|
|
|
function wp_session_cleanup() { |
|
130
|
|
|
if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ( ! defined( 'WP_INSTALLING' ) ) { |
|
135
|
|
|
/** |
|
136
|
|
|
* Determine the size of each batch for deletion. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int |
|
139
|
|
|
*/ |
|
140
|
|
|
$batch_size = apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
141
|
|
|
|
|
142
|
|
|
// Delete a batch of old sessions |
|
143
|
|
|
WP_Session_Utils::delete_old_sessions( $batch_size ); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Allow other plugins to hook in to the garbage collection process. |
|
147
|
|
|
do_action( 'wp_session_cleanup' ); |
|
148
|
|
|
} |
|
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( time(), 'hourly', 'wp_session_garbage_collection' ); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
add_action( 'wp', 'wp_session_register_garbage_collection' ); |
|
160
|
|
|
|