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