Complex classes like Base_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 Base_Admin_Menu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class Base_Admin_Menu { |
||
| 16 | /** |
||
| 17 | * Holds class instances. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $instances; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Whether the current request is a REST API request. |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $is_api_request = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Domain of the current site. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $domain; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The CSS classes used to hide the submenu items in navigation. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | const HIDE_CSS_CLASS = 'hide-if-js'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Base_Admin_Menu constructor. |
||
| 46 | */ |
||
| 47 | protected function __construct() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Determine if the current request is from API |
||
| 60 | */ |
||
| 61 | public function set_is_api_request() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns class instance. |
||
| 70 | * |
||
| 71 | * @return Admin_Menu |
||
| 72 | */ |
||
| 73 | public static function get_instance() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets up class properties for REST API requests. |
||
| 85 | * |
||
| 86 | * @param \WP_REST_Response $response Response from the endpoint. |
||
| 87 | */ |
||
| 88 | public function rest_api_init( $response ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Updates the menu data of the given menu slug. |
||
| 96 | * |
||
| 97 | * @param string $slug Slug of the menu to update. |
||
| 98 | * @param string $url New menu URL. |
||
|
|
|||
| 99 | * @param string $title New menu title. |
||
| 100 | * @param string $cap New menu capability. |
||
| 101 | * @param string $icon New menu icon. |
||
| 102 | * @param int $position New menu position. |
||
| 103 | * @return bool Whether the menu has been updated. |
||
| 104 | */ |
||
| 105 | public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { |
||
| 106 | global $menu, $submenu; |
||
| 107 | |||
| 108 | $menu_item = null; |
||
| 109 | $menu_position = null; |
||
| 110 | |||
| 111 | foreach ( $menu as $i => $item ) { |
||
| 112 | if ( $slug === $item[2] ) { |
||
| 113 | $menu_item = $item; |
||
| 114 | $menu_position = $i; |
||
| 115 | break; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | if ( ! $menu_item ) { |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( $title ) { |
||
| 124 | $menu_item[0] = $title; |
||
| 125 | $menu_item[3] = esc_attr( $title ); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( $cap ) { |
||
| 129 | $menu_item[1] = $cap; |
||
| 130 | } |
||
| 131 | |||
| 132 | // Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus). |
||
| 133 | if ( $url ) { |
||
| 134 | $this->hide_submenu_page( $slug, $slug ); |
||
| 135 | |||
| 136 | if ( ! isset( $submenu[ $slug ] ) || ! $this->has_visible_items( $submenu[ $slug ] ) ) { |
||
| 137 | $menu_item[2] = $url; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | if ( $icon ) { |
||
| 142 | $menu_item[4] = 'menu-top'; |
||
| 143 | $menu_item[6] = $icon; |
||
| 144 | } |
||
| 145 | |||
| 146 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 147 | unset( $menu[ $menu_position ] ); |
||
| 148 | if ( $position ) { |
||
| 149 | $menu_position = $position; |
||
| 150 | } |
||
| 151 | $this->set_menu_item( $menu_item, $menu_position ); |
||
| 152 | |||
| 153 | // Only add submenu when there are other submenu items. |
||
| 154 | if ( $url && isset( $submenu[ $slug ] ) && $this->has_visible_items( $submenu[ $slug ] ) ) { |
||
| 155 | add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 ); |
||
| 156 | } |
||
| 157 | |||
| 158 | return true; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Updates the submenus of the given menu slug. |
||
| 163 | * |
||
| 164 | * It hides the menu by adding the `hide-if-js` css class and duplicates the submenu with the new slug. |
||
| 165 | * |
||
| 166 | * @param string $slug Menu slug. |
||
| 167 | * @param array $submenus_to_update Array of new submenu slugs. |
||
| 168 | */ |
||
| 169 | public function update_submenus( $slug, $submenus_to_update ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Adds a menu separator. |
||
| 222 | * |
||
| 223 | * @param int $position The position in the menu order this item should appear. |
||
| 224 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 225 | * Default: 'read'. |
||
| 226 | */ |
||
| 227 | public function add_admin_menu_separator( $position = null, $cap = 'read' ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Enqueues scripts and styles. |
||
| 241 | */ |
||
| 242 | public function enqueue_scripts() { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Mark the core colors stylesheets as RTL depending on the value from the environment. |
||
| 276 | * This fixes a core issue where the extra RTL data is not added to the colors stylesheet. |
||
| 277 | * https://core.trac.wordpress.org/ticket/53090 |
||
| 278 | */ |
||
| 279 | public function configure_colors_for_rtl_stylesheets() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Injects inline-styles for site icon for when third-party plugins remove enqueued stylesheets. |
||
| 285 | * Unable to use wp_add_inline_style as plugins remove styles from all non-standard handles |
||
| 286 | */ |
||
| 287 | public function set_site_icon_inline_styles() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Hide the submenu page based on slug and return the item that was hidden. |
||
| 299 | * |
||
| 300 | * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response. |
||
| 301 | * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist. |
||
| 302 | * |
||
| 303 | * A false|array value is returned to be consistent with remove_submenu_page() function |
||
| 304 | * |
||
| 305 | * @param string $menu_slug The parent menu slug. |
||
| 306 | * @param string $submenu_slug The submenu slug that should be hidden. |
||
| 307 | * @return false|array |
||
| 308 | */ |
||
| 309 | public function hide_submenu_page( $menu_slug, $submenu_slug ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Apply the hide-if-js CSS class to a submenu item. |
||
| 331 | * |
||
| 332 | * @param int $index The position of a submenu item in the submenu array. |
||
| 333 | * @param string $parent_slug The parent slug. |
||
| 334 | * @param array $item The submenu item. |
||
| 335 | */ |
||
| 336 | public function hide_submenu_element( $index, $parent_slug, $item ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Check if the menu has submenu items visible |
||
| 347 | * |
||
| 348 | * @param array $submenu_items The submenu items. |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function has_visible_items( $submenu_items ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return the number of existing submenu items under the supplied parent slug. |
||
| 364 | * |
||
| 365 | * @param string $parent_slug The slug of the parent menu. |
||
| 366 | * @return int The number of submenu items under $parent_slug. |
||
| 367 | */ |
||
| 368 | public function get_submenu_item_count( $parent_slug ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Adds the given menu item in the specified position. |
||
| 380 | * |
||
| 381 | * @param array $item The menu item to add. |
||
| 382 | * @param int $position The position in the menu order this item should appear. |
||
| 383 | */ |
||
| 384 | public function set_menu_item( $item, $position = null ) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Determines whether the current locale is right-to-left (RTL). |
||
| 401 | */ |
||
| 402 | public function is_rtl() { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Checks for any SVG icons in the menu, and overrides things so that |
||
| 408 | * we can display the icon in the correct colour for the theme. |
||
| 409 | */ |
||
| 410 | public function override_svg_icons() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Whether to use wp-admin pages rather than Calypso. |
||
| 472 | * |
||
| 473 | * Options: |
||
| 474 | * false - Calypso (Default). |
||
| 475 | * true - wp-admin. |
||
| 476 | * |
||
| 477 | * @return bool |
||
| 478 | */ |
||
| 479 | abstract public function should_link_to_wp_admin(); |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Create the desired menu output. |
||
| 483 | */ |
||
| 484 | abstract public function reregister_menu_items(); |
||
| 485 | } |
||
| 486 |
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.