abstract-wc-address-provider.php
9 months ago
abstract-wc-data.php
4 months ago
abstract-wc-deprecated-hooks.php
6 years ago
abstract-wc-integration.php
5 years ago
abstract-wc-log-handler.php
2 years ago
abstract-wc-object-query.php
5 years ago
abstract-wc-order.php
1 month ago
abstract-wc-payment-gateway.php
1 month ago
abstract-wc-payment-token.php
5 years ago
abstract-wc-privacy.php
5 years ago
abstract-wc-product.php
1 month ago
abstract-wc-session.php
10 months ago
abstract-wc-settings-api.php
1 year ago
abstract-wc-shipping-method.php
4 months ago
abstract-wc-widget.php
5 years ago
class-wc-background-process.php
5 years ago
abstract-wc-session.php
143 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handle data for the current customers session |
| 4 | * |
| 5 | * @class WC_Session |
| 6 | * @version 2.0.0 |
| 7 | * @package WooCommerce\Abstracts |
| 8 | */ |
| 9 | |
| 10 | declare(strict_types=1); |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * WC_Session |
| 18 | */ |
| 19 | abstract class WC_Session { |
| 20 | |
| 21 | /** |
| 22 | * Customer ID. |
| 23 | * |
| 24 | * @var ?string $_customer_id Customer ID. |
| 25 | */ |
| 26 | protected $_customer_id; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 27 | |
| 28 | /** |
| 29 | * Session Data. |
| 30 | * |
| 31 | * @var array $_data Data array. |
| 32 | */ |
| 33 | protected $_data = array(); // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 34 | |
| 35 | /** |
| 36 | * Dirty when the session needs saving. |
| 37 | * |
| 38 | * @var bool $_dirty When something changes |
| 39 | */ |
| 40 | protected $_dirty = false; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 41 | |
| 42 | /** |
| 43 | * Init hooks and session data. Extended by child classes. |
| 44 | * |
| 45 | * @since 3.3.0 |
| 46 | */ |
| 47 | public function init() {} |
| 48 | |
| 49 | /** |
| 50 | * Cleanup session data. Extended by child classes. |
| 51 | */ |
| 52 | public function cleanup_sessions() {} |
| 53 | |
| 54 | /** |
| 55 | * Magic get method. |
| 56 | * |
| 57 | * @param string $key Key to get. |
| 58 | * @return mixed |
| 59 | */ |
| 60 | public function __get( $key ) { |
| 61 | return $this->get( $key ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Magic set method. |
| 66 | * |
| 67 | * @param string $key Key to set. |
| 68 | * @param mixed $value Value to set. |
| 69 | */ |
| 70 | public function __set( $key, $value ) { |
| 71 | $this->set( $key, $value ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Magic isset method. |
| 76 | * |
| 77 | * @param string $key Key to check. |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function __isset( $key ) { |
| 81 | return isset( $this->_data[ sanitize_key( $key ) ] ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Magic unset method. |
| 86 | * |
| 87 | * @param string $key Key to unset. |
| 88 | */ |
| 89 | public function __unset( $key ) { |
| 90 | $key = sanitize_key( $key ); |
| 91 | if ( isset( $this->_data[ $key ] ) ) { |
| 92 | unset( $this->_data[ $key ] ); |
| 93 | $this->_dirty = true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get a session variable. |
| 99 | * |
| 100 | * @param string $key Key to get. |
| 101 | * @param mixed $default_value used if the session variable isn't set. |
| 102 | * @return mixed value of session variable |
| 103 | */ |
| 104 | public function get( $key, $default_value = null ) { |
| 105 | $key = sanitize_key( $key ); |
| 106 | return isset( $this->_data[ $key ] ) ? maybe_unserialize( $this->_data[ $key ] ) : $default_value; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set a session variable. |
| 111 | * |
| 112 | * @param string $key Key to set. |
| 113 | * @param mixed $value Value to set. |
| 114 | */ |
| 115 | public function set( $key, $value ) { |
| 116 | if ( null === $value ) { |
| 117 | $this->__unset( $key ); |
| 118 | |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | $key = sanitize_key( $key ); |
| 123 | $serialized_original_value = $this->_data[ $key ] ?? null; |
| 124 | $serialized_value = maybe_serialize( $value ); |
| 125 | |
| 126 | if ( $serialized_original_value === $serialized_value || maybe_unserialize( $serialized_original_value ) === $value ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | $this->_dirty = true; |
| 131 | $this->_data[ $key ] = $serialized_value; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get customer ID. If the session is not initialized, returns an empty string. |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | public function get_customer_id() { |
| 140 | return $this->_customer_id ?? ''; |
| 141 | } |
| 142 | } |
| 143 |