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_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 ); |
||
46 | |||
47 | $this->domain = ( new Status() )->get_site_suffix(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Determine if the current request is from API |
||
52 | */ |
||
53 | public function set_is_api_request() { |
||
54 | // Constant is not defined until parse_request. |
||
55 | if ( ! $this->is_api_request ) { |
||
56 | $this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST; |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns class instance. |
||
62 | * |
||
63 | * @return Admin_Menu |
||
64 | */ |
||
65 | public static function get_instance() { |
||
66 | $class = get_called_class(); |
||
67 | |||
68 | if ( empty( static::$instances[ $class ] ) ) { |
||
69 | static::$instances[ $class ] = new $class(); |
||
70 | } |
||
71 | |||
72 | return static::$instances[ $class ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Sets up class properties for REST API requests. |
||
77 | * |
||
78 | * @param \WP_REST_Response $response Response from the endpoint. |
||
79 | */ |
||
80 | public function rest_api_init( $response ) { |
||
81 | $this->is_api_request = true; |
||
82 | |||
83 | return $response; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Updates the menu data of the given menu slug. |
||
88 | * |
||
89 | * @param string $slug Slug of the menu to update. |
||
90 | * @param string $url New menu URL. |
||
|
|||
91 | * @param string $title New menu title. |
||
92 | * @param string $cap New menu capability. |
||
93 | * @param string $icon New menu icon. |
||
94 | * @param int $position New menu position. |
||
95 | * @return bool Whether the menu has been updated. |
||
96 | */ |
||
97 | public function update_menu( $slug, $url = null, $title = null, $cap = null, $icon = null, $position = null ) { |
||
98 | global $menu, $submenu; |
||
99 | |||
100 | $menu_item = null; |
||
101 | $menu_position = null; |
||
102 | |||
103 | foreach ( $menu as $i => $item ) { |
||
104 | if ( $slug === $item[2] ) { |
||
105 | $menu_item = $item; |
||
106 | $menu_position = $i; |
||
107 | break; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | if ( ! $menu_item ) { |
||
112 | return false; |
||
113 | } |
||
114 | |||
115 | if ( $title ) { |
||
116 | $menu_item[0] = $title; |
||
117 | $menu_item[3] = esc_attr( $title ); |
||
118 | } |
||
119 | |||
120 | if ( $cap ) { |
||
121 | $menu_item[1] = $cap; |
||
122 | } |
||
123 | |||
124 | // Change parent slug only if there are no submenus (the slug of the 1st submenu will be used if there are submenus). |
||
125 | if ( $url ) { |
||
126 | remove_submenu_page( $slug, $slug ); |
||
127 | if ( empty( $submenu[ $slug ] ) ) { |
||
128 | $menu_item[2] = $url; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | if ( $icon ) { |
||
133 | $menu_item[4] = 'menu-top'; |
||
134 | $menu_item[6] = $icon; |
||
135 | } |
||
136 | |||
137 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
||
138 | unset( $menu[ $menu_position ] ); |
||
139 | if ( $position ) { |
||
140 | $menu_position = $position; |
||
141 | } |
||
142 | $this->set_menu_item( $menu_item, $menu_position ); |
||
143 | |||
144 | // Only add submenu when there are other submenu items. |
||
145 | if ( $url && ! empty( $submenu[ $slug ] ) ) { |
||
146 | add_submenu_page( $slug, $menu_item[3], $menu_item[0], $menu_item[1], $url, null, 0 ); |
||
147 | } |
||
148 | |||
149 | return true; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Updates the submenus of the given menu slug. |
||
154 | * |
||
155 | * @param string $slug Menu slug. |
||
156 | * @param array $submenus_to_update Array of new submenu slugs. |
||
157 | */ |
||
158 | public function update_submenus( $slug, $submenus_to_update ) { |
||
173 | |||
174 | /** |
||
175 | * Adds a menu separator. |
||
176 | * |
||
177 | * @param int $position The position in the menu order this item should appear. |
||
178 | * @param string $cap Optional. The capability required for this menu to be displayed to the user. |
||
179 | * Default: 'read'. |
||
180 | */ |
||
181 | public function add_admin_menu_separator( $position = null, $cap = 'read' ) { |
||
192 | |||
193 | /** |
||
194 | * Enqueues scripts and styles. |
||
195 | */ |
||
196 | public function enqueue_scripts() { |
||
224 | |||
225 | /** |
||
226 | * Adds the given menu item in the specified position. |
||
227 | * |
||
228 | * @param array $item The menu item to add. |
||
229 | * @param int $position The position in the menu order this item should appear. |
||
230 | */ |
||
231 | public function set_menu_item( $item, $position = null ) { |
||
245 | |||
246 | /** |
||
247 | * Determines whether the current locale is right-to-left (RTL). |
||
248 | */ |
||
249 | public function is_rtl() { |
||
252 | |||
253 | /** |
||
254 | * Checks for any SVG icons in the menu, and overrides things so that |
||
255 | * we can display the icon in the correct colour for the theme. |
||
256 | */ |
||
257 | public function override_svg_icons() { |
||
304 | |||
305 | /** |
||
306 | * Whether to use wp-admin pages rather than Calypso. |
||
307 | * |
||
308 | * Options: |
||
309 | * false - Calypso (Default). |
||
310 | * true - wp-admin. |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | abstract public function should_link_to_wp_admin(); |
||
315 | |||
316 | /** |
||
317 | * Create the desired menu output. |
||
318 | */ |
||
319 | abstract public function reregister_menu_items(); |
||
320 | } |
||
321 |
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.