Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EE_Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 24 | final class EE_Config { |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * instance of the EE_Config object |
||
| 29 | * @var EE_Config $_instance |
||
| 30 | * @access private |
||
| 31 | */ |
||
| 32 | private static $_instance = NULL; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * An StdClass whose property names are addon slugs, |
||
| 36 | * and values are their config classes |
||
| 37 | * @var StdClass |
||
| 38 | */ |
||
| 39 | public $addons = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * |
||
| 43 | * @var EE_Admin_Config |
||
| 44 | */ |
||
| 45 | public $admin = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * |
||
| 49 | * @var EE_Core_Config |
||
| 50 | */ |
||
| 51 | public $core = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * @var EE_Currency_Config |
||
| 56 | */ |
||
| 57 | public $currency = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * |
||
| 61 | * @var EE_Organization_Config |
||
| 62 | */ |
||
| 63 | public $organization = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * |
||
| 67 | * @var EE_Registration_Config |
||
| 68 | */ |
||
| 69 | public $registration = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * @var EE_Template_Config |
||
| 74 | */ |
||
| 75 | public $template_settings = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Holds EE environment values. |
||
| 79 | * |
||
| 80 | * @var EE_Environment_Config |
||
| 81 | */ |
||
| 82 | public $environment = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * settings pertaining to Google maps |
||
| 86 | * |
||
| 87 | * @var EE_Map_Config |
||
| 88 | */ |
||
| 89 | public $map_settings = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | * @deprecated |
||
| 94 | * @var EE_Gateway_Config |
||
| 95 | */ |
||
| 96 | public $gateway = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array $_config_option_names |
||
| 100 | * @access private |
||
| 101 | */ |
||
| 102 | private $_config_option_names = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array $_module_route_map |
||
| 106 | * @access private |
||
| 107 | */ |
||
| 108 | private static $_module_route_map = array(); |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var array $_module_forward_map |
||
| 112 | * @access private |
||
| 113 | */ |
||
| 114 | private static $_module_forward_map = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array $_module_view_map |
||
| 118 | * @access private |
||
| 119 | */ |
||
| 120 | private static $_module_view_map = array(); |
||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @singleton method used to instantiate class object |
||
| 126 | * @access public |
||
| 127 | * @return EE_Config instance |
||
| 128 | */ |
||
| 129 | public static function instance() { |
||
| 130 | // check if class object is instantiated, and instantiated properly |
||
| 131 | if ( ! self::$_instance instanceof EE_Config ) { |
||
| 132 | self::$_instance = new self(); |
||
| 133 | } |
||
| 134 | return self::$_instance; |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Resets the config |
||
| 141 | * @param bool $hard_reset if TRUE, sets EE_CONFig back to its original settings in the database. If FALSE |
||
| 142 | * (default) leaves the database alone, and merely resets the EE_Config object to reflect its state in the database |
||
| 143 | * @param boolean $reinstantiate if TRUE (default) call instance() and return it. Otherwise, just leave |
||
| 144 | * $_instance as NULL. Useful in case you want to forget about the old instance on EE_Config, but might |
||
| 145 | * not be ready to instantiate EE_Config currently (eg if the site was put into maintenance mode) |
||
| 146 | * @return EE_Config |
||
| 147 | */ |
||
| 148 | public static function reset( $hard_reset = FALSE, $reinstantiate = TRUE ){ |
||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * class constructor |
||
| 172 | * |
||
| 173 | * @access private |
||
| 174 | * @return \EE_Config |
||
|
|
|||
| 175 | */ |
||
| 176 | private function __construct() { |
||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * use to get the current theme if needed from static context |
||
| 204 | * @return string current theme set. |
||
| 205 | */ |
||
| 206 | public static function get_current_theme() { |
||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * _initialize_config |
||
| 215 | * |
||
| 216 | * @access private |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | private function _initialize_config() { |
||
| 220 | //set defaults |
||
| 221 | $this->addons = new stdClass(); |
||
| 222 | // set _module_route_map |
||
| 223 | EE_Config::$_module_route_map = array(); |
||
| 224 | // set _module_forward_map |
||
| 225 | EE_Config::$_module_forward_map = array(); |
||
| 226 | // set _module_view_map |
||
| 227 | EE_Config::$_module_view_map = array(); |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * load core plugin configuration |
||
| 235 | * |
||
| 236 | * @access private |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | private function _load_core_config() { |
||
| 240 | // load_core_config__start hook |
||
| 241 | do_action( 'AHEE__EE_Config___load_core_config__start', $this ); |
||
| 242 | $espresso_config = $this->get_espresso_config(); |
||
| 243 | foreach ( $espresso_config as $config => $settings ) { |
||
| 244 | // load_core_config__start hook |
||
| 245 | $settings = apply_filters( 'FHEE__EE_Config___load_core_config__config_settings', $settings, $config, $this ); |
||
| 246 | if ( is_object( $settings ) && property_exists( $this, $config ) ) { |
||
| 247 | $this->$config = apply_filters( 'FHEE__EE_Config___load_core_config__' . $config, $settings ); |
||
| 248 | //call configs populate method to ensure any defaults are set for empty values. |
||
| 249 | if ( method_exists( $settings, 'populate' ) ) { |
||
| 250 | $this->$config->populate(); |
||
| 251 | } |
||
| 252 | if ( method_exists( $settings, 'do_hooks' ) ) { |
||
| 253 | $this->$config->do_hooks(); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | if ( apply_filters( 'FHEE__EE_Config___load_core_config__update_espresso_config', FALSE ) ) { |
||
| 258 | $this->update_espresso_config(); |
||
| 259 | } |
||
| 260 | // load_core_config__end hook |
||
| 261 | do_action( 'AHEE__EE_Config___load_core_config__end', $this ); |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * _verify_config |
||
| 268 | * |
||
| 269 | * @access protected |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | protected function _verify_config() { |
||
| 273 | |||
| 274 | $this->core = $this->core instanceof EE_Core_Config |
||
| 275 | ? $this->core : new EE_Core_Config(); |
||
| 276 | $this->core = apply_filters( 'FHEE__EE_Config___initialize_config__core', $this->core ); |
||
| 277 | |||
| 278 | $this->organization = $this->organization instanceof EE_Organization_Config |
||
| 279 | ? $this->organization : new EE_Organization_Config(); |
||
| 280 | $this->organization = apply_filters( 'FHEE__EE_Config___initialize_config__organization', $this->organization ); |
||
| 281 | |||
| 282 | $this->currency = $this->currency instanceof EE_Currency_Config |
||
| 283 | ? $this->currency : new EE_Currency_Config(); |
||
| 284 | $this->currency = apply_filters( 'FHEE__EE_Config___initialize_config__currency', $this->currency ); |
||
| 285 | |||
| 286 | $this->registration = $this->registration instanceof EE_Registration_Config |
||
| 287 | ? $this->registration : new EE_Registration_Config(); |
||
| 288 | $this->registration = apply_filters( 'FHEE__EE_Config___initialize_config__registration', $this->registration ); |
||
| 289 | |||
| 290 | $this->admin = $this->admin instanceof EE_Admin_Config |
||
| 291 | ? $this->admin : new EE_Admin_Config(); |
||
| 292 | $this->admin = apply_filters( 'FHEE__EE_Config___initialize_config__admin', $this->admin ); |
||
| 293 | |||
| 294 | $this->template_settings = $this->template_settings instanceof EE_Template_Config |
||
| 295 | ? $this->template_settings : new EE_Template_Config(); |
||
| 296 | $this->template_settings = apply_filters( 'FHEE__EE_Config___initialize_config__template_settings', $this->template_settings ); |
||
| 297 | |||
| 298 | $this->map_settings = $this->map_settings instanceof EE_Map_Config |
||
| 299 | ? $this->map_settings : new EE_Map_Config(); |
||
| 300 | $this->map_settings = apply_filters( 'FHEE__EE_Config___initialize_config__map_settings', $this->map_settings ); |
||
| 301 | |||
| 302 | $this->environment = $this->environment instanceof EE_Environment_Config |
||
| 303 | ? $this->environment : new EE_Environment_Config(); |
||
| 304 | $this->environment = apply_filters( 'FHEE__EE_Config___initialize_config__environment', $this->environment ); |
||
| 305 | |||
| 306 | $this->gateway = $this->gateway instanceof EE_Gateway_Config |
||
| 307 | ? $this->gateway : new EE_Gateway_Config(); |
||
| 308 | $this->gateway = apply_filters( 'FHEE__EE_Config___initialize_config__gateway', $this->gateway ); |
||
| 309 | |||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * get_espresso_config |
||
| 316 | * |
||
| 317 | * @access public |
||
| 318 | * @return array of espresso config stuff |
||
| 319 | */ |
||
| 320 | public function get_espresso_config() { |
||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * double_check_config_comparison |
||
| 329 | * |
||
| 330 | * @access public |
||
| 331 | * @param string $option |
||
| 332 | * @param $old_value |
||
| 333 | * @param $value |
||
| 334 | */ |
||
| 335 | public function double_check_config_comparison( $option = '', $old_value, $value ) { |
||
| 336 | // make sure we're checking the ee config |
||
| 337 | if ( $option == 'ee_config' ) { |
||
| 338 | // run a loose comparison of the old value against the new value for type and properties, |
||
| 339 | // but NOT exact instance like WP update_option does |
||
| 340 | if ( $value != $old_value ) { |
||
| 341 | // if they are NOT the same, then remove the hook, |
||
| 342 | // which means the subsequent update results will be based solely on the update query results |
||
| 343 | // the reason we do this is because, as stated above, |
||
| 344 | // WP update_option performs an exact instance comparison (===) on any update values passed to it |
||
| 345 | // this happens PRIOR to serialization and any subsequent update. |
||
| 346 | // If values are found to match their previous old value, |
||
| 347 | // then WP bails before performing any update. |
||
| 348 | // Since we are passing the EE_Config object, it is comparing the EXACT instance of the saved version |
||
| 349 | // it just pulled from the db, with the one being passed to it (which will not match). |
||
| 350 | // HOWEVER, once the object is serialized and passed off to MySQL to update, |
||
| 351 | // MySQL MAY ALSO NOT perform the update because |
||
| 352 | // the string it sees in the db looks the same as the new one it has been passed!!! |
||
| 353 | // This results in the query returning an "affected rows" value of ZERO, |
||
| 354 | // which gets returned immediately by WP update_option and looks like an error. |
||
| 355 | remove_action( 'update_option', array( $this, 'check_config_updated' )); |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * update_espresso_config |
||
| 364 | * |
||
| 365 | * @access public |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | protected function _reset_espresso_addon_config() { |
||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * update_espresso_config |
||
| 384 | * |
||
| 385 | * @access public |
||
| 386 | * @param bool $add_success |
||
| 387 | * @param bool $add_error |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | public function update_espresso_config( $add_success = FALSE, $add_error = TRUE ) { |
||
| 391 | // commented out the following re: https://events.codebasehq.com/projects/event-espresso/tickets/8197 |
||
| 392 | //$clone = clone( self::$_instance ); |
||
| 393 | //self::$_instance = NULL; |
||
| 394 | do_action( 'AHEE__EE_Config__update_espresso_config__begin',$this ); |
||
| 395 | $this->_reset_espresso_addon_config(); |
||
| 396 | // hook into update_option because that happens AFTER the ( $value === $old_value ) conditional |
||
| 397 | // but BEFORE the actual update occurs |
||
| 398 | add_action( 'update_option', array( $this, 'double_check_config_comparison' ), 1, 3 ); |
||
| 399 | // now update "ee_config" |
||
| 400 | $saved = update_option( 'ee_config', $this ); |
||
| 401 | // if not saved... check if the hook we just added still exists; |
||
| 402 | // if it does, it means one of two things: |
||
| 403 | // that update_option bailed at the ( $value === $old_value ) conditional, |
||
| 404 | // or... |
||
| 405 | // the db update query returned 0 rows affected |
||
| 406 | // (probably because the data value was the same from it's perspective) |
||
| 407 | // so the existence of the hook means that a negative result from update_option is NOT an error, |
||
| 408 | // but just means no update occurred, so don't display an error to the user. |
||
| 409 | // BUT... if update_option returns FALSE, AND the hook is missing, |
||
| 410 | // then it means that something truly went wrong |
||
| 411 | $saved = ! $saved ? has_action( 'update_option', array( $this, 'double_check_config_comparison' )) : $saved; |
||
| 412 | // remove our action since we don't want it in the system anymore |
||
| 413 | remove_action( 'update_option', array( $this, 'double_check_config_comparison' ), 1 ); |
||
| 414 | do_action( 'AHEE__EE_Config__update_espresso_config__end', $this, $saved ); |
||
| 415 | //self::$_instance = $clone; |
||
| 416 | //unset( $clone ); |
||
| 417 | // if config remains the same or was updated successfully |
||
| 418 | if ( $saved ) { |
||
| 419 | if ( $add_success ) { |
||
| 420 | EE_Error::add_success( |
||
| 421 | __( 'The Event Espresso Configuration Settings have been successfully updated.', 'event_espresso' ), |
||
| 422 | __FILE__, __FUNCTION__, __LINE__ |
||
| 423 | ); |
||
| 424 | } |
||
| 425 | return TRUE; |
||
| 426 | } else { |
||
| 427 | if ( $add_error ) { |
||
| 428 | EE_Error::add_error( |
||
| 429 | __( 'The Event Espresso Configuration Settings were not updated.', 'event_espresso' ), |
||
| 430 | __FILE__, __FUNCTION__, __LINE__ |
||
| 431 | ); |
||
| 432 | } |
||
| 433 | return FALSE; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * _verify_config_params |
||
| 440 | * |
||
| 441 | * @access private |
||
| 442 | * @param string $section |
||
| 443 | * @param string $name |
||
| 444 | * @param string $config_class |
||
| 445 | * @param EE_Config_Base $config_obj |
||
| 446 | * @param array $tests_to_run |
||
| 447 | * @param bool $display_errors |
||
| 448 | * @return bool TRUE on success, FALSE on fail |
||
| 449 | */ |
||
| 450 | private function _verify_config_params( |
||
| 451 | $section = '', |
||
| 452 | $name = '', |
||
| 453 | $config_class = '', |
||
| 454 | $config_obj = NULL, |
||
| 455 | $tests_to_run = array( 1, 2, 3, 4, 5, 6, 7, 8 ), |
||
| 456 | $display_errors = TRUE |
||
| 457 | ) { |
||
| 458 | try { |
||
| 459 | foreach ( $tests_to_run as $test ) { |
||
| 460 | switch ( $test ) { |
||
| 461 | |||
| 462 | // TEST #1 : check that section was set |
||
| 463 | View Code Duplication | case 1 : |
|
| 464 | if ( empty( $section ) ) { |
||
| 465 | if ( $display_errors ) { |
||
| 466 | throw new EE_Error( |
||
| 467 | sprintf( |
||
| 468 | __( 'No configuration section has been provided while attempting to save "%s".', 'event_espresso' ), |
||
| 469 | $config_class |
||
| 470 | ) |
||
| 471 | ); |
||
| 472 | } |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | break; |
||
| 476 | |||
| 477 | // TEST #2 : check that settings section exists |
||
| 478 | View Code Duplication | case 2 : |
|
| 479 | if ( ! isset( $this->{$section} ) ) { |
||
| 480 | if ( $display_errors ) { |
||
| 481 | throw new EE_Error( |
||
| 482 | sprintf( __( 'The "%s" configuration section does not exist.', 'event_espresso' ), |
||
| 483 | $section ) |
||
| 484 | ); |
||
| 485 | } |
||
| 486 | return false; |
||
| 487 | } |
||
| 488 | break; |
||
| 489 | |||
| 490 | // TEST #3 : check that section is the proper format |
||
| 491 | case 3 : |
||
| 492 | if ( |
||
| 493 | ! ( $this->{$section} instanceof EE_Config_Base || $this->{$section} instanceof stdClass ) |
||
| 494 | ) { |
||
| 495 | if ( $display_errors ) { |
||
| 496 | throw new EE_Error( |
||
| 497 | sprintf( |
||
| 498 | __( 'The "%s" configuration settings have not been formatted correctly.', 'event_espresso' ), |
||
| 499 | $section |
||
| 500 | ) |
||
| 501 | ); |
||
| 502 | } |
||
| 503 | return false; |
||
| 504 | } |
||
| 505 | break; |
||
| 506 | |||
| 507 | // TEST #4 : check that config section name has been set |
||
| 508 | View Code Duplication | case 4 : |
|
| 509 | if ( empty( $name ) ) { |
||
| 510 | if ( $display_errors ) { |
||
| 511 | throw new EE_Error( |
||
| 512 | __( 'No name has been provided for the specific configuration section.', 'event_espresso' ) |
||
| 513 | ); |
||
| 514 | } |
||
| 515 | return false; |
||
| 516 | } |
||
| 517 | break; |
||
| 518 | |||
| 519 | // TEST #5 : check that a config class name has been set |
||
| 520 | View Code Duplication | case 5 : |
|
| 521 | if ( empty( $config_class ) ) { |
||
| 522 | if ( $display_errors ) { |
||
| 523 | throw new EE_Error( |
||
| 524 | __( 'No class name has been provided for the specific configuration section.', 'event_espresso' ) |
||
| 525 | ); |
||
| 526 | } |
||
| 527 | return false; |
||
| 528 | } |
||
| 529 | break; |
||
| 530 | |||
| 531 | // TEST #6 : verify config class is accessible |
||
| 532 | View Code Duplication | case 6 : |
|
| 533 | if ( ! class_exists( $config_class ) ) { |
||
| 534 | if ( $display_errors ) { |
||
| 535 | throw new EE_Error( |
||
| 536 | sprintf( |
||
| 537 | __( 'The "%s" class does not exist. Please ensure that an autoloader has been set for it.', 'event_espresso' ), |
||
| 538 | $config_class |
||
| 539 | ) |
||
| 540 | ); |
||
| 541 | } |
||
| 542 | return false; |
||
| 543 | } |
||
| 544 | break; |
||
| 545 | |||
| 546 | // TEST #7 : check that config has even been set |
||
| 547 | case 7 : |
||
| 548 | if ( ! isset( $this->{$section}->{$name} ) ) { |
||
| 549 | if ( $display_errors ) { |
||
| 550 | throw new EE_Error( |
||
| 551 | sprintf( |
||
| 552 | __( 'No configuration has been set for "%1$s->%2$s".', 'event_espresso' ), |
||
| 553 | $section, |
||
| 554 | $name |
||
| 555 | ) |
||
| 556 | ); |
||
| 557 | } |
||
| 558 | return false; |
||
| 559 | } else { |
||
| 560 | // and make sure it's not serialized |
||
| 561 | $this->{$section}->{$name} = maybe_unserialize( $this->{$section}->{$name} ); |
||
| 562 | } |
||
| 563 | break; |
||
| 564 | |||
| 565 | // TEST #8 : check that config is the requested type |
||
| 566 | case 8 : |
||
| 567 | if ( ! $this->{$section}->{$name} instanceof $config_class ) { |
||
| 568 | if ( $display_errors ) { |
||
| 569 | throw new EE_Error( |
||
| 570 | sprintf( |
||
| 571 | __( 'The configuration for "%1$s->%2$s" is not of the "%3$s" class.', 'event_espresso' ), |
||
| 572 | $section, |
||
| 573 | $name, |
||
| 574 | $config_class |
||
| 575 | ) |
||
| 576 | ); |
||
| 577 | } |
||
| 578 | return false; |
||
| 579 | } |
||
| 580 | break; |
||
| 581 | |||
| 582 | // TEST #9 : verify config object |
||
| 583 | View Code Duplication | case 9 : |
|
| 584 | if ( ! $config_obj instanceof EE_Config_Base ) { |
||
| 585 | if ( $display_errors ) { |
||
| 586 | throw new EE_Error( |
||
| 587 | sprintf( |
||
| 588 | __( 'The "%s" class is not an instance of EE_Config_Base.', 'event_espresso' ), |
||
| 589 | print_r( $config_obj, true ) |
||
| 590 | ) |
||
| 591 | ); |
||
| 592 | } |
||
| 593 | return false; |
||
| 594 | } |
||
| 595 | break; |
||
| 596 | |||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | } catch( EE_Error $e ) { |
||
| 601 | $e->get_error(); |
||
| 602 | } |
||
| 603 | // you have successfully run the gauntlet |
||
| 604 | return TRUE; |
||
| 605 | } |
||
| 606 | |||
| 607 | |||
| 608 | |||
| 609 | /** |
||
| 610 | * _generate_config_option_name |
||
| 611 | * |
||
| 612 | * @access protected |
||
| 613 | * @param string $section |
||
| 614 | * @param string $name |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | private function _generate_config_option_name( $section = '', $name = '' ) { |
||
| 620 | |||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * _set_config_class |
||
| 625 | * ensures that a config class is set, either from a passed config class or one generated from the config name |
||
| 626 | * |
||
| 627 | * @access private |
||
| 628 | * @param string $config_class |
||
| 629 | * @param string $name |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | private function _set_config_class( $config_class = '', $name = '' ) { |
||
| 633 | return ! empty( $config_class ) |
||
| 634 | ? $config_class |
||
| 635 | : str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $name ) ) ) . '_Config'; |
||
| 636 | } |
||
| 637 | |||
| 638 | |||
| 639 | /** |
||
| 640 | * set_config |
||
| 641 | * |
||
| 642 | * @access protected |
||
| 643 | * @param string $section |
||
| 644 | * @param string $name |
||
| 645 | * @param string $config_class |
||
| 646 | * @param EE_Config_Base $config_obj |
||
| 647 | * @return EE_Config_Base |
||
| 648 | */ |
||
| 649 | public function set_config( $section = '', $name = '', $config_class = '', EE_Config_Base $config_obj = null ) { |
||
| 650 | // ensure config class is set to something |
||
| 651 | $config_class = $this->_set_config_class( $config_class, $name ); |
||
| 652 | // run tests 1-4, 6, and 7 to verify all config params are set and valid |
||
| 653 | View Code Duplication | if ( ! $this->_verify_config_params( $section, $name, $config_class, null, array( 1, 2, 3, 4, 5, 6 ))) { |
|
| 654 | return null; |
||
| 655 | } |
||
| 656 | $config_option_name = $this->_generate_config_option_name( $section, $name ); |
||
| 657 | // if the config option name hasn't been added yet to the list of option names we're tracking, then do so now |
||
| 658 | if ( ! isset( $this->_config_option_names[ $config_option_name ] )) { |
||
| 659 | $this->_config_option_names[ $config_option_name ] = $config_class; |
||
| 660 | } |
||
| 661 | // verify the incoming config object but suppress errors |
||
| 662 | if ( ! $this->_verify_config_params( $section, $name, $config_class, $config_obj, array( 9 ), false )) { |
||
| 663 | $config_obj = new $config_class(); |
||
| 664 | } |
||
| 665 | if ( get_option( $config_option_name ) ) { |
||
| 666 | update_option( $config_option_name, $config_obj ); |
||
| 667 | $this->{$section}->{$name} = $config_obj; |
||
| 668 | return $this->{$section}->{$name}; |
||
| 669 | } else { |
||
| 670 | // create a wp-option for this config |
||
| 671 | if ( add_option( $config_option_name, $config_obj, '', 'no' )) { |
||
| 672 | $this->{$section}->{$name} = maybe_unserialize( $config_obj ); |
||
| 673 | return $this->{$section}->{$name}; |
||
| 674 | } else { |
||
| 675 | EE_Error::add_error( |
||
| 676 | sprintf( __( 'The "%s" could not be saved to the database.', 'event_espresso' ), $config_class ), |
||
| 677 | __FILE__, __FUNCTION__, __LINE__ |
||
| 678 | ); |
||
| 679 | return null; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | } |
||
| 683 | |||
| 684 | |||
| 685 | |||
| 686 | /** |
||
| 687 | * update_config |
||
| 688 | * Important: the config object must ALREADY be set, otherwise this will produce an error. |
||
| 689 | * |
||
| 690 | * @access public |
||
| 691 | * @param string $section |
||
| 692 | * @param string $name |
||
| 693 | * @param EE_Config_Base|string $config_obj |
||
| 694 | * @param bool $throw_errors |
||
| 695 | * @return bool |
||
| 696 | */ |
||
| 697 | public function update_config( $section = '', $name = '', $config_obj = '', $throw_errors = true ) { |
||
| 698 | $config_obj = maybe_unserialize( $config_obj ); |
||
| 699 | // get class name of the incoming object |
||
| 700 | $config_class = get_class( $config_obj ); |
||
| 701 | // run tests 1-5 and 9 to verify config |
||
| 702 | View Code Duplication | if ( ! $this->_verify_config_params( $section, $name, $config_class, $config_obj, array( 1, 2, 3, 4, 7, 9 ))) { |
|
| 703 | return false; |
||
| 704 | } |
||
| 705 | $config_option_name = $this->_generate_config_option_name( $section, $name ); |
||
| 706 | // check if config object has been added to db by seeing if config option name is in $this->_config_option_names array |
||
| 707 | if ( ! isset( $this->_config_option_names[ $config_option_name ] )) { |
||
| 708 | // save new config to db |
||
| 709 | return $this->set_config( $section, $name, $config_class, $config_obj ); |
||
| 710 | } else { |
||
| 711 | // first check if the record already exists |
||
| 712 | $existing_config = get_option( $config_option_name ); |
||
| 713 | $config_obj = serialize( $config_obj ); |
||
| 714 | // just return if db record is already up to date |
||
| 715 | if ( $existing_config == $config_obj ) { |
||
| 716 | $this->{$section}->{$name} = $config_obj; |
||
| 717 | return true; |
||
| 718 | } else if ( update_option( $config_option_name, $config_obj )) { |
||
| 719 | // update wp-option for this config class |
||
| 720 | $this->{$section}->{$name} = $config_obj; |
||
| 721 | return true; |
||
| 722 | } elseif ( $throw_errors ) { |
||
| 723 | EE_Error::add_error( |
||
| 724 | sprintf( |
||
| 725 | __( 'The "%1$s" object stored at"%2$s" was not successfully updated in the database.', 'event_espresso' ), |
||
| 726 | $config_class, |
||
| 727 | 'EE_Config->' . $section . '->' . $name |
||
| 728 | ), |
||
| 729 | __FILE__, __FUNCTION__, __LINE__ |
||
| 730 | ); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | return false; |
||
| 734 | } |
||
| 735 | |||
| 736 | |||
| 737 | |||
| 738 | /** |
||
| 739 | * get_config |
||
| 740 | * |
||
| 741 | * @access public |
||
| 742 | * @param string $section |
||
| 743 | * @param string $name |
||
| 744 | * @param string $config_class |
||
| 745 | * @return mixed EE_Config_Base | NULL |
||
| 746 | */ |
||
| 747 | public function get_config( $section = '', $name = '', $config_class = '' ) { |
||
| 748 | // ensure config class is set to something |
||
| 749 | $config_class = $this->_set_config_class( $config_class, $name ); |
||
| 750 | // run tests 1-4, 6 and 7 to verify that all params have been set |
||
| 751 | View Code Duplication | if ( ! $this->_verify_config_params( $section, $name, $config_class, NULL, array( 1, 2, 3, 4, 5, 6 ))) { |
|
| 752 | return NULL; |
||
| 753 | } |
||
| 754 | // now test if the requested config object exists, but suppress errors |
||
| 755 | if ( $this->_verify_config_params( $section, $name, $config_class, NULL, array( 7, 8 ), FALSE )) { |
||
| 756 | // config already exists, so pass it back |
||
| 757 | return $this->{$section}->{$name}; |
||
| 758 | } |
||
| 759 | // load config option from db if it exists |
||
| 760 | $config_obj = $this->get_config_option( $this->_generate_config_option_name( $section, $name )); |
||
| 761 | // verify the newly retrieved config object, but suppress errors |
||
| 762 | if ( $this->_verify_config_params( $section, $name, $config_class, $config_obj, array( 9 ), FALSE )) { |
||
| 763 | // config is good, so set it and pass it back |
||
| 764 | $this->{$section}->{$name} = $config_obj; |
||
| 765 | return $this->{$section}->{$name}; |
||
| 766 | } |
||
| 767 | // oops! $config_obj is not already set and does not exist in the db, so create a new one |
||
| 768 | $config_obj =$this->set_config( $section, $name, $config_class ); |
||
| 769 | // verify the newly created config object |
||
| 770 | if ( $this->_verify_config_params( $section, $name, $config_class, $config_obj, array( 9 ))) { |
||
| 771 | return $this->{$section}->{$name}; |
||
| 772 | } else { |
||
| 773 | EE_Error::add_error( |
||
| 774 | sprintf( __( 'The "%s" could not be retrieved from the database.', 'event_espresso' ), $config_class ), |
||
| 775 | __FILE__, __FUNCTION__, __LINE__ |
||
| 776 | ); |
||
| 777 | } |
||
| 778 | return NULL; |
||
| 779 | } |
||
| 780 | |||
| 781 | |||
| 782 | /** |
||
| 783 | * get_config_option |
||
| 784 | * |
||
| 785 | * @access public |
||
| 786 | * @param string $config_option_name |
||
| 787 | * @return mixed EE_Config_Base | FALSE |
||
| 788 | */ |
||
| 789 | public function get_config_option( $config_option_name = '' ) { |
||
| 793 | |||
| 794 | |||
| 795 | |||
| 796 | |||
| 797 | /** |
||
| 798 | * update_post_shortcodes |
||
| 799 | * |
||
| 800 | * @access public |
||
| 801 | * @param $page_for_posts |
||
| 802 | * @return void |
||
| 803 | */ |
||
| 804 | public function update_post_shortcodes( $page_for_posts = '' ) { |
||
| 805 | // make sure page_for_posts is set |
||
| 806 | $page_for_posts = ! empty( $page_for_posts ) ? $page_for_posts : EE_Config::get_page_for_posts(); |
||
| 807 | // critical page shortcodes that we do NOT want added to the Posts page (blog) |
||
| 808 | $critical_shortcodes = $this->core->get_critical_pages_shortcodes_array(); |
||
| 809 | // allow others to mess stuff up :D |
||
| 810 | do_action( 'AHEE__EE_Config__update_post_shortcodes', $this->core->post_shortcodes, $page_for_posts ); |
||
| 811 | // verify that post_shortcodes is set |
||
| 812 | $this->core->post_shortcodes = isset( $this->core->post_shortcodes ) && is_array( $this->core->post_shortcodes ) ? $this->core->post_shortcodes : array(); |
||
| 813 | // cycle thru post_shortcodes |
||
| 814 | foreach( $this->core->post_shortcodes as $post_name => $shortcodes ){ |
||
| 815 | // are there any shortcodes to track ? |
||
| 816 | if ( ! empty( $shortcodes )) { |
||
| 817 | // loop thru list of tracked shortcodes |
||
| 818 | foreach( $shortcodes as $shortcode => $post_id ) { |
||
| 819 | // if shortcode is for a critical page, BUT this is NOT the corresponding critical page for that shortcode |
||
| 820 | if ( isset( $critical_shortcodes[ $post_id ] ) && $post_name == $page_for_posts ) { |
||
| 821 | // then remove this shortcode, because we don't want critical page shortcodes like ESPRESSO_TXN_PAGE running on the "Posts Page" (blog) |
||
| 822 | unset( $this->core->post_shortcodes[ $post_name ][ $shortcode ] ); |
||
| 823 | } |
||
| 824 | // skip the posts page, because we want all shortcodes registered for it |
||
| 825 | if ( $post_name == $page_for_posts ) { |
||
| 826 | continue; |
||
| 827 | } |
||
| 828 | // make sure post still exists |
||
| 829 | $post = get_post( $post_id ); |
||
| 830 | if ( $post ) { |
||
| 831 | // check that the post name matches what we have saved |
||
| 832 | if ( $post->post_name == $post_name ) { |
||
| 833 | // if so, then break before hitting the unset below |
||
| 834 | continue; |
||
| 835 | } |
||
| 836 | } |
||
| 837 | // we don't like missing posts around here >:( |
||
| 838 | unset( $this->core->post_shortcodes[ $post_name ] ); |
||
| 839 | } |
||
| 840 | } else { |
||
| 841 | // you got no shortcodes to keep track of ! |
||
| 842 | unset( $this->core->post_shortcodes[ $post_name ] ); |
||
| 843 | } |
||
| 844 | } |
||
| 845 | //only show errors |
||
| 846 | $this->update_espresso_config(); |
||
| 847 | } |
||
| 848 | |||
| 849 | |||
| 850 | |||
| 851 | /** |
||
| 852 | * get_page_for_posts |
||
| 853 | * |
||
| 854 | * if the wp-option "show_on_front" is set to "page", then this is the post_name for the post set in the wp-option "page_for_posts", or "posts" if no page is selected |
||
| 855 | * |
||
| 856 | * |
||
| 857 | * @access public |
||
| 858 | * @return string |
||
| 859 | */ |
||
| 860 | View Code Duplication | public static function get_page_for_posts() { |
|
| 861 | $page_for_posts = get_option( 'page_for_posts' ); |
||
| 862 | if ( ! $page_for_posts ) { |
||
| 863 | return 'posts'; |
||
| 864 | } |
||
| 865 | /** @type WPDB $wpdb */ |
||
| 866 | global $wpdb; |
||
| 867 | $SQL = "SELECT post_name from $wpdb->posts WHERE post_type='posts' OR post_type='page' AND post_status='publish' AND ID=%d"; |
||
| 868 | return $wpdb->get_var( $wpdb->prepare( $SQL, $page_for_posts )); |
||
| 869 | } |
||
| 870 | |||
| 871 | |||
| 872 | |||
| 873 | /** |
||
| 874 | * register_shortcodes_and_modules. |
||
| 875 | * |
||
| 876 | * At this point, it's too early to tell if we're maintenance mode or not. |
||
| 877 | * In fact, this is where we give modules a chance to let core know they exist |
||
| 878 | * so they can help trigger maintenance mode if it's needed |
||
| 879 | * |
||
| 880 | * @access public |
||
| 881 | * @return void |
||
| 882 | */ |
||
| 883 | public function register_shortcodes_and_modules() { |
||
| 884 | if ( EE_Maintenance_Mode::disable_frontend_for_maintenance() ) { |
||
| 885 | return; |
||
| 886 | } |
||
| 887 | // allow shortcodes to register with WP and to set hooks for the rest of the system |
||
| 888 | EE_Registry::instance()->shortcodes =$this->_register_shortcodes(); |
||
| 889 | // allow modules to set hooks for the rest of the system |
||
| 890 | EE_Registry::instance()->modules = $this->_register_modules(); |
||
| 891 | } |
||
| 892 | |||
| 893 | |||
| 894 | /** |
||
| 895 | * initialize_shortcodes_and_modules |
||
| 896 | * meaning they can start adding their hooks to get stuff done |
||
| 897 | * |
||
| 898 | * @access public |
||
| 899 | * @return void |
||
| 900 | */ |
||
| 901 | public function initialize_shortcodes_and_modules() { |
||
| 902 | if ( EE_Maintenance_Mode::disable_frontend_for_maintenance() ) { |
||
| 903 | return; |
||
| 904 | } |
||
| 905 | // allow shortcodes to set hooks for the rest of the system |
||
| 906 | $this->_initialize_shortcodes(); |
||
| 907 | // allow modules to set hooks for the rest of the system |
||
| 908 | $this->_initialize_modules(); |
||
| 909 | } |
||
| 910 | |||
| 911 | |||
| 912 | |||
| 913 | |||
| 914 | /** |
||
| 915 | * widgets_init |
||
| 916 | * |
||
| 917 | * @access private |
||
| 918 | * @return void |
||
| 919 | */ |
||
| 920 | public function widgets_init() { |
||
| 921 | if ( EE_Maintenance_Mode::disable_frontend_for_maintenance() ) { |
||
| 922 | return; |
||
| 923 | } |
||
| 924 | //only init widgets on admin pages when not in complete maintenance, and |
||
| 925 | //on frontend when not in any maintenance mode |
||
| 926 | if (( is_admin() && EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance) || ! EE_Maintenance_Mode::instance()->level() ) { |
||
| 927 | // grab list of installed widgets |
||
| 928 | $widgets_to_register = glob( EE_WIDGETS . '*', GLOB_ONLYDIR ); |
||
| 929 | // filter list of modules to register |
||
| 930 | $widgets_to_register = apply_filters( 'FHEE__EE_Config__register_widgets__widgets_to_register', $widgets_to_register ); |
||
| 931 | |||
| 932 | if ( ! empty( $widgets_to_register ) ) { |
||
| 933 | // cycle thru widget folders |
||
| 934 | foreach ( $widgets_to_register as $widget_path ) { |
||
| 935 | // add to list of installed widget modules |
||
| 936 | EE_Config::register_ee_widget( $widget_path ); |
||
| 937 | } |
||
| 938 | } |
||
| 939 | // filter list of installed modules |
||
| 940 | EE_Registry::instance()->widgets = apply_filters( 'FHEE__EE_Config__register_widgets__installed_widgets', EE_Registry::instance()->widgets ); |
||
| 941 | } |
||
| 942 | } |
||
| 943 | |||
| 944 | |||
| 945 | |||
| 946 | /** |
||
| 947 | * register_ee_widget - makes core aware of this widget |
||
| 948 | * |
||
| 949 | * @access public |
||
| 950 | * @param string $widget_path - full path up to and including widget folder |
||
| 951 | * @return void |
||
| 952 | */ |
||
| 953 | public static function register_ee_widget( $widget_path = NULL ) { |
||
| 954 | do_action( 'AHEE__EE_Config__register_widget__begin', $widget_path ); |
||
| 955 | $widget_ext = '.widget.php'; |
||
| 956 | // make all separators match |
||
| 957 | $widget_path = rtrim( str_replace( '/\\', DS, $widget_path ), DS ); |
||
| 958 | // does the file path INCLUDE the actual file name as part of the path ? |
||
| 959 | if ( strpos( $widget_path, $widget_ext ) !== FALSE ) { |
||
| 960 | // grab and shortcode file name from directory name and break apart at dots |
||
| 961 | $file_name = explode( '.', basename( $widget_path )); |
||
| 962 | // take first segment from file name pieces and remove class prefix if it exists |
||
| 963 | $widget = strpos( $file_name[0], 'EEW_' ) === 0 ? substr( $file_name[0], 4 ) : $file_name[0]; |
||
| 964 | // sanitize shortcode directory name |
||
| 965 | $widget = sanitize_key( $widget ); |
||
| 966 | // now we need to rebuild the shortcode path |
||
| 967 | $widget_path = explode( DS, $widget_path ); |
||
| 968 | // remove last segment |
||
| 969 | array_pop( $widget_path ); |
||
| 970 | // glue it back together |
||
| 971 | $widget_path = implode( DS, $widget_path ); |
||
| 972 | } else { |
||
| 973 | // grab and sanitize widget directory name |
||
| 974 | $widget = sanitize_key( basename( $widget_path )); |
||
| 975 | } |
||
| 976 | // create classname from widget directory name |
||
| 977 | $widget = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $widget ))); |
||
| 978 | // add class prefix |
||
| 979 | $widget_class = 'EEW_' . $widget; |
||
| 980 | // does the widget exist ? |
||
| 981 | View Code Duplication | if ( ! is_readable( $widget_path . DS . $widget_class . $widget_ext )) { |
|
| 982 | $msg = sprintf( |
||
| 983 | __( 'The requested %s widget file could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso' ), |
||
| 984 | $widget_class, |
||
| 985 | $widget_path . DS . $widget_class . $widget_ext |
||
| 986 | ); |
||
| 987 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 988 | return; |
||
| 989 | } |
||
| 990 | // load the widget class file |
||
| 991 | require_once( $widget_path . DS . $widget_class . $widget_ext ); |
||
| 992 | // verify that class exists |
||
| 993 | if ( ! class_exists( $widget_class )) { |
||
| 994 | $msg = sprintf( __( 'The requested %s widget class does not exist.', 'event_espresso' ), $widget_class ); |
||
| 995 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 996 | return; |
||
| 997 | } |
||
| 998 | register_widget( $widget_class ); |
||
| 999 | // add to array of registered widgets |
||
| 1000 | EE_Registry::instance()->widgets->$widget_class = $widget_path . DS . $widget_class . $widget_ext; |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | |||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * _register_shortcodes |
||
| 1007 | * |
||
| 1008 | * @access private |
||
| 1009 | * @return array |
||
| 1010 | */ |
||
| 1011 | private function _register_shortcodes() { |
||
| 1012 | // grab list of installed shortcodes |
||
| 1013 | $shortcodes_to_register = glob( EE_SHORTCODES . '*', GLOB_ONLYDIR ); |
||
| 1014 | // filter list of modules to register |
||
| 1015 | $shortcodes_to_register = apply_filters( 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', $shortcodes_to_register ); |
||
| 1016 | if ( ! empty( $shortcodes_to_register ) ) { |
||
| 1017 | // cycle thru shortcode folders |
||
| 1018 | foreach ( $shortcodes_to_register as $shortcode_path ) { |
||
| 1019 | // add to list of installed shortcode modules |
||
| 1020 | EE_Config::register_shortcode( $shortcode_path ); |
||
| 1021 | } |
||
| 1022 | } |
||
| 1023 | // filter list of installed modules |
||
| 1024 | return apply_filters( 'FHEE__EE_Config___register_shortcodes__installed_shortcodes', EE_Registry::instance()->shortcodes ); |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | |||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * register_shortcode - makes core aware of this shortcode |
||
| 1031 | * |
||
| 1032 | * @access public |
||
| 1033 | * @param string $shortcode_path - full path up to and including shortcode folder |
||
| 1034 | * @return bool |
||
| 1035 | */ |
||
| 1036 | public static function register_shortcode( $shortcode_path = NULL ) { |
||
| 1037 | do_action( 'AHEE__EE_Config__register_shortcode__begin',$shortcode_path ); |
||
| 1038 | $shortcode_ext = '.shortcode.php'; |
||
| 1039 | // make all separators match |
||
| 1040 | $shortcode_path = str_replace( array( '\\', '/' ), DS, $shortcode_path ); |
||
| 1041 | // does the file path INCLUDE the actual file name as part of the path ? |
||
| 1042 | if ( strpos( $shortcode_path, $shortcode_ext ) !== FALSE ) { |
||
| 1043 | // grab shortcode file name from directory name and break apart at dots |
||
| 1044 | $shortcode_file = explode( '.', basename( $shortcode_path )); |
||
| 1045 | // take first segment from file name pieces and remove class prefix if it exists |
||
| 1046 | $shortcode = strpos( $shortcode_file[0], 'EES_' ) === 0 ? substr( $shortcode_file[0], 4 ) : $shortcode_file[0]; |
||
| 1047 | // sanitize shortcode directory name |
||
| 1048 | $shortcode = sanitize_key( $shortcode ); |
||
| 1049 | // now we need to rebuild the shortcode path |
||
| 1050 | $shortcode_path = explode( DS, $shortcode_path ); |
||
| 1051 | // remove last segment |
||
| 1052 | array_pop( $shortcode_path ); |
||
| 1053 | // glue it back together |
||
| 1054 | $shortcode_path = implode( DS, $shortcode_path ) . DS; |
||
| 1055 | } else { |
||
| 1056 | // we need to generate the filename based off of the folder name |
||
| 1057 | // grab and sanitize shortcode directory name |
||
| 1058 | $shortcode = sanitize_key( basename( $shortcode_path )); |
||
| 1059 | $shortcode_path = rtrim( $shortcode_path, DS ) . DS; |
||
| 1060 | } |
||
| 1061 | // create classname from shortcode directory or file name |
||
| 1062 | $shortcode = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $shortcode ))); |
||
| 1063 | // add class prefix |
||
| 1064 | $shortcode_class = 'EES_' . $shortcode; |
||
| 1065 | // does the shortcode exist ? |
||
| 1066 | View Code Duplication | if ( ! is_readable( $shortcode_path . DS . $shortcode_class . $shortcode_ext )) { |
|
| 1067 | $msg = sprintf( |
||
| 1068 | __( 'The requested %s shortcode file could not be found or is not readable due to file permissions. It should be in %s', 'event_espresso' ), |
||
| 1069 | $shortcode_class, |
||
| 1070 | $shortcode_path . DS . $shortcode_class . $shortcode_ext |
||
| 1071 | ); |
||
| 1072 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1073 | return FALSE; |
||
| 1074 | } |
||
| 1075 | // load the shortcode class file |
||
| 1076 | require_once( $shortcode_path . $shortcode_class . $shortcode_ext ); |
||
| 1077 | // verify that class exists |
||
| 1078 | if ( ! class_exists( $shortcode_class )) { |
||
| 1079 | $msg = sprintf( __( 'The requested %s shortcode class does not exist.', 'event_espresso' ), $shortcode_class ); |
||
| 1080 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1081 | return FALSE; |
||
| 1082 | } |
||
| 1083 | $shortcode = strtoupper( $shortcode ); |
||
| 1084 | // add to array of registered shortcodes |
||
| 1085 | EE_Registry::instance()->shortcodes->$shortcode = $shortcode_path . $shortcode_class . $shortcode_ext; |
||
| 1086 | return TRUE; |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | |||
| 1090 | |||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * _register_modules |
||
| 1094 | * |
||
| 1095 | * @access private |
||
| 1096 | * @return array |
||
| 1097 | */ |
||
| 1098 | private function _register_modules() { |
||
| 1099 | // grab list of installed modules |
||
| 1100 | $modules_to_register = glob( EE_MODULES . '*', GLOB_ONLYDIR ); |
||
| 1101 | // filter list of modules to register |
||
| 1102 | $modules_to_register = apply_filters( 'FHEE__EE_Config__register_modules__modules_to_register', $modules_to_register ); |
||
| 1103 | |||
| 1104 | |||
| 1105 | if ( ! empty( $modules_to_register ) ) { |
||
| 1106 | // loop through folders |
||
| 1107 | foreach ( $modules_to_register as $module_path ) { |
||
| 1108 | /**TEMPORARILY EXCLUDE gateways from modules for time being**/ |
||
| 1109 | if ( $module_path != EE_MODULES . 'zzz-copy-this-module-template' && $module_path != EE_MODULES . 'gateways' ) { |
||
| 1110 | // add to list of installed modules |
||
| 1111 | EE_Config::register_module( $module_path ); |
||
| 1112 | } |
||
| 1113 | } |
||
| 1114 | } |
||
| 1115 | // filter list of installed modules |
||
| 1116 | return apply_filters( 'FHEE__EE_Config___register_modules__installed_modules', EE_Registry::instance()->modules ); |
||
| 1117 | } |
||
| 1118 | |||
| 1119 | |||
| 1120 | |||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * register_module - makes core aware of this module |
||
| 1124 | * |
||
| 1125 | * @access public |
||
| 1126 | * @param string $module_path - full path up to and including module folder |
||
| 1127 | * @return bool |
||
| 1128 | */ |
||
| 1129 | public static function register_module( $module_path = NULL ) { |
||
| 1130 | do_action( 'AHEE__EE_Config__register_module__begin', $module_path ); |
||
| 1131 | $module_ext = '.module.php'; |
||
| 1132 | // make all separators match |
||
| 1133 | $module_path = str_replace( array( '\\', '/' ), DS, $module_path ); |
||
| 1134 | // does the file path INCLUDE the actual file name as part of the path ? |
||
| 1135 | if ( strpos( $module_path, $module_ext ) !== FALSE ) { |
||
| 1136 | // grab and shortcode file name from directory name and break apart at dots |
||
| 1137 | $module_file = explode( '.', basename( $module_path )); |
||
| 1138 | // now we need to rebuild the shortcode path |
||
| 1139 | $module_path = explode( DS, $module_path ); |
||
| 1140 | // remove last segment |
||
| 1141 | array_pop( $module_path ); |
||
| 1142 | // glue it back together |
||
| 1143 | $module_path = implode( DS, $module_path ) . DS; |
||
| 1144 | // take first segment from file name pieces and sanitize it |
||
| 1145 | $module = preg_replace( '/[^a-zA-Z0-9_\-]/', '', $module_file[0] ); |
||
| 1146 | // ensure class prefix is added |
||
| 1147 | $module_class = strpos( $module, 'EED_' ) !== 0 ? 'EED_' . $module : $module; |
||
| 1148 | } else { |
||
| 1149 | // we need to generate the filename based off of the folder name |
||
| 1150 | // grab and sanitize module name |
||
| 1151 | $module = strtolower( basename( $module_path )); |
||
| 1152 | $module = preg_replace( '/[^a-z0-9_\-]/', '', $module); |
||
| 1153 | // like trailingslashit() |
||
| 1154 | $module_path = rtrim( $module_path, DS ) . DS; |
||
| 1155 | // create classname from module directory name |
||
| 1156 | $module = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $module ))); |
||
| 1157 | // add class prefix |
||
| 1158 | $module_class = 'EED_' . $module; |
||
| 1159 | } |
||
| 1160 | // does the module exist ? |
||
| 1161 | View Code Duplication | if ( ! is_readable( $module_path . DS . $module_class . $module_ext )) { |
|
| 1162 | $msg = sprintf( __( 'The requested %s module file could not be found or is not readable due to file permissions.', 'event_espresso' ), $module ); |
||
| 1163 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1164 | return FALSE; |
||
| 1165 | } |
||
| 1166 | // load the module class file |
||
| 1167 | require_once( $module_path . $module_class . $module_ext ); |
||
| 1168 | // verify that class exists |
||
| 1169 | if ( ! class_exists( $module_class )) { |
||
| 1170 | $msg = sprintf( __( 'The requested %s module class does not exist.', 'event_espresso' ), $module_class ); |
||
| 1171 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1172 | return FALSE; |
||
| 1173 | } |
||
| 1174 | // add to array of registered modules |
||
| 1175 | EE_Registry::instance()->modules->$module_class = $module_path . $module_class . $module_ext; |
||
| 1176 | do_action( 'AHEE__EE_Config__register_module__complete', $module_class, EE_Registry::instance()->modules->$module_class ); |
||
| 1177 | return TRUE; |
||
| 1178 | } |
||
| 1179 | |||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * _initialize_shortcodes |
||
| 1183 | * allow shortcodes to set hooks for the rest of the system |
||
| 1184 | * |
||
| 1185 | * @access private |
||
| 1186 | * @return void |
||
| 1187 | */ |
||
| 1188 | private function _initialize_shortcodes() { |
||
| 1189 | // cycle thru shortcode folders |
||
| 1190 | foreach ( EE_Registry::instance()->shortcodes as $shortcode => $shortcode_path ) { |
||
| 1191 | // add class prefix |
||
| 1192 | $shortcode_class = 'EES_' . $shortcode; |
||
| 1193 | // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
||
| 1194 | // which set hooks ? |
||
| 1195 | if ( is_admin() ) { |
||
| 1196 | // fire immediately |
||
| 1197 | call_user_func( array( $shortcode_class, 'set_hooks_admin' )); |
||
| 1198 | } else { |
||
| 1199 | // delay until other systems are online |
||
| 1200 | add_action( 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', array( $shortcode_class,'set_hooks' )); |
||
| 1201 | // convert classname to UPPERCASE and create WP shortcode. |
||
| 1202 | $shortcode_tag = strtoupper( $shortcode ); |
||
| 1203 | // but first check if the shortcode has already been added before assigning 'fallback_shortcode_processor' |
||
| 1204 | if ( ! shortcode_exists( $shortcode_tag )) { |
||
| 1205 | // NOTE: this shortcode declaration will get overridden if the shortcode is successfully detected in the post content in EE_Front_Controller->_initialize_shortcodes() |
||
| 1206 | add_shortcode( $shortcode_tag, array( $shortcode_class, 'fallback_shortcode_processor' )); |
||
| 1207 | } |
||
| 1208 | } |
||
| 1209 | } |
||
| 1210 | } |
||
| 1211 | |||
| 1212 | |||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * _initialize_modules |
||
| 1216 | * allow modules to set hooks for the rest of the system |
||
| 1217 | * |
||
| 1218 | * @access private |
||
| 1219 | * @return void |
||
| 1220 | */ |
||
| 1221 | private function _initialize_modules() { |
||
| 1222 | // cycle thru shortcode folders |
||
| 1223 | foreach ( EE_Registry::instance()->modules as $module_class => $module_path ) { |
||
| 1224 | // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
||
| 1225 | // which set hooks ? |
||
| 1226 | if ( is_admin() ) { |
||
| 1227 | // fire immediately |
||
| 1228 | call_user_func( array( $module_class, 'set_hooks_admin' )); |
||
| 1229 | } else { |
||
| 1230 | // delay until other systems are online |
||
| 1231 | add_action( 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', array( $module_class,'set_hooks' )); |
||
| 1232 | } |
||
| 1233 | } |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | |||
| 1237 | |||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * register_route - adds module method routes to route_map |
||
| 1241 | * |
||
| 1242 | * @access public |
||
| 1243 | * @param string $route - "pretty" public alias for module method |
||
| 1244 | * @param string $module - module name (classname without EED_ prefix) |
||
| 1245 | * @param string $method_name - the actual module method to be routed to |
||
| 1246 | * @param string $key - url param key indicating a route is being called |
||
| 1247 | * @return bool |
||
| 1248 | */ |
||
| 1249 | public static function register_route( $route = NULL, $module = NULL, $method_name = NULL, $key = 'ee' ) { |
||
| 1250 | do_action( 'AHEE__EE_Config__register_route__begin', $route, $module, $method_name ); |
||
| 1251 | $module = str_replace( 'EED_', '', $module ); |
||
| 1252 | $module_class = 'EED_' . $module; |
||
| 1253 | if ( ! isset( EE_Registry::instance()->modules->$module_class )) { |
||
| 1254 | $msg = sprintf( __( 'The module %s has not been registered.', 'event_espresso' ), $module ); |
||
| 1255 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1256 | return FALSE; |
||
| 1257 | } |
||
| 1258 | View Code Duplication | if ( empty( $route )) { |
|
| 1259 | $msg = sprintf( __( 'No route has been supplied.', 'event_espresso' ), $route ); |
||
| 1260 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1261 | return FALSE; |
||
| 1262 | } |
||
| 1263 | View Code Duplication | if ( ! method_exists ( 'EED_' . $module, $method_name )) { |
|
| 1264 | $msg = sprintf( __( 'A valid class method for the %s route has not been supplied.', 'event_espresso' ), $route ); |
||
| 1265 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1266 | return FALSE; |
||
| 1267 | } |
||
| 1268 | EE_Config::$_module_route_map[ $key ][ $route ] = array( 'EED_' . $module, $method_name ); |
||
| 1269 | return TRUE; |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | |||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * get_route - get module method route |
||
| 1276 | * |
||
| 1277 | * @access public |
||
| 1278 | * @param string $route - "pretty" public alias for module method |
||
| 1279 | * @param string $key - url param key indicating a route is being called |
||
| 1280 | * @return string |
||
| 1281 | */ |
||
| 1282 | public static function get_route( $route = NULL, $key = 'ee' ) { |
||
| 1283 | do_action( 'AHEE__EE_Config__get_route__begin',$route ); |
||
| 1284 | $route = apply_filters( 'FHEE__EE_Config__get_route',$route ); |
||
| 1285 | if ( isset( EE_Config::$_module_route_map[ $key ][ $route ] )) { |
||
| 1286 | return EE_Config::$_module_route_map[ $key ][ $route ]; |
||
| 1287 | } |
||
| 1288 | return NULL; |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | |||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * get_routes - get ALL module method routes |
||
| 1295 | * |
||
| 1296 | * @access public |
||
| 1297 | * @return array |
||
| 1298 | */ |
||
| 1299 | public static function get_routes() { |
||
| 1302 | |||
| 1303 | |||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * register_forward - allows modules to forward request to another module for further processing |
||
| 1307 | * |
||
| 1308 | * @access public |
||
| 1309 | * @param string $route - "pretty" public alias for module method |
||
| 1310 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different forwards to be served based on status |
||
| 1311 | * @param array|string $forward - function name or array( class, method ) |
||
| 1312 | * @param string $key - url param key indicating a route is being called |
||
| 1313 | * @return bool |
||
| 1314 | */ |
||
| 1315 | public static function register_forward( $route = NULL, $status = 0, $forward = NULL, $key = 'ee' ) { |
||
| 1316 | do_action( 'AHEE__EE_Config__register_forward',$route,$status,$forward ); |
||
| 1317 | View Code Duplication | if ( ! isset( EE_Config::$_module_route_map[ $key ][ $route ] ) || empty( $route )) { |
|
| 1318 | $msg = sprintf( __( 'The module route %s for this forward has not been registered.', 'event_espresso' ), $route ); |
||
| 1319 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1320 | return FALSE; |
||
| 1321 | } |
||
| 1322 | View Code Duplication | if ( empty( $forward )) { |
|
| 1323 | $msg = sprintf( __( 'No forwarding route has been supplied.', 'event_espresso' ), $route ); |
||
| 1324 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1325 | return FALSE; |
||
| 1326 | } |
||
| 1327 | if ( is_array( $forward )) { |
||
| 1328 | View Code Duplication | if ( ! isset( $forward[1] )) { |
|
| 1329 | $msg = sprintf( __( 'A class method for the %s forwarding route has not been supplied.', 'event_espresso' ), $route ); |
||
| 1330 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1331 | return FALSE; |
||
| 1332 | } |
||
| 1333 | if ( ! method_exists( $forward[0], $forward[1] )) { |
||
| 1334 | $msg = sprintf( __( 'The class method %s for the %s forwarding route is in invalid.', 'event_espresso' ), $forward[1], $route ); |
||
| 1335 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1336 | return FALSE; |
||
| 1337 | } |
||
| 1338 | } else if ( ! function_exists( $forward )) { |
||
| 1339 | $msg = sprintf( __( 'The function %s for the %s forwarding route is in invalid.', 'event_espresso' ), $forward, $route ); |
||
| 1340 | EE_Error::add_error( $msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1341 | return FALSE; |
||
| 1342 | } |
||
| 1343 | EE_Config::$_module_forward_map[ $key ][ $route ][ absint( $status ) ] = $forward; |
||
| 1344 | return TRUE; |
||
| 1345 | } |
||
| 1346 | |||
| 1347 | |||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * get_forward - get forwarding route |
||
| 1351 | * |
||
| 1352 | * @access public |
||
| 1353 | * @param string $route - "pretty" public alias for module method |
||
| 1354 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different forwards to be served based on status |
||
| 1355 | * @param string $key - url param key indicating a route is being called |
||
| 1356 | * @return string |
||
| 1357 | */ |
||
| 1358 | View Code Duplication | public static function get_forward( $route = NULL, $status = 0, $key = 'ee' ) { |
|
| 1359 | do_action( 'AHEE__EE_Config__get_forward__begin',$route,$status ); |
||
| 1360 | if ( isset( EE_Config::$_module_forward_map[ $key ][ $route ][ $status ] )) { |
||
| 1361 | return apply_filters( 'FHEE__EE_Config__get_forward', EE_Config::$_module_forward_map[ $key ][ $route ][ $status ], $route,$status ); |
||
| 1362 | } |
||
| 1363 | return NULL; |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | |||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * register_forward - allows modules to specify different view templates for different method routes and status results |
||
| 1370 | * |
||
| 1371 | * @access public |
||
| 1372 | * @param string $route - "pretty" public alias for module method |
||
| 1373 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different views to be served based on status |
||
| 1374 | * @param string $view |
||
| 1375 | * @param string $key - url param key indicating a route is being called |
||
| 1376 | * @return bool |
||
| 1377 | */ |
||
| 1378 | public static function register_view( $route = NULL, $status = 0, $view = NULL, $key = 'ee' ) { |
||
| 1393 | |||
| 1394 | |||
| 1395 | |||
| 1396 | |||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * get_view - get view for route and status |
||
| 1400 | * |
||
| 1401 | * @access public |
||
| 1402 | * @param string $route - "pretty" public alias for module method |
||
| 1403 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different views to be served based on status |
||
| 1404 | * @param string $key - url param key indicating a route is being called |
||
| 1405 | * @return string |
||
| 1406 | */ |
||
| 1407 | View Code Duplication | public static function get_view( $route = NULL, $status = 0, $key = 'ee' ) { |
|
| 1408 | do_action( 'AHEE__EE_Config__get_view__begin',$route,$status ); |
||
| 1409 | if ( isset( EE_Config::$_module_view_map[ $key ][ $route ][ $status ] )) { |
||
| 1410 | return apply_filters( 'FHEE__EE_Config__get_view', EE_Config::$_module_view_map[ $key ][ $route ][ $status ], $route,$status ); |
||
| 1411 | } |
||
| 1412 | return NULL; |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | |||
| 1416 | |||
| 1417 | public function shutdown() { |
||
| 1420 | |||
| 1421 | |||
| 1422 | |||
| 1423 | } |
||
| 1424 | |||
| 1425 | |||
| 1426 | |||
| 1427 | |||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Base class used for config classes. These classes should generally not have |
||
| 1431 | * magic functions in use, except we'll allow them to magically set and get stuff... |
||
| 1432 | * basically, they should just be well-defined stdClasses |
||
| 1433 | */ |
||
| 1434 | class EE_Config_Base{ |
||
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Utility function for escaping the value of a property and returning. |
||
| 1438 | * |
||
| 1439 | * @param string $property property name (checks to see if exists). |
||
| 1440 | * @return mixed if a detected type found return the escaped value, otherwise just the raw value is returned. |
||
| 1441 | * @throws \EE_Error |
||
| 1442 | */ |
||
| 1443 | public function get_pretty( $property ) { |
||
| 1444 | if ( ! property_exists( $this, $property ) ) { |
||
| 1445 | throw new EE_Error( sprintf( __('%1$s::get_pretty() has been called with the property %2$s which does not exist on the %1$s config class.', 'event_espresso' ), get_class( $this ), $property ) ); |
||
| 1446 | } |
||
| 1447 | //just handling escaping of strings for now. |
||
| 1448 | if ( is_string( $this->$property ) ) { |
||
| 2643 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.