Completed
Push — update/anchor-fm-checks-spotif... ( f9835e...28fd2e )
by
unknown
234:35 queued 224:59
created

Admin_Menu::add_appearance_menu()   C

Complexity

Conditions 12
Paths 128

Size

Total Lines 66

Duplication

Lines 12
Ratio 18.18 %

Importance

Changes 0
Metric Value
cc 12
nc 128
nop 1
dl 12
loc 66
rs 6.1284
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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