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 Stats menu. |
||
109 | */ |
||
110 | public function add_stats_menu() { |
||
111 | add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 ); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Adds Upgrades menu. |
||
116 | * |
||
117 | * @param string $plan The current WPCOM plan of the blog. |
||
|
|||
118 | */ |
||
119 | public function add_upgrades_menu( $plan = null ) { |
||
120 | global $menu; |
||
121 | |||
122 | $menu_exists = false; |
||
123 | foreach ( $menu as $item ) { |
||
124 | if ( 'paid-upgrades.php' === $item[2] ) { |
||
125 | $menu_exists = true; |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if ( ! $menu_exists ) { |
||
131 | if ( $plan ) { |
||
132 | // Add display:none as a default for cases when CSS is not loaded. |
||
133 | $site_upgrades = '%1$s<span class="inline-text" style="display:none">%2$s</span>'; |
||
134 | $site_upgrades = sprintf( |
||
135 | $site_upgrades, |
||
136 | __( 'Upgrades', 'jetpack' ), |
||
137 | $plan |
||
138 | ); |
||
139 | } else { |
||
140 | $site_upgrades = __( 'Upgrades', 'jetpack' ); |
||
141 | } |
||
142 | |||
143 | add_menu_page( __( 'Upgrades', 'jetpack' ), $site_upgrades, 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 ); |
||
144 | } |
||
145 | |||
146 | add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/my-plan/' . $this->domain, null, 1 ); |
||
147 | add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 2 ); |
||
148 | |||
149 | if ( ! $menu_exists ) { |
||
150 | // Remove the submenu auto-created by Core. |
||
151 | $this->hide_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' ); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Adds Posts menu. |
||
157 | * |
||
158 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
159 | */ |
||
160 | public function add_posts_menu( $wp_admin = false ) { |
||
161 | if ( $wp_admin ) { |
||
162 | return; |
||
163 | } |
||
164 | |||
165 | $submenus_to_update = array( |
||
166 | 'edit.php' => 'https://wordpress.com/posts/' . $this->domain, |
||
167 | 'post-new.php' => 'https://wordpress.com/post/' . $this->domain, |
||
168 | 'edit-tags.php?taxonomy=category' => 'https://wordpress.com/settings/taxonomies/category/' . $this->domain, |
||
169 | 'edit-tags.php?taxonomy=post_tag' => 'https://wordpress.com/settings/taxonomies/post_tag/' . $this->domain, |
||
170 | ); |
||
171 | $this->update_submenus( 'edit.php', $submenus_to_update ); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Adds Media menu. |
||
176 | * |
||
177 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
178 | */ |
||
179 | public function add_media_menu( $wp_admin = false ) { |
||
180 | if ( $wp_admin ) { |
||
181 | return; |
||
182 | } |
||
183 | |||
184 | $this->hide_submenu_page( 'upload.php', 'media-new.php' ); |
||
185 | |||
186 | $this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Adds Page menu. |
||
191 | * |
||
192 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
193 | */ |
||
194 | public function add_page_menu( $wp_admin = false ) { |
||
195 | if ( $wp_admin ) { |
||
196 | return; |
||
197 | } |
||
198 | |||
199 | $submenus_to_update = array( |
||
200 | 'edit.php?post_type=page' => 'https://wordpress.com/pages/' . $this->domain, |
||
201 | 'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain, |
||
202 | ); |
||
203 | $this->update_submenus( 'edit.php?post_type=page', $submenus_to_update ); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Adds Testimonials menu. |
||
208 | * |
||
209 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
210 | */ |
||
211 | public function add_testimonials_menu( $wp_admin = false ) { |
||
212 | $this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin ); |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Adds Portfolio menu. |
||
217 | * |
||
218 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
219 | */ |
||
220 | public function add_portfolio_menu( $wp_admin = false ) { |
||
221 | $this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin ); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Adds a custom post type menu. |
||
226 | * |
||
227 | * @param string $post_type Custom post type. |
||
228 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
229 | */ |
||
230 | public function add_custom_post_type_menu( $post_type, $wp_admin = false ) { |
||
231 | if ( $wp_admin ) { |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | $submenus_to_update = array( |
||
236 | 'edit.php?post_type=' . $post_type => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain, |
||
237 | 'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain, |
||
238 | ); |
||
239 | $this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update ); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Adds Comments menu. |
||
244 | * |
||
245 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
246 | */ |
||
247 | public function add_comments_menu( $wp_admin = false ) { |
||
248 | if ( $wp_admin ) { |
||
249 | return; |
||
250 | } |
||
251 | |||
252 | $this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Adds Appearance menu. |
||
257 | * |
||
258 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
259 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
260 | * @return string The Customizer URL. |
||
261 | */ |
||
262 | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { |
||
263 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; |
||
264 | $default_customize_slug = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' ); |
||
265 | $default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug ); |
||
266 | // TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links(). |
||
267 | $default_customize_header_slug_2 = admin_url( 'themes.php?page=custom-header' ); |
||
268 | $default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug ); |
||
269 | // TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links(). |
||
270 | $default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) ); |
||
271 | |||
272 | if ( ! $wp_admin_customize ) { |
||
273 | $customize_url = 'https://wordpress.com/customize/' . $this->domain; |
||
274 | } elseif ( $this->is_api_request ) { |
||
275 | // In case this is an api request we will have to add the 'return' querystring via JS. |
||
276 | $customize_url = 'customize.php'; |
||
277 | } else { |
||
278 | $customize_url = $default_customize_slug; |
||
279 | } |
||
280 | |||
281 | $submenus_to_update = array( |
||
282 | $default_customize_slug => $customize_url, |
||
283 | $default_customize_header_slug_1 => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ), |
||
284 | $default_customize_header_slug_2 => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ), |
||
285 | $default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ), |
||
286 | $default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ), |
||
287 | ); |
||
288 | |||
289 | if ( ! $wp_admin_themes ) { |
||
290 | $submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain; |
||
291 | } |
||
292 | |||
293 | if ( ! $wp_admin_customize ) { |
||
294 | $submenus_to_update['widgets.php'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url ); |
||
295 | $submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url ); |
||
296 | $submenus_to_update['nav-menus.php'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url ); |
||
297 | } |
||
298 | |||
299 | $this->update_submenus( 'themes.php', $submenus_to_update ); |
||
300 | |||
301 | $this->hide_submenu_page( 'themes.php', 'custom-header' ); |
||
302 | $this->hide_submenu_page( 'themes.php', 'custom-background' ); |
||
303 | |||
304 | return $customize_url; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Adds Plugins menu. |
||
309 | * |
||
310 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
311 | */ |
||
312 | public function add_plugins_menu( $wp_admin = false ) { |
||
322 | |||
323 | /** |
||
324 | * Adds Users menu. |
||
325 | * |
||
326 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
327 | */ |
||
328 | public function add_users_menu( $wp_admin = false ) { |
||
329 | if ( current_user_can( 'list_users' ) ) { |
||
330 | // We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ). |
||
331 | $submenus_to_update = array( |
||
332 | 'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain, |
||
333 | 'users.php' => 'https://wordpress.com/people/team/' . $this->domain, |
||
334 | ); |
||
335 | if ( ! $wp_admin ) { |
||
352 | |||
353 | /** |
||
354 | * Adds Tools menu. |
||
355 | * |
||
356 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
357 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
358 | */ |
||
359 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { |
||
375 | |||
376 | /** |
||
377 | * Adds Settings menu. |
||
378 | * |
||
379 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
380 | */ |
||
381 | public function add_options_menu( $wp_admin = false ) { |
||
391 | |||
392 | /** |
||
393 | * Adds Jetpack menu. |
||
394 | */ |
||
395 | public function add_jetpack_menu() { |
||
420 | |||
421 | /** |
||
422 | * Re-adds the Site Editor menu without the (beta) tag, and where we want it. |
||
423 | * |
||
424 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
425 | */ |
||
426 | public function add_gutenberg_menus( $wp_admin = false ) { |
||
449 | |||
450 | /** |
||
451 | * Whether to use wp-admin pages rather than Calypso. |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function should_link_to_wp_admin() { |
||
458 | } |
||
459 |
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.