@@ -12,125 +12,125 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class WPInv_Session_Handler extends WPInv_Session { |
14 | 14 | |
15 | - /** |
|
16 | - * Cookie name used for the session. |
|
17 | - * |
|
18 | - * @var string cookie name |
|
19 | - */ |
|
20 | - protected $_cookie; |
|
21 | - |
|
22 | - /** |
|
23 | - * Stores session expiry. |
|
24 | - * |
|
25 | - * @var int session due to expire timestamp |
|
26 | - */ |
|
27 | - protected $_session_expiring; |
|
28 | - |
|
29 | - /** |
|
30 | - * Stores session due to expire timestamp. |
|
31 | - * |
|
32 | - * @var string session expiration timestamp |
|
33 | - */ |
|
34 | - protected $_session_expiration; |
|
35 | - |
|
36 | - /** |
|
37 | - * True when the cookie exists. |
|
38 | - * |
|
39 | - * @var bool Based on whether a cookie exists. |
|
40 | - */ |
|
41 | - protected $_has_cookie = false; |
|
42 | - |
|
43 | - /** |
|
44 | - * Table name for session data. |
|
45 | - * |
|
46 | - * @var string Custom session table name |
|
47 | - */ |
|
48 | - protected $_table; |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructor for the session class. |
|
52 | - */ |
|
53 | - public function __construct() { |
|
54 | - |
|
55 | - $this->_cookie = apply_filters( 'wpinv_cookie', 'wpinv_session_' . COOKIEHASH ); |
|
15 | + /** |
|
16 | + * Cookie name used for the session. |
|
17 | + * |
|
18 | + * @var string cookie name |
|
19 | + */ |
|
20 | + protected $_cookie; |
|
21 | + |
|
22 | + /** |
|
23 | + * Stores session expiry. |
|
24 | + * |
|
25 | + * @var int session due to expire timestamp |
|
26 | + */ |
|
27 | + protected $_session_expiring; |
|
28 | + |
|
29 | + /** |
|
30 | + * Stores session due to expire timestamp. |
|
31 | + * |
|
32 | + * @var string session expiration timestamp |
|
33 | + */ |
|
34 | + protected $_session_expiration; |
|
35 | + |
|
36 | + /** |
|
37 | + * True when the cookie exists. |
|
38 | + * |
|
39 | + * @var bool Based on whether a cookie exists. |
|
40 | + */ |
|
41 | + protected $_has_cookie = false; |
|
42 | + |
|
43 | + /** |
|
44 | + * Table name for session data. |
|
45 | + * |
|
46 | + * @var string Custom session table name |
|
47 | + */ |
|
48 | + protected $_table; |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructor for the session class. |
|
52 | + */ |
|
53 | + public function __construct() { |
|
54 | + |
|
55 | + $this->_cookie = apply_filters( 'wpinv_cookie', 'wpinv_session_' . COOKIEHASH ); |
|
56 | 56 | add_action( 'init', array( $this, 'init' ), -1 ); |
57 | - add_action( 'wp_logout', array( $this, 'destroy_session' ) ); |
|
58 | - add_action( 'wp', array( $this, 'set_customer_session_cookie' ), 10 ); |
|
59 | - add_action( 'shutdown', array( $this, 'save_data' ), 20 ); |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Init hooks and session data. |
|
65 | - * |
|
66 | - * @since 3.3.0 |
|
67 | - */ |
|
68 | - public function init() { |
|
69 | - $this->init_session_cookie(); |
|
70 | - |
|
71 | - if ( ! is_user_logged_in() ) { |
|
72 | - add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ), 10, 2 ); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Setup cookie and customer ID. |
|
78 | - * |
|
79 | - * @since 3.6.0 |
|
80 | - */ |
|
81 | - public function init_session_cookie() { |
|
82 | - $cookie = $this->get_session_cookie(); |
|
83 | - |
|
84 | - if ( $cookie ) { |
|
85 | - $this->_customer_id = $cookie[0]; |
|
86 | - $this->_session_expiration = $cookie[1]; |
|
87 | - $this->_session_expiring = $cookie[2]; |
|
88 | - $this->_has_cookie = true; |
|
89 | - $this->_data = $this->get_session_data(); |
|
90 | - |
|
91 | - // If the user logs in, update session. |
|
92 | - if ( is_user_logged_in() && get_current_user_id() != $this->_customer_id ) { |
|
93 | - $this->_customer_id = get_current_user_id(); |
|
94 | - $this->_dirty = true; |
|
95 | - $this->save_data(); |
|
96 | - $this->set_customer_session_cookie( true ); |
|
97 | - } |
|
98 | - |
|
99 | - // Update session if its close to expiring. |
|
100 | - if ( time() > $this->_session_expiring ) { |
|
101 | - $this->set_session_expiration(); |
|
102 | - $this->update_session_timestamp( $this->_customer_id, $this->_session_expiration ); |
|
103 | - } |
|
104 | - } else { |
|
105 | - $this->set_session_expiration(); |
|
106 | - $this->_customer_id = $this->generate_customer_id(); |
|
107 | - $this->_data = $this->get_session_data(); |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Sets the session cookie on-demand (usually after adding an item to the cart). |
|
113 | - * |
|
114 | - * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. |
|
115 | - * |
|
116 | - * Warning: Cookies will only be set if this is called before the headers are sent. |
|
117 | - * |
|
118 | - * @param bool $set Should the session cookie be set. |
|
119 | - */ |
|
120 | - public function set_customer_session_cookie( $set ) { |
|
121 | - if ( $set ) { |
|
122 | - $to_hash = $this->_customer_id . '|' . $this->_session_expiration; |
|
123 | - $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
124 | - $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash; |
|
125 | - $this->_has_cookie = true; |
|
126 | - |
|
127 | - if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) { |
|
128 | - $this->setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true ); |
|
129 | - } |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){ |
|
57 | + add_action( 'wp_logout', array( $this, 'destroy_session' ) ); |
|
58 | + add_action( 'wp', array( $this, 'set_customer_session_cookie' ), 10 ); |
|
59 | + add_action( 'shutdown', array( $this, 'save_data' ), 20 ); |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Init hooks and session data. |
|
65 | + * |
|
66 | + * @since 3.3.0 |
|
67 | + */ |
|
68 | + public function init() { |
|
69 | + $this->init_session_cookie(); |
|
70 | + |
|
71 | + if ( ! is_user_logged_in() ) { |
|
72 | + add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ), 10, 2 ); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Setup cookie and customer ID. |
|
78 | + * |
|
79 | + * @since 3.6.0 |
|
80 | + */ |
|
81 | + public function init_session_cookie() { |
|
82 | + $cookie = $this->get_session_cookie(); |
|
83 | + |
|
84 | + if ( $cookie ) { |
|
85 | + $this->_customer_id = $cookie[0]; |
|
86 | + $this->_session_expiration = $cookie[1]; |
|
87 | + $this->_session_expiring = $cookie[2]; |
|
88 | + $this->_has_cookie = true; |
|
89 | + $this->_data = $this->get_session_data(); |
|
90 | + |
|
91 | + // If the user logs in, update session. |
|
92 | + if ( is_user_logged_in() && get_current_user_id() != $this->_customer_id ) { |
|
93 | + $this->_customer_id = get_current_user_id(); |
|
94 | + $this->_dirty = true; |
|
95 | + $this->save_data(); |
|
96 | + $this->set_customer_session_cookie( true ); |
|
97 | + } |
|
98 | + |
|
99 | + // Update session if its close to expiring. |
|
100 | + if ( time() > $this->_session_expiring ) { |
|
101 | + $this->set_session_expiration(); |
|
102 | + $this->update_session_timestamp( $this->_customer_id, $this->_session_expiration ); |
|
103 | + } |
|
104 | + } else { |
|
105 | + $this->set_session_expiration(); |
|
106 | + $this->_customer_id = $this->generate_customer_id(); |
|
107 | + $this->_data = $this->get_session_data(); |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Sets the session cookie on-demand (usually after adding an item to the cart). |
|
113 | + * |
|
114 | + * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. |
|
115 | + * |
|
116 | + * Warning: Cookies will only be set if this is called before the headers are sent. |
|
117 | + * |
|
118 | + * @param bool $set Should the session cookie be set. |
|
119 | + */ |
|
120 | + public function set_customer_session_cookie( $set ) { |
|
121 | + if ( $set ) { |
|
122 | + $to_hash = $this->_customer_id . '|' . $this->_session_expiration; |
|
123 | + $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
124 | + $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash; |
|
125 | + $this->_has_cookie = true; |
|
126 | + |
|
127 | + if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) { |
|
128 | + $this->setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true ); |
|
129 | + } |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){ |
|
134 | 134 | if ( ! headers_sent() ) { |
135 | 135 | setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters( 'wpinv_cookie_httponly', $httponly, $name, $value, $expire, $secure ) ); |
136 | 136 | } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
@@ -139,86 +139,86 @@ discard block |
||
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | - /** |
|
143 | - * Should the session cookie be secure? |
|
144 | - * |
|
145 | - * @since 3.6.0 |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - protected function use_secure_cookie() { |
|
142 | + /** |
|
143 | + * Should the session cookie be secure? |
|
144 | + * |
|
145 | + * @since 3.6.0 |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + protected function use_secure_cookie() { |
|
149 | 149 | $is_https = false !== strstr( get_option( 'home' ), 'https:' ); |
150 | - return apply_filters( 'wpinv_session_use_secure_cookie', $is_https && is_ssl() ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - public function has_session() { |
|
159 | - return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine. |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Set session expiration. |
|
164 | - */ |
|
165 | - public function set_session_expiration() { |
|
166 | - $this->_session_expiring = time() + intval( apply_filters( 'wpinv_session_expiring', 60 * 60 * 47 ) ); // 47 Hours. |
|
167 | - $this->_session_expiration = time() + intval( apply_filters( 'wpinv_session_expiration', 60 * 60 * 48 ) ); // 48 Hours. |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Generates session ids. |
|
172 | - * |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - public function generate_customer_id() { |
|
176 | - require_once ABSPATH . 'wp-includes/class-phpass.php'; |
|
177 | - $hasher = new PasswordHash( 8, false ); |
|
178 | - return md5( $hasher->get_random_bytes( 32 ) ); |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the session cookie, if set. Otherwise return false. |
|
183 | - * |
|
184 | - * Session cookies without a customer ID are invalid. |
|
185 | - * |
|
186 | - * @return bool|array |
|
187 | - */ |
|
188 | - public function get_session_cookie() { |
|
189 | - $cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine. |
|
190 | - |
|
191 | - if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) { |
|
192 | - return false; |
|
193 | - } |
|
194 | - |
|
195 | - list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value ); |
|
196 | - |
|
197 | - if ( empty( $customer_id ) ) { |
|
198 | - return false; |
|
199 | - } |
|
200 | - |
|
201 | - // Validate hash. |
|
202 | - $to_hash = $customer_id . '|' . $session_expiration; |
|
203 | - $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
204 | - |
|
205 | - if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) { |
|
206 | - return false; |
|
207 | - } |
|
208 | - |
|
209 | - return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash ); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Get session data. |
|
214 | - * |
|
215 | - * @return array |
|
216 | - */ |
|
217 | - public function get_session_data() { |
|
218 | - return $this->has_session() ? (array) $this->get_session( $this->_customer_id ) : array(); |
|
219 | - } |
|
220 | - |
|
221 | - public function generate_key($customer_id){ |
|
150 | + return apply_filters( 'wpinv_session_use_secure_cookie', $is_https && is_ssl() ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Return true if the current user has an active session, i.e. a cookie to retrieve values. |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + public function has_session() { |
|
159 | + return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine. |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Set session expiration. |
|
164 | + */ |
|
165 | + public function set_session_expiration() { |
|
166 | + $this->_session_expiring = time() + intval( apply_filters( 'wpinv_session_expiring', 60 * 60 * 47 ) ); // 47 Hours. |
|
167 | + $this->_session_expiration = time() + intval( apply_filters( 'wpinv_session_expiration', 60 * 60 * 48 ) ); // 48 Hours. |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Generates session ids. |
|
172 | + * |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + public function generate_customer_id() { |
|
176 | + require_once ABSPATH . 'wp-includes/class-phpass.php'; |
|
177 | + $hasher = new PasswordHash( 8, false ); |
|
178 | + return md5( $hasher->get_random_bytes( 32 ) ); |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the session cookie, if set. Otherwise return false. |
|
183 | + * |
|
184 | + * Session cookies without a customer ID are invalid. |
|
185 | + * |
|
186 | + * @return bool|array |
|
187 | + */ |
|
188 | + public function get_session_cookie() { |
|
189 | + $cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine. |
|
190 | + |
|
191 | + if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) { |
|
192 | + return false; |
|
193 | + } |
|
194 | + |
|
195 | + list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value ); |
|
196 | + |
|
197 | + if ( empty( $customer_id ) ) { |
|
198 | + return false; |
|
199 | + } |
|
200 | + |
|
201 | + // Validate hash. |
|
202 | + $to_hash = $customer_id . '|' . $session_expiration; |
|
203 | + $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
204 | + |
|
205 | + if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) { |
|
206 | + return false; |
|
207 | + } |
|
208 | + |
|
209 | + return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash ); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Get session data. |
|
214 | + * |
|
215 | + * @return array |
|
216 | + */ |
|
217 | + public function get_session_data() { |
|
218 | + return $this->has_session() ? (array) $this->get_session( $this->_customer_id ) : array(); |
|
219 | + } |
|
220 | + |
|
221 | + public function generate_key($customer_id){ |
|
222 | 222 | if(!$customer_id){ |
223 | 223 | return; |
224 | 224 | } |
@@ -226,68 +226,68 @@ discard block |
||
226 | 226 | return 'wpi_trans_'.$customer_id; |
227 | 227 | } |
228 | 228 | |
229 | - /** |
|
230 | - * Save data. |
|
231 | - */ |
|
232 | - public function save_data() { |
|
233 | - // Dirty if something changed - prevents saving nothing new. |
|
234 | - if ( $this->_dirty && $this->has_session() ) { |
|
229 | + /** |
|
230 | + * Save data. |
|
231 | + */ |
|
232 | + public function save_data() { |
|
233 | + // Dirty if something changed - prevents saving nothing new. |
|
234 | + if ( $this->_dirty && $this->has_session() ) { |
|
235 | 235 | |
236 | 236 | set_transient( $this->generate_key($this->_customer_id), $this->_data, $this->_session_expiration); |
237 | 237 | |
238 | - $this->_dirty = false; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Destroy all session data. |
|
244 | - */ |
|
245 | - public function destroy_session() { |
|
246 | - $this->delete_session( $this->_customer_id ); |
|
247 | - $this->forget_session(); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Forget all session data without destroying it. |
|
252 | - */ |
|
253 | - public function forget_session() { |
|
254 | - $this->setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true ); |
|
255 | - |
|
256 | - wpinv_empty_cart(); |
|
257 | - |
|
258 | - $this->_data = array(); |
|
259 | - $this->_dirty = false; |
|
260 | - $this->_customer_id = $this->generate_customer_id(); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * When a user is logged out, ensure they have a unique nonce by using the customer/session ID. |
|
265 | - * |
|
266 | - * @param int $uid User ID. |
|
267 | - * @return string |
|
268 | - */ |
|
269 | - public function nonce_user_logged_out( $uid ) { |
|
270 | - |
|
271 | - // Check if one of our nonces. |
|
272 | - if ( substr( $uid, 0, 5 ) === 'wpinv' || substr( $uid, 0, 7 ) === 'getpaid' ) { |
|
273 | - return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid; |
|
274 | - } |
|
275 | - |
|
276 | - return $uid; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Returns the session. |
|
281 | - * |
|
282 | - * @param string $customer_id Customer ID. |
|
283 | - * @param mixed $default Default session value. |
|
284 | - * @return string|array |
|
285 | - */ |
|
286 | - public function get_session( $customer_id, $default = false ) { |
|
287 | - |
|
288 | - if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
289 | - return array(); |
|
290 | - } |
|
238 | + $this->_dirty = false; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Destroy all session data. |
|
244 | + */ |
|
245 | + public function destroy_session() { |
|
246 | + $this->delete_session( $this->_customer_id ); |
|
247 | + $this->forget_session(); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Forget all session data without destroying it. |
|
252 | + */ |
|
253 | + public function forget_session() { |
|
254 | + $this->setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true ); |
|
255 | + |
|
256 | + wpinv_empty_cart(); |
|
257 | + |
|
258 | + $this->_data = array(); |
|
259 | + $this->_dirty = false; |
|
260 | + $this->_customer_id = $this->generate_customer_id(); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * When a user is logged out, ensure they have a unique nonce by using the customer/session ID. |
|
265 | + * |
|
266 | + * @param int $uid User ID. |
|
267 | + * @return string |
|
268 | + */ |
|
269 | + public function nonce_user_logged_out( $uid ) { |
|
270 | + |
|
271 | + // Check if one of our nonces. |
|
272 | + if ( substr( $uid, 0, 5 ) === 'wpinv' || substr( $uid, 0, 7 ) === 'getpaid' ) { |
|
273 | + return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid; |
|
274 | + } |
|
275 | + |
|
276 | + return $uid; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Returns the session. |
|
281 | + * |
|
282 | + * @param string $customer_id Customer ID. |
|
283 | + * @param mixed $default Default session value. |
|
284 | + * @return string|array |
|
285 | + */ |
|
286 | + public function get_session( $customer_id, $default = false ) { |
|
287 | + |
|
288 | + if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
289 | + return array(); |
|
290 | + } |
|
291 | 291 | |
292 | 292 | $key = $this->generate_key($customer_id); |
293 | 293 | $value = get_transient($key); |
@@ -296,30 +296,30 @@ discard block |
||
296 | 296 | $value = $default; |
297 | 297 | } |
298 | 298 | |
299 | - return maybe_unserialize( $value ); |
|
300 | - } |
|
299 | + return maybe_unserialize( $value ); |
|
300 | + } |
|
301 | 301 | |
302 | - /** |
|
303 | - * Delete the session from the cache and database. |
|
304 | - * |
|
305 | - * @param int $customer_id Customer ID. |
|
306 | - */ |
|
307 | - public function delete_session( $customer_id ) { |
|
302 | + /** |
|
303 | + * Delete the session from the cache and database. |
|
304 | + * |
|
305 | + * @param int $customer_id Customer ID. |
|
306 | + */ |
|
307 | + public function delete_session( $customer_id ) { |
|
308 | 308 | |
309 | 309 | $key = $this->generate_key($customer_id); |
310 | 310 | |
311 | - delete_transient($key); |
|
312 | - } |
|
311 | + delete_transient($key); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Update the session expiry timestamp. |
|
316 | - * |
|
317 | - * @param string $customer_id Customer ID. |
|
318 | - * @param int $timestamp Timestamp to expire the cookie. |
|
319 | - */ |
|
320 | - public function update_session_timestamp( $customer_id, $timestamp ) { |
|
314 | + /** |
|
315 | + * Update the session expiry timestamp. |
|
316 | + * |
|
317 | + * @param string $customer_id Customer ID. |
|
318 | + * @param int $timestamp Timestamp to expire the cookie. |
|
319 | + */ |
|
320 | + public function update_session_timestamp( $customer_id, $timestamp ) { |
|
321 | 321 | |
322 | 322 | set_transient( $this->generate_key($customer_id), maybe_serialize( $this->_data ), $timestamp); |
323 | 323 | |
324 | - } |
|
324 | + } |
|
325 | 325 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | |
8 | -defined( 'ABSPATH' ) || exit; |
|
8 | +defined('ABSPATH') || exit; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * Session handler class. |
@@ -52,11 +52,11 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function __construct() { |
54 | 54 | |
55 | - $this->_cookie = apply_filters( 'wpinv_cookie', 'wpinv_session_' . COOKIEHASH ); |
|
56 | - add_action( 'init', array( $this, 'init' ), -1 ); |
|
57 | - add_action( 'wp_logout', array( $this, 'destroy_session' ) ); |
|
58 | - add_action( 'wp', array( $this, 'set_customer_session_cookie' ), 10 ); |
|
59 | - add_action( 'shutdown', array( $this, 'save_data' ), 20 ); |
|
55 | + $this->_cookie = apply_filters('wpinv_cookie', 'wpinv_session_' . COOKIEHASH); |
|
56 | + add_action('init', array($this, 'init'), -1); |
|
57 | + add_action('wp_logout', array($this, 'destroy_session')); |
|
58 | + add_action('wp', array($this, 'set_customer_session_cookie'), 10); |
|
59 | + add_action('shutdown', array($this, 'save_data'), 20); |
|
60 | 60 | |
61 | 61 | } |
62 | 62 | |
@@ -68,8 +68,8 @@ discard block |
||
68 | 68 | public function init() { |
69 | 69 | $this->init_session_cookie(); |
70 | 70 | |
71 | - if ( ! is_user_logged_in() ) { |
|
72 | - add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ), 10, 2 ); |
|
71 | + if (!is_user_logged_in()) { |
|
72 | + add_filter('nonce_user_logged_out', array($this, 'nonce_user_logged_out'), 10, 2); |
|
73 | 73 | } |
74 | 74 | } |
75 | 75 | |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | public function init_session_cookie() { |
82 | 82 | $cookie = $this->get_session_cookie(); |
83 | 83 | |
84 | - if ( $cookie ) { |
|
84 | + if ($cookie) { |
|
85 | 85 | $this->_customer_id = $cookie[0]; |
86 | 86 | $this->_session_expiration = $cookie[1]; |
87 | 87 | $this->_session_expiring = $cookie[2]; |
@@ -89,17 +89,17 @@ discard block |
||
89 | 89 | $this->_data = $this->get_session_data(); |
90 | 90 | |
91 | 91 | // If the user logs in, update session. |
92 | - if ( is_user_logged_in() && get_current_user_id() != $this->_customer_id ) { |
|
92 | + if (is_user_logged_in() && get_current_user_id() != $this->_customer_id) { |
|
93 | 93 | $this->_customer_id = get_current_user_id(); |
94 | 94 | $this->_dirty = true; |
95 | 95 | $this->save_data(); |
96 | - $this->set_customer_session_cookie( true ); |
|
96 | + $this->set_customer_session_cookie(true); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | // Update session if its close to expiring. |
100 | - if ( time() > $this->_session_expiring ) { |
|
100 | + if (time() > $this->_session_expiring) { |
|
101 | 101 | $this->set_session_expiration(); |
102 | - $this->update_session_timestamp( $this->_customer_id, $this->_session_expiration ); |
|
102 | + $this->update_session_timestamp($this->_customer_id, $this->_session_expiration); |
|
103 | 103 | } |
104 | 104 | } else { |
105 | 105 | $this->set_session_expiration(); |
@@ -117,25 +117,25 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @param bool $set Should the session cookie be set. |
119 | 119 | */ |
120 | - public function set_customer_session_cookie( $set ) { |
|
121 | - if ( $set ) { |
|
120 | + public function set_customer_session_cookie($set) { |
|
121 | + if ($set) { |
|
122 | 122 | $to_hash = $this->_customer_id . '|' . $this->_session_expiration; |
123 | - $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
123 | + $cookie_hash = hash_hmac('md5', $to_hash, wp_hash($to_hash)); |
|
124 | 124 | $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash; |
125 | 125 | $this->_has_cookie = true; |
126 | 126 | |
127 | - if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) { |
|
128 | - $this->setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true ); |
|
127 | + if (!isset($_COOKIE[$this->_cookie]) || $_COOKIE[$this->_cookie] !== $cookie_value) { |
|
128 | + $this->setcookie($this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true); |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
133 | - public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){ |
|
134 | - if ( ! headers_sent() ) { |
|
135 | - setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters( 'wpinv_cookie_httponly', $httponly, $name, $value, $expire, $secure ) ); |
|
136 | - } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
|
137 | - headers_sent( $file, $line ); |
|
138 | - trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine |
|
133 | + public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false) { |
|
134 | + if (!headers_sent()) { |
|
135 | + setcookie($name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters('wpinv_cookie_httponly', $httponly, $name, $value, $expire, $secure)); |
|
136 | + } elseif (defined('WP_DEBUG') && WP_DEBUG) { |
|
137 | + headers_sent($file, $line); |
|
138 | + trigger_error("{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE); // @codingStandardsIgnoreLine |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
@@ -146,8 +146,8 @@ discard block |
||
146 | 146 | * @return bool |
147 | 147 | */ |
148 | 148 | protected function use_secure_cookie() { |
149 | - $is_https = false !== strstr( get_option( 'home' ), 'https:' ); |
|
150 | - return apply_filters( 'wpinv_session_use_secure_cookie', $is_https && is_ssl() ); |
|
149 | + $is_https = false !== strstr(get_option('home'), 'https:'); |
|
150 | + return apply_filters('wpinv_session_use_secure_cookie', $is_https && is_ssl()); |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | /** |
@@ -156,15 +156,15 @@ discard block |
||
156 | 156 | * @return bool |
157 | 157 | */ |
158 | 158 | public function has_session() { |
159 | - return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine. |
|
159 | + return isset($_COOKIE[$this->_cookie]) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine. |
|
160 | 160 | } |
161 | 161 | |
162 | 162 | /** |
163 | 163 | * Set session expiration. |
164 | 164 | */ |
165 | 165 | public function set_session_expiration() { |
166 | - $this->_session_expiring = time() + intval( apply_filters( 'wpinv_session_expiring', 60 * 60 * 47 ) ); // 47 Hours. |
|
167 | - $this->_session_expiration = time() + intval( apply_filters( 'wpinv_session_expiration', 60 * 60 * 48 ) ); // 48 Hours. |
|
166 | + $this->_session_expiring = time() + intval(apply_filters('wpinv_session_expiring', 60 * 60 * 47)); // 47 Hours. |
|
167 | + $this->_session_expiration = time() + intval(apply_filters('wpinv_session_expiration', 60 * 60 * 48)); // 48 Hours. |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -174,8 +174,8 @@ discard block |
||
174 | 174 | */ |
175 | 175 | public function generate_customer_id() { |
176 | 176 | require_once ABSPATH . 'wp-includes/class-phpass.php'; |
177 | - $hasher = new PasswordHash( 8, false ); |
|
178 | - return md5( $hasher->get_random_bytes( 32 ) ); |
|
177 | + $hasher = new PasswordHash(8, false); |
|
178 | + return md5($hasher->get_random_bytes(32)); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
@@ -186,27 +186,27 @@ discard block |
||
186 | 186 | * @return bool|array |
187 | 187 | */ |
188 | 188 | public function get_session_cookie() { |
189 | - $cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine. |
|
189 | + $cookie_value = isset($_COOKIE[$this->_cookie]) ? wp_unslash($_COOKIE[$this->_cookie]) : false; // @codingStandardsIgnoreLine. |
|
190 | 190 | |
191 | - if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) { |
|
191 | + if (empty($cookie_value) || !is_string($cookie_value)) { |
|
192 | 192 | return false; |
193 | 193 | } |
194 | 194 | |
195 | - list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value ); |
|
195 | + list($customer_id, $session_expiration, $session_expiring, $cookie_hash) = explode('||', $cookie_value); |
|
196 | 196 | |
197 | - if ( empty( $customer_id ) ) { |
|
197 | + if (empty($customer_id)) { |
|
198 | 198 | return false; |
199 | 199 | } |
200 | 200 | |
201 | 201 | // Validate hash. |
202 | 202 | $to_hash = $customer_id . '|' . $session_expiration; |
203 | - $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); |
|
203 | + $hash = hash_hmac('md5', $to_hash, wp_hash($to_hash)); |
|
204 | 204 | |
205 | - if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) { |
|
205 | + if (empty($cookie_hash) || !hash_equals($hash, $cookie_hash)) { |
|
206 | 206 | return false; |
207 | 207 | } |
208 | 208 | |
209 | - return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash ); |
|
209 | + return array($customer_id, $session_expiration, $session_expiring, $cookie_hash); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -215,15 +215,15 @@ discard block |
||
215 | 215 | * @return array |
216 | 216 | */ |
217 | 217 | public function get_session_data() { |
218 | - return $this->has_session() ? (array) $this->get_session( $this->_customer_id ) : array(); |
|
218 | + return $this->has_session() ? (array) $this->get_session($this->_customer_id) : array(); |
|
219 | 219 | } |
220 | 220 | |
221 | - public function generate_key($customer_id){ |
|
222 | - if(!$customer_id){ |
|
221 | + public function generate_key($customer_id) { |
|
222 | + if (!$customer_id) { |
|
223 | 223 | return; |
224 | 224 | } |
225 | 225 | |
226 | - return 'wpi_trans_'.$customer_id; |
|
226 | + return 'wpi_trans_' . $customer_id; |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
@@ -231,9 +231,9 @@ discard block |
||
231 | 231 | */ |
232 | 232 | public function save_data() { |
233 | 233 | // Dirty if something changed - prevents saving nothing new. |
234 | - if ( $this->_dirty && $this->has_session() ) { |
|
234 | + if ($this->_dirty && $this->has_session()) { |
|
235 | 235 | |
236 | - set_transient( $this->generate_key($this->_customer_id), $this->_data, $this->_session_expiration); |
|
236 | + set_transient($this->generate_key($this->_customer_id), $this->_data, $this->_session_expiration); |
|
237 | 237 | |
238 | 238 | $this->_dirty = false; |
239 | 239 | } |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | * Destroy all session data. |
244 | 244 | */ |
245 | 245 | public function destroy_session() { |
246 | - $this->delete_session( $this->_customer_id ); |
|
246 | + $this->delete_session($this->_customer_id); |
|
247 | 247 | $this->forget_session(); |
248 | 248 | } |
249 | 249 | |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * Forget all session data without destroying it. |
252 | 252 | */ |
253 | 253 | public function forget_session() { |
254 | - $this->setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true ); |
|
254 | + $this->setcookie($this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true); |
|
255 | 255 | |
256 | 256 | wpinv_empty_cart(); |
257 | 257 | |
@@ -266,10 +266,10 @@ discard block |
||
266 | 266 | * @param int $uid User ID. |
267 | 267 | * @return string |
268 | 268 | */ |
269 | - public function nonce_user_logged_out( $uid ) { |
|
269 | + public function nonce_user_logged_out($uid) { |
|
270 | 270 | |
271 | 271 | // Check if one of our nonces. |
272 | - if ( substr( $uid, 0, 5 ) === 'wpinv' || substr( $uid, 0, 7 ) === 'getpaid' ) { |
|
272 | + if (substr($uid, 0, 5) === 'wpinv' || substr($uid, 0, 7) === 'getpaid') { |
|
273 | 273 | return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid; |
274 | 274 | } |
275 | 275 | |
@@ -283,20 +283,20 @@ discard block |
||
283 | 283 | * @param mixed $default Default session value. |
284 | 284 | * @return string|array |
285 | 285 | */ |
286 | - public function get_session( $customer_id, $default = false ) { |
|
286 | + public function get_session($customer_id, $default = false) { |
|
287 | 287 | |
288 | - if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
288 | + if (defined('WP_SETUP_CONFIG')) { |
|
289 | 289 | return array(); |
290 | 290 | } |
291 | 291 | |
292 | 292 | $key = $this->generate_key($customer_id); |
293 | 293 | $value = get_transient($key); |
294 | 294 | |
295 | - if ( !$value ) { |
|
295 | + if (!$value) { |
|
296 | 296 | $value = $default; |
297 | 297 | } |
298 | 298 | |
299 | - return maybe_unserialize( $value ); |
|
299 | + return maybe_unserialize($value); |
|
300 | 300 | } |
301 | 301 | |
302 | 302 | /** |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * |
305 | 305 | * @param int $customer_id Customer ID. |
306 | 306 | */ |
307 | - public function delete_session( $customer_id ) { |
|
307 | + public function delete_session($customer_id) { |
|
308 | 308 | |
309 | 309 | $key = $this->generate_key($customer_id); |
310 | 310 | |
@@ -317,9 +317,9 @@ discard block |
||
317 | 317 | * @param string $customer_id Customer ID. |
318 | 318 | * @param int $timestamp Timestamp to expire the cookie. |
319 | 319 | */ |
320 | - public function update_session_timestamp( $customer_id, $timestamp ) { |
|
320 | + public function update_session_timestamp($customer_id, $timestamp) { |
|
321 | 321 | |
322 | - set_transient( $this->generate_key($customer_id), maybe_serialize( $this->_data ), $timestamp); |
|
322 | + set_transient($this->generate_key($customer_id), maybe_serialize($this->_data), $timestamp); |
|
323 | 323 | |
324 | 324 | } |
325 | 325 | } |