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 | // Constant is not defined until parse_request. |
||
| 24 | if ( ! $this->is_api_request ) { |
||
| 25 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST; |
||
| 26 | } |
||
| 27 | |||
| 28 | /* |
||
| 29 | * Whether links should point to Calypso or wp-admin. |
||
| 30 | * |
||
| 31 | * Options: |
||
| 32 | * false - Calypso (Default). |
||
| 33 | * true - wp-admin. |
||
| 34 | */ |
||
| 35 | $wp_admin = $this->should_link_to_wp_admin(); |
||
| 36 | |||
| 37 | // Remove separators. |
||
| 38 | remove_menu_page( 'separator1' ); |
||
| 39 | |||
| 40 | $this->add_stats_menu(); |
||
| 41 | $this->add_upgrades_menu(); |
||
| 42 | $this->add_posts_menu( $wp_admin ); |
||
| 43 | $this->add_media_menu( $wp_admin ); |
||
| 44 | $this->add_page_menu( $wp_admin ); |
||
| 45 | $this->add_testimonials_menu( $wp_admin ); |
||
| 46 | $this->add_portfolio_menu( $wp_admin ); |
||
| 47 | $this->add_comments_menu( $wp_admin ); |
||
| 48 | |||
| 49 | // Whether Themes/Customize links should point to Calypso (false) or wp-admin (true). |
||
| 50 | $wp_admin_themes = $wp_admin; |
||
| 51 | $wp_admin_customize = $wp_admin; |
||
| 52 | $this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize ); |
||
| 53 | $this->add_plugins_menu( $wp_admin ); |
||
| 54 | $this->add_users_menu( $wp_admin ); |
||
| 55 | |||
| 56 | // Whether Import/Export links should point to Calypso (false) or wp-admin (true). |
||
| 57 | $wp_admin_import = $wp_admin; |
||
| 58 | $wp_admin_export = $wp_admin; |
||
| 59 | $this->add_tools_menu( $wp_admin_import, $wp_admin_export ); |
||
| 60 | |||
| 61 | $this->add_options_menu( $wp_admin ); |
||
| 62 | $this->add_jetpack_menu(); |
||
| 63 | $this->add_gutenberg_menus( $wp_admin ); |
||
| 64 | |||
| 65 | // Remove Links Manager menu since its usage is discouraged. https://github.com/Automattic/wp-calypso/issues/51188. |
||
| 66 | // @see https://core.trac.wordpress.org/ticket/21307#comment:73. |
||
| 67 | if ( $this->should_disable_links_manager() ) { |
||
| 68 | remove_menu_page( 'link-manager.php' ); |
||
| 69 | } |
||
| 70 | |||
| 71 | ksort( $GLOBALS['menu'] ); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Check if Links Manager is being used. |
||
| 76 | */ |
||
| 77 | public function should_disable_links_manager() { |
||
| 78 | // The max ID number of the auto-generated links. |
||
| 79 | // See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com. |
||
| 80 | $max_default_id = 10; |
||
| 81 | |||
| 82 | // We are only checking the latest entry link_id so are limiting the query to 1. |
||
| 83 | $link_manager_links = get_bookmarks( |
||
| 84 | array( |
||
| 85 | 'orderby' => 'link_id', |
||
| 86 | 'order' => 'DESC', |
||
| 87 | 'limit' => 1, |
||
| 88 | 'hide_invisible' => 0, |
||
| 89 | ) |
||
| 90 | ); |
||
| 91 | |||
| 92 | // Ordered links by ID descending, check if the first ID is more than $max_default_id. |
||
| 93 | if ( count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | return true; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Adds My Home menu. |
||
| 102 | */ |
||
| 103 | public function add_my_home_menu() { |
||
| 104 | $this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Adds upsell nudge as a menu. |
||
| 109 | * |
||
| 110 | * @param object $nudge The $nudge object containing the content, CTA, link and tracks. |
||
| 111 | */ |
||
| 112 | public function add_upsell_nudge( $nudge ) { |
||
| 113 | $message = ' |
||
| 114 | <div class="upsell_banner"> |
||
| 115 | <div class="banner__info"> |
||
| 116 | <div class="banner__title">%1$s</div> |
||
| 117 | </div> |
||
| 118 | <div class="banner__action"> |
||
| 119 | <button type="button" class="button">%2$s</button> |
||
| 120 | </div> |
||
| 121 | </div>'; |
||
| 122 | |||
| 123 | $message = sprintf( |
||
| 124 | $message, |
||
| 125 | wp_kses( $nudge['content'], array() ), |
||
| 126 | wp_kses( $nudge['cta'], array() ) |
||
| 127 | ); |
||
| 128 | |||
| 129 | add_menu_page( 'site-notices', $message, 'read', 'https://wordpress.com' . $nudge['link'], null, null, 1 ); |
||
| 130 | add_filter( 'add_menu_classes', array( $this, 'set_site_notices_menu_class' ) ); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Adds a custom element class and id for Site Notices's menu item. |
||
| 135 | * |
||
| 136 | * @param array $menu Associative array of administration menu items. |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function set_site_notices_menu_class( array $menu ) { |
||
| 140 | foreach ( $menu as $key => $menu_item ) { |
||
| 141 | if ( 'site-notices' !== $menu_item[3] ) { |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | $classes = ' toplevel_page_site-notices'; |
||
| 146 | |||
| 147 | if ( isset( $menu_item[4] ) ) { |
||
| 148 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
| 149 | $menu[ $key ][5] = 'toplevel_page_site-notices'; |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return $menu; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Adds Stats menu. |
||
| 159 | */ |
||
| 160 | public function add_stats_menu() { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Adds Upgrades menu. |
||
| 166 | * |
||
| 167 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|
|||
| 168 | */ |
||
| 169 | public function add_upgrades_menu( $plan = null ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Adds Posts menu. |
||
| 207 | * |
||
| 208 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 209 | */ |
||
| 210 | public function add_posts_menu( $wp_admin = false ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Adds Media menu. |
||
| 226 | * |
||
| 227 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 228 | */ |
||
| 229 | public function add_media_menu( $wp_admin = false ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Adds Page menu. |
||
| 241 | * |
||
| 242 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 243 | */ |
||
| 244 | public function add_page_menu( $wp_admin = false ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Adds Testimonials menu. |
||
| 258 | * |
||
| 259 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 260 | */ |
||
| 261 | public function add_testimonials_menu( $wp_admin = false ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Adds Portfolio menu. |
||
| 267 | * |
||
| 268 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 269 | */ |
||
| 270 | public function add_portfolio_menu( $wp_admin = false ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds a custom post type menu. |
||
| 276 | * |
||
| 277 | * @param string $post_type Custom post type. |
||
| 278 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 279 | */ |
||
| 280 | public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Adds Comments menu. |
||
| 294 | * |
||
| 295 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 296 | */ |
||
| 297 | public function add_comments_menu( $wp_admin = false ) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Adds Appearance menu. |
||
| 307 | * |
||
| 308 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 309 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 310 | * @return string The Customizer URL. |
||
| 311 | */ |
||
| 312 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Adds Plugins menu. |
||
| 359 | * |
||
| 360 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 361 | */ |
||
| 362 | public function add_plugins_menu( $wp_admin = false ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Adds Users menu. |
||
| 375 | * |
||
| 376 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 377 | */ |
||
| 378 | public function add_users_menu( $wp_admin = false ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Adds Tools menu. |
||
| 405 | * |
||
| 406 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 407 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
| 408 | */ |
||
| 409 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Adds Settings menu. |
||
| 428 | * |
||
| 429 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 430 | */ |
||
| 431 | public function add_options_menu( $wp_admin = false ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Adds Jetpack menu. |
||
| 448 | */ |
||
| 449 | public function add_jetpack_menu() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Re-adds the Site Editor menu without the (beta) tag, and where we want it. |
||
| 477 | * |
||
| 478 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
| 479 | */ |
||
| 480 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Whether to use wp-admin pages rather than Calypso. |
||
| 506 | * |
||
| 507 | * @return bool |
||
| 508 | */ |
||
| 509 | public function should_link_to_wp_admin() { |
||
| 512 | } |
||
| 513 |
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.