-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-df-wc-session-handler.php
306 lines (253 loc) · 8.35 KB
/
class-df-wc-session-handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Handle data for the current customers session.
* Implements the WC_Session abstract class
*
* Based on WooCommerce's WC_Session_Handler which is in-turn partly based on WP SESSION by Eric Mann.
* Those implementations used the wp_options table to store sessions.
* This relocates data storage to a dedicated table to avoid bloating and frequent invalidation of the WP options cache.
*
* @class DF_WC_Session_Handler
* @version 1.2.1
* @author Jeff Brand
* @author WooThemes
*/
class DF_WC_Session_Handler extends WC_Session {
/** cookie name */
private $_cookie;
/** session due to expire timestamp */
private $_session_expiring;
/** session expiration timestamp */
private $_session_expiration;
/** Bool based on whether a cookie exists **/
private $_has_cookie = false;
/** Table name for custom session records **/
private $_table;
/** Cache group **/
private $_cachegroup = 'df_wc_sessions';
/** Override use of cache API */
private $_force_cache = null;
//@todo: Deprecate this in the future based on https://github.com/woothemes/woocommerce/issues/6846
public function set( $key, $value ) {
if ( $value !== $this->get( $key ) ) {
parent::set( $key, $value );
}
}
/**
* Constructor for the session class.
*
* @access public
* @return void
*/
public function __construct() {
global $wpdb;
$this->_table = $wpdb->prefix . 'df_wc_sessions';
$this->_cookie = 'wp_woocommerce_session_' . COOKIEHASH;
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
if ( $cookie = $this->get_session_cookie() ) {
$this->_customer_id = $cookie[0];
$this->_session_expiration = $cookie[1];
$this->_session_expiring = $cookie[2];
$this->_has_cookie = true;
// Update session if its close to expiring
if ( time() > $this->_session_expiring ) {
$this->set_session_expiration();
$this->update_expiration( $this->_customer_id, $this->_session_expiration );
// This doesn't update the cache's expiration.
}
} else {
$this->set_session_expiration();
$this->_customer_id = $this->generate_customer_id();
}
$this->_data = $this->get_session_data();
// Actions
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) );
if ( ! is_user_logged_in() ) {
add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) );
}
}
/**
* Sets the session cookie on-demand (usually after adding an item to the cart).
*
* Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set.
*
* Warning: Cookies will only be set if this is called before the headers are sent.
*/
public function set_customer_session_cookie( $set ) {
if ( $set ) {
// Set/renew our cookie
$to_hash = $this->_customer_id . $this->_session_expiration;
$cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
$cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash;
$this->_has_cookie = true;
// Set the cookie
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );
}
}
/**
* Return true if the current user has an active session, i.e. a cookie to retrieve values
* @return boolean
*/
public function has_session() {
return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in();
}
/**
* set_session_expiration function.
*
* @access public
* @return void
*/
public function set_session_expiration() {
$this->_session_expiring = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours
$this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours
}
/**
* Generate a unique customer ID for guests, or return user ID if logged in.
*
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID.
*
* @access public
* @return int|string
*/
public function generate_customer_id() {
if ( is_user_logged_in() ) {
return get_current_user_id();
} else {
require_once( ABSPATH . 'wp-includes/class-phpass.php');
$hasher = new PasswordHash( 8, false );
return md5( $hasher->get_random_bytes( 32 ) );
}
}
/**
* get_session_cookie function.
*
* @access public
* @return mixed
*/
public function get_session_cookie() {
if ( empty( $_COOKIE[ $this->_cookie ] ) ) {
return false;
}
list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $_COOKIE[ $this->_cookie ] );
// Validate hash
$to_hash = $customer_id . $session_expiration;
$hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
if ( $hash != $cookie_hash ) {
return false;
}
return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
}
/**
* get_session_data function.
*
* @access public
* @return array
*/
public function get_session_data() {
global $wpdb;
if ( ! $this->has_session() ) {
return array();
}
$val = $this->use_cache() ? wp_cache_get( $this->_customer_id, $this->_cachegroup ) : false;
if ( false === $val ) {
$val = $wpdb->get_var( $wpdb->prepare( "SELECT data FROM $this->_table WHERE customer_id=%s LIMIT 1", $this->_customer_id ) );
// Populate cache with empty array to prevent DB query.
if ( $val === null ) {
$val = serialize( array() );
}
if ( $this->use_cache() ) {
$expire_in = $this->_session_expiration - time();
wp_cache_set( $this->_customer_id, $val, $this->_cachegroup, $expire_in );
}
}
return $val ? (array) @unserialize( $val ) : array();
}
/**
* save_data function.
*
* @access public
* @return void
*/
public function save_data() {
global $wpdb;
// Dirty if something changed - prevents saving nothing new
if ( $this->_dirty && $this->has_session() ) {
$data = array(
'customer_id' => $this->_customer_id,
'expiration' => $this->_session_expiration,
'data' => serialize( $this->_data )
);
if ( $this->use_cache() ) {
wp_cache_delete( $this->_customer_id, $this->_cachegroup );
}
$wpdb->replace( $this->_table, $data );
}
}
/**
* Destroy all session data
*/
public function destroy_session() {
// Clear cookie
wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, apply_filters( 'wc_session_use_secure_cookie', false ) );
global $wpdb;
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE customer_id=%s", $this->_customer_id ) );
// Clear cart
wc_empty_cart();
// Clear cache
if ( $this->use_cache() ) {
wp_cache_delete( $this->_customer_id, $this->_cachegroup );
}
// Clear data
$this->_data = array();
$this->_dirty = false;
$this->_customer_id = $this->generate_customer_id();
}
/**
* cleanup_sessions function.
*
* @access public
* @return void
*/
public function cleanup_sessions() {
global $wpdb;
if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE expiration < %d", time() ) );
if ( $this->use_cache() ) {
wp_cache_flush();
}
}
}
public function destroy_all_sessions() {
global $wpdb;
$wpdb->query( "TRUNCATE TABLE $this->_table" );
if ( $this->use_cache() ) {
wp_cache_flush();
}
return true;
}
public function update_expiration( $customer_id, $expiration ) {
global $wpdb;
$wpdb->update( $this->_table, array( 'expiration' => (int) $expiration ), compact( 'customer_id' ) );
}
/**
* Force use of cache
* null: use external cache when present (default)
* true: always use cache
* false: never use cache
*/
public function force_cache( $force ) {
$this->_force_cache = $force;
}
private function use_cache() {
$use_cache = ( null !== $this->_force_cache ) ? $this->_force_cache : wp_using_ext_object_cache();
return $use_cache;
}
}