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