Completed
Push — renovate/slack-web-api-5.x ( 42edfe...6ad45d )
by
unknown
184:59 queued 173:57
created

Admin_Menu   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 545
Duplicated Lines 14.68 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 64
lcom 2
cbo 1
dl 80
loc 545
rs 3.28
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 40 3
A add_my_home_menu() 0 17 3
A add_stats_menu() 0 3 1
A add_upgrades_menu() 11 11 1
A add_posts_menu() 18 18 2
A add_media_menu() 0 12 2
A add_page_menu() 17 17 2
A add_testimonials_menu() 0 3 1
A add_portfolio_menu() 0 3 1
B add_custom_post_type_menu() 0 44 9
A add_comments_menu() 0 21 3
C add_appearance_menu() 12 61 11
A appearance_parent_file() 0 7 2
A add_plugins_menu() 0 3 1
A add_users_menu() 0 21 4
A add_tools_menu() 0 18 2
A add_options_menu() 17 17 2
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

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