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_Admin 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_Admin, 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_Admin { |
||
25 | |||
26 | /** |
||
27 | * EE_Admin Object |
||
28 | * @private _instance |
||
29 | * @private protected |
||
30 | */ |
||
31 | private static $_instance = NULL; |
||
32 | |||
33 | /** |
||
34 | * EE_Registry Object |
||
35 | * @var EE_Registry $EE |
||
36 | * @access protected |
||
37 | */ |
||
38 | protected $EE = NULL; |
||
39 | |||
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | *@ singleton method used to instantiate class object |
||
45 | *@ access public |
||
46 | *@ return class instance |
||
47 | */ |
||
48 | public static function instance() { |
||
49 | // check if class object is instantiated |
||
50 | if ( ! self::$_instance instanceof EE_Admin ) { |
||
51 | self::$_instance = new self(); |
||
52 | } |
||
53 | return self::$_instance; |
||
54 | } |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * class constructor |
||
60 | */ |
||
61 | protected function __construct() { |
||
62 | // define global EE_Admin constants |
||
63 | $this->_define_all_constants(); |
||
64 | // set autoloaders for our admin page classes based on included path information |
||
65 | EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder( EE_ADMIN ); |
||
66 | // admin hooks |
||
67 | add_filter( 'plugin_action_links', array( $this, 'filter_plugin_actions' ), 10, 2 ); |
||
68 | // load EE_Request_Handler early |
||
69 | add_action( 'AHEE__EE_System__core_loaded_and_ready', array( $this, 'get_request' )); |
||
70 | add_action( 'AHEE__EE_System__initialize_last', array( $this, 'init' )); |
||
71 | add_action( 'AHEE__EE_Admin_Page__route_admin_request', array( $this, 'route_admin_request' ), 100, 2 ); |
||
72 | add_action( 'wp_loaded', array( $this, 'wp_loaded' ), 100 ); |
||
73 | add_action( 'admin_init', array( $this, 'admin_init' ), 100 ); |
||
74 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 20 ); |
||
75 | add_action( 'admin_notices', array( $this, 'display_admin_notices' ), 10 ); |
||
76 | add_action( 'network_admin_notices', array( $this, 'display_admin_notices' ), 10 ); |
||
77 | add_filter( 'pre_update_option', array( $this, 'check_for_invalid_datetime_formats' ), 100, 2 ); |
||
78 | add_filter('admin_footer_text', array( $this, 'espresso_admin_footer' )); |
||
79 | |||
80 | //reset Environment config (we only do this on admin page loads); |
||
81 | EE_Registry::instance()->CFG->environment->recheck_values(); |
||
82 | |||
83 | do_action( 'AHEE__EE_Admin__loaded' ); |
||
84 | } |
||
85 | |||
86 | |||
87 | |||
88 | |||
89 | |||
90 | /** |
||
91 | * _define_all_constants |
||
92 | * define constants that are set globally for all admin pages |
||
93 | * |
||
94 | * @access private |
||
95 | * @return void |
||
96 | */ |
||
97 | private function _define_all_constants() { |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * filter_plugin_actions - adds links to the Plugins page listing |
||
109 | * |
||
110 | * @access public |
||
111 | * @param array $links |
||
112 | * @param string $plugin |
||
113 | * @return array |
||
114 | */ |
||
115 | public function filter_plugin_actions( $links, $plugin ) { |
||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * _get_request |
||
141 | * |
||
142 | * @access public |
||
143 | * @return void |
||
144 | */ |
||
145 | public function get_request() { |
||
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * hide_admin_pages_except_maintenance_mode |
||
154 | * |
||
155 | * @access public |
||
156 | * @param array $admin_page_folder_names |
||
157 | * @return array |
||
158 | */ |
||
159 | public function hide_admin_pages_except_maintenance_mode( $admin_page_folder_names = array() ){ |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * init- should fire after shortcode, module, addon, other plugin (default priority), and even EE_Front_Controller's init phases have run |
||
171 | * |
||
172 | * @access public |
||
173 | * @return void |
||
174 | */ |
||
175 | public function init() { |
||
209 | |||
210 | |||
211 | |||
212 | |||
213 | /** |
||
214 | * this simply hooks into the nav menu setup of pages metabox and makes sure that we remove EE critical pages from the list of options. |
||
215 | * |
||
216 | * the wp function "wp_nav_menu_item_post_type_meta_box" found in wp-admin/includes/nav-menu.php looks for the "_default_query" property on the post_type object and it uses that to override any queries found in the existing query for the given post type. Note that _default_query is not a normal property on the post_type object. It's found ONLY in this particular context. |
||
217 | * @param object $post_type WP post type object |
||
218 | * @return object WP post type object |
||
219 | */ |
||
220 | public function remove_pages_from_nav_menu( $post_type ) { |
||
231 | |||
232 | |||
233 | |||
234 | /** |
||
235 | * WP by default only shows three metaboxes in "nav-menus.php" for first times users. We want to make sure our metaboxes get shown as well |
||
236 | * |
||
237 | * @access public |
||
238 | * @return void |
||
239 | */ |
||
240 | public function enable_hidden_ee_nav_menu_metaboxes() { |
||
265 | |||
266 | |||
267 | |||
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * This method simply registers custom nav menu boxes for "nav_menus.php route" |
||
273 | * |
||
274 | * Currently EE is using this to make sure there are menu options for our CPT archive page routes. |
||
275 | * |
||
276 | * @todo modify this so its more dynamic and automatic for all ee CPTs and setups and can also be hooked into by addons etc. |
||
277 | * |
||
278 | * @access public |
||
279 | * @return void |
||
280 | */ |
||
281 | public function register_custom_nav_menu_boxes() { |
||
284 | |||
285 | |||
286 | |||
287 | |||
288 | /** |
||
289 | * Use this to edit the post link for our cpts so that the edit link points to the correct page. |
||
290 | * |
||
291 | * @since 4.3.0 |
||
292 | * |
||
293 | * @param string $link the original link generated by wp |
||
294 | * @param int $id post id |
||
295 | * @param string $context optional, defaults to display. How to write the '&' |
||
296 | * |
||
297 | * @return string the (maybe) modified link |
||
298 | */ |
||
299 | public function modify_edit_post_link( $link, $id, $context ) { |
||
313 | |||
314 | |||
315 | |||
316 | |||
317 | public function ee_cpt_archive_pages() { |
||
394 | |||
395 | |||
396 | |||
397 | /** |
||
398 | * Returns an array of event archive nav items. |
||
399 | * |
||
400 | * @todo for now this method is just in place so when it gets abstracted further we can substitute in whatever method we use for getting the extra nav menu items |
||
401 | * @return array |
||
402 | */ |
||
403 | View Code Duplication | private function _get_extra_nav_menu_pages_items() { |
|
411 | |||
412 | |||
413 | |||
414 | /** |
||
415 | * Setup nav menu walker item for usage in the event archive nav menu metabox. It receives a menu_item array with the properites and converts it to the menu item object. |
||
416 | * |
||
417 | * @see wp_setup_nav_menu_item() in wp-includes/nav-menu.php |
||
418 | * @param $menuitem |
||
419 | * @return stdClass |
||
420 | */ |
||
421 | private function _setup_extra_nav_menu_pages_items( $menuitem ) { |
||
446 | |||
447 | |||
448 | /** |
||
449 | * This is the action hook for the AHEE__EE_Admin_Page__route_admin_request hook that fires off right before an EE_Admin_Page route is called. |
||
450 | * |
||
451 | * @return void |
||
452 | */ |
||
453 | public function route_admin_request() {} |
||
454 | |||
455 | |||
456 | |||
457 | /** |
||
458 | * wp_loaded should fire on the WordPress wp_loaded hook. This fires on a VERY late priority. |
||
459 | * @return void |
||
460 | */ |
||
461 | public function wp_loaded() {} |
||
462 | |||
463 | |||
464 | |||
465 | |||
466 | /** |
||
467 | * admin_init |
||
468 | * |
||
469 | * @access public |
||
470 | * @return void |
||
471 | */ |
||
472 | public function admin_init() { |
||
499 | |||
500 | |||
501 | /** |
||
502 | * Callback for wp_dropdown_pages hook to remove ee critical pages from the dropdown selection. |
||
503 | * |
||
504 | * @param string $output Current output. |
||
505 | * @return string |
||
506 | */ |
||
507 | public function modify_dropdown_pages( $output ) { |
||
529 | |||
530 | |||
531 | |||
532 | /** |
||
533 | * enqueue all admin scripts that need loaded for admin pages |
||
534 | * |
||
535 | * @access public |
||
536 | * @return void |
||
537 | */ |
||
538 | public function enqueue_admin_scripts() { |
||
539 | // this javascript is loaded on every admin page to catch any injections ee needs to add to wp run js. |
||
540 | // Note: the intention of this script is to only do TARGETED injections. I.E, only injecting on certain script calls. |
||
541 | wp_enqueue_script('ee-inject-wp', EE_ADMIN_URL . 'assets/ee-cpt-wp-injects.js', array('jquery'), EVENT_ESPRESSO_VERSION, TRUE); |
||
542 | // register cookie script for future dependencies |
||
543 | wp_register_script('jquery-cookie', EE_THIRD_PARTY_URL . 'joyride/jquery.cookie.js', array('jquery'), '2.1', TRUE ); |
||
544 | // jquery_validate loading is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again via: add_filter( 'FHEE_load_jquery_validate', '__return_true' ); |
||
545 | View Code Duplication | if ( apply_filters( 'FHEE_load_jquery_validate', FALSE ) ) { |
|
546 | // register jQuery Validate |
||
547 | wp_register_script('jquery-validate', EE_GLOBAL_ASSETS_URL . 'scripts/jquery.validate.min.js', array('jquery'), '1.11.1', TRUE); |
||
548 | } |
||
549 | //joyride is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again vai: add_filter('FHEE_load_joyride', '__return_true' ); |
||
550 | if ( apply_filters( 'FHEE_load_joyride', FALSE ) ) { |
||
551 | //joyride style |
||
552 | wp_register_style('joyride-css', EE_THIRD_PARTY_URL . 'joyride/joyride-2.1.css', array(), '2.1'); |
||
553 | wp_register_style('ee-joyride-css', EE_GLOBAL_ASSETS_URL . 'css/ee-joyride-styles.css', array('joyride-css'), EVENT_ESPRESSO_VERSION ); |
||
554 | wp_register_script('joyride-modernizr', EE_THIRD_PARTY_URL . 'joyride/modernizr.mq.js', array(), '2.1', TRUE ); |
||
555 | //joyride JS |
||
556 | wp_register_script('jquery-joyride', EE_THIRD_PARTY_URL . 'joyride/jquery.joyride-2.1.js', array('jquery-cookie', 'joyride-modernizr'), '2.1', TRUE ); |
||
557 | // wanna go for a joyride? |
||
558 | wp_enqueue_style('ee-joyride-css'); |
||
559 | wp_enqueue_script('jquery-joyride'); |
||
560 | } |
||
561 | //qtip is turned OFF by default, but prior to the admin_enqueue_scripts hook, can be turned back on again via: add_filter('FHEE_load_qtips', '__return_true' ); |
||
562 | if ( apply_filters( 'FHEE_load_qtip', FALSE ) ) { |
||
563 | EE_Registry::instance()->load_helper('Qtip_Loader'); |
||
564 | EEH_Qtip_Loader::instance()->register_and_enqueue(); |
||
565 | } |
||
566 | //accounting.js library |
||
567 | // @link http://josscrowcroft.github.io/accounting.js/ |
||
568 | if ( apply_filters( 'FHEE_load_accounting_js', FALSE ) ) { |
||
569 | wp_register_script( 'ee-accounting', EE_GLOBAL_ASSETS_URL . 'scripts/ee-accounting-config.js', array('ee-accounting-core'), EVENT_ESPRESSO_VERSION, TRUE ); |
||
570 | wp_register_script( 'ee-accounting-core', EE_THIRD_PARTY_URL . 'accounting/accounting.js', array('underscore'), '0.3.2', TRUE ); |
||
571 | wp_enqueue_script( 'ee-accounting' ); |
||
572 | // array of settings to get converted to JSON array via wp_localize_script |
||
573 | $currency_config = array( |
||
574 | 'currency' => array( |
||
575 | 'symbol' => EE_Registry::instance()->CFG->currency->sign, |
||
576 | 'format' => array( |
||
577 | 'pos' => EE_Registry::instance()->CFG->currency->sign_b4 ? '%s%v' : '%v%s', |
||
578 | 'neg' => EE_Registry::instance()->CFG->currency->sign_b4 ? '- %s%v' : '- %v%s', |
||
579 | 'zero' => EE_Registry::instance()->CFG->currency->sign_b4 ? '%s--' : '--%s' |
||
580 | ), |
||
581 | 'decimal' => EE_Registry::instance()->CFG->currency->dec_mrk, |
||
582 | 'thousand' => EE_Registry::instance()->CFG->currency->thsnds, |
||
583 | 'precision' => EE_Registry::instance()->CFG->currency->dec_plc |
||
584 | ), |
||
585 | 'number' => array( |
||
586 | 'precision' => EE_Registry::instance()->CFG->currency->dec_plc, |
||
587 | 'thousand' => EE_Registry::instance()->CFG->currency->thsnds, |
||
588 | 'decimal' => EE_Registry::instance()->CFG->currency->dec_mrk |
||
589 | ) |
||
590 | ); |
||
591 | wp_localize_script('ee-accounting', 'EE_ACCOUNTING_CFG', $currency_config); |
||
592 | } |
||
593 | } |
||
594 | |||
595 | |||
596 | |||
597 | /** |
||
598 | * display_admin_notices |
||
599 | * |
||
600 | * @access public |
||
601 | * @return string |
||
602 | */ |
||
603 | public function display_admin_notices() { |
||
606 | |||
607 | |||
608 | |||
609 | /** |
||
610 | * get_persistent_admin_notices |
||
611 | * |
||
612 | * @access public |
||
613 | * @return void |
||
614 | */ |
||
615 | public function get_persistent_admin_notices() { |
||
624 | |||
625 | |||
626 | |||
627 | /** |
||
628 | * dismiss_persistent_admin_notice |
||
629 | * |
||
630 | * @access public |
||
631 | * @return void |
||
632 | */ |
||
633 | public function dismiss_ee_nag_notice_callback() { |
||
636 | |||
637 | |||
638 | |||
639 | /** |
||
640 | * @param $elements |
||
641 | * @return array |
||
642 | */ |
||
643 | public function dashboard_glance_items( $elements ) { |
||
666 | |||
667 | |||
668 | |||
669 | /** |
||
670 | * parse_post_content_on_save |
||
671 | * |
||
672 | * any time a post is saved, we need to check for any EE shortcodes that may be embedded in the content, |
||
673 | * and then track what posts those shortcodes are on, so that we can initialize shortcodes well before the_content() runs. |
||
674 | * this allows us to do things like enqueue scripts for shortcodes ONLY on the pages the shortcodes are actually used on |
||
675 | * |
||
676 | * @access public |
||
677 | * @param $post_ID |
||
678 | * @param $post |
||
679 | * @return void |
||
680 | */ |
||
681 | public static function parse_post_content_on_save( $post_ID, $post ) { |
||
741 | |||
742 | |||
743 | |||
744 | /** |
||
745 | * check_for_invalid_datetime_formats |
||
746 | * |
||
747 | * if an admin changes their date or time format settings on the WP General Settings admin page, verify that their selected format can be parsed by PHP |
||
748 | * |
||
749 | * @access public |
||
750 | * @param $value |
||
751 | * @param $option |
||
752 | * @throws EE_Error |
||
753 | * @return string |
||
754 | */ |
||
755 | public function check_for_invalid_datetime_formats( $value, $option ) { |
||
802 | |||
803 | |||
804 | |||
805 | /** |
||
806 | * reset_page_for_posts_on_change |
||
807 | * |
||
808 | * if an admin is on the WP Reading Settings page and changes the option for "Posts page", then we need to attribute any shortcodes for the previous blog page to the new blog page |
||
809 | * |
||
810 | * @access public |
||
811 | * @param $option |
||
812 | * @param $old_value |
||
813 | * @param $value |
||
814 | * @return void |
||
815 | */ |
||
816 | public function reset_page_for_posts_on_change( $option, $old_value, $value ) { |
||
826 | |||
827 | |||
828 | |||
829 | /** |
||
830 | * its_eSpresso - converts the less commonly used spelling of "Expresso" to "Espresso" |
||
831 | * |
||
832 | * @access public |
||
833 | * @param $content |
||
834 | * @return string |
||
835 | */ |
||
836 | public function its_eSpresso( $content ) { |
||
839 | |||
840 | |||
841 | |||
842 | /** |
||
843 | * espresso_admin_footer |
||
844 | * |
||
845 | * @access public |
||
846 | * @return string |
||
847 | */ |
||
848 | public function espresso_admin_footer() { |
||
855 | |||
856 | |||
857 | |||
858 | /** |
||
859 | * static method for registering ee admin page. |
||
860 | * |
||
861 | * This method is deprecated in favor of the new location in EE_Register_Admin_Page::register. |
||
862 | * |
||
863 | * @since 4.3.0 |
||
864 | * @deprecated 4.3.0 Use EE_Register_Admin_Page::register() instead |
||
865 | * @see EE_Register_Admin_Page::register() |
||
866 | * |
||
867 | * @param $page_basename |
||
868 | * @param $page_path |
||
869 | * @param array $config |
||
870 | * @return void |
||
871 | */ |
||
872 | public static function register_ee_admin_page( $page_basename, $page_path, $config = array() ) { |
||
878 | |||
879 | |||
880 | } |
||
881 | // End of file EE_Admin.core.php |
||
883 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.