Passed
Pull Request — master (#217)
by Patrik
03:45
created

WPInv_Session::cleanup_sessions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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