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 | * Base_Admin_Menu constructor. |
||
| 39 | */ |
||
| 40 | protected function __construct() { |
||
| 41 | add_action( 'admin_menu', array( $this, 'set_is_api_request' ), 99997 ); |
||
| 42 | add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99998 ); |
||
| 43 | add_filter( 'admin_menu', array( $this, 'override_svg_icons' ), 99999 ); |
||
| 44 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
||
| 45 | add_action( 'admin_head', array( $this, 'set_site_icon_inline_styles' ) ); |
||
| 46 | add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 ); |
||
| 47 | |||
| 48 | $this->domain = ( new Status() )->get_site_suffix(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Determine if the current request is from API |
||
| 53 | */ |
||
| 54 | public function set_is_api_request() { |
||
| 55 | // Constant is not defined until parse_request. |
||
| 56 | if ( ! $this->is_api_request ) { |
||
| 57 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Returns class instance. |
||
| 63 | * |
||
| 64 | * @return Admin_Menu |
||
| 65 | */ |
||
| 66 | public static function get_instance() { |
||
| 67 | $class = get_called_class(); |
||
| 68 | |||
| 69 | if ( empty( static::$instances[ $class ] ) ) { |
||
| 70 | static::$instances[ $class ] = new $class(); |
||
| 71 | } |
||
| 72 | |||
| 73 | return static::$instances[ $class ]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sets up class properties for REST API requests. |
||
| 78 | * |
||
| 79 | * @param \WP_REST_Response $response Response from the endpoint. |
||
| 80 | */ |
||
| 81 | public function rest_api_init( $response ) { |
||
| 82 | $this->is_api_request = true; |
||
| 83 | |||
| 84 | return $response; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Updates the menu data of the given menu slug. |
||
| 89 | * |
||
| 90 | * @param string $slug Slug of the menu to update. |
||
| 91 | * @param string $url New menu URL. |
||
|
|
|||
| 92 | * @param string $title New menu title. |
||
| 93 | * @param string $cap New menu capability. |
||
| 94 | * @param string $icon New menu icon. |
||
| 95 | * @param int $position New menu position. |
||
| 96 | * @return bool Whether the menu has been updated. |
||
| 97 | */ |
||
| 98 | public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { |
||
| 99 | global $menu, $submenu; |
||
| 100 | |||
| 101 | $menu_item = null; |
||
| 102 | $menu_position = null; |
||
| 103 | |||
| 104 | foreach ( $menu as $i => $item ) { |
||
| 105 | if ( $slug === $item[2] ) { |
||
| 106 | $menu_item = $item; |
||
| 107 | $menu_position = $i; |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ( ! $menu_item ) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ( $title ) { |
||
| 117 | $menu_item[0] = $title; |
||
| 118 | $menu_item[3] = esc_attr( $title ); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ( $cap ) { |
||
| 122 | $menu_item[1] = $cap; |
||
| 123 | } |
||
| 124 | |||
| 125 | // Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus). |
||
| 126 | if ( $url ) { |
||
| 127 | remove_submenu_page( $slug, $slug ); |
||
| 128 | if ( empty( $submenu[ $slug ] ) ) { |
||
| 129 | $menu_item[2] = $url; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | if ( $icon ) { |
||
| 134 | $menu_item[4] = 'menu-top'; |
||
| 135 | $menu_item[6] = $icon; |
||
| 136 | } |
||
| 137 | |||
| 138 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
| 139 | unset( $menu[ $menu_position ] ); |
||
| 140 | if ( $position ) { |
||
| 141 | $menu_position = $position; |
||
| 142 | } |
||
| 143 | $this->set_menu_item( $menu_item, $menu_position ); |
||
| 144 | |||
| 145 | // Only add submenu when there are other submenu items. |
||
| 146 | if ( $url && ! empty( $submenu[ $slug ] ) ) { |
||
| 147 | add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 ); |
||
| 148 | } |
||
| 149 | |||
| 150 | return true; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Updates the submenus of the given menu slug. |
||
| 155 | * |
||
| 156 | * @param string $slug Menu slug. |
||
| 157 | * @param array $submenus_to_update Array of new submenu slugs. |
||
| 158 | */ |
||
| 159 | public function update_submenus( $slug, $submenus_to_update ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Adds a menu separator. |
||
| 177 | * |
||
| 178 | * @param int $position The position in the menu order this item should appear. |
||
| 179 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
| 180 | * Default: 'read'. |
||
| 181 | */ |
||
| 182 | public function add_admin_menu_separator( $position = null, $cap = 'read' ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Enqueues scripts and styles. |
||
| 196 | */ |
||
| 197 | public function enqueue_scripts() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Injects inline-styles for site icon for when third-party plugins remove enqueued stylesheets. |
||
| 228 | * Unable to use wp_add_inline_style as plugins remove styles from all non-standard handles |
||
| 229 | */ |
||
| 230 | public function set_site_icon_inline_styles() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Adds the given menu item in the specified position. |
||
| 242 | * |
||
| 243 | * @param array $item The menu item to add. |
||
| 244 | * @param int $position The position in the menu order this item should appear. |
||
| 245 | */ |
||
| 246 | public function set_menu_item( $item, $position = null ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Determines whether the current locale is right-to-left (RTL). |
||
| 263 | */ |
||
| 264 | public function is_rtl() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Checks for any SVG icons in the menu, and overrides things so that |
||
| 270 | * we can display the icon in the correct colour for the theme. |
||
| 271 | */ |
||
| 272 | public function override_svg_icons() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Whether to use wp-admin pages rather than Calypso. |
||
| 322 | * |
||
| 323 | * Options: |
||
| 324 | * false - Calypso (Default). |
||
| 325 | * true - wp-admin. |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | abstract public function should_link_to_wp_admin(); |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Create the desired menu output. |
||
| 333 | */ |
||
| 334 | abstract public function reregister_menu_items(); |
||
| 335 | } |
||
| 336 |
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.