@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | * set session data |
| 434 | 434 | * @access public |
| 435 | 435 | * @param array $data |
| 436 | - * @return TRUE on success, FALSE on fail |
|
| 436 | + * @return boolean on success, FALSE on fail |
|
| 437 | 437 | */ |
| 438 | 438 | public function set_session_data( $data ) { |
| 439 | 439 | |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | /** |
| 462 | 462 | * @initiate session |
| 463 | 463 | * @access private |
| 464 | - * @return TRUE on success, FALSE on fail |
|
| 464 | + * @return boolean on success, FALSE on fail |
|
| 465 | 465 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
| 466 | 466 | * @throws \EE_Error |
| 467 | 467 | */ |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | * @update session data prior to saving to the db |
| 682 | 682 | * @access public |
| 683 | 683 | * @param bool $new_session |
| 684 | - * @return TRUE on success, FALSE on fail |
|
| 684 | + * @return boolean on success, FALSE on fail |
|
| 685 | 685 | * @throws \EE_Error |
| 686 | 686 | */ |
| 687 | 687 | public function update( $new_session = FALSE ) { |
@@ -787,7 +787,7 @@ discard block |
||
| 787 | 787 | * _save_session_to_db |
| 788 | 788 | * |
| 789 | 789 | * @access public |
| 790 | - * @return string |
|
| 790 | + * @return boolean |
|
| 791 | 791 | * @throws \EE_Error |
| 792 | 792 | */ |
| 793 | 793 | private function _save_session_to_db() { |
@@ -959,7 +959,7 @@ discard block |
||
| 959 | 959 | * @access public |
| 960 | 960 | * @param array $data_to_reset |
| 961 | 961 | * @param bool $show_all_notices |
| 962 | - * @return TRUE on success, FALSE on fail |
|
| 962 | + * @return boolean on success, FALSE on fail |
|
| 963 | 963 | */ |
| 964 | 964 | public function reset_data( $data_to_reset = array(), $show_all_notices = FALSE ) { |
| 965 | 965 | // if $data_to_reset is not in an array, then put it in one |
@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php use EventEspresso\core\exceptions\InvalidSessionDataException; |
| 2 | 2 | use EventEspresso\core\services\cache\CacheStorageInterface; |
| 3 | 3 | |
| 4 | -if (!defined( 'EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed');} |
|
| 4 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) {exit('No direct script access allowed'); } |
|
| 5 | 5 | /** |
| 6 | 6 | * |
| 7 | 7 | * EE_Session class |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * array for defining default session vars |
| 107 | 107 | * @var array |
| 108 | 108 | */ |
| 109 | - private $_default_session_vars = array ( |
|
| 109 | + private $_default_session_vars = array( |
|
| 110 | 110 | 'id' => null, |
| 111 | 111 | 'user_id' => null, |
| 112 | 112 | 'ip_address' => null, |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | // check if class object is instantiated |
| 135 | 135 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
| 136 | 136 | // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
| 137 | - if ( ! self::$_instance instanceof EE_Session && apply_filters( 'FHEE_load_EE_Session', true ) ) { |
|
| 138 | - self::$_instance = new self($cache_storage, $encryption ); |
|
| 137 | + if ( ! self::$_instance instanceof EE_Session && apply_filters('FHEE_load_EE_Session', true)) { |
|
| 138 | + self::$_instance = new self($cache_storage, $encryption); |
|
| 139 | 139 | } |
| 140 | 140 | return self::$_instance; |
| 141 | 141 | } |
@@ -150,15 +150,15 @@ discard block |
||
| 150 | 150 | * @throws \EE_Error |
| 151 | 151 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
| 152 | 152 | */ |
| 153 | - protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null ) { |
|
| 153 | + protected function __construct(CacheStorageInterface $cache_storage, EE_Encryption $encryption = null) { |
|
| 154 | 154 | |
| 155 | 155 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
| 156 | - if ( ! apply_filters( 'FHEE_load_EE_Session', true ) ) { |
|
| 156 | + if ( ! apply_filters('FHEE_load_EE_Session', true)) { |
|
| 157 | 157 | return; |
| 158 | 158 | } |
| 159 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
|
| 160 | - if ( ! defined( 'ESPRESSO_SESSION' ) ) { |
|
| 161 | - define( 'ESPRESSO_SESSION', true ); |
|
| 159 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 160 | + if ( ! defined('ESPRESSO_SESSION')) { |
|
| 161 | + define('ESPRESSO_SESSION', true); |
|
| 162 | 162 | } |
| 163 | 163 | // default session lifespan in seconds |
| 164 | 164 | $this->_lifespan = apply_filters( |
@@ -172,12 +172,12 @@ discard block |
||
| 172 | 172 | * } |
| 173 | 173 | */ |
| 174 | 174 | // retrieve session options from db |
| 175 | - $session_settings = (array) get_option( 'ee_session_settings', array() ); |
|
| 176 | - if ( ! empty( $session_settings )) { |
|
| 175 | + $session_settings = (array) get_option('ee_session_settings', array()); |
|
| 176 | + if ( ! empty($session_settings)) { |
|
| 177 | 177 | // cycle though existing session options |
| 178 | - foreach ( $session_settings as $var_name => $session_setting ) { |
|
| 178 | + foreach ($session_settings as $var_name => $session_setting) { |
|
| 179 | 179 | // set values for class properties |
| 180 | - $var_name = '_' . $var_name; |
|
| 180 | + $var_name = '_'.$var_name; |
|
| 181 | 181 | $this->{$var_name} = $session_setting; |
| 182 | 182 | } |
| 183 | 183 | } |
@@ -188,15 +188,15 @@ discard block |
||
| 188 | 188 | // encrypt data via: $this->encryption->encrypt(); |
| 189 | 189 | $this->encryption = $encryption; |
| 190 | 190 | // filter hook allows outside functions/classes/plugins to change default empty cart |
| 191 | - $extra_default_session_vars = apply_filters( 'FHEE__EE_Session__construct__extra_default_session_vars', array() ); |
|
| 192 | - array_merge( $this->_default_session_vars, $extra_default_session_vars ); |
|
| 191 | + $extra_default_session_vars = apply_filters('FHEE__EE_Session__construct__extra_default_session_vars', array()); |
|
| 192 | + array_merge($this->_default_session_vars, $extra_default_session_vars); |
|
| 193 | 193 | // apply default session vars |
| 194 | 194 | $this->_set_defaults(); |
| 195 | 195 | add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
| 196 | 196 | // check request for 'clear_session' param |
| 197 | - add_action( 'AHEE__EE_Request_Handler__construct__complete', array( $this, 'wp_loaded' )); |
|
| 197 | + add_action('AHEE__EE_Request_Handler__construct__complete', array($this, 'wp_loaded')); |
|
| 198 | 198 | // once everything is all said and done, |
| 199 | - add_action( 'shutdown', array( $this, 'update' ), 100 ); |
|
| 199 | + add_action('shutdown', array($this, 'update'), 100); |
|
| 200 | 200 | $this->configure_garbage_collection_filters(); |
| 201 | 201 | } |
| 202 | 202 | |
@@ -284,11 +284,11 @@ discard block |
||
| 284 | 284 | */ |
| 285 | 285 | private function _set_defaults() { |
| 286 | 286 | // set some defaults |
| 287 | - foreach ( $this->_default_session_vars as $key => $default_var ) { |
|
| 288 | - if ( is_array( $default_var )) { |
|
| 289 | - $this->_session_data[ $key ] = array(); |
|
| 287 | + foreach ($this->_default_session_vars as $key => $default_var) { |
|
| 288 | + if (is_array($default_var)) { |
|
| 289 | + $this->_session_data[$key] = array(); |
|
| 290 | 290 | } else { |
| 291 | - $this->_session_data[ $key ] = ''; |
|
| 291 | + $this->_session_data[$key] = ''; |
|
| 292 | 292 | } |
| 293 | 293 | } |
| 294 | 294 | } |
@@ -343,7 +343,7 @@ discard block |
||
| 343 | 343 | * @param \EE_Checkout $checkout |
| 344 | 344 | * @return bool |
| 345 | 345 | */ |
| 346 | - public function set_checkout( EE_Checkout $checkout ) { |
|
| 346 | + public function set_checkout(EE_Checkout $checkout) { |
|
| 347 | 347 | $this->_session_data['checkout'] = $checkout; |
| 348 | 348 | return TRUE; |
| 349 | 349 | } |
@@ -376,9 +376,9 @@ discard block |
||
| 376 | 376 | * @return bool |
| 377 | 377 | * @throws \EE_Error |
| 378 | 378 | */ |
| 379 | - public function set_transaction( EE_Transaction $transaction ) { |
|
| 379 | + public function set_transaction(EE_Transaction $transaction) { |
|
| 380 | 380 | // first remove the session from the transaction before we save the transaction in the session |
| 381 | - $transaction->set_txn_session_data( NULL ); |
|
| 381 | + $transaction->set_txn_session_data(NULL); |
|
| 382 | 382 | $this->_session_data['transaction'] = $transaction; |
| 383 | 383 | return TRUE; |
| 384 | 384 | } |
@@ -414,15 +414,15 @@ discard block |
||
| 414 | 414 | * @param bool $reset_cache |
| 415 | 415 | * @return array |
| 416 | 416 | */ |
| 417 | - public function get_session_data( $key = NULL, $reset_cache = FALSE ) { |
|
| 418 | - if ( $reset_cache ) { |
|
| 417 | + public function get_session_data($key = NULL, $reset_cache = FALSE) { |
|
| 418 | + if ($reset_cache) { |
|
| 419 | 419 | $this->reset_cart(); |
| 420 | 420 | $this->reset_checkout(); |
| 421 | 421 | $this->reset_transaction(); |
| 422 | 422 | } |
| 423 | - if ( ! empty( $key )) { |
|
| 424 | - return isset( $this->_session_data[ $key ] ) ? $this->_session_data[ $key ] : NULL; |
|
| 425 | - } else { |
|
| 423 | + if ( ! empty($key)) { |
|
| 424 | + return isset($this->_session_data[$key]) ? $this->_session_data[$key] : NULL; |
|
| 425 | + } else { |
|
| 426 | 426 | return $this->_session_data; |
| 427 | 427 | } |
| 428 | 428 | } |
@@ -435,20 +435,20 @@ discard block |
||
| 435 | 435 | * @param array $data |
| 436 | 436 | * @return TRUE on success, FALSE on fail |
| 437 | 437 | */ |
| 438 | - public function set_session_data( $data ) { |
|
| 438 | + public function set_session_data($data) { |
|
| 439 | 439 | |
| 440 | 440 | // nothing ??? bad data ??? go home! |
| 441 | - if ( empty( $data ) || ! is_array( $data )) { |
|
| 442 | - EE_Error::add_error( __( 'No session data or invalid session data was provided.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 441 | + if (empty($data) || ! is_array($data)) { |
|
| 442 | + EE_Error::add_error(__('No session data or invalid session data was provided.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 443 | 443 | return FALSE; |
| 444 | 444 | } |
| 445 | 445 | |
| 446 | - foreach ( $data as $key =>$value ) { |
|
| 447 | - if ( isset( $this->_default_session_vars[ $key ] )) { |
|
| 448 | - EE_Error::add_error( sprintf( __( 'Sorry! %s is a default session datum and can not be reset.', 'event_espresso' ), $key ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 446 | + foreach ($data as $key =>$value) { |
|
| 447 | + if (isset($this->_default_session_vars[$key])) { |
|
| 448 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', 'event_espresso'), $key), __FILE__, __FUNCTION__, __LINE__); |
|
| 449 | 449 | return FALSE; |
| 450 | 450 | } else { |
| 451 | - $this->_session_data[ $key ] = $value; |
|
| 451 | + $this->_session_data[$key] = $value; |
|
| 452 | 452 | } |
| 453 | 453 | } |
| 454 | 454 | |
@@ -466,9 +466,9 @@ discard block |
||
| 466 | 466 | * @throws \EE_Error |
| 467 | 467 | */ |
| 468 | 468 | private function _espresso_session() { |
| 469 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, '' ); |
|
| 469 | + do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
| 470 | 470 | // check that session has started |
| 471 | - if ( session_id() === '' ) { |
|
| 471 | + if (session_id() === '') { |
|
| 472 | 472 | //starts a new session if one doesn't already exist, or re-initiates an existing one |
| 473 | 473 | session_start(); |
| 474 | 474 | } |
@@ -477,39 +477,39 @@ discard block |
||
| 477 | 477 | // and the visitors IP |
| 478 | 478 | $this->_ip_address = $this->_visitor_ip(); |
| 479 | 479 | // set the "user agent" |
| 480 | - $this->_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr( $_SERVER['HTTP_USER_AGENT'] ) : FALSE; |
|
| 480 | + $this->_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr($_SERVER['HTTP_USER_AGENT']) : FALSE; |
|
| 481 | 481 | // now let's retrieve what's in the db |
| 482 | 482 | $session_data = $this->_retrieve_session_data(); |
| 483 | - if (! empty($session_data)) { |
|
| 483 | + if ( ! empty($session_data)) { |
|
| 484 | 484 | // get the current time in UTC |
| 485 | - $this->_time = isset( $this->_time ) ? $this->_time : time(); |
|
| 485 | + $this->_time = isset($this->_time) ? $this->_time : time(); |
|
| 486 | 486 | // and reset the session expiration |
| 487 | - $this->_expiration = isset( $session_data['expiration'] ) |
|
| 487 | + $this->_expiration = isset($session_data['expiration']) |
|
| 488 | 488 | ? $session_data['expiration'] |
| 489 | 489 | : $this->_time + $this->_lifespan; |
| 490 | 490 | } else { |
| 491 | 491 | // set initial site access time and the session expiration |
| 492 | 492 | $this->_set_init_access_and_expiration(); |
| 493 | 493 | // set referer |
| 494 | - $this->_session_data[ 'pages_visited' ][ $this->_session_data['init_access'] ] = isset( $_SERVER['HTTP_REFERER'] ) |
|
| 495 | - ? esc_attr( $_SERVER['HTTP_REFERER'] ) |
|
| 494 | + $this->_session_data['pages_visited'][$this->_session_data['init_access']] = isset($_SERVER['HTTP_REFERER']) |
|
| 495 | + ? esc_attr($_SERVER['HTTP_REFERER']) |
|
| 496 | 496 | : ''; |
| 497 | 497 | // no previous session = go back and create one (on top of the data above) |
| 498 | 498 | return FALSE; |
| 499 | 499 | } |
| 500 | 500 | // now the user agent |
| 501 | - if ( $session_data['user_agent'] !== $this->_user_agent ) { |
|
| 501 | + if ($session_data['user_agent'] !== $this->_user_agent) { |
|
| 502 | 502 | return FALSE; |
| 503 | 503 | } |
| 504 | 504 | // wait a minute... how old are you? |
| 505 | - if ( $this->_time > $this->_expiration ) { |
|
| 505 | + if ($this->_time > $this->_expiration) { |
|
| 506 | 506 | // yer too old fer me! |
| 507 | 507 | $this->_expired = true; |
| 508 | 508 | // wipe out everything that isn't a default session datum |
| 509 | - $this->clear_session( __CLASS__, __FUNCTION__ ); |
|
| 509 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 510 | 510 | } |
| 511 | 511 | // make event espresso session data available to plugin |
| 512 | - $this->_session_data = array_merge( $this->_session_data, $session_data ); |
|
| 512 | + $this->_session_data = array_merge($this->_session_data, $session_data); |
|
| 513 | 513 | return TRUE; |
| 514 | 514 | |
| 515 | 515 | } |
@@ -525,7 +525,7 @@ discard block |
||
| 525 | 525 | */ |
| 526 | 526 | protected function _retrieve_session_data() |
| 527 | 527 | { |
| 528 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
| 528 | + $ssn_key = EE_Session::session_id_prefix.$this->_sid; |
|
| 529 | 529 | try { |
| 530 | 530 | // we're using WP's Transient API to store session data using the PHP session ID as the option name |
| 531 | 531 | $session_data = $this->cache_storage->get($ssn_key, false); |
@@ -534,7 +534,7 @@ discard block |
||
| 534 | 534 | } |
| 535 | 535 | if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
| 536 | 536 | $hash_check = $this->cache_storage->get( |
| 537 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 537 | + EE_Session::hash_check_prefix.$this->_sid, |
|
| 538 | 538 | false |
| 539 | 539 | ); |
| 540 | 540 | if ($hash_check && $hash_check !== md5($session_data)) { |
@@ -544,7 +544,7 @@ discard block |
||
| 544 | 544 | 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
| 545 | 545 | 'event_espresso' |
| 546 | 546 | ), |
| 547 | - EE_Session::session_id_prefix . $this->_sid |
|
| 547 | + EE_Session::session_id_prefix.$this->_sid |
|
| 548 | 548 | ), |
| 549 | 549 | __FILE__, __FUNCTION__, __LINE__ |
| 550 | 550 | ); |
@@ -556,17 +556,17 @@ discard block |
||
| 556 | 556 | $row = $wpdb->get_row( |
| 557 | 557 | $wpdb->prepare( |
| 558 | 558 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
| 559 | - '_transient_' . $ssn_key |
|
| 559 | + '_transient_'.$ssn_key |
|
| 560 | 560 | ) |
| 561 | 561 | ); |
| 562 | 562 | $session_data = is_object($row) ? $row->option_value : null; |
| 563 | 563 | if ($session_data) { |
| 564 | 564 | $session_data = preg_replace_callback( |
| 565 | 565 | '!s:(d+):"(.*?)";!', |
| 566 | - function ($match) { |
|
| 566 | + function($match) { |
|
| 567 | 567 | return $match[1] === strlen($match[2]) |
| 568 | 568 | ? $match[0] |
| 569 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
| 569 | + : 's:'.strlen($match[2]).':"'.$match[2].'";'; |
|
| 570 | 570 | }, |
| 571 | 571 | $session_data |
| 572 | 572 | ); |
@@ -587,7 +587,7 @@ discard block |
||
| 587 | 587 | 'event_espresso' |
| 588 | 588 | ); |
| 589 | 589 | $msg .= WP_DEBUG |
| 590 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 590 | + ? '<br><pre>'.print_r($session_data, true).'</pre><br>'.$this->find_serialize_error($session_data) |
|
| 591 | 591 | : ''; |
| 592 | 592 | throw new InvalidSessionDataException($msg, 0, $e); |
| 593 | 593 | } |
@@ -600,11 +600,11 @@ discard block |
||
| 600 | 600 | 'event_espresso' |
| 601 | 601 | ); |
| 602 | 602 | $msg .= WP_DEBUG |
| 603 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 603 | + ? '<br><pre>'.print_r($session_data, true).'</pre><br>'.$this->find_serialize_error($session_data) |
|
| 604 | 604 | : ''; |
| 605 | 605 | throw new InvalidSessionDataException($msg); |
| 606 | 606 | } |
| 607 | - if ( isset($session_data['transaction'] ) && absint($session_data['transaction'] ) !== 0 ) { |
|
| 607 | + if (isset($session_data['transaction']) && absint($session_data['transaction']) !== 0) { |
|
| 608 | 608 | $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
| 609 | 609 | $session_data['transaction'] |
| 610 | 610 | ); |
@@ -625,12 +625,12 @@ discard block |
||
| 625 | 625 | */ |
| 626 | 626 | protected function _generate_session_id() { |
| 627 | 627 | // check if the SID was passed explicitly, otherwise get from session, then add salt and hash it to reduce length |
| 628 | - if ( isset( $_REQUEST[ 'EESID' ] ) ) { |
|
| 629 | - $session_id = sanitize_text_field( $_REQUEST[ 'EESID' ] ); |
|
| 628 | + if (isset($_REQUEST['EESID'])) { |
|
| 629 | + $session_id = sanitize_text_field($_REQUEST['EESID']); |
|
| 630 | 630 | } else { |
| 631 | - $session_id = md5( session_id() . get_current_blog_id() . $this->_get_sid_salt() ); |
|
| 631 | + $session_id = md5(session_id().get_current_blog_id().$this->_get_sid_salt()); |
|
| 632 | 632 | } |
| 633 | - return apply_filters( 'FHEE__EE_Session___generate_session_id__session_id', $session_id ); |
|
| 633 | + return apply_filters('FHEE__EE_Session___generate_session_id__session_id', $session_id); |
|
| 634 | 634 | } |
| 635 | 635 | |
| 636 | 636 | |
@@ -642,20 +642,20 @@ discard block |
||
| 642 | 642 | */ |
| 643 | 643 | protected function _get_sid_salt() { |
| 644 | 644 | // was session id salt already saved to db ? |
| 645 | - if ( empty( $this->_sid_salt ) ) { |
|
| 645 | + if (empty($this->_sid_salt)) { |
|
| 646 | 646 | // no? then maybe use WP defined constant |
| 647 | - if ( defined( 'AUTH_SALT' ) ) { |
|
| 647 | + if (defined('AUTH_SALT')) { |
|
| 648 | 648 | $this->_sid_salt = AUTH_SALT; |
| 649 | 649 | } |
| 650 | 650 | // if salt doesn't exist or is too short |
| 651 | - if ( strlen( $this->_sid_salt ) < 32 ) { |
|
| 651 | + if (strlen($this->_sid_salt) < 32) { |
|
| 652 | 652 | // create a new one |
| 653 | - $this->_sid_salt = wp_generate_password( 64 ); |
|
| 653 | + $this->_sid_salt = wp_generate_password(64); |
|
| 654 | 654 | } |
| 655 | 655 | // and save it as a permanent session setting |
| 656 | - $session_settings = get_option( 'ee_session_settings' ); |
|
| 657 | - $session_settings[ 'sid_salt' ] = $this->_sid_salt; |
|
| 658 | - update_option( 'ee_session_settings', $session_settings ); |
|
| 656 | + $session_settings = get_option('ee_session_settings'); |
|
| 657 | + $session_settings['sid_salt'] = $this->_sid_salt; |
|
| 658 | + update_option('ee_session_settings', $session_settings); |
|
| 659 | 659 | } |
| 660 | 660 | return $this->_sid_salt; |
| 661 | 661 | } |
@@ -684,19 +684,19 @@ discard block |
||
| 684 | 684 | * @return TRUE on success, FALSE on fail |
| 685 | 685 | * @throws \EE_Error |
| 686 | 686 | */ |
| 687 | - public function update( $new_session = FALSE ) { |
|
| 688 | - $this->_session_data = isset( $this->_session_data ) |
|
| 689 | - && is_array( $this->_session_data ) |
|
| 690 | - && isset( $this->_session_data['id']) |
|
| 687 | + public function update($new_session = FALSE) { |
|
| 688 | + $this->_session_data = isset($this->_session_data) |
|
| 689 | + && is_array($this->_session_data) |
|
| 690 | + && isset($this->_session_data['id']) |
|
| 691 | 691 | ? $this->_session_data |
| 692 | 692 | : array(); |
| 693 | - if ( empty( $this->_session_data )) { |
|
| 693 | + if (empty($this->_session_data)) { |
|
| 694 | 694 | $this->_set_defaults(); |
| 695 | 695 | } |
| 696 | 696 | $session_data = array(); |
| 697 | - foreach ( $this->_session_data as $key => $value ) { |
|
| 697 | + foreach ($this->_session_data as $key => $value) { |
|
| 698 | 698 | |
| 699 | - switch( $key ) { |
|
| 699 | + switch ($key) { |
|
| 700 | 700 | |
| 701 | 701 | case 'id' : |
| 702 | 702 | // session ID |
@@ -714,7 +714,7 @@ discard block |
||
| 714 | 714 | break; |
| 715 | 715 | |
| 716 | 716 | case 'init_access' : |
| 717 | - $session_data['init_access'] = absint( $value ); |
|
| 717 | + $session_data['init_access'] = absint($value); |
|
| 718 | 718 | break; |
| 719 | 719 | |
| 720 | 720 | case 'last_access' : |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | |
| 725 | 725 | case 'expiration' : |
| 726 | 726 | // when the session expires |
| 727 | - $session_data['expiration'] = ! empty( $this->_expiration ) |
|
| 727 | + $session_data['expiration'] = ! empty($this->_expiration) |
|
| 728 | 728 | ? $this->_expiration |
| 729 | 729 | : $session_data['init_access'] + $this->_lifespan; |
| 730 | 730 | break; |
@@ -736,11 +736,11 @@ discard block |
||
| 736 | 736 | |
| 737 | 737 | case 'pages_visited' : |
| 738 | 738 | $page_visit = $this->_get_page_visit(); |
| 739 | - if ( $page_visit ) { |
|
| 739 | + if ($page_visit) { |
|
| 740 | 740 | // set pages visited where the first will be the http referrer |
| 741 | - $this->_session_data[ 'pages_visited' ][ $this->_time ] = $page_visit; |
|
| 741 | + $this->_session_data['pages_visited'][$this->_time] = $page_visit; |
|
| 742 | 742 | // we'll only save the last 10 page visits. |
| 743 | - $session_data[ 'pages_visited' ] = array_slice( $this->_session_data['pages_visited'], -10 ); |
|
| 743 | + $session_data['pages_visited'] = array_slice($this->_session_data['pages_visited'], -10); |
|
| 744 | 744 | } |
| 745 | 745 | break; |
| 746 | 746 | |
@@ -754,9 +754,9 @@ discard block |
||
| 754 | 754 | |
| 755 | 755 | $this->_session_data = $session_data; |
| 756 | 756 | // creating a new session does not require saving to the db just yet |
| 757 | - if ( ! $new_session ) { |
|
| 757 | + if ( ! $new_session) { |
|
| 758 | 758 | // ready? let's save |
| 759 | - if ( $this->_save_session_to_db() ) { |
|
| 759 | + if ($this->_save_session_to_db()) { |
|
| 760 | 760 | return TRUE; |
| 761 | 761 | } else { |
| 762 | 762 | return FALSE; |
@@ -776,9 +776,9 @@ discard block |
||
| 776 | 776 | * @throws \EE_Error |
| 777 | 777 | */ |
| 778 | 778 | private function _create_espresso_session( ) { |
| 779 | - do_action( 'AHEE_log', __CLASS__, __FUNCTION__, '' ); |
|
| 779 | + do_action('AHEE_log', __CLASS__, __FUNCTION__, ''); |
|
| 780 | 780 | // use the update function for now with $new_session arg set to TRUE |
| 781 | - return $this->update( TRUE ) ? TRUE : FALSE; |
|
| 781 | + return $this->update(TRUE) ? TRUE : FALSE; |
|
| 782 | 782 | } |
| 783 | 783 | |
| 784 | 784 | |
@@ -798,7 +798,7 @@ discard block |
||
| 798 | 798 | EE_Registry::instance()->REQ->front_ajax |
| 799 | 799 | || ( |
| 800 | 800 | // OR an admin request that is NOT AJAX |
| 801 | - ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
|
| 801 | + ! (defined('DOING_AJAX') && DOING_AJAX) |
|
| 802 | 802 | && is_admin() |
| 803 | 803 | ) |
| 804 | 804 | || ( |
@@ -811,8 +811,8 @@ discard block |
||
| 811 | 811 | return false; |
| 812 | 812 | } |
| 813 | 813 | $transaction = $this->transaction(); |
| 814 | - if ( $transaction instanceof EE_Transaction ) { |
|
| 815 | - if ( ! $transaction->ID() ) { |
|
| 814 | + if ($transaction instanceof EE_Transaction) { |
|
| 815 | + if ( ! $transaction->ID()) { |
|
| 816 | 816 | $transaction->save(); |
| 817 | 817 | } |
| 818 | 818 | $this->_session_data['transaction'] = $transaction->ID(); |
@@ -821,19 +821,19 @@ discard block |
||
| 821 | 821 | $session_data = serialize($this->_session_data); |
| 822 | 822 | // do we need to also encode it to avoid corrupted data when saved to the db? |
| 823 | 823 | $session_data = $this->_use_encryption |
| 824 | - ? $this->encryption->base64_string_encode( $session_data ) |
|
| 824 | + ? $this->encryption->base64_string_encode($session_data) |
|
| 825 | 825 | : $session_data; |
| 826 | 826 | // maybe save hash check |
| 827 | - if ( apply_filters( 'FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG ) ) { |
|
| 827 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 828 | 828 | $this->cache_storage->add( |
| 829 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 829 | + EE_Session::hash_check_prefix.$this->_sid, |
|
| 830 | 830 | md5($session_data), |
| 831 | 831 | $this->_lifespan |
| 832 | 832 | ); |
| 833 | 833 | } |
| 834 | 834 | // we're using the Transient API for storing session data, |
| 835 | 835 | return $this->cache_storage->add( |
| 836 | - EE_Session::session_id_prefix . $this->_sid, |
|
| 836 | + EE_Session::session_id_prefix.$this->_sid, |
|
| 837 | 837 | $session_data, |
| 838 | 838 | $this->_lifespan |
| 839 | 839 | ); |
@@ -862,10 +862,10 @@ discard block |
||
| 862 | 862 | 'HTTP_FORWARDED', |
| 863 | 863 | 'REMOTE_ADDR' |
| 864 | 864 | ); |
| 865 | - foreach ( $server_keys as $key ){ |
|
| 866 | - if ( isset( $_SERVER[ $key ] )) { |
|
| 867 | - foreach ( array_map( 'trim', explode( ',', $_SERVER[ $key ] )) as $ip ) { |
|
| 868 | - if ( $ip === '127.0.0.1' || filter_var( $ip, FILTER_VALIDATE_IP ) !== FALSE ) { |
|
| 865 | + foreach ($server_keys as $key) { |
|
| 866 | + if (isset($_SERVER[$key])) { |
|
| 867 | + foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip) { |
|
| 868 | + if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP) !== FALSE) { |
|
| 869 | 869 | $visitor_ip = $ip; |
| 870 | 870 | } |
| 871 | 871 | } |
@@ -884,32 +884,32 @@ discard block |
||
| 884 | 884 | * @return string |
| 885 | 885 | */ |
| 886 | 886 | public function _get_page_visit() { |
| 887 | - $page_visit = home_url('/') . 'wp-admin/admin-ajax.php'; |
|
| 887 | + $page_visit = home_url('/').'wp-admin/admin-ajax.php'; |
|
| 888 | 888 | // check for request url |
| 889 | - if ( isset( $_SERVER['REQUEST_URI'] )) { |
|
| 889 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 890 | 890 | $http_host = ''; |
| 891 | 891 | $page_id = '?'; |
| 892 | 892 | $e_reg = ''; |
| 893 | - $request_uri = esc_url( $_SERVER['REQUEST_URI'] ); |
|
| 894 | - $ru_bits = explode( '?', $request_uri ); |
|
| 893 | + $request_uri = esc_url($_SERVER['REQUEST_URI']); |
|
| 894 | + $ru_bits = explode('?', $request_uri); |
|
| 895 | 895 | $request_uri = $ru_bits[0]; |
| 896 | 896 | // check for and grab host as well |
| 897 | - if ( isset( $_SERVER['HTTP_HOST'] )) { |
|
| 898 | - $http_host = esc_url( $_SERVER['HTTP_HOST'] ); |
|
| 897 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
| 898 | + $http_host = esc_url($_SERVER['HTTP_HOST']); |
|
| 899 | 899 | } |
| 900 | 900 | // check for page_id in SERVER REQUEST |
| 901 | - if ( isset( $_REQUEST['page_id'] )) { |
|
| 901 | + if (isset($_REQUEST['page_id'])) { |
|
| 902 | 902 | // rebuild $e_reg without any of the extra parameters |
| 903 | - $page_id = '?page_id=' . esc_attr( $_REQUEST['page_id'] ) . '&'; |
|
| 903 | + $page_id = '?page_id='.esc_attr($_REQUEST['page_id']).'&'; |
|
| 904 | 904 | } |
| 905 | 905 | // check for $e_reg in SERVER REQUEST |
| 906 | - if ( isset( $_REQUEST['ee'] )) { |
|
| 906 | + if (isset($_REQUEST['ee'])) { |
|
| 907 | 907 | // rebuild $e_reg without any of the extra parameters |
| 908 | - $e_reg = 'ee=' . esc_attr( $_REQUEST['ee'] ); |
|
| 908 | + $e_reg = 'ee='.esc_attr($_REQUEST['ee']); |
|
| 909 | 909 | } |
| 910 | - $page_visit = rtrim( $http_host . $request_uri . $page_id . $e_reg, '?' ); |
|
| 910 | + $page_visit = rtrim($http_host.$request_uri.$page_id.$e_reg, '?'); |
|
| 911 | 911 | } |
| 912 | - return $page_visit !== home_url( '/wp-admin/admin-ajax.php' ) ? $page_visit : ''; |
|
| 912 | + return $page_visit !== home_url('/wp-admin/admin-ajax.php') ? $page_visit : ''; |
|
| 913 | 913 | |
| 914 | 914 | } |
| 915 | 915 | |
@@ -939,14 +939,14 @@ discard block |
||
| 939 | 939 | * @return void |
| 940 | 940 | * @throws \EE_Error |
| 941 | 941 | */ |
| 942 | - public function clear_session( $class = '', $function = '' ) { |
|
| 942 | + public function clear_session($class = '', $function = '') { |
|
| 943 | 943 | //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>'; |
| 944 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()' ); |
|
| 944 | + do_action('AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : '.$class.'::'.$function.'()'); |
|
| 945 | 945 | $this->reset_cart(); |
| 946 | 946 | $this->reset_checkout(); |
| 947 | 947 | $this->reset_transaction(); |
| 948 | 948 | // wipe out everything that isn't a default session datum |
| 949 | - $this->reset_data( array_keys( $this->_session_data )); |
|
| 949 | + $this->reset_data(array_keys($this->_session_data)); |
|
| 950 | 950 | // reset initial site access time and the session expiration |
| 951 | 951 | $this->_set_init_access_and_expiration(); |
| 952 | 952 | $this->_save_session_to_db(); |
@@ -961,42 +961,42 @@ discard block |
||
| 961 | 961 | * @param bool $show_all_notices |
| 962 | 962 | * @return TRUE on success, FALSE on fail |
| 963 | 963 | */ |
| 964 | - public function reset_data( $data_to_reset = array(), $show_all_notices = FALSE ) { |
|
| 964 | + public function reset_data($data_to_reset = array(), $show_all_notices = FALSE) { |
|
| 965 | 965 | // if $data_to_reset is not in an array, then put it in one |
| 966 | - if ( ! is_array( $data_to_reset ) ) { |
|
| 967 | - $data_to_reset = array ( $data_to_reset ); |
|
| 966 | + if ( ! is_array($data_to_reset)) { |
|
| 967 | + $data_to_reset = array($data_to_reset); |
|
| 968 | 968 | } |
| 969 | 969 | // nothing ??? go home! |
| 970 | - if ( empty( $data_to_reset )) { |
|
| 971 | - EE_Error::add_error( __( 'No session data could be reset, because no session var name was provided.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 970 | + if (empty($data_to_reset)) { |
|
| 971 | + EE_Error::add_error(__('No session data could be reset, because no session var name was provided.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
| 972 | 972 | return FALSE; |
| 973 | 973 | } |
| 974 | 974 | $return_value = TRUE; |
| 975 | 975 | // since $data_to_reset is an array, cycle through the values |
| 976 | - foreach ( $data_to_reset as $reset ) { |
|
| 976 | + foreach ($data_to_reset as $reset) { |
|
| 977 | 977 | |
| 978 | 978 | // first check to make sure it is a valid session var |
| 979 | - if ( isset( $this->_session_data[ $reset ] )) { |
|
| 979 | + if (isset($this->_session_data[$reset])) { |
|
| 980 | 980 | // then check to make sure it is not a default var |
| 981 | - if ( ! array_key_exists( $reset, $this->_default_session_vars )) { |
|
| 981 | + if ( ! array_key_exists($reset, $this->_default_session_vars)) { |
|
| 982 | 982 | // remove session var |
| 983 | - unset( $this->_session_data[ $reset ] ); |
|
| 984 | - if ( $show_all_notices ) { |
|
| 985 | - EE_Error::add_success( sprintf( __( 'The session variable %s was removed.', 'event_espresso' ), $reset ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 983 | + unset($this->_session_data[$reset]); |
|
| 984 | + if ($show_all_notices) { |
|
| 985 | + EE_Error::add_success(sprintf(__('The session variable %s was removed.', 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 986 | 986 | } |
| 987 | - $return_value = !isset($return_value) ? TRUE : $return_value; |
|
| 987 | + $return_value = ! isset($return_value) ? TRUE : $return_value; |
|
| 988 | 988 | |
| 989 | 989 | } else { |
| 990 | 990 | // yeeeeeeeeerrrrrrrrrrr OUT !!!! |
| 991 | - if ( $show_all_notices ) { |
|
| 992 | - EE_Error::add_error( sprintf( __( 'Sorry! %s is a default session datum and can not be reset.', 'event_espresso' ), $reset ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 991 | + if ($show_all_notices) { |
|
| 992 | + EE_Error::add_error(sprintf(__('Sorry! %s is a default session datum and can not be reset.', 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 993 | 993 | } |
| 994 | 994 | $return_value = FALSE; |
| 995 | 995 | } |
| 996 | 996 | |
| 997 | - } else if ( $show_all_notices ) { |
|
| 997 | + } else if ($show_all_notices) { |
|
| 998 | 998 | // oops! that session var does not exist! |
| 999 | - EE_Error::add_error( sprintf( __( 'The session item provided, %s, is invalid or does not exist.', 'event_espresso' ), $reset ), __FILE__, __FUNCTION__, __LINE__ ); |
|
| 999 | + EE_Error::add_error(sprintf(__('The session item provided, %s, is invalid or does not exist.', 'event_espresso'), $reset), __FILE__, __FUNCTION__, __LINE__); |
|
| 1000 | 1000 | $return_value = FALSE; |
| 1001 | 1001 | } |
| 1002 | 1002 | |
@@ -1015,8 +1015,8 @@ discard block |
||
| 1015 | 1015 | * @throws \EE_Error |
| 1016 | 1016 | */ |
| 1017 | 1017 | public function wp_loaded() { |
| 1018 | - if ( isset( EE_Registry::instance()->REQ ) && EE_Registry::instance()->REQ->is_set( 'clear_session' )) { |
|
| 1019 | - $this->clear_session( __CLASS__, __FUNCTION__ ); |
|
| 1018 | + if (isset(EE_Registry::instance()->REQ) && EE_Registry::instance()->REQ->is_set('clear_session')) { |
|
| 1019 | + $this->clear_session(__CLASS__, __FUNCTION__); |
|
| 1020 | 1020 | } |
| 1021 | 1021 | } |
| 1022 | 1022 | |
@@ -1052,7 +1052,7 @@ discard block |
||
| 1052 | 1052 | // or use that for the new transient cleanup query limit |
| 1053 | 1053 | add_filter( |
| 1054 | 1054 | 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
| 1055 | - function () use ($expired_session_transient_delete_query_limit) { |
|
| 1055 | + function() use ($expired_session_transient_delete_query_limit) { |
|
| 1056 | 1056 | return $expired_session_transient_delete_query_limit; |
| 1057 | 1057 | } |
| 1058 | 1058 | ); |
@@ -1066,34 +1066,34 @@ discard block |
||
| 1066 | 1066 | * @param $data1 |
| 1067 | 1067 | * @return string |
| 1068 | 1068 | */ |
| 1069 | - private function find_serialize_error( $data1 ) { |
|
| 1069 | + private function find_serialize_error($data1) { |
|
| 1070 | 1070 | $error = '<pre>'; |
| 1071 | 1071 | $data2 = preg_replace_callback( |
| 1072 | 1072 | '!s:(\d+):"(.*?)";!', |
| 1073 | - function ( $match ) { |
|
| 1074 | - return ( $match[1] === strlen( $match[2] ) ) |
|
| 1073 | + function($match) { |
|
| 1074 | + return ($match[1] === strlen($match[2])) |
|
| 1075 | 1075 | ? $match[0] |
| 1076 | 1076 | : 's:' |
| 1077 | - . strlen( $match[2] ) |
|
| 1077 | + . strlen($match[2]) |
|
| 1078 | 1078 | . ':"' |
| 1079 | 1079 | . $match[2] |
| 1080 | 1080 | . '";'; |
| 1081 | 1081 | }, |
| 1082 | 1082 | $data1 |
| 1083 | 1083 | ); |
| 1084 | - $max = ( strlen( $data1 ) > strlen( $data2 ) ) ? strlen( $data1 ) : strlen( $data2 ); |
|
| 1085 | - $error .= $data1 . PHP_EOL; |
|
| 1086 | - $error .= $data2 . PHP_EOL; |
|
| 1087 | - for ( $i = 0; $i < $max; $i++ ) { |
|
| 1088 | - if ( @$data1[ $i ] !== @$data2[ $i ] ) { |
|
| 1089 | - $error .= 'Difference ' . @$data1[ $i ] . ' != ' . @$data2[ $i ] . PHP_EOL; |
|
| 1090 | - $error .= "\t-> ORD number " . ord( @$data1[ $i ] ) . ' != ' . ord( @$data2[ $i ] ) . PHP_EOL; |
|
| 1091 | - $error .= "\t-> Line Number = $i" . PHP_EOL; |
|
| 1092 | - $start = ( $i - 20 ); |
|
| 1093 | - $start = ( $start < 0 ) ? 0 : $start; |
|
| 1084 | + $max = (strlen($data1) > strlen($data2)) ? strlen($data1) : strlen($data2); |
|
| 1085 | + $error .= $data1.PHP_EOL; |
|
| 1086 | + $error .= $data2.PHP_EOL; |
|
| 1087 | + for ($i = 0; $i < $max; $i++) { |
|
| 1088 | + if (@$data1[$i] !== @$data2[$i]) { |
|
| 1089 | + $error .= 'Difference '.@$data1[$i].' != '.@$data2[$i].PHP_EOL; |
|
| 1090 | + $error .= "\t-> ORD number ".ord(@$data1[$i]).' != '.ord(@$data2[$i]).PHP_EOL; |
|
| 1091 | + $error .= "\t-> Line Number = $i".PHP_EOL; |
|
| 1092 | + $start = ($i - 20); |
|
| 1093 | + $start = ($start < 0) ? 0 : $start; |
|
| 1094 | 1094 | $length = 40; |
| 1095 | 1095 | $point = $max - $i; |
| 1096 | - if ( $point < 20 ) { |
|
| 1096 | + if ($point < 20) { |
|
| 1097 | 1097 | $rlength = 1; |
| 1098 | 1098 | $rpoint = -$point; |
| 1099 | 1099 | } else { |
@@ -1102,16 +1102,16 @@ discard block |
||
| 1102 | 1102 | } |
| 1103 | 1103 | $error .= "\t-> Section Data1 = "; |
| 1104 | 1104 | $error .= substr_replace( |
| 1105 | - substr( $data1, $start, $length ), |
|
| 1106 | - "<b style=\"color:green\">{$data1[ $i ]}</b>", |
|
| 1105 | + substr($data1, $start, $length), |
|
| 1106 | + "<b style=\"color:green\">{$data1[$i]}</b>", |
|
| 1107 | 1107 | $rpoint, |
| 1108 | 1108 | $rlength |
| 1109 | 1109 | ); |
| 1110 | 1110 | $error .= PHP_EOL; |
| 1111 | 1111 | $error .= "\t-> Section Data2 = "; |
| 1112 | 1112 | $error .= substr_replace( |
| 1113 | - substr( $data2, $start, $length ), |
|
| 1114 | - "<b style=\"color:red\">{$data2[ $i ]}</b>", |
|
| 1113 | + substr($data2, $start, $length), |
|
| 1114 | + "<b style=\"color:red\">{$data2[$i]}</b>", |
|
| 1115 | 1115 | $rpoint, |
| 1116 | 1116 | $rlength |
| 1117 | 1117 | ); |
@@ -22,19 +22,19 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | private static $_instance; |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var CacheStorageInterface $cache_storage |
|
| 27 | - */ |
|
| 28 | - protected $cache_storage; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * EE_Encryption object |
|
| 32 | - * |
|
| 33 | - * @var EE_Encryption |
|
| 34 | - */ |
|
| 35 | - protected $encryption; |
|
| 36 | - |
|
| 37 | - /** |
|
| 25 | + /** |
|
| 26 | + * @var CacheStorageInterface $cache_storage |
|
| 27 | + */ |
|
| 28 | + protected $cache_storage; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * EE_Encryption object |
|
| 32 | + * |
|
| 33 | + * @var EE_Encryption |
|
| 34 | + */ |
|
| 35 | + protected $encryption; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | 38 | * the session id |
| 39 | 39 | * @var string |
| 40 | 40 | */ |
@@ -65,12 +65,12 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | private $_expiration; |
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * whether or not session has expired at some point |
|
| 70 | - * |
|
| 71 | - * @var boolean |
|
| 72 | - */ |
|
| 73 | - private $_expired = false; |
|
| 68 | + /** |
|
| 69 | + * whether or not session has expired at some point |
|
| 70 | + * |
|
| 71 | + * @var boolean |
|
| 72 | + */ |
|
| 73 | + private $_expired = false; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * current time as Unix timestamp in GMT |
@@ -107,30 +107,30 @@ discard block |
||
| 107 | 107 | * @var array |
| 108 | 108 | */ |
| 109 | 109 | private $_default_session_vars = array ( |
| 110 | - 'id' => null, |
|
| 111 | - 'user_id' => null, |
|
| 112 | - 'ip_address' => null, |
|
| 113 | - 'user_agent' => null, |
|
| 114 | - 'init_access' => null, |
|
| 115 | - 'last_access' => null, |
|
| 116 | - 'expiration' => null, |
|
| 117 | - 'pages_visited' => array(), |
|
| 110 | + 'id' => null, |
|
| 111 | + 'user_id' => null, |
|
| 112 | + 'ip_address' => null, |
|
| 113 | + 'user_agent' => null, |
|
| 114 | + 'init_access' => null, |
|
| 115 | + 'last_access' => null, |
|
| 116 | + 'expiration' => null, |
|
| 117 | + 'pages_visited' => array(), |
|
| 118 | 118 | ); |
| 119 | 119 | |
| 120 | 120 | |
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | 123 | * @singleton method used to instantiate class object |
| 124 | - * @param CacheStorageInterface $cache_storage |
|
| 125 | - * @param \EE_Encryption $encryption |
|
| 124 | + * @param CacheStorageInterface $cache_storage |
|
| 125 | + * @param \EE_Encryption $encryption |
|
| 126 | 126 | * @return EE_Session |
| 127 | 127 | * @throws InvalidSessionDataException |
| 128 | 128 | * @throws \EE_Error |
| 129 | 129 | */ |
| 130 | 130 | public static function instance( |
| 131 | - CacheStorageInterface $cache_storage = null, |
|
| 132 | - EE_Encryption $encryption = null |
|
| 133 | - ) { |
|
| 131 | + CacheStorageInterface $cache_storage = null, |
|
| 132 | + EE_Encryption $encryption = null |
|
| 133 | + ) { |
|
| 134 | 134 | // check if class object is instantiated |
| 135 | 135 | // session loading is turned ON by default, but prior to the init hook, can be turned back OFF via: |
| 136 | 136 | // add_filter( 'FHEE_load_EE_Session', '__return_false' ); |
@@ -143,10 +143,10 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | - * protected constructor to prevent direct creation |
|
| 147 | - * |
|
| 148 | - * @param CacheStorageInterface $cache_storage |
|
| 149 | - * @param \EE_Encryption $encryption |
|
| 146 | + * protected constructor to prevent direct creation |
|
| 147 | + * |
|
| 148 | + * @param CacheStorageInterface $cache_storage |
|
| 149 | + * @param \EE_Encryption $encryption |
|
| 150 | 150 | * @throws \EE_Error |
| 151 | 151 | * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
| 152 | 152 | */ |
@@ -181,59 +181,59 @@ discard block |
||
| 181 | 181 | $this->{$var_name} = $session_setting; |
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | - $this->cache_storage = $cache_storage; |
|
| 185 | - // are we using encryption? |
|
| 186 | - $this->_use_encryption = $encryption instanceof EE_Encryption && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
| 187 | - // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
| 188 | - // encrypt data via: $this->encryption->encrypt(); |
|
| 189 | - $this->encryption = $encryption; |
|
| 184 | + $this->cache_storage = $cache_storage; |
|
| 185 | + // are we using encryption? |
|
| 186 | + $this->_use_encryption = $encryption instanceof EE_Encryption && EE_Registry::instance()->CFG->admin->encode_session_data(); |
|
| 187 | + // \EEH_Debug_Tools::printr($this->_use_encryption, '$this->_use_encryption', __FILE__, __LINE__); |
|
| 188 | + // encrypt data via: $this->encryption->encrypt(); |
|
| 189 | + $this->encryption = $encryption; |
|
| 190 | 190 | // filter hook allows outside functions/classes/plugins to change default empty cart |
| 191 | 191 | $extra_default_session_vars = apply_filters( 'FHEE__EE_Session__construct__extra_default_session_vars', array() ); |
| 192 | 192 | array_merge( $this->_default_session_vars, $extra_default_session_vars ); |
| 193 | 193 | // apply default session vars |
| 194 | 194 | $this->_set_defaults(); |
| 195 | - add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
| 196 | - // check request for 'clear_session' param |
|
| 195 | + add_action('AHEE__EE_System__initialize', array($this, 'open_session')); |
|
| 196 | + // check request for 'clear_session' param |
|
| 197 | 197 | add_action( 'AHEE__EE_Request_Handler__construct__complete', array( $this, 'wp_loaded' )); |
| 198 | 198 | // once everything is all said and done, |
| 199 | 199 | add_action( 'shutdown', array( $this, 'update' ), 100 ); |
| 200 | - $this->configure_garbage_collection_filters(); |
|
| 200 | + $this->configure_garbage_collection_filters(); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | |
| 204 | 204 | |
| 205 | - /** |
|
| 206 | - * @return void |
|
| 207 | - * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 208 | - * @throws \EE_Error |
|
| 209 | - */ |
|
| 205 | + /** |
|
| 206 | + * @return void |
|
| 207 | + * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 208 | + * @throws \EE_Error |
|
| 209 | + */ |
|
| 210 | 210 | public function open_session() { |
| 211 | - // check for existing session and retrieve it from db |
|
| 212 | - if ( ! $this->_espresso_session()) { |
|
| 213 | - // or just start a new one |
|
| 214 | - $this->_create_espresso_session(); |
|
| 215 | - } |
|
| 216 | - } |
|
| 211 | + // check for existing session and retrieve it from db |
|
| 212 | + if ( ! $this->_espresso_session()) { |
|
| 213 | + // or just start a new one |
|
| 214 | + $this->_create_espresso_session(); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | 218 | |
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * @return bool |
|
| 222 | - */ |
|
| 223 | - public function expired() |
|
| 224 | - { |
|
| 225 | - return $this->_expired; |
|
| 226 | - } |
|
| 220 | + /** |
|
| 221 | + * @return bool |
|
| 222 | + */ |
|
| 223 | + public function expired() |
|
| 224 | + { |
|
| 225 | + return $this->_expired; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | 228 | |
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * @return void |
|
| 232 | - */ |
|
| 233 | - public function reset_expired() |
|
| 234 | - { |
|
| 235 | - $this->_expired = false; |
|
| 236 | - } |
|
| 230 | + /** |
|
| 231 | + * @return void |
|
| 232 | + */ |
|
| 233 | + public function reset_expired() |
|
| 234 | + { |
|
| 235 | + $this->_expired = false; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | |
| 239 | 239 | /** |
@@ -245,24 +245,24 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | |
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * @return int |
|
| 250 | - */ |
|
| 251 | - public function extension() |
|
| 252 | - { |
|
| 253 | - return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', (10 * MINUTE_IN_SECONDS)); |
|
| 254 | - } |
|
| 248 | + /** |
|
| 249 | + * @return int |
|
| 250 | + */ |
|
| 251 | + public function extension() |
|
| 252 | + { |
|
| 253 | + return apply_filters('FHEE__EE_Session__extend_expiration__seconds_added', (10 * MINUTE_IN_SECONDS)); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | 256 | |
| 257 | 257 | |
| 258 | - /** |
|
| 259 | - * @param int $time number of seconds to add to session expiration |
|
| 260 | - */ |
|
| 261 | - public function extend_expiration($time = 0) |
|
| 262 | - { |
|
| 263 | - $time = $time ? $time : $this->extension(); |
|
| 264 | - $this->_expiration += absint($time); |
|
| 265 | - } |
|
| 258 | + /** |
|
| 259 | + * @param int $time number of seconds to add to session expiration |
|
| 260 | + */ |
|
| 261 | + public function extend_expiration($time = 0) |
|
| 262 | + { |
|
| 263 | + $time = $time ? $time : $this->extension(); |
|
| 264 | + $this->_expiration += absint($time); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | 267 | |
| 268 | 268 | |
@@ -306,15 +306,15 @@ discard block |
||
| 306 | 306 | |
| 307 | 307 | |
| 308 | 308 | |
| 309 | - /** |
|
| 310 | - * @param \EE_Cart $cart |
|
| 311 | - * @return bool |
|
| 312 | - */ |
|
| 313 | - public function set_cart(EE_Cart $cart) |
|
| 314 | - { |
|
| 315 | - $this->_session_data['cart'] = $cart; |
|
| 316 | - return true; |
|
| 317 | - } |
|
| 309 | + /** |
|
| 310 | + * @param \EE_Cart $cart |
|
| 311 | + * @return bool |
|
| 312 | + */ |
|
| 313 | + public function set_cart(EE_Cart $cart) |
|
| 314 | + { |
|
| 315 | + $this->_session_data['cart'] = $cart; |
|
| 316 | + return true; |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | 319 | |
| 320 | 320 | |
@@ -322,7 +322,7 @@ discard block |
||
| 322 | 322 | * reset_cart |
| 323 | 323 | */ |
| 324 | 324 | public function reset_cart() { |
| 325 | - do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
| 325 | + do_action('AHEE__EE_Session__reset_cart__before_reset', $this); |
|
| 326 | 326 | $this->_session_data['cart'] = NULL; |
| 327 | 327 | } |
| 328 | 328 | |
@@ -332,9 +332,9 @@ discard block |
||
| 332 | 332 | * @return \EE_Cart |
| 333 | 333 | */ |
| 334 | 334 | public function cart() { |
| 335 | - return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
| 336 | - ? $this->_session_data['cart'] |
|
| 337 | - : null; |
|
| 335 | + return isset($this->_session_data['cart']) && $this->_session_data['cart'] instanceof EE_Cart |
|
| 336 | + ? $this->_session_data['cart'] |
|
| 337 | + : null; |
|
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | |
@@ -354,7 +354,7 @@ discard block |
||
| 354 | 354 | * reset_checkout |
| 355 | 355 | */ |
| 356 | 356 | public function reset_checkout() { |
| 357 | - do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
| 357 | + do_action('AHEE__EE_Session__reset_checkout__before_reset', $this); |
|
| 358 | 358 | $this->_session_data['checkout'] = NULL; |
| 359 | 359 | } |
| 360 | 360 | |
@@ -364,9 +364,9 @@ discard block |
||
| 364 | 364 | * @return \EE_Checkout |
| 365 | 365 | */ |
| 366 | 366 | public function checkout() { |
| 367 | - return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
| 368 | - ? $this->_session_data['checkout'] |
|
| 369 | - : null; |
|
| 367 | + return isset($this->_session_data['checkout']) && $this->_session_data['checkout'] instanceof EE_Checkout |
|
| 368 | + ? $this->_session_data['checkout'] |
|
| 369 | + : null; |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | |
@@ -389,7 +389,7 @@ discard block |
||
| 389 | 389 | * reset_transaction |
| 390 | 390 | */ |
| 391 | 391 | public function reset_transaction() { |
| 392 | - do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
| 392 | + do_action('AHEE__EE_Session__reset_transaction__before_reset', $this); |
|
| 393 | 393 | $this->_session_data['transaction'] = NULL; |
| 394 | 394 | } |
| 395 | 395 | |
@@ -399,10 +399,10 @@ discard block |
||
| 399 | 399 | * @return \EE_Transaction |
| 400 | 400 | */ |
| 401 | 401 | public function transaction() { |
| 402 | - return isset($this->_session_data['transaction']) |
|
| 403 | - && $this->_session_data['transaction'] instanceof EE_Transaction |
|
| 404 | - ? $this->_session_data['transaction'] |
|
| 405 | - : null; |
|
| 402 | + return isset($this->_session_data['transaction']) |
|
| 403 | + && $this->_session_data['transaction'] instanceof EE_Transaction |
|
| 404 | + ? $this->_session_data['transaction'] |
|
| 405 | + : null; |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | |
@@ -479,16 +479,16 @@ discard block |
||
| 479 | 479 | // set the "user agent" |
| 480 | 480 | $this->_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? esc_attr( $_SERVER['HTTP_USER_AGENT'] ) : FALSE; |
| 481 | 481 | // now let's retrieve what's in the db |
| 482 | - $session_data = $this->_retrieve_session_data(); |
|
| 483 | - if (! empty($session_data)) { |
|
| 484 | - // get the current time in UTC |
|
| 482 | + $session_data = $this->_retrieve_session_data(); |
|
| 483 | + if (! empty($session_data)) { |
|
| 484 | + // get the current time in UTC |
|
| 485 | 485 | $this->_time = isset( $this->_time ) ? $this->_time : time(); |
| 486 | 486 | // and reset the session expiration |
| 487 | 487 | $this->_expiration = isset( $session_data['expiration'] ) |
| 488 | 488 | ? $session_data['expiration'] |
| 489 | 489 | : $this->_time + $this->_lifespan; |
| 490 | 490 | } else { |
| 491 | - // set initial site access time and the session expiration |
|
| 491 | + // set initial site access time and the session expiration |
|
| 492 | 492 | $this->_set_init_access_and_expiration(); |
| 493 | 493 | // set referer |
| 494 | 494 | $this->_session_data[ 'pages_visited' ][ $this->_session_data['init_access'] ] = isset( $_SERVER['HTTP_REFERER'] ) |
@@ -497,14 +497,14 @@ discard block |
||
| 497 | 497 | // no previous session = go back and create one (on top of the data above) |
| 498 | 498 | return FALSE; |
| 499 | 499 | } |
| 500 | - // now the user agent |
|
| 500 | + // now the user agent |
|
| 501 | 501 | if ( $session_data['user_agent'] !== $this->_user_agent ) { |
| 502 | 502 | return FALSE; |
| 503 | 503 | } |
| 504 | 504 | // wait a minute... how old are you? |
| 505 | 505 | if ( $this->_time > $this->_expiration ) { |
| 506 | 506 | // yer too old fer me! |
| 507 | - $this->_expired = true; |
|
| 507 | + $this->_expired = true; |
|
| 508 | 508 | // wipe out everything that isn't a default session datum |
| 509 | 509 | $this->clear_session( __CLASS__, __FUNCTION__ ); |
| 510 | 510 | } |
@@ -516,101 +516,101 @@ discard block |
||
| 516 | 516 | |
| 517 | 517 | |
| 518 | 518 | |
| 519 | - /** |
|
| 520 | - * _get_session_data |
|
| 521 | - * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup databases |
|
| 522 | - * |
|
| 523 | - * @return array |
|
| 524 | - * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 525 | - */ |
|
| 526 | - protected function _retrieve_session_data() |
|
| 527 | - { |
|
| 528 | - $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
| 529 | - try { |
|
| 530 | - // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
| 531 | - $session_data = $this->cache_storage->get($ssn_key, false); |
|
| 532 | - if (empty($session_data)) { |
|
| 533 | - return array(); |
|
| 534 | - } |
|
| 535 | - if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 536 | - $hash_check = $this->cache_storage->get( |
|
| 537 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 538 | - false |
|
| 539 | - ); |
|
| 540 | - if ($hash_check && $hash_check !== md5($session_data)) { |
|
| 541 | - EE_Error::add_error( |
|
| 542 | - sprintf( |
|
| 543 | - __( |
|
| 544 | - 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
| 545 | - 'event_espresso' |
|
| 546 | - ), |
|
| 547 | - EE_Session::session_id_prefix . $this->_sid |
|
| 548 | - ), |
|
| 549 | - __FILE__, __FUNCTION__, __LINE__ |
|
| 550 | - ); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - } catch (Exception $e) { |
|
| 554 | - // let's just eat that error for now and attempt to correct any corrupted data |
|
| 555 | - global $wpdb; |
|
| 556 | - $row = $wpdb->get_row( |
|
| 557 | - $wpdb->prepare( |
|
| 558 | - "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
| 559 | - '_transient_' . $ssn_key |
|
| 560 | - ) |
|
| 561 | - ); |
|
| 562 | - $session_data = is_object($row) ? $row->option_value : null; |
|
| 563 | - if ($session_data) { |
|
| 564 | - $session_data = preg_replace_callback( |
|
| 565 | - '!s:(d+):"(.*?)";!', |
|
| 566 | - function ($match) { |
|
| 567 | - return $match[1] === strlen($match[2]) |
|
| 568 | - ? $match[0] |
|
| 569 | - : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
| 570 | - }, |
|
| 571 | - $session_data |
|
| 572 | - ); |
|
| 573 | - } |
|
| 574 | - $session_data = maybe_unserialize($session_data); |
|
| 575 | - } |
|
| 576 | - // in case the data is encoded... try to decode it |
|
| 577 | - $session_data = $this->encryption instanceof EE_Encryption |
|
| 578 | - ? $this->encryption->base64_string_decode($session_data) |
|
| 579 | - : $session_data; |
|
| 580 | - |
|
| 581 | - if ( ! is_array($session_data)) { |
|
| 582 | - try { |
|
| 583 | - $session_data = maybe_unserialize($session_data); |
|
| 584 | - } catch (Exception $e) { |
|
| 585 | - $msg = esc_html__( |
|
| 586 | - 'An error occurred while attempting to unserialize the session data.', |
|
| 587 | - 'event_espresso' |
|
| 588 | - ); |
|
| 589 | - $msg .= WP_DEBUG |
|
| 590 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 591 | - : ''; |
|
| 592 | - throw new InvalidSessionDataException($msg, 0, $e); |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - // just a check to make sure the session array is indeed an array |
|
| 596 | - if ( ! is_array($session_data)) { |
|
| 597 | - // no?!?! then something is wrong |
|
| 598 | - $msg = esc_html__( |
|
| 599 | - 'The session data is missing, invalid, or corrupted.', |
|
| 600 | - 'event_espresso' |
|
| 601 | - ); |
|
| 602 | - $msg .= WP_DEBUG |
|
| 603 | - ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 604 | - : ''; |
|
| 605 | - throw new InvalidSessionDataException($msg); |
|
| 606 | - } |
|
| 607 | - if ( isset($session_data['transaction'] ) && absint($session_data['transaction'] ) !== 0 ) { |
|
| 608 | - $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
| 609 | - $session_data['transaction'] |
|
| 610 | - ); |
|
| 611 | - } |
|
| 612 | - return $session_data; |
|
| 613 | - } |
|
| 519 | + /** |
|
| 520 | + * _get_session_data |
|
| 521 | + * Retrieves the session data, and attempts to correct any encoding issues that can occur due to improperly setup databases |
|
| 522 | + * |
|
| 523 | + * @return array |
|
| 524 | + * @throws \EventEspresso\core\exceptions\InvalidSessionDataException |
|
| 525 | + */ |
|
| 526 | + protected function _retrieve_session_data() |
|
| 527 | + { |
|
| 528 | + $ssn_key = EE_Session::session_id_prefix . $this->_sid; |
|
| 529 | + try { |
|
| 530 | + // we're using WP's Transient API to store session data using the PHP session ID as the option name |
|
| 531 | + $session_data = $this->cache_storage->get($ssn_key, false); |
|
| 532 | + if (empty($session_data)) { |
|
| 533 | + return array(); |
|
| 534 | + } |
|
| 535 | + if (apply_filters('FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG)) { |
|
| 536 | + $hash_check = $this->cache_storage->get( |
|
| 537 | + EE_Session::hash_check_prefix . $this->_sid, |
|
| 538 | + false |
|
| 539 | + ); |
|
| 540 | + if ($hash_check && $hash_check !== md5($session_data)) { |
|
| 541 | + EE_Error::add_error( |
|
| 542 | + sprintf( |
|
| 543 | + __( |
|
| 544 | + 'The stored data for session %1$s failed to pass a hash check and therefore appears to be invalid.', |
|
| 545 | + 'event_espresso' |
|
| 546 | + ), |
|
| 547 | + EE_Session::session_id_prefix . $this->_sid |
|
| 548 | + ), |
|
| 549 | + __FILE__, __FUNCTION__, __LINE__ |
|
| 550 | + ); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + } catch (Exception $e) { |
|
| 554 | + // let's just eat that error for now and attempt to correct any corrupted data |
|
| 555 | + global $wpdb; |
|
| 556 | + $row = $wpdb->get_row( |
|
| 557 | + $wpdb->prepare( |
|
| 558 | + "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
|
| 559 | + '_transient_' . $ssn_key |
|
| 560 | + ) |
|
| 561 | + ); |
|
| 562 | + $session_data = is_object($row) ? $row->option_value : null; |
|
| 563 | + if ($session_data) { |
|
| 564 | + $session_data = preg_replace_callback( |
|
| 565 | + '!s:(d+):"(.*?)";!', |
|
| 566 | + function ($match) { |
|
| 567 | + return $match[1] === strlen($match[2]) |
|
| 568 | + ? $match[0] |
|
| 569 | + : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; |
|
| 570 | + }, |
|
| 571 | + $session_data |
|
| 572 | + ); |
|
| 573 | + } |
|
| 574 | + $session_data = maybe_unserialize($session_data); |
|
| 575 | + } |
|
| 576 | + // in case the data is encoded... try to decode it |
|
| 577 | + $session_data = $this->encryption instanceof EE_Encryption |
|
| 578 | + ? $this->encryption->base64_string_decode($session_data) |
|
| 579 | + : $session_data; |
|
| 580 | + |
|
| 581 | + if ( ! is_array($session_data)) { |
|
| 582 | + try { |
|
| 583 | + $session_data = maybe_unserialize($session_data); |
|
| 584 | + } catch (Exception $e) { |
|
| 585 | + $msg = esc_html__( |
|
| 586 | + 'An error occurred while attempting to unserialize the session data.', |
|
| 587 | + 'event_espresso' |
|
| 588 | + ); |
|
| 589 | + $msg .= WP_DEBUG |
|
| 590 | + ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 591 | + : ''; |
|
| 592 | + throw new InvalidSessionDataException($msg, 0, $e); |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + // just a check to make sure the session array is indeed an array |
|
| 596 | + if ( ! is_array($session_data)) { |
|
| 597 | + // no?!?! then something is wrong |
|
| 598 | + $msg = esc_html__( |
|
| 599 | + 'The session data is missing, invalid, or corrupted.', |
|
| 600 | + 'event_espresso' |
|
| 601 | + ); |
|
| 602 | + $msg .= WP_DEBUG |
|
| 603 | + ? '<br><pre>' . print_r($session_data, true) . '</pre><br>' . $this->find_serialize_error($session_data) |
|
| 604 | + : ''; |
|
| 605 | + throw new InvalidSessionDataException($msg); |
|
| 606 | + } |
|
| 607 | + if ( isset($session_data['transaction'] ) && absint($session_data['transaction'] ) !== 0 ) { |
|
| 608 | + $session_data['transaction'] = EEM_Transaction::instance()->get_one_by_ID( |
|
| 609 | + $session_data['transaction'] |
|
| 610 | + ); |
|
| 611 | + } |
|
| 612 | + return $session_data; |
|
| 613 | + } |
|
| 614 | 614 | |
| 615 | 615 | |
| 616 | 616 | |
@@ -677,13 +677,13 @@ discard block |
||
| 677 | 677 | |
| 678 | 678 | |
| 679 | 679 | |
| 680 | - /** |
|
| 681 | - * @update session data prior to saving to the db |
|
| 682 | - * @access public |
|
| 683 | - * @param bool $new_session |
|
| 684 | - * @return TRUE on success, FALSE on fail |
|
| 685 | - * @throws \EE_Error |
|
| 686 | - */ |
|
| 680 | + /** |
|
| 681 | + * @update session data prior to saving to the db |
|
| 682 | + * @access public |
|
| 683 | + * @param bool $new_session |
|
| 684 | + * @return TRUE on success, FALSE on fail |
|
| 685 | + * @throws \EE_Error |
|
| 686 | + */ |
|
| 687 | 687 | public function update( $new_session = FALSE ) { |
| 688 | 688 | $this->_session_data = isset( $this->_session_data ) |
| 689 | 689 | && is_array( $this->_session_data ) |
@@ -769,12 +769,12 @@ discard block |
||
| 769 | 769 | |
| 770 | 770 | |
| 771 | 771 | |
| 772 | - /** |
|
| 773 | - * @create session data array |
|
| 774 | - * @access public |
|
| 775 | - * @return bool |
|
| 776 | - * @throws \EE_Error |
|
| 777 | - */ |
|
| 772 | + /** |
|
| 773 | + * @create session data array |
|
| 774 | + * @access public |
|
| 775 | + * @return bool |
|
| 776 | + * @throws \EE_Error |
|
| 777 | + */ |
|
| 778 | 778 | private function _create_espresso_session( ) { |
| 779 | 779 | do_action( 'AHEE_log', __CLASS__, __FUNCTION__, '' ); |
| 780 | 780 | // use the update function for now with $new_session arg set to TRUE |
@@ -794,19 +794,19 @@ discard block |
||
| 794 | 794 | if ( |
| 795 | 795 | // if the current request is NOT one of the following |
| 796 | 796 | ! ( |
| 797 | - // an an AJAX request from the frontend |
|
| 798 | - EE_Registry::instance()->REQ->front_ajax |
|
| 799 | - || ( |
|
| 800 | - // OR an admin request that is NOT AJAX |
|
| 797 | + // an an AJAX request from the frontend |
|
| 798 | + EE_Registry::instance()->REQ->front_ajax |
|
| 799 | + || ( |
|
| 800 | + // OR an admin request that is NOT AJAX |
|
| 801 | 801 | ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 802 | - && is_admin() |
|
| 802 | + && is_admin() |
|
| 803 | 803 | ) |
| 804 | - || ( |
|
| 805 | - // OR an espresso page |
|
| 806 | - EE_Registry::instance()->REQ instanceof EE_Request_Handler |
|
| 807 | - && EE_Registry::instance()->REQ->is_espresso_page() |
|
| 808 | - ) |
|
| 809 | - ) |
|
| 804 | + || ( |
|
| 805 | + // OR an espresso page |
|
| 806 | + EE_Registry::instance()->REQ instanceof EE_Request_Handler |
|
| 807 | + && EE_Registry::instance()->REQ->is_espresso_page() |
|
| 808 | + ) |
|
| 809 | + ) |
|
| 810 | 810 | ) { |
| 811 | 811 | return false; |
| 812 | 812 | } |
@@ -817,27 +817,27 @@ discard block |
||
| 817 | 817 | } |
| 818 | 818 | $this->_session_data['transaction'] = $transaction->ID(); |
| 819 | 819 | } |
| 820 | - // then serialize all of our session data |
|
| 820 | + // then serialize all of our session data |
|
| 821 | 821 | $session_data = serialize($this->_session_data); |
| 822 | 822 | // do we need to also encode it to avoid corrupted data when saved to the db? |
| 823 | 823 | $session_data = $this->_use_encryption |
| 824 | - ? $this->encryption->base64_string_encode( $session_data ) |
|
| 825 | - : $session_data; |
|
| 824 | + ? $this->encryption->base64_string_encode( $session_data ) |
|
| 825 | + : $session_data; |
|
| 826 | 826 | // maybe save hash check |
| 827 | 827 | if ( apply_filters( 'FHEE__EE_Session___perform_session_id_hash_check', WP_DEBUG ) ) { |
| 828 | - $this->cache_storage->add( |
|
| 829 | - EE_Session::hash_check_prefix . $this->_sid, |
|
| 830 | - md5($session_data), |
|
| 831 | - $this->_lifespan |
|
| 832 | - ); |
|
| 833 | - } |
|
| 834 | - // we're using the Transient API for storing session data, |
|
| 835 | - return $this->cache_storage->add( |
|
| 836 | - EE_Session::session_id_prefix . $this->_sid, |
|
| 837 | - $session_data, |
|
| 838 | - $this->_lifespan |
|
| 839 | - ); |
|
| 840 | - } |
|
| 828 | + $this->cache_storage->add( |
|
| 829 | + EE_Session::hash_check_prefix . $this->_sid, |
|
| 830 | + md5($session_data), |
|
| 831 | + $this->_lifespan |
|
| 832 | + ); |
|
| 833 | + } |
|
| 834 | + // we're using the Transient API for storing session data, |
|
| 835 | + return $this->cache_storage->add( |
|
| 836 | + EE_Session::session_id_prefix . $this->_sid, |
|
| 837 | + $session_data, |
|
| 838 | + $this->_lifespan |
|
| 839 | + ); |
|
| 840 | + } |
|
| 841 | 841 | |
| 842 | 842 | |
| 843 | 843 | |
@@ -930,18 +930,18 @@ discard block |
||
| 930 | 930 | |
| 931 | 931 | |
| 932 | 932 | |
| 933 | - /** |
|
| 934 | - * Clear EE_Session data |
|
| 935 | - * |
|
| 936 | - * @access public |
|
| 937 | - * @param string $class |
|
| 938 | - * @param string $function |
|
| 939 | - * @return void |
|
| 940 | - * @throws \EE_Error |
|
| 941 | - */ |
|
| 933 | + /** |
|
| 934 | + * Clear EE_Session data |
|
| 935 | + * |
|
| 936 | + * @access public |
|
| 937 | + * @param string $class |
|
| 938 | + * @param string $function |
|
| 939 | + * @return void |
|
| 940 | + * @throws \EE_Error |
|
| 941 | + */ |
|
| 942 | 942 | public function clear_session( $class = '', $function = '' ) { |
| 943 | 943 | //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>'; |
| 944 | - do_action( 'AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()' ); |
|
| 944 | + do_action( 'AHEE_log', __FILE__, __FUNCTION__, 'session cleared by : ' . $class . '::' . $function . '()' ); |
|
| 945 | 945 | $this->reset_cart(); |
| 946 | 946 | $this->reset_checkout(); |
| 947 | 947 | $this->reset_transaction(); |
@@ -1008,12 +1008,12 @@ discard block |
||
| 1008 | 1008 | |
| 1009 | 1009 | |
| 1010 | 1010 | |
| 1011 | - /** |
|
| 1012 | - * wp_loaded |
|
| 1013 | - * |
|
| 1014 | - * @access public |
|
| 1015 | - * @throws \EE_Error |
|
| 1016 | - */ |
|
| 1011 | + /** |
|
| 1012 | + * wp_loaded |
|
| 1013 | + * |
|
| 1014 | + * @access public |
|
| 1015 | + * @throws \EE_Error |
|
| 1016 | + */ |
|
| 1017 | 1017 | public function wp_loaded() { |
| 1018 | 1018 | if ( isset( EE_Registry::instance()->REQ ) && EE_Registry::instance()->REQ->is_set( 'clear_session' )) { |
| 1019 | 1019 | $this->clear_session( __CLASS__, __FUNCTION__ ); |
@@ -1022,12 +1022,12 @@ discard block |
||
| 1022 | 1022 | |
| 1023 | 1023 | |
| 1024 | 1024 | |
| 1025 | - /** |
|
| 1026 | - * Used to reset the entire object (for tests). |
|
| 1027 | - * |
|
| 1028 | - * @since 4.3.0 |
|
| 1029 | - * @throws \EE_Error |
|
| 1030 | - */ |
|
| 1025 | + /** |
|
| 1026 | + * Used to reset the entire object (for tests). |
|
| 1027 | + * |
|
| 1028 | + * @since 4.3.0 |
|
| 1029 | + * @throws \EE_Error |
|
| 1030 | + */ |
|
| 1031 | 1031 | public function reset_instance() { |
| 1032 | 1032 | $this->clear_session(); |
| 1033 | 1033 | self::$_instance = NULL; |
@@ -1035,29 +1035,29 @@ discard block |
||
| 1035 | 1035 | |
| 1036 | 1036 | |
| 1037 | 1037 | |
| 1038 | - public function configure_garbage_collection_filters() |
|
| 1039 | - { |
|
| 1040 | - // run old filter we had for controlling session cleanup |
|
| 1041 | - $expired_session_transient_delete_query_limit = absint( |
|
| 1042 | - apply_filters( |
|
| 1043 | - 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1044 | - 50 |
|
| 1045 | - ) |
|
| 1046 | - ); |
|
| 1047 | - // is there a value? or one that is different than the default 50 records? |
|
| 1048 | - if ($expired_session_transient_delete_query_limit === 0) { |
|
| 1049 | - // hook into TransientCacheStorage in case Session cleanup was turned off |
|
| 1050 | - add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
| 1051 | - } else if ($expired_session_transient_delete_query_limit !== 50) { |
|
| 1052 | - // or use that for the new transient cleanup query limit |
|
| 1053 | - add_filter( |
|
| 1054 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 1055 | - function () use ($expired_session_transient_delete_query_limit) { |
|
| 1056 | - return $expired_session_transient_delete_query_limit; |
|
| 1057 | - } |
|
| 1058 | - ); |
|
| 1059 | - } |
|
| 1060 | - } |
|
| 1038 | + public function configure_garbage_collection_filters() |
|
| 1039 | + { |
|
| 1040 | + // run old filter we had for controlling session cleanup |
|
| 1041 | + $expired_session_transient_delete_query_limit = absint( |
|
| 1042 | + apply_filters( |
|
| 1043 | + 'FHEE__EE_Session__garbage_collection___expired_session_transient_delete_query_limit', |
|
| 1044 | + 50 |
|
| 1045 | + ) |
|
| 1046 | + ); |
|
| 1047 | + // is there a value? or one that is different than the default 50 records? |
|
| 1048 | + if ($expired_session_transient_delete_query_limit === 0) { |
|
| 1049 | + // hook into TransientCacheStorage in case Session cleanup was turned off |
|
| 1050 | + add_filter('FHEE__TransientCacheStorage__transient_cleanup_schedule', '__return_zero'); |
|
| 1051 | + } else if ($expired_session_transient_delete_query_limit !== 50) { |
|
| 1052 | + // or use that for the new transient cleanup query limit |
|
| 1053 | + add_filter( |
|
| 1054 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 1055 | + function () use ($expired_session_transient_delete_query_limit) { |
|
| 1056 | + return $expired_session_transient_delete_query_limit; |
|
| 1057 | + } |
|
| 1058 | + ); |
|
| 1059 | + } |
|
| 1060 | + } |
|
| 1061 | 1061 | |
| 1062 | 1062 | |
| 1063 | 1063 | |
@@ -234,7 +234,7 @@ |
||
| 234 | 234 | /** |
| 235 | 235 | * delete multiple transients and remove tracking |
| 236 | 236 | * |
| 237 | - * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
| 237 | + * @param string[] $transient_keys [required] array of full or partial transient keys to be deleted |
|
| 238 | 238 | */ |
| 239 | 239 | public function deleteMany(array $transient_keys) |
| 240 | 240 | { |
@@ -1,10 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace EventEspresso\core\services\cache; |
| 3 | 3 | |
| 4 | -use EE_Error; |
|
| 5 | -use WP_Error; |
|
| 6 | -use wpdb; |
|
| 7 | - |
|
| 8 | 4 | defined('EVENT_ESPRESSO_VERSION') || exit; |
| 9 | 5 | |
| 10 | 6 | |
@@ -23,361 +23,361 @@ |
||
| 23 | 23 | class TransientCacheStorage implements CacheStorageInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * wp-option option_name for tracking transients |
|
| 28 | - * |
|
| 29 | - * @type string |
|
| 30 | - */ |
|
| 31 | - const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var int $current_time |
|
| 35 | - */ |
|
| 36 | - private $current_time = 0; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * how often to perform transient cleanup |
|
| 40 | - * |
|
| 41 | - * @var string $transient_cleanup_frequency |
|
| 42 | - */ |
|
| 43 | - private $transient_cleanup_frequency = 'hour'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * options for how often to perform transient cleanup |
|
| 47 | - * |
|
| 48 | - * @var array $transient_cleanup_frequency_options |
|
| 49 | - */ |
|
| 50 | - private $transient_cleanup_frequency_options = array(); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array $transients |
|
| 54 | - */ |
|
| 55 | - private $transients = array(); |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * TransientCacheStorage constructor. |
|
| 61 | - */ |
|
| 62 | - public function __construct() |
|
| 63 | - { |
|
| 64 | - $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
| 65 | - // round current time down to closest 5 minutes to simplify scheduling |
|
| 66 | - $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
| 67 | - $this->transients = (array)get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
| 68 | - if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
| 69 | - add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Sets how often transient cleanup occurs |
|
| 77 | - * |
|
| 78 | - * @return int |
|
| 79 | - */ |
|
| 80 | - private function setTransientCleanupFrequency() |
|
| 81 | - { |
|
| 82 | - // sets how often transients are cleaned up |
|
| 83 | - $this->transient_cleanup_frequency_options = apply_filters( |
|
| 84 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
| 85 | - array( |
|
| 86 | - 'off', |
|
| 87 | - '15-minutes', |
|
| 88 | - 'hour', |
|
| 89 | - '12-hours', |
|
| 90 | - 'day', |
|
| 91 | - ) |
|
| 92 | - ); |
|
| 93 | - $transient_cleanup_frequency = apply_filters( |
|
| 94 | - 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
| 95 | - 'hour' |
|
| 96 | - ); |
|
| 97 | - return in_array( |
|
| 98 | - $transient_cleanup_frequency, |
|
| 99 | - $this->transient_cleanup_frequency_options, |
|
| 100 | - true |
|
| 101 | - ) |
|
| 102 | - ? $transient_cleanup_frequency |
|
| 103 | - : 'hour'; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
| 110 | - * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
| 111 | - * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
| 112 | - * during the next scheduled cleanup after its expiration. |
|
| 113 | - * We also round off the current time timestamp to the closest 5 minutes |
|
| 114 | - * just to make the timestamps a little easier to round which helps with debugging. |
|
| 115 | - * |
|
| 116 | - * @param int $timestamp [required] |
|
| 117 | - * @param string $cleanup_frequency |
|
| 118 | - * @param bool $round_up |
|
| 119 | - * @return false|int |
|
| 120 | - */ |
|
| 121 | - private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
| 122 | - { |
|
| 123 | - $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
| 124 | - // in order to round the time to the closest xx minutes (or hours), |
|
| 125 | - // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
| 126 | - // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
| 127 | - // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
| 128 | - // and bump the hour, which could bump the day, which could bump the month, etc, |
|
| 129 | - // which would be bad because we don't always want to round up, |
|
| 130 | - // but when we do we can easily achieve that by simply adding the desired offset, |
|
| 131 | - $minutes = '00'; |
|
| 132 | - $hours = 'H'; |
|
| 133 | - switch ($cleanup_frequency) { |
|
| 134 | - case '5-minutes' : |
|
| 135 | - $minutes = floor((int)date('i', $timestamp) / 5) * 5; |
|
| 136 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
| 137 | - $offset = MINUTE_IN_SECONDS * 5; |
|
| 138 | - break; |
|
| 139 | - case '15-minutes' : |
|
| 140 | - $minutes = floor((int)date('i', $timestamp) / 15) * 15; |
|
| 141 | - $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
| 142 | - $offset = MINUTE_IN_SECONDS * 15; |
|
| 143 | - break; |
|
| 144 | - case '12-hours' : |
|
| 145 | - $hours = floor((int)date('H', $timestamp) / 12) * 12; |
|
| 146 | - $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
| 147 | - $offset = HOUR_IN_SECONDS * 12; |
|
| 148 | - break; |
|
| 149 | - case 'day' : |
|
| 150 | - $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
| 151 | - $offset = DAY_IN_SECONDS; |
|
| 152 | - break; |
|
| 153 | - case 'hour' : |
|
| 154 | - default : |
|
| 155 | - $offset = HOUR_IN_SECONDS; |
|
| 156 | - break; |
|
| 157 | - } |
|
| 158 | - $rounded_timestamp = strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
| 159 | - $rounded_timestamp += $round_up ? $offset : 0; |
|
| 160 | - return apply_filters( |
|
| 161 | - 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
| 162 | - $rounded_timestamp, |
|
| 163 | - $timestamp, |
|
| 164 | - $cleanup_frequency, |
|
| 165 | - $round_up |
|
| 166 | - ); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Saves supplied data to a transient |
|
| 173 | - * if an expiration is set, then it automatically schedules the transient for cleanup |
|
| 174 | - * |
|
| 175 | - * @param string $transient_key [required] |
|
| 176 | - * @param string $data [required] |
|
| 177 | - * @param int $expiration number of seconds until the cache expires |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function add($transient_key, $data, $expiration = 0) |
|
| 181 | - { |
|
| 182 | - $expiration = (int)abs($expiration); |
|
| 183 | - $saved = set_transient($transient_key, $data, $expiration); |
|
| 184 | - if ($saved && $expiration) { |
|
| 185 | - $this->scheduleTransientCleanup($transient_key, $expiration); |
|
| 186 | - } |
|
| 187 | - return $saved; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * retrieves transient data |
|
| 194 | - * automatically triggers early cache refresh for standard cache items |
|
| 195 | - * in order to avoid cache stampedes on busy sites. |
|
| 196 | - * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
| 197 | - * the $standard_cache parameter should be set to false when retrieving data |
|
| 198 | - * |
|
| 199 | - * @param string $transient_key [required] |
|
| 200 | - * @param bool $standard_cache |
|
| 201 | - * @return mixed|null |
|
| 202 | - */ |
|
| 203 | - public function get($transient_key, $standard_cache = true) |
|
| 204 | - { |
|
| 205 | - // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
| 206 | - // check if known cache expires within the next minute, |
|
| 207 | - // and if so, remove it from our tracking and and return nothing. |
|
| 208 | - // this should trigger the cache content to be regenerated during this request, |
|
| 209 | - // while allowing any following requests to still access the existing cache |
|
| 210 | - // until it gets replaced with the refreshed content |
|
| 211 | - if ( |
|
| 212 | - $standard_cache |
|
| 213 | - && isset($this->transients[$transient_key]) |
|
| 214 | - && $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
|
| 215 | - ) { |
|
| 216 | - unset($this->transients[$transient_key]); |
|
| 217 | - $this->updateTransients(); |
|
| 218 | - return null; |
|
| 219 | - } |
|
| 220 | - $content = get_transient($transient_key); |
|
| 221 | - return $content !== false ? $content : null; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * delete a single transient and remove tracking |
|
| 228 | - * |
|
| 229 | - * @param string $transient_key [required] full or partial transient key to be deleted |
|
| 230 | - */ |
|
| 231 | - public function delete($transient_key) |
|
| 232 | - { |
|
| 233 | - $this->deleteMany(array($transient_key)); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * delete multiple transients and remove tracking |
|
| 240 | - * |
|
| 241 | - * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
| 242 | - */ |
|
| 243 | - public function deleteMany(array $transient_keys) |
|
| 244 | - { |
|
| 245 | - $full_transient_keys = array(); |
|
| 246 | - foreach ($this->transients as $transient_key => $expiration) { |
|
| 247 | - foreach ($transient_keys as $transient_key_to_delete) { |
|
| 248 | - if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
| 249 | - $full_transient_keys[] = $transient_key; |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - if ($this->deleteTransientKeys($full_transient_keys)) { |
|
| 254 | - $this->updateTransients(); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * sorts transients numerically by timestamp |
|
| 262 | - * then saves the transient schedule to a WP option |
|
| 263 | - */ |
|
| 264 | - private function updateTransients() |
|
| 265 | - { |
|
| 266 | - asort($this->transients, SORT_NUMERIC); |
|
| 267 | - update_option( |
|
| 268 | - TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
| 269 | - $this->transients |
|
| 270 | - ); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * schedules a transient for cleanup by adding it to the transient tracking |
|
| 277 | - * |
|
| 278 | - * @param string $transient_key [required] |
|
| 279 | - * @param int $expiration [required] |
|
| 280 | - */ |
|
| 281 | - private function scheduleTransientCleanup($transient_key, $expiration) |
|
| 282 | - { |
|
| 283 | - // make sure a valid future timestamp is set |
|
| 284 | - $expiration += $expiration < time() ? time() : 0; |
|
| 285 | - // and round to the closest 15 minutes |
|
| 286 | - $expiration = $this->roundTimestamp($expiration); |
|
| 287 | - // save transients to clear using their ID as the key to avoid duplicates |
|
| 288 | - $this->transients[$transient_key] = $expiration; |
|
| 289 | - $this->updateTransients(); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Since our tracked transients are sorted by their timestamps |
|
| 296 | - * we can grab the first transient and see when it is scheduled for cleanup. |
|
| 297 | - * If that timestamp is less than or equal to the current time, |
|
| 298 | - * then cleanup is triggered |
|
| 299 | - */ |
|
| 300 | - public function checkTransientCleanupSchedule() |
|
| 301 | - { |
|
| 302 | - if (empty($this->transients)) { |
|
| 303 | - return; |
|
| 304 | - } |
|
| 305 | - // when do we run the next cleanup job? |
|
| 306 | - reset($this->transients); |
|
| 307 | - $next_scheduled_cleanup = current($this->transients); |
|
| 308 | - // if the next cleanup job is scheduled for the current hour |
|
| 309 | - if ($next_scheduled_cleanup <= $this->current_time) { |
|
| 310 | - if ($this->cleanupExpiredTransients()) { |
|
| 311 | - $this->updateTransients(); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * loops through the array of tracked transients, |
|
| 320 | - * compiles a list of those that have expired, and sends that list off for deletion. |
|
| 321 | - * Also removes any bad records from the transients array |
|
| 322 | - * |
|
| 323 | - * @return bool |
|
| 324 | - */ |
|
| 325 | - private function cleanupExpiredTransients() |
|
| 326 | - { |
|
| 327 | - $update = false; |
|
| 328 | - // filter the query limit. Set to 0 to turn off garbage collection |
|
| 329 | - $limit = (int)abs( |
|
| 330 | - apply_filters( |
|
| 331 | - 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 332 | - 50 |
|
| 333 | - ) |
|
| 334 | - ); |
|
| 335 | - // non-zero LIMIT means take out the trash |
|
| 336 | - if ($limit) { |
|
| 337 | - $transient_keys = array(); |
|
| 338 | - foreach ($this->transients as $transient_key => $expiration) { |
|
| 339 | - if ($expiration > $this->current_time) { |
|
| 340 | - continue; |
|
| 341 | - } |
|
| 342 | - if ( ! $expiration || ! $transient_key) { |
|
| 343 | - unset($this->transients[$transient_key]); |
|
| 344 | - $update = true; |
|
| 345 | - continue; |
|
| 346 | - } |
|
| 347 | - $transient_keys[] = $transient_key; |
|
| 348 | - } |
|
| 349 | - // delete expired keys, but maintain value of $update if nothing is deleted |
|
| 350 | - $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
| 351 | - do_action( 'FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
| 352 | - } |
|
| 353 | - return $update; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * calls delete_transient() on each transient key provided, up to the specified limit |
|
| 360 | - * |
|
| 361 | - * @param array $transient_keys [required] |
|
| 362 | - * @param int $limit |
|
| 363 | - * @return bool |
|
| 364 | - */ |
|
| 365 | - private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
| 366 | - { |
|
| 367 | - if (empty($transient_keys)) { |
|
| 368 | - return false; |
|
| 369 | - } |
|
| 370 | - $counter = 0; |
|
| 371 | - foreach ($transient_keys as $transient_key) { |
|
| 372 | - if($counter === $limit){ |
|
| 373 | - break; |
|
| 374 | - } |
|
| 375 | - delete_transient($transient_key); |
|
| 376 | - unset($this->transients[$transient_key]); |
|
| 377 | - $counter++; |
|
| 378 | - } |
|
| 379 | - return $counter > 0; |
|
| 380 | - } |
|
| 26 | + /** |
|
| 27 | + * wp-option option_name for tracking transients |
|
| 28 | + * |
|
| 29 | + * @type string |
|
| 30 | + */ |
|
| 31 | + const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var int $current_time |
|
| 35 | + */ |
|
| 36 | + private $current_time = 0; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * how often to perform transient cleanup |
|
| 40 | + * |
|
| 41 | + * @var string $transient_cleanup_frequency |
|
| 42 | + */ |
|
| 43 | + private $transient_cleanup_frequency = 'hour'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * options for how often to perform transient cleanup |
|
| 47 | + * |
|
| 48 | + * @var array $transient_cleanup_frequency_options |
|
| 49 | + */ |
|
| 50 | + private $transient_cleanup_frequency_options = array(); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array $transients |
|
| 54 | + */ |
|
| 55 | + private $transients = array(); |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * TransientCacheStorage constructor. |
|
| 61 | + */ |
|
| 62 | + public function __construct() |
|
| 63 | + { |
|
| 64 | + $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
|
| 65 | + // round current time down to closest 5 minutes to simplify scheduling |
|
| 66 | + $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
|
| 67 | + $this->transients = (array)get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
| 68 | + if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
|
| 69 | + add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Sets how often transient cleanup occurs |
|
| 77 | + * |
|
| 78 | + * @return int |
|
| 79 | + */ |
|
| 80 | + private function setTransientCleanupFrequency() |
|
| 81 | + { |
|
| 82 | + // sets how often transients are cleaned up |
|
| 83 | + $this->transient_cleanup_frequency_options = apply_filters( |
|
| 84 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
|
| 85 | + array( |
|
| 86 | + 'off', |
|
| 87 | + '15-minutes', |
|
| 88 | + 'hour', |
|
| 89 | + '12-hours', |
|
| 90 | + 'day', |
|
| 91 | + ) |
|
| 92 | + ); |
|
| 93 | + $transient_cleanup_frequency = apply_filters( |
|
| 94 | + 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
|
| 95 | + 'hour' |
|
| 96 | + ); |
|
| 97 | + return in_array( |
|
| 98 | + $transient_cleanup_frequency, |
|
| 99 | + $this->transient_cleanup_frequency_options, |
|
| 100 | + true |
|
| 101 | + ) |
|
| 102 | + ? $transient_cleanup_frequency |
|
| 103 | + : 'hour'; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * we need to be able to round timestamps off to match the set transient cleanup frequency |
|
| 110 | + * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
|
| 111 | + * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
|
| 112 | + * during the next scheduled cleanup after its expiration. |
|
| 113 | + * We also round off the current time timestamp to the closest 5 minutes |
|
| 114 | + * just to make the timestamps a little easier to round which helps with debugging. |
|
| 115 | + * |
|
| 116 | + * @param int $timestamp [required] |
|
| 117 | + * @param string $cleanup_frequency |
|
| 118 | + * @param bool $round_up |
|
| 119 | + * @return false|int |
|
| 120 | + */ |
|
| 121 | + private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
|
| 122 | + { |
|
| 123 | + $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
|
| 124 | + // in order to round the time to the closest xx minutes (or hours), |
|
| 125 | + // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
|
| 126 | + // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
|
| 127 | + // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
|
| 128 | + // and bump the hour, which could bump the day, which could bump the month, etc, |
|
| 129 | + // which would be bad because we don't always want to round up, |
|
| 130 | + // but when we do we can easily achieve that by simply adding the desired offset, |
|
| 131 | + $minutes = '00'; |
|
| 132 | + $hours = 'H'; |
|
| 133 | + switch ($cleanup_frequency) { |
|
| 134 | + case '5-minutes' : |
|
| 135 | + $minutes = floor((int)date('i', $timestamp) / 5) * 5; |
|
| 136 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
| 137 | + $offset = MINUTE_IN_SECONDS * 5; |
|
| 138 | + break; |
|
| 139 | + case '15-minutes' : |
|
| 140 | + $minutes = floor((int)date('i', $timestamp) / 15) * 15; |
|
| 141 | + $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
|
| 142 | + $offset = MINUTE_IN_SECONDS * 15; |
|
| 143 | + break; |
|
| 144 | + case '12-hours' : |
|
| 145 | + $hours = floor((int)date('H', $timestamp) / 12) * 12; |
|
| 146 | + $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
|
| 147 | + $offset = HOUR_IN_SECONDS * 12; |
|
| 148 | + break; |
|
| 149 | + case 'day' : |
|
| 150 | + $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
|
| 151 | + $offset = DAY_IN_SECONDS; |
|
| 152 | + break; |
|
| 153 | + case 'hour' : |
|
| 154 | + default : |
|
| 155 | + $offset = HOUR_IN_SECONDS; |
|
| 156 | + break; |
|
| 157 | + } |
|
| 158 | + $rounded_timestamp = strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
|
| 159 | + $rounded_timestamp += $round_up ? $offset : 0; |
|
| 160 | + return apply_filters( |
|
| 161 | + 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
|
| 162 | + $rounded_timestamp, |
|
| 163 | + $timestamp, |
|
| 164 | + $cleanup_frequency, |
|
| 165 | + $round_up |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Saves supplied data to a transient |
|
| 173 | + * if an expiration is set, then it automatically schedules the transient for cleanup |
|
| 174 | + * |
|
| 175 | + * @param string $transient_key [required] |
|
| 176 | + * @param string $data [required] |
|
| 177 | + * @param int $expiration number of seconds until the cache expires |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function add($transient_key, $data, $expiration = 0) |
|
| 181 | + { |
|
| 182 | + $expiration = (int)abs($expiration); |
|
| 183 | + $saved = set_transient($transient_key, $data, $expiration); |
|
| 184 | + if ($saved && $expiration) { |
|
| 185 | + $this->scheduleTransientCleanup($transient_key, $expiration); |
|
| 186 | + } |
|
| 187 | + return $saved; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * retrieves transient data |
|
| 194 | + * automatically triggers early cache refresh for standard cache items |
|
| 195 | + * in order to avoid cache stampedes on busy sites. |
|
| 196 | + * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
|
| 197 | + * the $standard_cache parameter should be set to false when retrieving data |
|
| 198 | + * |
|
| 199 | + * @param string $transient_key [required] |
|
| 200 | + * @param bool $standard_cache |
|
| 201 | + * @return mixed|null |
|
| 202 | + */ |
|
| 203 | + public function get($transient_key, $standard_cache = true) |
|
| 204 | + { |
|
| 205 | + // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
|
| 206 | + // check if known cache expires within the next minute, |
|
| 207 | + // and if so, remove it from our tracking and and return nothing. |
|
| 208 | + // this should trigger the cache content to be regenerated during this request, |
|
| 209 | + // while allowing any following requests to still access the existing cache |
|
| 210 | + // until it gets replaced with the refreshed content |
|
| 211 | + if ( |
|
| 212 | + $standard_cache |
|
| 213 | + && isset($this->transients[$transient_key]) |
|
| 214 | + && $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
|
| 215 | + ) { |
|
| 216 | + unset($this->transients[$transient_key]); |
|
| 217 | + $this->updateTransients(); |
|
| 218 | + return null; |
|
| 219 | + } |
|
| 220 | + $content = get_transient($transient_key); |
|
| 221 | + return $content !== false ? $content : null; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * delete a single transient and remove tracking |
|
| 228 | + * |
|
| 229 | + * @param string $transient_key [required] full or partial transient key to be deleted |
|
| 230 | + */ |
|
| 231 | + public function delete($transient_key) |
|
| 232 | + { |
|
| 233 | + $this->deleteMany(array($transient_key)); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * delete multiple transients and remove tracking |
|
| 240 | + * |
|
| 241 | + * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
|
| 242 | + */ |
|
| 243 | + public function deleteMany(array $transient_keys) |
|
| 244 | + { |
|
| 245 | + $full_transient_keys = array(); |
|
| 246 | + foreach ($this->transients as $transient_key => $expiration) { |
|
| 247 | + foreach ($transient_keys as $transient_key_to_delete) { |
|
| 248 | + if (strpos($transient_key, $transient_key_to_delete) !== false) { |
|
| 249 | + $full_transient_keys[] = $transient_key; |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + if ($this->deleteTransientKeys($full_transient_keys)) { |
|
| 254 | + $this->updateTransients(); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * sorts transients numerically by timestamp |
|
| 262 | + * then saves the transient schedule to a WP option |
|
| 263 | + */ |
|
| 264 | + private function updateTransients() |
|
| 265 | + { |
|
| 266 | + asort($this->transients, SORT_NUMERIC); |
|
| 267 | + update_option( |
|
| 268 | + TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, |
|
| 269 | + $this->transients |
|
| 270 | + ); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * schedules a transient for cleanup by adding it to the transient tracking |
|
| 277 | + * |
|
| 278 | + * @param string $transient_key [required] |
|
| 279 | + * @param int $expiration [required] |
|
| 280 | + */ |
|
| 281 | + private function scheduleTransientCleanup($transient_key, $expiration) |
|
| 282 | + { |
|
| 283 | + // make sure a valid future timestamp is set |
|
| 284 | + $expiration += $expiration < time() ? time() : 0; |
|
| 285 | + // and round to the closest 15 minutes |
|
| 286 | + $expiration = $this->roundTimestamp($expiration); |
|
| 287 | + // save transients to clear using their ID as the key to avoid duplicates |
|
| 288 | + $this->transients[$transient_key] = $expiration; |
|
| 289 | + $this->updateTransients(); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Since our tracked transients are sorted by their timestamps |
|
| 296 | + * we can grab the first transient and see when it is scheduled for cleanup. |
|
| 297 | + * If that timestamp is less than or equal to the current time, |
|
| 298 | + * then cleanup is triggered |
|
| 299 | + */ |
|
| 300 | + public function checkTransientCleanupSchedule() |
|
| 301 | + { |
|
| 302 | + if (empty($this->transients)) { |
|
| 303 | + return; |
|
| 304 | + } |
|
| 305 | + // when do we run the next cleanup job? |
|
| 306 | + reset($this->transients); |
|
| 307 | + $next_scheduled_cleanup = current($this->transients); |
|
| 308 | + // if the next cleanup job is scheduled for the current hour |
|
| 309 | + if ($next_scheduled_cleanup <= $this->current_time) { |
|
| 310 | + if ($this->cleanupExpiredTransients()) { |
|
| 311 | + $this->updateTransients(); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * loops through the array of tracked transients, |
|
| 320 | + * compiles a list of those that have expired, and sends that list off for deletion. |
|
| 321 | + * Also removes any bad records from the transients array |
|
| 322 | + * |
|
| 323 | + * @return bool |
|
| 324 | + */ |
|
| 325 | + private function cleanupExpiredTransients() |
|
| 326 | + { |
|
| 327 | + $update = false; |
|
| 328 | + // filter the query limit. Set to 0 to turn off garbage collection |
|
| 329 | + $limit = (int)abs( |
|
| 330 | + apply_filters( |
|
| 331 | + 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
|
| 332 | + 50 |
|
| 333 | + ) |
|
| 334 | + ); |
|
| 335 | + // non-zero LIMIT means take out the trash |
|
| 336 | + if ($limit) { |
|
| 337 | + $transient_keys = array(); |
|
| 338 | + foreach ($this->transients as $transient_key => $expiration) { |
|
| 339 | + if ($expiration > $this->current_time) { |
|
| 340 | + continue; |
|
| 341 | + } |
|
| 342 | + if ( ! $expiration || ! $transient_key) { |
|
| 343 | + unset($this->transients[$transient_key]); |
|
| 344 | + $update = true; |
|
| 345 | + continue; |
|
| 346 | + } |
|
| 347 | + $transient_keys[] = $transient_key; |
|
| 348 | + } |
|
| 349 | + // delete expired keys, but maintain value of $update if nothing is deleted |
|
| 350 | + $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
|
| 351 | + do_action( 'FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
| 352 | + } |
|
| 353 | + return $update; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * calls delete_transient() on each transient key provided, up to the specified limit |
|
| 360 | + * |
|
| 361 | + * @param array $transient_keys [required] |
|
| 362 | + * @param int $limit |
|
| 363 | + * @return bool |
|
| 364 | + */ |
|
| 365 | + private function deleteTransientKeys(array $transient_keys, $limit = 50) |
|
| 366 | + { |
|
| 367 | + if (empty($transient_keys)) { |
|
| 368 | + return false; |
|
| 369 | + } |
|
| 370 | + $counter = 0; |
|
| 371 | + foreach ($transient_keys as $transient_key) { |
|
| 372 | + if($counter === $limit){ |
|
| 373 | + break; |
|
| 374 | + } |
|
| 375 | + delete_transient($transient_key); |
|
| 376 | + unset($this->transients[$transient_key]); |
|
| 377 | + $counter++; |
|
| 378 | + } |
|
| 379 | + return $counter > 0; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | 382 | |
| 383 | 383 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
| 65 | 65 | // round current time down to closest 5 minutes to simplify scheduling |
| 66 | 66 | $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
| 67 | - $this->transients = (array)get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
| 67 | + $this->transients = (array) get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
|
| 68 | 68 | if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
| 69 | 69 | add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
| 70 | 70 | } |
@@ -132,17 +132,17 @@ discard block |
||
| 132 | 132 | $hours = 'H'; |
| 133 | 133 | switch ($cleanup_frequency) { |
| 134 | 134 | case '5-minutes' : |
| 135 | - $minutes = floor((int)date('i', $timestamp) / 5) * 5; |
|
| 135 | + $minutes = floor((int) date('i', $timestamp) / 5) * 5; |
|
| 136 | 136 | $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
| 137 | 137 | $offset = MINUTE_IN_SECONDS * 5; |
| 138 | 138 | break; |
| 139 | 139 | case '15-minutes' : |
| 140 | - $minutes = floor((int)date('i', $timestamp) / 15) * 15; |
|
| 140 | + $minutes = floor((int) date('i', $timestamp) / 15) * 15; |
|
| 141 | 141 | $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
| 142 | 142 | $offset = MINUTE_IN_SECONDS * 15; |
| 143 | 143 | break; |
| 144 | 144 | case '12-hours' : |
| 145 | - $hours = floor((int)date('H', $timestamp) / 12) * 12; |
|
| 145 | + $hours = floor((int) date('H', $timestamp) / 12) * 12; |
|
| 146 | 146 | $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
| 147 | 147 | $offset = HOUR_IN_SECONDS * 12; |
| 148 | 148 | break; |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | */ |
| 180 | 180 | public function add($transient_key, $data, $expiration = 0) |
| 181 | 181 | { |
| 182 | - $expiration = (int)abs($expiration); |
|
| 182 | + $expiration = (int) abs($expiration); |
|
| 183 | 183 | $saved = set_transient($transient_key, $data, $expiration); |
| 184 | 184 | if ($saved && $expiration) { |
| 185 | 185 | $this->scheduleTransientCleanup($transient_key, $expiration); |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | { |
| 327 | 327 | $update = false; |
| 328 | 328 | // filter the query limit. Set to 0 to turn off garbage collection |
| 329 | - $limit = (int)abs( |
|
| 329 | + $limit = (int) abs( |
|
| 330 | 330 | apply_filters( |
| 331 | 331 | 'FHEE__TransientCacheStorage__clearExpiredTransients__limit', |
| 332 | 332 | 50 |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | } |
| 349 | 349 | // delete expired keys, but maintain value of $update if nothing is deleted |
| 350 | 350 | $update = $this->deleteTransientKeys($transient_keys, $limit) ? true : $update; |
| 351 | - do_action( 'FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
| 351 | + do_action('FHEE__TransientCacheStorage__clearExpiredTransients__end', $this); |
|
| 352 | 352 | } |
| 353 | 353 | return $update; |
| 354 | 354 | } |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | } |
| 370 | 370 | $counter = 0; |
| 371 | 371 | foreach ($transient_keys as $transient_key) { |
| 372 | - if($counter === $limit){ |
|
| 372 | + if ($counter === $limit) { |
|
| 373 | 373 | break; |
| 374 | 374 | } |
| 375 | 375 | delete_transient($transient_key); |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | use OutOfBoundsException; |
| 14 | 14 | |
| 15 | 15 | if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
| 16 | - exit('No direct script access allowed'); |
|
| 16 | + exit('No direct script access allowed'); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -32,502 +32,502 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * This was the best coffee related name I could think of to represent class name "aliases" |
|
| 37 | - * So classes can be found via an alias identifier, |
|
| 38 | - * that is revealed when it is run through... the filters... eh? get it? |
|
| 39 | - * |
|
| 40 | - * @var array $filters |
|
| 41 | - */ |
|
| 42 | - private $filters = array(); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * These are the classes that will actually build the objects (to order of course) |
|
| 46 | - * |
|
| 47 | - * @var array $coffee_makers |
|
| 48 | - */ |
|
| 49 | - private $coffee_makers = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * where the instantiated "singleton" objects are stored |
|
| 53 | - * |
|
| 54 | - * @var CollectionInterface $carafe |
|
| 55 | - */ |
|
| 56 | - private $carafe; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * collection of Recipes that instruct us how to brew objects |
|
| 60 | - * |
|
| 61 | - * @var CollectionInterface $recipes |
|
| 62 | - */ |
|
| 63 | - private $recipes; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * collection of closures for brewing objects |
|
| 67 | - * |
|
| 68 | - * @var CollectionInterface $reservoir |
|
| 69 | - */ |
|
| 70 | - private $reservoir; |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * CoffeeShop constructor |
|
| 76 | - */ |
|
| 77 | - public function __construct() |
|
| 78 | - { |
|
| 79 | - // array for storing class aliases |
|
| 80 | - $this->filters = array(); |
|
| 81 | - // create collection for storing shared services |
|
| 82 | - $this->carafe = new LooseCollection( '' ); |
|
| 83 | - // create collection for storing recipes that tell how to build services and entities |
|
| 84 | - $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
| 85 | - // create collection for storing closures for constructing new entities |
|
| 86 | - $this->reservoir = new Collection('Closure'); |
|
| 87 | - // create collection for storing the generators that build our services and entity closures |
|
| 88 | - $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Returns true if the container can return an entry for the given identifier. |
|
| 95 | - * Returns false otherwise. |
|
| 96 | - * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
| 97 | - * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
| 98 | - * |
|
| 99 | - * @param string $identifier Identifier of the entry to look for. |
|
| 100 | - * Typically a Fully Qualified Class Name |
|
| 101 | - * @return boolean |
|
| 102 | - */ |
|
| 103 | - public function has($identifier) |
|
| 104 | - { |
|
| 105 | - $identifier = $this->filterIdentifier($identifier); |
|
| 106 | - return $this->carafe->has($identifier); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * finds a previously brewed (SHARED) service and returns it |
|
| 113 | - * |
|
| 114 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
| 115 | - * Typically a Fully Qualified Class Name |
|
| 116 | - * @return mixed |
|
| 117 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 118 | - */ |
|
| 119 | - public function get($identifier) |
|
| 120 | - { |
|
| 121 | - $identifier = $this->filterIdentifier($identifier); |
|
| 122 | - if ($this->carafe->has($identifier)) { |
|
| 123 | - return $this->carafe->get($identifier); |
|
| 124 | - } |
|
| 125 | - throw new ServiceNotFoundException($identifier); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * returns an instance of the requested entity type using the supplied arguments. |
|
| 132 | - * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
| 133 | - * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
| 134 | - * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
| 135 | - * then a new entity will be instantiated from the closure and returned. |
|
| 136 | - * If a closure does not exist, then one will be built and added to the reservoir |
|
| 137 | - * before instantiating the requested entity. |
|
| 138 | - * |
|
| 139 | - * @param string $identifier Identifier for the entity class to be constructed. |
|
| 140 | - * Typically a Fully Qualified Class Name |
|
| 141 | - * @param array $arguments an array of arguments to be passed to the entity constructor |
|
| 142 | - * @param string $type |
|
| 143 | - * @return mixed |
|
| 144 | - * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 145 | - */ |
|
| 146 | - public function brew($identifier, $arguments = array(), $type = '') |
|
| 147 | - { |
|
| 148 | - // resolve any class aliases that may exist |
|
| 149 | - $identifier = $this->filterIdentifier($identifier); |
|
| 150 | - try { |
|
| 151 | - // is a shared service being requested? |
|
| 152 | - if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
| 153 | - // if a shared service was requested and an instance is in the carafe, then return it |
|
| 154 | - return $this->get($identifier); |
|
| 155 | - } |
|
| 156 | - } catch (ServiceNotFoundException $e) { |
|
| 157 | - // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
| 158 | - // and instead, attempt to build whatever was requested |
|
| 159 | - } |
|
| 160 | - $brewed = false; |
|
| 161 | - // if the reservoir doesn't have a closure already for the requested identifier, |
|
| 162 | - // then neither a shared service nor a closure for making entities has been built yet |
|
| 163 | - if ( ! $this->reservoir->has($identifier)) { |
|
| 164 | - // so let's brew something up and add it to the proper collection |
|
| 165 | - $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
| 166 | - } |
|
| 167 | - // was the brewed item a callable factory function ? |
|
| 168 | - if (is_callable($brewed)) { |
|
| 169 | - // then instantiate a new entity from the cached closure |
|
| 170 | - $entity = $brewed($arguments); |
|
| 171 | - } else if ($brewed) { |
|
| 172 | - // requested object was a shared entity, so attempt to get it from the carafe again |
|
| 173 | - // because if it wasn't there before, then it should have just been brewed and added, |
|
| 174 | - // but if it still isn't there, then this time |
|
| 175 | - // the thrown ServiceNotFoundException will not be caught |
|
| 176 | - $entity = $this->get($identifier); |
|
| 177 | - } else { |
|
| 178 | - // if identifier is for a non-shared entity, |
|
| 179 | - // then either a cached closure already existed, or was just brewed |
|
| 180 | - $closure = $this->reservoir->get($identifier); |
|
| 181 | - $entity = $closure($arguments); |
|
| 182 | - } |
|
| 183 | - return $entity; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * @param CoffeeMakerInterface $coffee_maker |
|
| 190 | - * @param string $type |
|
| 191 | - * @return bool |
|
| 192 | - */ |
|
| 193 | - public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
| 194 | - { |
|
| 195 | - $type = CoffeeMaker::validateType($type); |
|
| 196 | - return $this->coffee_makers->add($coffee_maker, $type); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * @param string $identifier |
|
| 203 | - * @param callable $closure |
|
| 204 | - * @return callable|null |
|
| 205 | - */ |
|
| 206 | - public function addClosure($identifier, $closure) |
|
| 207 | - { |
|
| 208 | - if ( ! is_callable($closure)) { |
|
| 209 | - throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
| 210 | - } |
|
| 211 | - $identifier = $this->processIdentifier($identifier); |
|
| 212 | - if ($this->reservoir->add($closure, $identifier)) { |
|
| 213 | - return $closure; |
|
| 214 | - } |
|
| 215 | - return null; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @param string $identifier |
|
| 222 | - * @return boolean |
|
| 223 | - */ |
|
| 224 | - public function removeClosure($identifier) |
|
| 225 | - { |
|
| 226 | - $identifier = $this->processIdentifier($identifier); |
|
| 227 | - if ($this->reservoir->has($identifier)) { |
|
| 228 | - $this->reservoir->remove($this->reservoir->get($identifier)); |
|
| 229 | - if ( ! $this->reservoir->has($identifier)) { |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
| 240 | - * Typically a Fully Qualified Class Name |
|
| 241 | - * @param mixed $service |
|
| 242 | - * @return bool |
|
| 243 | - */ |
|
| 244 | - public function addService($identifier, $service) |
|
| 245 | - { |
|
| 246 | - $identifier = $this->processIdentifier($identifier); |
|
| 247 | - $service = $this->validateService($identifier, $service); |
|
| 248 | - return $this->carafe->add($service, $identifier); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * @param string $identifier |
|
| 255 | - * @return boolean |
|
| 256 | - */ |
|
| 257 | - public function removeService($identifier) |
|
| 258 | - { |
|
| 259 | - $identifier = $this->processIdentifier($identifier); |
|
| 260 | - if ($this->carafe->has($identifier)) { |
|
| 261 | - $this->carafe->remove($this->carafe->get($identifier)); |
|
| 262 | - if ( ! $this->carafe->has($identifier)) { |
|
| 263 | - return true; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - return false; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Adds instructions on how to brew objects |
|
| 273 | - * |
|
| 274 | - * @param RecipeInterface $recipe |
|
| 275 | - * @return mixed |
|
| 276 | - */ |
|
| 277 | - public function addRecipe(RecipeInterface $recipe) |
|
| 278 | - { |
|
| 279 | - $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
| 280 | - $identifier = $this->processIdentifier($recipe->identifier()); |
|
| 281 | - return $this->recipes->add($recipe, $identifier); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @param string $identifier The Recipe's identifier |
|
| 288 | - * @return boolean |
|
| 289 | - */ |
|
| 290 | - public function removeRecipe($identifier) |
|
| 291 | - { |
|
| 292 | - $identifier = $this->processIdentifier($identifier); |
|
| 293 | - if ($this->recipes->has($identifier)) { |
|
| 294 | - $this->recipes->remove( |
|
| 295 | - $this->recipes->get($identifier) |
|
| 296 | - ); |
|
| 297 | - if ( ! $this->recipes->has($identifier)) { |
|
| 298 | - return true; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - return false; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Get instructions on how to brew objects |
|
| 308 | - * |
|
| 309 | - * @param string $identifier Identifier for the entity class that the recipe applies to |
|
| 310 | - * Typically a Fully Qualified Class Name |
|
| 311 | - * @param string $type |
|
| 312 | - * @return RecipeInterface |
|
| 313 | - */ |
|
| 314 | - public function getRecipe($identifier, $type = '') |
|
| 315 | - { |
|
| 316 | - $identifier = $this->processIdentifier($identifier); |
|
| 317 | - if ($this->recipes->has($identifier)) { |
|
| 318 | - return $this->recipes->get($identifier); |
|
| 319 | - } |
|
| 320 | - $default_recipes = $this->getDefaultRecipes(); |
|
| 321 | - $matches = array(); |
|
| 322 | - foreach ($default_recipes as $wildcard => $default_recipe) { |
|
| 323 | - // is the wildcard recipe prefix in the identifier ? |
|
| 324 | - if (strpos($identifier, $wildcard) !== false) { |
|
| 325 | - // track matches and use the number of wildcard characters matched for the key |
|
| 326 | - $matches[strlen($wildcard)] = $default_recipe; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - if (count($matches) > 0) { |
|
| 330 | - // sort our recipes by the number of wildcard characters matched |
|
| 331 | - ksort($matches); |
|
| 332 | - // then grab the last recipe form the list, since it had the most matching characters |
|
| 333 | - $match = array_pop($matches); |
|
| 334 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 335 | - return $this->copyDefaultRecipe($match, $identifier, $type); |
|
| 336 | - } |
|
| 337 | - if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
| 338 | - // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 339 | - return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
| 340 | - } |
|
| 341 | - throw new OutOfBoundsException( |
|
| 342 | - sprintf( |
|
| 343 | - __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
| 344 | - $identifier |
|
| 345 | - ) |
|
| 346 | - ); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * adds class name aliases to list of filters |
|
| 353 | - * |
|
| 354 | - * @param string $identifier Identifier for the entity class that the alias applies to |
|
| 355 | - * Typically a Fully Qualified Class Name |
|
| 356 | - * @param array|string $aliases |
|
| 357 | - * @return void |
|
| 358 | - * @throws InvalidIdentifierException |
|
| 359 | - */ |
|
| 360 | - public function addAliases($identifier, $aliases) |
|
| 361 | - { |
|
| 362 | - if (empty($aliases)) { |
|
| 363 | - return; |
|
| 364 | - } |
|
| 365 | - $identifier = $this->processIdentifier($identifier); |
|
| 366 | - foreach ((array)$aliases as $alias) { |
|
| 367 | - $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * Adds a service to one of the internal collections |
|
| 375 | - * |
|
| 376 | - * @param $identifier |
|
| 377 | - * @param array $arguments |
|
| 378 | - * @param string $type |
|
| 379 | - * @return mixed |
|
| 380 | - * @throws ServiceExistsException |
|
| 381 | - */ |
|
| 382 | - private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
| 383 | - { |
|
| 384 | - if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
| 385 | - throw new ServiceExistsException($identifier); |
|
| 386 | - } |
|
| 387 | - $identifier = $this->filterIdentifier($identifier); |
|
| 388 | - $recipe = $this->getRecipe($identifier, $type); |
|
| 389 | - $type = ! empty($type) ? $type : $recipe->type(); |
|
| 390 | - $coffee_maker = $this->getCoffeeMaker($type); |
|
| 391 | - return $coffee_maker->brew($recipe, $arguments); |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * filters alias identifiers to find the real class name |
|
| 398 | - * |
|
| 399 | - * @param string $identifier Identifier for the entity class that the filter applies to |
|
| 400 | - * Typically a Fully Qualified Class Name |
|
| 401 | - * @return string |
|
| 402 | - * @throws InvalidIdentifierException |
|
| 403 | - */ |
|
| 404 | - private function filterIdentifier($identifier) |
|
| 405 | - { |
|
| 406 | - $identifier = $this->processIdentifier($identifier); |
|
| 407 | - return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
| 408 | - ? $this->filters[$identifier] |
|
| 409 | - : $identifier; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * verifies and standardizes identifiers |
|
| 416 | - * |
|
| 417 | - * @param string $identifier Identifier for the entity class |
|
| 418 | - * Typically a Fully Qualified Class Name |
|
| 419 | - * @return string |
|
| 420 | - * @throws InvalidIdentifierException |
|
| 421 | - */ |
|
| 422 | - private function processIdentifier($identifier) |
|
| 423 | - { |
|
| 424 | - if ( ! is_string($identifier)) { |
|
| 425 | - throw new InvalidIdentifierException( |
|
| 426 | - is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 427 | - '\Fully\Qualified\ClassName' |
|
| 428 | - ); |
|
| 429 | - } |
|
| 430 | - return ltrim($identifier, '\\'); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @param string $type |
|
| 437 | - * @return CoffeeMakerInterface |
|
| 438 | - * @throws InvalidDataTypeException |
|
| 439 | - * @throws InvalidClassException |
|
| 440 | - */ |
|
| 441 | - private function getCoffeeMaker($type) |
|
| 442 | - { |
|
| 443 | - if ( ! $this->coffee_makers->has($type)) { |
|
| 444 | - throw new OutOfBoundsException( |
|
| 445 | - __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
| 446 | - ); |
|
| 447 | - } |
|
| 448 | - return $this->coffee_makers->get($type); |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Retrieves all recipes that use a wildcard "*" in their identifier |
|
| 455 | - * This allows recipes to be set up for handling |
|
| 456 | - * legacy classes that do not support PSR-4 autoloading. |
|
| 457 | - * for example: |
|
| 458 | - * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
| 459 | - * |
|
| 460 | - * @return array |
|
| 461 | - */ |
|
| 462 | - private function getDefaultRecipes() |
|
| 463 | - { |
|
| 464 | - $default_recipes = array(); |
|
| 465 | - $this->recipes->rewind(); |
|
| 466 | - while ($this->recipes->valid()) { |
|
| 467 | - $identifier = $this->recipes->getInfo(); |
|
| 468 | - // does this recipe use a wildcard ? (but is NOT the global default) |
|
| 469 | - if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
| 470 | - // strip the wildcard and use identifier as key |
|
| 471 | - $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
| 472 | - } |
|
| 473 | - $this->recipes->next(); |
|
| 474 | - } |
|
| 475 | - return $default_recipes; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * clones a default recipe and then copies details |
|
| 482 | - * from the incoming request to it so that it can be used |
|
| 483 | - * |
|
| 484 | - * @param RecipeInterface $default_recipe |
|
| 485 | - * @param string $identifier |
|
| 486 | - * @param string $type |
|
| 487 | - * @return RecipeInterface |
|
| 488 | - */ |
|
| 489 | - private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
| 490 | - { |
|
| 491 | - $recipe = clone $default_recipe; |
|
| 492 | - if ( ! empty($type)) { |
|
| 493 | - $recipe->setType($type); |
|
| 494 | - } |
|
| 495 | - // is this the base default recipe ? |
|
| 496 | - if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
| 497 | - $recipe->setIdentifier($identifier); |
|
| 498 | - $recipe->setFqcn($identifier); |
|
| 499 | - return $recipe; |
|
| 500 | - } |
|
| 501 | - $recipe->setIdentifier($identifier); |
|
| 502 | - foreach ($default_recipe->paths() as $path) { |
|
| 503 | - $path = str_replace('*', $identifier, $path); |
|
| 504 | - if (is_readable($path)) { |
|
| 505 | - $recipe->setPaths($path); |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - $recipe->setFqcn($identifier); |
|
| 509 | - return $recipe; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * @param string $identifier Identifier for the entity class that the service applies to |
|
| 516 | - * Typically a Fully Qualified Class Name |
|
| 517 | - * @param mixed $service |
|
| 518 | - * @return object |
|
| 519 | - * @throws InvalidServiceException |
|
| 520 | - */ |
|
| 521 | - private function validateService($identifier, $service) |
|
| 522 | - { |
|
| 523 | - if ( ! is_object($service)) { |
|
| 524 | - throw new InvalidServiceException( |
|
| 525 | - $identifier, |
|
| 526 | - $service |
|
| 527 | - ); |
|
| 528 | - } |
|
| 529 | - return $service; |
|
| 530 | - } |
|
| 35 | + /** |
|
| 36 | + * This was the best coffee related name I could think of to represent class name "aliases" |
|
| 37 | + * So classes can be found via an alias identifier, |
|
| 38 | + * that is revealed when it is run through... the filters... eh? get it? |
|
| 39 | + * |
|
| 40 | + * @var array $filters |
|
| 41 | + */ |
|
| 42 | + private $filters = array(); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * These are the classes that will actually build the objects (to order of course) |
|
| 46 | + * |
|
| 47 | + * @var array $coffee_makers |
|
| 48 | + */ |
|
| 49 | + private $coffee_makers = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * where the instantiated "singleton" objects are stored |
|
| 53 | + * |
|
| 54 | + * @var CollectionInterface $carafe |
|
| 55 | + */ |
|
| 56 | + private $carafe; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * collection of Recipes that instruct us how to brew objects |
|
| 60 | + * |
|
| 61 | + * @var CollectionInterface $recipes |
|
| 62 | + */ |
|
| 63 | + private $recipes; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * collection of closures for brewing objects |
|
| 67 | + * |
|
| 68 | + * @var CollectionInterface $reservoir |
|
| 69 | + */ |
|
| 70 | + private $reservoir; |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * CoffeeShop constructor |
|
| 76 | + */ |
|
| 77 | + public function __construct() |
|
| 78 | + { |
|
| 79 | + // array for storing class aliases |
|
| 80 | + $this->filters = array(); |
|
| 81 | + // create collection for storing shared services |
|
| 82 | + $this->carafe = new LooseCollection( '' ); |
|
| 83 | + // create collection for storing recipes that tell how to build services and entities |
|
| 84 | + $this->recipes = new Collection('EventEspresso\core\services\container\RecipeInterface'); |
|
| 85 | + // create collection for storing closures for constructing new entities |
|
| 86 | + $this->reservoir = new Collection('Closure'); |
|
| 87 | + // create collection for storing the generators that build our services and entity closures |
|
| 88 | + $this->coffee_makers = new Collection('EventEspresso\core\services\container\CoffeeMakerInterface'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Returns true if the container can return an entry for the given identifier. |
|
| 95 | + * Returns false otherwise. |
|
| 96 | + * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
|
| 97 | + * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
|
| 98 | + * |
|
| 99 | + * @param string $identifier Identifier of the entry to look for. |
|
| 100 | + * Typically a Fully Qualified Class Name |
|
| 101 | + * @return boolean |
|
| 102 | + */ |
|
| 103 | + public function has($identifier) |
|
| 104 | + { |
|
| 105 | + $identifier = $this->filterIdentifier($identifier); |
|
| 106 | + return $this->carafe->has($identifier); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * finds a previously brewed (SHARED) service and returns it |
|
| 113 | + * |
|
| 114 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
| 115 | + * Typically a Fully Qualified Class Name |
|
| 116 | + * @return mixed |
|
| 117 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 118 | + */ |
|
| 119 | + public function get($identifier) |
|
| 120 | + { |
|
| 121 | + $identifier = $this->filterIdentifier($identifier); |
|
| 122 | + if ($this->carafe->has($identifier)) { |
|
| 123 | + return $this->carafe->get($identifier); |
|
| 124 | + } |
|
| 125 | + throw new ServiceNotFoundException($identifier); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * returns an instance of the requested entity type using the supplied arguments. |
|
| 132 | + * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
|
| 133 | + * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
|
| 134 | + * If the request is for a new entity and a closure exists in the reservoir for creating it, |
|
| 135 | + * then a new entity will be instantiated from the closure and returned. |
|
| 136 | + * If a closure does not exist, then one will be built and added to the reservoir |
|
| 137 | + * before instantiating the requested entity. |
|
| 138 | + * |
|
| 139 | + * @param string $identifier Identifier for the entity class to be constructed. |
|
| 140 | + * Typically a Fully Qualified Class Name |
|
| 141 | + * @param array $arguments an array of arguments to be passed to the entity constructor |
|
| 142 | + * @param string $type |
|
| 143 | + * @return mixed |
|
| 144 | + * @throws ServiceNotFoundException No service was found for this identifier. |
|
| 145 | + */ |
|
| 146 | + public function brew($identifier, $arguments = array(), $type = '') |
|
| 147 | + { |
|
| 148 | + // resolve any class aliases that may exist |
|
| 149 | + $identifier = $this->filterIdentifier($identifier); |
|
| 150 | + try { |
|
| 151 | + // is a shared service being requested? |
|
| 152 | + if (empty($type) || $type === CoffeeMaker::BREW_SHARED) { |
|
| 153 | + // if a shared service was requested and an instance is in the carafe, then return it |
|
| 154 | + return $this->get($identifier); |
|
| 155 | + } |
|
| 156 | + } catch (ServiceNotFoundException $e) { |
|
| 157 | + // if not then we'll just catch the ServiceNotFoundException but not do anything just yet, |
|
| 158 | + // and instead, attempt to build whatever was requested |
|
| 159 | + } |
|
| 160 | + $brewed = false; |
|
| 161 | + // if the reservoir doesn't have a closure already for the requested identifier, |
|
| 162 | + // then neither a shared service nor a closure for making entities has been built yet |
|
| 163 | + if ( ! $this->reservoir->has($identifier)) { |
|
| 164 | + // so let's brew something up and add it to the proper collection |
|
| 165 | + $brewed = $this->makeCoffee($identifier, $arguments, $type); |
|
| 166 | + } |
|
| 167 | + // was the brewed item a callable factory function ? |
|
| 168 | + if (is_callable($brewed)) { |
|
| 169 | + // then instantiate a new entity from the cached closure |
|
| 170 | + $entity = $brewed($arguments); |
|
| 171 | + } else if ($brewed) { |
|
| 172 | + // requested object was a shared entity, so attempt to get it from the carafe again |
|
| 173 | + // because if it wasn't there before, then it should have just been brewed and added, |
|
| 174 | + // but if it still isn't there, then this time |
|
| 175 | + // the thrown ServiceNotFoundException will not be caught |
|
| 176 | + $entity = $this->get($identifier); |
|
| 177 | + } else { |
|
| 178 | + // if identifier is for a non-shared entity, |
|
| 179 | + // then either a cached closure already existed, or was just brewed |
|
| 180 | + $closure = $this->reservoir->get($identifier); |
|
| 181 | + $entity = $closure($arguments); |
|
| 182 | + } |
|
| 183 | + return $entity; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * @param CoffeeMakerInterface $coffee_maker |
|
| 190 | + * @param string $type |
|
| 191 | + * @return bool |
|
| 192 | + */ |
|
| 193 | + public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
|
| 194 | + { |
|
| 195 | + $type = CoffeeMaker::validateType($type); |
|
| 196 | + return $this->coffee_makers->add($coffee_maker, $type); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * @param string $identifier |
|
| 203 | + * @param callable $closure |
|
| 204 | + * @return callable|null |
|
| 205 | + */ |
|
| 206 | + public function addClosure($identifier, $closure) |
|
| 207 | + { |
|
| 208 | + if ( ! is_callable($closure)) { |
|
| 209 | + throw new InvalidDataTypeException('$closure', $closure, 'Closure'); |
|
| 210 | + } |
|
| 211 | + $identifier = $this->processIdentifier($identifier); |
|
| 212 | + if ($this->reservoir->add($closure, $identifier)) { |
|
| 213 | + return $closure; |
|
| 214 | + } |
|
| 215 | + return null; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @param string $identifier |
|
| 222 | + * @return boolean |
|
| 223 | + */ |
|
| 224 | + public function removeClosure($identifier) |
|
| 225 | + { |
|
| 226 | + $identifier = $this->processIdentifier($identifier); |
|
| 227 | + if ($this->reservoir->has($identifier)) { |
|
| 228 | + $this->reservoir->remove($this->reservoir->get($identifier)); |
|
| 229 | + if ( ! $this->reservoir->has($identifier)) { |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
| 240 | + * Typically a Fully Qualified Class Name |
|
| 241 | + * @param mixed $service |
|
| 242 | + * @return bool |
|
| 243 | + */ |
|
| 244 | + public function addService($identifier, $service) |
|
| 245 | + { |
|
| 246 | + $identifier = $this->processIdentifier($identifier); |
|
| 247 | + $service = $this->validateService($identifier, $service); |
|
| 248 | + return $this->carafe->add($service, $identifier); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * @param string $identifier |
|
| 255 | + * @return boolean |
|
| 256 | + */ |
|
| 257 | + public function removeService($identifier) |
|
| 258 | + { |
|
| 259 | + $identifier = $this->processIdentifier($identifier); |
|
| 260 | + if ($this->carafe->has($identifier)) { |
|
| 261 | + $this->carafe->remove($this->carafe->get($identifier)); |
|
| 262 | + if ( ! $this->carafe->has($identifier)) { |
|
| 263 | + return true; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + return false; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Adds instructions on how to brew objects |
|
| 273 | + * |
|
| 274 | + * @param RecipeInterface $recipe |
|
| 275 | + * @return mixed |
|
| 276 | + */ |
|
| 277 | + public function addRecipe(RecipeInterface $recipe) |
|
| 278 | + { |
|
| 279 | + $this->addAliases($recipe->identifier(), $recipe->filters()); |
|
| 280 | + $identifier = $this->processIdentifier($recipe->identifier()); |
|
| 281 | + return $this->recipes->add($recipe, $identifier); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @param string $identifier The Recipe's identifier |
|
| 288 | + * @return boolean |
|
| 289 | + */ |
|
| 290 | + public function removeRecipe($identifier) |
|
| 291 | + { |
|
| 292 | + $identifier = $this->processIdentifier($identifier); |
|
| 293 | + if ($this->recipes->has($identifier)) { |
|
| 294 | + $this->recipes->remove( |
|
| 295 | + $this->recipes->get($identifier) |
|
| 296 | + ); |
|
| 297 | + if ( ! $this->recipes->has($identifier)) { |
|
| 298 | + return true; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + return false; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Get instructions on how to brew objects |
|
| 308 | + * |
|
| 309 | + * @param string $identifier Identifier for the entity class that the recipe applies to |
|
| 310 | + * Typically a Fully Qualified Class Name |
|
| 311 | + * @param string $type |
|
| 312 | + * @return RecipeInterface |
|
| 313 | + */ |
|
| 314 | + public function getRecipe($identifier, $type = '') |
|
| 315 | + { |
|
| 316 | + $identifier = $this->processIdentifier($identifier); |
|
| 317 | + if ($this->recipes->has($identifier)) { |
|
| 318 | + return $this->recipes->get($identifier); |
|
| 319 | + } |
|
| 320 | + $default_recipes = $this->getDefaultRecipes(); |
|
| 321 | + $matches = array(); |
|
| 322 | + foreach ($default_recipes as $wildcard => $default_recipe) { |
|
| 323 | + // is the wildcard recipe prefix in the identifier ? |
|
| 324 | + if (strpos($identifier, $wildcard) !== false) { |
|
| 325 | + // track matches and use the number of wildcard characters matched for the key |
|
| 326 | + $matches[strlen($wildcard)] = $default_recipe; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + if (count($matches) > 0) { |
|
| 330 | + // sort our recipes by the number of wildcard characters matched |
|
| 331 | + ksort($matches); |
|
| 332 | + // then grab the last recipe form the list, since it had the most matching characters |
|
| 333 | + $match = array_pop($matches); |
|
| 334 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 335 | + return $this->copyDefaultRecipe($match, $identifier, $type); |
|
| 336 | + } |
|
| 337 | + if ($this->recipes->has(Recipe::DEFAULT_ID)) { |
|
| 338 | + // since we are using a default recipe, we need to set it's identifier and fqcn |
|
| 339 | + return $this->copyDefaultRecipe($this->recipes->get(Recipe::DEFAULT_ID), $identifier, $type); |
|
| 340 | + } |
|
| 341 | + throw new OutOfBoundsException( |
|
| 342 | + sprintf( |
|
| 343 | + __('Could not brew coffee because no recipes were found for class "%1$s".', 'event_espresso'), |
|
| 344 | + $identifier |
|
| 345 | + ) |
|
| 346 | + ); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * adds class name aliases to list of filters |
|
| 353 | + * |
|
| 354 | + * @param string $identifier Identifier for the entity class that the alias applies to |
|
| 355 | + * Typically a Fully Qualified Class Name |
|
| 356 | + * @param array|string $aliases |
|
| 357 | + * @return void |
|
| 358 | + * @throws InvalidIdentifierException |
|
| 359 | + */ |
|
| 360 | + public function addAliases($identifier, $aliases) |
|
| 361 | + { |
|
| 362 | + if (empty($aliases)) { |
|
| 363 | + return; |
|
| 364 | + } |
|
| 365 | + $identifier = $this->processIdentifier($identifier); |
|
| 366 | + foreach ((array)$aliases as $alias) { |
|
| 367 | + $this->filters[$this->processIdentifier($alias)] = $identifier; |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * Adds a service to one of the internal collections |
|
| 375 | + * |
|
| 376 | + * @param $identifier |
|
| 377 | + * @param array $arguments |
|
| 378 | + * @param string $type |
|
| 379 | + * @return mixed |
|
| 380 | + * @throws ServiceExistsException |
|
| 381 | + */ |
|
| 382 | + private function makeCoffee($identifier, $arguments = array(), $type = '') |
|
| 383 | + { |
|
| 384 | + if ((empty($type) || $type === CoffeeMaker::BREW_SHARED) && $this->has($identifier)) { |
|
| 385 | + throw new ServiceExistsException($identifier); |
|
| 386 | + } |
|
| 387 | + $identifier = $this->filterIdentifier($identifier); |
|
| 388 | + $recipe = $this->getRecipe($identifier, $type); |
|
| 389 | + $type = ! empty($type) ? $type : $recipe->type(); |
|
| 390 | + $coffee_maker = $this->getCoffeeMaker($type); |
|
| 391 | + return $coffee_maker->brew($recipe, $arguments); |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * filters alias identifiers to find the real class name |
|
| 398 | + * |
|
| 399 | + * @param string $identifier Identifier for the entity class that the filter applies to |
|
| 400 | + * Typically a Fully Qualified Class Name |
|
| 401 | + * @return string |
|
| 402 | + * @throws InvalidIdentifierException |
|
| 403 | + */ |
|
| 404 | + private function filterIdentifier($identifier) |
|
| 405 | + { |
|
| 406 | + $identifier = $this->processIdentifier($identifier); |
|
| 407 | + return isset($this->filters[$identifier]) && ! empty($this->filters[$identifier]) |
|
| 408 | + ? $this->filters[$identifier] |
|
| 409 | + : $identifier; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * verifies and standardizes identifiers |
|
| 416 | + * |
|
| 417 | + * @param string $identifier Identifier for the entity class |
|
| 418 | + * Typically a Fully Qualified Class Name |
|
| 419 | + * @return string |
|
| 420 | + * @throws InvalidIdentifierException |
|
| 421 | + */ |
|
| 422 | + private function processIdentifier($identifier) |
|
| 423 | + { |
|
| 424 | + if ( ! is_string($identifier)) { |
|
| 425 | + throw new InvalidIdentifierException( |
|
| 426 | + is_object($identifier) ? get_class($identifier) : gettype($identifier), |
|
| 427 | + '\Fully\Qualified\ClassName' |
|
| 428 | + ); |
|
| 429 | + } |
|
| 430 | + return ltrim($identifier, '\\'); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @param string $type |
|
| 437 | + * @return CoffeeMakerInterface |
|
| 438 | + * @throws InvalidDataTypeException |
|
| 439 | + * @throws InvalidClassException |
|
| 440 | + */ |
|
| 441 | + private function getCoffeeMaker($type) |
|
| 442 | + { |
|
| 443 | + if ( ! $this->coffee_makers->has($type)) { |
|
| 444 | + throw new OutOfBoundsException( |
|
| 445 | + __('The requested coffee maker is either missing or invalid.', 'event_espresso') |
|
| 446 | + ); |
|
| 447 | + } |
|
| 448 | + return $this->coffee_makers->get($type); |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Retrieves all recipes that use a wildcard "*" in their identifier |
|
| 455 | + * This allows recipes to be set up for handling |
|
| 456 | + * legacy classes that do not support PSR-4 autoloading. |
|
| 457 | + * for example: |
|
| 458 | + * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
|
| 459 | + * |
|
| 460 | + * @return array |
|
| 461 | + */ |
|
| 462 | + private function getDefaultRecipes() |
|
| 463 | + { |
|
| 464 | + $default_recipes = array(); |
|
| 465 | + $this->recipes->rewind(); |
|
| 466 | + while ($this->recipes->valid()) { |
|
| 467 | + $identifier = $this->recipes->getInfo(); |
|
| 468 | + // does this recipe use a wildcard ? (but is NOT the global default) |
|
| 469 | + if ($identifier !== Recipe::DEFAULT_ID && strpos($identifier, '*') !== false) { |
|
| 470 | + // strip the wildcard and use identifier as key |
|
| 471 | + $default_recipes[str_replace('*', '', $identifier)] = $this->recipes->current(); |
|
| 472 | + } |
|
| 473 | + $this->recipes->next(); |
|
| 474 | + } |
|
| 475 | + return $default_recipes; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * clones a default recipe and then copies details |
|
| 482 | + * from the incoming request to it so that it can be used |
|
| 483 | + * |
|
| 484 | + * @param RecipeInterface $default_recipe |
|
| 485 | + * @param string $identifier |
|
| 486 | + * @param string $type |
|
| 487 | + * @return RecipeInterface |
|
| 488 | + */ |
|
| 489 | + private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
|
| 490 | + { |
|
| 491 | + $recipe = clone $default_recipe; |
|
| 492 | + if ( ! empty($type)) { |
|
| 493 | + $recipe->setType($type); |
|
| 494 | + } |
|
| 495 | + // is this the base default recipe ? |
|
| 496 | + if ($default_recipe->identifier() === Recipe::DEFAULT_ID) { |
|
| 497 | + $recipe->setIdentifier($identifier); |
|
| 498 | + $recipe->setFqcn($identifier); |
|
| 499 | + return $recipe; |
|
| 500 | + } |
|
| 501 | + $recipe->setIdentifier($identifier); |
|
| 502 | + foreach ($default_recipe->paths() as $path) { |
|
| 503 | + $path = str_replace('*', $identifier, $path); |
|
| 504 | + if (is_readable($path)) { |
|
| 505 | + $recipe->setPaths($path); |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + $recipe->setFqcn($identifier); |
|
| 509 | + return $recipe; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * @param string $identifier Identifier for the entity class that the service applies to |
|
| 516 | + * Typically a Fully Qualified Class Name |
|
| 517 | + * @param mixed $service |
|
| 518 | + * @return object |
|
| 519 | + * @throws InvalidServiceException |
|
| 520 | + */ |
|
| 521 | + private function validateService($identifier, $service) |
|
| 522 | + { |
|
| 523 | + if ( ! is_object($service)) { |
|
| 524 | + throw new InvalidServiceException( |
|
| 525 | + $identifier, |
|
| 526 | + $service |
|
| 527 | + ); |
|
| 528 | + } |
|
| 529 | + return $service; |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | 532 | } |
| 533 | 533 | // End of file CoffeeShop.php |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | use EventEspresso\core\libraries\iframe_display\Iframe; |
| 5 | 5 | |
| 6 | 6 | if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
| 7 | - exit( 'No direct script access allowed' ); |
|
| 7 | + exit( 'No direct script access allowed' ); |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | |
@@ -21,57 +21,57 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * EventsArchiveIframe constructor. |
|
| 26 | - * |
|
| 27 | - * @param \EED_Events_Archive $EED_Events_Archive |
|
| 28 | - * @throws \DomainException |
|
| 29 | - */ |
|
| 30 | - public function __construct( $EED_Events_Archive ) |
|
| 31 | - { |
|
| 32 | - \EE_Registry::instance()->REQ->set_espresso_page( true ); |
|
| 33 | - add_filter('FHEE__EED_Events_Archive__event_list_iframe', '__return_true'); |
|
| 34 | - $EED_Events_Archive->event_list(); |
|
| 35 | - /** @var \EventEspresso\core\domain\entities\shortcodes\EspressoEvents $event_list */ |
|
| 36 | - $event_list = \EE_Registry::instance()->create('EventEspresso\core\domain\entities\shortcodes\EspressoEvents'); |
|
| 37 | - parent::__construct( |
|
| 38 | - esc_html__( 'Event List', 'event_espresso' ), |
|
| 39 | - $event_list->processShortcode() |
|
| 40 | - ); |
|
| 41 | - $this->addStylesheets( |
|
| 42 | - apply_filters( |
|
| 43 | - 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css', |
|
| 44 | - array( |
|
| 45 | - 'espresso_default' => is_readable( EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css' ) |
|
| 46 | - ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION |
|
| 47 | - : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
| 48 | - ), |
|
| 49 | - $this |
|
| 50 | - ) |
|
| 51 | - ); |
|
| 52 | - $this->addScripts( |
|
| 53 | - apply_filters( |
|
| 54 | - 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js', |
|
| 55 | - array( |
|
| 56 | - 'gmap_api' => sprintf( |
|
| 57 | - 'https://maps.googleapis.com/maps/api/js?key=%s', |
|
| 58 | - apply_filters( |
|
| 59 | - 'FHEE__EEH_Maps__espresso_google_maps_js__api_key', |
|
| 60 | - \EE_Registry::instance()->CFG->map_settings->google_map_api_key |
|
| 61 | - ) |
|
| 62 | - ), |
|
| 63 | - 'ee_gmap' => EE_HELPERS_ASSETS . 'ee_gmap.js?ver=1.0', |
|
| 64 | - ), |
|
| 65 | - $this |
|
| 66 | - ) |
|
| 67 | - ); |
|
| 68 | - $this->addLocalizedVars( |
|
| 69 | - array( |
|
| 70 | - 'ee_gmap' => \EEH_Maps::$gmap_vars, |
|
| 71 | - ), |
|
| 72 | - 'ee_gmap_vars' |
|
| 73 | - ); |
|
| 74 | - } |
|
| 24 | + /** |
|
| 25 | + * EventsArchiveIframe constructor. |
|
| 26 | + * |
|
| 27 | + * @param \EED_Events_Archive $EED_Events_Archive |
|
| 28 | + * @throws \DomainException |
|
| 29 | + */ |
|
| 30 | + public function __construct( $EED_Events_Archive ) |
|
| 31 | + { |
|
| 32 | + \EE_Registry::instance()->REQ->set_espresso_page( true ); |
|
| 33 | + add_filter('FHEE__EED_Events_Archive__event_list_iframe', '__return_true'); |
|
| 34 | + $EED_Events_Archive->event_list(); |
|
| 35 | + /** @var \EventEspresso\core\domain\entities\shortcodes\EspressoEvents $event_list */ |
|
| 36 | + $event_list = \EE_Registry::instance()->create('EventEspresso\core\domain\entities\shortcodes\EspressoEvents'); |
|
| 37 | + parent::__construct( |
|
| 38 | + esc_html__( 'Event List', 'event_espresso' ), |
|
| 39 | + $event_list->processShortcode() |
|
| 40 | + ); |
|
| 41 | + $this->addStylesheets( |
|
| 42 | + apply_filters( |
|
| 43 | + 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css', |
|
| 44 | + array( |
|
| 45 | + 'espresso_default' => is_readable( EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css' ) |
|
| 46 | + ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION |
|
| 47 | + : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
| 48 | + ), |
|
| 49 | + $this |
|
| 50 | + ) |
|
| 51 | + ); |
|
| 52 | + $this->addScripts( |
|
| 53 | + apply_filters( |
|
| 54 | + 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__js', |
|
| 55 | + array( |
|
| 56 | + 'gmap_api' => sprintf( |
|
| 57 | + 'https://maps.googleapis.com/maps/api/js?key=%s', |
|
| 58 | + apply_filters( |
|
| 59 | + 'FHEE__EEH_Maps__espresso_google_maps_js__api_key', |
|
| 60 | + \EE_Registry::instance()->CFG->map_settings->google_map_api_key |
|
| 61 | + ) |
|
| 62 | + ), |
|
| 63 | + 'ee_gmap' => EE_HELPERS_ASSETS . 'ee_gmap.js?ver=1.0', |
|
| 64 | + ), |
|
| 65 | + $this |
|
| 66 | + ) |
|
| 67 | + ); |
|
| 68 | + $this->addLocalizedVars( |
|
| 69 | + array( |
|
| 70 | + 'ee_gmap' => \EEH_Maps::$gmap_vars, |
|
| 71 | + ), |
|
| 72 | + 'ee_gmap_vars' |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | |
| 77 | 77 | |
@@ -3,8 +3,8 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | use EventEspresso\core\libraries\iframe_display\Iframe; |
| 5 | 5 | |
| 6 | -if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
| 7 | - exit( 'No direct script access allowed' ); |
|
| 6 | +if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
|
| 7 | + exit('No direct script access allowed'); |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | |
@@ -27,24 +27,24 @@ discard block |
||
| 27 | 27 | * @param \EED_Events_Archive $EED_Events_Archive |
| 28 | 28 | * @throws \DomainException |
| 29 | 29 | */ |
| 30 | - public function __construct( $EED_Events_Archive ) |
|
| 30 | + public function __construct($EED_Events_Archive) |
|
| 31 | 31 | { |
| 32 | - \EE_Registry::instance()->REQ->set_espresso_page( true ); |
|
| 32 | + \EE_Registry::instance()->REQ->set_espresso_page(true); |
|
| 33 | 33 | add_filter('FHEE__EED_Events_Archive__event_list_iframe', '__return_true'); |
| 34 | 34 | $EED_Events_Archive->event_list(); |
| 35 | 35 | /** @var \EventEspresso\core\domain\entities\shortcodes\EspressoEvents $event_list */ |
| 36 | 36 | $event_list = \EE_Registry::instance()->create('EventEspresso\core\domain\entities\shortcodes\EspressoEvents'); |
| 37 | 37 | parent::__construct( |
| 38 | - esc_html__( 'Event List', 'event_espresso' ), |
|
| 38 | + esc_html__('Event List', 'event_espresso'), |
|
| 39 | 39 | $event_list->processShortcode() |
| 40 | 40 | ); |
| 41 | 41 | $this->addStylesheets( |
| 42 | 42 | apply_filters( |
| 43 | 43 | 'FHEE__EventEspresso_modules_events_archive_EventsArchiveIframe__display__css', |
| 44 | 44 | array( |
| 45 | - 'espresso_default' => is_readable( EVENT_ESPRESSO_UPLOAD_DIR . 'css/style.css' ) |
|
| 46 | - ? EVENT_ESPRESSO_UPLOAD_DIR . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION |
|
| 47 | - : EE_GLOBAL_ASSETS_URL . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION, |
|
| 45 | + 'espresso_default' => is_readable(EVENT_ESPRESSO_UPLOAD_DIR.'css/style.css') |
|
| 46 | + ? EVENT_ESPRESSO_UPLOAD_DIR.'css/espresso_default.css?ver='.EVENT_ESPRESSO_VERSION |
|
| 47 | + : EE_GLOBAL_ASSETS_URL.'css/espresso_default.css?ver='.EVENT_ESPRESSO_VERSION, |
|
| 48 | 48 | ), |
| 49 | 49 | $this |
| 50 | 50 | ) |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | \EE_Registry::instance()->CFG->map_settings->google_map_api_key |
| 61 | 61 | ) |
| 62 | 62 | ), |
| 63 | - 'ee_gmap' => EE_HELPERS_ASSETS . 'ee_gmap.js?ver=1.0', |
|
| 63 | + 'ee_gmap' => EE_HELPERS_ASSETS.'ee_gmap.js?ver=1.0', |
|
| 64 | 64 | ), |
| 65 | 65 | $this |
| 66 | 66 | ) |
@@ -16,1374 +16,1374 @@ |
||
| 16 | 16 | class EE_Registry |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * EE_Registry Object |
|
| 21 | - * |
|
| 22 | - * @var EE_Registry $_instance |
|
| 23 | - * @access private |
|
| 24 | - */ |
|
| 25 | - private static $_instance = null; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var EE_Dependency_Map $_dependency_map |
|
| 29 | - * @access protected |
|
| 30 | - */ |
|
| 31 | - protected $_dependency_map = null; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var array $_class_abbreviations |
|
| 35 | - * @access protected |
|
| 36 | - */ |
|
| 37 | - protected $_class_abbreviations = array(); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @access public |
|
| 41 | - * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | - */ |
|
| 43 | - public $BUS; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * EE_Cart Object |
|
| 47 | - * |
|
| 48 | - * @access public |
|
| 49 | - * @var EE_Cart $CART |
|
| 50 | - */ |
|
| 51 | - public $CART = null; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * EE_Config Object |
|
| 55 | - * |
|
| 56 | - * @access public |
|
| 57 | - * @var EE_Config $CFG |
|
| 58 | - */ |
|
| 59 | - public $CFG = null; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * EE_Network_Config Object |
|
| 63 | - * |
|
| 64 | - * @access public |
|
| 65 | - * @var EE_Network_Config $NET_CFG |
|
| 66 | - */ |
|
| 67 | - public $NET_CFG = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * StdClass object for storing library classes in |
|
| 71 | - * |
|
| 72 | - * @public LIB |
|
| 73 | - * @var StdClass $LIB |
|
| 74 | - */ |
|
| 75 | - public $LIB = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * EE_Request_Handler Object |
|
| 79 | - * |
|
| 80 | - * @access public |
|
| 81 | - * @var EE_Request_Handler $REQ |
|
| 82 | - */ |
|
| 83 | - public $REQ = null; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * EE_Session Object |
|
| 87 | - * |
|
| 88 | - * @access public |
|
| 89 | - * @var EE_Session $SSN |
|
| 90 | - */ |
|
| 91 | - public $SSN = null; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * holds the ee capabilities object. |
|
| 95 | - * |
|
| 96 | - * @since 4.5.0 |
|
| 97 | - * @var EE_Capabilities |
|
| 98 | - */ |
|
| 99 | - public $CAP = null; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * holds the EE_Message_Resource_Manager object. |
|
| 103 | - * |
|
| 104 | - * @since 4.9.0 |
|
| 105 | - * @var EE_Message_Resource_Manager |
|
| 106 | - */ |
|
| 107 | - public $MRM = null; |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Holds the Assets Registry instance |
|
| 112 | - * @var Registry |
|
| 113 | - */ |
|
| 114 | - public $AssetsRegistry = null; |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | - * |
|
| 119 | - * @access public |
|
| 120 | - * @var EE_Addon[] |
|
| 121 | - */ |
|
| 122 | - public $addons = null; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * $models |
|
| 126 | - * @access public |
|
| 127 | - * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | - */ |
|
| 129 | - public $models = array(); |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * $modules |
|
| 133 | - * @access public |
|
| 134 | - * @var EED_Module[] $modules |
|
| 135 | - */ |
|
| 136 | - public $modules = null; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * $shortcodes |
|
| 140 | - * @access public |
|
| 141 | - * @var EES_Shortcode[] $shortcodes |
|
| 142 | - */ |
|
| 143 | - public $shortcodes = null; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * $widgets |
|
| 147 | - * @access public |
|
| 148 | - * @var WP_Widget[] $widgets |
|
| 149 | - */ |
|
| 150 | - public $widgets = null; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * $non_abstract_db_models |
|
| 154 | - * @access public |
|
| 155 | - * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | - * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | - * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | - * classnames (eg "EEM_Event") |
|
| 159 | - */ |
|
| 160 | - public $non_abstract_db_models = array(); |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * $i18n_js_strings - internationalization for JS strings |
|
| 165 | - * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | - * in js file: var translatedString = eei18n.string_key; |
|
| 167 | - * |
|
| 168 | - * @access public |
|
| 169 | - * @var array |
|
| 170 | - */ |
|
| 171 | - public static $i18n_js_strings = array(); |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * $main_file - path to espresso.php |
|
| 176 | - * |
|
| 177 | - * @access public |
|
| 178 | - * @var array |
|
| 179 | - */ |
|
| 180 | - public $main_file; |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * array of ReflectionClass objects where the key is the class name |
|
| 184 | - * |
|
| 185 | - * @access public |
|
| 186 | - * @var ReflectionClass[] |
|
| 187 | - */ |
|
| 188 | - public $_reflectors; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | - * |
|
| 193 | - * @access protected |
|
| 194 | - * @var boolean $_cache_on |
|
| 195 | - */ |
|
| 196 | - protected $_cache_on = true; |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @singleton method used to instantiate class object |
|
| 202 | - * @access public |
|
| 203 | - * @param \EE_Dependency_Map $dependency_map |
|
| 204 | - * @return \EE_Registry instance |
|
| 205 | - */ |
|
| 206 | - public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | - { |
|
| 208 | - // check if class object is instantiated |
|
| 209 | - if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | - self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | - } |
|
| 212 | - return self::$_instance; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - *protected constructor to prevent direct creation |
|
| 219 | - * |
|
| 220 | - * @Constructor |
|
| 221 | - * @access protected |
|
| 222 | - * @param \EE_Dependency_Map $dependency_map |
|
| 223 | - * @return \EE_Registry |
|
| 224 | - */ |
|
| 225 | - protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 226 | - { |
|
| 227 | - $this->_dependency_map = $dependency_map; |
|
| 228 | - add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * initialize |
|
| 235 | - */ |
|
| 236 | - public function initialize() |
|
| 237 | - { |
|
| 238 | - $this->_class_abbreviations = apply_filters( |
|
| 239 | - 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 240 | - array( |
|
| 241 | - 'EE_Config' => 'CFG', |
|
| 242 | - 'EE_Session' => 'SSN', |
|
| 243 | - 'EE_Capabilities' => 'CAP', |
|
| 244 | - 'EE_Cart' => 'CART', |
|
| 245 | - 'EE_Network_Config' => 'NET_CFG', |
|
| 246 | - 'EE_Request_Handler' => 'REQ', |
|
| 247 | - 'EE_Message_Resource_Manager' => 'MRM', |
|
| 248 | - 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 249 | - ) |
|
| 250 | - ); |
|
| 251 | - // class library |
|
| 252 | - $this->LIB = new stdClass(); |
|
| 253 | - $this->addons = new stdClass(); |
|
| 254 | - $this->modules = new stdClass(); |
|
| 255 | - $this->shortcodes = new stdClass(); |
|
| 256 | - $this->widgets = new stdClass(); |
|
| 257 | - $this->load_core('Base', array(), true); |
|
| 258 | - // add our request and response objects to the cache |
|
| 259 | - $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 260 | - $this->_set_cached_class( |
|
| 261 | - $request_loader(), |
|
| 262 | - 'EE_Request' |
|
| 263 | - ); |
|
| 264 | - $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 265 | - $this->_set_cached_class( |
|
| 266 | - $response_loader(), |
|
| 267 | - 'EE_Response' |
|
| 268 | - ); |
|
| 269 | - add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * init |
|
| 276 | - * |
|
| 277 | - * @access public |
|
| 278 | - * @return void |
|
| 279 | - */ |
|
| 280 | - public function init() |
|
| 281 | - { |
|
| 282 | - $this->AssetsRegistry = new Registry(); |
|
| 283 | - // Get current page protocol |
|
| 284 | - $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 285 | - // Output admin-ajax.php URL with same protocol as current page |
|
| 286 | - self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 287 | - self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * localize_i18n_js_strings |
|
| 294 | - * |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - public static function localize_i18n_js_strings() |
|
| 298 | - { |
|
| 299 | - $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 300 | - foreach ($i18n_js_strings as $key => $value) { |
|
| 301 | - if (is_scalar($value)) { |
|
| 302 | - $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param mixed string | EED_Module $module |
|
| 312 | - */ |
|
| 313 | - public function add_module($module) |
|
| 314 | - { |
|
| 315 | - if ($module instanceof EED_Module) { |
|
| 316 | - $module_class = get_class($module); |
|
| 317 | - $this->modules->{$module_class} = $module; |
|
| 318 | - } else { |
|
| 319 | - if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 320 | - $this->load_core('Module_Request_Router'); |
|
| 321 | - } |
|
| 322 | - $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * @param string $module_name |
|
| 330 | - * @return mixed EED_Module | NULL |
|
| 331 | - */ |
|
| 332 | - public function get_module($module_name = '') |
|
| 333 | - { |
|
| 334 | - return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * loads core classes - must be singletons |
|
| 341 | - * |
|
| 342 | - * @access public |
|
| 343 | - * @param string $class_name - simple class name ie: session |
|
| 344 | - * @param mixed $arguments |
|
| 345 | - * @param bool $load_only |
|
| 346 | - * @return mixed |
|
| 347 | - */ |
|
| 348 | - public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 349 | - { |
|
| 350 | - $core_paths = apply_filters( |
|
| 351 | - 'FHEE__EE_Registry__load_core__core_paths', |
|
| 352 | - array( |
|
| 353 | - EE_CORE, |
|
| 354 | - EE_ADMIN, |
|
| 355 | - EE_CPTS, |
|
| 356 | - EE_CORE . 'data_migration_scripts' . DS, |
|
| 357 | - EE_CORE . 'request_stack' . DS, |
|
| 358 | - EE_CORE . 'middleware' . DS, |
|
| 359 | - ) |
|
| 360 | - ); |
|
| 361 | - // retrieve instantiated class |
|
| 362 | - return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * loads service classes |
|
| 369 | - * |
|
| 370 | - * @access public |
|
| 371 | - * @param string $class_name - simple class name ie: session |
|
| 372 | - * @param mixed $arguments |
|
| 373 | - * @param bool $load_only |
|
| 374 | - * @return mixed |
|
| 375 | - */ |
|
| 376 | - public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 377 | - { |
|
| 378 | - $service_paths = apply_filters( |
|
| 379 | - 'FHEE__EE_Registry__load_service__service_paths', |
|
| 380 | - array( |
|
| 381 | - EE_CORE . 'services' . DS, |
|
| 382 | - ) |
|
| 383 | - ); |
|
| 384 | - // retrieve instantiated class |
|
| 385 | - return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * loads data_migration_scripts |
|
| 392 | - * |
|
| 393 | - * @access public |
|
| 394 | - * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 395 | - * @param mixed $arguments |
|
| 396 | - * @return EE_Data_Migration_Script_Base|mixed |
|
| 397 | - */ |
|
| 398 | - public function load_dms($class_name, $arguments = array()) |
|
| 399 | - { |
|
| 400 | - // retrieve instantiated class |
|
| 401 | - return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * loads object creating classes - must be singletons |
|
| 408 | - * |
|
| 409 | - * @param string $class_name - simple class name ie: attendee |
|
| 410 | - * @param mixed $arguments - an array of arguments to pass to the class |
|
| 411 | - * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
|
| 412 | - * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 413 | - * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 414 | - * @return EE_Base_Class | bool |
|
| 415 | - */ |
|
| 416 | - public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 417 | - { |
|
| 418 | - $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 419 | - EE_CORE, |
|
| 420 | - EE_CLASSES, |
|
| 421 | - EE_BUSINESS, |
|
| 422 | - )); |
|
| 423 | - // retrieve instantiated class |
|
| 424 | - return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only); |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * loads helper classes - must be singletons |
|
| 431 | - * |
|
| 432 | - * @param string $class_name - simple class name ie: price |
|
| 433 | - * @param mixed $arguments |
|
| 434 | - * @param bool $load_only |
|
| 435 | - * @return EEH_Base | bool |
|
| 436 | - */ |
|
| 437 | - public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 438 | - { |
|
| 439 | - // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 440 | - $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 441 | - // retrieve instantiated class |
|
| 442 | - return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * loads core classes - must be singletons |
|
| 449 | - * |
|
| 450 | - * @access public |
|
| 451 | - * @param string $class_name - simple class name ie: session |
|
| 452 | - * @param mixed $arguments |
|
| 453 | - * @param bool $load_only |
|
| 454 | - * @param bool $cache whether to cache the object or not. |
|
| 455 | - * @return mixed |
|
| 456 | - */ |
|
| 457 | - public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 458 | - { |
|
| 459 | - $paths = array( |
|
| 460 | - EE_LIBRARIES, |
|
| 461 | - EE_LIBRARIES . 'messages' . DS, |
|
| 462 | - EE_LIBRARIES . 'shortcodes' . DS, |
|
| 463 | - EE_LIBRARIES . 'qtips' . DS, |
|
| 464 | - EE_LIBRARIES . 'payment_methods' . DS, |
|
| 465 | - ); |
|
| 466 | - // retrieve instantiated class |
|
| 467 | - return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * loads model classes - must be singletons |
|
| 474 | - * |
|
| 475 | - * @param string $class_name - simple class name ie: price |
|
| 476 | - * @param mixed $arguments |
|
| 477 | - * @param bool $load_only |
|
| 478 | - * @return EEM_Base | bool |
|
| 479 | - */ |
|
| 480 | - public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 481 | - { |
|
| 482 | - $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 483 | - EE_MODELS, |
|
| 484 | - EE_CORE, |
|
| 485 | - )); |
|
| 486 | - // retrieve instantiated class |
|
| 487 | - return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * loads model classes - must be singletons |
|
| 494 | - * |
|
| 495 | - * @param string $class_name - simple class name ie: price |
|
| 496 | - * @param mixed $arguments |
|
| 497 | - * @param bool $load_only |
|
| 498 | - * @return mixed | bool |
|
| 499 | - */ |
|
| 500 | - public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 501 | - { |
|
| 502 | - $paths = array( |
|
| 503 | - EE_MODELS . 'fields' . DS, |
|
| 504 | - EE_MODELS . 'helpers' . DS, |
|
| 505 | - EE_MODELS . 'relations' . DS, |
|
| 506 | - EE_MODELS . 'strategies' . DS, |
|
| 507 | - ); |
|
| 508 | - // retrieve instantiated class |
|
| 509 | - return $this->_load($paths, 'EE_', $class_name, '', $arguments, false, true, $load_only); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Determines if $model_name is the name of an actual EE model. |
|
| 516 | - * |
|
| 517 | - * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 518 | - * @return boolean |
|
| 519 | - */ |
|
| 520 | - public function is_model_name($model_name) |
|
| 521 | - { |
|
| 522 | - return isset($this->models[$model_name]) ? true : false; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * generic class loader |
|
| 529 | - * |
|
| 530 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 531 | - * @param string $file_name - file name ie: my_file.php, including extension |
|
| 532 | - * @param string $type - file type - core? class? helper? model? |
|
| 533 | - * @param mixed $arguments |
|
| 534 | - * @param bool $load_only |
|
| 535 | - * @return mixed |
|
| 536 | - */ |
|
| 537 | - public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 538 | - { |
|
| 539 | - // retrieve instantiated class |
|
| 540 | - return $this->_load($path_to_file, '', $file_name, $type, $arguments, false, true, $load_only); |
|
| 541 | - } |
|
| 542 | - |
|
| 543 | - |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * load_addon |
|
| 547 | - * |
|
| 548 | - * @param string $path_to_file - directory path to file location, not including filename |
|
| 549 | - * @param string $class_name - full class name ie: My_Class |
|
| 550 | - * @param string $type - file type - core? class? helper? model? |
|
| 551 | - * @param mixed $arguments |
|
| 552 | - * @param bool $load_only |
|
| 553 | - * @return EE_Addon |
|
| 554 | - */ |
|
| 555 | - public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 556 | - { |
|
| 557 | - // retrieve instantiated class |
|
| 558 | - return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * instantiates, caches, and automatically resolves dependencies |
|
| 565 | - * for classes that use a Fully Qualified Class Name. |
|
| 566 | - * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 567 | - * then you need to use one of the existing load_*() methods |
|
| 568 | - * which can resolve the classname and filepath from the passed arguments |
|
| 569 | - * |
|
| 570 | - * @param bool|string $class_name Fully Qualified Class Name |
|
| 571 | - * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 572 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 573 | - * @param bool $from_db some classes are instantiated from the db |
|
| 574 | - * and thus call a different method to instantiate |
|
| 575 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 576 | - * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 577 | - * @return mixed null = failure to load or instantiate class object. |
|
| 578 | - * object = class loaded and instantiated successfully. |
|
| 579 | - * bool = fail or success when $load_only is true |
|
| 580 | - */ |
|
| 581 | - public function create( |
|
| 582 | - $class_name = false, |
|
| 583 | - $arguments = array(), |
|
| 584 | - $cache = false, |
|
| 585 | - $from_db = false, |
|
| 586 | - $load_only = false, |
|
| 587 | - $addon = false |
|
| 588 | - ) { |
|
| 589 | - $class_name = ltrim($class_name, '\\'); |
|
| 590 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | - if ( ! class_exists($class_name)) { |
|
| 592 | - // maybe the class is registered with a preceding \ |
|
| 593 | - $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | - // still doesn't exist ? |
|
| 595 | - if ( ! class_exists($class_name)) { |
|
| 596 | - return null; |
|
| 597 | - } |
|
| 598 | - } |
|
| 599 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | - if ($load_only) { |
|
| 601 | - return true; |
|
| 602 | - } |
|
| 603 | - $addon = $addon ? 'addon' : ''; |
|
| 604 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | - // return object if it's already cached |
|
| 609 | - $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | - if ($cached_class !== null) { |
|
| 611 | - return $cached_class; |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - // instantiate the requested object |
|
| 615 | - $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 616 | - if ($this->_cache_on && $cache) { |
|
| 617 | - // save it for later... kinda like gum { : $ |
|
| 618 | - $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
| 619 | - } |
|
| 620 | - $this->_cache_on = true; |
|
| 621 | - return $class_obj; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * instantiates, caches, and injects dependencies for classes |
|
| 628 | - * |
|
| 629 | - * @param array $file_paths an array of paths to folders to look in |
|
| 630 | - * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | - * @param bool|string $class_name $class name |
|
| 632 | - * @param string $type file type - core? class? helper? model? |
|
| 633 | - * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | - * @param bool $from_db some classes are instantiated from the db |
|
| 635 | - * and thus call a different method to instantiate |
|
| 636 | - * @param bool $cache whether to cache the instantiated object for reuse |
|
| 637 | - * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 638 | - * @return null|object|bool null = failure to load or instantiate class object. |
|
| 639 | - * object = class loaded and instantiated successfully. |
|
| 640 | - * bool = fail or success when $load_only is true |
|
| 641 | - */ |
|
| 642 | - protected function _load( |
|
| 643 | - $file_paths = array(), |
|
| 644 | - $class_prefix = 'EE_', |
|
| 645 | - $class_name = false, |
|
| 646 | - $type = 'class', |
|
| 647 | - $arguments = array(), |
|
| 648 | - $from_db = false, |
|
| 649 | - $cache = true, |
|
| 650 | - $load_only = false |
|
| 651 | - ) { |
|
| 652 | - $class_name = ltrim($class_name, '\\'); |
|
| 653 | - // strip php file extension |
|
| 654 | - $class_name = str_replace('.php', '', trim($class_name)); |
|
| 655 | - // does the class have a prefix ? |
|
| 656 | - if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 657 | - // make sure $class_prefix is uppercase |
|
| 658 | - $class_prefix = strtoupper(trim($class_prefix)); |
|
| 659 | - // add class prefix ONCE!!! |
|
| 660 | - $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 661 | - } |
|
| 662 | - $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 663 | - $class_exists = class_exists($class_name); |
|
| 664 | - // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 665 | - if ($load_only && $class_exists) { |
|
| 666 | - return true; |
|
| 667 | - } |
|
| 668 | - // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 669 | - // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 670 | - // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 671 | - if ($this->_cache_on && $cache && ! $load_only) { |
|
| 672 | - // return object if it's already cached |
|
| 673 | - $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 674 | - if ($cached_class !== null) { |
|
| 675 | - return $cached_class; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 679 | - if ( ! $class_exists) { |
|
| 680 | - // get full path to file |
|
| 681 | - $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 682 | - // load the file |
|
| 683 | - $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 684 | - // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 685 | - if ( ! $loaded || $load_only) { |
|
| 686 | - // return boolean if only loading, or null if an object was expected |
|
| 687 | - return $load_only ? $loaded : null; |
|
| 688 | - } |
|
| 689 | - } |
|
| 690 | - // instantiate the requested object |
|
| 691 | - $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 692 | - if ($this->_cache_on && $cache) { |
|
| 693 | - // save it for later... kinda like gum { : $ |
|
| 694 | - $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
| 695 | - } |
|
| 696 | - $this->_cache_on = true; |
|
| 697 | - return $class_obj; |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - |
|
| 701 | - |
|
| 702 | - /** |
|
| 703 | - * _get_cached_class |
|
| 704 | - * attempts to find a cached version of the requested class |
|
| 705 | - * by looking in the following places: |
|
| 706 | - * $this->{$class_abbreviation} ie: $this->CART |
|
| 707 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 708 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 709 | - * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 710 | - * |
|
| 711 | - * @access protected |
|
| 712 | - * @param string $class_name |
|
| 713 | - * @param string $class_prefix |
|
| 714 | - * @return mixed |
|
| 715 | - */ |
|
| 716 | - protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 717 | - { |
|
| 718 | - if (isset($this->_class_abbreviations[$class_name])) { |
|
| 719 | - $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 720 | - } else { |
|
| 721 | - // have to specify something, but not anything that will conflict |
|
| 722 | - $class_abbreviation = 'FANCY_BATMAN_PANTS'; |
|
| 723 | - } |
|
| 724 | - // check if class has already been loaded, and return it if it has been |
|
| 725 | - if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 726 | - return $this->{$class_abbreviation}; |
|
| 727 | - } else if (isset ($this->{$class_name})) { |
|
| 728 | - return $this->{$class_name}; |
|
| 729 | - } else if (isset ($this->LIB->{$class_name})) { |
|
| 730 | - return $this->LIB->{$class_name}; |
|
| 731 | - } else if ($class_prefix == 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | - return $this->addons->{$class_name}; |
|
| 733 | - } |
|
| 734 | - return null; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * _resolve_path |
|
| 741 | - * attempts to find a full valid filepath for the requested class. |
|
| 742 | - * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 743 | - * then returns that path if the target file has been found and is readable |
|
| 744 | - * |
|
| 745 | - * @access protected |
|
| 746 | - * @param string $class_name |
|
| 747 | - * @param string $type |
|
| 748 | - * @param array $file_paths |
|
| 749 | - * @return string | bool |
|
| 750 | - */ |
|
| 751 | - protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 752 | - { |
|
| 753 | - // make sure $file_paths is an array |
|
| 754 | - $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 755 | - // cycle thru paths |
|
| 756 | - foreach ($file_paths as $key => $file_path) { |
|
| 757 | - // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 758 | - $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 759 | - // prep file type |
|
| 760 | - $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 761 | - // build full file path |
|
| 762 | - $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 763 | - //does the file exist and can be read ? |
|
| 764 | - if (is_readable($file_paths[$key])) { |
|
| 765 | - return $file_paths[$key]; |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - return false; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * _require_file |
|
| 775 | - * basically just performs a require_once() |
|
| 776 | - * but with some error handling |
|
| 777 | - * |
|
| 778 | - * @access protected |
|
| 779 | - * @param string $path |
|
| 780 | - * @param string $class_name |
|
| 781 | - * @param string $type |
|
| 782 | - * @param array $file_paths |
|
| 783 | - * @return boolean |
|
| 784 | - * @throws \EE_Error |
|
| 785 | - */ |
|
| 786 | - protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 787 | - { |
|
| 788 | - // don't give up! you gotta... |
|
| 789 | - try { |
|
| 790 | - //does the file exist and can it be read ? |
|
| 791 | - if ( ! $path) { |
|
| 792 | - // so sorry, can't find the file |
|
| 793 | - throw new EE_Error ( |
|
| 794 | - sprintf( |
|
| 795 | - __('The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', 'event_espresso'), |
|
| 796 | - trim($type, '.'), |
|
| 797 | - $class_name, |
|
| 798 | - '<br />' . implode(',<br />', $file_paths) |
|
| 799 | - ) |
|
| 800 | - ); |
|
| 801 | - } |
|
| 802 | - // get the file |
|
| 803 | - require_once($path); |
|
| 804 | - // if the class isn't already declared somewhere |
|
| 805 | - if (class_exists($class_name, false) === false) { |
|
| 806 | - // so sorry, not a class |
|
| 807 | - throw new EE_Error( |
|
| 808 | - sprintf( |
|
| 809 | - __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 810 | - $type, |
|
| 811 | - $path, |
|
| 812 | - $class_name |
|
| 813 | - ) |
|
| 814 | - ); |
|
| 815 | - } |
|
| 816 | - } catch (EE_Error $e) { |
|
| 817 | - $e->get_error(); |
|
| 818 | - return false; |
|
| 819 | - } |
|
| 820 | - return true; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - |
|
| 824 | - |
|
| 825 | - /** |
|
| 826 | - * _create_object |
|
| 827 | - * Attempts to instantiate the requested class via any of the |
|
| 828 | - * commonly used instantiation methods employed throughout EE. |
|
| 829 | - * The priority for instantiation is as follows: |
|
| 830 | - * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 831 | - * - model objects via their 'new_instance_from_db' method |
|
| 832 | - * - model objects via their 'new_instance' method |
|
| 833 | - * - "singleton" classes" via their 'instance' method |
|
| 834 | - * - standard instantiable classes via their __constructor |
|
| 835 | - * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 836 | - * then the constructor for the requested class will be examined to determine |
|
| 837 | - * if any dependencies exist, and if they can be injected. |
|
| 838 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 839 | - * |
|
| 840 | - * @access protected |
|
| 841 | - * @param string $class_name |
|
| 842 | - * @param array $arguments |
|
| 843 | - * @param string $type |
|
| 844 | - * @param bool $from_db |
|
| 845 | - * @return null | object |
|
| 846 | - * @throws \EE_Error |
|
| 847 | - */ |
|
| 848 | - protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 849 | - { |
|
| 850 | - $class_obj = null; |
|
| 851 | - $instantiation_mode = '0) none'; |
|
| 852 | - // don't give up! you gotta... |
|
| 853 | - try { |
|
| 854 | - // create reflection |
|
| 855 | - $reflector = $this->get_ReflectionClass($class_name); |
|
| 856 | - // make sure arguments are an array |
|
| 857 | - $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 858 | - // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 859 | - // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 860 | - $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 861 | - ? $arguments |
|
| 862 | - : array($arguments); |
|
| 863 | - // attempt to inject dependencies ? |
|
| 864 | - if ($this->_dependency_map->has($class_name)) { |
|
| 865 | - $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 866 | - } |
|
| 867 | - // instantiate the class if possible |
|
| 868 | - if ($reflector->isAbstract()) { |
|
| 869 | - // nothing to instantiate, loading file was enough |
|
| 870 | - // does not throw an exception so $instantiation_mode is unused |
|
| 871 | - // $instantiation_mode = "1) no constructor abstract class"; |
|
| 872 | - $class_obj = true; |
|
| 873 | - } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 874 | - // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 875 | - $instantiation_mode = "2) no constructor but instantiable"; |
|
| 876 | - $class_obj = $reflector->newInstance(); |
|
| 877 | - } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 878 | - $instantiation_mode = "3) new_instance_from_db()"; |
|
| 879 | - $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 880 | - } else if (method_exists($class_name, 'new_instance')) { |
|
| 881 | - $instantiation_mode = "4) new_instance()"; |
|
| 882 | - $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 883 | - } else if (method_exists($class_name, 'instance')) { |
|
| 884 | - $instantiation_mode = "5) instance()"; |
|
| 885 | - $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 886 | - } else if ($reflector->isInstantiable()) { |
|
| 887 | - $instantiation_mode = "6) constructor"; |
|
| 888 | - $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 889 | - } else { |
|
| 890 | - // heh ? something's not right ! |
|
| 891 | - throw new EE_Error( |
|
| 892 | - sprintf( |
|
| 893 | - __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 894 | - $type, |
|
| 895 | - $class_name |
|
| 896 | - ) |
|
| 897 | - ); |
|
| 898 | - } |
|
| 899 | - } catch (Exception $e) { |
|
| 900 | - if ( ! $e instanceof EE_Error) { |
|
| 901 | - $e = new EE_Error( |
|
| 902 | - sprintf( |
|
| 903 | - __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 904 | - $class_name, |
|
| 905 | - '<br />', |
|
| 906 | - $e->getMessage(), |
|
| 907 | - $instantiation_mode |
|
| 908 | - ) |
|
| 909 | - ); |
|
| 910 | - } |
|
| 911 | - $e->get_error(); |
|
| 912 | - } |
|
| 913 | - return $class_obj; |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 920 | - * @param array $array |
|
| 921 | - * @return bool |
|
| 922 | - */ |
|
| 923 | - protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 924 | - { |
|
| 925 | - return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - |
|
| 929 | - |
|
| 930 | - /** |
|
| 931 | - * getReflectionClass |
|
| 932 | - * checks if a ReflectionClass object has already been generated for a class |
|
| 933 | - * and returns that instead of creating a new one |
|
| 934 | - * |
|
| 935 | - * @access public |
|
| 936 | - * @param string $class_name |
|
| 937 | - * @return ReflectionClass |
|
| 938 | - */ |
|
| 939 | - public function get_ReflectionClass($class_name) |
|
| 940 | - { |
|
| 941 | - if ( |
|
| 942 | - ! isset($this->_reflectors[$class_name]) |
|
| 943 | - || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 944 | - ) { |
|
| 945 | - $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 946 | - } |
|
| 947 | - return $this->_reflectors[$class_name]; |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - |
|
| 951 | - |
|
| 952 | - /** |
|
| 953 | - * _resolve_dependencies |
|
| 954 | - * examines the constructor for the requested class to determine |
|
| 955 | - * if any dependencies exist, and if they can be injected. |
|
| 956 | - * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 957 | - * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 958 | - * For example: |
|
| 959 | - * if attempting to load a class "Foo" with the following constructor: |
|
| 960 | - * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 961 | - * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 962 | - * but only IF they are NOT already present in the incoming arguments array, |
|
| 963 | - * and the correct classes can be loaded |
|
| 964 | - * |
|
| 965 | - * @access protected |
|
| 966 | - * @param ReflectionClass $reflector |
|
| 967 | - * @param string $class_name |
|
| 968 | - * @param array $arguments |
|
| 969 | - * @return array |
|
| 970 | - * @throws \ReflectionException |
|
| 971 | - */ |
|
| 972 | - protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 973 | - { |
|
| 974 | - // let's examine the constructor |
|
| 975 | - $constructor = $reflector->getConstructor(); |
|
| 976 | - // whu? huh? nothing? |
|
| 977 | - if ( ! $constructor) { |
|
| 978 | - return $arguments; |
|
| 979 | - } |
|
| 980 | - // get constructor parameters |
|
| 981 | - $params = $constructor->getParameters(); |
|
| 982 | - // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 983 | - $argument_keys = array_keys($arguments); |
|
| 984 | - // now loop thru all of the constructors expected parameters |
|
| 985 | - foreach ($params as $index => $param) { |
|
| 986 | - // is this a dependency for a specific class ? |
|
| 987 | - $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 988 | - if ( |
|
| 989 | - // param is not even a class |
|
| 990 | - empty($param_class) |
|
| 991 | - // and something already exists in the incoming arguments for this param |
|
| 992 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 993 | - ) { |
|
| 994 | - // so let's skip this argument and move on to the next |
|
| 995 | - continue; |
|
| 996 | - } else if ( |
|
| 997 | - // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 998 | - ! empty($param_class) |
|
| 999 | - && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1000 | - && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1001 | - ) { |
|
| 1002 | - // skip this argument and move on to the next |
|
| 1003 | - continue; |
|
| 1004 | - } else if ( |
|
| 1005 | - // parameter is type hinted as a class, and should be injected |
|
| 1006 | - ! empty($param_class) |
|
| 1007 | - && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1008 | - ) { |
|
| 1009 | - $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1010 | - } else { |
|
| 1011 | - try { |
|
| 1012 | - $arguments[$index] = $param->getDefaultValue(); |
|
| 1013 | - } catch (ReflectionException $e) { |
|
| 1014 | - throw new ReflectionException( |
|
| 1015 | - sprintf( |
|
| 1016 | - __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1017 | - $e->getMessage(), |
|
| 1018 | - $param->getName() |
|
| 1019 | - ) |
|
| 1020 | - ); |
|
| 1021 | - } |
|
| 1022 | - } |
|
| 1023 | - } |
|
| 1024 | - return $arguments; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * @access protected |
|
| 1031 | - * @param string $class_name |
|
| 1032 | - * @param string $param_class |
|
| 1033 | - * @param array $arguments |
|
| 1034 | - * @param mixed $index |
|
| 1035 | - * @return array |
|
| 1036 | - */ |
|
| 1037 | - protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1038 | - { |
|
| 1039 | - $dependency = null; |
|
| 1040 | - // should dependency be loaded from cache ? |
|
| 1041 | - $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1042 | - !== EE_Dependency_Map::load_new_object |
|
| 1043 | - ? true |
|
| 1044 | - : false; |
|
| 1045 | - // we might have a dependency... |
|
| 1046 | - // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1047 | - $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1048 | - // and grab it if it exists |
|
| 1049 | - if ($cached_class instanceof $param_class) { |
|
| 1050 | - $dependency = $cached_class; |
|
| 1051 | - } else if ($param_class != $class_name) { |
|
| 1052 | - // obtain the loader method from the dependency map |
|
| 1053 | - $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1054 | - // is loader a custom closure ? |
|
| 1055 | - if ($loader instanceof Closure) { |
|
| 1056 | - $dependency = $loader(); |
|
| 1057 | - } else { |
|
| 1058 | - // set the cache on property for the recursive loading call |
|
| 1059 | - $this->_cache_on = $cache_on; |
|
| 1060 | - // if not, then let's try and load it via the registry |
|
| 1061 | - if (method_exists($this, $loader)) { |
|
| 1062 | - $dependency = $this->{$loader}($param_class); |
|
| 1063 | - } else { |
|
| 1064 | - $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1065 | - } |
|
| 1066 | - } |
|
| 1067 | - } |
|
| 1068 | - // did we successfully find the correct dependency ? |
|
| 1069 | - if ($dependency instanceof $param_class) { |
|
| 1070 | - // then let's inject it into the incoming array of arguments at the correct location |
|
| 1071 | - if (isset($argument_keys[$index])) { |
|
| 1072 | - $arguments[$argument_keys[$index]] = $dependency; |
|
| 1073 | - } else { |
|
| 1074 | - $arguments[$index] = $dependency; |
|
| 1075 | - } |
|
| 1076 | - } |
|
| 1077 | - return $arguments; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - |
|
| 1081 | - |
|
| 1082 | - /** |
|
| 1083 | - * _set_cached_class |
|
| 1084 | - * attempts to cache the instantiated class locally |
|
| 1085 | - * in one of the following places, in the following order: |
|
| 1086 | - * $this->{class_abbreviation} ie: $this->CART |
|
| 1087 | - * $this->{$class_name} ie: $this->Some_Class |
|
| 1088 | - * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1089 | - * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1090 | - * |
|
| 1091 | - * @access protected |
|
| 1092 | - * @param object $class_obj |
|
| 1093 | - * @param string $class_name |
|
| 1094 | - * @param string $class_prefix |
|
| 1095 | - * @param bool $from_db |
|
| 1096 | - * @return void |
|
| 1097 | - */ |
|
| 1098 | - protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
| 1099 | - { |
|
| 1100 | - if (empty($class_obj)) { |
|
| 1101 | - return; |
|
| 1102 | - } |
|
| 1103 | - // return newly instantiated class |
|
| 1104 | - if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1105 | - $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1106 | - $this->{$class_abbreviation} = $class_obj; |
|
| 1107 | - } else if (property_exists($this, $class_name)) { |
|
| 1108 | - $this->{$class_name} = $class_obj; |
|
| 1109 | - } else if ($class_prefix == 'addon') { |
|
| 1110 | - $this->addons->{$class_name} = $class_obj; |
|
| 1111 | - } else if ( ! $from_db) { |
|
| 1112 | - $this->LIB->{$class_name} = $class_obj; |
|
| 1113 | - } |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - |
|
| 1117 | - |
|
| 1118 | - /** |
|
| 1119 | - * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1120 | - * |
|
| 1121 | - * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1122 | - * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1123 | - * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1124 | - * @param array $arguments |
|
| 1125 | - * @return object |
|
| 1126 | - */ |
|
| 1127 | - public static function factory($classname, $arguments = array()) |
|
| 1128 | - { |
|
| 1129 | - $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1130 | - if ($loader instanceof Closure) { |
|
| 1131 | - return $loader($arguments); |
|
| 1132 | - } else if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1133 | - return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1134 | - } |
|
| 1135 | - return null; |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - |
|
| 1140 | - /** |
|
| 1141 | - * Gets the addon by its name/slug (not classname. For that, just |
|
| 1142 | - * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1143 | - * |
|
| 1144 | - * @param string $name |
|
| 1145 | - * @return EE_Addon |
|
| 1146 | - */ |
|
| 1147 | - public function get_addon_by_name($name) |
|
| 1148 | - { |
|
| 1149 | - foreach ($this->addons as $addon) { |
|
| 1150 | - if ($addon->name() == $name) { |
|
| 1151 | - return $addon; |
|
| 1152 | - } |
|
| 1153 | - } |
|
| 1154 | - return null; |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - |
|
| 1158 | - |
|
| 1159 | - /** |
|
| 1160 | - * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is |
|
| 1161 | - * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1162 | - * |
|
| 1163 | - * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1164 | - */ |
|
| 1165 | - public function get_addons_by_name() |
|
| 1166 | - { |
|
| 1167 | - $addons = array(); |
|
| 1168 | - foreach ($this->addons as $addon) { |
|
| 1169 | - $addons[$addon->name()] = $addon; |
|
| 1170 | - } |
|
| 1171 | - return $addons; |
|
| 1172 | - } |
|
| 1173 | - |
|
| 1174 | - |
|
| 1175 | - |
|
| 1176 | - /** |
|
| 1177 | - * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1178 | - * a stale copy of it around |
|
| 1179 | - * |
|
| 1180 | - * @param string $model_name |
|
| 1181 | - * @return \EEM_Base |
|
| 1182 | - * @throws \EE_Error |
|
| 1183 | - */ |
|
| 1184 | - public function reset_model($model_name) |
|
| 1185 | - { |
|
| 1186 | - $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1187 | - if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1188 | - return null; |
|
| 1189 | - } |
|
| 1190 | - //get that model reset it and make sure we nuke the old reference to it |
|
| 1191 | - if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1192 | - $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1193 | - } else { |
|
| 1194 | - throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1195 | - } |
|
| 1196 | - return $this->LIB->{$model_class_name}; |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - |
|
| 1200 | - |
|
| 1201 | - /** |
|
| 1202 | - * Resets the registry. |
|
| 1203 | - * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1204 | - * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1205 | - * - $_dependency_map |
|
| 1206 | - * - $_class_abbreviations |
|
| 1207 | - * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1208 | - * - $REQ: Still on the same request so no need to change. |
|
| 1209 | - * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1210 | - * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1211 | - * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1212 | - * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1213 | - * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1214 | - * switch or on the restore. |
|
| 1215 | - * - $modules |
|
| 1216 | - * - $shortcodes |
|
| 1217 | - * - $widgets |
|
| 1218 | - * |
|
| 1219 | - * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1220 | - * the Registry to its state at the beginning of the request |
|
| 1221 | - * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1222 | - * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1223 | - * currently reinstantiate the singletons at the moment) |
|
| 1224 | - * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1225 | - * code instead can just change the model context to a different blog id if necessary |
|
| 1226 | - * @return EE_Registry |
|
| 1227 | - */ |
|
| 1228 | - public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1229 | - { |
|
| 1230 | - $instance = self::instance(); |
|
| 1231 | - EEH_Activation::reset(); |
|
| 1232 | - //properties that get reset |
|
| 1233 | - $instance->_cache_on = true; |
|
| 1234 | - $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1235 | - $instance->CART = null; |
|
| 1236 | - $instance->MRM = null; |
|
| 1237 | - $instance->AssetsRegistry = new Registry(); |
|
| 1238 | - //messages reset |
|
| 1239 | - EED_Messages::reset(); |
|
| 1240 | - if ($reset_models) { |
|
| 1241 | - foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1242 | - $instance->reset_model($model_name); |
|
| 1243 | - } |
|
| 1244 | - } |
|
| 1245 | - $instance->LIB = new stdClass(); |
|
| 1246 | - return $instance; |
|
| 1247 | - } |
|
| 1248 | - |
|
| 1249 | - |
|
| 1250 | - |
|
| 1251 | - /** |
|
| 1252 | - * @override magic methods |
|
| 1253 | - * @return void |
|
| 1254 | - */ |
|
| 1255 | - final function __destruct() |
|
| 1256 | - { |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - |
|
| 1260 | - |
|
| 1261 | - /** |
|
| 1262 | - * @param $a |
|
| 1263 | - * @param $b |
|
| 1264 | - */ |
|
| 1265 | - final function __call($a, $b) |
|
| 1266 | - { |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - |
|
| 1270 | - |
|
| 1271 | - /** |
|
| 1272 | - * @param $a |
|
| 1273 | - */ |
|
| 1274 | - final function __get($a) |
|
| 1275 | - { |
|
| 1276 | - } |
|
| 1277 | - |
|
| 1278 | - |
|
| 1279 | - |
|
| 1280 | - /** |
|
| 1281 | - * @param $a |
|
| 1282 | - * @param $b |
|
| 1283 | - */ |
|
| 1284 | - final function __set($a, $b) |
|
| 1285 | - { |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - |
|
| 1289 | - |
|
| 1290 | - /** |
|
| 1291 | - * @param $a |
|
| 1292 | - */ |
|
| 1293 | - final function __isset($a) |
|
| 1294 | - { |
|
| 1295 | - } |
|
| 19 | + /** |
|
| 20 | + * EE_Registry Object |
|
| 21 | + * |
|
| 22 | + * @var EE_Registry $_instance |
|
| 23 | + * @access private |
|
| 24 | + */ |
|
| 25 | + private static $_instance = null; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var EE_Dependency_Map $_dependency_map |
|
| 29 | + * @access protected |
|
| 30 | + */ |
|
| 31 | + protected $_dependency_map = null; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var array $_class_abbreviations |
|
| 35 | + * @access protected |
|
| 36 | + */ |
|
| 37 | + protected $_class_abbreviations = array(); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @access public |
|
| 41 | + * @var \EventEspresso\core\services\commands\CommandBusInterface $BUS |
|
| 42 | + */ |
|
| 43 | + public $BUS; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * EE_Cart Object |
|
| 47 | + * |
|
| 48 | + * @access public |
|
| 49 | + * @var EE_Cart $CART |
|
| 50 | + */ |
|
| 51 | + public $CART = null; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * EE_Config Object |
|
| 55 | + * |
|
| 56 | + * @access public |
|
| 57 | + * @var EE_Config $CFG |
|
| 58 | + */ |
|
| 59 | + public $CFG = null; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * EE_Network_Config Object |
|
| 63 | + * |
|
| 64 | + * @access public |
|
| 65 | + * @var EE_Network_Config $NET_CFG |
|
| 66 | + */ |
|
| 67 | + public $NET_CFG = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * StdClass object for storing library classes in |
|
| 71 | + * |
|
| 72 | + * @public LIB |
|
| 73 | + * @var StdClass $LIB |
|
| 74 | + */ |
|
| 75 | + public $LIB = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * EE_Request_Handler Object |
|
| 79 | + * |
|
| 80 | + * @access public |
|
| 81 | + * @var EE_Request_Handler $REQ |
|
| 82 | + */ |
|
| 83 | + public $REQ = null; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * EE_Session Object |
|
| 87 | + * |
|
| 88 | + * @access public |
|
| 89 | + * @var EE_Session $SSN |
|
| 90 | + */ |
|
| 91 | + public $SSN = null; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * holds the ee capabilities object. |
|
| 95 | + * |
|
| 96 | + * @since 4.5.0 |
|
| 97 | + * @var EE_Capabilities |
|
| 98 | + */ |
|
| 99 | + public $CAP = null; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * holds the EE_Message_Resource_Manager object. |
|
| 103 | + * |
|
| 104 | + * @since 4.9.0 |
|
| 105 | + * @var EE_Message_Resource_Manager |
|
| 106 | + */ |
|
| 107 | + public $MRM = null; |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Holds the Assets Registry instance |
|
| 112 | + * @var Registry |
|
| 113 | + */ |
|
| 114 | + public $AssetsRegistry = null; |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * $addons - StdClass object for holding addons which have registered themselves to work with EE core |
|
| 118 | + * |
|
| 119 | + * @access public |
|
| 120 | + * @var EE_Addon[] |
|
| 121 | + */ |
|
| 122 | + public $addons = null; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * $models |
|
| 126 | + * @access public |
|
| 127 | + * @var EEM_Base[] $models keys are 'short names' (eg Event), values are class names (eg 'EEM_Event') |
|
| 128 | + */ |
|
| 129 | + public $models = array(); |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * $modules |
|
| 133 | + * @access public |
|
| 134 | + * @var EED_Module[] $modules |
|
| 135 | + */ |
|
| 136 | + public $modules = null; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * $shortcodes |
|
| 140 | + * @access public |
|
| 141 | + * @var EES_Shortcode[] $shortcodes |
|
| 142 | + */ |
|
| 143 | + public $shortcodes = null; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * $widgets |
|
| 147 | + * @access public |
|
| 148 | + * @var WP_Widget[] $widgets |
|
| 149 | + */ |
|
| 150 | + public $widgets = null; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * $non_abstract_db_models |
|
| 154 | + * @access public |
|
| 155 | + * @var array this is an array of all implemented model names (i.e. not the parent abstract models, or models |
|
| 156 | + * which don't actually fetch items from the DB in the normal way (ie, are not children of EEM_Base)). |
|
| 157 | + * Keys are model "short names" (eg "Event") as used in model relations, and values are |
|
| 158 | + * classnames (eg "EEM_Event") |
|
| 159 | + */ |
|
| 160 | + public $non_abstract_db_models = array(); |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * $i18n_js_strings - internationalization for JS strings |
|
| 165 | + * usage: EE_Registry::i18n_js_strings['string_key'] = __( 'string to translate.', 'event_espresso' ); |
|
| 166 | + * in js file: var translatedString = eei18n.string_key; |
|
| 167 | + * |
|
| 168 | + * @access public |
|
| 169 | + * @var array |
|
| 170 | + */ |
|
| 171 | + public static $i18n_js_strings = array(); |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * $main_file - path to espresso.php |
|
| 176 | + * |
|
| 177 | + * @access public |
|
| 178 | + * @var array |
|
| 179 | + */ |
|
| 180 | + public $main_file; |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * array of ReflectionClass objects where the key is the class name |
|
| 184 | + * |
|
| 185 | + * @access public |
|
| 186 | + * @var ReflectionClass[] |
|
| 187 | + */ |
|
| 188 | + public $_reflectors; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * boolean flag to indicate whether or not to load/save dependencies from/to the cache |
|
| 192 | + * |
|
| 193 | + * @access protected |
|
| 194 | + * @var boolean $_cache_on |
|
| 195 | + */ |
|
| 196 | + protected $_cache_on = true; |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @singleton method used to instantiate class object |
|
| 202 | + * @access public |
|
| 203 | + * @param \EE_Dependency_Map $dependency_map |
|
| 204 | + * @return \EE_Registry instance |
|
| 205 | + */ |
|
| 206 | + public static function instance(\EE_Dependency_Map $dependency_map = null) |
|
| 207 | + { |
|
| 208 | + // check if class object is instantiated |
|
| 209 | + if ( ! self::$_instance instanceof EE_Registry) { |
|
| 210 | + self::$_instance = new EE_Registry($dependency_map); |
|
| 211 | + } |
|
| 212 | + return self::$_instance; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + *protected constructor to prevent direct creation |
|
| 219 | + * |
|
| 220 | + * @Constructor |
|
| 221 | + * @access protected |
|
| 222 | + * @param \EE_Dependency_Map $dependency_map |
|
| 223 | + * @return \EE_Registry |
|
| 224 | + */ |
|
| 225 | + protected function __construct(\EE_Dependency_Map $dependency_map) |
|
| 226 | + { |
|
| 227 | + $this->_dependency_map = $dependency_map; |
|
| 228 | + add_action('EE_Load_Espresso_Core__handle_request__initialize_core_loading', array($this, 'initialize')); |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * initialize |
|
| 235 | + */ |
|
| 236 | + public function initialize() |
|
| 237 | + { |
|
| 238 | + $this->_class_abbreviations = apply_filters( |
|
| 239 | + 'FHEE__EE_Registry____construct___class_abbreviations', |
|
| 240 | + array( |
|
| 241 | + 'EE_Config' => 'CFG', |
|
| 242 | + 'EE_Session' => 'SSN', |
|
| 243 | + 'EE_Capabilities' => 'CAP', |
|
| 244 | + 'EE_Cart' => 'CART', |
|
| 245 | + 'EE_Network_Config' => 'NET_CFG', |
|
| 246 | + 'EE_Request_Handler' => 'REQ', |
|
| 247 | + 'EE_Message_Resource_Manager' => 'MRM', |
|
| 248 | + 'EventEspresso\core\services\commands\CommandBus' => 'BUS', |
|
| 249 | + ) |
|
| 250 | + ); |
|
| 251 | + // class library |
|
| 252 | + $this->LIB = new stdClass(); |
|
| 253 | + $this->addons = new stdClass(); |
|
| 254 | + $this->modules = new stdClass(); |
|
| 255 | + $this->shortcodes = new stdClass(); |
|
| 256 | + $this->widgets = new stdClass(); |
|
| 257 | + $this->load_core('Base', array(), true); |
|
| 258 | + // add our request and response objects to the cache |
|
| 259 | + $request_loader = $this->_dependency_map->class_loader('EE_Request'); |
|
| 260 | + $this->_set_cached_class( |
|
| 261 | + $request_loader(), |
|
| 262 | + 'EE_Request' |
|
| 263 | + ); |
|
| 264 | + $response_loader = $this->_dependency_map->class_loader('EE_Response'); |
|
| 265 | + $this->_set_cached_class( |
|
| 266 | + $response_loader(), |
|
| 267 | + 'EE_Response' |
|
| 268 | + ); |
|
| 269 | + add_action('AHEE__EE_System__set_hooks_for_core', array($this, 'init')); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * init |
|
| 276 | + * |
|
| 277 | + * @access public |
|
| 278 | + * @return void |
|
| 279 | + */ |
|
| 280 | + public function init() |
|
| 281 | + { |
|
| 282 | + $this->AssetsRegistry = new Registry(); |
|
| 283 | + // Get current page protocol |
|
| 284 | + $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; |
|
| 285 | + // Output admin-ajax.php URL with same protocol as current page |
|
| 286 | + self::$i18n_js_strings['ajax_url'] = admin_url('admin-ajax.php', $protocol); |
|
| 287 | + self::$i18n_js_strings['wp_debug'] = defined('WP_DEBUG') ? WP_DEBUG : false; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * localize_i18n_js_strings |
|
| 294 | + * |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + public static function localize_i18n_js_strings() |
|
| 298 | + { |
|
| 299 | + $i18n_js_strings = (array)EE_Registry::$i18n_js_strings; |
|
| 300 | + foreach ($i18n_js_strings as $key => $value) { |
|
| 301 | + if (is_scalar($value)) { |
|
| 302 | + $i18n_js_strings[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8'); |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + return "/* <![CDATA[ */ var eei18n = " . wp_json_encode($i18n_js_strings) . '; /* ]]> */'; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param mixed string | EED_Module $module |
|
| 312 | + */ |
|
| 313 | + public function add_module($module) |
|
| 314 | + { |
|
| 315 | + if ($module instanceof EED_Module) { |
|
| 316 | + $module_class = get_class($module); |
|
| 317 | + $this->modules->{$module_class} = $module; |
|
| 318 | + } else { |
|
| 319 | + if ( ! class_exists('EE_Module_Request_Router')) { |
|
| 320 | + $this->load_core('Module_Request_Router'); |
|
| 321 | + } |
|
| 322 | + $this->modules->{$module} = EE_Module_Request_Router::module_factory($module); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * @param string $module_name |
|
| 330 | + * @return mixed EED_Module | NULL |
|
| 331 | + */ |
|
| 332 | + public function get_module($module_name = '') |
|
| 333 | + { |
|
| 334 | + return isset($this->modules->{$module_name}) ? $this->modules->{$module_name} : null; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * loads core classes - must be singletons |
|
| 341 | + * |
|
| 342 | + * @access public |
|
| 343 | + * @param string $class_name - simple class name ie: session |
|
| 344 | + * @param mixed $arguments |
|
| 345 | + * @param bool $load_only |
|
| 346 | + * @return mixed |
|
| 347 | + */ |
|
| 348 | + public function load_core($class_name, $arguments = array(), $load_only = false) |
|
| 349 | + { |
|
| 350 | + $core_paths = apply_filters( |
|
| 351 | + 'FHEE__EE_Registry__load_core__core_paths', |
|
| 352 | + array( |
|
| 353 | + EE_CORE, |
|
| 354 | + EE_ADMIN, |
|
| 355 | + EE_CPTS, |
|
| 356 | + EE_CORE . 'data_migration_scripts' . DS, |
|
| 357 | + EE_CORE . 'request_stack' . DS, |
|
| 358 | + EE_CORE . 'middleware' . DS, |
|
| 359 | + ) |
|
| 360 | + ); |
|
| 361 | + // retrieve instantiated class |
|
| 362 | + return $this->_load($core_paths, 'EE_', $class_name, 'core', $arguments, false, true, $load_only); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * loads service classes |
|
| 369 | + * |
|
| 370 | + * @access public |
|
| 371 | + * @param string $class_name - simple class name ie: session |
|
| 372 | + * @param mixed $arguments |
|
| 373 | + * @param bool $load_only |
|
| 374 | + * @return mixed |
|
| 375 | + */ |
|
| 376 | + public function load_service($class_name, $arguments = array(), $load_only = false) |
|
| 377 | + { |
|
| 378 | + $service_paths = apply_filters( |
|
| 379 | + 'FHEE__EE_Registry__load_service__service_paths', |
|
| 380 | + array( |
|
| 381 | + EE_CORE . 'services' . DS, |
|
| 382 | + ) |
|
| 383 | + ); |
|
| 384 | + // retrieve instantiated class |
|
| 385 | + return $this->_load($service_paths, 'EE_', $class_name, 'class', $arguments, false, true, $load_only); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * loads data_migration_scripts |
|
| 392 | + * |
|
| 393 | + * @access public |
|
| 394 | + * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 |
|
| 395 | + * @param mixed $arguments |
|
| 396 | + * @return EE_Data_Migration_Script_Base|mixed |
|
| 397 | + */ |
|
| 398 | + public function load_dms($class_name, $arguments = array()) |
|
| 399 | + { |
|
| 400 | + // retrieve instantiated class |
|
| 401 | + return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, false, false, false); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * loads object creating classes - must be singletons |
|
| 408 | + * |
|
| 409 | + * @param string $class_name - simple class name ie: attendee |
|
| 410 | + * @param mixed $arguments - an array of arguments to pass to the class |
|
| 411 | + * @param bool $from_db - some classes are instantiated from the db and thus call a different method to instantiate |
|
| 412 | + * @param bool $cache if you don't want the class to be stored in the internal cache (non-persistent) then set this to FALSE (ie. when instantiating model objects from client in a loop) |
|
| 413 | + * @param bool $load_only whether or not to just load the file and NOT instantiate, or load AND instantiate (default) |
|
| 414 | + * @return EE_Base_Class | bool |
|
| 415 | + */ |
|
| 416 | + public function load_class($class_name, $arguments = array(), $from_db = false, $cache = true, $load_only = false) |
|
| 417 | + { |
|
| 418 | + $paths = apply_filters('FHEE__EE_Registry__load_class__paths', array( |
|
| 419 | + EE_CORE, |
|
| 420 | + EE_CLASSES, |
|
| 421 | + EE_BUSINESS, |
|
| 422 | + )); |
|
| 423 | + // retrieve instantiated class |
|
| 424 | + return $this->_load($paths, 'EE_', $class_name, 'class', $arguments, $from_db, $cache, $load_only); |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * loads helper classes - must be singletons |
|
| 431 | + * |
|
| 432 | + * @param string $class_name - simple class name ie: price |
|
| 433 | + * @param mixed $arguments |
|
| 434 | + * @param bool $load_only |
|
| 435 | + * @return EEH_Base | bool |
|
| 436 | + */ |
|
| 437 | + public function load_helper($class_name, $arguments = array(), $load_only = true) |
|
| 438 | + { |
|
| 439 | + // todo: add doing_it_wrong() in a few versions after all addons have had calls to this method removed |
|
| 440 | + $helper_paths = apply_filters('FHEE__EE_Registry__load_helper__helper_paths', array(EE_HELPERS)); |
|
| 441 | + // retrieve instantiated class |
|
| 442 | + return $this->_load($helper_paths, 'EEH_', $class_name, 'helper', $arguments, false, true, $load_only); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * loads core classes - must be singletons |
|
| 449 | + * |
|
| 450 | + * @access public |
|
| 451 | + * @param string $class_name - simple class name ie: session |
|
| 452 | + * @param mixed $arguments |
|
| 453 | + * @param bool $load_only |
|
| 454 | + * @param bool $cache whether to cache the object or not. |
|
| 455 | + * @return mixed |
|
| 456 | + */ |
|
| 457 | + public function load_lib($class_name, $arguments = array(), $load_only = false, $cache = true) |
|
| 458 | + { |
|
| 459 | + $paths = array( |
|
| 460 | + EE_LIBRARIES, |
|
| 461 | + EE_LIBRARIES . 'messages' . DS, |
|
| 462 | + EE_LIBRARIES . 'shortcodes' . DS, |
|
| 463 | + EE_LIBRARIES . 'qtips' . DS, |
|
| 464 | + EE_LIBRARIES . 'payment_methods' . DS, |
|
| 465 | + ); |
|
| 466 | + // retrieve instantiated class |
|
| 467 | + return $this->_load($paths, 'EE_', $class_name, 'lib', $arguments, false, $cache, $load_only); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * loads model classes - must be singletons |
|
| 474 | + * |
|
| 475 | + * @param string $class_name - simple class name ie: price |
|
| 476 | + * @param mixed $arguments |
|
| 477 | + * @param bool $load_only |
|
| 478 | + * @return EEM_Base | bool |
|
| 479 | + */ |
|
| 480 | + public function load_model($class_name, $arguments = array(), $load_only = false) |
|
| 481 | + { |
|
| 482 | + $paths = apply_filters('FHEE__EE_Registry__load_model__paths', array( |
|
| 483 | + EE_MODELS, |
|
| 484 | + EE_CORE, |
|
| 485 | + )); |
|
| 486 | + // retrieve instantiated class |
|
| 487 | + return $this->_load($paths, 'EEM_', $class_name, 'model', $arguments, false, true, $load_only); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * loads model classes - must be singletons |
|
| 494 | + * |
|
| 495 | + * @param string $class_name - simple class name ie: price |
|
| 496 | + * @param mixed $arguments |
|
| 497 | + * @param bool $load_only |
|
| 498 | + * @return mixed | bool |
|
| 499 | + */ |
|
| 500 | + public function load_model_class($class_name, $arguments = array(), $load_only = true) |
|
| 501 | + { |
|
| 502 | + $paths = array( |
|
| 503 | + EE_MODELS . 'fields' . DS, |
|
| 504 | + EE_MODELS . 'helpers' . DS, |
|
| 505 | + EE_MODELS . 'relations' . DS, |
|
| 506 | + EE_MODELS . 'strategies' . DS, |
|
| 507 | + ); |
|
| 508 | + // retrieve instantiated class |
|
| 509 | + return $this->_load($paths, 'EE_', $class_name, '', $arguments, false, true, $load_only); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Determines if $model_name is the name of an actual EE model. |
|
| 516 | + * |
|
| 517 | + * @param string $model_name like Event, Attendee, Question_Group_Question, etc. |
|
| 518 | + * @return boolean |
|
| 519 | + */ |
|
| 520 | + public function is_model_name($model_name) |
|
| 521 | + { |
|
| 522 | + return isset($this->models[$model_name]) ? true : false; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * generic class loader |
|
| 529 | + * |
|
| 530 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 531 | + * @param string $file_name - file name ie: my_file.php, including extension |
|
| 532 | + * @param string $type - file type - core? class? helper? model? |
|
| 533 | + * @param mixed $arguments |
|
| 534 | + * @param bool $load_only |
|
| 535 | + * @return mixed |
|
| 536 | + */ |
|
| 537 | + public function load_file($path_to_file, $file_name, $type = '', $arguments = array(), $load_only = true) |
|
| 538 | + { |
|
| 539 | + // retrieve instantiated class |
|
| 540 | + return $this->_load($path_to_file, '', $file_name, $type, $arguments, false, true, $load_only); |
|
| 541 | + } |
|
| 542 | + |
|
| 543 | + |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * load_addon |
|
| 547 | + * |
|
| 548 | + * @param string $path_to_file - directory path to file location, not including filename |
|
| 549 | + * @param string $class_name - full class name ie: My_Class |
|
| 550 | + * @param string $type - file type - core? class? helper? model? |
|
| 551 | + * @param mixed $arguments |
|
| 552 | + * @param bool $load_only |
|
| 553 | + * @return EE_Addon |
|
| 554 | + */ |
|
| 555 | + public function load_addon($path_to_file, $class_name, $type = 'class', $arguments = array(), $load_only = false) |
|
| 556 | + { |
|
| 557 | + // retrieve instantiated class |
|
| 558 | + return $this->_load($path_to_file, 'addon', $class_name, $type, $arguments, false, true, $load_only); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * instantiates, caches, and automatically resolves dependencies |
|
| 565 | + * for classes that use a Fully Qualified Class Name. |
|
| 566 | + * if the class is not capable of being loaded using PSR-4 autoloading, |
|
| 567 | + * then you need to use one of the existing load_*() methods |
|
| 568 | + * which can resolve the classname and filepath from the passed arguments |
|
| 569 | + * |
|
| 570 | + * @param bool|string $class_name Fully Qualified Class Name |
|
| 571 | + * @param array $arguments an argument, or array of arguments to pass to the class upon instantiation |
|
| 572 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 573 | + * @param bool $from_db some classes are instantiated from the db |
|
| 574 | + * and thus call a different method to instantiate |
|
| 575 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 576 | + * @param bool|string $addon if true, will cache the object in the EE_Registry->$addons array |
|
| 577 | + * @return mixed null = failure to load or instantiate class object. |
|
| 578 | + * object = class loaded and instantiated successfully. |
|
| 579 | + * bool = fail or success when $load_only is true |
|
| 580 | + */ |
|
| 581 | + public function create( |
|
| 582 | + $class_name = false, |
|
| 583 | + $arguments = array(), |
|
| 584 | + $cache = false, |
|
| 585 | + $from_db = false, |
|
| 586 | + $load_only = false, |
|
| 587 | + $addon = false |
|
| 588 | + ) { |
|
| 589 | + $class_name = ltrim($class_name, '\\'); |
|
| 590 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 591 | + if ( ! class_exists($class_name)) { |
|
| 592 | + // maybe the class is registered with a preceding \ |
|
| 593 | + $class_name = strpos($class_name, '\\') !== 0 ? '\\' . $class_name : $class_name; |
|
| 594 | + // still doesn't exist ? |
|
| 595 | + if ( ! class_exists($class_name)) { |
|
| 596 | + return null; |
|
| 597 | + } |
|
| 598 | + } |
|
| 599 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 600 | + if ($load_only) { |
|
| 601 | + return true; |
|
| 602 | + } |
|
| 603 | + $addon = $addon ? 'addon' : ''; |
|
| 604 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 605 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 606 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 607 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 608 | + // return object if it's already cached |
|
| 609 | + $cached_class = $this->_get_cached_class($class_name, $addon); |
|
| 610 | + if ($cached_class !== null) { |
|
| 611 | + return $cached_class; |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + // instantiate the requested object |
|
| 615 | + $class_obj = $this->_create_object($class_name, $arguments, $addon, $from_db); |
|
| 616 | + if ($this->_cache_on && $cache) { |
|
| 617 | + // save it for later... kinda like gum { : $ |
|
| 618 | + $this->_set_cached_class($class_obj, $class_name, $addon, $from_db); |
|
| 619 | + } |
|
| 620 | + $this->_cache_on = true; |
|
| 621 | + return $class_obj; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * instantiates, caches, and injects dependencies for classes |
|
| 628 | + * |
|
| 629 | + * @param array $file_paths an array of paths to folders to look in |
|
| 630 | + * @param string $class_prefix EE or EEM or... ??? |
|
| 631 | + * @param bool|string $class_name $class name |
|
| 632 | + * @param string $type file type - core? class? helper? model? |
|
| 633 | + * @param mixed $arguments an argument or array of arguments to pass to the class upon instantiation |
|
| 634 | + * @param bool $from_db some classes are instantiated from the db |
|
| 635 | + * and thus call a different method to instantiate |
|
| 636 | + * @param bool $cache whether to cache the instantiated object for reuse |
|
| 637 | + * @param bool $load_only if true, will only load the file, but will NOT instantiate an object |
|
| 638 | + * @return null|object|bool null = failure to load or instantiate class object. |
|
| 639 | + * object = class loaded and instantiated successfully. |
|
| 640 | + * bool = fail or success when $load_only is true |
|
| 641 | + */ |
|
| 642 | + protected function _load( |
|
| 643 | + $file_paths = array(), |
|
| 644 | + $class_prefix = 'EE_', |
|
| 645 | + $class_name = false, |
|
| 646 | + $type = 'class', |
|
| 647 | + $arguments = array(), |
|
| 648 | + $from_db = false, |
|
| 649 | + $cache = true, |
|
| 650 | + $load_only = false |
|
| 651 | + ) { |
|
| 652 | + $class_name = ltrim($class_name, '\\'); |
|
| 653 | + // strip php file extension |
|
| 654 | + $class_name = str_replace('.php', '', trim($class_name)); |
|
| 655 | + // does the class have a prefix ? |
|
| 656 | + if ( ! empty($class_prefix) && $class_prefix != 'addon') { |
|
| 657 | + // make sure $class_prefix is uppercase |
|
| 658 | + $class_prefix = strtoupper(trim($class_prefix)); |
|
| 659 | + // add class prefix ONCE!!! |
|
| 660 | + $class_name = $class_prefix . str_replace($class_prefix, '', $class_name); |
|
| 661 | + } |
|
| 662 | + $class_name = $this->_dependency_map->get_alias($class_name); |
|
| 663 | + $class_exists = class_exists($class_name); |
|
| 664 | + // if we're only loading the class and it already exists, then let's just return true immediately |
|
| 665 | + if ($load_only && $class_exists) { |
|
| 666 | + return true; |
|
| 667 | + } |
|
| 668 | + // $this->_cache_on is toggled during the recursive loading that can occur with dependency injection |
|
| 669 | + // $cache is controlled by individual calls to separate Registry loader methods like load_class() |
|
| 670 | + // $load_only is also controlled by individual calls to separate Registry loader methods like load_file() |
|
| 671 | + if ($this->_cache_on && $cache && ! $load_only) { |
|
| 672 | + // return object if it's already cached |
|
| 673 | + $cached_class = $this->_get_cached_class($class_name, $class_prefix); |
|
| 674 | + if ($cached_class !== null) { |
|
| 675 | + return $cached_class; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + // if the class doesn't already exist.. then we need to try and find the file and load it |
|
| 679 | + if ( ! $class_exists) { |
|
| 680 | + // get full path to file |
|
| 681 | + $path = $this->_resolve_path($class_name, $type, $file_paths); |
|
| 682 | + // load the file |
|
| 683 | + $loaded = $this->_require_file($path, $class_name, $type, $file_paths); |
|
| 684 | + // if loading failed, or we are only loading a file but NOT instantiating an object |
|
| 685 | + if ( ! $loaded || $load_only) { |
|
| 686 | + // return boolean if only loading, or null if an object was expected |
|
| 687 | + return $load_only ? $loaded : null; |
|
| 688 | + } |
|
| 689 | + } |
|
| 690 | + // instantiate the requested object |
|
| 691 | + $class_obj = $this->_create_object($class_name, $arguments, $type, $from_db); |
|
| 692 | + if ($this->_cache_on && $cache) { |
|
| 693 | + // save it for later... kinda like gum { : $ |
|
| 694 | + $this->_set_cached_class($class_obj, $class_name, $class_prefix, $from_db); |
|
| 695 | + } |
|
| 696 | + $this->_cache_on = true; |
|
| 697 | + return $class_obj; |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + |
|
| 701 | + |
|
| 702 | + /** |
|
| 703 | + * _get_cached_class |
|
| 704 | + * attempts to find a cached version of the requested class |
|
| 705 | + * by looking in the following places: |
|
| 706 | + * $this->{$class_abbreviation} ie: $this->CART |
|
| 707 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 708 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 709 | + * $this->addon->{$class_name} ie: $this->addon->Some_Addon_Class |
|
| 710 | + * |
|
| 711 | + * @access protected |
|
| 712 | + * @param string $class_name |
|
| 713 | + * @param string $class_prefix |
|
| 714 | + * @return mixed |
|
| 715 | + */ |
|
| 716 | + protected function _get_cached_class($class_name, $class_prefix = '') |
|
| 717 | + { |
|
| 718 | + if (isset($this->_class_abbreviations[$class_name])) { |
|
| 719 | + $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 720 | + } else { |
|
| 721 | + // have to specify something, but not anything that will conflict |
|
| 722 | + $class_abbreviation = 'FANCY_BATMAN_PANTS'; |
|
| 723 | + } |
|
| 724 | + // check if class has already been loaded, and return it if it has been |
|
| 725 | + if (isset($this->{$class_abbreviation}) && ! is_null($this->{$class_abbreviation})) { |
|
| 726 | + return $this->{$class_abbreviation}; |
|
| 727 | + } else if (isset ($this->{$class_name})) { |
|
| 728 | + return $this->{$class_name}; |
|
| 729 | + } else if (isset ($this->LIB->{$class_name})) { |
|
| 730 | + return $this->LIB->{$class_name}; |
|
| 731 | + } else if ($class_prefix == 'addon' && isset ($this->addons->{$class_name})) { |
|
| 732 | + return $this->addons->{$class_name}; |
|
| 733 | + } |
|
| 734 | + return null; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * _resolve_path |
|
| 741 | + * attempts to find a full valid filepath for the requested class. |
|
| 742 | + * loops thru each of the base paths in the $file_paths array and appends : "{classname} . {file type} . php" |
|
| 743 | + * then returns that path if the target file has been found and is readable |
|
| 744 | + * |
|
| 745 | + * @access protected |
|
| 746 | + * @param string $class_name |
|
| 747 | + * @param string $type |
|
| 748 | + * @param array $file_paths |
|
| 749 | + * @return string | bool |
|
| 750 | + */ |
|
| 751 | + protected function _resolve_path($class_name, $type = '', $file_paths = array()) |
|
| 752 | + { |
|
| 753 | + // make sure $file_paths is an array |
|
| 754 | + $file_paths = is_array($file_paths) ? $file_paths : array($file_paths); |
|
| 755 | + // cycle thru paths |
|
| 756 | + foreach ($file_paths as $key => $file_path) { |
|
| 757 | + // convert all separators to proper DS, if no filepath, then use EE_CLASSES |
|
| 758 | + $file_path = $file_path ? str_replace(array('/', '\\'), DS, $file_path) : EE_CLASSES; |
|
| 759 | + // prep file type |
|
| 760 | + $type = ! empty($type) ? trim($type, '.') . '.' : ''; |
|
| 761 | + // build full file path |
|
| 762 | + $file_paths[$key] = rtrim($file_path, DS) . DS . $class_name . '.' . $type . 'php'; |
|
| 763 | + //does the file exist and can be read ? |
|
| 764 | + if (is_readable($file_paths[$key])) { |
|
| 765 | + return $file_paths[$key]; |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + return false; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * _require_file |
|
| 775 | + * basically just performs a require_once() |
|
| 776 | + * but with some error handling |
|
| 777 | + * |
|
| 778 | + * @access protected |
|
| 779 | + * @param string $path |
|
| 780 | + * @param string $class_name |
|
| 781 | + * @param string $type |
|
| 782 | + * @param array $file_paths |
|
| 783 | + * @return boolean |
|
| 784 | + * @throws \EE_Error |
|
| 785 | + */ |
|
| 786 | + protected function _require_file($path, $class_name, $type = '', $file_paths = array()) |
|
| 787 | + { |
|
| 788 | + // don't give up! you gotta... |
|
| 789 | + try { |
|
| 790 | + //does the file exist and can it be read ? |
|
| 791 | + if ( ! $path) { |
|
| 792 | + // so sorry, can't find the file |
|
| 793 | + throw new EE_Error ( |
|
| 794 | + sprintf( |
|
| 795 | + __('The %1$s file %2$s could not be located or is not readable due to file permissions. Please ensure that the following filepath(s) are correct: %3$s', 'event_espresso'), |
|
| 796 | + trim($type, '.'), |
|
| 797 | + $class_name, |
|
| 798 | + '<br />' . implode(',<br />', $file_paths) |
|
| 799 | + ) |
|
| 800 | + ); |
|
| 801 | + } |
|
| 802 | + // get the file |
|
| 803 | + require_once($path); |
|
| 804 | + // if the class isn't already declared somewhere |
|
| 805 | + if (class_exists($class_name, false) === false) { |
|
| 806 | + // so sorry, not a class |
|
| 807 | + throw new EE_Error( |
|
| 808 | + sprintf( |
|
| 809 | + __('The %s file %s does not appear to contain the %s Class.', 'event_espresso'), |
|
| 810 | + $type, |
|
| 811 | + $path, |
|
| 812 | + $class_name |
|
| 813 | + ) |
|
| 814 | + ); |
|
| 815 | + } |
|
| 816 | + } catch (EE_Error $e) { |
|
| 817 | + $e->get_error(); |
|
| 818 | + return false; |
|
| 819 | + } |
|
| 820 | + return true; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + |
|
| 824 | + |
|
| 825 | + /** |
|
| 826 | + * _create_object |
|
| 827 | + * Attempts to instantiate the requested class via any of the |
|
| 828 | + * commonly used instantiation methods employed throughout EE. |
|
| 829 | + * The priority for instantiation is as follows: |
|
| 830 | + * - abstract classes or any class flagged as "load only" (no instantiation occurs) |
|
| 831 | + * - model objects via their 'new_instance_from_db' method |
|
| 832 | + * - model objects via their 'new_instance' method |
|
| 833 | + * - "singleton" classes" via their 'instance' method |
|
| 834 | + * - standard instantiable classes via their __constructor |
|
| 835 | + * Prior to instantiation, if the classname exists in the dependency_map, |
|
| 836 | + * then the constructor for the requested class will be examined to determine |
|
| 837 | + * if any dependencies exist, and if they can be injected. |
|
| 838 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 839 | + * |
|
| 840 | + * @access protected |
|
| 841 | + * @param string $class_name |
|
| 842 | + * @param array $arguments |
|
| 843 | + * @param string $type |
|
| 844 | + * @param bool $from_db |
|
| 845 | + * @return null | object |
|
| 846 | + * @throws \EE_Error |
|
| 847 | + */ |
|
| 848 | + protected function _create_object($class_name, $arguments = array(), $type = '', $from_db = false) |
|
| 849 | + { |
|
| 850 | + $class_obj = null; |
|
| 851 | + $instantiation_mode = '0) none'; |
|
| 852 | + // don't give up! you gotta... |
|
| 853 | + try { |
|
| 854 | + // create reflection |
|
| 855 | + $reflector = $this->get_ReflectionClass($class_name); |
|
| 856 | + // make sure arguments are an array |
|
| 857 | + $arguments = is_array($arguments) ? $arguments : array($arguments); |
|
| 858 | + // and if arguments array is numerically and sequentially indexed, then we want it to remain as is, |
|
| 859 | + // else wrap it in an additional array so that it doesn't get split into multiple parameters |
|
| 860 | + $arguments = $this->_array_is_numerically_and_sequentially_indexed($arguments) |
|
| 861 | + ? $arguments |
|
| 862 | + : array($arguments); |
|
| 863 | + // attempt to inject dependencies ? |
|
| 864 | + if ($this->_dependency_map->has($class_name)) { |
|
| 865 | + $arguments = $this->_resolve_dependencies($reflector, $class_name, $arguments); |
|
| 866 | + } |
|
| 867 | + // instantiate the class if possible |
|
| 868 | + if ($reflector->isAbstract()) { |
|
| 869 | + // nothing to instantiate, loading file was enough |
|
| 870 | + // does not throw an exception so $instantiation_mode is unused |
|
| 871 | + // $instantiation_mode = "1) no constructor abstract class"; |
|
| 872 | + $class_obj = true; |
|
| 873 | + } else if ($reflector->getConstructor() === null && $reflector->isInstantiable() && empty($arguments)) { |
|
| 874 | + // no constructor = static methods only... nothing to instantiate, loading file was enough |
|
| 875 | + $instantiation_mode = "2) no constructor but instantiable"; |
|
| 876 | + $class_obj = $reflector->newInstance(); |
|
| 877 | + } else if ($from_db && method_exists($class_name, 'new_instance_from_db')) { |
|
| 878 | + $instantiation_mode = "3) new_instance_from_db()"; |
|
| 879 | + $class_obj = call_user_func_array(array($class_name, 'new_instance_from_db'), $arguments); |
|
| 880 | + } else if (method_exists($class_name, 'new_instance')) { |
|
| 881 | + $instantiation_mode = "4) new_instance()"; |
|
| 882 | + $class_obj = call_user_func_array(array($class_name, 'new_instance'), $arguments); |
|
| 883 | + } else if (method_exists($class_name, 'instance')) { |
|
| 884 | + $instantiation_mode = "5) instance()"; |
|
| 885 | + $class_obj = call_user_func_array(array($class_name, 'instance'), $arguments); |
|
| 886 | + } else if ($reflector->isInstantiable()) { |
|
| 887 | + $instantiation_mode = "6) constructor"; |
|
| 888 | + $class_obj = $reflector->newInstanceArgs($arguments); |
|
| 889 | + } else { |
|
| 890 | + // heh ? something's not right ! |
|
| 891 | + throw new EE_Error( |
|
| 892 | + sprintf( |
|
| 893 | + __('The %s file %s could not be instantiated.', 'event_espresso'), |
|
| 894 | + $type, |
|
| 895 | + $class_name |
|
| 896 | + ) |
|
| 897 | + ); |
|
| 898 | + } |
|
| 899 | + } catch (Exception $e) { |
|
| 900 | + if ( ! $e instanceof EE_Error) { |
|
| 901 | + $e = new EE_Error( |
|
| 902 | + sprintf( |
|
| 903 | + __('The following error occurred while attempting to instantiate "%1$s": %2$s %3$s %2$s instantiation mode : %4$s', 'event_espresso'), |
|
| 904 | + $class_name, |
|
| 905 | + '<br />', |
|
| 906 | + $e->getMessage(), |
|
| 907 | + $instantiation_mode |
|
| 908 | + ) |
|
| 909 | + ); |
|
| 910 | + } |
|
| 911 | + $e->get_error(); |
|
| 912 | + } |
|
| 913 | + return $class_obj; |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential |
|
| 920 | + * @param array $array |
|
| 921 | + * @return bool |
|
| 922 | + */ |
|
| 923 | + protected function _array_is_numerically_and_sequentially_indexed(array $array) |
|
| 924 | + { |
|
| 925 | + return ! empty($array) ? array_keys($array) === range(0, count($array) - 1) : true; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + |
|
| 929 | + |
|
| 930 | + /** |
|
| 931 | + * getReflectionClass |
|
| 932 | + * checks if a ReflectionClass object has already been generated for a class |
|
| 933 | + * and returns that instead of creating a new one |
|
| 934 | + * |
|
| 935 | + * @access public |
|
| 936 | + * @param string $class_name |
|
| 937 | + * @return ReflectionClass |
|
| 938 | + */ |
|
| 939 | + public function get_ReflectionClass($class_name) |
|
| 940 | + { |
|
| 941 | + if ( |
|
| 942 | + ! isset($this->_reflectors[$class_name]) |
|
| 943 | + || ! $this->_reflectors[$class_name] instanceof ReflectionClass |
|
| 944 | + ) { |
|
| 945 | + $this->_reflectors[$class_name] = new ReflectionClass($class_name); |
|
| 946 | + } |
|
| 947 | + return $this->_reflectors[$class_name]; |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + |
|
| 951 | + |
|
| 952 | + /** |
|
| 953 | + * _resolve_dependencies |
|
| 954 | + * examines the constructor for the requested class to determine |
|
| 955 | + * if any dependencies exist, and if they can be injected. |
|
| 956 | + * If so, then those classes will be added to the array of arguments passed to the constructor |
|
| 957 | + * PLZ NOTE: this is achieved by type hinting the constructor params |
|
| 958 | + * For example: |
|
| 959 | + * if attempting to load a class "Foo" with the following constructor: |
|
| 960 | + * __construct( Bar $bar_class, Fighter $grohl_class ) |
|
| 961 | + * then $bar_class and $grohl_class will be added to the $arguments array, |
|
| 962 | + * but only IF they are NOT already present in the incoming arguments array, |
|
| 963 | + * and the correct classes can be loaded |
|
| 964 | + * |
|
| 965 | + * @access protected |
|
| 966 | + * @param ReflectionClass $reflector |
|
| 967 | + * @param string $class_name |
|
| 968 | + * @param array $arguments |
|
| 969 | + * @return array |
|
| 970 | + * @throws \ReflectionException |
|
| 971 | + */ |
|
| 972 | + protected function _resolve_dependencies(ReflectionClass $reflector, $class_name, $arguments = array()) |
|
| 973 | + { |
|
| 974 | + // let's examine the constructor |
|
| 975 | + $constructor = $reflector->getConstructor(); |
|
| 976 | + // whu? huh? nothing? |
|
| 977 | + if ( ! $constructor) { |
|
| 978 | + return $arguments; |
|
| 979 | + } |
|
| 980 | + // get constructor parameters |
|
| 981 | + $params = $constructor->getParameters(); |
|
| 982 | + // and the keys for the incoming arguments array so that we can compare existing arguments with what is expected |
|
| 983 | + $argument_keys = array_keys($arguments); |
|
| 984 | + // now loop thru all of the constructors expected parameters |
|
| 985 | + foreach ($params as $index => $param) { |
|
| 986 | + // is this a dependency for a specific class ? |
|
| 987 | + $param_class = $param->getClass() ? $param->getClass()->name : null; |
|
| 988 | + if ( |
|
| 989 | + // param is not even a class |
|
| 990 | + empty($param_class) |
|
| 991 | + // and something already exists in the incoming arguments for this param |
|
| 992 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 993 | + ) { |
|
| 994 | + // so let's skip this argument and move on to the next |
|
| 995 | + continue; |
|
| 996 | + } else if ( |
|
| 997 | + // parameter is type hinted as a class, exists as an incoming argument, AND it's the correct class |
|
| 998 | + ! empty($param_class) |
|
| 999 | + && isset($argument_keys[$index], $arguments[$argument_keys[$index]]) |
|
| 1000 | + && $arguments[$argument_keys[$index]] instanceof $param_class |
|
| 1001 | + ) { |
|
| 1002 | + // skip this argument and move on to the next |
|
| 1003 | + continue; |
|
| 1004 | + } else if ( |
|
| 1005 | + // parameter is type hinted as a class, and should be injected |
|
| 1006 | + ! empty($param_class) |
|
| 1007 | + && $this->_dependency_map->has_dependency_for_class($class_name, $param_class) |
|
| 1008 | + ) { |
|
| 1009 | + $arguments = $this->_resolve_dependency($class_name, $param_class, $arguments, $index); |
|
| 1010 | + } else { |
|
| 1011 | + try { |
|
| 1012 | + $arguments[$index] = $param->getDefaultValue(); |
|
| 1013 | + } catch (ReflectionException $e) { |
|
| 1014 | + throw new ReflectionException( |
|
| 1015 | + sprintf( |
|
| 1016 | + __('%1$s for parameter "$%2$s"', 'event_espresso'), |
|
| 1017 | + $e->getMessage(), |
|
| 1018 | + $param->getName() |
|
| 1019 | + ) |
|
| 1020 | + ); |
|
| 1021 | + } |
|
| 1022 | + } |
|
| 1023 | + } |
|
| 1024 | + return $arguments; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * @access protected |
|
| 1031 | + * @param string $class_name |
|
| 1032 | + * @param string $param_class |
|
| 1033 | + * @param array $arguments |
|
| 1034 | + * @param mixed $index |
|
| 1035 | + * @return array |
|
| 1036 | + */ |
|
| 1037 | + protected function _resolve_dependency($class_name, $param_class, $arguments, $index) |
|
| 1038 | + { |
|
| 1039 | + $dependency = null; |
|
| 1040 | + // should dependency be loaded from cache ? |
|
| 1041 | + $cache_on = $this->_dependency_map->loading_strategy_for_class_dependency($class_name, $param_class) |
|
| 1042 | + !== EE_Dependency_Map::load_new_object |
|
| 1043 | + ? true |
|
| 1044 | + : false; |
|
| 1045 | + // we might have a dependency... |
|
| 1046 | + // let's MAYBE try and find it in our cache if that's what's been requested |
|
| 1047 | + $cached_class = $cache_on ? $this->_get_cached_class($param_class) : null; |
|
| 1048 | + // and grab it if it exists |
|
| 1049 | + if ($cached_class instanceof $param_class) { |
|
| 1050 | + $dependency = $cached_class; |
|
| 1051 | + } else if ($param_class != $class_name) { |
|
| 1052 | + // obtain the loader method from the dependency map |
|
| 1053 | + $loader = $this->_dependency_map->class_loader($param_class); |
|
| 1054 | + // is loader a custom closure ? |
|
| 1055 | + if ($loader instanceof Closure) { |
|
| 1056 | + $dependency = $loader(); |
|
| 1057 | + } else { |
|
| 1058 | + // set the cache on property for the recursive loading call |
|
| 1059 | + $this->_cache_on = $cache_on; |
|
| 1060 | + // if not, then let's try and load it via the registry |
|
| 1061 | + if (method_exists($this, $loader)) { |
|
| 1062 | + $dependency = $this->{$loader}($param_class); |
|
| 1063 | + } else { |
|
| 1064 | + $dependency = $this->create($param_class, array(), $cache_on); |
|
| 1065 | + } |
|
| 1066 | + } |
|
| 1067 | + } |
|
| 1068 | + // did we successfully find the correct dependency ? |
|
| 1069 | + if ($dependency instanceof $param_class) { |
|
| 1070 | + // then let's inject it into the incoming array of arguments at the correct location |
|
| 1071 | + if (isset($argument_keys[$index])) { |
|
| 1072 | + $arguments[$argument_keys[$index]] = $dependency; |
|
| 1073 | + } else { |
|
| 1074 | + $arguments[$index] = $dependency; |
|
| 1075 | + } |
|
| 1076 | + } |
|
| 1077 | + return $arguments; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + |
|
| 1081 | + |
|
| 1082 | + /** |
|
| 1083 | + * _set_cached_class |
|
| 1084 | + * attempts to cache the instantiated class locally |
|
| 1085 | + * in one of the following places, in the following order: |
|
| 1086 | + * $this->{class_abbreviation} ie: $this->CART |
|
| 1087 | + * $this->{$class_name} ie: $this->Some_Class |
|
| 1088 | + * $this->addon->{$$class_name} ie: $this->addon->Some_Addon_Class |
|
| 1089 | + * $this->LIB->{$class_name} ie: $this->LIB->Some_Class |
|
| 1090 | + * |
|
| 1091 | + * @access protected |
|
| 1092 | + * @param object $class_obj |
|
| 1093 | + * @param string $class_name |
|
| 1094 | + * @param string $class_prefix |
|
| 1095 | + * @param bool $from_db |
|
| 1096 | + * @return void |
|
| 1097 | + */ |
|
| 1098 | + protected function _set_cached_class($class_obj, $class_name, $class_prefix = '', $from_db = false) |
|
| 1099 | + { |
|
| 1100 | + if (empty($class_obj)) { |
|
| 1101 | + return; |
|
| 1102 | + } |
|
| 1103 | + // return newly instantiated class |
|
| 1104 | + if (isset($this->_class_abbreviations[$class_name])) { |
|
| 1105 | + $class_abbreviation = $this->_class_abbreviations[$class_name]; |
|
| 1106 | + $this->{$class_abbreviation} = $class_obj; |
|
| 1107 | + } else if (property_exists($this, $class_name)) { |
|
| 1108 | + $this->{$class_name} = $class_obj; |
|
| 1109 | + } else if ($class_prefix == 'addon') { |
|
| 1110 | + $this->addons->{$class_name} = $class_obj; |
|
| 1111 | + } else if ( ! $from_db) { |
|
| 1112 | + $this->LIB->{$class_name} = $class_obj; |
|
| 1113 | + } |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * call any loader that's been registered in the EE_Dependency_Map::$_class_loaders array |
|
| 1120 | + * |
|
| 1121 | + * @param string $classname PLEASE NOTE: the class name needs to match what's registered |
|
| 1122 | + * in the EE_Dependency_Map::$_class_loaders array, |
|
| 1123 | + * including the class prefix, ie: "EE_", "EEM_", "EEH_", etc |
|
| 1124 | + * @param array $arguments |
|
| 1125 | + * @return object |
|
| 1126 | + */ |
|
| 1127 | + public static function factory($classname, $arguments = array()) |
|
| 1128 | + { |
|
| 1129 | + $loader = self::instance()->_dependency_map->class_loader($classname); |
|
| 1130 | + if ($loader instanceof Closure) { |
|
| 1131 | + return $loader($arguments); |
|
| 1132 | + } else if (method_exists(EE_Registry::instance(), $loader)) { |
|
| 1133 | + return EE_Registry::instance()->{$loader}($classname, $arguments); |
|
| 1134 | + } |
|
| 1135 | + return null; |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + |
|
| 1140 | + /** |
|
| 1141 | + * Gets the addon by its name/slug (not classname. For that, just |
|
| 1142 | + * use the classname as the property name on EE_Config::instance()->addons) |
|
| 1143 | + * |
|
| 1144 | + * @param string $name |
|
| 1145 | + * @return EE_Addon |
|
| 1146 | + */ |
|
| 1147 | + public function get_addon_by_name($name) |
|
| 1148 | + { |
|
| 1149 | + foreach ($this->addons as $addon) { |
|
| 1150 | + if ($addon->name() == $name) { |
|
| 1151 | + return $addon; |
|
| 1152 | + } |
|
| 1153 | + } |
|
| 1154 | + return null; |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + |
|
| 1158 | + |
|
| 1159 | + /** |
|
| 1160 | + * Gets an array of all the registered addons, where the keys are their names. (ie, what each returns for their name() function) They're already available on EE_Config::instance()->addons as properties, where each property's name is |
|
| 1161 | + * the addon's classname. So if you just want to get the addon by classname, use EE_Config::instance()->addons->{classname} |
|
| 1162 | + * |
|
| 1163 | + * @return EE_Addon[] where the KEYS are the addon's name() |
|
| 1164 | + */ |
|
| 1165 | + public function get_addons_by_name() |
|
| 1166 | + { |
|
| 1167 | + $addons = array(); |
|
| 1168 | + foreach ($this->addons as $addon) { |
|
| 1169 | + $addons[$addon->name()] = $addon; |
|
| 1170 | + } |
|
| 1171 | + return $addons; |
|
| 1172 | + } |
|
| 1173 | + |
|
| 1174 | + |
|
| 1175 | + |
|
| 1176 | + /** |
|
| 1177 | + * Resets the specified model's instance AND makes sure EE_Registry doesn't keep |
|
| 1178 | + * a stale copy of it around |
|
| 1179 | + * |
|
| 1180 | + * @param string $model_name |
|
| 1181 | + * @return \EEM_Base |
|
| 1182 | + * @throws \EE_Error |
|
| 1183 | + */ |
|
| 1184 | + public function reset_model($model_name) |
|
| 1185 | + { |
|
| 1186 | + $model_class_name = strpos($model_name, 'EEM_') !== 0 ? "EEM_{$model_name}" : $model_name; |
|
| 1187 | + if ( ! isset($this->LIB->{$model_class_name}) || ! $this->LIB->{$model_class_name} instanceof EEM_Base) { |
|
| 1188 | + return null; |
|
| 1189 | + } |
|
| 1190 | + //get that model reset it and make sure we nuke the old reference to it |
|
| 1191 | + if ($this->LIB->{$model_class_name} instanceof $model_class_name && is_callable(array($model_class_name, 'reset'))) { |
|
| 1192 | + $this->LIB->{$model_class_name} = $this->LIB->{$model_class_name}->reset(); |
|
| 1193 | + } else { |
|
| 1194 | + throw new EE_Error(sprintf(__('Model %s does not have a method "reset"', 'event_espresso'), $model_name)); |
|
| 1195 | + } |
|
| 1196 | + return $this->LIB->{$model_class_name}; |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + |
|
| 1200 | + |
|
| 1201 | + /** |
|
| 1202 | + * Resets the registry. |
|
| 1203 | + * The criteria for what gets reset is based on what can be shared between sites on the same request when switch_to_blog |
|
| 1204 | + * is used in a multisite install. Here is a list of things that are NOT reset. |
|
| 1205 | + * - $_dependency_map |
|
| 1206 | + * - $_class_abbreviations |
|
| 1207 | + * - $NET_CFG (EE_Network_Config): The config is shared network wide so no need to reset. |
|
| 1208 | + * - $REQ: Still on the same request so no need to change. |
|
| 1209 | + * - $CAP: There is no site specific state in the EE_Capability class. |
|
| 1210 | + * - $SSN: Although ideally, the session should not be shared between site switches, we can't reset it because only one Session |
|
| 1211 | + * can be active in a single request. Resetting could resolve in "headers already sent" errors. |
|
| 1212 | + * - $addons: In multisite, the state of the addons is something controlled via hooks etc in a normal request. So |
|
| 1213 | + * for now, we won't reset the addons because it could break calls to an add-ons class/methods in the |
|
| 1214 | + * switch or on the restore. |
|
| 1215 | + * - $modules |
|
| 1216 | + * - $shortcodes |
|
| 1217 | + * - $widgets |
|
| 1218 | + * |
|
| 1219 | + * @param boolean $hard whether to reset data in the database too, or just refresh |
|
| 1220 | + * the Registry to its state at the beginning of the request |
|
| 1221 | + * @param boolean $reinstantiate whether to create new instances of EE_Registry's singletons too, |
|
| 1222 | + * or just reset without re-instantiating (handy to set to FALSE if you're not sure if you CAN |
|
| 1223 | + * currently reinstantiate the singletons at the moment) |
|
| 1224 | + * @param bool $reset_models Defaults to true. When false, then the models are not reset. This is so client |
|
| 1225 | + * code instead can just change the model context to a different blog id if necessary |
|
| 1226 | + * @return EE_Registry |
|
| 1227 | + */ |
|
| 1228 | + public static function reset($hard = false, $reinstantiate = true, $reset_models = true) |
|
| 1229 | + { |
|
| 1230 | + $instance = self::instance(); |
|
| 1231 | + EEH_Activation::reset(); |
|
| 1232 | + //properties that get reset |
|
| 1233 | + $instance->_cache_on = true; |
|
| 1234 | + $instance->CFG = EE_Config::reset($hard, $reinstantiate); |
|
| 1235 | + $instance->CART = null; |
|
| 1236 | + $instance->MRM = null; |
|
| 1237 | + $instance->AssetsRegistry = new Registry(); |
|
| 1238 | + //messages reset |
|
| 1239 | + EED_Messages::reset(); |
|
| 1240 | + if ($reset_models) { |
|
| 1241 | + foreach (array_keys($instance->non_abstract_db_models) as $model_name) { |
|
| 1242 | + $instance->reset_model($model_name); |
|
| 1243 | + } |
|
| 1244 | + } |
|
| 1245 | + $instance->LIB = new stdClass(); |
|
| 1246 | + return $instance; |
|
| 1247 | + } |
|
| 1248 | + |
|
| 1249 | + |
|
| 1250 | + |
|
| 1251 | + /** |
|
| 1252 | + * @override magic methods |
|
| 1253 | + * @return void |
|
| 1254 | + */ |
|
| 1255 | + final function __destruct() |
|
| 1256 | + { |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + |
|
| 1260 | + |
|
| 1261 | + /** |
|
| 1262 | + * @param $a |
|
| 1263 | + * @param $b |
|
| 1264 | + */ |
|
| 1265 | + final function __call($a, $b) |
|
| 1266 | + { |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + |
|
| 1270 | + |
|
| 1271 | + /** |
|
| 1272 | + * @param $a |
|
| 1273 | + */ |
|
| 1274 | + final function __get($a) |
|
| 1275 | + { |
|
| 1276 | + } |
|
| 1277 | + |
|
| 1278 | + |
|
| 1279 | + |
|
| 1280 | + /** |
|
| 1281 | + * @param $a |
|
| 1282 | + * @param $b |
|
| 1283 | + */ |
|
| 1284 | + final function __set($a, $b) |
|
| 1285 | + { |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + |
|
| 1289 | + |
|
| 1290 | + /** |
|
| 1291 | + * @param $a |
|
| 1292 | + */ |
|
| 1293 | + final function __isset($a) |
|
| 1294 | + { |
|
| 1295 | + } |
|
| 1296 | 1296 | |
| 1297 | 1297 | |
| 1298 | 1298 | |
| 1299 | - /** |
|
| 1300 | - * @param $a |
|
| 1301 | - */ |
|
| 1302 | - final function __unset($a) |
|
| 1303 | - { |
|
| 1304 | - } |
|
| 1299 | + /** |
|
| 1300 | + * @param $a |
|
| 1301 | + */ |
|
| 1302 | + final function __unset($a) |
|
| 1303 | + { |
|
| 1304 | + } |
|
| 1305 | 1305 | |
| 1306 | 1306 | |
| 1307 | 1307 | |
| 1308 | - /** |
|
| 1309 | - * @return array |
|
| 1310 | - */ |
|
| 1311 | - final function __sleep() |
|
| 1312 | - { |
|
| 1313 | - return array(); |
|
| 1314 | - } |
|
| 1308 | + /** |
|
| 1309 | + * @return array |
|
| 1310 | + */ |
|
| 1311 | + final function __sleep() |
|
| 1312 | + { |
|
| 1313 | + return array(); |
|
| 1314 | + } |
|
| 1315 | 1315 | |
| 1316 | 1316 | |
| 1317 | 1317 | |
| 1318 | - final function __wakeup() |
|
| 1319 | - { |
|
| 1320 | - } |
|
| 1318 | + final function __wakeup() |
|
| 1319 | + { |
|
| 1320 | + } |
|
| 1321 | 1321 | |
| 1322 | 1322 | |
| 1323 | 1323 | |
| 1324 | - /** |
|
| 1325 | - * @return string |
|
| 1326 | - */ |
|
| 1327 | - final function __toString() |
|
| 1328 | - { |
|
| 1329 | - return ''; |
|
| 1330 | - } |
|
| 1324 | + /** |
|
| 1325 | + * @return string |
|
| 1326 | + */ |
|
| 1327 | + final function __toString() |
|
| 1328 | + { |
|
| 1329 | + return ''; |
|
| 1330 | + } |
|
| 1331 | 1331 | |
| 1332 | 1332 | |
| 1333 | 1333 | |
| 1334 | - final function __invoke() |
|
| 1335 | - { |
|
| 1336 | - } |
|
| 1334 | + final function __invoke() |
|
| 1335 | + { |
|
| 1336 | + } |
|
| 1337 | 1337 | |
| 1338 | 1338 | |
| 1339 | 1339 | |
| 1340 | - final static function __set_state() |
|
| 1341 | - { |
|
| 1342 | - } |
|
| 1340 | + final static function __set_state() |
|
| 1341 | + { |
|
| 1342 | + } |
|
| 1343 | 1343 | |
| 1344 | 1344 | |
| 1345 | 1345 | |
| 1346 | - final function __clone() |
|
| 1347 | - { |
|
| 1348 | - } |
|
| 1346 | + final function __clone() |
|
| 1347 | + { |
|
| 1348 | + } |
|
| 1349 | 1349 | |
| 1350 | 1350 | |
| 1351 | 1351 | |
| 1352 | - /** |
|
| 1353 | - * @param $a |
|
| 1354 | - * @param $b |
|
| 1355 | - */ |
|
| 1356 | - final static function __callStatic($a, $b) |
|
| 1357 | - { |
|
| 1358 | - } |
|
| 1352 | + /** |
|
| 1353 | + * @param $a |
|
| 1354 | + * @param $b |
|
| 1355 | + */ |
|
| 1356 | + final static function __callStatic($a, $b) |
|
| 1357 | + { |
|
| 1358 | + } |
|
| 1359 | 1359 | |
| 1360 | 1360 | |
| 1361 | 1361 | |
| 1362 | - /** |
|
| 1363 | - * Gets all the custom post type models defined |
|
| 1364 | - * |
|
| 1365 | - * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1366 | - */ |
|
| 1367 | - public function cpt_models() |
|
| 1368 | - { |
|
| 1369 | - $cpt_models = array(); |
|
| 1370 | - foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1371 | - if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1372 | - $cpt_models[$short_name] = $classname; |
|
| 1373 | - } |
|
| 1374 | - } |
|
| 1375 | - return $cpt_models; |
|
| 1376 | - } |
|
| 1362 | + /** |
|
| 1363 | + * Gets all the custom post type models defined |
|
| 1364 | + * |
|
| 1365 | + * @return array keys are model "short names" (Eg "Event") and keys are classnames (eg "EEM_Event") |
|
| 1366 | + */ |
|
| 1367 | + public function cpt_models() |
|
| 1368 | + { |
|
| 1369 | + $cpt_models = array(); |
|
| 1370 | + foreach ($this->non_abstract_db_models as $short_name => $classname) { |
|
| 1371 | + if (is_subclass_of($classname, 'EEM_CPT_Base')) { |
|
| 1372 | + $cpt_models[$short_name] = $classname; |
|
| 1373 | + } |
|
| 1374 | + } |
|
| 1375 | + return $cpt_models; |
|
| 1376 | + } |
|
| 1377 | 1377 | |
| 1378 | 1378 | |
| 1379 | 1379 | |
| 1380 | - /** |
|
| 1381 | - * @return \EE_Config |
|
| 1382 | - */ |
|
| 1383 | - public static function CFG() |
|
| 1384 | - { |
|
| 1385 | - return self::instance()->CFG; |
|
| 1386 | - } |
|
| 1380 | + /** |
|
| 1381 | + * @return \EE_Config |
|
| 1382 | + */ |
|
| 1383 | + public static function CFG() |
|
| 1384 | + { |
|
| 1385 | + return self::instance()->CFG; |
|
| 1386 | + } |
|
| 1387 | 1387 | |
| 1388 | 1388 | |
| 1389 | 1389 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | } |
| 64 | 64 | } |
| 65 | 65 | return $this->shortcodeContent( |
| 66 | - $this->sanitizeAttributes((array)$attributes) |
|
| 66 | + $this->sanitizeAttributes((array) $attributes) |
|
| 67 | 67 | ); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | // serialized attributes |
| 92 | 92 | wp_json_encode($attributes), |
| 93 | 93 | // Closure for generating content if cache is expired |
| 94 | - function () use ($shortcode, $attributes) { |
|
| 94 | + function() use ($shortcode, $attributes) { |
|
| 95 | 95 | return $shortcode->processShortcode($attributes); |
| 96 | 96 | }, |
| 97 | 97 | // filterable cache expiration set by each shortcode |
@@ -20,198 +20,198 @@ |
||
| 20 | 20 | abstract class EspressoShortcode implements ShortcodeInterface |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * transient prefix |
|
| 25 | - * |
|
| 26 | - * @type string |
|
| 27 | - */ |
|
| 28 | - const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var PostRelatedCacheManager $cache_manager |
|
| 32 | - */ |
|
| 33 | - private $cache_manager; |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * EspressoShortcode constructor |
|
| 39 | - * |
|
| 40 | - * @param PostRelatedCacheManager $cache_manager |
|
| 41 | - */ |
|
| 42 | - public function __construct(PostRelatedCacheManager $cache_manager) |
|
| 43 | - { |
|
| 44 | - $this->cache_manager = $cache_manager; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * enqueues scripts then processes the shortcode |
|
| 51 | - * |
|
| 52 | - * @param array $attributes |
|
| 53 | - * @return string |
|
| 54 | - * @throws \EE_Error |
|
| 55 | - */ |
|
| 56 | - final public function processShortcodeCallback($attributes = array()) |
|
| 57 | - { |
|
| 58 | - if ($this instanceof EnqueueAssetsInterface) { |
|
| 59 | - if (is_admin()) { |
|
| 60 | - $this->enqueueAdminScripts(); |
|
| 61 | - } else { |
|
| 62 | - $this->enqueueScripts(); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - return $this->shortcodeContent( |
|
| 66 | - $this->sanitizeAttributes((array)$attributes) |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * If shortcode caching is enabled for the shortcode, |
|
| 74 | - * and cached results exist, then that will be returned |
|
| 75 | - * else new content will be generated. |
|
| 76 | - * If caching is enabled, then the new content will be cached for later. |
|
| 77 | - * |
|
| 78 | - * @param array $attributes |
|
| 79 | - * @return mixed|string |
|
| 80 | - * @throws \EE_Error |
|
| 81 | - */ |
|
| 82 | - private function shortcodeContent(array $attributes) |
|
| 83 | - { |
|
| 84 | - $shortcode = $this; |
|
| 85 | - $post_ID = $this->currentPostID(); |
|
| 86 | - // something like "SC_EVENTS-123" |
|
| 87 | - $cache_ID = $this->shortcodeCacheID($post_ID); |
|
| 88 | - $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
| 89 | - return $this->cache_manager->get( |
|
| 90 | - $cache_ID, |
|
| 91 | - // serialized attributes |
|
| 92 | - wp_json_encode($attributes), |
|
| 93 | - // Closure for generating content if cache is expired |
|
| 94 | - function () use ($shortcode, $attributes) { |
|
| 95 | - return $shortcode->processShortcode($attributes); |
|
| 96 | - }, |
|
| 97 | - // filterable cache expiration set by each shortcode |
|
| 98 | - apply_filters( |
|
| 99 | - 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
| 100 | - $this->cacheExpiration(), |
|
| 101 | - $this->getTag(), |
|
| 102 | - $this |
|
| 103 | - ) |
|
| 104 | - ); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return int |
|
| 111 | - * @throws \EE_Error |
|
| 112 | - */ |
|
| 113 | - private function currentPostID() |
|
| 114 | - { |
|
| 115 | - // try to get EE_Event any way we can |
|
| 116 | - $event = EEH_Event_View::get_event(); |
|
| 117 | - // then get some kind of ID |
|
| 118 | - if ($event instanceof \EE_Event) { |
|
| 119 | - $post_ID = $event->ID(); |
|
| 120 | - } else { |
|
| 121 | - global $post; |
|
| 122 | - $post_ID = $post->ID; |
|
| 123 | - } |
|
| 124 | - return $post_ID; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param int $post_ID |
|
| 131 | - * @return string |
|
| 132 | - * @throws \EE_Error |
|
| 133 | - */ |
|
| 134 | - private function shortcodeCacheID($post_ID) |
|
| 135 | - { |
|
| 136 | - $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
| 137 | - return "SC_{$tag}-{$post_ID}"; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * array for defining custom attribute sanitization callbacks, |
|
| 144 | - * where keys match keys in your attributes array, |
|
| 145 | - * and values represent the sanitization function you wish to be applied to that attribute. |
|
| 146 | - * So for example, if you had an integer attribute named "event_id" |
|
| 147 | - * that you wanted to be sanitized using absint(), |
|
| 148 | - * then you would return the following: |
|
| 149 | - * array('event_id' => 'absint') |
|
| 150 | - * Entering 'skip_sanitization' for the callback value |
|
| 151 | - * means that no sanitization will be applied |
|
| 152 | - * on the assumption that the attribute |
|
| 153 | - * will be sanitized at some point... right? |
|
| 154 | - * You wouldn't pass around unsanitized attributes would you? |
|
| 155 | - * That would be very Tom Foolery of you!!! |
|
| 156 | - * |
|
| 157 | - * @return array |
|
| 158 | - */ |
|
| 159 | - protected function customAttributeSanitizationMap() |
|
| 160 | - { |
|
| 161 | - return array(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Performs basic sanitization on shortcode attributes |
|
| 168 | - * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
| 169 | - * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
| 170 | - * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
| 171 | - * all other attributes would be sanitized using the defaults in the switch statement below |
|
| 172 | - * |
|
| 173 | - * @param array $attributes |
|
| 174 | - * @return array |
|
| 175 | - */ |
|
| 176 | - private function sanitizeAttributes(array $attributes) |
|
| 177 | - { |
|
| 178 | - $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
| 179 | - foreach ($attributes as $key => $value) { |
|
| 180 | - // is a custom sanitization callback specified ? |
|
| 181 | - if (isset($custom_sanitization[$key])) { |
|
| 182 | - $callback = $custom_sanitization[$key]; |
|
| 183 | - if ($callback === 'skip_sanitization') { |
|
| 184 | - $attributes[$key] = $value; |
|
| 185 | - continue; |
|
| 186 | - } |
|
| 187 | - if (function_exists($callback)) { |
|
| 188 | - $attributes[$key] = $callback($value); |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - switch (true) { |
|
| 193 | - case $value === null : |
|
| 194 | - case is_int($value) : |
|
| 195 | - case is_float($value) : |
|
| 196 | - // typical booleans |
|
| 197 | - case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true) : |
|
| 198 | - $attributes[$key] = $value; |
|
| 199 | - break; |
|
| 200 | - case is_string($value) : |
|
| 201 | - $attributes[$key] = sanitize_text_field($value); |
|
| 202 | - break; |
|
| 203 | - case is_array($value) : |
|
| 204 | - $attributes[$key] = $this->sanitizeAttributes($value); |
|
| 205 | - break; |
|
| 206 | - default : |
|
| 207 | - // only remaining data types are Object and Resource |
|
| 208 | - // which are not allowed as shortcode attributes |
|
| 209 | - $attributes[$key] = null; |
|
| 210 | - break; |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - return $attributes; |
|
| 214 | - } |
|
| 23 | + /** |
|
| 24 | + * transient prefix |
|
| 25 | + * |
|
| 26 | + * @type string |
|
| 27 | + */ |
|
| 28 | + const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var PostRelatedCacheManager $cache_manager |
|
| 32 | + */ |
|
| 33 | + private $cache_manager; |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * EspressoShortcode constructor |
|
| 39 | + * |
|
| 40 | + * @param PostRelatedCacheManager $cache_manager |
|
| 41 | + */ |
|
| 42 | + public function __construct(PostRelatedCacheManager $cache_manager) |
|
| 43 | + { |
|
| 44 | + $this->cache_manager = $cache_manager; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * enqueues scripts then processes the shortcode |
|
| 51 | + * |
|
| 52 | + * @param array $attributes |
|
| 53 | + * @return string |
|
| 54 | + * @throws \EE_Error |
|
| 55 | + */ |
|
| 56 | + final public function processShortcodeCallback($attributes = array()) |
|
| 57 | + { |
|
| 58 | + if ($this instanceof EnqueueAssetsInterface) { |
|
| 59 | + if (is_admin()) { |
|
| 60 | + $this->enqueueAdminScripts(); |
|
| 61 | + } else { |
|
| 62 | + $this->enqueueScripts(); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + return $this->shortcodeContent( |
|
| 66 | + $this->sanitizeAttributes((array)$attributes) |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * If shortcode caching is enabled for the shortcode, |
|
| 74 | + * and cached results exist, then that will be returned |
|
| 75 | + * else new content will be generated. |
|
| 76 | + * If caching is enabled, then the new content will be cached for later. |
|
| 77 | + * |
|
| 78 | + * @param array $attributes |
|
| 79 | + * @return mixed|string |
|
| 80 | + * @throws \EE_Error |
|
| 81 | + */ |
|
| 82 | + private function shortcodeContent(array $attributes) |
|
| 83 | + { |
|
| 84 | + $shortcode = $this; |
|
| 85 | + $post_ID = $this->currentPostID(); |
|
| 86 | + // something like "SC_EVENTS-123" |
|
| 87 | + $cache_ID = $this->shortcodeCacheID($post_ID); |
|
| 88 | + $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
| 89 | + return $this->cache_manager->get( |
|
| 90 | + $cache_ID, |
|
| 91 | + // serialized attributes |
|
| 92 | + wp_json_encode($attributes), |
|
| 93 | + // Closure for generating content if cache is expired |
|
| 94 | + function () use ($shortcode, $attributes) { |
|
| 95 | + return $shortcode->processShortcode($attributes); |
|
| 96 | + }, |
|
| 97 | + // filterable cache expiration set by each shortcode |
|
| 98 | + apply_filters( |
|
| 99 | + 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
| 100 | + $this->cacheExpiration(), |
|
| 101 | + $this->getTag(), |
|
| 102 | + $this |
|
| 103 | + ) |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return int |
|
| 111 | + * @throws \EE_Error |
|
| 112 | + */ |
|
| 113 | + private function currentPostID() |
|
| 114 | + { |
|
| 115 | + // try to get EE_Event any way we can |
|
| 116 | + $event = EEH_Event_View::get_event(); |
|
| 117 | + // then get some kind of ID |
|
| 118 | + if ($event instanceof \EE_Event) { |
|
| 119 | + $post_ID = $event->ID(); |
|
| 120 | + } else { |
|
| 121 | + global $post; |
|
| 122 | + $post_ID = $post->ID; |
|
| 123 | + } |
|
| 124 | + return $post_ID; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param int $post_ID |
|
| 131 | + * @return string |
|
| 132 | + * @throws \EE_Error |
|
| 133 | + */ |
|
| 134 | + private function shortcodeCacheID($post_ID) |
|
| 135 | + { |
|
| 136 | + $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
| 137 | + return "SC_{$tag}-{$post_ID}"; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * array for defining custom attribute sanitization callbacks, |
|
| 144 | + * where keys match keys in your attributes array, |
|
| 145 | + * and values represent the sanitization function you wish to be applied to that attribute. |
|
| 146 | + * So for example, if you had an integer attribute named "event_id" |
|
| 147 | + * that you wanted to be sanitized using absint(), |
|
| 148 | + * then you would return the following: |
|
| 149 | + * array('event_id' => 'absint') |
|
| 150 | + * Entering 'skip_sanitization' for the callback value |
|
| 151 | + * means that no sanitization will be applied |
|
| 152 | + * on the assumption that the attribute |
|
| 153 | + * will be sanitized at some point... right? |
|
| 154 | + * You wouldn't pass around unsanitized attributes would you? |
|
| 155 | + * That would be very Tom Foolery of you!!! |
|
| 156 | + * |
|
| 157 | + * @return array |
|
| 158 | + */ |
|
| 159 | + protected function customAttributeSanitizationMap() |
|
| 160 | + { |
|
| 161 | + return array(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Performs basic sanitization on shortcode attributes |
|
| 168 | + * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
| 169 | + * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
| 170 | + * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
| 171 | + * all other attributes would be sanitized using the defaults in the switch statement below |
|
| 172 | + * |
|
| 173 | + * @param array $attributes |
|
| 174 | + * @return array |
|
| 175 | + */ |
|
| 176 | + private function sanitizeAttributes(array $attributes) |
|
| 177 | + { |
|
| 178 | + $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
| 179 | + foreach ($attributes as $key => $value) { |
|
| 180 | + // is a custom sanitization callback specified ? |
|
| 181 | + if (isset($custom_sanitization[$key])) { |
|
| 182 | + $callback = $custom_sanitization[$key]; |
|
| 183 | + if ($callback === 'skip_sanitization') { |
|
| 184 | + $attributes[$key] = $value; |
|
| 185 | + continue; |
|
| 186 | + } |
|
| 187 | + if (function_exists($callback)) { |
|
| 188 | + $attributes[$key] = $callback($value); |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + switch (true) { |
|
| 193 | + case $value === null : |
|
| 194 | + case is_int($value) : |
|
| 195 | + case is_float($value) : |
|
| 196 | + // typical booleans |
|
| 197 | + case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true) : |
|
| 198 | + $attributes[$key] = $value; |
|
| 199 | + break; |
|
| 200 | + case is_string($value) : |
|
| 201 | + $attributes[$key] = sanitize_text_field($value); |
|
| 202 | + break; |
|
| 203 | + case is_array($value) : |
|
| 204 | + $attributes[$key] = $this->sanitizeAttributes($value); |
|
| 205 | + break; |
|
| 206 | + default : |
|
| 207 | + // only remaining data types are Object and Resource |
|
| 208 | + // which are not allowed as shortcode attributes |
|
| 209 | + $attributes[$key] = null; |
|
| 210 | + break; |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + return $attributes; |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | 216 | |
| 217 | 217 | |
@@ -91,7 +91,7 @@ |
||
| 91 | 91 | $cache_id |
| 92 | 92 | ) |
| 93 | 93 | ); |
| 94 | - $cache_id = substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182); |
|
| 94 | + $cache_id = substr($this->cachePrefix().$id_prefix.'-'.md5($cache_id), 0, 182); |
|
| 95 | 95 | // is caching enabled for this content ? |
| 96 | 96 | if ($expiration) { |
| 97 | 97 | $content = $this->cache_storage->get($cache_id); |
@@ -18,117 +18,117 @@ |
||
| 18 | 18 | class BasicCacheManager implements CacheManagerInterface |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @type string |
|
| 23 | - */ |
|
| 24 | - const CACHE_PREFIX = 'ee_cache_'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * set to true to monitor when content is being served from cache or not |
|
| 28 | - * |
|
| 29 | - * @type boolean |
|
| 30 | - */ |
|
| 31 | - const DEBUG = false; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var CacheStorageInterface $cache_storage |
|
| 35 | - */ |
|
| 36 | - private $cache_storage; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * BasicCacheManager constructor. |
|
| 42 | - * |
|
| 43 | - * @param CacheStorageInterface $cache_storage [required] |
|
| 44 | - */ |
|
| 45 | - public function __construct(CacheStorageInterface $cache_storage) |
|
| 46 | - { |
|
| 47 | - $this->cache_storage = $cache_storage; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * returns a string that will be prepended to all cache identifiers |
|
| 54 | - * |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public function cachePrefix() |
|
| 58 | - { |
|
| 59 | - return BasicCacheManager::CACHE_PREFIX; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 66 | - * May also be helpful to include an additional specific identifier, |
|
| 67 | - * such as a post ID as part of the $id_prefix so that individual caches |
|
| 68 | - * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 69 | - * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 70 | - * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
| 71 | - * It is advisable to use some of the actual data |
|
| 72 | - * that is used to generate the content being cached, |
|
| 73 | - * in order to guarantee that the cache id is unique for that content. |
|
| 74 | - * The cache id will be md5'd before usage to make it more db friendly, |
|
| 75 | - * and the entire cache id string will be truncated to 190 characters. |
|
| 76 | - * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
| 77 | - * necessary, |
|
| 78 | - * we wrap our content creation in a Closure so that it is not executed until needed. |
|
| 79 | - * @param int $expiration |
|
| 80 | - * @return Closure|mixed |
|
| 81 | - */ |
|
| 82 | - public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS) |
|
| 83 | - { |
|
| 84 | - $content = ''; |
|
| 85 | - // how long should we cache this content for? 0 means no caching. |
|
| 86 | - $expiration = absint( |
|
| 87 | - apply_filters( |
|
| 88 | - 'FHEE__CacheManager__get__cache_expiration', |
|
| 89 | - $expiration, |
|
| 90 | - $id_prefix, |
|
| 91 | - $cache_id |
|
| 92 | - ) |
|
| 93 | - ); |
|
| 94 | - $cache_id = substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182); |
|
| 95 | - // is caching enabled for this content ? |
|
| 96 | - if ($expiration) { |
|
| 97 | - $content = $this->cache_storage->get($cache_id); |
|
| 98 | - } |
|
| 99 | - // any existing content ? |
|
| 100 | - if (empty($content)) { |
|
| 101 | - // nope! let's generate some new stuff |
|
| 102 | - $content = $callback(); |
|
| 103 | - // save the new content if caching is enabled |
|
| 104 | - if ($expiration) { |
|
| 105 | - if (BasicCacheManager::DEBUG) { |
|
| 106 | - \EEH_Debug_Tools::printr($cache_id, 'REFRESH CACHE', __FILE__, __LINE__); |
|
| 107 | - } |
|
| 108 | - $this->cache_storage->add($cache_id, $content, $expiration); |
|
| 109 | - } |
|
| 110 | - } else { |
|
| 111 | - if (BasicCacheManager::DEBUG) { |
|
| 112 | - \EEH_Debug_Tools::printr($cache_id, 'CACHED CONTENT', __FILE__, __LINE__); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - return $content; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
| 122 | - * or a specific ID targeting a single cache item |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function clear($cache_id) |
|
| 126 | - { |
|
| 127 | - // ensure incoming arg is in an array |
|
| 128 | - $cache_id = is_array($cache_id) ? $cache_id : array($cache_id); |
|
| 129 | - // delete corresponding transients for the supplied id prefix |
|
| 130 | - $this->cache_storage->deleteMany($cache_id); |
|
| 131 | - } |
|
| 21 | + /** |
|
| 22 | + * @type string |
|
| 23 | + */ |
|
| 24 | + const CACHE_PREFIX = 'ee_cache_'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * set to true to monitor when content is being served from cache or not |
|
| 28 | + * |
|
| 29 | + * @type boolean |
|
| 30 | + */ |
|
| 31 | + const DEBUG = false; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var CacheStorageInterface $cache_storage |
|
| 35 | + */ |
|
| 36 | + private $cache_storage; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * BasicCacheManager constructor. |
|
| 42 | + * |
|
| 43 | + * @param CacheStorageInterface $cache_storage [required] |
|
| 44 | + */ |
|
| 45 | + public function __construct(CacheStorageInterface $cache_storage) |
|
| 46 | + { |
|
| 47 | + $this->cache_storage = $cache_storage; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * returns a string that will be prepended to all cache identifiers |
|
| 54 | + * |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public function cachePrefix() |
|
| 58 | + { |
|
| 59 | + return BasicCacheManager::CACHE_PREFIX; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 66 | + * May also be helpful to include an additional specific identifier, |
|
| 67 | + * such as a post ID as part of the $id_prefix so that individual caches |
|
| 68 | + * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 69 | + * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 70 | + * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
| 71 | + * It is advisable to use some of the actual data |
|
| 72 | + * that is used to generate the content being cached, |
|
| 73 | + * in order to guarantee that the cache id is unique for that content. |
|
| 74 | + * The cache id will be md5'd before usage to make it more db friendly, |
|
| 75 | + * and the entire cache id string will be truncated to 190 characters. |
|
| 76 | + * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
| 77 | + * necessary, |
|
| 78 | + * we wrap our content creation in a Closure so that it is not executed until needed. |
|
| 79 | + * @param int $expiration |
|
| 80 | + * @return Closure|mixed |
|
| 81 | + */ |
|
| 82 | + public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS) |
|
| 83 | + { |
|
| 84 | + $content = ''; |
|
| 85 | + // how long should we cache this content for? 0 means no caching. |
|
| 86 | + $expiration = absint( |
|
| 87 | + apply_filters( |
|
| 88 | + 'FHEE__CacheManager__get__cache_expiration', |
|
| 89 | + $expiration, |
|
| 90 | + $id_prefix, |
|
| 91 | + $cache_id |
|
| 92 | + ) |
|
| 93 | + ); |
|
| 94 | + $cache_id = substr($this->cachePrefix() . $id_prefix . '-' . md5($cache_id), 0, 182); |
|
| 95 | + // is caching enabled for this content ? |
|
| 96 | + if ($expiration) { |
|
| 97 | + $content = $this->cache_storage->get($cache_id); |
|
| 98 | + } |
|
| 99 | + // any existing content ? |
|
| 100 | + if (empty($content)) { |
|
| 101 | + // nope! let's generate some new stuff |
|
| 102 | + $content = $callback(); |
|
| 103 | + // save the new content if caching is enabled |
|
| 104 | + if ($expiration) { |
|
| 105 | + if (BasicCacheManager::DEBUG) { |
|
| 106 | + \EEH_Debug_Tools::printr($cache_id, 'REFRESH CACHE', __FILE__, __LINE__); |
|
| 107 | + } |
|
| 108 | + $this->cache_storage->add($cache_id, $content, $expiration); |
|
| 109 | + } |
|
| 110 | + } else { |
|
| 111 | + if (BasicCacheManager::DEBUG) { |
|
| 112 | + \EEH_Debug_Tools::printr($cache_id, 'CACHED CONTENT', __FILE__, __LINE__); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + return $content; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
| 122 | + * or a specific ID targeting a single cache item |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function clear($cache_id) |
|
| 126 | + { |
|
| 127 | + // ensure incoming arg is in an array |
|
| 128 | + $cache_id = is_array($cache_id) ? $cache_id : array($cache_id); |
|
| 129 | + // delete corresponding transients for the supplied id prefix |
|
| 130 | + $this->cache_storage->deleteMany($cache_id); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | |
| 134 | 134 | |
@@ -16,92 +16,92 @@ |
||
| 16 | 16 | class PostRelatedCacheManager extends BasicCacheManager |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @type string |
|
| 21 | - */ |
|
| 22 | - const POST_CACHE_PREFIX = 'ee_cache_post_'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * wp-option option_name for tracking post related cache |
|
| 26 | - * |
|
| 27 | - * @type string |
|
| 28 | - */ |
|
| 29 | - const POST_CACHE_OPTIONS_KEY = 'ee_post_cache'; |
|
| 30 | - |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * PostRelatedCacheManager constructor. |
|
| 35 | - * |
|
| 36 | - * @param CacheStorageInterface $cache_storage |
|
| 37 | - */ |
|
| 38 | - public function __construct(CacheStorageInterface $cache_storage) |
|
| 39 | - { |
|
| 40 | - parent::__construct($cache_storage); |
|
| 41 | - add_action('save_post', array($this, 'clearPostRelatedCache')); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * returns a string that will be prepended to all cache identifiers |
|
| 48 | - * |
|
| 49 | - * @return string |
|
| 50 | - */ |
|
| 51 | - public function cachePrefix() |
|
| 52 | - { |
|
| 53 | - return PostRelatedCacheManager::POST_CACHE_PREFIX; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * If you are caching content that pertains to a Post of any type, |
|
| 60 | - * then it is recommended to pass the post id and cache id prefix to this method |
|
| 61 | - * so that it can be added to the post related cache tracking. |
|
| 62 | - * Then, whenever that post is updated, the cache will automatically be deleted, |
|
| 63 | - * which helps to ensure that outdated cache content will not be served |
|
| 64 | - * |
|
| 65 | - * @param int $post_ID [required] |
|
| 66 | - * @param string $id_prefix [required] Appended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 67 | - * May also be helpful to include an additional specific identifier, |
|
| 68 | - * such as a post ID as part of the $id_prefix so that individual caches |
|
| 69 | - * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 70 | - * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 71 | - */ |
|
| 72 | - public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
|
| 73 | - { |
|
| 74 | - $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 75 | - // if post is not already being tracked |
|
| 76 | - if ( ! isset($post_related_cache[$post_ID])) { |
|
| 77 | - // add array to add cache ids to |
|
| 78 | - $post_related_cache[$post_ID] = array(); |
|
| 79 | - } |
|
| 80 | - // add cache id to be tracked |
|
| 81 | - $post_related_cache[$post_ID][] = $id_prefix; |
|
| 82 | - update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * callback hooked into the WordPress "save_post" action |
|
| 89 | - * deletes any cache content associated with the post |
|
| 90 | - * |
|
| 91 | - * @param int $post_ID [required] |
|
| 92 | - */ |
|
| 93 | - public function clearPostRelatedCache($post_ID) |
|
| 94 | - { |
|
| 95 | - $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 96 | - // if post is not being tracked |
|
| 97 | - if ( ! isset($post_related_cache[$post_ID])) { |
|
| 98 | - return; |
|
| 99 | - } |
|
| 100 | - // get cache id prefixes for post, and delete their corresponding transients |
|
| 101 | - $this->clear($post_related_cache[$post_ID]); |
|
| 102 | - unset($post_related_cache[$post_ID]); |
|
| 103 | - update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
| 104 | - } |
|
| 19 | + /** |
|
| 20 | + * @type string |
|
| 21 | + */ |
|
| 22 | + const POST_CACHE_PREFIX = 'ee_cache_post_'; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * wp-option option_name for tracking post related cache |
|
| 26 | + * |
|
| 27 | + * @type string |
|
| 28 | + */ |
|
| 29 | + const POST_CACHE_OPTIONS_KEY = 'ee_post_cache'; |
|
| 30 | + |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * PostRelatedCacheManager constructor. |
|
| 35 | + * |
|
| 36 | + * @param CacheStorageInterface $cache_storage |
|
| 37 | + */ |
|
| 38 | + public function __construct(CacheStorageInterface $cache_storage) |
|
| 39 | + { |
|
| 40 | + parent::__construct($cache_storage); |
|
| 41 | + add_action('save_post', array($this, 'clearPostRelatedCache')); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * returns a string that will be prepended to all cache identifiers |
|
| 48 | + * |
|
| 49 | + * @return string |
|
| 50 | + */ |
|
| 51 | + public function cachePrefix() |
|
| 52 | + { |
|
| 53 | + return PostRelatedCacheManager::POST_CACHE_PREFIX; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * If you are caching content that pertains to a Post of any type, |
|
| 60 | + * then it is recommended to pass the post id and cache id prefix to this method |
|
| 61 | + * so that it can be added to the post related cache tracking. |
|
| 62 | + * Then, whenever that post is updated, the cache will automatically be deleted, |
|
| 63 | + * which helps to ensure that outdated cache content will not be served |
|
| 64 | + * |
|
| 65 | + * @param int $post_ID [required] |
|
| 66 | + * @param string $id_prefix [required] Appended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 67 | + * May also be helpful to include an additional specific identifier, |
|
| 68 | + * such as a post ID as part of the $id_prefix so that individual caches |
|
| 69 | + * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 70 | + * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 71 | + */ |
|
| 72 | + public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
|
| 73 | + { |
|
| 74 | + $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 75 | + // if post is not already being tracked |
|
| 76 | + if ( ! isset($post_related_cache[$post_ID])) { |
|
| 77 | + // add array to add cache ids to |
|
| 78 | + $post_related_cache[$post_ID] = array(); |
|
| 79 | + } |
|
| 80 | + // add cache id to be tracked |
|
| 81 | + $post_related_cache[$post_ID][] = $id_prefix; |
|
| 82 | + update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * callback hooked into the WordPress "save_post" action |
|
| 89 | + * deletes any cache content associated with the post |
|
| 90 | + * |
|
| 91 | + * @param int $post_ID [required] |
|
| 92 | + */ |
|
| 93 | + public function clearPostRelatedCache($post_ID) |
|
| 94 | + { |
|
| 95 | + $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 96 | + // if post is not being tracked |
|
| 97 | + if ( ! isset($post_related_cache[$post_ID])) { |
|
| 98 | + return; |
|
| 99 | + } |
|
| 100 | + // get cache id prefixes for post, and delete their corresponding transients |
|
| 101 | + $this->clear($post_related_cache[$post_ID]); |
|
| 102 | + unset($post_related_cache[$post_ID]); |
|
| 103 | + update_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, $post_related_cache); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | 106 | |
| 107 | 107 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | public function clearPostRelatedCacheOnUpdate($post_ID, $id_prefix) |
| 73 | 73 | { |
| 74 | - $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 74 | + $post_related_cache = (array) get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 75 | 75 | // if post is not already being tracked |
| 76 | 76 | if ( ! isset($post_related_cache[$post_ID])) { |
| 77 | 77 | // add array to add cache ids to |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | */ |
| 93 | 93 | public function clearPostRelatedCache($post_ID) |
| 94 | 94 | { |
| 95 | - $post_related_cache = (array)get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 95 | + $post_related_cache = (array) get_option(PostRelatedCacheManager::POST_CACHE_OPTIONS_KEY, array()); |
|
| 96 | 96 | // if post is not being tracked |
| 97 | 97 | if ( ! isset($post_related_cache[$post_ID])) { |
| 98 | 98 | return; |
@@ -18,43 +18,43 @@ |
||
| 18 | 18 | interface CacheManagerInterface |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * returns a string that will be prepended to all cache identifiers |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public function cachePrefix(); |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 32 | - * May also be helpful to include an additional specific identifier, |
|
| 33 | - * such as a post ID as part of the $id_prefix so that individual caches |
|
| 34 | - * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 35 | - * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 36 | - * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
| 37 | - * It is advisable to use some of the actual data |
|
| 38 | - * that is used to generate the content being cached, |
|
| 39 | - * in order to guarantee that the cache id is unique for that content. |
|
| 40 | - * The cache id will be md5'd before usage to make it more db friendly, |
|
| 41 | - * and the entire cache id string will be truncated to 190 characters. |
|
| 42 | - * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
| 43 | - * necessary, |
|
| 44 | - * we wrap our content creation in a Closure so that it is not executed until needed. |
|
| 45 | - * @param int $expiration |
|
| 46 | - * @return Closure|mixed |
|
| 47 | - */ |
|
| 48 | - public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS); |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
| 54 | - * or a specific ID targeting a single cache item |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function clear($cache_id); |
|
| 21 | + /** |
|
| 22 | + * returns a string that will be prepended to all cache identifiers |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public function cachePrefix(); |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param string $id_prefix [required] Prepended to all cache IDs. Can be helpful in finding specific cache types. |
|
| 32 | + * May also be helpful to include an additional specific identifier, |
|
| 33 | + * such as a post ID as part of the $id_prefix so that individual caches |
|
| 34 | + * can be found and/or cleared. ex: "venue-28", or "shortcode-156". |
|
| 35 | + * BasicCacheManager::CACHE_PREFIX will also be prepended to the cache id. |
|
| 36 | + * @param string $cache_id [required] Additional identifying details that make this cache unique. |
|
| 37 | + * It is advisable to use some of the actual data |
|
| 38 | + * that is used to generate the content being cached, |
|
| 39 | + * in order to guarantee that the cache id is unique for that content. |
|
| 40 | + * The cache id will be md5'd before usage to make it more db friendly, |
|
| 41 | + * and the entire cache id string will be truncated to 190 characters. |
|
| 42 | + * @param Closure $callback [required] since the point of caching is to avoid generating content when not |
|
| 43 | + * necessary, |
|
| 44 | + * we wrap our content creation in a Closure so that it is not executed until needed. |
|
| 45 | + * @param int $expiration |
|
| 46 | + * @return Closure|mixed |
|
| 47 | + */ |
|
| 48 | + public function get($id_prefix, $cache_id, Closure $callback, $expiration = HOUR_IN_SECONDS); |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param array|string $cache_id [required] Could be an ID prefix affecting many caches |
|
| 54 | + * or a specific ID targeting a single cache item |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function clear($cache_id); |
|
| 58 | 58 | |
| 59 | 59 | } |
| 60 | 60 | // End of file CacheManagerInterface.php |