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 = '' ) { |
||
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() { |
|
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() { |
||
889 | |||
890 | |||
891 | /** |
||
892 | * initialize_shortcodes_and_modules |
||
893 | * meaning they can start adding their hooks to get stuff done |
||
894 | * |
||
895 | * @access public |
||
896 | * @return void |
||
897 | */ |
||
898 | public function initialize_shortcodes_and_modules() { |
||
904 | |||
905 | |||
906 | |||
907 | |||
908 | /** |
||
909 | * widgets_init |
||
910 | * |
||
911 | * @access private |
||
912 | * @return void |
||
913 | */ |
||
914 | public function widgets_init() { |
||
934 | |||
935 | |||
936 | |||
937 | /** |
||
938 | * register_ee_widget - makes core aware of this widget |
||
939 | * |
||
940 | * @access public |
||
941 | * @param string $widget_path - full path up to and including widget folder |
||
942 | * @return void |
||
943 | */ |
||
944 | public static function register_ee_widget( $widget_path = NULL ) { |
||
993 | |||
994 | |||
995 | |||
996 | /** |
||
997 | * _register_shortcodes |
||
998 | * |
||
999 | * @access private |
||
1000 | * @return array |
||
1001 | */ |
||
1002 | private function _register_shortcodes() { |
||
1017 | |||
1018 | |||
1019 | |||
1020 | /** |
||
1021 | * register_shortcode - makes core aware of this shortcode |
||
1022 | * |
||
1023 | * @access public |
||
1024 | * @param string $shortcode_path - full path up to and including shortcode folder |
||
1025 | * @return bool |
||
1026 | */ |
||
1027 | public static function register_shortcode( $shortcode_path = NULL ) { |
||
1079 | |||
1080 | |||
1081 | |||
1082 | |||
1083 | /** |
||
1084 | * _register_modules |
||
1085 | * |
||
1086 | * @access private |
||
1087 | * @return array |
||
1088 | */ |
||
1089 | private function _register_modules() { |
||
1109 | |||
1110 | |||
1111 | |||
1112 | |||
1113 | /** |
||
1114 | * register_module - makes core aware of this module |
||
1115 | * |
||
1116 | * @access public |
||
1117 | * @param string $module_path - full path up to and including module folder |
||
1118 | * @return bool |
||
1119 | */ |
||
1120 | public static function register_module( $module_path = NULL ) { |
||
1170 | |||
1171 | |||
1172 | /** |
||
1173 | * _initialize_shortcodes |
||
1174 | * allow shortcodes to set hooks for the rest of the system |
||
1175 | * |
||
1176 | * @access private |
||
1177 | * @return void |
||
1178 | */ |
||
1179 | private function _initialize_shortcodes() { |
||
1202 | |||
1203 | |||
1204 | |||
1205 | /** |
||
1206 | * _initialize_modules |
||
1207 | * allow modules to set hooks for the rest of the system |
||
1208 | * |
||
1209 | * @access private |
||
1210 | * @return void |
||
1211 | */ |
||
1212 | private function _initialize_modules() { |
||
1226 | |||
1227 | |||
1228 | |||
1229 | |||
1230 | /** |
||
1231 | * register_route - adds module method routes to route_map |
||
1232 | * |
||
1233 | * @access public |
||
1234 | * @param string $route - "pretty" public alias for module method |
||
1235 | * @param string $module - module name (classname without EED_ prefix) |
||
1236 | * @param string $method_name - the actual module method to be routed to |
||
1237 | * @param string $key - url param key indicating a route is being called |
||
1238 | * @return bool |
||
1239 | */ |
||
1240 | public static function register_route( $route = NULL, $module = NULL, $method_name = NULL, $key = 'ee' ) { |
||
1262 | |||
1263 | |||
1264 | |||
1265 | /** |
||
1266 | * get_route - get module method route |
||
1267 | * |
||
1268 | * @access public |
||
1269 | * @param string $route - "pretty" public alias for module method |
||
1270 | * @param string $key - url param key indicating a route is being called |
||
1271 | * @return string |
||
1272 | */ |
||
1273 | public static function get_route( $route = NULL, $key = 'ee' ) { |
||
1281 | |||
1282 | |||
1283 | |||
1284 | /** |
||
1285 | * get_routes - get ALL module method routes |
||
1286 | * |
||
1287 | * @access public |
||
1288 | * @return array |
||
1289 | */ |
||
1290 | public static function get_routes() { |
||
1293 | |||
1294 | |||
1295 | |||
1296 | /** |
||
1297 | * register_forward - allows modules to forward request to another module for further processing |
||
1298 | * |
||
1299 | * @access public |
||
1300 | * @param string $route - "pretty" public alias for module method |
||
1301 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different forwards to be served based on status |
||
1302 | * @param array|string $forward - function name or array( class, method ) |
||
1303 | * @param string $key - url param key indicating a route is being called |
||
1304 | * @return bool |
||
1305 | */ |
||
1306 | public static function register_forward( $route = NULL, $status = 0, $forward = NULL, $key = 'ee' ) { |
||
1337 | |||
1338 | |||
1339 | |||
1340 | /** |
||
1341 | * get_forward - get forwarding route |
||
1342 | * |
||
1343 | * @access public |
||
1344 | * @param string $route - "pretty" public alias for module method |
||
1345 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different forwards to be served based on status |
||
1346 | * @param string $key - url param key indicating a route is being called |
||
1347 | * @return string |
||
1348 | */ |
||
1349 | View Code Duplication | public static function get_forward( $route = NULL, $status = 0, $key = 'ee' ) { |
|
1356 | |||
1357 | |||
1358 | |||
1359 | /** |
||
1360 | * register_forward - allows modules to specify different view templates for different method routes and status results |
||
1361 | * |
||
1362 | * @access public |
||
1363 | * @param string $route - "pretty" public alias for module method |
||
1364 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different views to be served based on status |
||
1365 | * @param string $view |
||
1366 | * @param string $key - url param key indicating a route is being called |
||
1367 | * @return bool |
||
1368 | */ |
||
1369 | public static function register_view( $route = NULL, $status = 0, $view = NULL, $key = 'ee' ) { |
||
1384 | |||
1385 | |||
1386 | |||
1387 | |||
1388 | |||
1389 | /** |
||
1390 | * get_view - get view for route and status |
||
1391 | * |
||
1392 | * @access public |
||
1393 | * @param string $route - "pretty" public alias for module method |
||
1394 | * @param integer $status - integer value corresponding to status constant strings set in module parent class, allows different views to be served based on status |
||
1395 | * @param string $key - url param key indicating a route is being called |
||
1396 | * @return string |
||
1397 | */ |
||
1398 | View Code Duplication | public static function get_view( $route = NULL, $status = 0, $key = 'ee' ) { |
|
1405 | |||
1406 | |||
1407 | |||
1408 | public function shutdown() { |
||
1411 | |||
1412 | |||
1413 | |||
1414 | } |
||
1415 | |||
2634 |
Adding a
@return
annotation 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.