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 |