Exceptions
2 years ago
Formatters
1 year ago
Payments
2 years ago
Routes
10 months ago
Schemas
10 months ago
Utilities
7 months ago
Authentication.php
11 months ago
Formatters.php
2 years ago
Legacy.php
1 year ago
RoutesController.php
1 year ago
SchemaController.php
1 year ago
SessionHandler.php
9 months ago
StoreApi.php
1 year ago
deprecated.php
2 years ago
functions.php
2 years ago
SessionHandler.php
117 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi; |
| 5 | |
| 6 | use Automattic\Jetpack\Constants; |
| 7 | use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils; |
| 8 | use WC_Session; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * SessionHandler class |
| 14 | */ |
| 15 | final class SessionHandler extends WC_Session { |
| 16 | /** |
| 17 | * Token from HTTP headers. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $token = ''; |
| 22 | |
| 23 | /** |
| 24 | * Table name for session data. |
| 25 | * |
| 26 | * @var string Custom session table name |
| 27 | */ |
| 28 | protected $table = ''; |
| 29 | |
| 30 | /** |
| 31 | * Expiration timestamp. |
| 32 | * |
| 33 | * @var int |
| 34 | */ |
| 35 | protected $session_expiration = 0; |
| 36 | |
| 37 | /** |
| 38 | * Constructor for the session class. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | $this->token = wc_clean( wp_unslash( $_SERVER['HTTP_CART_TOKEN'] ?? '' ) ); |
| 42 | $this->table = $GLOBALS['wpdb']->prefix . 'woocommerce_sessions'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Init hooks and session data. |
| 47 | */ |
| 48 | public function init() { |
| 49 | $this->init_session_from_token(); |
| 50 | add_action( 'shutdown', array( $this, 'save_data' ), 20 ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Process the token header to load the correct session. |
| 55 | */ |
| 56 | protected function init_session_from_token() { |
| 57 | $payload = CartTokenUtils::get_cart_token_payload( $this->token ); |
| 58 | |
| 59 | $this->_customer_id = $payload['user_id']; |
| 60 | $this->session_expiration = $payload['exp']; |
| 61 | $this->_data = (array) $this->get_session( $this->get_customer_id(), array() ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the session. |
| 66 | * |
| 67 | * @param string $customer_id Customer ID. |
| 68 | * @param mixed $default_value Default session value. |
| 69 | |
| 70 | * @return mixed Returns either the session data or the default value. Returns false if WP setup is in progress. |
| 71 | */ |
| 72 | public function get_session( $customer_id, $default_value = false ) { |
| 73 | global $wpdb; |
| 74 | |
| 75 | // This mimics behaviour from default WC_Session_Handler class. There will be no sessions retrieved while WP setup is due. |
| 76 | if ( Constants::is_defined( 'WP_SETUP_CONFIG' ) ) { |
| 77 | return $default_value; |
| 78 | } |
| 79 | |
| 80 | $value = $wpdb->get_var( |
| 81 | $wpdb->prepare( |
| 82 | 'SELECT session_value FROM %i WHERE session_key = %s', |
| 83 | $this->table, |
| 84 | $customer_id |
| 85 | ) |
| 86 | ); |
| 87 | |
| 88 | if ( is_null( $value ) ) { |
| 89 | $value = $default_value; |
| 90 | } |
| 91 | |
| 92 | return maybe_unserialize( $value ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Save data and delete user session. |
| 97 | */ |
| 98 | public function save_data() { |
| 99 | // Dirty if something changed - prevents saving nothing new. |
| 100 | if ( $this->_dirty ) { |
| 101 | global $wpdb; |
| 102 | |
| 103 | $wpdb->query( |
| 104 | $wpdb->prepare( |
| 105 | 'INSERT INTO %i (`session_key`, `session_value`, `session_expiry`) VALUES (%s, %s, %d) ON DUPLICATE KEY UPDATE `session_value` = VALUES(`session_value`), `session_expiry` = VALUES(`session_expiry`)', |
| 106 | $this->table, |
| 107 | $this->get_customer_id(), |
| 108 | maybe_serialize( $this->_data ), |
| 109 | $this->session_expiration |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | $this->_dirty = false; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 |