1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Abstracts session data access using WP_Session_Tokens
*/
class Loco_data_Session extends Loco_data_Serializable {
/**
* @var Loco_data_Session
*/
private static $current;
/**
* Value from wp_get_session_token
* @var string
*/
private $token;
/**
* @var WP_User_Meta_Session_Tokens
*/
private $manager;
/**
* Dirty flag: TODO abstract into array access setters
* @var bool
*/
private $dirty = false;
/**
* @return Loco_data_Session
*/
public static function get(){
if( ! self::$current ){
new Loco_data_Session;
}
return self::$current;
}
/**
* Trash data and remove from memory
*/
public static function destroy(){
if( self::$current ){
try {
self::$current->clear();
}
catch( Exception $e ){
// probably no session to destroy
}
self::$current = null;
}
}
/**
* Commit current session data to WordPress storage and remove from memory
*/
public static function close(){
if( self::$current && self::$current->dirty ){
self::$current->persist();
self::$current = null;
}
}
/**
* @internal
*/
final public function __construct( array $raw = array() ){
$this->token = wp_get_session_token();
if( ! $this->token ){
throw new Loco_error_Exception('Failed to get session token');
}
parent::__construct( array() );
$this->manager = WP_Session_Tokens::get_instance( get_current_user_id() );
// populate object from stored session data
$data = $this->getRaw();
if( isset($data['loco']) ){
$this->setUnserialized( $data['loco'] );
}
// any initial arbitrary data can be merged on top
foreach( $raw as $prop => $value ){
$this[$prop] = $value;
}
// enforce single instance
self::$current = $this;
// ensure against unclean shutdown
if( loco_debugging() ){
register_shutdown_function( array($this,'_on_shutdown') );
}
}
/**
* @internal
* Ensure against unclean use of session storage
*/
public function _on_shutdown(){
if( $this->dirty ){
trigger_error('Unclean session shutdown: call either Loco_data_Session::destroy or Loco_data_Session::close');
}
}
/**
* Get raw session data held by WordPress
* @return array
*/
private function getRaw(){
$data = $this->manager->get( $this->token );
// session data will exist if WordPress login is valid
if( ! $data || ! is_array($data) ){
throw new Loco_error_Exception('Invalid session');
}
return $data;
}
/**
* Persist object in WordPress usermeta table
* @return Loco_data_Session
*/
public function persist(){
$data = $this->getRaw();
$data['loco'] = $this->getSerializable();
$this->manager->update( $this->token, $data );
$this->dirty = false;
return $this;
}
/**
* Clear object data and remove our key from WordPress usermeta record
* @return Loco_data_Session
*/
public function clear(){
$data = $this->getRaw();
if( isset($data['loco']) ){
unset( $data['loco'] );
$this->manager->update( $this->token, $data );
}
$this->exchangeArray( array() );
$this->dirty = false;
return $this;
}
/**
* @param string name of messages bag, e.g. "errors"
* @param mixed optionally put data in rather than getting data out
* @return mixed
*/
public function flash( $bag, $data = null ){
if( isset($data) ){
$this->dirty = true;
$this[$bag][] = $data;
return;
}
// else get first object in bag and remove before returning
if( isset($this[$bag]) ){
if( $data = array_shift($this[$bag]) ){
$this->dirty = true;
return $data;
}
}
}
/**
* @internal
*/
public function offsetSet( $index, $newval ){
if( ! isset($this[$index]) || $newval !== $this[$index] ){
$this->dirty = true;
parent::offsetSet( $index, $newval );
}
}
/**
* @internal
*/
public function offsetUnset( $index ){
if( isset($this[$index]) ){
$this->dirty = true;
parent::offsetUnset( $index );
}
}
}