-
Notifications
You must be signed in to change notification settings - Fork 2
/
woocommerce-session-extend.php
executable file
·247 lines (205 loc) · 7.61 KB
/
woocommerce-session-extend.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
<?php
/**
* Plugin Name: WooCommerce Session Extend
* Plugin URI: github.com
* Description: This plugin simply extends your cart sessions so users can keep their cart items for up to 30 days
* Version: 1.0
* Author: Dwain Maralack
* Author URI: http://dwainm.com
* Requires at least 3.8
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action('plugins_loaded', 'load_woocommerce_session_extend_class');
//if ( ! class_exists( 'WooCommerce' ) ) exit; // exit if woocommerce is not installed
function load_woocommerce_session_extend_class(){
add_filter('woocommerce_session_handler', 'replace_with_custom_session_handler_class');
function replace_with_custom_session_handler_class(){
return 'WC_Custom_Session_Extend_Handler';
}
/**
* Custom WooCommerce class to Handle data for the current customers session, with the purpose of extending it
* to 30 Days.
*
* Implements the WC_Session abstract class
*
* Long term plan will be, if https://github.com/ericmann/wp-session-manager/ gains traction
* in WP core, this will be switched out to use it and maintain backwards compatibility :)
*
* Partly based on WP SESSION by Eric Mann.
*
* @class WC_Session_Handler
* @version 2.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
if( class_exists('WC_Session') ){ // check if WooCommerce is alive
class WC_Custom_Session_Extend_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;
/**
* Constructor for the session class.
*
* @access public
* @return void
*/
public function __construct() {
$this->_cookie = 'wp_woocommerce_session_' . COOKIEHASH;
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();
$session_expiry_option = '_wc_session_expires_' . $this->_customer_id;
// Check if option exists first to avoid auloading cleaned up sessions
if ( false === get_option( $session_expiry_option ) ) {
add_option( $session_expiry_option, $this->_session_expiration, '', 'no' );
} else {
update_option( $session_expiry_option, $this->_session_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 );
}
/**
* 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 * 719 ) ); // 30 Days
$this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 720 ) ); // 30 days Hours
}
/**
* generate_customer_id function.
*
* @access public
* @return mixed
*/
public function generate_customer_id() {
if ( is_user_logged_in() ) {
return get_current_user_id();
} else {
return wp_generate_password( 32, false );
}
}
/**
* 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() {
return (array) get_option( '_wc_session_' . $this->_customer_id, array() );
}
/**
* save_data function.
*
* @access public
* @return void
*/
public function save_data() {
// Dirty if something changed - prevents saving nothing new
if ( $this->_dirty && $this->has_session() ) {
$session_option = '_wc_session_' . $this->_customer_id;
$session_expiry_option = '_wc_session_expires_' . $this->_customer_id;
if ( false === get_option( $session_option ) ) {
add_option( $session_option, $this->_data, '', 'no' );
add_option( $session_expiry_option, $this->_session_expiration, '', 'no' );
} else {
update_option( $session_option, $this->_data );
}
}
}
/**
* cleanup_sessions function.
*
* @access public
* @return void
*/
public function cleanup_sessions() {
global $wpdb;
if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
$now = time();
$expired_sessions = array();
$wc_session_expires = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wc_session_expires_%'" );
foreach ( $wc_session_expires as $wc_session_expire ) {
if ( $now > intval( $wc_session_expire->option_value ) ) {
$session_id = substr( $wc_session_expire->option_name, 20 );
$expired_sessions[] = $wc_session_expire->option_name; // Expires key
$expired_sessions[] = "_wc_session_$session_id"; // Session key
}
}
if ( ! empty( $expired_sessions ) ) {
$expired_sessions_chunked = array_chunk( $expired_sessions, 100 );
foreach ( $expired_sessions_chunked as $chunk ) {
$option_names = implode( "','", $chunk );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" );
}
}
}
}
}// end class WC_Custom_Session_Extend_Handler
}// end WooCommerce class exists check
}// load_woocommerce_session_extend_class