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 * @category Abstract Class
9 * @author WooThemes
10 */
11 abstract class WC_Session {
12
13 /** @var int $_customer_id */
14 protected $_customer_id;
15
16 /** @var array $_data */
17 protected $_data = array();
18
19 /** @var bool $_dirty When something changes */
20 protected $_dirty = false;
21
22 /**
23 * __get function.
24 *
25 * @access public
26 * @param mixed $key
27 * @return mixed
28 */
29 public function __get( $key ) {
30 return $this->get( $key );
31 }
32
33 /**
34 * __set function.
35 *
36 * @access public
37 * @param mixed $key
38 * @param mixed $value
39 * @return void
40 */
41 public function __set( $key, $value ) {
42 $this->set( $key, $value );
43 }
44
45 /**
46 * __isset function.
47 *
48 * @access public
49 * @param mixed $key
50 * @return bool
51 */
52 public function __isset( $key ) {
53 return isset( $this->_data[ sanitize_title( $key ) ] );
54 }
55
56 /**
57 * __unset function.
58 *
59 * @access public
60 * @param mixed $key
61 * @return void
62 */
63 public function __unset( $key ) {
64 if ( isset( $this->_data[ $key ] ) ) {
65 unset( $this->_data[ $key ] );
66 $this->_dirty = true;
67 }
68 }
69
70 /**
71 * Get a session variable
72 *
73 * @param string $key
74 * @param mixed $default used if the session variable isn't set
75 * @return mixed value of session variable
76 */
77 public function get( $key, $default = null ) {
78 $key = sanitize_key( $key );
79 return isset( $this->_data[ $key ] ) ? maybe_unserialize( $this->_data[ $key ] ) : $default;
80 }
81
82 /**
83 * Set a session variable
84 *
85 * @param string $key
86 * @param mixed $value
87 */
88 public function set( $key, $value ) {
89 $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value );
90 $this->_dirty = true;
91 }
92
93 /**
94 * get_customer_id function.
95 *
96 * @access public
97 * @return int
98 */
99 public function get_customer_id() {
100 return $this->_customer_id;
101 }
102 }