Completed
Push — renovate/wordpress-monorepo ( 8cdadc...6203b6 )
by
unknown
552:54 queued 542:45
created

Admin_Menu   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 430
Duplicated Lines 5.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 430
rs 6.96
c 0
b 0
f 0
wmc 53
lcom 1
cbo 2

20 Methods

Rating   Name   Duplication   Size   Complexity  
A reregister_menu_items() 0 51 4
A should_disable_links_manager() 0 20 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() 11 11 2
A add_media_menu() 0 9 2
A add_page_menu() 11 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 31 4
A add_gutenberg_menus() 0 23 5
A should_link_to_wp_admin() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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