PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/class-lp-session-handler.php +98 -306 4.2.04.4.8 View file →
@@ -23,9 +23,9 @@
23 23
24 24 /**
25 25 * @var string cookie name
26 26 */
27 - private $_cookie = '';
27 + private $_cookie = 'lp_session_guest';
28 28
29 29 /**
30 30 * @var int session expiration timestamp
31 31 */
@@ -31,110 +31,29 @@
31 31 */
32 32 private $_session_expiration = 0;
33 33
34 34 /**
35 - * $var bool based on whether a cookie exists
36 - * @deprecated 4.2.0
37 - */
38 - private $_has_cookie = false;
39 -
40 - /**
41 - * @since 3.2.2
42 - *
43 - * @var bool
44 - */
45 - private $_has_browser_cookie = true;
46 -
47 - /**
48 - * @var string Custom session table name
49 - * @deprecated 4.2.0
50 - */
51 - private $_table;
52 -
53 - /**
54 35 * @var null
55 36 */
56 37 private static $_instance = null;
57 38
58 39 /**
59 - * __get function.
60 - *
61 - * @param mixed $key
62 - *
63 - * @return mixed
64 - * @deprecated 4.2.0
65 - * Addon Stripe, 2Checkout, Authorize, Certificate is using this method via call session->order_awaiting_payment
66 - * After change all to session->set('order_awaiting_payment') we can remove this method.
67 - */
68 - public function __get( $key ) {
69 - _deprecated_function( __METHOD__, '4.2.0' );
70 - //return $this->get( $key );
71 - }
72 -
73 - /**
74 - * __set function.
75 - *
76 - * @param mixed $key
77 - * @param mixed $value
78 - * @deprecated 4.2.0
79 - * Addon Stripe, 2Checkout, Authorize, Certificate is using this method via call session->order_awaiting_payment
80 - * After change all to session->set('order_awaiting_payment') we can remove this method.
81 - */
82 - public function __set( $key, $value ) {
83 - _deprecated_function( 'LP_Session::__set', '4.2.0' );
84 - // if ( $key === 'order_awaiting_payment' ) {
85 -
86 - // }
87 - //$this->set( $key, $value );
88 - }
89 -
90 - /**
91 - * __isset function.
92 - *
93 - * @param mixed $key
94 - *
95 - * @return bool
96 - * @deprecated 4.2.0
97 - */
98 - public function __isset( $key ) {
99 - _deprecated_function( __METHOD__, '4.2.0' );
100 - //return isset( $this->_data[ sanitize_key( $key ) ] );
101 - }
102 -
103 - /**
104 - * __unset function.
105 - *
106 - * @param mixed $key
107 - * @deprecated 4.2.0
108 - */
109 - public function __unset( $key ) {
110 - _deprecated_function( __METHOD__, '4.2.0' );
111 - if ( isset( $this->_data[ $key ] ) ) {
112 - unset( $this->_data[ $key ] );
113 - $this->_changed = true;
114 - }
115 - }
116 -
117 - //protected $schedule_id = 'learn-press/clear-expired-session';
118 -
119 - /**
120 40 * LP_Session_Handler constructor.
121 41 *
122 42 * @version 3.2.2
123 43 */
124 44 protected function __construct() {
45 + $this->init_hooks();
46 + if ( is_admin() ) {
47 + return;
48 + }
49 +
125 50 $this->init();
126 - $this->init_hooks();
127 51 }
128 52
129 53 protected function init_hooks() {
130 54 add_action( 'wp_login', [ $this, 'handle_when_user_login_success' ], 10, 2 );
131 55 add_action( 'wp_logout', array( $this, 'destroy_session' ) );
132 - //add_action( 'learn_press_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
133 - //add_action( 'learn_press_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
134 - //add_action( 'shutdown', array( $this, 'save_data' ), 20 );
135 - //add_action( 'wp', array( $this, 'schedule_event' ) );
136 - //add_action( $this->schedule_id, array( $this, 'cleanup_sessions' ), 10 );
137 56 }
138 57
139 58 /**
140 59 * Set COOKIE for only user guest
@@ -141,32 +60,31 @@
141 60 * Set data: customer_id, session_expiration
142 61 *
143 62 * @return self
144 63 * @since 3.0.0
145 - * @version 4.0.1
64 + * @version 4.0.3
146 65 * @modify Tungnx
147 66 */
148 67 protected function init(): LP_Session_Handler {
149 - $expire_time_for_guest = 2 * DAY_IN_SECONDS;
68 + $expire_time_for_guest = DAY_IN_SECONDS;
150 69 $expire_time_for_user = 6 * DAY_IN_SECONDS;
151 70
152 71 // Set data for user Guest.
153 72 if ( ! is_user_logged_in() ) { // Generate data and set cookie for guest
154 - $this->_cookie = '_learn_press_session_' . COOKIEHASH;
155 - $cookie = $this->get_cookie_data();
156 - // If cookie exists, set data from cookie for guest
157 - if ( ! empty( $cookie ) ) {
158 - $this->_customer_id = (string) $cookie[0];
159 - $this->_session_expiration = (int) $cookie[1];
160 - //$this->_has_cookie = true;
161 - if ( time() > $this->_session_expiration - HOUR_IN_SECONDS ) {
162 - $this->set_session_expiration( $expire_time_for_guest );
163 - $this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
73 + $this->set_session_expiration( $expire_time_for_guest );
74 +
75 + if ( LP_Settings::is_store_ip_customer() ) {
76 + $this->_customer_id = LP_Helper::get_client_ip();
77 + } else { // Store data in COOKIE
78 + $cookie = $this->get_cookie_data();
79 + // If cookie exists, set data from cookie for guest
80 + if ( empty( $cookie ) ) {
81 + // Create new cookie and session for user Guest.
82 + $this->_customer_id = apply_filters( 'lp/cookie/guest-id', 'g-' . uniqid() );
83 + $this->set_customer_session_cookie();
84 + } else {
85 + $this->_customer_id = $cookie;
164 86 }
165 - } else { // Create new cookie and session for user Guest.
166 - $this->set_session_expiration( $expire_time_for_guest );
167 - $this->_customer_id = $this->generate_guest_id();
168 - $this->set_customer_session_cookie();
169 87 }
170 88 } else { // Set data for user logged.
171 89 $this->set_session_expiration( $expire_time_for_user );
172 90 $this->_customer_id = get_current_user_id();
@@ -171,9 +89,10 @@
171 89 $this->set_session_expiration( $expire_time_for_user );
172 90 $this->_customer_id = get_current_user_id();
173 91 }
174 92
175 - //$this->_data = $this->get_session_data();
93 + // Get session data from DB.
94 + $this->_data = $this->get_session_data();
176 95
177 96 return $this;
178 97 }
179 98
@@ -185,11 +104,15 @@
185 104 *
186 105 * @return void
187 106 */
188 107 public function handle_when_user_login_success( $user_name, $user ) {
108 + // Remove COOKIE for user guest.
109 + learn_press_remove_cookie( $this->_cookie );
110 +
189 111 /**
190 112 * Must set wp_set_current_user to get_current_user_id and is_user_logged_in work correctly.
191 - * Don't know why WP 6.3 and lower run wrong.
113 + * Because WP note must do that.
114 + * Read more @see wp_signon
192 115 * If version WP after run correctly, remove wp_set_current_user.
193 116 */
194 117 wp_set_current_user( $user->ID );
195 118 $user_id = get_current_user_id();
@@ -200,9 +123,9 @@
200 123 if ( $user_id ) {
201 124 $customer_id = $this->get_customer_id();
202 125 $user_before = get_user_by( 'ID', $customer_id );
203 126 if ( ! $user_before ) {
204 - $this->delete_session( $customer_id . '' );
127 + $this->delete_session( $customer_id );
205 128 }
206 129
207 130 $this->_customer_id = $user_id;
208 131 }
@@ -207,14 +130,8 @@
207 130 $this->_customer_id = $user_id;
208 131 }
209 132 }
210 133
211 - /*public function schedule_event() {
212 - if ( ! wp_next_scheduled( $this->schedule_id ) ) {
213 - wp_schedule_event( time(), 'hourly', $this->schedule_id );
214 - }
215 - }*/
216 -
217 134 /**
218 135 * Set cookie for user guest.
219 136 *
220 137 * @return LP_Session_Handler
@@ -219,16 +136,11 @@
219 136 *
220 137 * @return LP_Session_Handler
221 138 */
222 139 public function set_customer_session_cookie(): LP_Session_Handler {
223 - $to_hash = $this->_customer_id . '|' . $this->_session_expiration;
224 - $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
225 - $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $cookie_hash;
226 - //$this->_has_cookie = true;
227 -
228 140 // Set the cookie
229 - if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) {
230 - learn_press_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration );
141 + if ( ! isset( $_COOKIE[ $this->_cookie ] ) ) {
142 + learn_press_setcookie( $this->_cookie, $this->_customer_id, $this->_session_expiration );
231 143 }
232 144
233 145 return $this;
234 146 }
@@ -233,25 +145,8 @@
233 145 return $this;
234 146 }
235 147
236 148 /**
237 - * @deprecated 4.2.0
238 - */
239 - public function has_cookie() {
240 - _deprecated_function( __METHOD__, '4.2.0' );
241 - //return $this->_has_cookie && $this->_has_browser_cookie;
242 - }
243 -
244 - /**
245 - * Check has session.
246 - *
247 - * @return bool
248 - */
249 - public function has_session(): bool {
250 - return isset( $_COOKIE[ $this->_cookie ] ) || is_user_logged_in();
251 - }
252 -
253 - /**
254 149 * Set session expiration.
255 150 *
256 151 * @param int $duration
257 152 *
@@ -263,38 +158,18 @@
263 158 return $this;
264 159 }
265 160
266 161 /**
267 - * Generate string customer id for guest
162 + * Get cookie of guest.
268 163 *
269 164 * @return string
270 165 */
271 - public function generate_guest_id(): string {
272 - require_once ABSPATH . 'wp-includes/class-phpass.php';
273 - $hasher = new PasswordHash( 12, false );
274 -
275 - return md5( $hasher->get_random_bytes( 32 ) );
276 - }
277 -
278 - /**
279 - * Get cookie of guest.
280 - *
281 - * @return array
282 - */
283 - public function get_cookie_data(): array {
166 + public function get_cookie_data(): string {
284 167 if ( empty( $_COOKIE[ $this->_cookie ] ) || ! is_string( $_COOKIE[ $this->_cookie ] ) ) {
285 - return [];
168 + return '';
286 169 }
287 170
288 - list( $customer_id, $session_expiration, $cookie_hash ) = explode( '||', $_COOKIE[ $this->_cookie ] );
289 -
290 - $to_hash = $customer_id . '|' . $session_expiration;
291 - $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
292 - if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) {
293 - return [];
294 - }
295 -
296 - return array( $customer_id, $session_expiration, $cookie_hash );
171 + return $_COOKIE[ $this->_cookie ];
297 172 }
298 173
299 174 /**
300 175 * Get session data
@@ -311,66 +186,55 @@
311 186 return (array) $this->get_session_by_customer_id( (string) $customer_id );
312 187 }
313 188
314 189 /**
315 - * Increment group cache prefix (invalidates cache).
316 - *
317 - * @param string $group
318 - * @deprecated 4.2.0
319 - */
320 - public function incr_cache_prefix( $group ) {
321 - _deprecated_function( __METHOD__, '4.2.0' );
322 - //wp_cache_incr( 'learn_press_' . $group . '_cache_prefix', 1, $group );
323 - }
324 -
325 - /**
326 190 * Save session data to the database.
327 191 *
328 192 * @return bool
193 + * @version 4.0.1
194 + * @since 3.0.0
329 195 */
330 196 public function save_data(): bool {
331 197 $res = false;
332 198
333 199 try {
334 - if ( $this->_changed && $this->has_session() ) {
335 - $lp_session_db = LP_Sessions_DB::getInstance();
200 + $lp_session_db = LP_Sessions_DB::getInstance();
336 201
337 - // Check exists on DB.
338 - $filter = new LP_Session_filter();
339 - $filter->collection = $lp_session_db->tb_lp_sessions;
340 - $filter->field_count = 'session_id';
341 - $filter->limit = 1;
342 - $filter->where[] = $lp_session_db->wpdb->prepare( 'AND session_key = %s', $this->_customer_id );
343 - $get = $lp_session_db->execute( $filter );
202 + // Check exists on DB.
203 + $filter = new LP_Session_filter();
204 + $filter->collection = $lp_session_db->tb_lp_sessions;
205 + $filter->field_count = 'session_id';
206 + $filter->limit = 1;
207 + $filter->where[] = $lp_session_db->wpdb->prepare( 'AND session_key = %s', $this->_customer_id );
208 + $get = $lp_session_db->execute( $filter );
344 209
345 - $data = [
346 - 'session_key' => (string) $this->_customer_id,
347 - 'session_value' => maybe_serialize( $this->_data ),
348 - 'session_expiry' => $this->_session_expiration,
349 - ];
350 - if ( ! empty( $get ) ) {
351 - // Update
352 - $lp_session_db->wpdb->update(
353 - $lp_session_db->tb_lp_sessions,
354 - $data,
355 - [ 'session_key' => $this->_customer_id ],
356 - [ '%s', '%s', '%d' ],
357 - [ '%s' ]
358 - );
359 - // Clear cache.
360 - LP_Session_Cache::instance()->clear( $this->_customer_id );
361 - } else {
362 - // Insert
363 - $lp_session_db->wpdb->insert(
364 - $lp_session_db->tb_lp_sessions,
365 - $data,
366 - [ '%s', '%s', '%d' ]
367 - );
368 - }
210 + $data = [
211 + 'session_key' => (string) $this->_customer_id,
212 + 'session_value' => maybe_serialize( $this->_data ),
213 + 'session_expiry' => $this->_session_expiration,
214 + ];
215 + if ( ! empty( $get ) ) {
216 + // Update
217 + $lp_session_db->wpdb->update(
218 + $lp_session_db->tb_lp_sessions,
219 + $data,
220 + [ 'session_key' => $this->_customer_id ],
221 + [ '%s', '%s', '%d' ],
222 + [ '%s' ]
223 + );
224 + } else {
225 + // Insert
226 + $lp_session_db->wpdb->insert(
227 + $lp_session_db->tb_lp_sessions,
228 + $data,
229 + [ '%s', '%s', '%d' ]
230 + );
231 + }
369 232
370 - $this->_changed = false;
371 - $res = true;
372 - }
233 + // Clear cache.
234 + LP_Session_Cache::instance()->clear( $this->_customer_id );
235 +
236 + $res = true;
373 237 } catch ( Throwable $e ) {
374 238 error_log( $e->getMessage() );
375 239 }
376 240
@@ -380,13 +244,8 @@
380 244 /**
381 245 * Destroy session.
382 246 */
383 247 public function destroy_session() {
384 - /*$id = $this->get( 'temp_user' );
385 - if ( $id ) {
386 - delete_user_meta( $id, '_lp_expiration' );
387 - }*/
388 -
389 248 // Clear cookie.
390 249 if ( ! empty( $this->_cookie ) ) {
391 250 learn_press_remove_cookie( $this->_cookie );
392 251 }
@@ -453,12 +312,15 @@
453 312 if ( $session !== false ) {
454 313 return $session;
455 314 }
456 315
457 - $filter = new LP_Session_Filter();
458 - $filter->session_key = $customer_id;
459 - $filter->limit = 1;
460 - $res = $lp_session_db->get_sessions( $filter );
316 + $filter = new LP_Session_Filter();
317 + $filter->session_key = $customer_id;
318 + $filter->only_fields = [ 'session_key', 'session_value' ];
319 + $filter->field_count = 'session_key';
320 + $filter->run_query_count = false;
321 + $filter->limit = 1;
322 + $res = $lp_session_db->get_sessions( $filter );
461 323 if ( ! empty( $res ) ) {
462 324 $session = $res[0]->session_value;
463 325 }
464 326
@@ -499,48 +361,29 @@
499 361 return $rs;
500 362 }
501 363
502 364 /**
503 - * Get session id.
365 + * Get session value.
504 366 *
505 - * @param $customer_id
506 - * @param $timestamp
367 + * @param string $key
368 + * @param mixed $default
507 369 *
508 - * @return bool
370 + * @return mixed|null
509 371 */
510 - public function update_session_timestamp( $customer_id, $timestamp ): bool {
511 - global $wpdb;
512 - $res = true;
513 -
514 - try {
515 - $wpdb->update(
516 - LP_Sessions_DB::getInstance()->tb_lp_sessions,
517 - [ 'session_expiry' => $timestamp ],
518 - [ 'session_key' => $customer_id ],
519 - [ '%d' ]
520 - );
521 - // Clear cache.
522 - LP_Session_Cache::instance()->clear( $customer_id );
523 - } catch ( Throwable $e ) {
524 - $res = false;
525 - error_log( $e->getMessage() );
526 - }
527 -
528 - return $res;
372 + public function get( string $key, $default = null ) {
373 + return isset( $this->_data[ $key ] ) ? LP_Helper::maybe_unserialize( $this->_data[ $key ] ) : $default;
529 374 }
530 375
531 376 /**
532 - * Remove a value from session by key.
377 + * Set session value.
533 378 *
534 379 * @param string $key
535 - * @param bool $force_change
380 + * @param mixed $value
381 + * @param bool $force_change
536 382 */
537 - public function remove( $key, $force_change = false ) {
538 - if ( ! array_key_exists( $key, $this->_data ) ) {
539 - return;
540 - }
541 - unset( $this->_data[ $key ] );
542 - $this->_changed = true;
383 + public function set( string $key, $value, bool $force_change = false ) {
384 + $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value );
385 +
543 386 if ( $force_change ) {
544 387 $this->save_data();
545 388 }
546 389 }
@@ -545,36 +388,21 @@
545 388 }
546 389 }
547 390
548 391 /**
549 - * Get session value.
392 + * Remove a value from session by key.
550 393 *
551 394 * @param string $key
552 - * @param mixed $default
553 - *
554 - * @return mixed|null
555 - */
556 - public function get( $key, $default = null ) {
557 - $key = sanitize_key( $key );
558 -
559 - return isset( $this->_data[ $key ] ) ? LP_Helper::maybe_unserialize( $this->_data[ $key ] ) : $default;
560 - }
561 -
562 - /**
563 - * Set session value.
564 - *
565 - * @param string $key
566 - * @param mixed $value
567 395 * @param bool $force_change
568 396 */
569 - public function set( $key, $value, $force_change = false ) {
570 - if ( $value !== $this->get( $key ) ) {
571 - $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value );
572 - $this->_changed = true;
397 + public function remove( string $key, bool $force_change = false ) {
398 + if ( ! array_key_exists( $key, $this->_data ) ) {
399 + return;
400 + }
401 + unset( $this->_data[ $key ] );
573 402
574 - if ( $force_change ) {
575 - $this->save_data();
576 - }
403 + if ( $force_change ) {
404 + $this->save_data();
577 405 }
578 406 }
579 407
580 408 /**
@@ -583,12 +411,8 @@
583 411 public function get_customer_id(): string {
584 412 return (string) $this->_customer_id;
585 413 }
586 414
587 - public function get_session_expiration() {
588 - return $this->_session_expiration;
589 - }
590 -
591 415 public static function instance() {
592 416 if ( is_null( self::$_instance ) ) {
593 417 self::$_instance = new self();
594 418 }
@@ -593,40 +417,8 @@
593 417 self::$_instance = new self();
594 418 }
595 419
596 420 return self::$_instance;
597 - }
598 -
599 - /**
600 - * @deprecated 4.2.0
601 - */
602 - public function offsetExists( $offset ) {
603 - _deprecated_function( __METHOD__, '4.2.0' );
604 - return array_key_exists( $offset, $this->_data );
605 - }
606 -
607 - /**
608 - * @deprecated 4.2.0
609 - */
610 - public function offsetGet( $offset ) {
611 - _deprecated_function( __METHOD__, '4.2.0' );
612 - //return $this->get( $offset );
613 - }
614 -
615 - /**
616 - * @deprecated 4.2.0
617 - */
618 - public function offsetUnset( $offset ) {
619 - _deprecated_function( __METHOD__, '4.2.0' );
620 - //$this->remove( $offset );
621 - }
622 -
623 - /**
624 - * @deprecated 4.2.0
625 - */
626 - public function offsetSet( $offset, $value ) {
627 - _deprecated_function( __METHOD__, '4.2.0' );
628 - //$this->set( $offset, $value );
629 421 }
630 422 }
631 423
632 424 /**