@@ -542,7 +542,7 @@ discard block |
||
| 542 | 542 | /** |
| 543 | 543 | * @initiate session |
| 544 | 544 | * @access private |
| 545 | - * @return TRUE on success, FALSE on fail |
|
| 545 | + * @return boolean on success, FALSE on fail |
|
| 546 | 546 | * @throws EE_Error |
| 547 | 547 | * @throws InvalidArgumentException |
| 548 | 548 | * @throws InvalidDataTypeException |
@@ -778,7 +778,7 @@ discard block |
||
| 778 | 778 | * @update session data prior to saving to the db |
| 779 | 779 | * @access public |
| 780 | 780 | * @param bool $new_session |
| 781 | - * @return TRUE on success, FALSE on fail |
|
| 781 | + * @return boolean on success, FALSE on fail |
|
| 782 | 782 | * @throws EE_Error |
| 783 | 783 | * @throws InvalidArgumentException |
| 784 | 784 | * @throws InvalidDataTypeException |
@@ -879,7 +879,7 @@ discard block |
||
| 879 | 879 | * _save_session_to_db |
| 880 | 880 | * |
| 881 | 881 | * @param bool $clear_session |
| 882 | - * @return string |
|
| 882 | + * @return boolean |
|
| 883 | 883 | * @throws EE_Error |
| 884 | 884 | * @throws InvalidArgumentException |
| 885 | 885 | * @throws InvalidDataTypeException |
@@ -20,1241 +20,1241 @@ discard block |
||
| 20 | 20 | class EE_Session implements SessionIdentifierInterface |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - const session_id_prefix = 'ee_ssn_'; |
|
| 24 | - |
|
| 25 | - const hash_check_prefix = 'ee_shc_'; |
|
| 26 | - |
|
| 27 | - const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
| 28 | - |
|
| 29 | - const EE_SESSION_STATUS_CLOSED = 0; |
|
| 30 | - |
|
| 31 | - const EE_SESSION_STATUS_OPEN = 1; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * instance of the EE_Session object |
|
| 35 | - * |
|
| 36 | - * @var EE_Session |
|
| 37 | - */ |
|
| 38 | - private static $_instance; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var CacheStorageInterface $cache_storage |
|
| 42 | - */ |
|
| 43 | - protected $cache_storage; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * EE_Encryption object |
|
| 47 | - * |
|
| 48 | - * @var EE_Encryption |
|
| 49 | - */ |
|
| 50 | - protected $encryption; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * the session id |
|
| 54 | - * |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - private $_sid; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * session id salt |
|
| 61 | - * |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - private $_sid_salt; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * session data |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - private $_session_data = array(); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * how long an EE session lasts |
|
| 75 | - * default session lifespan of 2 hours (for not so instant IPNs) |
|
| 76 | - * |
|
| 77 | - * @var int |
|
| 78 | - */ |
|
| 79 | - private $_lifespan; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * session expiration time as Unix timestamp in GMT |
|
| 83 | - * |
|
| 84 | - * @var int |
|
| 85 | - */ |
|
| 86 | - private $_expiration; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * whether or not session has expired at some point |
|
| 90 | - * |
|
| 91 | - * @var boolean |
|
| 92 | - */ |
|
| 93 | - private $_expired = false; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * current time as Unix timestamp in GMT |
|
| 97 | - * |
|
| 98 | - * @var int |
|
| 99 | - */ |
|
| 100 | - private $_time; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * whether to encrypt session data |
|
| 104 | - * |
|
| 105 | - * @var bool |
|
| 106 | - */ |
|
| 107 | - private $_use_encryption; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * well... according to the server... |
|
| 111 | - * |
|
| 112 | - * @var null |
|
| 113 | - */ |
|
| 114 | - private $_user_agent; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * do you really trust the server ? |
|
| 118 | - * |
|
| 119 | - * @var null |
|
| 120 | - */ |
|
| 121 | - private $_ip_address; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * current WP user_id |
|
| 125 | - * |
|
| 126 | - * @var null |
|
| 127 | - */ |
|
| 128 | - private $_wp_user_id; |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * array for defining default session vars |
|
| 132 | - * |
|
| 133 | - * @var array |
|
| 134 | - */ |
|
| 135 | - private $_default_session_vars = array( |
|
| 136 | - 'id' => null, |
|
| 137 | - 'user_id' => null, |
|
| 138 | - 'ip_address' => null, |
|
| 139 | - 'user_agent' => null, |
|
| 140 | - 'init_access' => null, |
|
| 141 | - 'last_access' => null, |
|
| 142 | - 'expiration' => null, |
|
| 143 | - 'pages_visited' => array(), |
|
| 144 | - ); |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * timestamp for when last garbage collection cycle was performed |
|
| 148 | - * |
|
| 149 | - * @var int $_last_gc |
|
| 150 | - */ |
|
| 151 | - private $_last_gc; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * whether session is active or not |
|
| 155 | - * |
|
| 156 | - * @var int $status |
|
| 157 | - */ |
|
| 158 | - private $status = EE_Session::EE_SESSION_STATUS_CLOSED; |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @singleton method used to instantiate class object |
|
| 164 | - * @param CacheStorageInterface $cache_storage |
|
| 165 | - * @param EE_Encryption $encryption |
|
| 166 | - * @return EE_Session |
|
| 167 | - * @throws InvalidArgumentException |
|
| 168 | - * @throws InvalidDataTypeException |
|
| 169 | - * @throws InvalidInterfaceException |
|
| 170 | - */ |
|
| 171 | - public static function instance( |
|
| 172 | - CacheStorageInterface $cache_storage = null, |
|
| 173 | - EE_Encryption $encryption = null |
|
| 174 | - ) { |
|
| 175 | - // check if class object is instantiated |
|
| 176 | - // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
| 177 | - // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
| 178 | - if (! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
| 179 | - self::$_instance = new self($cache_storage, $encryption); |
|
| 180 | - } |
|
| 181 | - return self::$_instance; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * protected constructor to prevent direct creation |
|
| 188 | - * |
|
| 189 | - * @param CacheStorageInterface $cache_storage |
|
| 190 | - * @param EE_Encryption $encryption |
|
| 191 | - * @throws InvalidArgumentException |
|
| 192 | - * @throws InvalidDataTypeException |
|
| 193 | - * @throws InvalidInterfaceException |
|
| 194 | - */ |
|
| 195 | - protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null) |
|
| 196 | - { |
|
| 197 | - // session loading is turned ON by default, |
|
| 198 | - // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
|
| 199 | - // (which currently fires on the init hook at priority 9), |
|
| 200 | - // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
| 201 | - if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
| 202 | - return; |
|
| 203 | - } |
|
| 204 | - if (! defined('ESPRESSO_SESSION')) { |
|
| 205 | - define('ESPRESSO_SESSION', true); |
|
| 206 | - } |
|
| 207 | - // default session lifespan in seconds |
|
| 208 | - $this->_lifespan = apply_filters( |
|
| 209 | - 'FHEE__EE_Session__construct___lifespan', |
|
| 210 | - 60 * MINUTE_IN_SECONDS |
|
| 211 | - ) + 1; |
|
| 212 | - /* |
|
| 23 | + const session_id_prefix = 'ee_ssn_'; |
|
| 24 | + |
|
| 25 | + const hash_check_prefix = 'ee_shc_'; |
|
| 26 | + |
|
| 27 | + const OPTION_NAME_SETTINGS = 'ee_session_settings'; |
|
| 28 | + |
|
| 29 | + const EE_SESSION_STATUS_CLOSED = 0; |
|
| 30 | + |
|
| 31 | + const EE_SESSION_STATUS_OPEN = 1; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * instance of the EE_Session object |
|
| 35 | + * |
|
| 36 | + * @var EE_Session |
|
| 37 | + */ |
|
| 38 | + private static $_instance; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var CacheStorageInterface $cache_storage |
|
| 42 | + */ |
|
| 43 | + protected $cache_storage; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * EE_Encryption object |
|
| 47 | + * |
|
| 48 | + * @var EE_Encryption |
|
| 49 | + */ |
|
| 50 | + protected $encryption; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * the session id |
|
| 54 | + * |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + private $_sid; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * session id salt |
|
| 61 | + * |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + private $_sid_salt; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * session data |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + private $_session_data = array(); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * how long an EE session lasts |
|
| 75 | + * default session lifespan of 2 hours (for not so instant IPNs) |
|
| 76 | + * |
|
| 77 | + * @var int |
|
| 78 | + */ |
|
| 79 | + private $_lifespan; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * session expiration time as Unix timestamp in GMT |
|
| 83 | + * |
|
| 84 | + * @var int |
|
| 85 | + */ |
|
| 86 | + private $_expiration; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * whether or not session has expired at some point |
|
| 90 | + * |
|
| 91 | + * @var boolean |
|
| 92 | + */ |
|
| 93 | + private $_expired = false; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * current time as Unix timestamp in GMT |
|
| 97 | + * |
|
| 98 | + * @var int |
|
| 99 | + */ |
|
| 100 | + private $_time; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * whether to encrypt session data |
|
| 104 | + * |
|
| 105 | + * @var bool |
|
| 106 | + */ |
|
| 107 | + private $_use_encryption; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * well... according to the server... |
|
| 111 | + * |
|
| 112 | + * @var null |
|
| 113 | + */ |
|
| 114 | + private $_user_agent; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * do you really trust the server ? |
|
| 118 | + * |
|
| 119 | + * @var null |
|
| 120 | + */ |
|
| 121 | + private $_ip_address; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * current WP user_id |
|
| 125 | + * |
|
| 126 | + * @var null |
|
| 127 | + */ |
|
| 128 | + private $_wp_user_id; |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * array for defining default session vars |
|
| 132 | + * |
|
| 133 | + * @var array |
|
| 134 | + */ |
|
| 135 | + private $_default_session_vars = array( |
|
| 136 | + 'id' => null, |
|
| 137 | + 'user_id' => null, |
|
| 138 | + 'ip_address' => null, |
|
| 139 | + 'user_agent' => null, |
|
| 140 | + 'init_access' => null, |
|
| 141 | + 'last_access' => null, |
|
| 142 | + 'expiration' => null, |
|
| 143 | + 'pages_visited' => array(), |
|
| 144 | + ); |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * timestamp for when last garbage collection cycle was performed |
|
| 148 | + * |
|
| 149 | + * @var int $_last_gc |
|
| 150 | + */ |
|
| 151 | + private $_last_gc; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * whether session is active or not |
|
| 155 | + * |
|
| 156 | + * @var int $status |
|
| 157 | + */ |
|
| 158 | + private $status = EE_Session::EE_SESSION_STATUS_CLOSED; |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @singleton method used to instantiate class object |
|
| 164 | + * @param CacheStorageInterface $cache_storage |
|
| 165 | + * @param EE_Encryption $encryption |
|
| 166 | + * @return EE_Session |
|
| 167 | + * @throws InvalidArgumentException |
|
| 168 | + * @throws InvalidDataTypeException |
|
| 169 | + * @throws InvalidInterfaceException |
|
| 170 | + */ |
|
| 171 | + public static function instance( |
|
| 172 | + CacheStorageInterface $cache_storage = null, |
|
| 173 | + EE_Encryption $encryption = null |
|
| 174 | + ) { |
|
| 175 | + // check if class object is instantiated |
|
| 176 | + // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
|
| 177 | + // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
| 178 | + if (! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
| 179 | + self::$_instance = new self($cache_storage, $encryption); |
|
| 180 | + } |
|
| 181 | + return self::$_instance; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * protected constructor to prevent direct creation |
|
| 188 | + * |
|
| 189 | + * @param CacheStorageInterface $cache_storage |
|
| 190 | + * @param EE_Encryption $encryption |
|
| 191 | + * @throws InvalidArgumentException |
|
| 192 | + * @throws InvalidDataTypeException |
|
| 193 | + * @throws InvalidInterfaceException |
|
| 194 | + */ |
|
| 195 | + protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null) |
|
| 196 | + { |
|
| 197 | + // session loading is turned ON by default, |
|
| 198 | + // but prior to the 'AHEE__EE_System__core_loaded_and_ready' hook |
|
| 199 | + // (which currently fires on the init hook at priority 9), |
|
| 200 | + // can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
|
| 201 | + if (! apply_filters('FHEE_load_EE_Session', true)) { |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | + if (! defined('ESPRESSO_SESSION')) { |
|
| 205 | + define('ESPRESSO_SESSION', true); |
|
| 206 | + } |
|
| 207 | + // default session lifespan in seconds |
|
| 208 | + $this->_lifespan = apply_filters( |
|
| 209 | + 'FHEE__EE_Session__construct___lifespan', |
|
| 210 | + 60 * MINUTE_IN_SECONDS |
|
| 211 | + ) + 1; |
|
| 212 | + /* |
|
| 213 | 213 | * do something like the following to adjust the session lifespan: |
| 214 | 214 | * public static function session_lifespan() { |
| 215 | 215 | * return 15 * MINUTE_IN_SECONDS; |
| 216 | 216 | * } |
| 217 | 217 | */ |
| 218 | - // retrieve session options from db |
|
| 219 | - $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
| 220 | - if (! empty($session_settings)) { |
|
| 221 | - // cycle though existing session options |
|
| 222 | - foreach ($session_settings as $var_name => $session_setting) { |
|
| 223 | - // set values for class properties |
|
| 224 | - $var_name = '_' . $var_name; |
|
| 225 | - $this->{$var_name} = $session_setting; |
|
| 226 | - } |
|
| 227 | - } |
|
| 228 | - $this->cache_storage = $cache_storage; |
|
| 229 | - // are we using encryption? |
|
| 230 | - $this->_use_encryption = $encryption instanceof EE_Encryption |
|
| 231 | - && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
| 232 | - // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
| 233 | - // encrypt data via: $this->encryption->encrypt(); |
|
| 234 | - $this->encryption = $encryption; |
|
| 235 | - // filter hook allows outside functions/classes/plugins to change default empty cart |
|
| 236 | - $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
| 237 | - array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
| 238 | - // apply default session vars |
|
| 239 | - $this->_set_defaults(); |
|
| 240 | - add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
| 241 | - // check request for 'clear_session' param |
|
| 242 | - add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
| 243 | - // once everything is all said and done, |
|
| 244 | - add_action('shutdown', array($this, 'update'), 100); |
|
| 245 | - add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
| 246 | - $this->configure_garbage_collection_filters(); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @return bool |
|
| 252 | - * @throws InvalidArgumentException |
|
| 253 | - * @throws InvalidDataTypeException |
|
| 254 | - * @throws InvalidInterfaceException |
|
| 255 | - */ |
|
| 256 | - public static function isLoadedAndActive() |
|
| 257 | - { |
|
| 258 | - return did_action('AHEE__EE_System__core_loaded_and_ready') |
|
| 259 | - && EE_Session::instance() instanceof EE_Session |
|
| 260 | - && EE_Session::instance()->isActive(); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @return bool |
|
| 266 | - */ |
|
| 267 | - public function isActive() |
|
| 268 | - { |
|
| 269 | - return $this->status === EE_Session::EE_SESSION_STATUS_OPEN; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @return void |
|
| 276 | - * @throws EE_Error |
|
| 277 | - * @throws InvalidArgumentException |
|
| 278 | - * @throws InvalidDataTypeException |
|
| 279 | - * @throws InvalidInterfaceException |
|
| 280 | - * @throws InvalidSessionDataException |
|
| 281 | - */ |
|
| 282 | - public function open_session() |
|
| 283 | - { |
|
| 284 | - // check for existing session and retrieve it from db |
|
| 285 | - if (! $this->_espresso_session()) { |
|
| 286 | - // or just start a new one |
|
| 287 | - $this->_create_espresso_session(); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @return bool |
|
| 295 | - */ |
|
| 296 | - public function expired() |
|
| 297 | - { |
|
| 298 | - return $this->_expired; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * @return void |
|
| 305 | - */ |
|
| 306 | - public function reset_expired() |
|
| 307 | - { |
|
| 308 | - $this->_expired = false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @return int |
|
| 314 | - */ |
|
| 315 | - public function expiration() |
|
| 316 | - { |
|
| 317 | - return $this->_expiration; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @return int |
|
| 324 | - */ |
|
| 325 | - public function extension() |
|
| 326 | - { |
|
| 327 | - return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @param int $time number of seconds to add to session expiration |
|
| 334 | - */ |
|
| 335 | - public function extend_expiration($time = 0) |
|
| 336 | - { |
|
| 337 | - $time = $time ? $time : $this->extension(); |
|
| 338 | - $this->_expiration += absint($time); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * @return int |
|
| 345 | - */ |
|
| 346 | - public function lifespan() |
|
| 347 | - { |
|
| 348 | - return $this->_lifespan; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * This just sets some defaults for the _session data property |
|
| 355 | - * |
|
| 356 | - * @access private |
|
| 357 | - * @return void |
|
| 358 | - */ |
|
| 359 | - private function _set_defaults() |
|
| 360 | - { |
|
| 361 | - // set some defaults |
|
| 362 | - foreach ($this->_default_session_vars as $key => $default_var) { |
|
| 363 | - if (is_array($default_var)) { |
|
| 364 | - $this->_session_data[ $key ] = array(); |
|
| 365 | - } else { |
|
| 366 | - $this->_session_data[ $key ] = ''; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - } |
|
| 218 | + // retrieve session options from db |
|
| 219 | + $session_settings = (array) get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
| 220 | + if (! empty($session_settings)) { |
|
| 221 | + // cycle though existing session options |
|
| 222 | + foreach ($session_settings as $var_name => $session_setting) { |
|
| 223 | + // set values for class properties |
|
| 224 | + $var_name = '_' . $var_name; |
|
| 225 | + $this->{$var_name} = $session_setting; |
|
| 226 | + } |
|
| 227 | + } |
|
| 228 | + $this->cache_storage = $cache_storage; |
|
| 229 | + // are we using encryption? |
|
| 230 | + $this->_use_encryption = $encryption instanceof EE_Encryption |
|
| 231 | + && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
| 232 | + // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
| 233 | + // encrypt data via: $this->encryption->encrypt(); |
|
| 234 | + $this->encryption = $encryption; |
|
| 235 | + // filter hook allows outside functions/classes/plugins to change default empty cart |
|
| 236 | + $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
| 237 | + array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
| 238 | + // apply default session vars |
|
| 239 | + $this->_set_defaults(); |
|
| 240 | + add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
| 241 | + // check request for 'clear_session' param |
|
| 242 | + add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
| 243 | + // once everything is all said and done, |
|
| 244 | + add_action('shutdown', array($this, 'update'), 100); |
|
| 245 | + add_action('shutdown', array($this, 'garbageCollection'), 1000); |
|
| 246 | + $this->configure_garbage_collection_filters(); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @return bool |
|
| 252 | + * @throws InvalidArgumentException |
|
| 253 | + * @throws InvalidDataTypeException |
|
| 254 | + * @throws InvalidInterfaceException |
|
| 255 | + */ |
|
| 256 | + public static function isLoadedAndActive() |
|
| 257 | + { |
|
| 258 | + return did_action('AHEE__EE_System__core_loaded_and_ready') |
|
| 259 | + && EE_Session::instance() instanceof EE_Session |
|
| 260 | + && EE_Session::instance()->isActive(); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @return bool |
|
| 266 | + */ |
|
| 267 | + public function isActive() |
|
| 268 | + { |
|
| 269 | + return $this->status === EE_Session::EE_SESSION_STATUS_OPEN; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @return void |
|
| 276 | + * @throws EE_Error |
|
| 277 | + * @throws InvalidArgumentException |
|
| 278 | + * @throws InvalidDataTypeException |
|
| 279 | + * @throws InvalidInterfaceException |
|
| 280 | + * @throws InvalidSessionDataException |
|
| 281 | + */ |
|
| 282 | + public function open_session() |
|
| 283 | + { |
|
| 284 | + // check for existing session and retrieve it from db |
|
| 285 | + if (! $this->_espresso_session()) { |
|
| 286 | + // or just start a new one |
|
| 287 | + $this->_create_espresso_session(); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @return bool |
|
| 295 | + */ |
|
| 296 | + public function expired() |
|
| 297 | + { |
|
| 298 | + return $this->_expired; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * @return void |
|
| 305 | + */ |
|
| 306 | + public function reset_expired() |
|
| 307 | + { |
|
| 308 | + $this->_expired = false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @return int |
|
| 314 | + */ |
|
| 315 | + public function expiration() |
|
| 316 | + { |
|
| 317 | + return $this->_expiration; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @return int |
|
| 324 | + */ |
|
| 325 | + public function extension() |
|
| 326 | + { |
|
| 327 | + return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', 10 * MINUTE_IN_SECONDS); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @param int $time number of seconds to add to session expiration |
|
| 334 | + */ |
|
| 335 | + public function extend_expiration($time = 0) |
|
| 336 | + { |
|
| 337 | + $time = $time ? $time : $this->extension(); |
|
| 338 | + $this->_expiration += absint($time); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * @return int |
|
| 345 | + */ |
|
| 346 | + public function lifespan() |
|
| 347 | + { |
|
| 348 | + return $this->_lifespan; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * This just sets some defaults for the _session data property |
|
| 355 | + * |
|
| 356 | + * @access private |
|
| 357 | + * @return void |
|
| 358 | + */ |
|
| 359 | + private function _set_defaults() |
|
| 360 | + { |
|
| 361 | + // set some defaults |
|
| 362 | + foreach ($this->_default_session_vars as $key => $default_var) { |
|
| 363 | + if (is_array($default_var)) { |
|
| 364 | + $this->_session_data[ $key ] = array(); |
|
| 365 | + } else { |
|
| 366 | + $this->_session_data[ $key ] = ''; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | 370 | |
| 371 | 371 | |
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @retrieve session data |
|
| 375 | - * @access public |
|
| 376 | - * @return string |
|
| 377 | - */ |
|
| 378 | - public function id() |
|
| 379 | - { |
|
| 380 | - return $this->_sid; |
|
| 381 | - } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @retrieve session data |
|
| 375 | + * @access public |
|
| 376 | + * @return string |
|
| 377 | + */ |
|
| 378 | + public function id() |
|
| 379 | + { |
|
| 380 | + return $this->_sid; |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | 383 | |
| 384 | 384 | |
| 385 | - /** |
|
| 386 | - * @param \EE_Cart $cart |
|
| 387 | - * @return bool |
|
| 388 | - */ |
|
| 389 | - public function set_cart(EE_Cart $cart) |
|
| 390 | - { |
|
| 391 | - $this->_session_data['cart'] = $cart; |
|
| 392 | - return true; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * reset_cart |
|
| 399 | - */ |
|
| 400 | - public function reset_cart() |
|
| 401 | - { |
|
| 402 | - do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
| 403 | - $this->_session_data['cart'] = null; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * @return \EE_Cart |
|
| 410 | - */ |
|
| 411 | - public function cart() |
|
| 412 | - { |
|
| 413 | - return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
| 414 | - ? $this->_session_data['cart'] |
|
| 415 | - : null; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * @param \EE_Checkout $checkout |
|
| 422 | - * @return bool |
|
| 423 | - */ |
|
| 424 | - public function set_checkout(EE_Checkout $checkout) |
|
| 425 | - { |
|
| 426 | - $this->_session_data['checkout'] = $checkout; |
|
| 427 | - return true; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * reset_checkout |
|
| 434 | - */ |
|
| 435 | - public function reset_checkout() |
|
| 436 | - { |
|
| 437 | - do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
| 438 | - $this->_session_data['checkout'] = null; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * @return \EE_Checkout |
|
| 445 | - */ |
|
| 446 | - public function checkout() |
|
| 447 | - { |
|
| 448 | - return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
| 449 | - ? $this->_session_data['checkout'] |
|
| 450 | - : null; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * @param \EE_Transaction $transaction |
|
| 457 | - * @return bool |
|
| 458 | - * @throws EE_Error |
|
| 459 | - */ |
|
| 460 | - public function set_transaction(EE_Transaction $transaction) |
|
| 461 | - { |
|
| 462 | - // first remove the session from the transaction before we save the transaction in the session |
|
| 463 | - $transaction->set_txn_session_data(null); |
|
| 464 | - $this->_session_data['transaction'] = $transaction; |
|
| 465 | - return true; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * reset_transaction |
|
| 472 | - */ |
|
| 473 | - public function reset_transaction() |
|
| 474 | - { |
|
| 475 | - do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
| 476 | - $this->_session_data['transaction'] = null; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * @return \EE_Transaction |
|
| 483 | - */ |
|
| 484 | - public function transaction() |
|
| 485 | - { |
|
| 486 | - return isset($this->_session_data['transaction']) |
|
| 487 | - && $this->_session_data['transaction'] instanceof EE_Transaction |
|
| 488 | - ? $this->_session_data['transaction'] |
|
| 489 | - : null; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * retrieve session data |
|
| 495 | - * |
|
| 496 | - * @param null $key |
|
| 497 | - * @param bool $reset_cache |
|
| 498 | - * @return array |
|
| 499 | - */ |
|
| 500 | - public function get_session_data($key = null, $reset_cache = false) |
|
| 501 | - { |
|
| 502 | - if ($reset_cache) { |
|
| 503 | - $this->reset_cart(); |
|
| 504 | - $this->reset_checkout(); |
|
| 505 | - $this->reset_transaction(); |
|
| 506 | - } |
|
| 507 | - if (! empty($key)) { |
|
| 508 | - return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
| 509 | - } |
|
| 510 | - return $this->_session_data; |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Returns TRUE on success, FALSE on fail |
|
| 516 | - * |
|
| 517 | - * @param array $data |
|
| 518 | - * @return bool |
|
| 519 | - */ |
|
| 520 | - public function set_session_data($data) |
|
| 521 | - { |
|
| 522 | - |
|
| 523 | - // nothing ??? bad data ??? go home! |
|
| 524 | - if (empty($data) || ! is_array($data)) { |
|
| 525 | - EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
|
| 526 | - __FUNCTION__, __LINE__); |
|
| 527 | - return false; |
|
| 528 | - } |
|
| 529 | - foreach ($data as $key => $value) { |
|
| 530 | - if (isset($this->_default_session_vars[ $key ])) { |
|
| 531 | - EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
| 532 | - 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
|
| 533 | - return false; |
|
| 534 | - } |
|
| 535 | - $this->_session_data[ $key ] = $value; |
|
| 536 | - } |
|
| 537 | - return true; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * @initiate session |
|
| 544 | - * @access private |
|
| 545 | - * @return TRUE on success, FALSE on fail |
|
| 546 | - * @throws EE_Error |
|
| 547 | - * @throws InvalidArgumentException |
|
| 548 | - * @throws InvalidDataTypeException |
|
| 549 | - * @throws InvalidInterfaceException |
|
| 550 | - * @throws InvalidSessionDataException |
|
| 551 | - */ |
|
| 552 | - private function _espresso_session() |
|
| 553 | - { |
|
| 554 | - do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 555 | - // check that session has started |
|
| 556 | - if (session_id() === '') { |
|
| 557 | - //starts a new session if one doesn't already exist, or re-initiates an existing one |
|
| 558 | - session_start(); |
|
| 559 | - } |
|
| 560 | - $this->status = EE_Session::EE_SESSION_STATUS_OPEN; |
|
| 561 | - // get our modified session ID |
|
| 562 | - $this->_sid = $this->_generate_session_id(); |
|
| 563 | - // and the visitors IP |
|
| 564 | - $this->_ip_address = $this->_visitor_ip(); |
|
| 565 | - // set the "user agent" |
|
| 566 | - $this->_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr($_SERVER['HTTP_USER_AGENT']) : false; |
|
| 567 | - // now let's retrieve what's in the db |
|
| 568 | - $session_data = $this->_retrieve_session_data(); |
|
| 569 | - if (! empty($session_data)) { |
|
| 570 | - // get the current time in UTC |
|
| 571 | - $this->_time = $this->_time !== null ? $this->_time : time(); |
|
| 572 | - // and reset the session expiration |
|
| 573 | - $this->_expiration = isset($session_data['expiration']) |
|
| 574 | - ? $session_data['expiration'] |
|
| 575 | - : $this->_time + $this->_lifespan; |
|
| 576 | - } else { |
|
| 577 | - // set initial site access time and the session expiration |
|
| 578 | - $this->_set_init_access_and_expiration(); |
|
| 579 | - // set referer |
|
| 580 | - $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = isset($_SERVER['HTTP_REFERER']) |
|
| 581 | - ? esc_attr($_SERVER['HTTP_REFERER']) |
|
| 582 | - : ''; |
|
| 583 | - // no previous session = go back and create one (on top of the data above) |
|
| 584 | - return false; |
|
| 585 | - } |
|
| 586 | - // now the user agent |
|
| 587 | - if ($session_data['user_agent'] !== $this->_user_agent) { |
|
| 588 | - return false; |
|
| 589 | - } |
|
| 590 | - // wait a minute... how old are you? |
|
| 591 | - if ($this->_time > $this->_expiration) { |
|
| 592 | - // yer too old fer me! |
|
| 593 | - $this->_expired = true; |
|
| 594 | - // wipe out everything that isn't a default session datum |
|
| 595 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 596 | - } |
|
| 597 | - // make event espresso session data available to plugin |
|
| 598 | - $this->_session_data = array_merge($this->_session_data, $session_data); |
|
| 599 | - return true; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * _get_session_data |
|
| 606 | - * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
| 607 | - * databases |
|
| 608 | - * |
|
| 609 | - * @return array |
|
| 610 | - * @throws EE_Error |
|
| 611 | - * @throws InvalidArgumentException |
|
| 612 | - * @throws InvalidSessionDataException |
|
| 613 | - * @throws InvalidDataTypeException |
|
| 614 | - * @throws InvalidInterfaceException |
|
| 615 | - */ |
|
| 616 | - protected function _retrieve_session_data() |
|
| 617 | - { |
|
| 618 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
| 619 | - try { |
|
| 620 | - // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
| 621 | - $session_data = $this->cache_storage->get($ssn_key, false); |
|
| 622 | - if (empty($session_data)) { |
|
| 623 | - return array(); |
|
| 624 | - } |
|
| 625 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 626 | - $hash_check = $this->cache_storage->get( |
|
| 627 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 628 | - false |
|
| 629 | - ); |
|
| 630 | - if ($hash_check && $hash_check !== md5($session_data)) { |
|
| 631 | - EE_Error::add_error( |
|
| 632 | - sprintf( |
|
| 633 | - __( |
|
| 634 | - 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
| 635 | - 'event_espresso' |
|
| 636 | - ), |
|
| 637 | - EE_Session::session_id_prefix . $this->_sid |
|
| 638 | - ), |
|
| 639 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 640 | - ); |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - } catch (Exception $e) { |
|
| 644 | - // let's just eat that error for now and attempt to correct any corrupted data |
|
| 645 | - global $wpdb; |
|
| 646 | - $row = $wpdb->get_row( |
|
| 647 | - $wpdb->prepare( |
|
| 648 | - "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
| 649 | - '_transient_' . $ssn_key |
|
| 650 | - ) |
|
| 651 | - ); |
|
| 652 | - $session_data = is_object($row) ? $row->option_value : null; |
|
| 653 | - if ($session_data) { |
|
| 654 | - $session_data = preg_replace_callback( |
|
| 655 | - '!s:(d+):"(.*?)";!', |
|
| 656 | - function ($match) |
|
| 657 | - { |
|
| 658 | - return $match[1] === strlen($match[2]) |
|
| 659 | - ? $match[0] |
|
| 660 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
| 661 | - }, |
|
| 662 | - $session_data |
|
| 663 | - ); |
|
| 664 | - } |
|
| 665 | - $session_data = maybe_unserialize($session_data); |
|
| 666 | - } |
|
| 667 | - // in case the data is encoded... try to decode it |
|
| 668 | - $session_data = $this->encryption instanceof EE_Encryption |
|
| 669 | - ? $this->encryption->base64_string_decode($session_data) |
|
| 670 | - : $session_data; |
|
| 671 | - if (! is_array($session_data)) { |
|
| 672 | - try { |
|
| 673 | - $session_data = maybe_unserialize($session_data); |
|
| 674 | - } catch (Exception $e) { |
|
| 675 | - $msg = esc_html__( |
|
| 676 | - 'An error occurred while attempting to unserialize the session data.', |
|
| 677 | - 'event_espresso' |
|
| 678 | - ); |
|
| 679 | - $msg .= WP_DEBUG |
|
| 680 | - ? '<br><pre>' |
|
| 681 | - . print_r($session_data, true) |
|
| 682 | - . '</pre><br>' |
|
| 683 | - . $this->find_serialize_error($session_data) |
|
| 684 | - : ''; |
|
| 685 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
| 686 | - throw new InvalidSessionDataException($msg, 0, $e); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - // just a check to make sure the session array is indeed an array |
|
| 690 | - if (! is_array($session_data)) { |
|
| 691 | - // no?!?! then something is wrong |
|
| 692 | - $msg = esc_html__( |
|
| 693 | - 'The session data is missing, invalid, or corrupted.', |
|
| 694 | - 'event_espresso' |
|
| 695 | - ); |
|
| 696 | - $msg .= WP_DEBUG |
|
| 697 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 698 | - : ''; |
|
| 699 | - $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
| 700 | - throw new InvalidSessionDataException($msg); |
|
| 701 | - } |
|
| 702 | - if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
| 703 | - $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
| 704 | - $session_data['transaction'] |
|
| 705 | - ); |
|
| 706 | - } |
|
| 707 | - return $session_data; |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - |
|
| 711 | - |
|
| 712 | - /** |
|
| 713 | - * _generate_session_id |
|
| 714 | - * Retrieves the PHP session id either directly from the PHP session, |
|
| 715 | - * or from the $_REQUEST array if it was passed in from an AJAX request. |
|
| 716 | - * The session id is then salted and hashed (mmm sounds tasty) |
|
| 717 | - * so that it can be safely used as a $_REQUEST param |
|
| 718 | - * |
|
| 719 | - * @return string |
|
| 720 | - */ |
|
| 721 | - protected function _generate_session_id() |
|
| 722 | - { |
|
| 723 | - // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
| 724 | - if (isset($_REQUEST['EESID'])) { |
|
| 725 | - $session_id = sanitize_text_field($_REQUEST['EESID']); |
|
| 726 | - } else { |
|
| 727 | - $session_id = md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
| 728 | - } |
|
| 729 | - return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * _get_sid_salt |
|
| 736 | - * |
|
| 737 | - * @return string |
|
| 738 | - */ |
|
| 739 | - protected function _get_sid_salt() |
|
| 740 | - { |
|
| 741 | - // was session id salt already saved to db ? |
|
| 742 | - if (empty($this->_sid_salt)) { |
|
| 743 | - // no? then maybe use WP defined constant |
|
| 744 | - if (defined('AUTH_SALT')) { |
|
| 745 | - $this->_sid_salt = AUTH_SALT; |
|
| 746 | - } |
|
| 747 | - // if salt doesn't exist or is too short |
|
| 748 | - if (strlen($this->_sid_salt) < 32) { |
|
| 749 | - // create a new one |
|
| 750 | - $this->_sid_salt = wp_generate_password(64); |
|
| 751 | - } |
|
| 752 | - // and save it as a permanent session setting |
|
| 753 | - $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
| 754 | - } |
|
| 755 | - return $this->_sid_salt; |
|
| 756 | - } |
|
| 757 | - |
|
| 758 | - |
|
| 759 | - |
|
| 760 | - /** |
|
| 761 | - * _set_init_access_and_expiration |
|
| 762 | - * |
|
| 763 | - * @return void |
|
| 764 | - */ |
|
| 765 | - protected function _set_init_access_and_expiration() |
|
| 766 | - { |
|
| 767 | - $this->_time = time(); |
|
| 768 | - $this->_expiration = $this->_time + $this->_lifespan; |
|
| 769 | - // set initial site access time |
|
| 770 | - $this->_session_data['init_access'] = $this->_time; |
|
| 771 | - // and the session expiration |
|
| 772 | - $this->_session_data['expiration'] = $this->_expiration; |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - |
|
| 776 | - |
|
| 777 | - /** |
|
| 778 | - * @update session data prior to saving to the db |
|
| 779 | - * @access public |
|
| 780 | - * @param bool $new_session |
|
| 781 | - * @return TRUE on success, FALSE on fail |
|
| 782 | - * @throws EE_Error |
|
| 783 | - * @throws InvalidArgumentException |
|
| 784 | - * @throws InvalidDataTypeException |
|
| 785 | - * @throws InvalidInterfaceException |
|
| 786 | - */ |
|
| 787 | - public function update($new_session = false) |
|
| 788 | - { |
|
| 789 | - $this->_session_data = $this->_session_data !== null |
|
| 790 | - && is_array($this->_session_data) |
|
| 791 | - && isset($this->_session_data['id']) |
|
| 792 | - ? $this->_session_data |
|
| 793 | - : array(); |
|
| 794 | - if (empty($this->_session_data)) { |
|
| 795 | - $this->_set_defaults(); |
|
| 796 | - } |
|
| 797 | - $session_data = array(); |
|
| 798 | - foreach ($this->_session_data as $key => $value) { |
|
| 799 | - |
|
| 800 | - switch ($key) { |
|
| 801 | - |
|
| 802 | - case 'id' : |
|
| 803 | - // session ID |
|
| 804 | - $session_data['id'] = $this->_sid; |
|
| 805 | - break; |
|
| 806 | - case 'ip_address' : |
|
| 807 | - // visitor ip address |
|
| 808 | - $session_data['ip_address'] = $this->_visitor_ip(); |
|
| 809 | - break; |
|
| 810 | - case 'user_agent' : |
|
| 811 | - // visitor user_agent |
|
| 812 | - $session_data['user_agent'] = $this->_user_agent; |
|
| 813 | - break; |
|
| 814 | - case 'init_access' : |
|
| 815 | - $session_data['init_access'] = absint($value); |
|
| 816 | - break; |
|
| 817 | - case 'last_access' : |
|
| 818 | - // current access time |
|
| 819 | - $session_data['last_access'] = $this->_time; |
|
| 820 | - break; |
|
| 821 | - case 'expiration' : |
|
| 822 | - // when the session expires |
|
| 823 | - $session_data['expiration'] = ! empty($this->_expiration) |
|
| 824 | - ? $this->_expiration |
|
| 825 | - : $session_data['init_access'] + $this->_lifespan; |
|
| 826 | - break; |
|
| 827 | - case 'user_id' : |
|
| 828 | - // current user if logged in |
|
| 829 | - $session_data['user_id'] = $this->_wp_user_id(); |
|
| 830 | - break; |
|
| 831 | - case 'pages_visited' : |
|
| 832 | - $page_visit = $this->_get_page_visit(); |
|
| 833 | - if ($page_visit) { |
|
| 834 | - // set pages visited where the first will be the http referrer |
|
| 835 | - $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
| 836 | - // we'll only save the last 10 page visits. |
|
| 837 | - $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
| 838 | - } |
|
| 839 | - break; |
|
| 840 | - default : |
|
| 841 | - // carry any other data over |
|
| 842 | - $session_data[ $key ] = $this->_session_data[ $key ]; |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - $this->_session_data = $session_data; |
|
| 846 | - // creating a new session does not require saving to the db just yet |
|
| 847 | - if (! $new_session) { |
|
| 848 | - // ready? let's save |
|
| 849 | - if ($this->_save_session_to_db()) { |
|
| 850 | - return true; |
|
| 851 | - } |
|
| 852 | - return false; |
|
| 853 | - } |
|
| 854 | - // meh, why not? |
|
| 855 | - return true; |
|
| 856 | - } |
|
| 857 | - |
|
| 858 | - |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * @create session data array |
|
| 862 | - * @access public |
|
| 863 | - * @return bool |
|
| 864 | - * @throws EE_Error |
|
| 865 | - * @throws InvalidArgumentException |
|
| 866 | - * @throws InvalidDataTypeException |
|
| 867 | - * @throws InvalidInterfaceException |
|
| 868 | - */ |
|
| 869 | - private function _create_espresso_session() |
|
| 870 | - { |
|
| 871 | - do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
| 872 | - // use the update function for now with $new_session arg set to TRUE |
|
| 873 | - return $this->update(true) ? true : false; |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * _save_session_to_db |
|
| 880 | - * |
|
| 881 | - * @param bool $clear_session |
|
| 882 | - * @return string |
|
| 883 | - * @throws EE_Error |
|
| 884 | - * @throws InvalidArgumentException |
|
| 885 | - * @throws InvalidDataTypeException |
|
| 886 | - * @throws InvalidInterfaceException |
|
| 887 | - */ |
|
| 888 | - private function _save_session_to_db($clear_session = false) |
|
| 889 | - { |
|
| 890 | - // unless we're deleting the session data, don't save anything if there isn't a cart |
|
| 891 | - if (! $clear_session && ! $this->cart() instanceof EE_Cart) { |
|
| 892 | - return false; |
|
| 893 | - } |
|
| 894 | - $transaction = $this->transaction(); |
|
| 895 | - if ($transaction instanceof EE_Transaction) { |
|
| 896 | - if (! $transaction->ID()) { |
|
| 897 | - $transaction->save(); |
|
| 898 | - } |
|
| 899 | - $this->_session_data['transaction'] = $transaction->ID(); |
|
| 900 | - } |
|
| 901 | - // then serialize all of our session data |
|
| 902 | - $session_data = serialize($this->_session_data); |
|
| 903 | - // do we need to also encode it to avoid corrupted data when saved to the db? |
|
| 904 | - $session_data = $this->_use_encryption |
|
| 905 | - ? $this->encryption->base64_string_encode($session_data) |
|
| 906 | - : $session_data; |
|
| 907 | - // maybe save hash check |
|
| 908 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 909 | - $this->cache_storage->add( |
|
| 910 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 911 | - md5($session_data), |
|
| 912 | - $this->_lifespan |
|
| 913 | - ); |
|
| 914 | - } |
|
| 915 | - // we're using the Transient API for storing session data, |
|
| 916 | - return $this->cache_storage->add( |
|
| 917 | - EE_Session::session_id_prefix . $this->_sid, |
|
| 918 | - $session_data, |
|
| 919 | - $this->_lifespan |
|
| 920 | - ); |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - |
|
| 924 | - |
|
| 925 | - /** |
|
| 926 | - * _visitor_ip |
|
| 927 | - * attempt to get IP address of current visitor from server |
|
| 928 | - * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 929 | - * |
|
| 930 | - * @access public |
|
| 931 | - * @return string |
|
| 932 | - */ |
|
| 933 | - private function _visitor_ip() |
|
| 934 | - { |
|
| 935 | - $visitor_ip = '0.0.0.0'; |
|
| 936 | - $server_keys = array( |
|
| 937 | - 'HTTP_CLIENT_IP', |
|
| 938 | - 'HTTP_X_FORWARDED_FOR', |
|
| 939 | - 'HTTP_X_FORWARDED', |
|
| 940 | - 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 941 | - 'HTTP_FORWARDED_FOR', |
|
| 942 | - 'HTTP_FORWARDED', |
|
| 943 | - 'REMOTE_ADDR', |
|
| 944 | - ); |
|
| 945 | - foreach ($server_keys as $key) { |
|
| 946 | - if (isset($_SERVER[ $key ])) { |
|
| 947 | - foreach (array_map('trim', explode(',', $_SERVER[ $key ])) as $ip) { |
|
| 948 | - if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 949 | - $visitor_ip = $ip; |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - } |
|
| 953 | - } |
|
| 954 | - return $visitor_ip; |
|
| 955 | - } |
|
| 956 | - |
|
| 957 | - |
|
| 958 | - |
|
| 959 | - /** |
|
| 960 | - * @get the full page request the visitor is accessing |
|
| 961 | - * @access public |
|
| 962 | - * @return string |
|
| 963 | - */ |
|
| 964 | - public function _get_page_visit() |
|
| 965 | - { |
|
| 966 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
| 967 | - // check for request url |
|
| 968 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
| 969 | - $http_host = ''; |
|
| 970 | - $page_id = '?'; |
|
| 971 | - $e_reg = ''; |
|
| 972 | - $request_uri = esc_url($_SERVER['REQUEST_URI']); |
|
| 973 | - $ru_bits = explode('?', $request_uri); |
|
| 974 | - $request_uri = $ru_bits[0]; |
|
| 975 | - // check for and grab host as well |
|
| 976 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
| 977 | - $http_host = esc_url($_SERVER['HTTP_HOST']); |
|
| 978 | - } |
|
| 979 | - // check for page_id in SERVER REQUEST |
|
| 980 | - if (isset($_REQUEST['page_id'])) { |
|
| 981 | - // rebuild $e_reg without any of the extra parameters |
|
| 982 | - $page_id = '?page_id=' . esc_attr($_REQUEST['page_id']) . '&'; |
|
| 983 | - } |
|
| 984 | - // check for $e_reg in SERVER REQUEST |
|
| 985 | - if (isset($_REQUEST['ee'])) { |
|
| 986 | - // rebuild $e_reg without any of the extra parameters |
|
| 987 | - $e_reg = 'ee=' . esc_attr($_REQUEST['ee']); |
|
| 988 | - } |
|
| 989 | - $page_visit = rtrim($http_host . $request_uri . $page_id . $e_reg, '?'); |
|
| 990 | - } |
|
| 991 | - return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
| 992 | - } |
|
| 993 | - |
|
| 994 | - |
|
| 995 | - |
|
| 996 | - /** |
|
| 997 | - * @the current wp user id |
|
| 998 | - * @access public |
|
| 999 | - * @return int |
|
| 1000 | - */ |
|
| 1001 | - public function _wp_user_id() |
|
| 1002 | - { |
|
| 1003 | - // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
| 1004 | - $this->_wp_user_id = get_current_user_id(); |
|
| 1005 | - return $this->_wp_user_id; |
|
| 1006 | - } |
|
| 1007 | - |
|
| 1008 | - |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * Clear EE_Session data |
|
| 1012 | - * |
|
| 1013 | - * @access public |
|
| 1014 | - * @param string $class |
|
| 1015 | - * @param string $function |
|
| 1016 | - * @return void |
|
| 1017 | - * @throws EE_Error |
|
| 1018 | - * @throws InvalidArgumentException |
|
| 1019 | - * @throws InvalidDataTypeException |
|
| 1020 | - * @throws InvalidInterfaceException |
|
| 1021 | - */ |
|
| 1022 | - public function clear_session($class = '', $function = '') |
|
| 1023 | - { |
|
| 1024 | - //echo '<h3 style="color:#999;line-height:.9em;"><span style="color:#2EA2CC">' . __CLASS__ . '</span>::<span style="color:#E76700">' . __FUNCTION__ . '( ' . $class . '::' . $function . '() )</span><br/><span style="font-size:9px;font-weight:normal;">' . __FILE__ . '</span> <b style="font-size:10px;"> ' . __LINE__ . ' </b></h3>'; |
|
| 1025 | - do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
| 1026 | - $this->reset_cart(); |
|
| 1027 | - $this->reset_checkout(); |
|
| 1028 | - $this->reset_transaction(); |
|
| 1029 | - // wipe out everything that isn't a default session datum |
|
| 1030 | - $this->reset_data(array_keys($this->_session_data)); |
|
| 1031 | - // reset initial site access time and the session expiration |
|
| 1032 | - $this->_set_init_access_and_expiration(); |
|
| 1033 | - $this->_save_session_to_db(true); |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - |
|
| 1037 | - /** |
|
| 1038 | - * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
|
| 1039 | - * |
|
| 1040 | - * @param array|mixed $data_to_reset |
|
| 1041 | - * @param bool $show_all_notices |
|
| 1042 | - * @return bool |
|
| 1043 | - */ |
|
| 1044 | - public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
| 1045 | - { |
|
| 1046 | - // if $data_to_reset is not in an array, then put it in one |
|
| 1047 | - if (! is_array($data_to_reset)) { |
|
| 1048 | - $data_to_reset = array($data_to_reset); |
|
| 1049 | - } |
|
| 1050 | - // nothing ??? go home! |
|
| 1051 | - if (empty($data_to_reset)) { |
|
| 1052 | - EE_Error::add_error(__('No session data could be reset, because no session var name was provided.', |
|
| 1053 | - 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 1054 | - return false; |
|
| 1055 | - } |
|
| 1056 | - $return_value = true; |
|
| 1057 | - // since $data_to_reset is an array, cycle through the values |
|
| 1058 | - foreach ($data_to_reset as $reset) { |
|
| 1059 | - |
|
| 1060 | - // first check to make sure it is a valid session var |
|
| 1061 | - if (isset($this->_session_data[ $reset ])) { |
|
| 1062 | - // then check to make sure it is not a default var |
|
| 1063 | - if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
| 1064 | - // remove session var |
|
| 1065 | - unset($this->_session_data[ $reset ]); |
|
| 1066 | - if ($show_all_notices) { |
|
| 1067 | - EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), |
|
| 1068 | - $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1069 | - } |
|
| 1070 | - } else { |
|
| 1071 | - // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
| 1072 | - if ($show_all_notices) { |
|
| 1073 | - EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
| 1074 | - 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1075 | - } |
|
| 1076 | - $return_value = false; |
|
| 1077 | - } |
|
| 1078 | - } elseif ($show_all_notices) { |
|
| 1079 | - // oops! that session var does not exist! |
|
| 1080 | - EE_Error::add_error(sprintf(__('The session item provided, %s, is invalid or does not exist.', |
|
| 1081 | - 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1082 | - $return_value = false; |
|
| 1083 | - } |
|
| 1084 | - } // end of foreach |
|
| 1085 | - return $return_value; |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - |
|
| 1089 | - |
|
| 1090 | - /** |
|
| 1091 | - * wp_loaded |
|
| 1092 | - * |
|
| 1093 | - * @access public |
|
| 1094 | - * @throws EE_Error |
|
| 1095 | - * @throws InvalidDataTypeException |
|
| 1096 | - * @throws InvalidInterfaceException |
|
| 1097 | - * @throws InvalidArgumentException |
|
| 1098 | - */ |
|
| 1099 | - public function wp_loaded() |
|
| 1100 | - { |
|
| 1101 | - if ( |
|
| 1102 | - EE_Registry::instance()->REQ instanceof EE_Request_Handler |
|
| 1103 | - && EE_Registry::instance()->REQ->is_set('clear_session') |
|
| 1104 | - ) { |
|
| 1105 | - $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 1106 | - } |
|
| 1107 | - } |
|
| 1108 | - |
|
| 1109 | - |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * Used to reset the entire object (for tests). |
|
| 1113 | - * |
|
| 1114 | - * @since 4.3.0 |
|
| 1115 | - * @throws EE_Error |
|
| 1116 | - * @throws InvalidDataTypeException |
|
| 1117 | - * @throws InvalidInterfaceException |
|
| 1118 | - * @throws InvalidArgumentException |
|
| 1119 | - */ |
|
| 1120 | - public function reset_instance() |
|
| 1121 | - { |
|
| 1122 | - $this->clear_session(); |
|
| 1123 | - self::$_instance = null; |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - |
|
| 1127 | - |
|
| 1128 | - public function configure_garbage_collection_filters() |
|
| 1129 | - { |
|
| 1130 | - // run old filter we had for controlling session cleanup |
|
| 1131 | - $expired_session_transient_delete_query_limit = absint( |
|
| 1132 | - apply_filters( |
|
| 1133 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1134 | - 50 |
|
| 1135 | - ) |
|
| 1136 | - ); |
|
| 1137 | - // is there a value? or one that is different than the default 50 records? |
|
| 1138 | - if ($expired_session_transient_delete_query_limit === 0) { |
|
| 1139 | - // hook into TransientCacheStorage in case Session cleanup was turned off |
|
| 1140 | - add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
| 1141 | - } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
| 1142 | - // or use that for the new transient cleanup query limit |
|
| 1143 | - add_filter( |
|
| 1144 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 1145 | - function () use ($expired_session_transient_delete_query_limit) |
|
| 1146 | - { |
|
| 1147 | - return $expired_session_transient_delete_query_limit; |
|
| 1148 | - } |
|
| 1149 | - ); |
|
| 1150 | - } |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - |
|
| 1154 | - |
|
| 1155 | - /** |
|
| 1156 | - * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
| 1157 | - * @param $data1 |
|
| 1158 | - * @return string |
|
| 1159 | - */ |
|
| 1160 | - private function find_serialize_error($data1) |
|
| 1161 | - { |
|
| 1162 | - $error = '<pre>'; |
|
| 1163 | - $data2 = preg_replace_callback( |
|
| 1164 | - '!s:(\d+):"(.*?)";!', |
|
| 1165 | - function ($match) |
|
| 1166 | - { |
|
| 1167 | - return ($match[1] === strlen($match[2])) |
|
| 1168 | - ? $match[0] |
|
| 1169 | - : 's:' |
|
| 1170 | - . strlen($match[2]) |
|
| 1171 | - . ':"' |
|
| 1172 | - . $match[2] |
|
| 1173 | - . '";'; |
|
| 1174 | - }, |
|
| 1175 | - $data1 |
|
| 1176 | - ); |
|
| 1177 | - $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
| 1178 | - $error .= $data1 . PHP_EOL; |
|
| 1179 | - $error .= $data2 . PHP_EOL; |
|
| 1180 | - for ($i = 0; $i < $max; $i++) { |
|
| 1181 | - if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
| 1182 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
| 1183 | - $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
| 1184 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
| 1185 | - $start = ($i - 20); |
|
| 1186 | - $start = ($start < 0) ? 0 : $start; |
|
| 1187 | - $length = 40; |
|
| 1188 | - $point = $max - $i; |
|
| 1189 | - if ($point < 20) { |
|
| 1190 | - $rlength = 1; |
|
| 1191 | - $rpoint = -$point; |
|
| 1192 | - } else { |
|
| 1193 | - $rpoint = $length - 20; |
|
| 1194 | - $rlength = 1; |
|
| 1195 | - } |
|
| 1196 | - $error .= "\t-> Section Data1 = "; |
|
| 1197 | - $error .= substr_replace( |
|
| 1198 | - substr($data1, $start, $length), |
|
| 1199 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
| 1200 | - $rpoint, |
|
| 1201 | - $rlength |
|
| 1202 | - ); |
|
| 1203 | - $error .= PHP_EOL; |
|
| 1204 | - $error .= "\t-> Section Data2 = "; |
|
| 1205 | - $error .= substr_replace( |
|
| 1206 | - substr($data2, $start, $length), |
|
| 1207 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
| 1208 | - $rpoint, |
|
| 1209 | - $rlength |
|
| 1210 | - ); |
|
| 1211 | - $error .= PHP_EOL; |
|
| 1212 | - } |
|
| 1213 | - } |
|
| 1214 | - $error .= '</pre>'; |
|
| 1215 | - return $error; |
|
| 1216 | - } |
|
| 1217 | - |
|
| 1218 | - |
|
| 1219 | - /** |
|
| 1220 | - * Saves an array of settings used for configuring aspects of session behaviour |
|
| 1221 | - * |
|
| 1222 | - * @param array $updated_settings |
|
| 1223 | - */ |
|
| 1224 | - private function updateSessionSettings(array $updated_settings = array()) |
|
| 1225 | - { |
|
| 1226 | - // add existing settings, but only if not included in incoming $updated_settings array |
|
| 1227 | - $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
| 1228 | - update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - |
|
| 1232 | - /** |
|
| 1233 | - * garbage_collection |
|
| 1234 | - */ |
|
| 1235 | - public function garbageCollection() |
|
| 1236 | - { |
|
| 1237 | - // only perform during regular requests if last garbage collection was over an hour ago |
|
| 1238 | - if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
| 1239 | - $this->_last_gc = time(); |
|
| 1240 | - $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
| 1241 | - /** @type WPDB $wpdb */ |
|
| 1242 | - global $wpdb; |
|
| 1243 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
| 1244 | - $expired_session_transient_delete_query_limit = absint( |
|
| 1245 | - apply_filters( |
|
| 1246 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1247 | - 50 |
|
| 1248 | - ) |
|
| 1249 | - ); |
|
| 1250 | - // non-zero LIMIT means take out the trash |
|
| 1251 | - if ($expired_session_transient_delete_query_limit) { |
|
| 1252 | - $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
| 1253 | - $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
| 1254 | - // since transient expiration timestamps are set in the future, we can compare against NOW |
|
| 1255 | - // but we only want to pick up any trash that's been around for more than a day |
|
| 1256 | - $expiration = time() - DAY_IN_SECONDS; |
|
| 1257 | - $SQL = " |
|
| 385 | + /** |
|
| 386 | + * @param \EE_Cart $cart |
|
| 387 | + * @return bool |
|
| 388 | + */ |
|
| 389 | + public function set_cart(EE_Cart $cart) |
|
| 390 | + { |
|
| 391 | + $this->_session_data['cart'] = $cart; |
|
| 392 | + return true; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * reset_cart |
|
| 399 | + */ |
|
| 400 | + public function reset_cart() |
|
| 401 | + { |
|
| 402 | + do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
| 403 | + $this->_session_data['cart'] = null; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * @return \EE_Cart |
|
| 410 | + */ |
|
| 411 | + public function cart() |
|
| 412 | + { |
|
| 413 | + return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
| 414 | + ? $this->_session_data['cart'] |
|
| 415 | + : null; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * @param \EE_Checkout $checkout |
|
| 422 | + * @return bool |
|
| 423 | + */ |
|
| 424 | + public function set_checkout(EE_Checkout $checkout) |
|
| 425 | + { |
|
| 426 | + $this->_session_data['checkout'] = $checkout; |
|
| 427 | + return true; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * reset_checkout |
|
| 434 | + */ |
|
| 435 | + public function reset_checkout() |
|
| 436 | + { |
|
| 437 | + do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
| 438 | + $this->_session_data['checkout'] = null; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * @return \EE_Checkout |
|
| 445 | + */ |
|
| 446 | + public function checkout() |
|
| 447 | + { |
|
| 448 | + return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
| 449 | + ? $this->_session_data['checkout'] |
|
| 450 | + : null; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * @param \EE_Transaction $transaction |
|
| 457 | + * @return bool |
|
| 458 | + * @throws EE_Error |
|
| 459 | + */ |
|
| 460 | + public function set_transaction(EE_Transaction $transaction) |
|
| 461 | + { |
|
| 462 | + // first remove the session from the transaction before we save the transaction in the session |
|
| 463 | + $transaction->set_txn_session_data(null); |
|
| 464 | + $this->_session_data['transaction'] = $transaction; |
|
| 465 | + return true; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * reset_transaction |
|
| 472 | + */ |
|
| 473 | + public function reset_transaction() |
|
| 474 | + { |
|
| 475 | + do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
| 476 | + $this->_session_data['transaction'] = null; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * @return \EE_Transaction |
|
| 483 | + */ |
|
| 484 | + public function transaction() |
|
| 485 | + { |
|
| 486 | + return isset($this->_session_data['transaction']) |
|
| 487 | + && $this->_session_data['transaction'] instanceof EE_Transaction |
|
| 488 | + ? $this->_session_data['transaction'] |
|
| 489 | + : null; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * retrieve session data |
|
| 495 | + * |
|
| 496 | + * @param null $key |
|
| 497 | + * @param bool $reset_cache |
|
| 498 | + * @return array |
|
| 499 | + */ |
|
| 500 | + public function get_session_data($key = null, $reset_cache = false) |
|
| 501 | + { |
|
| 502 | + if ($reset_cache) { |
|
| 503 | + $this->reset_cart(); |
|
| 504 | + $this->reset_checkout(); |
|
| 505 | + $this->reset_transaction(); |
|
| 506 | + } |
|
| 507 | + if (! empty($key)) { |
|
| 508 | + return isset($this->_session_data[ $key ]) ? $this->_session_data[ $key ] : null; |
|
| 509 | + } |
|
| 510 | + return $this->_session_data; |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Returns TRUE on success, FALSE on fail |
|
| 516 | + * |
|
| 517 | + * @param array $data |
|
| 518 | + * @return bool |
|
| 519 | + */ |
|
| 520 | + public function set_session_data($data) |
|
| 521 | + { |
|
| 522 | + |
|
| 523 | + // nothing ??? bad data ??? go home! |
|
| 524 | + if (empty($data) || ! is_array($data)) { |
|
| 525 | + EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, |
|
| 526 | + __FUNCTION__, __LINE__); |
|
| 527 | + return false; |
|
| 528 | + } |
|
| 529 | + foreach ($data as $key => $value) { |
|
| 530 | + if (isset($this->_default_session_vars[ $key ])) { |
|
| 531 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
| 532 | + 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
|
| 533 | + return false; |
|
| 534 | + } |
|
| 535 | + $this->_session_data[ $key ] = $value; |
|
| 536 | + } |
|
| 537 | + return true; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * @initiate session |
|
| 544 | + * @access private |
|
| 545 | + * @return TRUE on success, FALSE on fail |
|
| 546 | + * @throws EE_Error |
|
| 547 | + * @throws InvalidArgumentException |
|
| 548 | + * @throws InvalidDataTypeException |
|
| 549 | + * @throws InvalidInterfaceException |
|
| 550 | + * @throws InvalidSessionDataException |
|
| 551 | + */ |
|
| 552 | + private function _espresso_session() |
|
| 553 | + { |
|
| 554 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 555 | + // check that session has started |
|
| 556 | + if (session_id() === '') { |
|
| 557 | + //starts a new session if one doesn't already exist, or re-initiates an existing one |
|
| 558 | + session_start(); |
|
| 559 | + } |
|
| 560 | + $this->status = EE_Session::EE_SESSION_STATUS_OPEN; |
|
| 561 | + // get our modified session ID |
|
| 562 | + $this->_sid = $this->_generate_session_id(); |
|
| 563 | + // and the visitors IP |
|
| 564 | + $this->_ip_address = $this->_visitor_ip(); |
|
| 565 | + // set the "user agent" |
|
| 566 | + $this->_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr($_SERVER['HTTP_USER_AGENT']) : false; |
|
| 567 | + // now let's retrieve what's in the db |
|
| 568 | + $session_data = $this->_retrieve_session_data(); |
|
| 569 | + if (! empty($session_data)) { |
|
| 570 | + // get the current time in UTC |
|
| 571 | + $this->_time = $this->_time !== null ? $this->_time : time(); |
|
| 572 | + // and reset the session expiration |
|
| 573 | + $this->_expiration = isset($session_data['expiration']) |
|
| 574 | + ? $session_data['expiration'] |
|
| 575 | + : $this->_time + $this->_lifespan; |
|
| 576 | + } else { |
|
| 577 | + // set initial site access time and the session expiration |
|
| 578 | + $this->_set_init_access_and_expiration(); |
|
| 579 | + // set referer |
|
| 580 | + $this->_session_data['pages_visited'][ $this->_session_data['init_access'] ] = isset($_SERVER['HTTP_REFERER']) |
|
| 581 | + ? esc_attr($_SERVER['HTTP_REFERER']) |
|
| 582 | + : ''; |
|
| 583 | + // no previous session = go back and create one (on top of the data above) |
|
| 584 | + return false; |
|
| 585 | + } |
|
| 586 | + // now the user agent |
|
| 587 | + if ($session_data['user_agent'] !== $this->_user_agent) { |
|
| 588 | + return false; |
|
| 589 | + } |
|
| 590 | + // wait a minute... how old are you? |
|
| 591 | + if ($this->_time > $this->_expiration) { |
|
| 592 | + // yer too old fer me! |
|
| 593 | + $this->_expired = true; |
|
| 594 | + // wipe out everything that isn't a default session datum |
|
| 595 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 596 | + } |
|
| 597 | + // make event espresso session data available to plugin |
|
| 598 | + $this->_session_data = array_merge($this->_session_data, $session_data); |
|
| 599 | + return true; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * _get_session_data |
|
| 606 | + * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup |
|
| 607 | + * databases |
|
| 608 | + * |
|
| 609 | + * @return array |
|
| 610 | + * @throws EE_Error |
|
| 611 | + * @throws InvalidArgumentException |
|
| 612 | + * @throws InvalidSessionDataException |
|
| 613 | + * @throws InvalidDataTypeException |
|
| 614 | + * @throws InvalidInterfaceException |
|
| 615 | + */ |
|
| 616 | + protected function _retrieve_session_data() |
|
| 617 | + { |
|
| 618 | + $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
| 619 | + try { |
|
| 620 | + // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
| 621 | + $session_data = $this->cache_storage->get($ssn_key, false); |
|
| 622 | + if (empty($session_data)) { |
|
| 623 | + return array(); |
|
| 624 | + } |
|
| 625 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 626 | + $hash_check = $this->cache_storage->get( |
|
| 627 | + EE_Session::hash_check_prefix . $this->_sid, |
|
| 628 | + false |
|
| 629 | + ); |
|
| 630 | + if ($hash_check && $hash_check !== md5($session_data)) { |
|
| 631 | + EE_Error::add_error( |
|
| 632 | + sprintf( |
|
| 633 | + __( |
|
| 634 | + 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
| 635 | + 'event_espresso' |
|
| 636 | + ), |
|
| 637 | + EE_Session::session_id_prefix . $this->_sid |
|
| 638 | + ), |
|
| 639 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 640 | + ); |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + } catch (Exception $e) { |
|
| 644 | + // let's just eat that error for now and attempt to correct any corrupted data |
|
| 645 | + global $wpdb; |
|
| 646 | + $row = $wpdb->get_row( |
|
| 647 | + $wpdb->prepare( |
|
| 648 | + "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
| 649 | + '_transient_' . $ssn_key |
|
| 650 | + ) |
|
| 651 | + ); |
|
| 652 | + $session_data = is_object($row) ? $row->option_value : null; |
|
| 653 | + if ($session_data) { |
|
| 654 | + $session_data = preg_replace_callback( |
|
| 655 | + '!s:(d+):"(.*?)";!', |
|
| 656 | + function ($match) |
|
| 657 | + { |
|
| 658 | + return $match[1] === strlen($match[2]) |
|
| 659 | + ? $match[0] |
|
| 660 | + : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
| 661 | + }, |
|
| 662 | + $session_data |
|
| 663 | + ); |
|
| 664 | + } |
|
| 665 | + $session_data = maybe_unserialize($session_data); |
|
| 666 | + } |
|
| 667 | + // in case the data is encoded... try to decode it |
|
| 668 | + $session_data = $this->encryption instanceof EE_Encryption |
|
| 669 | + ? $this->encryption->base64_string_decode($session_data) |
|
| 670 | + : $session_data; |
|
| 671 | + if (! is_array($session_data)) { |
|
| 672 | + try { |
|
| 673 | + $session_data = maybe_unserialize($session_data); |
|
| 674 | + } catch (Exception $e) { |
|
| 675 | + $msg = esc_html__( |
|
| 676 | + 'An error occurred while attempting to unserialize the session data.', |
|
| 677 | + 'event_espresso' |
|
| 678 | + ); |
|
| 679 | + $msg .= WP_DEBUG |
|
| 680 | + ? '<br><pre>' |
|
| 681 | + . print_r($session_data, true) |
|
| 682 | + . '</pre><br>' |
|
| 683 | + . $this->find_serialize_error($session_data) |
|
| 684 | + : ''; |
|
| 685 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
| 686 | + throw new InvalidSessionDataException($msg, 0, $e); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + // just a check to make sure the session array is indeed an array |
|
| 690 | + if (! is_array($session_data)) { |
|
| 691 | + // no?!?! then something is wrong |
|
| 692 | + $msg = esc_html__( |
|
| 693 | + 'The session data is missing, invalid, or corrupted.', |
|
| 694 | + 'event_espresso' |
|
| 695 | + ); |
|
| 696 | + $msg .= WP_DEBUG |
|
| 697 | + ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 698 | + : ''; |
|
| 699 | + $this->cache_storage->delete(EE_Session::session_id_prefix . $this->_sid); |
|
| 700 | + throw new InvalidSessionDataException($msg); |
|
| 701 | + } |
|
| 702 | + if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
| 703 | + $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
| 704 | + $session_data['transaction'] |
|
| 705 | + ); |
|
| 706 | + } |
|
| 707 | + return $session_data; |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + |
|
| 711 | + |
|
| 712 | + /** |
|
| 713 | + * _generate_session_id |
|
| 714 | + * Retrieves the PHP session id either directly from the PHP session, |
|
| 715 | + * or from the $_REQUEST array if it was passed in from an AJAX request. |
|
| 716 | + * The session id is then salted and hashed (mmm sounds tasty) |
|
| 717 | + * so that it can be safely used as a $_REQUEST param |
|
| 718 | + * |
|
| 719 | + * @return string |
|
| 720 | + */ |
|
| 721 | + protected function _generate_session_id() |
|
| 722 | + { |
|
| 723 | + // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
|
| 724 | + if (isset($_REQUEST['EESID'])) { |
|
| 725 | + $session_id = sanitize_text_field($_REQUEST['EESID']); |
|
| 726 | + } else { |
|
| 727 | + $session_id = md5(session_id() . get_current_blog_id() . $this->_get_sid_salt()); |
|
| 728 | + } |
|
| 729 | + return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * _get_sid_salt |
|
| 736 | + * |
|
| 737 | + * @return string |
|
| 738 | + */ |
|
| 739 | + protected function _get_sid_salt() |
|
| 740 | + { |
|
| 741 | + // was session id salt already saved to db ? |
|
| 742 | + if (empty($this->_sid_salt)) { |
|
| 743 | + // no? then maybe use WP defined constant |
|
| 744 | + if (defined('AUTH_SALT')) { |
|
| 745 | + $this->_sid_salt = AUTH_SALT; |
|
| 746 | + } |
|
| 747 | + // if salt doesn't exist or is too short |
|
| 748 | + if (strlen($this->_sid_salt) < 32) { |
|
| 749 | + // create a new one |
|
| 750 | + $this->_sid_salt = wp_generate_password(64); |
|
| 751 | + } |
|
| 752 | + // and save it as a permanent session setting |
|
| 753 | + $this->updateSessionSettings(array('sid_salt' => $this->_sid_salt)); |
|
| 754 | + } |
|
| 755 | + return $this->_sid_salt; |
|
| 756 | + } |
|
| 757 | + |
|
| 758 | + |
|
| 759 | + |
|
| 760 | + /** |
|
| 761 | + * _set_init_access_and_expiration |
|
| 762 | + * |
|
| 763 | + * @return void |
|
| 764 | + */ |
|
| 765 | + protected function _set_init_access_and_expiration() |
|
| 766 | + { |
|
| 767 | + $this->_time = time(); |
|
| 768 | + $this->_expiration = $this->_time + $this->_lifespan; |
|
| 769 | + // set initial site access time |
|
| 770 | + $this->_session_data['init_access'] = $this->_time; |
|
| 771 | + // and the session expiration |
|
| 772 | + $this->_session_data['expiration'] = $this->_expiration; |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + |
|
| 776 | + |
|
| 777 | + /** |
|
| 778 | + * @update session data prior to saving to the db |
|
| 779 | + * @access public |
|
| 780 | + * @param bool $new_session |
|
| 781 | + * @return TRUE on success, FALSE on fail |
|
| 782 | + * @throws EE_Error |
|
| 783 | + * @throws InvalidArgumentException |
|
| 784 | + * @throws InvalidDataTypeException |
|
| 785 | + * @throws InvalidInterfaceException |
|
| 786 | + */ |
|
| 787 | + public function update($new_session = false) |
|
| 788 | + { |
|
| 789 | + $this->_session_data = $this->_session_data !== null |
|
| 790 | + && is_array($this->_session_data) |
|
| 791 | + && isset($this->_session_data['id']) |
|
| 792 | + ? $this->_session_data |
|
| 793 | + : array(); |
|
| 794 | + if (empty($this->_session_data)) { |
|
| 795 | + $this->_set_defaults(); |
|
| 796 | + } |
|
| 797 | + $session_data = array(); |
|
| 798 | + foreach ($this->_session_data as $key => $value) { |
|
| 799 | + |
|
| 800 | + switch ($key) { |
|
| 801 | + |
|
| 802 | + case 'id' : |
|
| 803 | + // session ID |
|
| 804 | + $session_data['id'] = $this->_sid; |
|
| 805 | + break; |
|
| 806 | + case 'ip_address' : |
|
| 807 | + // visitor ip address |
|
| 808 | + $session_data['ip_address'] = $this->_visitor_ip(); |
|
| 809 | + break; |
|
| 810 | + case 'user_agent' : |
|
| 811 | + // visitor user_agent |
|
| 812 | + $session_data['user_agent'] = $this->_user_agent; |
|
| 813 | + break; |
|
| 814 | + case 'init_access' : |
|
| 815 | + $session_data['init_access'] = absint($value); |
|
| 816 | + break; |
|
| 817 | + case 'last_access' : |
|
| 818 | + // current access time |
|
| 819 | + $session_data['last_access'] = $this->_time; |
|
| 820 | + break; |
|
| 821 | + case 'expiration' : |
|
| 822 | + // when the session expires |
|
| 823 | + $session_data['expiration'] = ! empty($this->_expiration) |
|
| 824 | + ? $this->_expiration |
|
| 825 | + : $session_data['init_access'] + $this->_lifespan; |
|
| 826 | + break; |
|
| 827 | + case 'user_id' : |
|
| 828 | + // current user if logged in |
|
| 829 | + $session_data['user_id'] = $this->_wp_user_id(); |
|
| 830 | + break; |
|
| 831 | + case 'pages_visited' : |
|
| 832 | + $page_visit = $this->_get_page_visit(); |
|
| 833 | + if ($page_visit) { |
|
| 834 | + // set pages visited where the first will be the http referrer |
|
| 835 | + $this->_session_data['pages_visited'][ $this->_time ] = $page_visit; |
|
| 836 | + // we'll only save the last 10 page visits. |
|
| 837 | + $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
| 838 | + } |
|
| 839 | + break; |
|
| 840 | + default : |
|
| 841 | + // carry any other data over |
|
| 842 | + $session_data[ $key ] = $this->_session_data[ $key ]; |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + $this->_session_data = $session_data; |
|
| 846 | + // creating a new session does not require saving to the db just yet |
|
| 847 | + if (! $new_session) { |
|
| 848 | + // ready? let's save |
|
| 849 | + if ($this->_save_session_to_db()) { |
|
| 850 | + return true; |
|
| 851 | + } |
|
| 852 | + return false; |
|
| 853 | + } |
|
| 854 | + // meh, why not? |
|
| 855 | + return true; |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * @create session data array |
|
| 862 | + * @access public |
|
| 863 | + * @return bool |
|
| 864 | + * @throws EE_Error |
|
| 865 | + * @throws InvalidArgumentException |
|
| 866 | + * @throws InvalidDataTypeException |
|
| 867 | + * @throws InvalidInterfaceException |
|
| 868 | + */ |
|
| 869 | + private function _create_espresso_session() |
|
| 870 | + { |
|
| 871 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
| 872 | + // use the update function for now with $new_session arg set to TRUE |
|
| 873 | + return $this->update(true) ? true : false; |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * _save_session_to_db |
|
| 880 | + * |
|
| 881 | + * @param bool $clear_session |
|
| 882 | + * @return string |
|
| 883 | + * @throws EE_Error |
|
| 884 | + * @throws InvalidArgumentException |
|
| 885 | + * @throws InvalidDataTypeException |
|
| 886 | + * @throws InvalidInterfaceException |
|
| 887 | + */ |
|
| 888 | + private function _save_session_to_db($clear_session = false) |
|
| 889 | + { |
|
| 890 | + // unless we're deleting the session data, don't save anything if there isn't a cart |
|
| 891 | + if (! $clear_session && ! $this->cart() instanceof EE_Cart) { |
|
| 892 | + return false; |
|
| 893 | + } |
|
| 894 | + $transaction = $this->transaction(); |
|
| 895 | + if ($transaction instanceof EE_Transaction) { |
|
| 896 | + if (! $transaction->ID()) { |
|
| 897 | + $transaction->save(); |
|
| 898 | + } |
|
| 899 | + $this->_session_data['transaction'] = $transaction->ID(); |
|
| 900 | + } |
|
| 901 | + // then serialize all of our session data |
|
| 902 | + $session_data = serialize($this->_session_data); |
|
| 903 | + // do we need to also encode it to avoid corrupted data when saved to the db? |
|
| 904 | + $session_data = $this->_use_encryption |
|
| 905 | + ? $this->encryption->base64_string_encode($session_data) |
|
| 906 | + : $session_data; |
|
| 907 | + // maybe save hash check |
|
| 908 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 909 | + $this->cache_storage->add( |
|
| 910 | + EE_Session::hash_check_prefix . $this->_sid, |
|
| 911 | + md5($session_data), |
|
| 912 | + $this->_lifespan |
|
| 913 | + ); |
|
| 914 | + } |
|
| 915 | + // we're using the Transient API for storing session data, |
|
| 916 | + return $this->cache_storage->add( |
|
| 917 | + EE_Session::session_id_prefix . $this->_sid, |
|
| 918 | + $session_data, |
|
| 919 | + $this->_lifespan |
|
| 920 | + ); |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + |
|
| 924 | + |
|
| 925 | + /** |
|
| 926 | + * _visitor_ip |
|
| 927 | + * attempt to get IP address of current visitor from server |
|
| 928 | + * plz see: http://stackoverflow.com/a/2031935/1475279 |
|
| 929 | + * |
|
| 930 | + * @access public |
|
| 931 | + * @return string |
|
| 932 | + */ |
|
| 933 | + private function _visitor_ip() |
|
| 934 | + { |
|
| 935 | + $visitor_ip = '0.0.0.0'; |
|
| 936 | + $server_keys = array( |
|
| 937 | + 'HTTP_CLIENT_IP', |
|
| 938 | + 'HTTP_X_FORWARDED_FOR', |
|
| 939 | + 'HTTP_X_FORWARDED', |
|
| 940 | + 'HTTP_X_CLUSTER_CLIENT_IP', |
|
| 941 | + 'HTTP_FORWARDED_FOR', |
|
| 942 | + 'HTTP_FORWARDED', |
|
| 943 | + 'REMOTE_ADDR', |
|
| 944 | + ); |
|
| 945 | + foreach ($server_keys as $key) { |
|
| 946 | + if (isset($_SERVER[ $key ])) { |
|
| 947 | + foreach (array_map('trim', explode(',', $_SERVER[ $key ])) as $ip) { |
|
| 948 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== false) { |
|
| 949 | + $visitor_ip = $ip; |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + } |
|
| 953 | + } |
|
| 954 | + return $visitor_ip; |
|
| 955 | + } |
|
| 956 | + |
|
| 957 | + |
|
| 958 | + |
|
| 959 | + /** |
|
| 960 | + * @get the full page request the visitor is accessing |
|
| 961 | + * @access public |
|
| 962 | + * @return string |
|
| 963 | + */ |
|
| 964 | + public function _get_page_visit() |
|
| 965 | + { |
|
| 966 | + $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
| 967 | + // check for request url |
|
| 968 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 969 | + $http_host = ''; |
|
| 970 | + $page_id = '?'; |
|
| 971 | + $e_reg = ''; |
|
| 972 | + $request_uri = esc_url($_SERVER['REQUEST_URI']); |
|
| 973 | + $ru_bits = explode('?', $request_uri); |
|
| 974 | + $request_uri = $ru_bits[0]; |
|
| 975 | + // check for and grab host as well |
|
| 976 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
| 977 | + $http_host = esc_url($_SERVER['HTTP_HOST']); |
|
| 978 | + } |
|
| 979 | + // check for page_id in SERVER REQUEST |
|
| 980 | + if (isset($_REQUEST['page_id'])) { |
|
| 981 | + // rebuild $e_reg without any of the extra parameters |
|
| 982 | + $page_id = '?page_id=' . esc_attr($_REQUEST['page_id']) . '&'; |
|
| 983 | + } |
|
| 984 | + // check for $e_reg in SERVER REQUEST |
|
| 985 | + if (isset($_REQUEST['ee'])) { |
|
| 986 | + // rebuild $e_reg without any of the extra parameters |
|
| 987 | + $e_reg = 'ee=' . esc_attr($_REQUEST['ee']); |
|
| 988 | + } |
|
| 989 | + $page_visit = rtrim($http_host . $request_uri . $page_id . $e_reg, '?'); |
|
| 990 | + } |
|
| 991 | + return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
| 992 | + } |
|
| 993 | + |
|
| 994 | + |
|
| 995 | + |
|
| 996 | + /** |
|
| 997 | + * @the current wp user id |
|
| 998 | + * @access public |
|
| 999 | + * @return int |
|
| 1000 | + */ |
|
| 1001 | + public function _wp_user_id() |
|
| 1002 | + { |
|
| 1003 | + // if I need to explain the following lines of code, then you shouldn't be looking at this! |
|
| 1004 | + $this->_wp_user_id = get_current_user_id(); |
|
| 1005 | + return $this->_wp_user_id; |
|
| 1006 | + } |
|
| 1007 | + |
|
| 1008 | + |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * Clear EE_Session data |
|
| 1012 | + * |
|
| 1013 | + * @access public |
|
| 1014 | + * @param string $class |
|
| 1015 | + * @param string $function |
|
| 1016 | + * @return void |
|
| 1017 | + * @throws EE_Error |
|
| 1018 | + * @throws InvalidArgumentException |
|
| 1019 | + * @throws InvalidDataTypeException |
|
| 1020 | + * @throws InvalidInterfaceException |
|
| 1021 | + */ |
|
| 1022 | + public function clear_session($class = '', $function = '') |
|
| 1023 | + { |
|
| 1024 | + //echo '<h3 style="color:#999;line-height:.9em;"><span style="color:#2EA2CC">' . __CLASS__ . '</span>::<span style="color:#E76700">' . __FUNCTION__ . '( ' . $class . '::' . $function . '() )</span><br/><span style="font-size:9px;font-weight:normal;">' . __FILE__ . '</span> <b style="font-size:10px;"> ' . __LINE__ . ' </b></h3>'; |
|
| 1025 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()'); |
|
| 1026 | + $this->reset_cart(); |
|
| 1027 | + $this->reset_checkout(); |
|
| 1028 | + $this->reset_transaction(); |
|
| 1029 | + // wipe out everything that isn't a default session datum |
|
| 1030 | + $this->reset_data(array_keys($this->_session_data)); |
|
| 1031 | + // reset initial site access time and the session expiration |
|
| 1032 | + $this->_set_init_access_and_expiration(); |
|
| 1033 | + $this->_save_session_to_db(true); |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + |
|
| 1037 | + /** |
|
| 1038 | + * resets all non-default session vars. Returns TRUE on success, FALSE on fail |
|
| 1039 | + * |
|
| 1040 | + * @param array|mixed $data_to_reset |
|
| 1041 | + * @param bool $show_all_notices |
|
| 1042 | + * @return bool |
|
| 1043 | + */ |
|
| 1044 | + public function reset_data($data_to_reset = array(), $show_all_notices = false) |
|
| 1045 | + { |
|
| 1046 | + // if $data_to_reset is not in an array, then put it in one |
|
| 1047 | + if (! is_array($data_to_reset)) { |
|
| 1048 | + $data_to_reset = array($data_to_reset); |
|
| 1049 | + } |
|
| 1050 | + // nothing ??? go home! |
|
| 1051 | + if (empty($data_to_reset)) { |
|
| 1052 | + EE_Error::add_error(__('No session data could be reset, because no session var name was provided.', |
|
| 1053 | + 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 1054 | + return false; |
|
| 1055 | + } |
|
| 1056 | + $return_value = true; |
|
| 1057 | + // since $data_to_reset is an array, cycle through the values |
|
| 1058 | + foreach ($data_to_reset as $reset) { |
|
| 1059 | + |
|
| 1060 | + // first check to make sure it is a valid session var |
|
| 1061 | + if (isset($this->_session_data[ $reset ])) { |
|
| 1062 | + // then check to make sure it is not a default var |
|
| 1063 | + if (! array_key_exists($reset, $this->_default_session_vars)) { |
|
| 1064 | + // remove session var |
|
| 1065 | + unset($this->_session_data[ $reset ]); |
|
| 1066 | + if ($show_all_notices) { |
|
| 1067 | + EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), |
|
| 1068 | + $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1069 | + } |
|
| 1070 | + } else { |
|
| 1071 | + // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
|
| 1072 | + if ($show_all_notices) { |
|
| 1073 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', |
|
| 1074 | + 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1075 | + } |
|
| 1076 | + $return_value = false; |
|
| 1077 | + } |
|
| 1078 | + } elseif ($show_all_notices) { |
|
| 1079 | + // oops! that session var does not exist! |
|
| 1080 | + EE_Error::add_error(sprintf(__('The session item provided, %s, is invalid or does not exist.', |
|
| 1081 | + 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1082 | + $return_value = false; |
|
| 1083 | + } |
|
| 1084 | + } // end of foreach |
|
| 1085 | + return $return_value; |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + |
|
| 1089 | + |
|
| 1090 | + /** |
|
| 1091 | + * wp_loaded |
|
| 1092 | + * |
|
| 1093 | + * @access public |
|
| 1094 | + * @throws EE_Error |
|
| 1095 | + * @throws InvalidDataTypeException |
|
| 1096 | + * @throws InvalidInterfaceException |
|
| 1097 | + * @throws InvalidArgumentException |
|
| 1098 | + */ |
|
| 1099 | + public function wp_loaded() |
|
| 1100 | + { |
|
| 1101 | + if ( |
|
| 1102 | + EE_Registry::instance()->REQ instanceof EE_Request_Handler |
|
| 1103 | + && EE_Registry::instance()->REQ->is_set('clear_session') |
|
| 1104 | + ) { |
|
| 1105 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 1106 | + } |
|
| 1107 | + } |
|
| 1108 | + |
|
| 1109 | + |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * Used to reset the entire object (for tests). |
|
| 1113 | + * |
|
| 1114 | + * @since 4.3.0 |
|
| 1115 | + * @throws EE_Error |
|
| 1116 | + * @throws InvalidDataTypeException |
|
| 1117 | + * @throws InvalidInterfaceException |
|
| 1118 | + * @throws InvalidArgumentException |
|
| 1119 | + */ |
|
| 1120 | + public function reset_instance() |
|
| 1121 | + { |
|
| 1122 | + $this->clear_session(); |
|
| 1123 | + self::$_instance = null; |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + |
|
| 1127 | + |
|
| 1128 | + public function configure_garbage_collection_filters() |
|
| 1129 | + { |
|
| 1130 | + // run old filter we had for controlling session cleanup |
|
| 1131 | + $expired_session_transient_delete_query_limit = absint( |
|
| 1132 | + apply_filters( |
|
| 1133 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1134 | + 50 |
|
| 1135 | + ) |
|
| 1136 | + ); |
|
| 1137 | + // is there a value? or one that is different than the default 50 records? |
|
| 1138 | + if ($expired_session_transient_delete_query_limit === 0) { |
|
| 1139 | + // hook into TransientCacheStorage in case Session cleanup was turned off |
|
| 1140 | + add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
| 1141 | + } elseif ($expired_session_transient_delete_query_limit !== 50) { |
|
| 1142 | + // or use that for the new transient cleanup query limit |
|
| 1143 | + add_filter( |
|
| 1144 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 1145 | + function () use ($expired_session_transient_delete_query_limit) |
|
| 1146 | + { |
|
| 1147 | + return $expired_session_transient_delete_query_limit; |
|
| 1148 | + } |
|
| 1149 | + ); |
|
| 1150 | + } |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + |
|
| 1154 | + |
|
| 1155 | + /** |
|
| 1156 | + * @see http://stackoverflow.com/questions/10152904/unserialize-function-unserialize-error-at-offset/21389439#10152996 |
|
| 1157 | + * @param $data1 |
|
| 1158 | + * @return string |
|
| 1159 | + */ |
|
| 1160 | + private function find_serialize_error($data1) |
|
| 1161 | + { |
|
| 1162 | + $error = '<pre>'; |
|
| 1163 | + $data2 = preg_replace_callback( |
|
| 1164 | + '!s:(\d+):"(.*?)";!', |
|
| 1165 | + function ($match) |
|
| 1166 | + { |
|
| 1167 | + return ($match[1] === strlen($match[2])) |
|
| 1168 | + ? $match[0] |
|
| 1169 | + : 's:' |
|
| 1170 | + . strlen($match[2]) |
|
| 1171 | + . ':"' |
|
| 1172 | + . $match[2] |
|
| 1173 | + . '";'; |
|
| 1174 | + }, |
|
| 1175 | + $data1 |
|
| 1176 | + ); |
|
| 1177 | + $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
| 1178 | + $error .= $data1 . PHP_EOL; |
|
| 1179 | + $error .= $data2 . PHP_EOL; |
|
| 1180 | + for ($i = 0; $i < $max; $i++) { |
|
| 1181 | + if (@$data1[ $i ] !== @$data2[ $i ]) { |
|
| 1182 | + $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
| 1183 | + $error .= "\t-> ORD number " . ord(@$data1[ $i ]) . ' != ' . ord(@$data2[ $i ]) . PHP_EOL; |
|
| 1184 | + $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
| 1185 | + $start = ($i - 20); |
|
| 1186 | + $start = ($start < 0) ? 0 : $start; |
|
| 1187 | + $length = 40; |
|
| 1188 | + $point = $max - $i; |
|
| 1189 | + if ($point < 20) { |
|
| 1190 | + $rlength = 1; |
|
| 1191 | + $rpoint = -$point; |
|
| 1192 | + } else { |
|
| 1193 | + $rpoint = $length - 20; |
|
| 1194 | + $rlength = 1; |
|
| 1195 | + } |
|
| 1196 | + $error .= "\t-> Section Data1 = "; |
|
| 1197 | + $error .= substr_replace( |
|
| 1198 | + substr($data1, $start, $length), |
|
| 1199 | + "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
| 1200 | + $rpoint, |
|
| 1201 | + $rlength |
|
| 1202 | + ); |
|
| 1203 | + $error .= PHP_EOL; |
|
| 1204 | + $error .= "\t-> Section Data2 = "; |
|
| 1205 | + $error .= substr_replace( |
|
| 1206 | + substr($data2, $start, $length), |
|
| 1207 | + "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
| 1208 | + $rpoint, |
|
| 1209 | + $rlength |
|
| 1210 | + ); |
|
| 1211 | + $error .= PHP_EOL; |
|
| 1212 | + } |
|
| 1213 | + } |
|
| 1214 | + $error .= '</pre>'; |
|
| 1215 | + return $error; |
|
| 1216 | + } |
|
| 1217 | + |
|
| 1218 | + |
|
| 1219 | + /** |
|
| 1220 | + * Saves an array of settings used for configuring aspects of session behaviour |
|
| 1221 | + * |
|
| 1222 | + * @param array $updated_settings |
|
| 1223 | + */ |
|
| 1224 | + private function updateSessionSettings(array $updated_settings = array()) |
|
| 1225 | + { |
|
| 1226 | + // add existing settings, but only if not included in incoming $updated_settings array |
|
| 1227 | + $updated_settings += get_option(EE_Session::OPTION_NAME_SETTINGS, array()); |
|
| 1228 | + update_option(EE_Session::OPTION_NAME_SETTINGS, $updated_settings); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + |
|
| 1232 | + /** |
|
| 1233 | + * garbage_collection |
|
| 1234 | + */ |
|
| 1235 | + public function garbageCollection() |
|
| 1236 | + { |
|
| 1237 | + // only perform during regular requests if last garbage collection was over an hour ago |
|
| 1238 | + if (! (defined('DOING_AJAX') && DOING_AJAX) && (time() - HOUR_IN_SECONDS) >= $this->_last_gc) { |
|
| 1239 | + $this->_last_gc = time(); |
|
| 1240 | + $this->updateSessionSettings(array('last_gc' => $this->_last_gc)); |
|
| 1241 | + /** @type WPDB $wpdb */ |
|
| 1242 | + global $wpdb; |
|
| 1243 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
| 1244 | + $expired_session_transient_delete_query_limit = absint( |
|
| 1245 | + apply_filters( |
|
| 1246 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1247 | + 50 |
|
| 1248 | + ) |
|
| 1249 | + ); |
|
| 1250 | + // non-zero LIMIT means take out the trash |
|
| 1251 | + if ($expired_session_transient_delete_query_limit) { |
|
| 1252 | + $session_key = str_replace('_', '\_', EE_Session::session_id_prefix); |
|
| 1253 | + $hash_check_key = str_replace('_', '\_', EE_Session::hash_check_prefix); |
|
| 1254 | + // since transient expiration timestamps are set in the future, we can compare against NOW |
|
| 1255 | + // but we only want to pick up any trash that's been around for more than a day |
|
| 1256 | + $expiration = time() - DAY_IN_SECONDS; |
|
| 1257 | + $SQL = " |
|
| 1258 | 1258 | SELECT option_name |
| 1259 | 1259 | FROM {$wpdb->options} |
| 1260 | 1260 | WHERE |
@@ -1263,19 +1263,19 @@ discard block |
||
| 1263 | 1263 | AND option_value < {$expiration} |
| 1264 | 1264 | LIMIT {$expired_session_transient_delete_query_limit} |
| 1265 | 1265 | "; |
| 1266 | - // produces something like: |
|
| 1267 | - // SELECT option_name FROM wp_options |
|
| 1268 | - // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
| 1269 | - // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
| 1270 | - // AND option_value < 1508368198 LIMIT 50 |
|
| 1271 | - $expired_sessions = $wpdb->get_col($SQL); |
|
| 1272 | - // valid results? |
|
| 1273 | - if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
| 1274 | - $this->cache_storage->deleteMany($expired_sessions, true); |
|
| 1275 | - } |
|
| 1276 | - } |
|
| 1277 | - } |
|
| 1278 | - } |
|
| 1266 | + // produces something like: |
|
| 1267 | + // SELECT option_name FROM wp_options |
|
| 1268 | + // WHERE ( option_name LIKE '\_transient\_timeout\_ee\_ssn\_%' |
|
| 1269 | + // OR option_name LIKE '\_transient\_timeout\_ee\_shc\_%' ) |
|
| 1270 | + // AND option_value < 1508368198 LIMIT 50 |
|
| 1271 | + $expired_sessions = $wpdb->get_col($SQL); |
|
| 1272 | + // valid results? |
|
| 1273 | + if (! $expired_sessions instanceof WP_Error && ! empty($expired_sessions)) { |
|
| 1274 | + $this->cache_storage->deleteMany($expired_sessions, true); |
|
| 1275 | + } |
|
| 1276 | + } |
|
| 1277 | + } |
|
| 1278 | + } |
|
| 1279 | 1279 | |
| 1280 | 1280 | |
| 1281 | 1281 | |
@@ -11,8 +11,8 @@ discard block |
||
| 11 | 11 | // if you're a dev and want to receive all errors via email |
| 12 | 12 | // add this to your wp-config.php: define( 'EE_ERROR_EMAILS', TRUE ); |
| 13 | 13 | if (defined('WP_DEBUG') && WP_DEBUG === true && defined('EE_ERROR_EMAILS') && EE_ERROR_EMAILS === true) { |
| 14 | - set_error_handler(array('EE_Error', 'error_handler')); |
|
| 15 | - register_shutdown_function(array('EE_Error', 'fatal_error_handler')); |
|
| 14 | + set_error_handler(array('EE_Error', 'error_handler')); |
|
| 15 | + register_shutdown_function(array('EE_Error', 'fatal_error_handler')); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | |
@@ -27,258 +27,258 @@ discard block |
||
| 27 | 27 | class EE_Error extends Exception |
| 28 | 28 | { |
| 29 | 29 | |
| 30 | - const OPTIONS_KEY_NOTICES = 'ee_notices'; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * name of the file to log exceptions to |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private static $_exception_log_file = 'espresso_error_log.txt'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * stores details for all exception |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - private static $_all_exceptions = array(); |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * tracks number of errors |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - private static $_error_count = 0; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var array $_espresso_notices |
|
| 56 | - */ |
|
| 57 | - private static $_espresso_notices = array('success' => false, 'errors' => false, 'attention' => false); |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @override default exception handling |
|
| 63 | - * @param string $message |
|
| 64 | - * @param int $code |
|
| 65 | - * @param Exception|null $previous |
|
| 66 | - */ |
|
| 67 | - public function __construct($message, $code = 0, Exception $previous = null) |
|
| 68 | - { |
|
| 69 | - if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
| 70 | - parent::__construct($message, $code); |
|
| 71 | - } else { |
|
| 72 | - parent::__construct($message, $code, $previous); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * error_handler |
|
| 79 | - * |
|
| 80 | - * @param $code |
|
| 81 | - * @param $message |
|
| 82 | - * @param $file |
|
| 83 | - * @param $line |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - public static function error_handler($code, $message, $file, $line) |
|
| 87 | - { |
|
| 88 | - $type = EE_Error::error_type($code); |
|
| 89 | - $site = site_url(); |
|
| 90 | - switch ($site) { |
|
| 91 | - case 'http://ee4.eventespresso.com/' : |
|
| 92 | - case 'http://ee4decaf.eventespresso.com/' : |
|
| 93 | - case 'http://ee4hf.eventespresso.com/' : |
|
| 94 | - case 'http://ee4a.eventespresso.com/' : |
|
| 95 | - case 'http://ee4ad.eventespresso.com/' : |
|
| 96 | - case 'http://ee4b.eventespresso.com/' : |
|
| 97 | - case 'http://ee4bd.eventespresso.com/' : |
|
| 98 | - case 'http://ee4d.eventespresso.com/' : |
|
| 99 | - case 'http://ee4dd.eventespresso.com/' : |
|
| 100 | - $to = '[email protected]'; |
|
| 101 | - break; |
|
| 102 | - default : |
|
| 103 | - $to = get_option('admin_email'); |
|
| 104 | - } |
|
| 105 | - $subject = $type . ' ' . $message . ' in ' . EVENT_ESPRESSO_VERSION . ' on ' . site_url(); |
|
| 106 | - $msg = EE_Error::_format_error($type, $message, $file, $line); |
|
| 107 | - if (function_exists('wp_mail')) { |
|
| 108 | - add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
| 109 | - wp_mail($to, $subject, $msg); |
|
| 110 | - } |
|
| 111 | - echo '<div id="message" class="espresso-notices error"><p>'; |
|
| 112 | - echo $type . ': ' . $message . '<br />' . $file . ' line ' . $line; |
|
| 113 | - echo '<br /></p></div>'; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * error_type |
|
| 120 | - * http://www.php.net/manual/en/errorfunc.constants.php#109430 |
|
| 121 | - * |
|
| 122 | - * @param $code |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - public static function error_type($code) |
|
| 126 | - { |
|
| 127 | - switch ($code) { |
|
| 128 | - case E_ERROR: // 1 // |
|
| 129 | - return 'E_ERROR'; |
|
| 130 | - case E_WARNING: // 2 // |
|
| 131 | - return 'E_WARNING'; |
|
| 132 | - case E_PARSE: // 4 // |
|
| 133 | - return 'E_PARSE'; |
|
| 134 | - case E_NOTICE: // 8 // |
|
| 135 | - return 'E_NOTICE'; |
|
| 136 | - case E_CORE_ERROR: // 16 // |
|
| 137 | - return 'E_CORE_ERROR'; |
|
| 138 | - case E_CORE_WARNING: // 32 // |
|
| 139 | - return 'E_CORE_WARNING'; |
|
| 140 | - case E_COMPILE_ERROR: // 64 // |
|
| 141 | - return 'E_COMPILE_ERROR'; |
|
| 142 | - case E_COMPILE_WARNING: // 128 // |
|
| 143 | - return 'E_COMPILE_WARNING'; |
|
| 144 | - case E_USER_ERROR: // 256 // |
|
| 145 | - return 'E_USER_ERROR'; |
|
| 146 | - case E_USER_WARNING: // 512 // |
|
| 147 | - return 'E_USER_WARNING'; |
|
| 148 | - case E_USER_NOTICE: // 1024 // |
|
| 149 | - return 'E_USER_NOTICE'; |
|
| 150 | - case E_STRICT: // 2048 // |
|
| 151 | - return 'E_STRICT'; |
|
| 152 | - case E_RECOVERABLE_ERROR: // 4096 // |
|
| 153 | - return 'E_RECOVERABLE_ERROR'; |
|
| 154 | - case E_DEPRECATED: // 8192 // |
|
| 155 | - return 'E_DEPRECATED'; |
|
| 156 | - case E_USER_DEPRECATED: // 16384 // |
|
| 157 | - return 'E_USER_DEPRECATED'; |
|
| 158 | - case E_ALL: // 16384 // |
|
| 159 | - return 'E_ALL'; |
|
| 160 | - } |
|
| 161 | - return ''; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * fatal_error_handler |
|
| 168 | - * |
|
| 169 | - * @return void |
|
| 170 | - */ |
|
| 171 | - public static function fatal_error_handler() |
|
| 172 | - { |
|
| 173 | - $last_error = error_get_last(); |
|
| 174 | - if ($last_error['type'] === E_ERROR) { |
|
| 175 | - EE_Error::error_handler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); |
|
| 176 | - } |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * _format_error |
|
| 183 | - * |
|
| 184 | - * @param $code |
|
| 185 | - * @param $message |
|
| 186 | - * @param $file |
|
| 187 | - * @param $line |
|
| 188 | - * @return string |
|
| 189 | - */ |
|
| 190 | - private static function _format_error($code, $message, $file, $line) |
|
| 191 | - { |
|
| 192 | - $html = "<table cellpadding='5'><thead bgcolor='#f8f8f8'><th>Item</th><th align='left'>Details</th></thead><tbody>"; |
|
| 193 | - $html .= "<tr valign='top'><td><b>Code</b></td><td>$code</td></tr>"; |
|
| 194 | - $html .= "<tr valign='top'><td><b>Error</b></td><td>$message</td></tr>"; |
|
| 195 | - $html .= "<tr valign='top'><td><b>File</b></td><td>$file</td></tr>"; |
|
| 196 | - $html .= "<tr valign='top'><td><b>Line</b></td><td>$line</td></tr>"; |
|
| 197 | - $html .= '</tbody></table>'; |
|
| 198 | - return $html; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * set_content_type |
|
| 205 | - * |
|
| 206 | - * @param $content_type |
|
| 207 | - * @return string |
|
| 208 | - */ |
|
| 209 | - public static function set_content_type($content_type) |
|
| 210 | - { |
|
| 211 | - return 'text/html'; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @return void |
|
| 218 | - * @throws EE_Error |
|
| 219 | - * @throws ReflectionException |
|
| 220 | - */ |
|
| 221 | - public function get_error() |
|
| 222 | - { |
|
| 223 | - if (apply_filters('FHEE__EE_Error__get_error__show_normal_exceptions', false)) { |
|
| 224 | - throw $this; |
|
| 225 | - } |
|
| 226 | - // get separate user and developer messages if they exist |
|
| 227 | - $msg = explode('||', $this->getMessage()); |
|
| 228 | - $user_msg = $msg[0]; |
|
| 229 | - $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 230 | - $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 231 | - // add details to _all_exceptions array |
|
| 232 | - $x_time = time(); |
|
| 233 | - self::$_all_exceptions[$x_time]['name'] = get_class($this); |
|
| 234 | - self::$_all_exceptions[$x_time]['file'] = $this->getFile(); |
|
| 235 | - self::$_all_exceptions[$x_time]['line'] = $this->getLine(); |
|
| 236 | - self::$_all_exceptions[$x_time]['msg'] = $msg; |
|
| 237 | - self::$_all_exceptions[$x_time]['code'] = $this->getCode(); |
|
| 238 | - self::$_all_exceptions[$x_time]['trace'] = $this->getTrace(); |
|
| 239 | - self::$_all_exceptions[$x_time]['string'] = $this->getTraceAsString(); |
|
| 240 | - self::$_error_count++; |
|
| 241 | - //add_action( 'shutdown', array( $this, 'display_errors' )); |
|
| 242 | - $this->display_errors(); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param bool $check_stored |
|
| 248 | - * @param string $type_to_check |
|
| 249 | - * @return bool |
|
| 250 | - * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 251 | - * @throws \InvalidArgumentException |
|
| 252 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 253 | - * @throws InvalidInterfaceException |
|
| 254 | - */ |
|
| 255 | - public static function has_error($check_stored = false, $type_to_check = 'errors') |
|
| 256 | - { |
|
| 257 | - $has_error = isset(self::$_espresso_notices[$type_to_check]) |
|
| 258 | - && ! empty(self::$_espresso_notices[$type_to_check]) |
|
| 259 | - ? true |
|
| 260 | - : false; |
|
| 261 | - if ($check_stored && ! $has_error) { |
|
| 262 | - $notices = EE_Error::getStoredNotices(); |
|
| 263 | - foreach ($notices as $type => $notice) { |
|
| 264 | - if ($type === $type_to_check && $notice) { |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - return $has_error; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @echo string |
|
| 276 | - * @throws \ReflectionException |
|
| 277 | - */ |
|
| 278 | - public function display_errors() |
|
| 279 | - { |
|
| 280 | - $trace_details = ''; |
|
| 281 | - $output = ' |
|
| 30 | + const OPTIONS_KEY_NOTICES = 'ee_notices'; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * name of the file to log exceptions to |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private static $_exception_log_file = 'espresso_error_log.txt'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * stores details for all exception |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + private static $_all_exceptions = array(); |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * tracks number of errors |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + private static $_error_count = 0; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var array $_espresso_notices |
|
| 56 | + */ |
|
| 57 | + private static $_espresso_notices = array('success' => false, 'errors' => false, 'attention' => false); |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @override default exception handling |
|
| 63 | + * @param string $message |
|
| 64 | + * @param int $code |
|
| 65 | + * @param Exception|null $previous |
|
| 66 | + */ |
|
| 67 | + public function __construct($message, $code = 0, Exception $previous = null) |
|
| 68 | + { |
|
| 69 | + if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
|
| 70 | + parent::__construct($message, $code); |
|
| 71 | + } else { |
|
| 72 | + parent::__construct($message, $code, $previous); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * error_handler |
|
| 79 | + * |
|
| 80 | + * @param $code |
|
| 81 | + * @param $message |
|
| 82 | + * @param $file |
|
| 83 | + * @param $line |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + public static function error_handler($code, $message, $file, $line) |
|
| 87 | + { |
|
| 88 | + $type = EE_Error::error_type($code); |
|
| 89 | + $site = site_url(); |
|
| 90 | + switch ($site) { |
|
| 91 | + case 'http://ee4.eventespresso.com/' : |
|
| 92 | + case 'http://ee4decaf.eventespresso.com/' : |
|
| 93 | + case 'http://ee4hf.eventespresso.com/' : |
|
| 94 | + case 'http://ee4a.eventespresso.com/' : |
|
| 95 | + case 'http://ee4ad.eventespresso.com/' : |
|
| 96 | + case 'http://ee4b.eventespresso.com/' : |
|
| 97 | + case 'http://ee4bd.eventespresso.com/' : |
|
| 98 | + case 'http://ee4d.eventespresso.com/' : |
|
| 99 | + case 'http://ee4dd.eventespresso.com/' : |
|
| 100 | + $to = '[email protected]'; |
|
| 101 | + break; |
|
| 102 | + default : |
|
| 103 | + $to = get_option('admin_email'); |
|
| 104 | + } |
|
| 105 | + $subject = $type . ' ' . $message . ' in ' . EVENT_ESPRESSO_VERSION . ' on ' . site_url(); |
|
| 106 | + $msg = EE_Error::_format_error($type, $message, $file, $line); |
|
| 107 | + if (function_exists('wp_mail')) { |
|
| 108 | + add_filter('wp_mail_content_type', array('EE_Error', 'set_content_type')); |
|
| 109 | + wp_mail($to, $subject, $msg); |
|
| 110 | + } |
|
| 111 | + echo '<div id="message" class="espresso-notices error"><p>'; |
|
| 112 | + echo $type . ': ' . $message . '<br />' . $file . ' line ' . $line; |
|
| 113 | + echo '<br /></p></div>'; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * error_type |
|
| 120 | + * http://www.php.net/manual/en/errorfunc.constants.php#109430 |
|
| 121 | + * |
|
| 122 | + * @param $code |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + public static function error_type($code) |
|
| 126 | + { |
|
| 127 | + switch ($code) { |
|
| 128 | + case E_ERROR: // 1 // |
|
| 129 | + return 'E_ERROR'; |
|
| 130 | + case E_WARNING: // 2 // |
|
| 131 | + return 'E_WARNING'; |
|
| 132 | + case E_PARSE: // 4 // |
|
| 133 | + return 'E_PARSE'; |
|
| 134 | + case E_NOTICE: // 8 // |
|
| 135 | + return 'E_NOTICE'; |
|
| 136 | + case E_CORE_ERROR: // 16 // |
|
| 137 | + return 'E_CORE_ERROR'; |
|
| 138 | + case E_CORE_WARNING: // 32 // |
|
| 139 | + return 'E_CORE_WARNING'; |
|
| 140 | + case E_COMPILE_ERROR: // 64 // |
|
| 141 | + return 'E_COMPILE_ERROR'; |
|
| 142 | + case E_COMPILE_WARNING: // 128 // |
|
| 143 | + return 'E_COMPILE_WARNING'; |
|
| 144 | + case E_USER_ERROR: // 256 // |
|
| 145 | + return 'E_USER_ERROR'; |
|
| 146 | + case E_USER_WARNING: // 512 // |
|
| 147 | + return 'E_USER_WARNING'; |
|
| 148 | + case E_USER_NOTICE: // 1024 // |
|
| 149 | + return 'E_USER_NOTICE'; |
|
| 150 | + case E_STRICT: // 2048 // |
|
| 151 | + return 'E_STRICT'; |
|
| 152 | + case E_RECOVERABLE_ERROR: // 4096 // |
|
| 153 | + return 'E_RECOVERABLE_ERROR'; |
|
| 154 | + case E_DEPRECATED: // 8192 // |
|
| 155 | + return 'E_DEPRECATED'; |
|
| 156 | + case E_USER_DEPRECATED: // 16384 // |
|
| 157 | + return 'E_USER_DEPRECATED'; |
|
| 158 | + case E_ALL: // 16384 // |
|
| 159 | + return 'E_ALL'; |
|
| 160 | + } |
|
| 161 | + return ''; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * fatal_error_handler |
|
| 168 | + * |
|
| 169 | + * @return void |
|
| 170 | + */ |
|
| 171 | + public static function fatal_error_handler() |
|
| 172 | + { |
|
| 173 | + $last_error = error_get_last(); |
|
| 174 | + if ($last_error['type'] === E_ERROR) { |
|
| 175 | + EE_Error::error_handler(E_ERROR, $last_error['message'], $last_error['file'], $last_error['line']); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * _format_error |
|
| 183 | + * |
|
| 184 | + * @param $code |
|
| 185 | + * @param $message |
|
| 186 | + * @param $file |
|
| 187 | + * @param $line |
|
| 188 | + * @return string |
|
| 189 | + */ |
|
| 190 | + private static function _format_error($code, $message, $file, $line) |
|
| 191 | + { |
|
| 192 | + $html = "<table cellpadding='5'><thead bgcolor='#f8f8f8'><th>Item</th><th align='left'>Details</th></thead><tbody>"; |
|
| 193 | + $html .= "<tr valign='top'><td><b>Code</b></td><td>$code</td></tr>"; |
|
| 194 | + $html .= "<tr valign='top'><td><b>Error</b></td><td>$message</td></tr>"; |
|
| 195 | + $html .= "<tr valign='top'><td><b>File</b></td><td>$file</td></tr>"; |
|
| 196 | + $html .= "<tr valign='top'><td><b>Line</b></td><td>$line</td></tr>"; |
|
| 197 | + $html .= '</tbody></table>'; |
|
| 198 | + return $html; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * set_content_type |
|
| 205 | + * |
|
| 206 | + * @param $content_type |
|
| 207 | + * @return string |
|
| 208 | + */ |
|
| 209 | + public static function set_content_type($content_type) |
|
| 210 | + { |
|
| 211 | + return 'text/html'; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @return void |
|
| 218 | + * @throws EE_Error |
|
| 219 | + * @throws ReflectionException |
|
| 220 | + */ |
|
| 221 | + public function get_error() |
|
| 222 | + { |
|
| 223 | + if (apply_filters('FHEE__EE_Error__get_error__show_normal_exceptions', false)) { |
|
| 224 | + throw $this; |
|
| 225 | + } |
|
| 226 | + // get separate user and developer messages if they exist |
|
| 227 | + $msg = explode('||', $this->getMessage()); |
|
| 228 | + $user_msg = $msg[0]; |
|
| 229 | + $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 230 | + $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 231 | + // add details to _all_exceptions array |
|
| 232 | + $x_time = time(); |
|
| 233 | + self::$_all_exceptions[$x_time]['name'] = get_class($this); |
|
| 234 | + self::$_all_exceptions[$x_time]['file'] = $this->getFile(); |
|
| 235 | + self::$_all_exceptions[$x_time]['line'] = $this->getLine(); |
|
| 236 | + self::$_all_exceptions[$x_time]['msg'] = $msg; |
|
| 237 | + self::$_all_exceptions[$x_time]['code'] = $this->getCode(); |
|
| 238 | + self::$_all_exceptions[$x_time]['trace'] = $this->getTrace(); |
|
| 239 | + self::$_all_exceptions[$x_time]['string'] = $this->getTraceAsString(); |
|
| 240 | + self::$_error_count++; |
|
| 241 | + //add_action( 'shutdown', array( $this, 'display_errors' )); |
|
| 242 | + $this->display_errors(); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param bool $check_stored |
|
| 248 | + * @param string $type_to_check |
|
| 249 | + * @return bool |
|
| 250 | + * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
|
| 251 | + * @throws \InvalidArgumentException |
|
| 252 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
| 253 | + * @throws InvalidInterfaceException |
|
| 254 | + */ |
|
| 255 | + public static function has_error($check_stored = false, $type_to_check = 'errors') |
|
| 256 | + { |
|
| 257 | + $has_error = isset(self::$_espresso_notices[$type_to_check]) |
|
| 258 | + && ! empty(self::$_espresso_notices[$type_to_check]) |
|
| 259 | + ? true |
|
| 260 | + : false; |
|
| 261 | + if ($check_stored && ! $has_error) { |
|
| 262 | + $notices = EE_Error::getStoredNotices(); |
|
| 263 | + foreach ($notices as $type => $notice) { |
|
| 264 | + if ($type === $type_to_check && $notice) { |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + return $has_error; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @echo string |
|
| 276 | + * @throws \ReflectionException |
|
| 277 | + */ |
|
| 278 | + public function display_errors() |
|
| 279 | + { |
|
| 280 | + $trace_details = ''; |
|
| 281 | + $output = ' |
|
| 282 | 282 | <style type="text/css"> |
| 283 | 283 | #ee-error-message { |
| 284 | 284 | max-width:90% !important; |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | } |
| 335 | 335 | </style> |
| 336 | 336 | <div id="ee-error-message" class="error">'; |
| 337 | - if (! WP_DEBUG) { |
|
| 338 | - $output .= ' |
|
| 337 | + if (! WP_DEBUG) { |
|
| 338 | + $output .= ' |
|
| 339 | 339 | <p>'; |
| 340 | - } |
|
| 341 | - // cycle thru errors |
|
| 342 | - foreach (self::$_all_exceptions as $time => $ex) { |
|
| 343 | - $error_code = ''; |
|
| 344 | - // process trace info |
|
| 345 | - if (empty($ex['trace'])) { |
|
| 346 | - $trace_details .= __( |
|
| 347 | - 'Sorry, but no trace information was available for this exception.', |
|
| 348 | - 'event_espresso' |
|
| 349 | - ); |
|
| 350 | - } else { |
|
| 351 | - $trace_details .= ' |
|
| 340 | + } |
|
| 341 | + // cycle thru errors |
|
| 342 | + foreach (self::$_all_exceptions as $time => $ex) { |
|
| 343 | + $error_code = ''; |
|
| 344 | + // process trace info |
|
| 345 | + if (empty($ex['trace'])) { |
|
| 346 | + $trace_details .= __( |
|
| 347 | + 'Sorry, but no trace information was available for this exception.', |
|
| 348 | + 'event_espresso' |
|
| 349 | + ); |
|
| 350 | + } else { |
|
| 351 | + $trace_details .= ' |
|
| 352 | 352 | <div id="ee-trace-details"> |
| 353 | 353 | <table width="100%" border="0" cellpadding="5" cellspacing="0"> |
| 354 | 354 | <tr> |
@@ -358,43 +358,43 @@ discard block |
||
| 358 | 358 | <th scope="col" align="left">Class</th> |
| 359 | 359 | <th scope="col" align="left">Method( arguments )</th> |
| 360 | 360 | </tr>'; |
| 361 | - $last_on_stack = count($ex['trace']) - 1; |
|
| 362 | - // reverse array so that stack is in proper chronological order |
|
| 363 | - $sorted_trace = array_reverse($ex['trace']); |
|
| 364 | - foreach ($sorted_trace as $nmbr => $trace) { |
|
| 365 | - $file = isset($trace['file']) ? $trace['file'] : ''; |
|
| 366 | - $class = isset($trace['class']) ? $trace['class'] : ''; |
|
| 367 | - $type = isset($trace['type']) ? $trace['type'] : ''; |
|
| 368 | - $function = isset($trace['function']) ? $trace['function'] : ''; |
|
| 369 | - $args = isset($trace['args']) ? $this->_convert_args_to_string($trace['args']) : ''; |
|
| 370 | - $line = isset($trace['line']) ? $trace['line'] : ''; |
|
| 371 | - $zebra = ($nmbr % 2) ? ' odd' : ''; |
|
| 372 | - if (empty($file) && ! empty($class)) { |
|
| 373 | - $a = new ReflectionClass($class); |
|
| 374 | - $file = $a->getFileName(); |
|
| 375 | - if (empty($line) && ! empty($function)) { |
|
| 376 | - try { |
|
| 377 | - //if $function is a closure, this throws an exception |
|
| 378 | - $b = new ReflectionMethod($class, $function); |
|
| 379 | - $line = $b->getStartLine(); |
|
| 380 | - } catch (Exception $closure_exception) { |
|
| 381 | - $line = 'unknown'; |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - if ($nmbr === $last_on_stack) { |
|
| 386 | - $file = $ex['file'] !== '' ? $ex['file'] : $file; |
|
| 387 | - $line = $ex['line'] !== '' ? $ex['line'] : $line; |
|
| 388 | - $error_code = self::generate_error_code($file, $trace['function'], $line); |
|
| 389 | - } |
|
| 390 | - $nmbr_dsply = ! empty($nmbr) ? $nmbr : ' '; |
|
| 391 | - $line_dsply = ! empty($line) ? $line : ' '; |
|
| 392 | - $file_dsply = ! empty($file) ? $file : ' '; |
|
| 393 | - $class_dsply = ! empty($class) ? $class : ' '; |
|
| 394 | - $type_dsply = ! empty($type) ? $type : ' '; |
|
| 395 | - $function_dsply = ! empty($function) ? $function : ' '; |
|
| 396 | - $args_dsply = ! empty($args) ? '( ' . $args . ' )' : ''; |
|
| 397 | - $trace_details .= ' |
|
| 361 | + $last_on_stack = count($ex['trace']) - 1; |
|
| 362 | + // reverse array so that stack is in proper chronological order |
|
| 363 | + $sorted_trace = array_reverse($ex['trace']); |
|
| 364 | + foreach ($sorted_trace as $nmbr => $trace) { |
|
| 365 | + $file = isset($trace['file']) ? $trace['file'] : ''; |
|
| 366 | + $class = isset($trace['class']) ? $trace['class'] : ''; |
|
| 367 | + $type = isset($trace['type']) ? $trace['type'] : ''; |
|
| 368 | + $function = isset($trace['function']) ? $trace['function'] : ''; |
|
| 369 | + $args = isset($trace['args']) ? $this->_convert_args_to_string($trace['args']) : ''; |
|
| 370 | + $line = isset($trace['line']) ? $trace['line'] : ''; |
|
| 371 | + $zebra = ($nmbr % 2) ? ' odd' : ''; |
|
| 372 | + if (empty($file) && ! empty($class)) { |
|
| 373 | + $a = new ReflectionClass($class); |
|
| 374 | + $file = $a->getFileName(); |
|
| 375 | + if (empty($line) && ! empty($function)) { |
|
| 376 | + try { |
|
| 377 | + //if $function is a closure, this throws an exception |
|
| 378 | + $b = new ReflectionMethod($class, $function); |
|
| 379 | + $line = $b->getStartLine(); |
|
| 380 | + } catch (Exception $closure_exception) { |
|
| 381 | + $line = 'unknown'; |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + if ($nmbr === $last_on_stack) { |
|
| 386 | + $file = $ex['file'] !== '' ? $ex['file'] : $file; |
|
| 387 | + $line = $ex['line'] !== '' ? $ex['line'] : $line; |
|
| 388 | + $error_code = self::generate_error_code($file, $trace['function'], $line); |
|
| 389 | + } |
|
| 390 | + $nmbr_dsply = ! empty($nmbr) ? $nmbr : ' '; |
|
| 391 | + $line_dsply = ! empty($line) ? $line : ' '; |
|
| 392 | + $file_dsply = ! empty($file) ? $file : ' '; |
|
| 393 | + $class_dsply = ! empty($class) ? $class : ' '; |
|
| 394 | + $type_dsply = ! empty($type) ? $type : ' '; |
|
| 395 | + $function_dsply = ! empty($function) ? $function : ' '; |
|
| 396 | + $args_dsply = ! empty($args) ? '( ' . $args . ' )' : ''; |
|
| 397 | + $trace_details .= ' |
|
| 398 | 398 | <tr> |
| 399 | 399 | <td align="right" class="' . $zebra . '">' . $nmbr_dsply . '</td> |
| 400 | 400 | <td align="right" class="' . $zebra . '">' . $line_dsply . '</td> |
@@ -402,619 +402,619 @@ discard block |
||
| 402 | 402 | <td align="left" class="' . $zebra . '">' . $class_dsply . '</td> |
| 403 | 403 | <td align="left" class="' . $zebra . '">' . $type_dsply . $function_dsply . $args_dsply . '</td> |
| 404 | 404 | </tr>'; |
| 405 | - } |
|
| 406 | - $trace_details .= ' |
|
| 405 | + } |
|
| 406 | + $trace_details .= ' |
|
| 407 | 407 | </table> |
| 408 | 408 | </div>'; |
| 409 | - } |
|
| 410 | - $ex['code'] = $ex['code'] ? $ex['code'] : $error_code; |
|
| 411 | - // add generic non-identifying messages for non-privileged users |
|
| 412 | - if (! WP_DEBUG) { |
|
| 413 | - $output .= '<span class="ee-error-user-msg-spn">' |
|
| 414 | - . trim($ex['msg']) |
|
| 415 | - . '</span> <sup>' |
|
| 416 | - . $ex['code'] |
|
| 417 | - . '</sup><br />'; |
|
| 418 | - } else { |
|
| 419 | - // or helpful developer messages if debugging is on |
|
| 420 | - $output .= ' |
|
| 409 | + } |
|
| 410 | + $ex['code'] = $ex['code'] ? $ex['code'] : $error_code; |
|
| 411 | + // add generic non-identifying messages for non-privileged users |
|
| 412 | + if (! WP_DEBUG) { |
|
| 413 | + $output .= '<span class="ee-error-user-msg-spn">' |
|
| 414 | + . trim($ex['msg']) |
|
| 415 | + . '</span> <sup>' |
|
| 416 | + . $ex['code'] |
|
| 417 | + . '</sup><br />'; |
|
| 418 | + } else { |
|
| 419 | + // or helpful developer messages if debugging is on |
|
| 420 | + $output .= ' |
|
| 421 | 421 | <div class="ee-error-dev-msg-dv"> |
| 422 | 422 | <p class="ee-error-dev-msg-pg"> |
| 423 | 423 | <strong class="ee-error-dev-msg-str">An ' |
| 424 | - . $ex['name'] |
|
| 425 | - . ' exception was thrown!</strong> <span>code: ' |
|
| 426 | - . $ex['code'] |
|
| 427 | - . '</span><br /> |
|
| 424 | + . $ex['name'] |
|
| 425 | + . ' exception was thrown!</strong> <span>code: ' |
|
| 426 | + . $ex['code'] |
|
| 427 | + . '</span><br /> |
|
| 428 | 428 | <span class="big-text">"' |
| 429 | - . trim($ex['msg']) |
|
| 430 | - . '"</span><br/> |
|
| 429 | + . trim($ex['msg']) |
|
| 430 | + . '"</span><br/> |
|
| 431 | 431 | <a id="display-ee-error-trace-' |
| 432 | - . self::$_error_count |
|
| 433 | - . $time |
|
| 434 | - . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-' |
|
| 435 | - . self::$_error_count |
|
| 436 | - . $time |
|
| 437 | - . '"> |
|
| 432 | + . self::$_error_count |
|
| 433 | + . $time |
|
| 434 | + . '" class="display-ee-error-trace-lnk small-text" rel="ee-error-trace-' |
|
| 435 | + . self::$_error_count |
|
| 436 | + . $time |
|
| 437 | + . '"> |
|
| 438 | 438 | ' |
| 439 | - . __('click to view backtrace and class/method details', 'event_espresso') |
|
| 440 | - . ' |
|
| 439 | + . __('click to view backtrace and class/method details', 'event_espresso') |
|
| 440 | + . ' |
|
| 441 | 441 | </a><br /> |
| 442 | 442 | <span class="small-text lt-grey-text">' |
| 443 | - . $ex['file'] |
|
| 444 | - . ' ( line no: ' |
|
| 445 | - . $ex['line'] |
|
| 446 | - . ' )</span> |
|
| 443 | + . $ex['file'] |
|
| 444 | + . ' ( line no: ' |
|
| 445 | + . $ex['line'] |
|
| 446 | + . ' )</span> |
|
| 447 | 447 | </p> |
| 448 | 448 | <div id="ee-error-trace-' |
| 449 | - . self::$_error_count |
|
| 450 | - . $time |
|
| 451 | - . '-dv" class="ee-error-trace-dv" style="display: none;"> |
|
| 449 | + . self::$_error_count |
|
| 450 | + . $time |
|
| 451 | + . '-dv" class="ee-error-trace-dv" style="display: none;"> |
|
| 452 | 452 | ' |
| 453 | - . $trace_details; |
|
| 454 | - if (! empty($class)) { |
|
| 455 | - $output .= ' |
|
| 453 | + . $trace_details; |
|
| 454 | + if (! empty($class)) { |
|
| 455 | + $output .= ' |
|
| 456 | 456 | <div style="padding:3px; margin:0 0 1em; border:1px solid #666; background:#fff; border-radius:3px;"> |
| 457 | 457 | <div style="padding:1em 2em; border:1px solid #666; background:#f9f9f9;"> |
| 458 | 458 | <h3>Class Details</h3>'; |
| 459 | - $a = new ReflectionClass($class); |
|
| 460 | - $output .= ' |
|
| 459 | + $a = new ReflectionClass($class); |
|
| 460 | + $output .= ' |
|
| 461 | 461 | <pre>' . $a . '</pre> |
| 462 | 462 | </div> |
| 463 | 463 | </div>'; |
| 464 | - } |
|
| 465 | - $output .= ' |
|
| 464 | + } |
|
| 465 | + $output .= ' |
|
| 466 | 466 | </div> |
| 467 | 467 | </div> |
| 468 | 468 | <br />'; |
| 469 | - } |
|
| 470 | - $this->write_to_error_log($time, $ex); |
|
| 471 | - } |
|
| 472 | - // remove last linebreak |
|
| 473 | - $output = substr($output, 0, -6); |
|
| 474 | - if (! WP_DEBUG) { |
|
| 475 | - $output .= ' |
|
| 469 | + } |
|
| 470 | + $this->write_to_error_log($time, $ex); |
|
| 471 | + } |
|
| 472 | + // remove last linebreak |
|
| 473 | + $output = substr($output, 0, -6); |
|
| 474 | + if (! WP_DEBUG) { |
|
| 475 | + $output .= ' |
|
| 476 | 476 | </p>'; |
| 477 | - } |
|
| 478 | - $output .= ' |
|
| 477 | + } |
|
| 478 | + $output .= ' |
|
| 479 | 479 | </div>'; |
| 480 | - $output .= self::_print_scripts(true); |
|
| 481 | - if (defined('DOING_AJAX')) { |
|
| 482 | - echo wp_json_encode(array('error' => $output)); |
|
| 483 | - exit(); |
|
| 484 | - } |
|
| 485 | - echo $output; |
|
| 486 | - die(); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * generate string from exception trace args |
|
| 493 | - * |
|
| 494 | - * @param array $arguments |
|
| 495 | - * @param bool $array |
|
| 496 | - * @return string |
|
| 497 | - */ |
|
| 498 | - private function _convert_args_to_string($arguments = array(), $array = false) |
|
| 499 | - { |
|
| 500 | - $arg_string = ''; |
|
| 501 | - if (! empty($arguments)) { |
|
| 502 | - $args = array(); |
|
| 503 | - foreach ($arguments as $arg) { |
|
| 504 | - if (! empty($arg)) { |
|
| 505 | - if (is_string($arg)) { |
|
| 506 | - $args[] = " '" . $arg . "'"; |
|
| 507 | - } elseif (is_array($arg)) { |
|
| 508 | - $args[] = 'ARRAY(' . $this->_convert_args_to_string($arg, true); |
|
| 509 | - } elseif ($arg === null) { |
|
| 510 | - $args[] = ' NULL'; |
|
| 511 | - } elseif (is_bool($arg)) { |
|
| 512 | - $args[] = ($arg) ? ' TRUE' : ' FALSE'; |
|
| 513 | - } elseif (is_object($arg)) { |
|
| 514 | - $args[] = ' OBJECT ' . get_class($arg); |
|
| 515 | - } elseif (is_resource($arg)) { |
|
| 516 | - $args[] = get_resource_type($arg); |
|
| 517 | - } else { |
|
| 518 | - $args[] = $arg; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - $arg_string = implode(', ', $args); |
|
| 523 | - } |
|
| 524 | - if ($array) { |
|
| 525 | - $arg_string .= ' )'; |
|
| 526 | - } |
|
| 527 | - return $arg_string; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * add error message |
|
| 534 | - * |
|
| 535 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 536 | - * separate messages for user || dev |
|
| 537 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 538 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 539 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 540 | - * @return void |
|
| 541 | - */ |
|
| 542 | - public static function add_error($msg = null, $file = null, $func = null, $line = null) |
|
| 543 | - { |
|
| 544 | - self::_add_notice('errors', $msg, $file, $func, $line); |
|
| 545 | - self::$_error_count++; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * If WP_DEBUG is active, throws an exception. If WP_DEBUG is off, just |
|
| 552 | - * adds an error |
|
| 553 | - * |
|
| 554 | - * @param string $msg |
|
| 555 | - * @param string $file |
|
| 556 | - * @param string $func |
|
| 557 | - * @param string $line |
|
| 558 | - * @throws EE_Error |
|
| 559 | - */ |
|
| 560 | - public static function throw_exception_if_debugging($msg = null, $file = null, $func = null, $line = null) |
|
| 561 | - { |
|
| 562 | - if (WP_DEBUG) { |
|
| 563 | - throw new EE_Error($msg); |
|
| 564 | - } |
|
| 565 | - EE_Error::add_error($msg, $file, $func, $line); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * add success message |
|
| 572 | - * |
|
| 573 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 574 | - * separate messages for user || dev |
|
| 575 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 576 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 577 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 578 | - * @return void |
|
| 579 | - */ |
|
| 580 | - public static function add_success($msg = null, $file = null, $func = null, $line = null) |
|
| 581 | - { |
|
| 582 | - self::_add_notice('success', $msg, $file, $func, $line); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * add attention message |
|
| 589 | - * |
|
| 590 | - * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 591 | - * separate messages for user || dev |
|
| 592 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 593 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 594 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 595 | - * @return void |
|
| 596 | - */ |
|
| 597 | - public static function add_attention($msg = null, $file = null, $func = null, $line = null) |
|
| 598 | - { |
|
| 599 | - self::_add_notice('attention', $msg, $file, $func, $line); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * @param string $type whether the message is for a success or error notification |
|
| 606 | - * @param string $msg the message to display to users or developers |
|
| 607 | - * - adding a double pipe || (OR) creates separate messages for user || dev |
|
| 608 | - * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 609 | - * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 610 | - * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 611 | - * @return void |
|
| 612 | - */ |
|
| 613 | - private static function _add_notice($type = 'success', $msg = '', $file = '', $func = '', $line = '') |
|
| 614 | - { |
|
| 615 | - if (empty($msg)) { |
|
| 616 | - EE_Error::doing_it_wrong( |
|
| 617 | - 'EE_Error::add_' . $type . '()', |
|
| 618 | - sprintf( |
|
| 619 | - __('Notifications are not much use without a message! Please add a message to the EE_Error::add_%s() call made in %s on line %d', |
|
| 620 | - 'event_espresso'), |
|
| 621 | - $type, |
|
| 622 | - $file, |
|
| 623 | - $line |
|
| 624 | - ), |
|
| 625 | - EVENT_ESPRESSO_VERSION |
|
| 626 | - ); |
|
| 627 | - } |
|
| 628 | - if ($type === 'errors' && (empty($file) || empty($func) || empty($line))) { |
|
| 629 | - EE_Error::doing_it_wrong( |
|
| 630 | - 'EE_Error::add_error()', |
|
| 631 | - __('You need to provide the file name, function name, and line number that the error occurred on in order to better assist with debugging.', |
|
| 632 | - 'event_espresso'), |
|
| 633 | - EVENT_ESPRESSO_VERSION |
|
| 634 | - ); |
|
| 635 | - } |
|
| 636 | - // get separate user and developer messages if they exist |
|
| 637 | - $msg = explode('||', $msg); |
|
| 638 | - $user_msg = $msg[0]; |
|
| 639 | - $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 640 | - /** |
|
| 641 | - * Do an action so other code can be triggered when a notice is created |
|
| 642 | - * |
|
| 643 | - * @param string $type can be 'errors', 'attention', or 'success' |
|
| 644 | - * @param string $user_msg message displayed to user when WP_DEBUG is off |
|
| 645 | - * @param string $user_msg message displayed to user when WP_DEBUG is on |
|
| 646 | - * @param string $file file where error was generated |
|
| 647 | - * @param string $func function where error was generated |
|
| 648 | - * @param string $line line where error was generated |
|
| 649 | - */ |
|
| 650 | - do_action('AHEE__EE_Error___add_notice', $type, $user_msg, $dev_msg, $file, $func, $line); |
|
| 651 | - $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 652 | - // add notice if message exists |
|
| 653 | - if (! empty($msg)) { |
|
| 654 | - // get error code |
|
| 655 | - $notice_code = EE_Error::generate_error_code($file, $func, $line); |
|
| 656 | - if (WP_DEBUG && $type === 'errors') { |
|
| 657 | - $msg .= '<br/><span class="tiny-text">' . $notice_code . '</span>'; |
|
| 658 | - } |
|
| 659 | - // add notice. Index by code if it's not blank |
|
| 660 | - if ($notice_code) { |
|
| 661 | - self::$_espresso_notices[$type][$notice_code] = $msg; |
|
| 662 | - } else { |
|
| 663 | - self::$_espresso_notices[$type][] = $msg; |
|
| 664 | - } |
|
| 665 | - add_action('wp_footer', array('EE_Error', 'enqueue_error_scripts'), 1); |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * in some case it may be necessary to overwrite the existing success messages |
|
| 672 | - * |
|
| 673 | - * @return void |
|
| 674 | - */ |
|
| 675 | - public static function overwrite_success() |
|
| 676 | - { |
|
| 677 | - self::$_espresso_notices['success'] = false; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * in some case it may be necessary to overwrite the existing attention messages |
|
| 684 | - * |
|
| 685 | - * @return void |
|
| 686 | - */ |
|
| 687 | - public static function overwrite_attention() |
|
| 688 | - { |
|
| 689 | - self::$_espresso_notices['attention'] = false; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * in some case it may be necessary to overwrite the existing error messages |
|
| 696 | - * |
|
| 697 | - * @return void |
|
| 698 | - */ |
|
| 699 | - public static function overwrite_errors() |
|
| 700 | - { |
|
| 701 | - self::$_espresso_notices['errors'] = false; |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * @return void |
|
| 708 | - */ |
|
| 709 | - public static function reset_notices() |
|
| 710 | - { |
|
| 711 | - self::$_espresso_notices['success'] = false; |
|
| 712 | - self::$_espresso_notices['attention'] = false; |
|
| 713 | - self::$_espresso_notices['errors'] = false; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * @return int |
|
| 720 | - */ |
|
| 721 | - public static function has_notices() |
|
| 722 | - { |
|
| 723 | - $has_notices = 0; |
|
| 724 | - // check for success messages |
|
| 725 | - $has_notices = self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success']) |
|
| 726 | - ? 3 |
|
| 727 | - : $has_notices; |
|
| 728 | - // check for attention messages |
|
| 729 | - $has_notices = self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention']) |
|
| 730 | - ? 2 |
|
| 731 | - : $has_notices; |
|
| 732 | - // check for error messages |
|
| 733 | - $has_notices = self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors']) |
|
| 734 | - ? 1 |
|
| 735 | - : $has_notices; |
|
| 736 | - return $has_notices; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * This simply returns non formatted error notices as they were sent into the EE_Error object. |
|
| 742 | - * |
|
| 743 | - * @since 4.9.0 |
|
| 744 | - * @return array |
|
| 745 | - */ |
|
| 746 | - public static function get_vanilla_notices() |
|
| 747 | - { |
|
| 748 | - return array( |
|
| 749 | - 'success' => isset(self::$_espresso_notices['success']) |
|
| 750 | - ? self::$_espresso_notices['success'] |
|
| 751 | - : array(), |
|
| 752 | - 'attention' => isset(self::$_espresso_notices['attention']) |
|
| 753 | - ? self::$_espresso_notices['attention'] |
|
| 754 | - : array(), |
|
| 755 | - 'errors' => isset(self::$_espresso_notices['errors']) |
|
| 756 | - ? self::$_espresso_notices['errors'] |
|
| 757 | - : array(), |
|
| 758 | - ); |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - |
|
| 762 | - /** |
|
| 763 | - * @return array |
|
| 764 | - * @throws InvalidArgumentException |
|
| 765 | - * @throws InvalidDataTypeException |
|
| 766 | - * @throws InvalidInterfaceException |
|
| 767 | - */ |
|
| 768 | - public static function getStoredNotices() |
|
| 769 | - { |
|
| 770 | - if ($user_id = get_current_user_id()) { |
|
| 771 | - // get notices for logged in user |
|
| 772 | - $notices = get_user_option(EE_Error::OPTIONS_KEY_NOTICES, $user_id); |
|
| 773 | - return is_array($notices) ? $notices : array(); |
|
| 774 | - } |
|
| 775 | - if (EE_Session::isLoadedAndActive()) { |
|
| 776 | - // get notices for user currently engaged in a session |
|
| 777 | - $session_data = EE_Session::instance()->get_session_data(EE_Error::OPTIONS_KEY_NOTICES); |
|
| 778 | - return is_array($session_data) ? $session_data : array(); |
|
| 779 | - } |
|
| 780 | - // get global notices and hope they apply to the current site visitor |
|
| 781 | - $notices = get_option(EE_Error::OPTIONS_KEY_NOTICES, array()); |
|
| 782 | - return is_array($notices) ? $notices : array(); |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * @param array $notices |
|
| 788 | - * @return bool |
|
| 789 | - * @throws InvalidArgumentException |
|
| 790 | - * @throws InvalidDataTypeException |
|
| 791 | - * @throws InvalidInterfaceException |
|
| 792 | - */ |
|
| 793 | - public static function storeNotices(array $notices) |
|
| 794 | - { |
|
| 795 | - if ($user_id = get_current_user_id()) { |
|
| 796 | - // store notices for logged in user |
|
| 797 | - return (bool) update_user_option( |
|
| 798 | - EE_Error::OPTIONS_KEY_NOTICES, |
|
| 799 | - $user_id, |
|
| 800 | - $notices |
|
| 801 | - ); |
|
| 802 | - } |
|
| 803 | - if (EE_Session::isLoadedAndActive()) { |
|
| 804 | - // store notices for user currently engaged in a session |
|
| 805 | - return EE_Session::instance()->set_session_data( |
|
| 806 | - array(EE_Error::OPTIONS_KEY_NOTICES => $notices) |
|
| 807 | - ); |
|
| 808 | - } |
|
| 809 | - // store global notices and hope they apply to the same site visitor on the next request |
|
| 810 | - return update_option(EE_Error::OPTIONS_KEY_NOTICES, $notices); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - |
|
| 814 | - /** |
|
| 815 | - * @return bool|TRUE |
|
| 816 | - * @throws InvalidArgumentException |
|
| 817 | - * @throws InvalidDataTypeException |
|
| 818 | - * @throws InvalidInterfaceException |
|
| 819 | - */ |
|
| 820 | - public static function clearNotices() |
|
| 821 | - { |
|
| 822 | - if ($user_id = get_current_user_id()) { |
|
| 823 | - // clear notices for logged in user |
|
| 824 | - return (bool) update_user_option( |
|
| 825 | - EE_Error::OPTIONS_KEY_NOTICES, |
|
| 826 | - $user_id, |
|
| 827 | - array() |
|
| 828 | - ); |
|
| 829 | - } |
|
| 830 | - if (EE_Session::isLoadedAndActive()) { |
|
| 831 | - // clear notices for user currently engaged in a session |
|
| 832 | - return EE_Session::instance()->reset_data(EE_Error::OPTIONS_KEY_NOTICES); |
|
| 833 | - } |
|
| 834 | - // clear global notices and hope none belonged to some for some other site visitor |
|
| 835 | - return update_option(EE_Error::OPTIONS_KEY_NOTICES, array()); |
|
| 836 | - } |
|
| 837 | - |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * compile all error or success messages into one string |
|
| 841 | - * |
|
| 842 | - * @see EE_Error::get_raw_notices if you want the raw notices without any preparations made to them |
|
| 843 | - * @param boolean $format_output whether or not to format the messages for display in the WP admin |
|
| 844 | - * @param boolean $save_to_transient whether or not to save notices to the db for retrieval on next request |
|
| 845 | - * - ONLY do this just before redirecting |
|
| 846 | - * @param boolean $remove_empty whether or not to unset empty messages |
|
| 847 | - * @return array |
|
| 848 | - * @throws InvalidArgumentException |
|
| 849 | - * @throws InvalidDataTypeException |
|
| 850 | - * @throws InvalidInterfaceException |
|
| 851 | - */ |
|
| 852 | - public static function get_notices($format_output = true, $save_to_transient = false, $remove_empty = true) |
|
| 853 | - { |
|
| 854 | - $success_messages = ''; |
|
| 855 | - $attention_messages = ''; |
|
| 856 | - $error_messages = ''; |
|
| 857 | - // either save notices to the db |
|
| 858 | - if ($save_to_transient || isset($_REQUEST['activate-selected'])) { |
|
| 859 | - self::$_espresso_notices = array_merge( |
|
| 860 | - EE_Error::getStoredNotices(), |
|
| 861 | - self::$_espresso_notices |
|
| 862 | - ); |
|
| 863 | - EE_Error::storeNotices(self::$_espresso_notices); |
|
| 864 | - return array(); |
|
| 865 | - } |
|
| 866 | - $print_scripts = EE_Error::combineExistingAndNewNotices(); |
|
| 867 | - // check for success messages |
|
| 868 | - if (self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success'])) { |
|
| 869 | - // combine messages |
|
| 870 | - $success_messages .= implode(self::$_espresso_notices['success'], '<br />'); |
|
| 871 | - $print_scripts = true; |
|
| 872 | - } |
|
| 873 | - // check for attention messages |
|
| 874 | - if (self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention'])) { |
|
| 875 | - // combine messages |
|
| 876 | - $attention_messages .= implode(self::$_espresso_notices['attention'], '<br />'); |
|
| 877 | - $print_scripts = true; |
|
| 878 | - } |
|
| 879 | - // check for error messages |
|
| 880 | - if (self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors'])) { |
|
| 881 | - $error_messages .= count(self::$_espresso_notices['errors']) > 1 |
|
| 882 | - ? __('The following errors have occurred:<br />', 'event_espresso') |
|
| 883 | - : __('An error has occurred:<br />', 'event_espresso'); |
|
| 884 | - // combine messages |
|
| 885 | - $error_messages .= implode(self::$_espresso_notices['errors'], '<br />'); |
|
| 886 | - $print_scripts = true; |
|
| 887 | - } |
|
| 888 | - if ($format_output) { |
|
| 889 | - $notices = EE_Error::formatNoticesOutput( |
|
| 890 | - $success_messages, |
|
| 891 | - $attention_messages, |
|
| 892 | - $error_messages |
|
| 893 | - ); |
|
| 894 | - } else { |
|
| 895 | - $notices = array( |
|
| 896 | - 'success' => $success_messages, |
|
| 897 | - 'attention' => $attention_messages, |
|
| 898 | - 'errors' => $error_messages, |
|
| 899 | - ); |
|
| 900 | - if ($remove_empty) { |
|
| 901 | - // remove empty notices |
|
| 902 | - foreach ($notices as $type => $notice) { |
|
| 903 | - if (empty($notice)) { |
|
| 904 | - unset($notices[$type]); |
|
| 905 | - } |
|
| 906 | - } |
|
| 907 | - } |
|
| 908 | - } |
|
| 909 | - if ($print_scripts) { |
|
| 910 | - self::_print_scripts(); |
|
| 911 | - } |
|
| 912 | - return $notices; |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * @return bool |
|
| 918 | - * @throws InvalidArgumentException |
|
| 919 | - * @throws InvalidDataTypeException |
|
| 920 | - * @throws InvalidInterfaceException |
|
| 921 | - */ |
|
| 922 | - private static function combineExistingAndNewNotices() |
|
| 923 | - { |
|
| 924 | - $print_scripts = false; |
|
| 925 | - // grab any notices that have been previously saved |
|
| 926 | - $notices = EE_Error::getStoredNotices(); |
|
| 927 | - if (! empty($notices)) { |
|
| 928 | - foreach ($notices as $type => $notice) { |
|
| 929 | - if (is_array($notice) && ! empty($notice)) { |
|
| 930 | - // make sure that existing notice type is an array |
|
| 931 | - self::$_espresso_notices[ $type ] = is_array(self::$_espresso_notices[ $type ]) |
|
| 932 | - && ! empty(self::$_espresso_notices[ $type ]) |
|
| 933 | - ? self::$_espresso_notices[ $type ] |
|
| 934 | - : array(); |
|
| 935 | - // add newly created notices to existing ones |
|
| 936 | - self::$_espresso_notices[ $type ] += $notice; |
|
| 937 | - $print_scripts = true; |
|
| 938 | - } |
|
| 939 | - } |
|
| 940 | - // now clear any stored notices |
|
| 941 | - EE_Error::clearNotices(); |
|
| 942 | - } |
|
| 943 | - return $print_scripts; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * @param string $success_messages |
|
| 949 | - * @param string $attention_messages |
|
| 950 | - * @param string $error_messages |
|
| 951 | - * @return string |
|
| 952 | - */ |
|
| 953 | - private static function formatNoticesOutput($success_messages, $attention_messages, $error_messages) |
|
| 954 | - { |
|
| 955 | - $notices = '<div id="espresso-notices">'; |
|
| 956 | - $close = is_admin() |
|
| 957 | - ? '' |
|
| 958 | - : '<a class="close-espresso-notice hide-if-no-js"><span class="dashicons dashicons-no"/></a>'; |
|
| 959 | - if ($success_messages !== '') { |
|
| 960 | - $css_id = is_admin() ? 'message' : 'espresso-notices-success'; |
|
| 961 | - $css_class = is_admin() ? 'updated fade' : 'success fade-away'; |
|
| 962 | - //showMessage( $success_messages ); |
|
| 963 | - $notices .= '<div id="' . $css_id . '" ' |
|
| 964 | - . 'class="espresso-notices ' . $css_class . '" ' |
|
| 965 | - . 'style="display:none;">' |
|
| 966 | - . '<p>' . $success_messages . '</p>' |
|
| 967 | - . $close |
|
| 968 | - . '</div>'; |
|
| 969 | - } |
|
| 970 | - if ($attention_messages !== '') { |
|
| 971 | - $css_id = is_admin() ? 'message' : 'espresso-notices-attention'; |
|
| 972 | - $css_class = is_admin() ? 'updated ee-notices-attention' : 'attention fade-away'; |
|
| 973 | - //showMessage( $error_messages, TRUE ); |
|
| 974 | - $notices .= '<div id="' . $css_id . '" ' |
|
| 975 | - . 'class="espresso-notices ' . $css_class . '" ' |
|
| 976 | - . 'style="display:none;">' |
|
| 977 | - . '<p>' . $attention_messages . '</p>' |
|
| 978 | - . $close |
|
| 979 | - . '</div>'; |
|
| 980 | - } |
|
| 981 | - if ($error_messages !== '') { |
|
| 982 | - $css_id = is_admin() ? 'message' : 'espresso-notices-error'; |
|
| 983 | - $css_class = is_admin() ? 'error' : 'error fade-away'; |
|
| 984 | - //showMessage( $error_messages, TRUE ); |
|
| 985 | - $notices .= '<div id="' . $css_id . '" ' |
|
| 986 | - . 'class="espresso-notices ' . $css_class . '" ' |
|
| 987 | - . 'style="display:none;">' |
|
| 988 | - . '<p>' . $error_messages . '</p>' |
|
| 989 | - . $close |
|
| 990 | - . '</div>'; |
|
| 991 | - } |
|
| 992 | - $notices .= '</div>'; |
|
| 993 | - return $notices; |
|
| 994 | - } |
|
| 995 | - |
|
| 996 | - |
|
| 997 | - |
|
| 998 | - /** |
|
| 999 | - * _print_scripts |
|
| 1000 | - * |
|
| 1001 | - * @param bool $force_print |
|
| 1002 | - * @return string |
|
| 1003 | - */ |
|
| 1004 | - private static function _print_scripts($force_print = false) |
|
| 1005 | - { |
|
| 1006 | - if (! $force_print && (did_action('admin_enqueue_scripts') || did_action('wp_enqueue_scripts'))) { |
|
| 1007 | - if (wp_script_is('ee_error_js', 'enqueued')) { |
|
| 1008 | - return ''; |
|
| 1009 | - } |
|
| 1010 | - if (wp_script_is('ee_error_js', 'registered')) { |
|
| 1011 | - wp_enqueue_style('espresso_default'); |
|
| 1012 | - wp_enqueue_style('espresso_custom_css'); |
|
| 1013 | - wp_enqueue_script('ee_error_js'); |
|
| 1014 | - wp_localize_script('ee_error_js', 'ee_settings', array('wp_debug' => WP_DEBUG)); |
|
| 1015 | - } |
|
| 1016 | - } else { |
|
| 1017 | - return ' |
|
| 480 | + $output .= self::_print_scripts(true); |
|
| 481 | + if (defined('DOING_AJAX')) { |
|
| 482 | + echo wp_json_encode(array('error' => $output)); |
|
| 483 | + exit(); |
|
| 484 | + } |
|
| 485 | + echo $output; |
|
| 486 | + die(); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * generate string from exception trace args |
|
| 493 | + * |
|
| 494 | + * @param array $arguments |
|
| 495 | + * @param bool $array |
|
| 496 | + * @return string |
|
| 497 | + */ |
|
| 498 | + private function _convert_args_to_string($arguments = array(), $array = false) |
|
| 499 | + { |
|
| 500 | + $arg_string = ''; |
|
| 501 | + if (! empty($arguments)) { |
|
| 502 | + $args = array(); |
|
| 503 | + foreach ($arguments as $arg) { |
|
| 504 | + if (! empty($arg)) { |
|
| 505 | + if (is_string($arg)) { |
|
| 506 | + $args[] = " '" . $arg . "'"; |
|
| 507 | + } elseif (is_array($arg)) { |
|
| 508 | + $args[] = 'ARRAY(' . $this->_convert_args_to_string($arg, true); |
|
| 509 | + } elseif ($arg === null) { |
|
| 510 | + $args[] = ' NULL'; |
|
| 511 | + } elseif (is_bool($arg)) { |
|
| 512 | + $args[] = ($arg) ? ' TRUE' : ' FALSE'; |
|
| 513 | + } elseif (is_object($arg)) { |
|
| 514 | + $args[] = ' OBJECT ' . get_class($arg); |
|
| 515 | + } elseif (is_resource($arg)) { |
|
| 516 | + $args[] = get_resource_type($arg); |
|
| 517 | + } else { |
|
| 518 | + $args[] = $arg; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + $arg_string = implode(', ', $args); |
|
| 523 | + } |
|
| 524 | + if ($array) { |
|
| 525 | + $arg_string .= ' )'; |
|
| 526 | + } |
|
| 527 | + return $arg_string; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * add error message |
|
| 534 | + * |
|
| 535 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 536 | + * separate messages for user || dev |
|
| 537 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 538 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 539 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 540 | + * @return void |
|
| 541 | + */ |
|
| 542 | + public static function add_error($msg = null, $file = null, $func = null, $line = null) |
|
| 543 | + { |
|
| 544 | + self::_add_notice('errors', $msg, $file, $func, $line); |
|
| 545 | + self::$_error_count++; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * If WP_DEBUG is active, throws an exception. If WP_DEBUG is off, just |
|
| 552 | + * adds an error |
|
| 553 | + * |
|
| 554 | + * @param string $msg |
|
| 555 | + * @param string $file |
|
| 556 | + * @param string $func |
|
| 557 | + * @param string $line |
|
| 558 | + * @throws EE_Error |
|
| 559 | + */ |
|
| 560 | + public static function throw_exception_if_debugging($msg = null, $file = null, $func = null, $line = null) |
|
| 561 | + { |
|
| 562 | + if (WP_DEBUG) { |
|
| 563 | + throw new EE_Error($msg); |
|
| 564 | + } |
|
| 565 | + EE_Error::add_error($msg, $file, $func, $line); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * add success message |
|
| 572 | + * |
|
| 573 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 574 | + * separate messages for user || dev |
|
| 575 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 576 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 577 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 578 | + * @return void |
|
| 579 | + */ |
|
| 580 | + public static function add_success($msg = null, $file = null, $func = null, $line = null) |
|
| 581 | + { |
|
| 582 | + self::_add_notice('success', $msg, $file, $func, $line); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * add attention message |
|
| 589 | + * |
|
| 590 | + * @param string $msg the message to display to users or developers - adding a double pipe || (OR) creates |
|
| 591 | + * separate messages for user || dev |
|
| 592 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 593 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 594 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 595 | + * @return void |
|
| 596 | + */ |
|
| 597 | + public static function add_attention($msg = null, $file = null, $func = null, $line = null) |
|
| 598 | + { |
|
| 599 | + self::_add_notice('attention', $msg, $file, $func, $line); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * @param string $type whether the message is for a success or error notification |
|
| 606 | + * @param string $msg the message to display to users or developers |
|
| 607 | + * - adding a double pipe || (OR) creates separate messages for user || dev |
|
| 608 | + * @param string $file the file that the error occurred in - just use __FILE__ |
|
| 609 | + * @param string $func the function/method that the error occurred in - just use __FUNCTION__ |
|
| 610 | + * @param string $line the line number where the error occurred - just use __LINE__ |
|
| 611 | + * @return void |
|
| 612 | + */ |
|
| 613 | + private static function _add_notice($type = 'success', $msg = '', $file = '', $func = '', $line = '') |
|
| 614 | + { |
|
| 615 | + if (empty($msg)) { |
|
| 616 | + EE_Error::doing_it_wrong( |
|
| 617 | + 'EE_Error::add_' . $type . '()', |
|
| 618 | + sprintf( |
|
| 619 | + __('Notifications are not much use without a message! Please add a message to the EE_Error::add_%s() call made in %s on line %d', |
|
| 620 | + 'event_espresso'), |
|
| 621 | + $type, |
|
| 622 | + $file, |
|
| 623 | + $line |
|
| 624 | + ), |
|
| 625 | + EVENT_ESPRESSO_VERSION |
|
| 626 | + ); |
|
| 627 | + } |
|
| 628 | + if ($type === 'errors' && (empty($file) || empty($func) || empty($line))) { |
|
| 629 | + EE_Error::doing_it_wrong( |
|
| 630 | + 'EE_Error::add_error()', |
|
| 631 | + __('You need to provide the file name, function name, and line number that the error occurred on in order to better assist with debugging.', |
|
| 632 | + 'event_espresso'), |
|
| 633 | + EVENT_ESPRESSO_VERSION |
|
| 634 | + ); |
|
| 635 | + } |
|
| 636 | + // get separate user and developer messages if they exist |
|
| 637 | + $msg = explode('||', $msg); |
|
| 638 | + $user_msg = $msg[0]; |
|
| 639 | + $dev_msg = isset($msg[1]) ? $msg[1] : $msg[0]; |
|
| 640 | + /** |
|
| 641 | + * Do an action so other code can be triggered when a notice is created |
|
| 642 | + * |
|
| 643 | + * @param string $type can be 'errors', 'attention', or 'success' |
|
| 644 | + * @param string $user_msg message displayed to user when WP_DEBUG is off |
|
| 645 | + * @param string $user_msg message displayed to user when WP_DEBUG is on |
|
| 646 | + * @param string $file file where error was generated |
|
| 647 | + * @param string $func function where error was generated |
|
| 648 | + * @param string $line line where error was generated |
|
| 649 | + */ |
|
| 650 | + do_action('AHEE__EE_Error___add_notice', $type, $user_msg, $dev_msg, $file, $func, $line); |
|
| 651 | + $msg = WP_DEBUG ? $dev_msg : $user_msg; |
|
| 652 | + // add notice if message exists |
|
| 653 | + if (! empty($msg)) { |
|
| 654 | + // get error code |
|
| 655 | + $notice_code = EE_Error::generate_error_code($file, $func, $line); |
|
| 656 | + if (WP_DEBUG && $type === 'errors') { |
|
| 657 | + $msg .= '<br/><span class="tiny-text">' . $notice_code . '</span>'; |
|
| 658 | + } |
|
| 659 | + // add notice. Index by code if it's not blank |
|
| 660 | + if ($notice_code) { |
|
| 661 | + self::$_espresso_notices[$type][$notice_code] = $msg; |
|
| 662 | + } else { |
|
| 663 | + self::$_espresso_notices[$type][] = $msg; |
|
| 664 | + } |
|
| 665 | + add_action('wp_footer', array('EE_Error', 'enqueue_error_scripts'), 1); |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * in some case it may be necessary to overwrite the existing success messages |
|
| 672 | + * |
|
| 673 | + * @return void |
|
| 674 | + */ |
|
| 675 | + public static function overwrite_success() |
|
| 676 | + { |
|
| 677 | + self::$_espresso_notices['success'] = false; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * in some case it may be necessary to overwrite the existing attention messages |
|
| 684 | + * |
|
| 685 | + * @return void |
|
| 686 | + */ |
|
| 687 | + public static function overwrite_attention() |
|
| 688 | + { |
|
| 689 | + self::$_espresso_notices['attention'] = false; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * in some case it may be necessary to overwrite the existing error messages |
|
| 696 | + * |
|
| 697 | + * @return void |
|
| 698 | + */ |
|
| 699 | + public static function overwrite_errors() |
|
| 700 | + { |
|
| 701 | + self::$_espresso_notices['errors'] = false; |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * @return void |
|
| 708 | + */ |
|
| 709 | + public static function reset_notices() |
|
| 710 | + { |
|
| 711 | + self::$_espresso_notices['success'] = false; |
|
| 712 | + self::$_espresso_notices['attention'] = false; |
|
| 713 | + self::$_espresso_notices['errors'] = false; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * @return int |
|
| 720 | + */ |
|
| 721 | + public static function has_notices() |
|
| 722 | + { |
|
| 723 | + $has_notices = 0; |
|
| 724 | + // check for success messages |
|
| 725 | + $has_notices = self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success']) |
|
| 726 | + ? 3 |
|
| 727 | + : $has_notices; |
|
| 728 | + // check for attention messages |
|
| 729 | + $has_notices = self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention']) |
|
| 730 | + ? 2 |
|
| 731 | + : $has_notices; |
|
| 732 | + // check for error messages |
|
| 733 | + $has_notices = self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors']) |
|
| 734 | + ? 1 |
|
| 735 | + : $has_notices; |
|
| 736 | + return $has_notices; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * This simply returns non formatted error notices as they were sent into the EE_Error object. |
|
| 742 | + * |
|
| 743 | + * @since 4.9.0 |
|
| 744 | + * @return array |
|
| 745 | + */ |
|
| 746 | + public static function get_vanilla_notices() |
|
| 747 | + { |
|
| 748 | + return array( |
|
| 749 | + 'success' => isset(self::$_espresso_notices['success']) |
|
| 750 | + ? self::$_espresso_notices['success'] |
|
| 751 | + : array(), |
|
| 752 | + 'attention' => isset(self::$_espresso_notices['attention']) |
|
| 753 | + ? self::$_espresso_notices['attention'] |
|
| 754 | + : array(), |
|
| 755 | + 'errors' => isset(self::$_espresso_notices['errors']) |
|
| 756 | + ? self::$_espresso_notices['errors'] |
|
| 757 | + : array(), |
|
| 758 | + ); |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + |
|
| 762 | + /** |
|
| 763 | + * @return array |
|
| 764 | + * @throws InvalidArgumentException |
|
| 765 | + * @throws InvalidDataTypeException |
|
| 766 | + * @throws InvalidInterfaceException |
|
| 767 | + */ |
|
| 768 | + public static function getStoredNotices() |
|
| 769 | + { |
|
| 770 | + if ($user_id = get_current_user_id()) { |
|
| 771 | + // get notices for logged in user |
|
| 772 | + $notices = get_user_option(EE_Error::OPTIONS_KEY_NOTICES, $user_id); |
|
| 773 | + return is_array($notices) ? $notices : array(); |
|
| 774 | + } |
|
| 775 | + if (EE_Session::isLoadedAndActive()) { |
|
| 776 | + // get notices for user currently engaged in a session |
|
| 777 | + $session_data = EE_Session::instance()->get_session_data(EE_Error::OPTIONS_KEY_NOTICES); |
|
| 778 | + return is_array($session_data) ? $session_data : array(); |
|
| 779 | + } |
|
| 780 | + // get global notices and hope they apply to the current site visitor |
|
| 781 | + $notices = get_option(EE_Error::OPTIONS_KEY_NOTICES, array()); |
|
| 782 | + return is_array($notices) ? $notices : array(); |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * @param array $notices |
|
| 788 | + * @return bool |
|
| 789 | + * @throws InvalidArgumentException |
|
| 790 | + * @throws InvalidDataTypeException |
|
| 791 | + * @throws InvalidInterfaceException |
|
| 792 | + */ |
|
| 793 | + public static function storeNotices(array $notices) |
|
| 794 | + { |
|
| 795 | + if ($user_id = get_current_user_id()) { |
|
| 796 | + // store notices for logged in user |
|
| 797 | + return (bool) update_user_option( |
|
| 798 | + EE_Error::OPTIONS_KEY_NOTICES, |
|
| 799 | + $user_id, |
|
| 800 | + $notices |
|
| 801 | + ); |
|
| 802 | + } |
|
| 803 | + if (EE_Session::isLoadedAndActive()) { |
|
| 804 | + // store notices for user currently engaged in a session |
|
| 805 | + return EE_Session::instance()->set_session_data( |
|
| 806 | + array(EE_Error::OPTIONS_KEY_NOTICES => $notices) |
|
| 807 | + ); |
|
| 808 | + } |
|
| 809 | + // store global notices and hope they apply to the same site visitor on the next request |
|
| 810 | + return update_option(EE_Error::OPTIONS_KEY_NOTICES, $notices); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * @return bool|TRUE |
|
| 816 | + * @throws InvalidArgumentException |
|
| 817 | + * @throws InvalidDataTypeException |
|
| 818 | + * @throws InvalidInterfaceException |
|
| 819 | + */ |
|
| 820 | + public static function clearNotices() |
|
| 821 | + { |
|
| 822 | + if ($user_id = get_current_user_id()) { |
|
| 823 | + // clear notices for logged in user |
|
| 824 | + return (bool) update_user_option( |
|
| 825 | + EE_Error::OPTIONS_KEY_NOTICES, |
|
| 826 | + $user_id, |
|
| 827 | + array() |
|
| 828 | + ); |
|
| 829 | + } |
|
| 830 | + if (EE_Session::isLoadedAndActive()) { |
|
| 831 | + // clear notices for user currently engaged in a session |
|
| 832 | + return EE_Session::instance()->reset_data(EE_Error::OPTIONS_KEY_NOTICES); |
|
| 833 | + } |
|
| 834 | + // clear global notices and hope none belonged to some for some other site visitor |
|
| 835 | + return update_option(EE_Error::OPTIONS_KEY_NOTICES, array()); |
|
| 836 | + } |
|
| 837 | + |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * compile all error or success messages into one string |
|
| 841 | + * |
|
| 842 | + * @see EE_Error::get_raw_notices if you want the raw notices without any preparations made to them |
|
| 843 | + * @param boolean $format_output whether or not to format the messages for display in the WP admin |
|
| 844 | + * @param boolean $save_to_transient whether or not to save notices to the db for retrieval on next request |
|
| 845 | + * - ONLY do this just before redirecting |
|
| 846 | + * @param boolean $remove_empty whether or not to unset empty messages |
|
| 847 | + * @return array |
|
| 848 | + * @throws InvalidArgumentException |
|
| 849 | + * @throws InvalidDataTypeException |
|
| 850 | + * @throws InvalidInterfaceException |
|
| 851 | + */ |
|
| 852 | + public static function get_notices($format_output = true, $save_to_transient = false, $remove_empty = true) |
|
| 853 | + { |
|
| 854 | + $success_messages = ''; |
|
| 855 | + $attention_messages = ''; |
|
| 856 | + $error_messages = ''; |
|
| 857 | + // either save notices to the db |
|
| 858 | + if ($save_to_transient || isset($_REQUEST['activate-selected'])) { |
|
| 859 | + self::$_espresso_notices = array_merge( |
|
| 860 | + EE_Error::getStoredNotices(), |
|
| 861 | + self::$_espresso_notices |
|
| 862 | + ); |
|
| 863 | + EE_Error::storeNotices(self::$_espresso_notices); |
|
| 864 | + return array(); |
|
| 865 | + } |
|
| 866 | + $print_scripts = EE_Error::combineExistingAndNewNotices(); |
|
| 867 | + // check for success messages |
|
| 868 | + if (self::$_espresso_notices['success'] && ! empty(self::$_espresso_notices['success'])) { |
|
| 869 | + // combine messages |
|
| 870 | + $success_messages .= implode(self::$_espresso_notices['success'], '<br />'); |
|
| 871 | + $print_scripts = true; |
|
| 872 | + } |
|
| 873 | + // check for attention messages |
|
| 874 | + if (self::$_espresso_notices['attention'] && ! empty(self::$_espresso_notices['attention'])) { |
|
| 875 | + // combine messages |
|
| 876 | + $attention_messages .= implode(self::$_espresso_notices['attention'], '<br />'); |
|
| 877 | + $print_scripts = true; |
|
| 878 | + } |
|
| 879 | + // check for error messages |
|
| 880 | + if (self::$_espresso_notices['errors'] && ! empty(self::$_espresso_notices['errors'])) { |
|
| 881 | + $error_messages .= count(self::$_espresso_notices['errors']) > 1 |
|
| 882 | + ? __('The following errors have occurred:<br />', 'event_espresso') |
|
| 883 | + : __('An error has occurred:<br />', 'event_espresso'); |
|
| 884 | + // combine messages |
|
| 885 | + $error_messages .= implode(self::$_espresso_notices['errors'], '<br />'); |
|
| 886 | + $print_scripts = true; |
|
| 887 | + } |
|
| 888 | + if ($format_output) { |
|
| 889 | + $notices = EE_Error::formatNoticesOutput( |
|
| 890 | + $success_messages, |
|
| 891 | + $attention_messages, |
|
| 892 | + $error_messages |
|
| 893 | + ); |
|
| 894 | + } else { |
|
| 895 | + $notices = array( |
|
| 896 | + 'success' => $success_messages, |
|
| 897 | + 'attention' => $attention_messages, |
|
| 898 | + 'errors' => $error_messages, |
|
| 899 | + ); |
|
| 900 | + if ($remove_empty) { |
|
| 901 | + // remove empty notices |
|
| 902 | + foreach ($notices as $type => $notice) { |
|
| 903 | + if (empty($notice)) { |
|
| 904 | + unset($notices[$type]); |
|
| 905 | + } |
|
| 906 | + } |
|
| 907 | + } |
|
| 908 | + } |
|
| 909 | + if ($print_scripts) { |
|
| 910 | + self::_print_scripts(); |
|
| 911 | + } |
|
| 912 | + return $notices; |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * @return bool |
|
| 918 | + * @throws InvalidArgumentException |
|
| 919 | + * @throws InvalidDataTypeException |
|
| 920 | + * @throws InvalidInterfaceException |
|
| 921 | + */ |
|
| 922 | + private static function combineExistingAndNewNotices() |
|
| 923 | + { |
|
| 924 | + $print_scripts = false; |
|
| 925 | + // grab any notices that have been previously saved |
|
| 926 | + $notices = EE_Error::getStoredNotices(); |
|
| 927 | + if (! empty($notices)) { |
|
| 928 | + foreach ($notices as $type => $notice) { |
|
| 929 | + if (is_array($notice) && ! empty($notice)) { |
|
| 930 | + // make sure that existing notice type is an array |
|
| 931 | + self::$_espresso_notices[ $type ] = is_array(self::$_espresso_notices[ $type ]) |
|
| 932 | + && ! empty(self::$_espresso_notices[ $type ]) |
|
| 933 | + ? self::$_espresso_notices[ $type ] |
|
| 934 | + : array(); |
|
| 935 | + // add newly created notices to existing ones |
|
| 936 | + self::$_espresso_notices[ $type ] += $notice; |
|
| 937 | + $print_scripts = true; |
|
| 938 | + } |
|
| 939 | + } |
|
| 940 | + // now clear any stored notices |
|
| 941 | + EE_Error::clearNotices(); |
|
| 942 | + } |
|
| 943 | + return $print_scripts; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * @param string $success_messages |
|
| 949 | + * @param string $attention_messages |
|
| 950 | + * @param string $error_messages |
|
| 951 | + * @return string |
|
| 952 | + */ |
|
| 953 | + private static function formatNoticesOutput($success_messages, $attention_messages, $error_messages) |
|
| 954 | + { |
|
| 955 | + $notices = '<div id="espresso-notices">'; |
|
| 956 | + $close = is_admin() |
|
| 957 | + ? '' |
|
| 958 | + : '<a class="close-espresso-notice hide-if-no-js"><span class="dashicons dashicons-no"/></a>'; |
|
| 959 | + if ($success_messages !== '') { |
|
| 960 | + $css_id = is_admin() ? 'message' : 'espresso-notices-success'; |
|
| 961 | + $css_class = is_admin() ? 'updated fade' : 'success fade-away'; |
|
| 962 | + //showMessage( $success_messages ); |
|
| 963 | + $notices .= '<div id="' . $css_id . '" ' |
|
| 964 | + . 'class="espresso-notices ' . $css_class . '" ' |
|
| 965 | + . 'style="display:none;">' |
|
| 966 | + . '<p>' . $success_messages . '</p>' |
|
| 967 | + . $close |
|
| 968 | + . '</div>'; |
|
| 969 | + } |
|
| 970 | + if ($attention_messages !== '') { |
|
| 971 | + $css_id = is_admin() ? 'message' : 'espresso-notices-attention'; |
|
| 972 | + $css_class = is_admin() ? 'updated ee-notices-attention' : 'attention fade-away'; |
|
| 973 | + //showMessage( $error_messages, TRUE ); |
|
| 974 | + $notices .= '<div id="' . $css_id . '" ' |
|
| 975 | + . 'class="espresso-notices ' . $css_class . '" ' |
|
| 976 | + . 'style="display:none;">' |
|
| 977 | + . '<p>' . $attention_messages . '</p>' |
|
| 978 | + . $close |
|
| 979 | + . '</div>'; |
|
| 980 | + } |
|
| 981 | + if ($error_messages !== '') { |
|
| 982 | + $css_id = is_admin() ? 'message' : 'espresso-notices-error'; |
|
| 983 | + $css_class = is_admin() ? 'error' : 'error fade-away'; |
|
| 984 | + //showMessage( $error_messages, TRUE ); |
|
| 985 | + $notices .= '<div id="' . $css_id . '" ' |
|
| 986 | + . 'class="espresso-notices ' . $css_class . '" ' |
|
| 987 | + . 'style="display:none;">' |
|
| 988 | + . '<p>' . $error_messages . '</p>' |
|
| 989 | + . $close |
|
| 990 | + . '</div>'; |
|
| 991 | + } |
|
| 992 | + $notices .= '</div>'; |
|
| 993 | + return $notices; |
|
| 994 | + } |
|
| 995 | + |
|
| 996 | + |
|
| 997 | + |
|
| 998 | + /** |
|
| 999 | + * _print_scripts |
|
| 1000 | + * |
|
| 1001 | + * @param bool $force_print |
|
| 1002 | + * @return string |
|
| 1003 | + */ |
|
| 1004 | + private static function _print_scripts($force_print = false) |
|
| 1005 | + { |
|
| 1006 | + if (! $force_print && (did_action('admin_enqueue_scripts') || did_action('wp_enqueue_scripts'))) { |
|
| 1007 | + if (wp_script_is('ee_error_js', 'enqueued')) { |
|
| 1008 | + return ''; |
|
| 1009 | + } |
|
| 1010 | + if (wp_script_is('ee_error_js', 'registered')) { |
|
| 1011 | + wp_enqueue_style('espresso_default'); |
|
| 1012 | + wp_enqueue_style('espresso_custom_css'); |
|
| 1013 | + wp_enqueue_script('ee_error_js'); |
|
| 1014 | + wp_localize_script('ee_error_js', 'ee_settings', array('wp_debug' => WP_DEBUG)); |
|
| 1015 | + } |
|
| 1016 | + } else { |
|
| 1017 | + return ' |
|
| 1018 | 1018 | <script> |
| 1019 | 1019 | /* <![CDATA[ */ |
| 1020 | 1020 | const ee_settings = {"wp_debug":"' . WP_DEBUG . '"}; |
@@ -1024,223 +1024,223 @@ discard block |
||
| 1024 | 1024 | <script src="' . EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js' . '?ver=' . espresso_version() . '" type="text/javascript"></script> |
| 1025 | 1025 | <script src="' . EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js' . '?ver=' . espresso_version() . '" type="text/javascript"></script> |
| 1026 | 1026 | '; |
| 1027 | - } |
|
| 1028 | - return ''; |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - |
|
| 1032 | - |
|
| 1033 | - /** |
|
| 1034 | - * @return void |
|
| 1035 | - */ |
|
| 1036 | - public static function enqueue_error_scripts() |
|
| 1037 | - { |
|
| 1038 | - self::_print_scripts(); |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - |
|
| 1042 | - |
|
| 1043 | - /** |
|
| 1044 | - * create error code from filepath, function name, |
|
| 1045 | - * and line number where exception or error was thrown |
|
| 1046 | - * |
|
| 1047 | - * @param string $file |
|
| 1048 | - * @param string $func |
|
| 1049 | - * @param string $line |
|
| 1050 | - * @return string |
|
| 1051 | - */ |
|
| 1052 | - public static function generate_error_code($file = '', $func = '', $line = '') |
|
| 1053 | - { |
|
| 1054 | - $file = explode('.', basename($file)); |
|
| 1055 | - $error_code = ! empty($file[0]) ? $file[0] : ''; |
|
| 1056 | - $error_code .= ! empty($func) ? ' - ' . $func : ''; |
|
| 1057 | - $error_code .= ! empty($line) ? ' - ' . $line : ''; |
|
| 1058 | - return $error_code; |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - |
|
| 1062 | - |
|
| 1063 | - /** |
|
| 1064 | - * write exception details to log file |
|
| 1065 | - * Since 4.9.53.rc.006 this writes to the standard PHP log file, not EE's custom log file |
|
| 1066 | - * |
|
| 1067 | - * @param int $time |
|
| 1068 | - * @param array $ex |
|
| 1069 | - * @param bool $clear |
|
| 1070 | - * @return void |
|
| 1071 | - */ |
|
| 1072 | - public function write_to_error_log($time = 0, $ex = array(), $clear = false) |
|
| 1073 | - { |
|
| 1074 | - if (empty($ex)) { |
|
| 1075 | - return; |
|
| 1076 | - } |
|
| 1077 | - if (! $time) { |
|
| 1078 | - $time = time(); |
|
| 1079 | - } |
|
| 1080 | - $exception_log = '----------------------------------------------------------------------------------------' |
|
| 1081 | - . PHP_EOL; |
|
| 1082 | - $exception_log .= '[' . date('Y-m-d H:i:s', $time) . '] Exception Details' . PHP_EOL; |
|
| 1083 | - $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL; |
|
| 1084 | - $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL; |
|
| 1085 | - $exception_log .= 'File: ' . $ex['file'] . PHP_EOL; |
|
| 1086 | - $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL; |
|
| 1087 | - $exception_log .= 'Stack trace: ' . PHP_EOL; |
|
| 1088 | - $exception_log .= $ex['string'] . PHP_EOL; |
|
| 1089 | - $exception_log .= '----------------------------------------------------------------------------------------' |
|
| 1090 | - . PHP_EOL; |
|
| 1091 | - try { |
|
| 1092 | - error_log($exception_log); |
|
| 1093 | - } catch (EE_Error $e) { |
|
| 1094 | - EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', |
|
| 1095 | - 'event_espresso'), $e->getMessage())); |
|
| 1096 | - } |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - |
|
| 1100 | - |
|
| 1101 | - /** |
|
| 1102 | - * This is just a wrapper for the EEH_Debug_Tools::instance()->doing_it_wrong() method. |
|
| 1103 | - * doing_it_wrong() is used in those cases where a normal PHP error won't get thrown, |
|
| 1104 | - * but the code execution is done in a manner that could lead to unexpected results |
|
| 1105 | - * (i.e. running to early, or too late in WP or EE loading process). |
|
| 1106 | - * A good test for knowing whether to use this method is: |
|
| 1107 | - * 1. Is there going to be a PHP error if something isn't setup/used correctly? |
|
| 1108 | - * Yes -> use EE_Error::add_error() or throw new EE_Error() |
|
| 1109 | - * 2. If this is loaded before something else, it won't break anything, |
|
| 1110 | - * but just wont' do what its supposed to do? Yes -> use EE_Error::doing_it_wrong() |
|
| 1111 | - * |
|
| 1112 | - * @uses constant WP_DEBUG test if wp_debug is on or not |
|
| 1113 | - * @param string $function The function that was called |
|
| 1114 | - * @param string $message A message explaining what has been done incorrectly |
|
| 1115 | - * @param string $version The version of Event Espresso where the error was added |
|
| 1116 | - * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
|
| 1117 | - * for a deprecated function. This allows deprecation to occur during one version, |
|
| 1118 | - * but not have any notices appear until a later version. This allows developers |
|
| 1119 | - * extra time to update their code before notices appear. |
|
| 1120 | - * @param int $error_type |
|
| 1121 | - */ |
|
| 1122 | - public static function doing_it_wrong( |
|
| 1123 | - $function, |
|
| 1124 | - $message, |
|
| 1125 | - $version, |
|
| 1126 | - $applies_when = '', |
|
| 1127 | - $error_type = null |
|
| 1128 | - ) { |
|
| 1129 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 1130 | - EEH_Debug_Tools::instance()->doing_it_wrong($function, $message, $version, $applies_when, $error_type); |
|
| 1131 | - } |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - |
|
| 1135 | - |
|
| 1136 | - /** |
|
| 1137 | - * Like get_notices, but returns an array of all the notices of the given type. |
|
| 1138 | - * |
|
| 1139 | - * @return array { |
|
| 1140 | - * @type array $success all the success messages |
|
| 1141 | - * @type array $errors all the error messages |
|
| 1142 | - * @type array $attention all the attention messages |
|
| 1143 | - * } |
|
| 1144 | - */ |
|
| 1145 | - public static function get_raw_notices() |
|
| 1146 | - { |
|
| 1147 | - return self::$_espresso_notices; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - |
|
| 1151 | - |
|
| 1152 | - /** |
|
| 1153 | - * @deprecated 4.9.27 |
|
| 1154 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 1155 | - * @param string $pan_message the message to be stored persistently until dismissed |
|
| 1156 | - * @param bool $force_update allows one to enforce the reappearance of a persistent message. |
|
| 1157 | - * @return void |
|
| 1158 | - * @throws InvalidDataTypeException |
|
| 1159 | - */ |
|
| 1160 | - public static function add_persistent_admin_notice($pan_name = '', $pan_message, $force_update = false) |
|
| 1161 | - { |
|
| 1162 | - new PersistentAdminNotice( |
|
| 1163 | - $pan_name, |
|
| 1164 | - $pan_message, |
|
| 1165 | - $force_update |
|
| 1166 | - ); |
|
| 1167 | - EE_Error::doing_it_wrong( |
|
| 1168 | - __METHOD__, |
|
| 1169 | - sprintf( |
|
| 1170 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1171 | - '\EventEspresso\core\domain\entities\notifications\PersistentAdminNotice' |
|
| 1172 | - ), |
|
| 1173 | - '4.9.27' |
|
| 1174 | - ); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - |
|
| 1178 | - |
|
| 1179 | - /** |
|
| 1180 | - * @deprecated 4.9.27 |
|
| 1181 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 1182 | - * @param bool $purge |
|
| 1183 | - * @param bool $return |
|
| 1184 | - * @throws DomainException |
|
| 1185 | - * @throws InvalidInterfaceException |
|
| 1186 | - * @throws InvalidDataTypeException |
|
| 1187 | - * @throws ServiceNotFoundException |
|
| 1188 | - * @throws InvalidArgumentException |
|
| 1189 | - */ |
|
| 1190 | - public static function dismiss_persistent_admin_notice($pan_name = '', $purge = false, $return = false) |
|
| 1191 | - { |
|
| 1192 | - /** @var PersistentAdminNoticeManager $persistent_admin_notice_manager */ |
|
| 1193 | - $persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 1194 | - 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1195 | - ); |
|
| 1196 | - $persistent_admin_notice_manager->dismissNotice($pan_name, $purge, $return); |
|
| 1197 | - EE_Error::doing_it_wrong( |
|
| 1198 | - __METHOD__, |
|
| 1199 | - sprintf( |
|
| 1200 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1201 | - '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1202 | - ), |
|
| 1203 | - '4.9.27' |
|
| 1204 | - ); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - |
|
| 1208 | - |
|
| 1209 | - /** |
|
| 1210 | - * @deprecated 4.9.27 |
|
| 1211 | - * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 1212 | - * @param string $pan_message the message to be stored persistently until dismissed |
|
| 1213 | - * @param string $return_url URL to go back to after nag notice is dismissed |
|
| 1214 | - */ |
|
| 1215 | - public static function display_persistent_admin_notices($pan_name = '', $pan_message = '', $return_url = '') |
|
| 1216 | - { |
|
| 1217 | - EE_Error::doing_it_wrong( |
|
| 1218 | - __METHOD__, |
|
| 1219 | - sprintf( |
|
| 1220 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1221 | - '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1222 | - ), |
|
| 1223 | - '4.9.27' |
|
| 1224 | - ); |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - |
|
| 1228 | - |
|
| 1229 | - /** |
|
| 1230 | - * @deprecated 4.9.27 |
|
| 1231 | - * @param string $return_url |
|
| 1232 | - */ |
|
| 1233 | - public static function get_persistent_admin_notices($return_url = '') |
|
| 1234 | - { |
|
| 1235 | - EE_Error::doing_it_wrong( |
|
| 1236 | - __METHOD__, |
|
| 1237 | - sprintf( |
|
| 1238 | - __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1239 | - '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1240 | - ), |
|
| 1241 | - '4.9.27' |
|
| 1242 | - ); |
|
| 1243 | - } |
|
| 1027 | + } |
|
| 1028 | + return ''; |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + |
|
| 1032 | + |
|
| 1033 | + /** |
|
| 1034 | + * @return void |
|
| 1035 | + */ |
|
| 1036 | + public static function enqueue_error_scripts() |
|
| 1037 | + { |
|
| 1038 | + self::_print_scripts(); |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + |
|
| 1042 | + |
|
| 1043 | + /** |
|
| 1044 | + * create error code from filepath, function name, |
|
| 1045 | + * and line number where exception or error was thrown |
|
| 1046 | + * |
|
| 1047 | + * @param string $file |
|
| 1048 | + * @param string $func |
|
| 1049 | + * @param string $line |
|
| 1050 | + * @return string |
|
| 1051 | + */ |
|
| 1052 | + public static function generate_error_code($file = '', $func = '', $line = '') |
|
| 1053 | + { |
|
| 1054 | + $file = explode('.', basename($file)); |
|
| 1055 | + $error_code = ! empty($file[0]) ? $file[0] : ''; |
|
| 1056 | + $error_code .= ! empty($func) ? ' - ' . $func : ''; |
|
| 1057 | + $error_code .= ! empty($line) ? ' - ' . $line : ''; |
|
| 1058 | + return $error_code; |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + |
|
| 1062 | + |
|
| 1063 | + /** |
|
| 1064 | + * write exception details to log file |
|
| 1065 | + * Since 4.9.53.rc.006 this writes to the standard PHP log file, not EE's custom log file |
|
| 1066 | + * |
|
| 1067 | + * @param int $time |
|
| 1068 | + * @param array $ex |
|
| 1069 | + * @param bool $clear |
|
| 1070 | + * @return void |
|
| 1071 | + */ |
|
| 1072 | + public function write_to_error_log($time = 0, $ex = array(), $clear = false) |
|
| 1073 | + { |
|
| 1074 | + if (empty($ex)) { |
|
| 1075 | + return; |
|
| 1076 | + } |
|
| 1077 | + if (! $time) { |
|
| 1078 | + $time = time(); |
|
| 1079 | + } |
|
| 1080 | + $exception_log = '----------------------------------------------------------------------------------------' |
|
| 1081 | + . PHP_EOL; |
|
| 1082 | + $exception_log .= '[' . date('Y-m-d H:i:s', $time) . '] Exception Details' . PHP_EOL; |
|
| 1083 | + $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL; |
|
| 1084 | + $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL; |
|
| 1085 | + $exception_log .= 'File: ' . $ex['file'] . PHP_EOL; |
|
| 1086 | + $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL; |
|
| 1087 | + $exception_log .= 'Stack trace: ' . PHP_EOL; |
|
| 1088 | + $exception_log .= $ex['string'] . PHP_EOL; |
|
| 1089 | + $exception_log .= '----------------------------------------------------------------------------------------' |
|
| 1090 | + . PHP_EOL; |
|
| 1091 | + try { |
|
| 1092 | + error_log($exception_log); |
|
| 1093 | + } catch (EE_Error $e) { |
|
| 1094 | + EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', |
|
| 1095 | + 'event_espresso'), $e->getMessage())); |
|
| 1096 | + } |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + |
|
| 1100 | + |
|
| 1101 | + /** |
|
| 1102 | + * This is just a wrapper for the EEH_Debug_Tools::instance()->doing_it_wrong() method. |
|
| 1103 | + * doing_it_wrong() is used in those cases where a normal PHP error won't get thrown, |
|
| 1104 | + * but the code execution is done in a manner that could lead to unexpected results |
|
| 1105 | + * (i.e. running to early, or too late in WP or EE loading process). |
|
| 1106 | + * A good test for knowing whether to use this method is: |
|
| 1107 | + * 1. Is there going to be a PHP error if something isn't setup/used correctly? |
|
| 1108 | + * Yes -> use EE_Error::add_error() or throw new EE_Error() |
|
| 1109 | + * 2. If this is loaded before something else, it won't break anything, |
|
| 1110 | + * but just wont' do what its supposed to do? Yes -> use EE_Error::doing_it_wrong() |
|
| 1111 | + * |
|
| 1112 | + * @uses constant WP_DEBUG test if wp_debug is on or not |
|
| 1113 | + * @param string $function The function that was called |
|
| 1114 | + * @param string $message A message explaining what has been done incorrectly |
|
| 1115 | + * @param string $version The version of Event Espresso where the error was added |
|
| 1116 | + * @param string $applies_when a version string for when you want the doing_it_wrong notice to begin appearing |
|
| 1117 | + * for a deprecated function. This allows deprecation to occur during one version, |
|
| 1118 | + * but not have any notices appear until a later version. This allows developers |
|
| 1119 | + * extra time to update their code before notices appear. |
|
| 1120 | + * @param int $error_type |
|
| 1121 | + */ |
|
| 1122 | + public static function doing_it_wrong( |
|
| 1123 | + $function, |
|
| 1124 | + $message, |
|
| 1125 | + $version, |
|
| 1126 | + $applies_when = '', |
|
| 1127 | + $error_type = null |
|
| 1128 | + ) { |
|
| 1129 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
| 1130 | + EEH_Debug_Tools::instance()->doing_it_wrong($function, $message, $version, $applies_when, $error_type); |
|
| 1131 | + } |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + |
|
| 1135 | + |
|
| 1136 | + /** |
|
| 1137 | + * Like get_notices, but returns an array of all the notices of the given type. |
|
| 1138 | + * |
|
| 1139 | + * @return array { |
|
| 1140 | + * @type array $success all the success messages |
|
| 1141 | + * @type array $errors all the error messages |
|
| 1142 | + * @type array $attention all the attention messages |
|
| 1143 | + * } |
|
| 1144 | + */ |
|
| 1145 | + public static function get_raw_notices() |
|
| 1146 | + { |
|
| 1147 | + return self::$_espresso_notices; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + |
|
| 1151 | + |
|
| 1152 | + /** |
|
| 1153 | + * @deprecated 4.9.27 |
|
| 1154 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 1155 | + * @param string $pan_message the message to be stored persistently until dismissed |
|
| 1156 | + * @param bool $force_update allows one to enforce the reappearance of a persistent message. |
|
| 1157 | + * @return void |
|
| 1158 | + * @throws InvalidDataTypeException |
|
| 1159 | + */ |
|
| 1160 | + public static function add_persistent_admin_notice($pan_name = '', $pan_message, $force_update = false) |
|
| 1161 | + { |
|
| 1162 | + new PersistentAdminNotice( |
|
| 1163 | + $pan_name, |
|
| 1164 | + $pan_message, |
|
| 1165 | + $force_update |
|
| 1166 | + ); |
|
| 1167 | + EE_Error::doing_it_wrong( |
|
| 1168 | + __METHOD__, |
|
| 1169 | + sprintf( |
|
| 1170 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1171 | + '\EventEspresso\core\domain\entities\notifications\PersistentAdminNotice' |
|
| 1172 | + ), |
|
| 1173 | + '4.9.27' |
|
| 1174 | + ); |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + |
|
| 1178 | + |
|
| 1179 | + /** |
|
| 1180 | + * @deprecated 4.9.27 |
|
| 1181 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be dismissed |
|
| 1182 | + * @param bool $purge |
|
| 1183 | + * @param bool $return |
|
| 1184 | + * @throws DomainException |
|
| 1185 | + * @throws InvalidInterfaceException |
|
| 1186 | + * @throws InvalidDataTypeException |
|
| 1187 | + * @throws ServiceNotFoundException |
|
| 1188 | + * @throws InvalidArgumentException |
|
| 1189 | + */ |
|
| 1190 | + public static function dismiss_persistent_admin_notice($pan_name = '', $purge = false, $return = false) |
|
| 1191 | + { |
|
| 1192 | + /** @var PersistentAdminNoticeManager $persistent_admin_notice_manager */ |
|
| 1193 | + $persistent_admin_notice_manager = LoaderFactory::getLoader()->getShared( |
|
| 1194 | + 'EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1195 | + ); |
|
| 1196 | + $persistent_admin_notice_manager->dismissNotice($pan_name, $purge, $return); |
|
| 1197 | + EE_Error::doing_it_wrong( |
|
| 1198 | + __METHOD__, |
|
| 1199 | + sprintf( |
|
| 1200 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1201 | + '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1202 | + ), |
|
| 1203 | + '4.9.27' |
|
| 1204 | + ); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + |
|
| 1208 | + |
|
| 1209 | + /** |
|
| 1210 | + * @deprecated 4.9.27 |
|
| 1211 | + * @param string $pan_name the name, or key of the Persistent Admin Notice to be stored |
|
| 1212 | + * @param string $pan_message the message to be stored persistently until dismissed |
|
| 1213 | + * @param string $return_url URL to go back to after nag notice is dismissed |
|
| 1214 | + */ |
|
| 1215 | + public static function display_persistent_admin_notices($pan_name = '', $pan_message = '', $return_url = '') |
|
| 1216 | + { |
|
| 1217 | + EE_Error::doing_it_wrong( |
|
| 1218 | + __METHOD__, |
|
| 1219 | + sprintf( |
|
| 1220 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1221 | + '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1222 | + ), |
|
| 1223 | + '4.9.27' |
|
| 1224 | + ); |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + |
|
| 1228 | + |
|
| 1229 | + /** |
|
| 1230 | + * @deprecated 4.9.27 |
|
| 1231 | + * @param string $return_url |
|
| 1232 | + */ |
|
| 1233 | + public static function get_persistent_admin_notices($return_url = '') |
|
| 1234 | + { |
|
| 1235 | + EE_Error::doing_it_wrong( |
|
| 1236 | + __METHOD__, |
|
| 1237 | + sprintf( |
|
| 1238 | + __('Usage is deprecated. Use "%1$s" instead.', 'event_espresso'), |
|
| 1239 | + '\EventEspresso\core\services\notifications\PersistentAdminNoticeManager' |
|
| 1240 | + ), |
|
| 1241 | + '4.9.27' |
|
| 1242 | + ); |
|
| 1243 | + } |
|
| 1244 | 1244 | |
| 1245 | 1245 | |
| 1246 | 1246 | |
@@ -1255,27 +1255,27 @@ discard block |
||
| 1255 | 1255 | */ |
| 1256 | 1256 | function espresso_error_enqueue_scripts() |
| 1257 | 1257 | { |
| 1258 | - // js for error handling |
|
| 1259 | - wp_register_script( |
|
| 1260 | - 'espresso_core', |
|
| 1261 | - EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1262 | - array('jquery'), |
|
| 1263 | - EVENT_ESPRESSO_VERSION, |
|
| 1264 | - false |
|
| 1265 | - ); |
|
| 1266 | - wp_register_script( |
|
| 1267 | - 'ee_error_js', |
|
| 1268 | - EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1269 | - array('espresso_core'), |
|
| 1270 | - EVENT_ESPRESSO_VERSION, |
|
| 1271 | - false |
|
| 1272 | - ); |
|
| 1258 | + // js for error handling |
|
| 1259 | + wp_register_script( |
|
| 1260 | + 'espresso_core', |
|
| 1261 | + EE_GLOBAL_ASSETS_URL . 'scripts/espresso_core.js', |
|
| 1262 | + array('jquery'), |
|
| 1263 | + EVENT_ESPRESSO_VERSION, |
|
| 1264 | + false |
|
| 1265 | + ); |
|
| 1266 | + wp_register_script( |
|
| 1267 | + 'ee_error_js', |
|
| 1268 | + EE_GLOBAL_ASSETS_URL . 'scripts/EE_Error.js', |
|
| 1269 | + array('espresso_core'), |
|
| 1270 | + EVENT_ESPRESSO_VERSION, |
|
| 1271 | + false |
|
| 1272 | + ); |
|
| 1273 | 1273 | } |
| 1274 | 1274 | |
| 1275 | 1275 | if (is_admin()) { |
| 1276 | - add_action('admin_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1276 | + add_action('admin_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1277 | 1277 | } else { |
| 1278 | - add_action('wp_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1278 | + add_action('wp_enqueue_scripts', 'espresso_error_enqueue_scripts', 2); |
|
| 1279 | 1279 | } |
| 1280 | 1280 | |
| 1281 | 1281 | |