Completed
Push — update/link-manager-only-when-... ( ebbd0a )
by
unknown
116:05 queued 104:10
created

Admin_Menu   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 429
Duplicated Lines 5.13 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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