Completed
Push — add/jetpack-assistant-ui ( 5d7d1b...7f10ad )
by
unknown
286:14 queued 274:26
created

Admin_Menu   F

Complexity

Total Complexity 74

Size/Duplication

Total Lines 606
Duplicated Lines 17.33 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 74
lcom 2
cbo 1
dl 105
loc 606
rs 2.474
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A get_instance() 0 9 2
A rest_api_init() 0 3 1
A reregister_menu_items() 0 35 3
A add_my_home_menu() 0 23 4
A add_stats_menu() 0 3 1
A add_posts_menu() 24 24 3
A add_media_menu() 0 19 3
A add_page_menu() 24 24 3
A add_testimonials_menu() 0 3 1
A add_portfolio_menu() 0 3 1
B add_custom_post_type_menu() 0 52 10
A add_comments_menu() 0 28 4
C add_appearance_menu() 12 66 12
A add_plugins_menu() 0 3 1
A add_users_menu() 0 28 5
A add_tools_menu() 0 24 3
A migrate_submenus() 0 14 4
A add_admin_menu_separator() 0 14 1
A enqueue_scripts() 5 22 4
A dequeue_scripts() 0 3 1
A should_link_to_wp_admin() 0 3 1
A add_upgrades_menu() 17 17 2
A add_options_menu() 23 23 3

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 Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Admin_Menu.
14
 */
15
class 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
	 * Admin_Menu constructor.
39
	 */
40
	protected function __construct() {
41
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
42
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
43
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
44
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
45
		add_action( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
46
47
		$this->domain = ( new Status() )->get_site_suffix();
48
	}
49
50
	/**
51
	 * Returns class instance.
52
	 *
53
	 * @return Admin_Menu
54
	 */
55
	public static function get_instance() {
56
		$class = get_called_class();
57
58
		if ( empty( static::$instances[ $class ] ) ) {
59
			static::$instances[ $class ] = new $class();
60
		}
61
62
		return static::$instances[ $class ];
63
	}
64
65
	/**
66
	 * Sets up class properties for REST API requests.
67
	 */
68
	public function rest_api_init() {
69
		$this->is_api_request = true;
70
	}
71
72
	/**
73
	 * Create the desired menu output.
74
	 */
75
	public function reregister_menu_items() {
76
		// Constant is not defined until parse_request.
77
		if ( ! $this->is_api_request ) {
78
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
79
		}
80
81
		/*
82
		 * Whether links should point to Calypso or wp-admin.
83
		 *
84
		 * Options:
85
		 * false - Calypso (Default).
86
		 * true  - wp-admin.
87
		 */
88
		$wp_admin = $this->should_link_to_wp_admin();
89
90
		// Remove separators.
91
		remove_menu_page( 'separator1' );
92
93
		$this->add_my_home_menu( $wp_admin );
94
		$this->add_stats_menu();
95
		$this->add_upgrades_menu();
96
		$this->add_posts_menu( $wp_admin );
97
		$this->add_media_menu( $wp_admin );
98
		$this->add_page_menu( $wp_admin );
99
		$this->add_testimonials_menu( $wp_admin );
100
		$this->add_portfolio_menu( $wp_admin );
101
		$this->add_comments_menu( $wp_admin );
102
		$this->add_appearance_menu( $wp_admin );
103
		$this->add_plugins_menu();
104
		$this->add_users_menu( $wp_admin );
105
		$this->add_tools_menu( $wp_admin );
106
		$this->add_options_menu( $wp_admin );
107
108
		ksort( $GLOBALS['menu'] );
109
	}
110
111
	/**
112
	 * Adds My Home menu.
113
	 *
114
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
115
	 */
116
	public function add_my_home_menu( $wp_admin = false ) {
117
		global $submenu;
118
119
		$menu_slug = $wp_admin ? 'index.php' : 'https://wordpress.com/home/' . $this->domain;
120
121
		remove_menu_page( 'index.php' );
122
		remove_submenu_page( 'index.php', 'index.php' );
123
124
		add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 'dashicons-admin-home', 2 );
125
126
		// Only add submenu when there are other submenu items.
127
		if ( ! empty( $submenu['index.php'] ) ) {
128
			add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 1 );
129
		}
130
131
		$this->migrate_submenus( 'index.php', $menu_slug );
132
		add_filter(
133
			'parent_file',
134
			function ( $parent_file ) use ( $menu_slug ) {
135
				return 'index.php' === $parent_file ? $menu_slug : $parent_file;
136
			}
137
		);
138
	}
139
140
	/**
141
	 * Adds Stats menu.
142
	 */
143
	public function add_stats_menu() {
144
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'edit_posts', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
145
	}
146
147
	/**
148
	 * Adds Upgrades menu.
149
	 */
150 View Code Duplication
	public function add_upgrades_menu() {
151
		remove_menu_page( 'paid-upgrades.php' );
152
153
		$menu_slug = 'https://wordpress.com/plans/' . $this->domain;
154
155
		add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 );
156
		add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 5 );
157
		add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
158
159
		$this->migrate_submenus( 'paid-upgrades.php', $menu_slug );
160
		add_filter(
161
			'parent_file',
162
			function ( $parent_file ) use ( $menu_slug ) {
163
				return 'paid-upgrades.php' === $parent_file ? $menu_slug : $parent_file;
164
			}
165
		);
166
	}
167
168
	/**
169
	 * Adds Posts menu.
170
	 *
171
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
172
	 */
173 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
174
		if ( $wp_admin ) {
175
			return;
176
		}
177
178
		$ptype_obj = get_post_type_object( 'post' );
179
		$menu_slug = 'https://wordpress.com/posts/' . $this->domain;
180
181
		remove_menu_page( 'edit.php' );
182
		remove_submenu_page( 'edit.php', 'edit.php' );
183
		remove_submenu_page( 'edit.php', 'post-new.php' );
184
185
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-post', $ptype_obj->menu_position );
186
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
187
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/post/' . $this->domain, null, 10 );
188
189
		$this->migrate_submenus( 'edit.php', $menu_slug );
190
		add_filter(
191
			'parent_file',
192
			function ( $parent_file ) use ( $menu_slug ) {
193
				return 'edit.php' === $parent_file ? $menu_slug : $parent_file;
194
			}
195
		);
196
	}
197
198
	/**
199
	 * Adds Media menu.
200
	 *
201
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
202
	 */
203
	public function add_media_menu( $wp_admin = false ) {
204
		remove_submenu_page( 'upload.php', 'upload.php' );
205
		remove_submenu_page( 'upload.php', 'media-new.php' );
206
207
		if ( ! $wp_admin ) {
208
			$menu_slug = 'https://wordpress.com/media/' . $this->domain;
209
210
			remove_menu_page( 'upload.php' );
211
			add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 );
212
			$this->migrate_submenus( 'upload.php', $menu_slug );
213
214
			add_filter(
215
				'parent_file',
216
				function ( $parent_file ) use ( $menu_slug ) {
217
					return 'upload.php' === $parent_file ? $menu_slug : $parent_file;
218
				}
219
			);
220
		}
221
	}
222
223
	/**
224
	 * Adds Page menu.
225
	 *
226
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
227
	 */
228 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
229
		if ( $wp_admin ) {
230
			return;
231
		}
232
233
		$ptype_obj = get_post_type_object( 'page' );
234
		$menu_slug = 'https://wordpress.com/pages/' . $this->domain;
235
236
		remove_menu_page( 'edit.php?post_type=page' );
237
		remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
238
		remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
239
240
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, 'dashicons-admin-page', $ptype_obj->menu_position );
241
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
242
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/page/' . $this->domain, null, 10 );
243
244
		$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
245
		add_filter(
246
			'parent_file',
247
			function ( $parent_file ) use ( $menu_slug ) {
248
				return 'edit.php?post_type=page' === $parent_file ? $menu_slug : $parent_file;
249
			}
250
		);
251
	}
252
253
	/**
254
	 * Adds Testimonials menu.
255
	 *
256
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
257
	 */
258
	public function add_testimonials_menu( $wp_admin = false ) {
259
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
260
	}
261
262
	/**
263
	 * Adds Portfolio menu.
264
	 *
265
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
266
	 */
267
	public function add_portfolio_menu( $wp_admin = false ) {
268
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
269
	}
270
271
	/**
272
	 * Adds a custom post type menu.
273
	 *
274
	 * @param string $post_type Custom post type.
275
	 * @param bool   $wp_admin  Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
276
	 */
277
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
278
		if ( $wp_admin ) {
279
			return;
280
		}
281
282
		$ptype_obj = get_post_type_object( $post_type );
283
		if ( empty( $ptype_obj ) ) {
284
			return;
285
		}
286
287
		$cpt_slug  = 'edit.php?post_type=' . $post_type;
288
		$menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain;
289
290
		remove_menu_page( $cpt_slug );
291
		remove_submenu_page( $cpt_slug, $cpt_slug );
292
		remove_submenu_page( $cpt_slug, 'post-new.php?post_type=' . $post_type );
293
294
		// Menu icon.
295
		$menu_icon = 'dashicons-admin-post';
296
		if ( is_string( $ptype_obj->menu_icon ) ) {
297
			// Special handling for data:image/svg+xml and Dashicons.
298
			if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
299
				$menu_icon = $ptype_obj->menu_icon;
300
			} else {
301
				$menu_icon = esc_url( $ptype_obj->menu_icon );
302
			}
303
		}
304
305
		/*
306
		 * Menu position.
307
		 *
308
		 * If $ptype_menu_position is already populated or will be populated
309
		 * by a hard-coded value below, increment the position.
310
		 */
311
		$ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$GLOBALS['_wp_last_object_menu'];
312
		$core_menu_positions = array( 59, 60, 65, 70, 75, 80, 85, 99 );
313
		while ( isset( $GLOBALS['menu'][ $ptype_menu_position ] ) || in_array( $ptype_menu_position, $core_menu_positions, true ) ) {
314
			$ptype_menu_position++;
315
		}
316
317
		add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $menu_slug, null, $menu_icon, $ptype_menu_position );
318
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
319
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain, null, 10 );
320
		$this->migrate_submenus( $cpt_slug, $menu_slug );
321
322
		add_filter(
323
			'parent_file',
324
			function ( $parent_file ) use ( $cpt_slug, $menu_slug ) {
325
				return $cpt_slug === $parent_file ? $menu_slug : $parent_file;
326
			}
327
		);
328
	}
329
330
	/**
331
	 * Adds Comments menu.
332
	 *
333
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
334
	 */
335
	public function add_comments_menu( $wp_admin = false ) {
336
		if ( $wp_admin || ! current_user_can( 'edit_posts' ) ) {
337
			return;
338
		}
339
340
		$awaiting_mod      = wp_count_comments();
341
		$awaiting_mod      = $awaiting_mod->moderated;
342
		$awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
343
		/* translators: %s: Number of comments. */
344
		$awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n );
345
346
		/* translators: %s: Number of comments. */
347
		$menu_title = sprintf( __( 'Comments %s', 'jetpack' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count" aria-hidden="true">' . $awaiting_mod_i18n . '</span><span class="comments-in-moderation-text screen-reader-text">' . $awaiting_mod_text . '</span></span>' );
348
		$menu_slug  = 'https://wordpress.com/comments/all/' . $this->domain;
349
350
		remove_menu_page( 'edit-comments.php' );
351
		remove_submenu_page( 'edit-comments.php', 'edit-comments.php' );
352
353
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 );
354
355
		$this->migrate_submenus( 'edit-comments.php', $menu_slug );
356
		add_filter(
357
			'parent_file',
358
			function ( $parent_file ) use ( $menu_slug ) {
359
				return 'edit-comments.php' === $parent_file ? $menu_slug : $parent_file;
360
			}
361
		);
362
	}
363
364
	/**
365
	 * Adds Appearance menu.
366
	 *
367
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
368
	 */
369
	public function add_appearance_menu( $wp_admin = false ) {
370
		$user_can_customize = current_user_can( 'customize' );
371
		$appearance_cap     = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
372
		$customize_slug     = $wp_admin ? 'customize.php' : 'https://wordpress.com/customize/' . $this->domain;
373
		$themes_slug        = $wp_admin ? 'themes.php' : 'https://wordpress.com/themes/' . $this->domain;
374
		$customize_url      = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore
375
		remove_menu_page( 'themes.php' );
376
		remove_submenu_page( 'themes.php', 'themes.php' );
377
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
378
		remove_submenu_page( 'themes.php', $customize_url );
379
		remove_submenu_page( 'themes.php', 'custom-header' );
380
		remove_submenu_page( 'themes.php', 'custom-background' );
381
382
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 );
383
		add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 5 );
384
		add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 10 );
385
386
		// Maintain id as JS selector.
387
		$GLOBALS['menu'][60][5] = 'menu-appearance'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
388
389
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
390
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
391
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
392
393
			// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
394
			$customize_header_url = admin_url( 'themes.php?page=custom-header' );
395
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
396
397
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug );
398
			add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 );
399
		}
400
401
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
402
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
403
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
404
405
			// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
406
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
407
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
408
409
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_slug );
410
			add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 );
411
		}
412
413 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
414
			remove_submenu_page( 'themes.php', 'widgets.php' );
415
416
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_slug );
417
			add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
418
		}
419
420 View Code Duplication
		if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
421
			remove_submenu_page( 'themes.php', 'nav-menus.php' );
422
423
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_slug );
424
			add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
425
		}
426
427
		$this->migrate_submenus( 'themes.php', $themes_slug );
428
		add_filter(
429
			'parent_file',
430
			function ( $parent_file ) use ( $themes_slug ) {
431
				return 'themes.php' === $parent_file ? $themes_slug : $parent_file;
432
			}
433
		);
434
	}
435
436
	/**
437
	 * Adds Plugins menu.
438
	 */
439
	public function add_plugins_menu() {
440
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
441
	}
442
443
	/**
444
	 * Adds Users menu.
445
	 *
446
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
447
	 */
448
	public function add_users_menu( $wp_admin = false ) {
449
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
450
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
451
		$profile_slug = $wp_admin ? 'profile.php' : 'https://wordpress.com/me';
452
453
		if ( current_user_can( 'list_users' ) ) {
454
			remove_menu_page( 'users.php' );
455
			remove_submenu_page( 'users.php', 'users.php' );
456
			remove_submenu_page( 'users.php', 'user-new.php' );
457
			remove_submenu_page( 'users.php', 'profile.php' );
458
			remove_submenu_page( 'users.php', 'grofiles-editor' );
459
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
460
461
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
462
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
463
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
464
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
465
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account', null, 20 );
466
467
			$this->migrate_submenus( 'users.php', $users_slug );
468
			add_filter(
469
				'parent_file',
470
				function ( $parent_file ) use ( $users_slug ) {
471
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
472
				}
473
			);
474
		}
475
	}
476
477
	/**
478
	 * Adds Tools menu.
479
	 *
480
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
481
	 */
482
	public function add_tools_menu( $wp_admin = false ) {
483
		if ( $wp_admin ) {
484
			return;
485
		}
486
487
		$admin_slug = 'tools.php';
488
		$menu_slug  = 'https://wordpress.com/marketing/tools/' . $this->domain;
489
490
		remove_menu_page( $admin_slug );
491
		remove_submenu_page( $admin_slug, $admin_slug );
492
		remove_submenu_page( $admin_slug, 'import.php' );
493
		remove_submenu_page( $admin_slug, 'delete-blog' );
494
495
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 );
496
		add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $this->domain, null, 15 );
497
498
		$this->migrate_submenus( $admin_slug, $menu_slug );
499
		add_filter(
500
			'parent_file',
501
			function ( $parent_file ) use ( $menu_slug ) {
502
				return 'tools.php' === $parent_file ? $menu_slug : $parent_file;
503
			}
504
		);
505
	}
506
507
	/**
508
	 * Adds Settings menu.
509
	 *
510
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
511
	 */
512 View Code Duplication
	public function add_options_menu( $wp_admin = false ) {
513
		if ( $wp_admin ) {
514
			return;
515
		}
516
517
		$options_slug = 'https://wordpress.com/settings/general/' . $this->domain;
518
519
		remove_menu_page( 'options-general.php' );
520
		remove_submenu_page( 'options-general.php', 'options-general.php' );
521
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
522
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
523
524
		add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', $options_slug, null, 'dashicons-admin-settings', 80 );
525
		add_submenu_page( $options_slug, esc_attr__( 'General', 'jetpack' ), __( 'General', 'jetpack' ), 'manage_options', $options_slug, null, 10 );
526
527
		$this->migrate_submenus( 'options-general.php', $options_slug );
528
		add_filter(
529
			'parent_file',
530
			function ( $parent_file ) use ( $options_slug ) {
531
				return 'options-general.php' === $parent_file ? $options_slug : $parent_file;
532
			}
533
		);
534
	}
535
536
	/**
537
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
538
	 *
539
	 * @param string $old_slug WP-Admin menu slug.
540
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
541
	 */
542
	public function migrate_submenus( $old_slug, $new_slug ) {
543
		global $submenu;
544
545
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
546
			if ( ! empty( $submenu[ $new_slug ] ) ) {
547
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
548
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
549
			} else {
550
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
551
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
552
			}
553
			unset( $submenu[ $old_slug ] );
554
		}
555
	}
556
557
	/**
558
	 * Adds a menu separator.
559
	 *
560
	 * @param int    $position The position in the menu order this item should appear.
561
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
562
	 *                         Default: 'read'.
563
	 */
564
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
565
		global $menu;
566
		static $uid = 3;
567
568
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
569
		$menu[ $position ] = array(
570
			'',                               // Menu title (ignored).
571
			$cap,                             // Required capability.
572
			'separator-custom-' . ( ++$uid ), // URL or file (ignored, but must be unique).
573
			'',                               // Page title (ignored).
574
			'wp-menu-separator',              // CSS class. Identifies this item as a separator.
575
		);
576
		ksort( $menu );
577
	}
578
579
	/**
580
	 * Enqueues scripts and styles.
581
	 */
582
	public function enqueue_scripts() {
583
		$style_dependencies = array();
0 ignored issues
show
Unused Code introduced by
$style_dependencies is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
584
		$rtl                = is_rtl() ? '-rtl' : '';
585 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
586
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
587
		} else {
588
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
589
		}
590
		wp_enqueue_style(
591
			'jetpack-admin-menu',
592
			plugins_url( 'admin-menu.css', __FILE__ ),
593
			$style_dependencies,
594
			JETPACK__VERSION
595
		);
596
		wp_enqueue_script(
597
			'jetpack-admin-menu',
598
			plugins_url( 'admin-menu.js', __FILE__ ),
599
			array(),
600
			JETPACK__VERSION,
601
			true
602
		);
603
	}
604
605
	/**
606
	 * Dequeues unnecessary scripts.
607
	 */
608
	public function dequeue_scripts() {
609
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
610
	}
611
612
	/**
613
	 * Whether to use wp-admin pages rather than Calypso.
614
	 *
615
	 * @return bool
616
	 */
617
	public function should_link_to_wp_admin() {
618
		return get_user_option( 'jetpack_admin_menu_link_destination' );
619
	}
620
}
621