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() { |
||
48 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST || 0 === strpos( $_SERVER['REQUEST_URI'], '/?rest_route=%2Fwpcom%2Fv2%2Fadmin-menu' ); |
||
49 | $this->domain = ( new Status() )->get_site_suffix(); |
||
50 | |||
51 | add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99998 ); |
||
52 | add_action( 'admin_menu', array( $this, 'hide_parent_of_hidden_submenus' ), 99999 ); |
||
53 | |||
54 | if ( ! $this->is_api_request ) { |
||
55 | add_filter( 'admin_menu', array( $this, 'override_svg_icons' ), 99999 ); |
||
56 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 11 ); |
||
57 | add_action( 'admin_head', array( $this, 'set_site_icon_inline_styles' ) ); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Returns class instance. |
||
63 | * |
||
64 | * @return Admin_Menu |
||
65 | */ |
||
66 | public static function get_instance() { |
||
75 | |||
76 | /** |
||
77 | * Updates the menu data of the given menu slug. |
||
78 | * |
||
79 | * @param string $slug Slug of the menu to update. |
||
80 | * @param string $url New menu URL. |
||
|
|||
81 | * @param string $title New menu title. |
||
82 | * @param string $cap New menu capability. |
||
83 | * @param string $icon New menu icon. |
||
84 | * @param int $position New menu position. |
||
85 | * @return bool Whether the menu has been updated. |
||
86 | */ |
||
87 | public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { |
||
142 | |||
143 | /** |
||
144 | * Updates the submenus of the given menu slug. |
||
145 | * |
||
146 | * It hides the menu by adding the `hide-if-js` css class and duplicates the submenu with the new slug. |
||
147 | * |
||
148 | * @param string $slug Menu slug. |
||
149 | * @param array $submenus_to_update Array of new submenu slugs. |
||
150 | */ |
||
151 | public function update_submenus( $slug, $submenus_to_update ) { |
||
201 | |||
202 | /** |
||
203 | * Adds a menu separator. |
||
204 | * |
||
205 | * @param int $position The position in the menu order this item should appear. |
||
206 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
207 | * Default: 'read'. |
||
208 | */ |
||
209 | public function add_admin_menu_separator( $position = null, $cap = 'read' ) { |
||
220 | |||
221 | /** |
||
222 | * Enqueues scripts and styles. |
||
223 | */ |
||
224 | public function enqueue_scripts() { |
||
255 | |||
256 | /** |
||
257 | * Mark the core colors stylesheets as RTL depending on the value from the environment. |
||
258 | * This fixes a core issue where the extra RTL data is not added to the colors stylesheet. |
||
259 | * https://core.trac.wordpress.org/ticket/53090 |
||
260 | */ |
||
261 | public function configure_colors_for_rtl_stylesheets() { |
||
264 | |||
265 | /** |
||
266 | * Injects inline-styles for site icon for when third-party plugins remove enqueued stylesheets. |
||
267 | * Unable to use wp_add_inline_style as plugins remove styles from all non-standard handles |
||
268 | */ |
||
269 | public function set_site_icon_inline_styles() { |
||
278 | |||
279 | /** |
||
280 | * Hide the submenu page based on slug and return the item that was hidden. |
||
281 | * |
||
282 | * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response. |
||
283 | * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist. |
||
284 | * |
||
285 | * A false|array value is returned to be consistent with remove_submenu_page() function |
||
286 | * |
||
287 | * @param string $menu_slug The parent menu slug. |
||
288 | * @param string $submenu_slug The submenu slug that should be hidden. |
||
289 | * @return false|array |
||
290 | */ |
||
291 | public function hide_submenu_page( $menu_slug, $submenu_slug ) { |
||
310 | |||
311 | /** |
||
312 | * Apply the hide-if-js CSS class to a submenu item. |
||
313 | * |
||
314 | * @param int $index The position of a submenu item in the submenu array. |
||
315 | * @param string $parent_slug The parent slug. |
||
316 | * @param array $item The submenu item. |
||
317 | */ |
||
318 | public function hide_submenu_element( $index, $parent_slug, $item ) { |
||
326 | |||
327 | /** |
||
328 | * Check if the menu has submenu items visible |
||
329 | * |
||
330 | * @param array $submenu_items The submenu items. |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function has_visible_items( $submenu_items ) { |
||
341 | |||
342 | /** |
||
343 | * Return the number of existing submenu items under the supplied parent slug. |
||
344 | * |
||
345 | * @param string $parent_slug The slug of the parent menu. |
||
346 | * @return int The number of submenu items under $parent_slug. |
||
347 | */ |
||
348 | public function get_submenu_item_count( $parent_slug ) { |
||
357 | |||
358 | /** |
||
359 | * Adds the given menu item in the specified position. |
||
360 | * |
||
361 | * @param array $item The menu item to add. |
||
362 | * @param int $position The position in the menu order this item should appear. |
||
363 | */ |
||
364 | public function set_menu_item( $item, $position = null ) { |
||
378 | |||
379 | /** |
||
380 | * Determines whether the current locale is right-to-left (RTL). |
||
381 | */ |
||
382 | public function is_rtl() { |
||
385 | |||
386 | /** |
||
387 | * Checks for any SVG icons in the menu, and overrides things so that |
||
388 | * we can display the icon in the correct colour for the theme. |
||
389 | */ |
||
390 | public function override_svg_icons() { |
||
444 | |||
445 | /** |
||
446 | * Hide menus that are unauthorized and don't have visible submenus and cases when the menu has the same slug |
||
447 | * as the first submenu item. |
||
448 | * |
||
449 | * This must be done at the end of menu and submenu manipulation in order to avoid performing this check each time |
||
450 | * the submenus are altered. |
||
451 | */ |
||
452 | public function hide_parent_of_hidden_submenus() { |
||
484 | |||
485 | /** |
||
486 | * Sort the hidden submenus by moving them at the end of the array in order to avoid WP using them as default URLs. |
||
487 | * |
||
488 | * This operation has to be done at the end of submenu manipulation in order to guarantee that the hidden submenus |
||
489 | * are at the end of the array. |
||
490 | */ |
||
491 | public function sort_hidden_submenus() { |
||
506 | |||
507 | /** |
||
508 | * Check if the given item is visible or not in the admin menu. |
||
509 | * |
||
510 | * @param array $item A menu or submenu array. |
||
511 | */ |
||
512 | public function is_item_visible( $item ) { |
||
515 | |||
516 | /** |
||
517 | * Whether to use wp-admin pages rather than Calypso. |
||
518 | * |
||
519 | * Options: |
||
520 | * false - Calypso (Default). |
||
521 | * true - wp-admin. |
||
522 | * |
||
523 | * @return bool |
||
524 | */ |
||
525 | abstract public function should_link_to_wp_admin(); |
||
526 | |||
527 | /** |
||
528 | * Create the desired menu output. |
||
529 | */ |
||
530 | abstract public function reregister_menu_items(); |
||
531 | } |
||
532 |
This check looks for
@param
annotations 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.