PluginProbe ʕ •ᴥ•ʔ
Loco Translate / 2.8.0
Loco Translate v2.8.0
2.8.5 2.8.4 2.5.8 2.6.0 2.6.1 2.6.10 2.6.11 2.6.12 2.6.13 2.6.14 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0 2.8.1 2.8.2 2.8.3 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.2 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7
loco-translate / src / data / Session.php
loco-translate / src / data Last commit date
CompiledData.php 3 years ago Cookie.php 4 years ago Option.php 9 years ago Permissions.php 1 year ago Preferences.php 4 years ago RecentItems.php 2 years ago Serializable.php 3 years ago Session.php 4 years ago Settings.php 1 year ago Transient.php 7 years ago Upload.php 2 years ago
Session.php
197 lines
1 <?php
2 /**
3 * Abstracts session data access using WP_Session_Tokens
4 */
5 class Loco_data_Session extends Loco_data_Serializable {
6
7 /**
8 * @var Loco_data_Session
9 */
10 private static $current;
11
12 /**
13 * Value from wp_get_session_token
14 * @var string
15 */
16 private $token;
17
18 /**
19 * @var WP_User_Meta_Session_Tokens
20 */
21 private $manager;
22
23 /**
24 * Dirty flag: TODO abstract into array access setters
25 * @var bool
26 */
27 private $dirty = false;
28
29
30 /**
31 * @return Loco_data_Session
32 */
33 public static function get(){
34 if( ! self::$current ){
35 new Loco_data_Session;
36 }
37 return self::$current;
38 }
39
40
41
42 /**
43 * Trash data and remove from memory
44 */
45 public static function destroy(){
46 if( self::$current ){
47 try {
48 self::$current->clear();
49 }
50 catch( Exception $e ){
51 // probably no session to destroy
52 }
53 self::$current = null;
54 }
55 }
56
57
58
59 /**
60 * Commit current session data to WordPress storage and remove from memory
61 */
62 public static function close(){
63 if( self::$current && self::$current->dirty ){
64 self::$current->persist();
65 self::$current = null;
66 }
67 }
68
69
70
71 /**
72 * @internal
73 */
74 final public function __construct( array $raw = [] ){
75 $this->token = wp_get_session_token();
76 if( ! $this->token ){
77 throw new Loco_error_Exception('Failed to get session token');
78 }
79 parent::__construct( [] );
80 $this->manager = WP_Session_Tokens::get_instance( get_current_user_id() );
81 // populate object from stored session data
82 $data = $this->getRaw();
83 if( isset($data['loco']) ){
84 $this->setUnserialized( $data['loco'] );
85 }
86 // any initial arbitrary data can be merged on top
87 foreach( $raw as $prop => $value ){
88 $this[$prop] = $value;
89 }
90 // enforce single instance
91 self::$current = $this;
92 // ensure against unclean shutdown
93 if( loco_debugging() ){
94 register_shutdown_function( [$this,'_on_shutdown'] );
95 }
96 }
97
98
99 /**
100 * @internal
101 * Ensure against unclean use of session storage
102 */
103 public function _on_shutdown(){
104 if( $this->dirty ){
105 trigger_error('Unclean session shutdown: call either Loco_data_Session::destroy or Loco_data_Session::close');
106 }
107 }
108
109
110 /**
111 * Get raw session data held by WordPress
112 * @return array
113 */
114 private function getRaw(){
115 $data = $this->manager->get( $this->token );
116 // session data will exist if WordPress login is valid
117 if( ! $data || ! is_array($data) ){
118 throw new Loco_error_Exception('Invalid session');
119 }
120
121 return $data;
122 }
123
124
125 /**
126 * Persist object in WordPress usermeta table
127 * @return Loco_data_Session
128 */
129 public function persist(){
130 $data = $this->getRaw();
131 $data['loco'] = $this->getSerializable();
132 $this->manager->update( $this->token, $data );
133 $this->dirty = false;
134 return $this;
135 }
136
137
138 /**
139 * Clear object data and remove our key from WordPress usermeta record
140 * @return Loco_data_Session
141 */
142 public function clear(){
143 $data = $this->getRaw();
144 if( isset($data['loco']) ){
145 unset( $data['loco'] );
146 $this->manager->update( $this->token, $data );
147 }
148 $this->exchangeArray( [] );
149 $this->dirty = false;
150 return $this;
151 }
152
153
154 /**
155 * @param string name of messages bag, e.g. "errors"
156 * @param string optionally put message in rather than getting data out
157 * @return mixed
158 */
159 public function flash( $bag, $data = null ){
160 if( isset($data) ){
161 $this->dirty = true;
162 $this[$bag][] = $data;
163 }
164 // else get first object in bag and remove before returning
165 else if( isset($this[$bag]) ){
166 if( $data = array_shift($this[$bag]) ){
167 $this->dirty = true;
168 return $data;
169 }
170 }
171 return null;
172 }
173
174
175 /**
176 * {@inheritDoc}
177 */
178 public function offsetSet( $index, $newval ){
179 if( ! isset($this[$index]) || $newval !== $this[$index] ){
180 $this->dirty = true;
181 parent::offsetSet( $index, $newval );
182 }
183 }
184
185
186 /**
187 * {@inheritDoc}
188 */
189 public function offsetUnset( $index ){
190 if( isset($this[$index]) ){
191 $this->dirty = true;
192 parent::offsetUnset( $index );
193 }
194 }
195
196 }
197