| @@ -1,440 +1,440 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class LP_Session_Handler | |
| 5 | - * | |
| 6 | - * Only set COOKIE for user guest | |
| 7 | - */ | |
| 8 | -class LP_Session_Handler { | |
| 9 | - /** | |
| 10 | - * @var string $_customer_id | |
| 11 | - */ | |
| 12 | - public $_customer_id = ''; | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * @var array $_data | |
| 16 | - */ | |
| 17 | - protected $_data = array(); | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * @var bool $_changed | |
| 21 | - */ | |
| 22 | - protected $_changed = false; | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * @var string cookie name | |
| 26 | - */ | |
| 27 | - private $_cookie = 'lp_session_guest'; | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * @var int session expiration timestamp | |
| 31 | - */ | |
| 32 | - private $_session_expiration = 0; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * @var null | |
| 36 | - */ | |
| 37 | - private static $_instance = null; | |
| 38 | - | |
| 39 | - /** | |
| 40 | - * LP_Session_Handler constructor. | |
| 41 | - * | |
| 42 | - * @version 3.2.2 | |
| 43 | - */ | |
| 44 | - protected function __construct() { | |
| 45 | - $this->init_hooks(); | |
| 46 | - if ( is_admin() ) { | |
| 47 | - return; | |
| 48 | - } | |
| 49 | - | |
| 50 | - $this->init(); | |
| 51 | - } | |
| 52 | - | |
| 53 | - protected function init_hooks() { | |
| 54 | - add_action( 'wp_login', [ $this, 'handle_when_user_login_success' ], 10, 2 ); | |
| 55 | - add_action( 'wp_logout', array( $this, 'destroy_session' ) ); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Set COOKIE for only user guest | |
| 60 | - * Set data: customer_id, session_expiration | |
| 61 | - * | |
| 62 | - * @return self | |
| 63 | - * @since 3.0.0 | |
| 64 | - * @version 4.0.3 | |
| 65 | - * @modify Tungnx | |
| 66 | - */ | |
| 67 | - protected function init(): LP_Session_Handler { | |
| 68 | - $expire_time_for_guest = DAY_IN_SECONDS; | |
| 69 | - $expire_time_for_user = 6 * DAY_IN_SECONDS; | |
| 70 | - | |
| 71 | - // Set data for user Guest. | |
| 72 | - if ( ! is_user_logged_in() ) { // Generate data and set cookie for guest | |
| 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; | |
| 86 | - } | |
| 87 | - } | |
| 88 | - } else { // Set data for user logged. | |
| 89 | - $this->set_session_expiration( $expire_time_for_user ); | |
| 90 | - $this->_customer_id = get_current_user_id(); | |
| 91 | - } | |
| 92 | - | |
| 93 | - // Get session data from DB. | |
| 94 | - $this->_data = $this->get_session_data(); | |
| 95 | - | |
| 96 | - return $this; | |
| 97 | - } | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * Handle when user logged in. | |
| 101 | - * | |
| 102 | - * @param $user_name | |
| 103 | - * @param $user | |
| 104 | - * | |
| 105 | - * @return void | |
| 106 | - */ | |
| 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 | - | |
| 111 | - /** | |
| 112 | - * Must set wp_set_current_user to get_current_user_id and is_user_logged_in work correctly. | |
| 113 | - * Because WP note must do that. | |
| 114 | - * Read more @see wp_signon | |
| 115 | - * If version WP after run correctly, remove wp_set_current_user. | |
| 116 | - */ | |
| 117 | - wp_set_current_user( $user->ID ); | |
| 118 | - $user_id = get_current_user_id(); | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Delete session of user guest before. | |
| 122 | - */ | |
| 123 | - if ( $user_id ) { | |
| 124 | - $customer_id = $this->get_customer_id(); | |
| 125 | - $user_before = get_user_by( 'ID', $customer_id ); | |
| 126 | - if ( ! $user_before ) { | |
| 127 | - $this->delete_session( $customer_id ); | |
| 128 | - } | |
| 129 | - | |
| 130 | - $this->_customer_id = $user_id; | |
| 131 | - } | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Set cookie for user guest. | |
| 136 | - * | |
| 137 | - * @return LP_Session_Handler | |
| 138 | - */ | |
| 139 | - public function set_customer_session_cookie(): LP_Session_Handler { | |
| 140 | - // Set the cookie | |
| 141 | - if ( ! isset( $_COOKIE[ $this->_cookie ] ) ) { | |
| 142 | - learn_press_setcookie( $this->_cookie, $this->_customer_id, $this->_session_expiration ); | |
| 143 | - } | |
| 144 | - | |
| 145 | - return $this; | |
| 146 | - } | |
| 147 | - | |
| 148 | - /** | |
| 149 | - * Set session expiration. | |
| 150 | - * | |
| 151 | - * @param int $duration | |
| 152 | - * | |
| 153 | - * @return LP_Session_Handler | |
| 154 | - */ | |
| 155 | - public function set_session_expiration( int $duration = 0 ): LP_Session_Handler { | |
| 156 | - $this->_session_expiration = time() + $duration; | |
| 157 | - | |
| 158 | - return $this; | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Get cookie of guest. | |
| 163 | - * | |
| 164 | - * @return string | |
| 165 | - */ | |
| 166 | - public function get_cookie_data(): string { | |
| 167 | - if ( empty( $_COOKIE[ $this->_cookie ] ) || ! is_string( $_COOKIE[ $this->_cookie ] ) ) { | |
| 168 | - return ''; | |
| 169 | - } | |
| 170 | - | |
| 171 | - return $_COOKIE[ $this->_cookie ]; | |
| 172 | - } | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * Get session data | |
| 176 | - * | |
| 177 | - * @return array | |
| 178 | - */ | |
| 179 | - public function get_session_data(): array { | |
| 180 | - if ( is_user_logged_in() ) { | |
| 181 | - $customer_id = get_current_user_id(); | |
| 182 | - } else { | |
| 183 | - $customer_id = $this->get_customer_id(); | |
| 184 | - } | |
| 185 | - | |
| 186 | - return (array) $this->get_session_by_customer_id( (string) $customer_id ); | |
| 187 | - } | |
| 188 | - | |
| 189 | - /** | |
| 190 | - * Save session data to the database. | |
| 191 | - * | |
| 192 | - * @return bool | |
| 193 | - * @version 4.0.1 | |
| 194 | - * @since 3.0.0 | |
| 195 | - */ | |
| 196 | - public function save_data(): bool { | |
| 197 | - $res = false; | |
| 198 | - | |
| 199 | - try { | |
| 200 | - $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 201 | - | |
| 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 ); | |
| 209 | - | |
| 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 | - } | |
| 232 | - | |
| 233 | - // Clear cache. | |
| 234 | - LP_Session_Cache::instance()->clear( $this->_customer_id ); | |
| 235 | - | |
| 236 | - $res = true; | |
| 237 | - } catch ( Throwable $e ) { | |
| 238 | - error_log( $e->getMessage() ); | |
| 239 | - } | |
| 240 | - | |
| 241 | - return $res; | |
| 242 | - } | |
| 243 | - | |
| 244 | - /** | |
| 245 | - * Destroy session. | |
| 246 | - */ | |
| 247 | - public function destroy_session() { | |
| 248 | - // Clear cookie. | |
| 249 | - if ( ! empty( $this->_cookie ) ) { | |
| 250 | - learn_press_remove_cookie( $this->_cookie ); | |
| 251 | - } | |
| 252 | - | |
| 253 | - // Clear session expire. | |
| 254 | - $this->cleanup_sessions_expire(); | |
| 255 | - | |
| 256 | - $logout_redirect_page_id = LP_Settings::get_option( 'logout_redirect_page_id' ); | |
| 257 | - if ( $logout_redirect_page_id ) { | |
| 258 | - wp_safe_redirect( get_the_permalink( $logout_redirect_page_id ) ); | |
| 259 | - die; | |
| 260 | - } | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | - * Clear session expired. | |
| 265 | - * | |
| 266 | - * @return bool | |
| 267 | - */ | |
| 268 | - public function cleanup_sessions_expire(): bool { | |
| 269 | - $res = true; | |
| 270 | - $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 271 | - | |
| 272 | - try { | |
| 273 | - // Get session expired. | |
| 274 | - $filter_get = new LP_Session_Filter(); | |
| 275 | - $filter_get->where[] = $lp_session_db->wpdb->prepare( 'AND session_expiry < %d', time() ); | |
| 276 | - $sessions = $lp_session_db->get_sessions( $filter_get ); | |
| 277 | - // Clear cache. | |
| 278 | - foreach ( $sessions as $session ) { | |
| 279 | - LP_Session_Cache::instance()->clear( $session->session_key ); | |
| 280 | - } | |
| 281 | - | |
| 282 | - // Delete session expired. | |
| 283 | - $filter = new LP_Session_Filter(); | |
| 284 | - $filter->collection = $lp_session_db->tb_lp_sessions; | |
| 285 | - $filter->where[] = $lp_session_db->wpdb->prepare( 'AND session_expiry < %d', time() ); | |
| 286 | - $lp_session_db->delete_execute( $filter ); | |
| 287 | - } catch ( Throwable $e ) { | |
| 288 | - $res = false; | |
| 289 | - error_log( $e->getMessage() ); | |
| 290 | - } | |
| 291 | - | |
| 292 | - return $res; | |
| 293 | - } | |
| 294 | - | |
| 295 | - /** | |
| 296 | - * Get session on DB. | |
| 297 | - * | |
| 298 | - * @param string $customer_id | |
| 299 | - * | |
| 300 | - * @return false|mixed|string | |
| 301 | - * @Todo - Tungnx should handle data when save type json instead serialize. | |
| 302 | - */ | |
| 303 | - public function get_session_by_customer_id( string $customer_id = '' ) { | |
| 304 | - $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 305 | - $session = []; | |
| 306 | - | |
| 307 | - try { | |
| 308 | - // Get cache. | |
| 309 | - $session_cache = LP_Session_Cache::instance(); | |
| 310 | - $key_cache = $customer_id; | |
| 311 | - $session = $session_cache->get_cache( $key_cache ); | |
| 312 | - if ( $session !== false ) { | |
| 313 | - return $session; | |
| 314 | - } | |
| 315 | - | |
| 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 ); | |
| 323 | - if ( ! empty( $res ) ) { | |
| 324 | - $session = $res[0]->session_value; | |
| 325 | - } | |
| 326 | - | |
| 327 | - $session = LP_Helper::maybe_unserialize( $session ); | |
| 328 | - // Set cache. | |
| 329 | - $session_cache->set_cache( $key_cache, $session ); | |
| 330 | - } catch ( Throwable $e ) { | |
| 331 | - error_log( $e->getMessage() ); | |
| 332 | - } | |
| 333 | - | |
| 334 | - return $session; | |
| 335 | - } | |
| 336 | - | |
| 337 | - /** | |
| 338 | - * Delete session by customer_id on DB. | |
| 339 | - * | |
| 340 | - * @param string $customer_id | |
| 341 | - * | |
| 342 | - * @return bool | |
| 343 | - */ | |
| 344 | - public function delete_session( string $customer_id ): bool { | |
| 345 | - global $wpdb; | |
| 346 | - $rs = true; | |
| 347 | - | |
| 348 | - try { | |
| 349 | - $wpdb->delete( | |
| 350 | - LP_Sessions_DB::getInstance()->tb_lp_sessions, | |
| 351 | - [ 'session_key' => $customer_id ], | |
| 352 | - [ '%s' ] | |
| 353 | - ); | |
| 354 | - // Clear cache. | |
| 355 | - LP_Session_Cache::instance()->clear( $customer_id ); | |
| 356 | - } catch ( Throwable $e ) { | |
| 357 | - $rs = false; | |
| 358 | - error_log( $e->getMessage() ); | |
| 359 | - } | |
| 360 | - | |
| 361 | - return $rs; | |
| 362 | - } | |
| 363 | - | |
| 364 | - /** | |
| 365 | - * Get session value. | |
| 366 | - * | |
| 367 | - * @param string $key | |
| 368 | - * @param mixed $default | |
| 369 | - * | |
| 370 | - * @return mixed|null | |
| 371 | - */ | |
| 372 | - public function get( string $key, $default = null ) { | |
| 373 | - return isset( $this->_data[ $key ] ) ? LP_Helper::maybe_unserialize( $this->_data[ $key ] ) : $default; | |
| 374 | - } | |
| 375 | - | |
| 376 | - /** | |
| 377 | - * Set session value. | |
| 378 | - * | |
| 379 | - * @param string $key | |
| 380 | - * @param mixed $value | |
| 381 | - * @param bool $force_change | |
| 382 | - */ | |
| 383 | - public function set( string $key, $value, bool $force_change = false ) { | |
| 384 | - $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value ); | |
| 385 | - | |
| 386 | - if ( $force_change ) { | |
| 387 | - $this->save_data(); | |
| 388 | - } | |
| 389 | - } | |
| 390 | - | |
| 391 | - /** | |
| 392 | - * Remove a value from session by key. | |
| 393 | - * | |
| 394 | - * @param string $key | |
| 395 | - * @param bool $force_change | |
| 396 | - */ | |
| 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 ] ); | |
| 402 | - | |
| 403 | - if ( $force_change ) { | |
| 404 | - $this->save_data(); | |
| 405 | - } | |
| 406 | - } | |
| 407 | - | |
| 408 | - /** | |
| 409 | - * Get customer id. | |
| 410 | - */ | |
| 411 | - public function get_customer_id(): string { | |
| 412 | - return (string) $this->_customer_id; | |
| 413 | - } | |
| 414 | - | |
| 415 | - public static function instance() { | |
| 416 | - if ( is_null( self::$_instance ) ) { | |
| 417 | - self::$_instance = new self(); | |
| 418 | - } | |
| 419 | - | |
| 420 | - return self::$_instance; | |
| 421 | - } | |
| 422 | -} | |
| 423 | - | |
| 424 | -/** | |
| 425 | - * @param $key | |
| 426 | - * @param null $default | |
| 427 | - * | |
| 428 | - * @return array|string | |
| 429 | - */ | |
| 430 | -function learn_press_session_get( $key, $default = null ) { | |
| 431 | - return LP_Session_Handler::instance()->get( $key, $default ); | |
| 432 | -} | |
| 433 | - | |
| 434 | -/** | |
| 435 | - * @param $key | |
| 436 | - * @param $value | |
| 437 | - */ | |
| 438 | -function learn_press_session_set( $key, $value ) { | |
| 439 | - LP_Session_Handler::instance()->set( $key, $value ); | |
| 440 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class LP_Session_Handler | |
| 5 | + * | |
| 6 | + * Only set COOKIE for user guest | |
| 7 | + */ | |
| 8 | +class LP_Session_Handler { | |
| 9 | + /** | |
| 10 | + * @var string $_customer_id | |
| 11 | + */ | |
| 12 | + public $_customer_id = ''; | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * @var array $_data | |
| 16 | + */ | |
| 17 | + protected $_data = array(); | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @var bool $_changed | |
| 21 | + */ | |
| 22 | + protected $_changed = false; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * @var string cookie name | |
| 26 | + */ | |
| 27 | + private $_cookie = 'lp_session_guest'; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @var int session expiration timestamp | |
| 31 | + */ | |
| 32 | + private $_session_expiration = 0; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @var null | |
| 36 | + */ | |
| 37 | + private static $_instance = null; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * LP_Session_Handler constructor. | |
| 41 | + * | |
| 42 | + * @version 3.2.2 | |
| 43 | + */ | |
| 44 | + protected function __construct() { | |
| 45 | + $this->init_hooks(); | |
| 46 | + if ( is_admin() ) { | |
| 47 | + return; | |
| 48 | + } | |
| 49 | + | |
| 50 | + $this->init(); | |
| 51 | + } | |
| 52 | + | |
| 53 | + protected function init_hooks() { | |
| 54 | + add_action( 'wp_login', [ $this, 'handle_when_user_login_success' ], 10, 2 ); | |
| 55 | + add_action( 'wp_logout', array( $this, 'destroy_session' ) ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Set COOKIE for only user guest | |
| 60 | + * Set data: customer_id, session_expiration | |
| 61 | + * | |
| 62 | + * @return self | |
| 63 | + * @since 3.0.0 | |
| 64 | + * @version 4.0.3 | |
| 65 | + * @modify Tungnx | |
| 66 | + */ | |
| 67 | + protected function init(): LP_Session_Handler { | |
| 68 | + $expire_time_for_guest = DAY_IN_SECONDS; | |
| 69 | + $expire_time_for_user = 6 * DAY_IN_SECONDS; | |
| 70 | + | |
| 71 | + // Set data for user Guest. | |
| 72 | + if ( ! is_user_logged_in() ) { // Generate data and set cookie for guest | |
| 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; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + } else { // Set data for user logged. | |
| 89 | + $this->set_session_expiration( $expire_time_for_user ); | |
| 90 | + $this->_customer_id = get_current_user_id(); | |
| 91 | + } | |
| 92 | + | |
| 93 | + // Get session data from DB. | |
| 94 | + $this->_data = $this->get_session_data(); | |
| 95 | + | |
| 96 | + return $this; | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Handle when user logged in. | |
| 101 | + * | |
| 102 | + * @param $user_name | |
| 103 | + * @param $user | |
| 104 | + * | |
| 105 | + * @return void | |
| 106 | + */ | |
| 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 | + | |
| 111 | + /** | |
| 112 | + * Must set wp_set_current_user to get_current_user_id and is_user_logged_in work correctly. | |
| 113 | + * Because WP note must do that. | |
| 114 | + * Read more @see wp_signon | |
| 115 | + * If version WP after run correctly, remove wp_set_current_user. | |
| 116 | + */ | |
| 117 | + wp_set_current_user( $user->ID ); | |
| 118 | + $user_id = get_current_user_id(); | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Delete session of user guest before. | |
| 122 | + */ | |
| 123 | + if ( $user_id ) { | |
| 124 | + $customer_id = $this->get_customer_id(); | |
| 125 | + $user_before = get_user_by( 'ID', $customer_id ); | |
| 126 | + if ( ! $user_before ) { | |
| 127 | + $this->delete_session( $customer_id ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + $this->_customer_id = $user_id; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Set cookie for user guest. | |
| 136 | + * | |
| 137 | + * @return LP_Session_Handler | |
| 138 | + */ | |
| 139 | + public function set_customer_session_cookie(): LP_Session_Handler { | |
| 140 | + // Set the cookie | |
| 141 | + if ( ! isset( $_COOKIE[ $this->_cookie ] ) ) { | |
| 142 | + learn_press_setcookie( $this->_cookie, $this->_customer_id, $this->_session_expiration ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + return $this; | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * Set session expiration. | |
| 150 | + * | |
| 151 | + * @param int $duration | |
| 152 | + * | |
| 153 | + * @return LP_Session_Handler | |
| 154 | + */ | |
| 155 | + public function set_session_expiration( int $duration = 0 ): LP_Session_Handler { | |
| 156 | + $this->_session_expiration = time() + $duration; | |
| 157 | + | |
| 158 | + return $this; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Get cookie of guest. | |
| 163 | + * | |
| 164 | + * @return string | |
| 165 | + */ | |
| 166 | + public function get_cookie_data(): string { | |
| 167 | + if ( empty( $_COOKIE[ $this->_cookie ] ) || ! is_string( $_COOKIE[ $this->_cookie ] ) ) { | |
| 168 | + return ''; | |
| 169 | + } | |
| 170 | + | |
| 171 | + return $_COOKIE[ $this->_cookie ]; | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Get session data | |
| 176 | + * | |
| 177 | + * @return array | |
| 178 | + */ | |
| 179 | + public function get_session_data(): array { | |
| 180 | + if ( is_user_logged_in() ) { | |
| 181 | + $customer_id = get_current_user_id(); | |
| 182 | + } else { | |
| 183 | + $customer_id = $this->get_customer_id(); | |
| 184 | + } | |
| 185 | + | |
| 186 | + return (array) $this->get_session_by_customer_id( (string) $customer_id ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Save session data to the database. | |
| 191 | + * | |
| 192 | + * @return bool | |
| 193 | + * @version 4.0.1 | |
| 194 | + * @since 3.0.0 | |
| 195 | + */ | |
| 196 | + public function save_data(): bool { | |
| 197 | + $res = false; | |
| 198 | + | |
| 199 | + try { | |
| 200 | + $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 201 | + | |
| 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 ); | |
| 209 | + | |
| 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 | + } | |
| 232 | + | |
| 233 | + // Clear cache. | |
| 234 | + LP_Session_Cache::instance()->clear( $this->_customer_id ); | |
| 235 | + | |
| 236 | + $res = true; | |
| 237 | + } catch ( Throwable $e ) { | |
| 238 | + error_log( $e->getMessage() ); | |
| 239 | + } | |
| 240 | + | |
| 241 | + return $res; | |
| 242 | + } | |
| 243 | + | |
| 244 | + /** | |
| 245 | + * Destroy session. | |
| 246 | + */ | |
| 247 | + public function destroy_session() { | |
| 248 | + // Clear cookie. | |
| 249 | + if ( ! empty( $this->_cookie ) ) { | |
| 250 | + learn_press_remove_cookie( $this->_cookie ); | |
| 251 | + } | |
| 252 | + | |
| 253 | + // Clear session expire. | |
| 254 | + $this->cleanup_sessions_expire(); | |
| 255 | + | |
| 256 | + $logout_redirect_page_id = LP_Settings::get_option( 'logout_redirect_page_id' ); | |
| 257 | + if ( $logout_redirect_page_id ) { | |
| 258 | + wp_safe_redirect( get_the_permalink( $logout_redirect_page_id ) ); | |
| 259 | + die; | |
| 260 | + } | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Clear session expired. | |
| 265 | + * | |
| 266 | + * @return bool | |
| 267 | + */ | |
| 268 | + public function cleanup_sessions_expire(): bool { | |
| 269 | + $res = true; | |
| 270 | + $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 271 | + | |
| 272 | + try { | |
| 273 | + // Get session expired. | |
| 274 | + $filter_get = new LP_Session_Filter(); | |
| 275 | + $filter_get->where[] = $lp_session_db->wpdb->prepare( 'AND session_expiry < %d', time() ); | |
| 276 | + $sessions = $lp_session_db->get_sessions( $filter_get ); | |
| 277 | + // Clear cache. | |
| 278 | + foreach ( $sessions as $session ) { | |
| 279 | + LP_Session_Cache::instance()->clear( $session->session_key ); | |
| 280 | + } | |
| 281 | + | |
| 282 | + // Delete session expired. | |
| 283 | + $filter = new LP_Session_Filter(); | |
| 284 | + $filter->collection = $lp_session_db->tb_lp_sessions; | |
| 285 | + $filter->where[] = $lp_session_db->wpdb->prepare( 'AND session_expiry < %d', time() ); | |
| 286 | + $lp_session_db->delete_execute( $filter ); | |
| 287 | + } catch ( Throwable $e ) { | |
| 288 | + $res = false; | |
| 289 | + error_log( $e->getMessage() ); | |
| 290 | + } | |
| 291 | + | |
| 292 | + return $res; | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Get session on DB. | |
| 297 | + * | |
| 298 | + * @param string $customer_id | |
| 299 | + * | |
| 300 | + * @return false|mixed|string | |
| 301 | + * @Todo - Tungnx should handle data when save type json instead serialize. | |
| 302 | + */ | |
| 303 | + public function get_session_by_customer_id( string $customer_id = '' ) { | |
| 304 | + $lp_session_db = LP_Sessions_DB::getInstance(); | |
| 305 | + $session = []; | |
| 306 | + | |
| 307 | + try { | |
| 308 | + // Get cache. | |
| 309 | + $session_cache = LP_Session_Cache::instance(); | |
| 310 | + $key_cache = $customer_id; | |
| 311 | + $session = $session_cache->get_cache( $key_cache ); | |
| 312 | + if ( $session !== false ) { | |
| 313 | + return $session; | |
| 314 | + } | |
| 315 | + | |
| 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 ); | |
| 323 | + if ( ! empty( $res ) ) { | |
| 324 | + $session = $res[0]->session_value; | |
| 325 | + } | |
| 326 | + | |
| 327 | + $session = LP_Helper::maybe_unserialize( $session ); | |
| 328 | + // Set cache. | |
| 329 | + $session_cache->set_cache( $key_cache, $session ); | |
| 330 | + } catch ( Throwable $e ) { | |
| 331 | + error_log( $e->getMessage() ); | |
| 332 | + } | |
| 333 | + | |
| 334 | + return $session; | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * Delete session by customer_id on DB. | |
| 339 | + * | |
| 340 | + * @param string $customer_id | |
| 341 | + * | |
| 342 | + * @return bool | |
| 343 | + */ | |
| 344 | + public function delete_session( string $customer_id ): bool { | |
| 345 | + global $wpdb; | |
| 346 | + $rs = true; | |
| 347 | + | |
| 348 | + try { | |
| 349 | + $wpdb->delete( | |
| 350 | + LP_Sessions_DB::getInstance()->tb_lp_sessions, | |
| 351 | + [ 'session_key' => $customer_id ], | |
| 352 | + [ '%s' ] | |
| 353 | + ); | |
| 354 | + // Clear cache. | |
| 355 | + LP_Session_Cache::instance()->clear( $customer_id ); | |
| 356 | + } catch ( Throwable $e ) { | |
| 357 | + $rs = false; | |
| 358 | + error_log( $e->getMessage() ); | |
| 359 | + } | |
| 360 | + | |
| 361 | + return $rs; | |
| 362 | + } | |
| 363 | + | |
| 364 | + /** | |
| 365 | + * Get session value. | |
| 366 | + * | |
| 367 | + * @param string $key | |
| 368 | + * @param mixed $default | |
| 369 | + * | |
| 370 | + * @return mixed|null | |
| 371 | + */ | |
| 372 | + public function get( string $key, $default = null ) { | |
| 373 | + return isset( $this->_data[ $key ] ) ? LP_Helper::maybe_unserialize( $this->_data[ $key ] ) : $default; | |
| 374 | + } | |
| 375 | + | |
| 376 | + /** | |
| 377 | + * Set session value. | |
| 378 | + * | |
| 379 | + * @param string $key | |
| 380 | + * @param mixed $value | |
| 381 | + * @param bool $force_change | |
| 382 | + */ | |
| 383 | + public function set( string $key, $value, bool $force_change = false ) { | |
| 384 | + $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value ); | |
| 385 | + | |
| 386 | + if ( $force_change ) { | |
| 387 | + $this->save_data(); | |
| 388 | + } | |
| 389 | + } | |
| 390 | + | |
| 391 | + /** | |
| 392 | + * Remove a value from session by key. | |
| 393 | + * | |
| 394 | + * @param string $key | |
| 395 | + * @param bool $force_change | |
| 396 | + */ | |
| 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 ] ); | |
| 402 | + | |
| 403 | + if ( $force_change ) { | |
| 404 | + $this->save_data(); | |
| 405 | + } | |
| 406 | + } | |
| 407 | + | |
| 408 | + /** | |
| 409 | + * Get customer id. | |
| 410 | + */ | |
| 411 | + public function get_customer_id(): string { | |
| 412 | + return (string) $this->_customer_id; | |
| 413 | + } | |
| 414 | + | |
| 415 | + public static function instance() { | |
| 416 | + if ( is_null( self::$_instance ) ) { | |
| 417 | + self::$_instance = new self(); | |
| 418 | + } | |
| 419 | + | |
| 420 | + return self::$_instance; | |
| 421 | + } | |
| 422 | +} | |
| 423 | + | |
| 424 | +/** | |
| 425 | + * @param $key | |
| 426 | + * @param null $default | |
| 427 | + * | |
| 428 | + * @return array|string | |
| 429 | + */ | |
| 430 | +function learn_press_session_get( $key, $default = null ) { | |
| 431 | + return LP_Session_Handler::instance()->get( $key, $default ); | |
| 432 | +} | |
| 433 | + | |
| 434 | +/** | |
| 435 | + * @param $key | |
| 436 | + * @param $value | |
| 437 | + */ | |
| 438 | +function learn_press_session_set( $key, $value ) { | |
| 439 | + LP_Session_Handler::instance()->set( $key, $value ); | |
| 440 | +} | |