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