Completed
Push — update/phpcs-likes ( c38a56...586808 )
by
unknown
253:23 queued 243:22
created

Admin_Menu   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 426
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 426
rs 7.44
c 0
b 0
f 0
wmc 52
lcom 1
cbo 2

20 Methods

Rating   Name   Duplication   Size   Complexity  
A reregister_menu_items() 0 51 4
A should_disable_links_manager() 0 22 3
A add_my_home_menu() 0 3 1
A add_stats_menu() 0 3 1
A add_upgrades_menu() 0 23 5
A add_posts_menu() 0 11 2
A add_media_menu() 0 9 2
A add_page_menu() 0 11 2
A add_testimonials_menu() 0 3 1
A add_portfolio_menu() 0 3 1
A add_custom_post_type_menu() 0 11 2
A add_comments_menu() 0 7 2
B add_appearance_menu() 0 44 6
A add_plugins_menu() 0 10 2
A add_users_menu() 0 24 4
A add_tools_menu() 0 16 3
A add_options_menu() 0 10 2
A add_jetpack_menu() 0 25 3
A add_gutenberg_menus() 0 23 5
A should_link_to_wp_admin() 0 3 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Redirect;
11
12
require_once __DIR__ . '/class-base-admin-menu.php';
13
14
/**
15
 * Class Admin_Menu.
16
 */
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
	public function add_upgrades_menu() {
118
		global $menu;
119
120
		$menu_exists = false;
121
		foreach ( $menu as $item ) {
122
			if ( 'paid-upgrades.php' === $item[2] ) {
123
				$menu_exists = true;
124
				break;
125
			}
126
		}
127
128
		if ( ! $menu_exists ) {
129
			add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
130
		}
131
132
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/' . $this->domain, null, 5 );
133
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
134
135
		if ( ! $menu_exists ) {
136
			// Remove the submenu auto-created by Core.
137
			remove_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
138
		}
139
	}
140
141
	/**
142
	 * Adds Posts menu.
143
	 *
144
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
145
	 */
146
	public function add_posts_menu( $wp_admin = false ) {
147
		if ( $wp_admin ) {
148
			return;
149
		}
150
151
		$submenus_to_update = array(
152
			'edit.php'     => 'https://wordpress.com/posts/' . $this->domain,
153
			'post-new.php' => 'https://wordpress.com/post/' . $this->domain,
154
		);
155
		$this->update_submenus( 'edit.php', $submenus_to_update );
156
	}
157
158
	/**
159
	 * Adds Media menu.
160
	 *
161
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
162
	 */
163
	public function add_media_menu( $wp_admin = false ) {
164
		if ( $wp_admin ) {
165
			return;
166
		}
167
168
		remove_submenu_page( 'upload.php', 'media-new.php' );
169
170
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
171
	}
172
173
	/**
174
	 * Adds Page menu.
175
	 *
176
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
177
	 */
178
	public function add_page_menu( $wp_admin = false ) {
179
		if ( $wp_admin ) {
180
			return;
181
		}
182
183
		$submenus_to_update = array(
184
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
185
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
186
		);
187
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
188
	}
189
190
	/**
191
	 * Adds Testimonials menu.
192
	 *
193
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
194
	 */
195
	public function add_testimonials_menu( $wp_admin = false ) {
196
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
197
	}
198
199
	/**
200
	 * Adds Portfolio menu.
201
	 *
202
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
203
	 */
204
	public function add_portfolio_menu( $wp_admin = false ) {
205
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
206
	}
207
208
	/**
209
	 * Adds a custom post type menu.
210
	 *
211
	 * @param string $post_type Custom post type.
212
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
213
	 */
214
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
215
		if ( $wp_admin ) {
216
			return;
217
		}
218
219
		$submenus_to_update = array(
220
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
221
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
222
		);
223
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
224
	}
225
226
	/**
227
	 * Adds Comments menu.
228
	 *
229
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
230
	 */
231
	public function add_comments_menu( $wp_admin = false ) {
232
		if ( $wp_admin ) {
233
			return;
234
		}
235
236
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
237
	}
238
239
	/**
240
	 * Adds Appearance menu.
241
	 *
242
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
243
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
244
	 * @return string The Customizer URL.
245
	 */
246
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
247
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
248
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
249
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
250
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
251
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
252
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
253
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
254
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
255
256
		if ( ! $wp_admin_customize ) {
257
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
258
		} elseif ( $this->is_api_request ) {
259
			// In case this is an api request we will have to add the 'return' querystring via JS.
260
			$customize_url = 'customize.php';
261
		} else {
262
			$customize_url = $default_customize_slug;
263
		}
264
265
		$submenus_to_update = array(
266
			$default_customize_slug              => $customize_url,
267
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
268
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
269
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
270
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
271
		);
272
273
		if ( ! $wp_admin_themes ) {
274
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
275
		}
276
277
		if ( ! $wp_admin_customize ) {
278
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
279
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
280
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
281
		}
282
283
		$this->update_submenus( 'themes.php', $submenus_to_update );
284
285
		remove_submenu_page( 'themes.php', 'custom-header' );
286
		remove_submenu_page( 'themes.php', 'custom-background' );
287
288
		return $customize_url;
289
	}
290
291
	/**
292
	 * Adds Plugins menu.
293
	 *
294
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
295
	 */
296
	public function add_plugins_menu( $wp_admin = false ) {
297
		if ( $wp_admin ) {
298
			return;
299
		}
300
301
		remove_submenu_page( 'plugins.php', 'plugin-install.php' );
302
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
303
304
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
305
	}
306
307
	/**
308
	 * Adds Users 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_users_menu( $wp_admin = false ) {
313
		if ( current_user_can( 'list_users' ) ) {
314
			// We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ).
315
			$submenus_to_update = array(
316
				'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
317
				'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
318
			);
319
			if ( ! $wp_admin ) {
320
				$submenus_to_update['profile.php'] = 'https://wordpress.com/me';
321
			}
322
			$this->update_submenus( 'users.php', $submenus_to_update );
323
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
324
		} else {
325
			if ( ! $wp_admin ) {
326
				$submenus_to_update = array(
327
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
328
					'profile.php'  => 'https://wordpress.com/me',
329
				);
330
				$this->update_submenus( 'profile.php', $submenus_to_update );
331
			}
332
333
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
334
		}
335
	}
336
337
	/**
338
	 * Adds Tools menu.
339
	 *
340
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
341
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
342
	 */
343
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
344
		$submenus_to_update = array();
345
		if ( ! $wp_admin_import ) {
346
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
347
		}
348
		if ( ! $wp_admin_export ) {
349
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
350
		}
351
		$this->update_submenus( 'tools.php', $submenus_to_update );
352
353
		remove_submenu_page( 'tools.php', 'tools.php' );
354
		remove_submenu_page( 'tools.php', 'delete-blog' );
355
356
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
357
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
358
	}
359
360
	/**
361
	 * Adds Settings menu.
362
	 *
363
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
364
	 */
365
	public function add_options_menu( $wp_admin = false ) {
366
		if ( $wp_admin ) {
367
			return;
368
		}
369
370
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
371
372
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
373
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
374
	}
375
376
	/**
377
	 * Adds Jetpack menu.
378
	 */
379
	public function add_jetpack_menu() {
380
		$this->add_admin_menu_separator( 50, 'manage_options' );
381
382
		// TODO: Replace with proper SVG data url.
383
		$icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 32 32' %3E%3Cpath fill='%23a0a5aa' d='M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z'%3E%3C/path%3E%3Cpolygon fill='%23fff' points='15,19 7,19 15,3 '%3E%3C/polygon%3E%3Cpolygon fill='%23fff' points='17,29 17,13 25,13 '%3E%3C/polygon%3E%3C/svg%3E";
384
385
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, 51 );
386
		if ( ! $is_menu_updated ) {
387
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, 51 );
388
		}
389
390
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
391
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
392
		/* translators: Jetpack sidebar menu item. */
393
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
394
395
		remove_submenu_page( 'jetpack', 'stats' );
396
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
397
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
398
399
		if ( ! $is_menu_updated ) {
400
			// Remove the submenu auto-created by Core.
401
			remove_submenu_page( 'jetpack', 'jetpack' );
402
		}
403
	}
404
405
	/**
406
	 * Re-adds the Site Editor menu without the (beta) tag, and where we want it.
407
	 *
408
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
409
	 */
410
	public function add_gutenberg_menus( $wp_admin = false ) {
411
		// We can bail if we don't meet the conditions of the Site Editor.
412
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
413
			return;
414
		}
415
416
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
417
		remove_menu_page( 'gutenberg-edit-site' );
418
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
419
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
420
421
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
422
423
		add_menu_page(
424
			__( 'Site Editor', 'jetpack' ),
425
			__( 'Site Editor', 'jetpack' ),
426
			'edit_theme_options',
427
			$link,
428
			$wp_admin ? 'gutenberg_edit_site_page' : null,
429
			'dashicons-layout',
430
			61 // Just under Appearance.
431
		);
432
	}
433
434
	/**
435
	 * Whether to use wp-admin pages rather than Calypso.
436
	 *
437
	 * @return bool
438
	 */
439
	public function should_link_to_wp_admin() {
440
		return get_user_option( 'jetpack_admin_menu_link_destination' );
441
	}
442
}
443