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 | * @access private |
||
28 | * @var EE_Admin $_instance |
||
29 | */ |
||
30 | private static $_instance; |
||
31 | |||
32 | |||
33 | |||
34 | /** |
||
35 | *@ singleton method used to instantiate class object |
||
36 | *@ access public |
||
37 | *@ return class instance |
||
38 | * |
||
39 | * @throws \EE_Error |
||
40 | */ |
||
41 | public static function instance() { |
||
42 | // check if class object is instantiated |
||
43 | if ( ! self::$_instance instanceof EE_Admin ) { |
||
44 | self::$_instance = new self(); |
||
45 | } |
||
46 | return self::$_instance; |
||
47 | } |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * class constructor |
||
53 | * |
||
54 | * @throws \EE_Error |
||
55 | */ |
||
56 | protected function __construct() { |
||
57 | // define global EE_Admin constants |
||
58 | $this->_define_all_constants(); |
||
59 | // set autoloaders for our admin page classes based on included path information |
||
60 | EEH_Autoloader::instance()->register_autoloaders_for_each_file_in_folder( EE_ADMIN ); |
||
61 | // admin hooks |
||
62 | add_filter( 'plugin_action_links', array( $this, 'filter_plugin_actions' ), 10, 2 ); |
||
63 | // load EE_Request_Handler early |
||
64 | add_action( 'AHEE__EE_System__core_loaded_and_ready', array( $this, 'get_request' )); |
||
65 | add_action( 'AHEE__EE_System__initialize_last', array( $this, 'init' )); |
||
66 | // post shortcode tracking |
||
67 | add_action( |
||
68 | 'AHEE__EE_System__initialize_last', |
||
69 | array( 'EventEspresso\core\admin\PostShortcodeTracking', 'set_hooks_admin' ) |
||
70 | ); |
||
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() { |
||
176 | //only enable most of the EE_Admin IF we're not in full maintenance mode |
||
177 | if ( EE_Maintenance_Mode::instance()->models_can_query() ){ |
||
178 | //ok so we want to enable the entire admin |
||
179 | add_action( 'wp_ajax_dismiss_ee_nag_notice', array( $this, 'dismiss_ee_nag_notice_callback' )); |
||
180 | add_action( 'admin_notices', array( $this, 'get_persistent_admin_notices' ), 9 ); |
||
181 | add_action( 'network_admin_notices', array( $this, 'get_persistent_admin_notices' ), 9 ); |
||
182 | //at a glance dashboard widget |
||
183 | add_filter( 'dashboard_glance_items', array( $this, 'dashboard_glance_items' ), 10 ); |
||
184 | //filter for get_edit_post_link used on comments for custom post types |
||
185 | add_filter( 'get_edit_post_link', array( $this, 'modify_edit_post_link' ), 10, 2 ); |
||
186 | } |
||
187 | // run the admin page factory but ONLY if we are doing an ee admin ajax request |
||
188 | if ( !defined('DOING_AJAX') || EE_ADMIN_AJAX ) { |
||
189 | try { |
||
190 | //this loads the controller for the admin pages which will setup routing etc |
||
191 | EE_Registry::instance()->load_core( 'Admin_Page_Loader' ); |
||
192 | } catch ( EE_Error $e ) { |
||
193 | $e->get_error(); |
||
194 | } |
||
195 | } |
||
196 | add_filter( 'content_save_pre', array( $this, 'its_eSpresso' ), 10, 1 ); |
||
197 | //make sure our CPTs and custom taxonomy metaboxes get shown for first time users |
||
198 | add_action('admin_head', array($this, 'enable_hidden_ee_nav_menu_metaboxes' ), 10 ); |
||
199 | add_action('admin_head', array( $this, 'register_custom_nav_menu_boxes' ), 10 ); |
||
200 | //exclude EE critical pages from all nav menus and wp_list_pages |
||
201 | add_filter('nav_menu_meta_box_object', array( $this, 'remove_pages_from_nav_menu'), 10 ); |
||
202 | } |
||
203 | |||
204 | |||
205 | |||
206 | |||
207 | /** |
||
208 | * 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. |
||
209 | * |
||
210 | * 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. |
||
211 | * @param object $post_type WP post type object |
||
212 | * @return object WP post type object |
||
213 | */ |
||
214 | public function remove_pages_from_nav_menu( $post_type ) { |
||
215 | //if this isn't the "pages" post type let's get out |
||
216 | if ( $post_type->name !== 'page' ) { |
||
217 | return $post_type; |
||
218 | } |
||
219 | $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
||
220 | |||
221 | $post_type->_default_query = array( |
||
222 | 'post__not_in' => $critical_pages ); |
||
223 | return $post_type; |
||
224 | } |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * 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 |
||
230 | * |
||
231 | * @access public |
||
232 | * @return void |
||
233 | */ |
||
234 | public function enable_hidden_ee_nav_menu_metaboxes() { |
||
259 | |||
260 | |||
261 | |||
262 | |||
263 | |||
264 | |||
265 | /** |
||
266 | * This method simply registers custom nav menu boxes for "nav_menus.php route" |
||
267 | * |
||
268 | * Currently EE is using this to make sure there are menu options for our CPT archive page routes. |
||
269 | * |
||
270 | * @todo modify this so its more dynamic and automatic for all ee CPTs and setups and can also be hooked into by addons etc. |
||
271 | * |
||
272 | * @access public |
||
273 | * @return void |
||
274 | */ |
||
275 | public function register_custom_nav_menu_boxes() { |
||
276 | add_meta_box( 'add-extra-nav-menu-pages', __('Event Espresso Pages', 'event_espresso'), array( $this, 'ee_cpt_archive_pages' ), 'nav-menus', 'side', 'core' ); |
||
277 | } |
||
278 | |||
279 | |||
280 | |||
281 | |||
282 | /** |
||
283 | * Use this to edit the post link for our cpts so that the edit link points to the correct page. |
||
284 | * |
||
285 | * @since 4.3.0 |
||
286 | * |
||
287 | * @param string $link the original link generated by wp |
||
288 | * @param int $id post id |
||
289 | * |
||
290 | * @return string the (maybe) modified link |
||
291 | */ |
||
292 | public function modify_edit_post_link( $link, $id ) { |
||
293 | if ( ! $post = get_post( $id ) ){ |
||
294 | return $link; |
||
295 | } |
||
296 | if ( $post->post_type === 'espresso_attendees' ) { |
||
297 | $query_args = array( |
||
298 | 'action' => 'edit_attendee', |
||
299 | 'post' => $id |
||
300 | ); |
||
301 | return EEH_URL::add_query_args_and_nonce( $query_args, admin_url('admin.php?page=espresso_registrations') ); |
||
302 | } |
||
303 | return $link; |
||
304 | } |
||
305 | |||
306 | |||
307 | |||
308 | |||
309 | public function ee_cpt_archive_pages() { |
||
310 | global $nav_menu_selected_id; |
||
311 | |||
312 | $db_fields = false; |
||
313 | $walker = new Walker_Nav_Menu_Checklist( $db_fields ); |
||
314 | $current_tab = 'event-archives'; |
||
315 | |||
316 | /*if ( ! empty( $_REQUEST['quick-search-posttype-' . $post_type_name] ) ) { |
||
317 | $current_tab = 'search'; |
||
318 | }/**/ |
||
319 | |||
320 | $removed_args = array( |
||
321 | 'action', |
||
322 | 'customlink-tab', |
||
323 | 'edit-menu-item', |
||
324 | 'menu-item', |
||
325 | 'page-tab', |
||
326 | '_wpnonce', |
||
327 | ); |
||
328 | |||
329 | ?> |
||
330 | <div id="posttype-extra-nav-menu-pages" class="posttypediv"> |
||
331 | <ul id="posttype-extra-nav-menu-pages-tabs" class="posttype-tabs add-menu-item-tabs"> |
||
332 | <li <?php echo ( 'event-archives' === $current_tab ? ' class="tabs"' : '' ); ?>> |
||
333 | <a class="nav-tab-link" data-type="tabs-panel-posttype-extra-nav-menu-pages-event-archives" href="<?php if ( $nav_menu_selected_id ) {echo esc_url(add_query_arg('extra-nav-menu-pages-tab', 'event-archives', remove_query_arg($removed_args)));} ?>#tabs-panel-posttype-extra-nav-menu-pages-event-archives"> |
||
334 | <?php _e( 'Event Archive Pages', 'event_espresso' ); ?> |
||
335 | </a> |
||
336 | </li> |
||
337 | <?php /* // temporarily removing but leaving skeleton in place in case we ever decide to add more tabs. |
||
338 | <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>> |
||
339 | <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . '-tab', 'all', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"> |
||
340 | <?php _e( 'View All' ); ?> |
||
341 | </a> |
||
342 | </li> |
||
343 | <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>> |
||
344 | <a class="nav-tab-link" data-type="tabs-panel-posttype-extra-nav-menu-pages-search" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg('extra-nav-menu-pages-tab', 'search', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-extra-nav-menu-pages-search"> |
||
345 | <?php _e( 'Search'); ?> |
||
346 | </a> |
||
347 | </li> --> |
||
348 | </ul><!-- .posttype-tabs --> |
||
349 | <?php */ ?> |
||
350 | |||
351 | <div id="tabs-panel-posttype-extra-nav-menu-pages-event-archives" class="tabs-panel <?php |
||
352 | echo ( 'event-archives' === $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); |
||
353 | ?>"> |
||
354 | <ul id="extra-nav-menu-pageschecklist-event-archives" class="categorychecklist form-no-clear"> |
||
355 | <?php |
||
356 | $pages = $this->_get_extra_nav_menu_pages_items(); |
||
357 | $args['walker'] = $walker; |
||
358 | echo walk_nav_menu_tree( array_map( array( $this, '_setup_extra_nav_menu_pages_items' ), $pages), 0, (object) $args ); |
||
359 | ?> |
||
360 | </ul> |
||
361 | </div><!-- /.tabs-panel --> |
||
362 | |||
363 | <p class="button-controls"> |
||
364 | <span class="list-controls"> |
||
365 | <a href="<?php |
||
366 | echo esc_url( add_query_arg( |
||
367 | array( |
||
368 | 'extra-nav-menu-pages-tab' => 'event-archives', |
||
369 | 'selectall' => 1, |
||
370 | ), |
||
371 | remove_query_arg( $removed_args ) |
||
372 | )); |
||
373 | ?>#posttype-extra-nav-menu-pages>" class="select-all"><?php _e('Select All'); ?></a> |
||
374 | </span> |
||
375 | |||
376 | <span class="add-to-menu"> |
||
377 | <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( __( 'Add to Menu' ) ); ?>" name="add-post-type-menu-item" id="<?php esc_attr_e( 'submit-posttype-extra-nav-menu-pages' ); ?>" /> |
||
378 | <span class="spinner"></span> |
||
379 | </span> |
||
380 | </p> |
||
381 | |||
382 | </div><!-- /.posttypediv --> |
||
383 | |||
384 | <?php |
||
385 | } |
||
386 | |||
387 | |||
388 | |||
389 | /** |
||
390 | * Returns an array of event archive nav items. |
||
391 | * |
||
392 | * @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 |
||
393 | * @return array |
||
394 | */ |
||
395 | View Code Duplication | private function _get_extra_nav_menu_pages_items() { |
|
403 | |||
404 | |||
405 | |||
406 | /** |
||
407 | * Setup nav menu walker item for usage in the event archive nav menu metabox. It receives a menu_item array with the properties and converts it to the menu item object. |
||
408 | * |
||
409 | * @see wp_setup_nav_menu_item() in wp-includes/nav-menu.php |
||
410 | * @param $menu_item_values |
||
411 | * @return stdClass |
||
412 | */ |
||
413 | private function _setup_extra_nav_menu_pages_items( $menu_item_values ) { |
||
438 | |||
439 | |||
440 | /** |
||
441 | * 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. |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | public function route_admin_request() {} |
||
446 | |||
447 | |||
448 | |||
449 | /** |
||
450 | * wp_loaded should fire on the WordPress wp_loaded hook. This fires on a VERY late priority. |
||
451 | * @return void |
||
452 | */ |
||
453 | public function wp_loaded() {} |
||
454 | |||
455 | |||
456 | |||
457 | |||
458 | /** |
||
459 | * admin_init |
||
460 | * |
||
461 | * @access public |
||
462 | * @return void |
||
463 | */ |
||
464 | public function admin_init() { |
||
465 | |||
466 | /** |
||
467 | * our cpt models must be instantiated on WordPress post processing routes (wp-admin/post.php), |
||
468 | * so any hooking into core WP routes is taken care of. So in this next few lines of code: |
||
469 | * - check if doing post processing. |
||
470 | * - check if doing post processing of one of EE CPTs |
||
471 | * - instantiate the corresponding EE CPT model for the post_type being processed. |
||
472 | */ |
||
473 | if ( isset( $_POST['action'], $_POST['post_type'] ) && $_POST['action'] === 'editpost' ) { |
||
474 | EE_Registry::instance()->load_core( 'Register_CPTs' ); |
||
475 | EE_Register_CPTs::instantiate_cpt_models( $_POST['post_type'] ); |
||
476 | } |
||
477 | |||
478 | |||
479 | /** |
||
480 | * This code is for removing any set EE critical pages from the "Static Page" option dropdowns on the |
||
481 | * 'options-reading.php' core WordPress admin settings page. This is for user-proofing. |
||
482 | */ |
||
483 | global $pagenow; |
||
484 | if ( $pagenow === 'options-reading.php' ) { |
||
485 | add_filter( 'wp_dropdown_pages', array( $this, 'modify_dropdown_pages' ) ); |
||
486 | } |
||
487 | |||
488 | } |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Callback for wp_dropdown_pages hook to remove ee critical pages from the dropdown selection. |
||
493 | * |
||
494 | * @param string $output Current output. |
||
495 | * @return string |
||
496 | */ |
||
497 | public function modify_dropdown_pages( $output ) { |
||
498 | //get critical pages |
||
499 | $critical_pages = EE_Registry::instance()->CFG->core->get_critical_pages_array(); |
||
500 | |||
501 | //split current output by line break for easier parsing. |
||
502 | $split_output = explode( "\n", $output ); |
||
503 | |||
504 | //loop through to remove any critical pages from the array. |
||
505 | foreach ( $critical_pages as $page_id ) { |
||
506 | $needle = 'value="' . $page_id . '"'; |
||
507 | foreach( $split_output as $key => $haystack ) { |
||
508 | if( strpos( $haystack, $needle ) !== false ) { |
||
509 | unset( $split_output[$key] ); |
||
510 | } |
||
511 | } |
||
512 | } |
||
513 | |||
514 | //replace output with the new contents |
||
515 | return implode( "\n", $split_output ); |
||
516 | } |
||
517 | |||
518 | |||
519 | |||
520 | /** |
||
521 | * enqueue all admin scripts that need loaded for admin pages |
||
522 | * |
||
523 | * @access public |
||
524 | * @return void |
||
525 | */ |
||
526 | public function enqueue_admin_scripts() { |
||
581 | |||
582 | |||
583 | |||
584 | /** |
||
585 | * display_admin_notices |
||
586 | * |
||
587 | * @access public |
||
588 | * @return string |
||
589 | */ |
||
590 | public function display_admin_notices() { |
||
593 | |||
594 | |||
595 | |||
596 | /** |
||
597 | * get_persistent_admin_notices |
||
598 | * |
||
599 | * @access public |
||
600 | * @return void |
||
601 | */ |
||
602 | public function get_persistent_admin_notices() { |
||
611 | |||
612 | |||
613 | |||
614 | /** |
||
615 | * dismiss_persistent_admin_notice |
||
616 | * |
||
617 | * @access public |
||
618 | * @return void |
||
619 | */ |
||
620 | public function dismiss_ee_nag_notice_callback() { |
||
623 | |||
624 | |||
625 | |||
626 | /** |
||
627 | * @param array $elements |
||
628 | * @return array |
||
629 | * @throws \EE_Error |
||
630 | */ |
||
631 | public function dashboard_glance_items($elements) { |
||
632 | $elements = is_array($elements) ? $elements : array($elements); |
||
655 | |||
656 | |||
657 | /** |
||
658 | * check_for_invalid_datetime_formats |
||
659 | * |
||
660 | * 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 |
||
661 | * |
||
662 | * @access public |
||
663 | * @param $value |
||
664 | * @param $option |
||
665 | * @throws EE_Error |
||
666 | * @return string |
||
667 | */ |
||
668 | public function check_for_invalid_datetime_formats( $value, $option ) { |
||
714 | |||
715 | |||
716 | |||
717 | /** |
||
718 | * its_eSpresso - converts the less commonly used spelling of "Expresso" to "Espresso" |
||
719 | * |
||
720 | * @access public |
||
721 | * @param $content |
||
722 | * @return string |
||
723 | */ |
||
724 | public function its_eSpresso( $content ) { |
||
727 | |||
728 | |||
729 | |||
730 | /** |
||
731 | * espresso_admin_footer |
||
732 | * |
||
733 | * @access public |
||
734 | * @return string |
||
735 | */ |
||
736 | public function espresso_admin_footer() { |
||
739 | |||
740 | |||
741 | |||
742 | /** |
||
743 | * static method for registering ee admin page. |
||
744 | * |
||
745 | * This method is deprecated in favor of the new location in EE_Register_Admin_Page::register. |
||
746 | * |
||
747 | * @since 4.3.0 |
||
748 | * @deprecated 4.3.0 Use EE_Register_Admin_Page::register() instead |
||
749 | * @see EE_Register_Admin_Page::register() |
||
750 | * |
||
751 | * @param $page_basename |
||
752 | * @param $page_path |
||
753 | * @param array $config |
||
754 | * @return void |
||
755 | */ |
||
756 | public static function register_ee_admin_page( $page_basename, $page_path, $config = array() ) { |
||
764 | |||
765 | |||
766 | |||
767 | /** |
||
768 | * @deprecated 4.8.41 |
||
769 | * @access public |
||
770 | * @param int $post_ID |
||
771 | * @param \WP_Post $post |
||
772 | * @return void |
||
773 | */ |
||
774 | public static function parse_post_content_on_save( $post_ID, $post ) { |
||
785 | |||
786 | |||
787 | |||
788 | /** |
||
789 | * @deprecated 4.8.41 |
||
790 | * @access public |
||
791 | * @param $option |
||
792 | * @param $old_value |
||
793 | * @param $value |
||
794 | * @return void |
||
795 | */ |
||
796 | public function reset_page_for_posts_on_change( $option, $old_value, $value ) { |
||
807 | |||
808 | } |
||
809 | // End of file EE_Admin.core.php |
||
811 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.