namespace Automattic\WooCommerce\StoreApi;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils;
defined( 'ABSPATH' ) || exit;
final class SessionHandler extends WC_Session {
* Token from HTTP headers.
* Table name for session data.
* @var string Custom session table name
protected $session_expiration;
* Constructor for the session class.
public function __construct() {
$this->token = wc_clean( wp_unslash( $_SERVER['HTTP_CART_TOKEN'] ?? '' ) );
$this->table = $GLOBALS['wpdb']->prefix . 'woocommerce_sessions';
* Init hooks and session data.
$this->init_session_from_token();
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
* Process the token header to load the correct session.
protected function init_session_from_token() {
$payload = CartTokenUtils::get_cart_token_payload( $this->token );
$this->_customer_id = $payload['user_id'];
$this->session_expiration = $payload['exp'];
$this->_data = (array) $this->get_session( $this->_customer_id, array() );
* @param string $customer_id Customer ID.
* @param mixed $default_value Default session value.
* @return string|array|bool
public function get_session( $customer_id, $default_value = false ) {
// This mimics behaviour from default WC_Session_Handler class. There will be no sessions retrieved while WP setup is due.
if ( Constants::is_defined( 'WP_SETUP_CONFIG' ) ) {
'SELECT session_value FROM %i WHERE session_key = %s',
if ( is_null( $value ) ) {
return maybe_unserialize( $value );
* Save data and delete user session.
public function save_data() {
// Dirty if something changed - prevents saving nothing new.
'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`)',
maybe_serialize( $this->_data ),
$this->session_expiration