Complex classes like Admin_Menu 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 Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Admin_Menu extends Base_Admin_Menu { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Create the desired menu output. |
||
| 21 | */ |
||
| 22 | public function reregister_menu_items() { |
||
| 23 | /* |
||
| 24 | * Whether links should point to Calypso or wp-admin. |
||
| 25 | * |
||
| 26 | * Options: |
||
| 27 | * false - Calypso (Default). |
||
| 28 | * true - wp-admin. |
||
| 29 | */ |
||
| 30 | $wp_admin = $this->should_link_to_wp_admin(); |
||
| 31 | |||
| 32 | // Remove separators. |
||
| 33 | remove_menu_page( 'separator1' ); |
||
| 34 | |||
| 35 | $this->add_stats_menu(); |
||
| 36 | $this->add_upgrades_menu(); |
||
| 37 | $this->add_posts_menu( $wp_admin ); |
||
| 38 | $this->add_media_menu( $wp_admin ); |
||
| 39 | $this->add_page_menu( $wp_admin ); |
||
| 40 | $this->add_testimonials_menu( $wp_admin ); |
||
| 41 | $this->add_portfolio_menu( $wp_admin ); |
||
| 42 | $this->add_comments_menu( $wp_admin ); |
||
| 43 | |||
| 44 | // Whether Themes/Customize links should point to Calypso (false) or wp-admin (true). |
||
| 45 | $wp_admin_themes = $wp_admin; |
||
| 46 | $wp_admin_customize = $wp_admin; |
||
| 47 | $this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 48 | $this->add_plugins_menu( $wp_admin ); |
||
| 49 | $this->add_users_menu( $wp_admin ); |
||
| 50 | |||
| 51 | // Whether Import/Export links should point to Calypso (false) or wp-admin (true). |
||
| 52 | $wp_admin_import = $wp_admin; |
||
| 53 | $wp_admin_export = $wp_admin; |
||
| 54 | $this->add_tools_menu( $wp_admin_import, $wp_admin_export ); |
||
| 55 | |||
| 56 | $this->add_options_menu( $wp_admin ); |
||
| 57 | $this->add_jetpack_menu(); |
||
| 58 | $this->add_gutenberg_menus( $wp_admin ); |
||
| 59 | |||
| 60 | // Remove Links Manager menu since its usage is discouraged. https://github.com/Automattic/wp-calypso/issues/51188. |
||
| 61 | // @see https://core.trac.wordpress.org/ticket/21307#comment:73. |
||
| 62 | if ( $this->should_disable_links_manager() ) { |
||
| 63 | remove_menu_page( 'link-manager.php' ); |
||
| 64 | } |
||
| 65 | |||
| 66 | ksort( $GLOBALS['menu'] ); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check if Links Manager is being used. |
||
| 71 | */ |
||
| 72 | public function should_disable_links_manager() { |
||
| 73 | // The max ID number of the auto-generated links. |
||
| 74 | // See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com. |
||
| 75 | $max_default_id = 10; |
||
| 76 | |||
| 77 | // We are only checking the latest entry link_id so are limiting the query to 1. |
||
| 78 | $link_manager_links = get_bookmarks( |
||
| 79 | array( |
||
| 80 | 'orderby' => 'link_id', |
||
| 81 | 'order' => 'DESC', |
||
| 82 | 'limit' => 1, |
||
| 83 | 'hide_invisible' => 0, |
||
| 84 | ) |
||
| 85 | ); |
||
| 86 | |||
| 87 | // Ordered links by ID descending, check if the first ID is more than $max_default_id. |
||
| 88 | if ( count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | return true; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds My Home menu. |
||
| 97 | */ |
||
| 98 | public function add_my_home_menu() { |
||
| 99 | $this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' ); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Adds upsell nudge as a menu. |
||
| 104 | * |
||
| 105 | * @param object $nudge The $nudge object containing the content, CTA, link and tracks. |
||
| 106 | */ |
||
| 107 | public function add_upsell_nudge( $nudge ) { |
||
| 108 | $message = ' |
||
| 109 | <div class="upsell_banner"> |
||
| 110 | <div class="banner__info"> |
||
| 111 | <div class="banner__title">%1$s</div> |
||
| 112 | </div> |
||
| 113 | <div class="banner__action"> |
||
| 114 | <button type="button" class="button">%2$s</button> |
||
| 115 | </div> |
||
| 116 | </div>'; |
||
| 117 | |||
| 118 | $message = sprintf( |
||
| 119 | $message, |
||
| 120 | wp_kses( $nudge['content'], array() ), |
||
| 121 | wp_kses( $nudge['cta'], array() ) |
||
| 122 | ); |
||
| 123 | |||
| 124 | add_menu_page( 'site-notices', $message, 'read', 'https://wordpress.com' . $nudge['link'], null, null, 1 ); |
||
| 125 | add_filter( 'add_menu_classes', array( $this, 'set_site_notices_menu_class' ) ); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Adds a custom element class and id for Site Notices's menu item. |
||
| 130 | * |
||
| 131 | * @param array $menu Associative array of administration menu items. |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | public function set_site_notices_menu_class( array $menu ) { |
||
| 135 | foreach ( $menu as $key => $menu_item ) { |
||
| 136 | if ( 'site-notices' !== $menu_item[3] ) { |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | $classes = ' toplevel_page_site-notices'; |
||
| 141 | |||
| 142 | if ( isset( $menu_item[4] ) ) { |
||
| 143 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 144 | $menu[ $key ][5] = 'toplevel_page_site-notices'; |
||
| 145 | break; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return $menu; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Adds Stats menu. |
||
| 154 | */ |
||
| 155 | public function add_stats_menu() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Adds Upgrades menu. |
||
| 161 | * |
||
| 162 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|
|||
| 163 | */ |
||
| 164 | public function add_upgrades_menu( $plan = null ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Adds Posts menu. |
||
| 202 | * |
||
| 203 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 204 | */ |
||
| 205 | public function add_posts_menu( $wp_admin = false ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Adds Media menu. |
||
| 221 | * |
||
| 222 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 223 | */ |
||
| 224 | public function add_media_menu( $wp_admin = false ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Adds Page menu. |
||
| 236 | * |
||
| 237 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 238 | */ |
||
| 239 | public function add_page_menu( $wp_admin = false ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Adds Testimonials menu. |
||
| 253 | * |
||
| 254 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 255 | */ |
||
| 256 | public function add_testimonials_menu( $wp_admin = false ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Adds Portfolio menu. |
||
| 262 | * |
||
| 263 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 264 | */ |
||
| 265 | public function add_portfolio_menu( $wp_admin = false ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Adds a custom post type menu. |
||
| 271 | * |
||
| 272 | * @param string $post_type Custom post type. |
||
| 273 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 274 | */ |
||
| 275 | public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Adds Comments menu. |
||
| 289 | * |
||
| 290 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 291 | */ |
||
| 292 | public function add_comments_menu( $wp_admin = false ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Adds Appearance menu. |
||
| 302 | * |
||
| 303 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 304 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 305 | * @return string The Customizer URL. |
||
| 306 | */ |
||
| 307 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Adds Plugins menu. |
||
| 354 | * |
||
| 355 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 356 | */ |
||
| 357 | public function add_plugins_menu( $wp_admin = false ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Adds Users menu. |
||
| 370 | * |
||
| 371 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 372 | */ |
||
| 373 | public function add_users_menu( $wp_admin = false ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Adds Tools menu. |
||
| 400 | * |
||
| 401 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 402 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 403 | */ |
||
| 404 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Adds Settings menu. |
||
| 423 | * |
||
| 424 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 425 | */ |
||
| 426 | public function add_options_menu( $wp_admin = false ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Adds Jetpack menu. |
||
| 448 | */ |
||
| 449 | public function add_jetpack_menu() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Re-adds the Site Editor menu without the (beta) tag, and where we want it. |
||
| 478 | * |
||
| 479 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 480 | */ |
||
| 481 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Whether to use wp-admin pages rather than Calypso. |
||
| 507 | * |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function should_link_to_wp_admin() { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns the current slug from the URL. |
||
| 516 | * |
||
| 517 | * @param object $screen Screen object (undocumented). |
||
| 518 | * |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function get_current_slug( $screen ) { |
||
| 522 | $slug = "{$screen->base}.php"; |
||
| 523 | if ( '' !== $screen->post_type ) { |
||
| 524 | $slug = add_query_arg( 'post_type', $screen->post_type, $slug ); |
||
| 525 | } |
||
| 526 | if ( '' !== $screen->taxonomy ) { |
||
| 527 | $slug = add_query_arg( 'taxonomy', $screen->taxonomy, $slug ); |
||
| 528 | } |
||
| 529 | |||
| 530 | return $slug; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Prepend a dashboard swithcer to the "Screen Options" box of the current page. |
||
| 535 | * Callback for the 'screen_settings' filter (available in WP 3.0 and up). |
||
| 536 | * |
||
| 537 | * @param string $current The currently added panels in screen options. |
||
| 538 | * @param object $screen Screen object (undocumented). |
||
| 539 | * |
||
| 540 | * @return string The HTML code to append to "Screen Options" |
||
| 541 | */ |
||
| 542 | public function register_dashboard_switcher( $current, $screen ) { |
||
| 564 | } |
||
| 565 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.