Completed
Push — add/cli-generate-gitaction ( f8af7a...9d2eaa )
by
unknown
44:31 queued 34:06
created

Admin_Menu::add_appearance_menu()   C

Complexity

Conditions 11
Paths 64

Size

Total Lines 68

Duplication

Lines 12
Ratio 17.65 %

Importance

Changes 0
Metric Value
cc 11
nc 64
nop 1
dl 12
loc 68
rs 6.5515
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 automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Redirect;
11
use Automattic\Jetpack\Status;
12
13
/**
14
 * Class Admin_Menu.
15
 */
16
class Admin_Menu {
17
	/**
18
	 * Holds class instances.
19
	 *
20
	 * @var array
21
	 */
22
	protected static $instances;
23
24
	/**
25
	 * Whether the current request is a REST API request.
26
	 *
27
	 * @var bool
28
	 */
29
	protected $is_api_request = false;
30
31
	/**
32
	 * Domain of the current site.
33
	 *
34
	 * @var string
35
	 */
36
	protected $domain;
37
38
	/**
39
	 * The customizer default link.
40
	 *
41
	 * @var string
42
	 */
43
	protected $customize_slug = 'customize.php';
44
45
	/**
46
	 * Admin_Menu constructor.
47
	 */
48
	protected function __construct() {
49
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
50
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
51
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
52
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
53
		add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
54
55
		$this->domain = ( new Status() )->get_site_suffix();
56
	}
57
58
	/**
59
	 * Returns class instance.
60
	 *
61
	 * @return Admin_Menu
62
	 */
63
	public static function get_instance() {
64
		$class = get_called_class();
65
66
		if ( empty( static::$instances[ $class ] ) ) {
67
			static::$instances[ $class ] = new $class();
68
		}
69
70
		return static::$instances[ $class ];
71
	}
72
73
	/**
74
	 * Sets up class properties for REST API requests.
75
	 *
76
	 * @param WP_REST_Response $response Response from the endpoint.
77
	 */
78
	public function rest_api_init( $response ) {
79
		$this->is_api_request = true;
80
81
		return $response;
82
	}
83
84
	/**
85
	 * Create the desired menu output.
86
	 */
87
	public function reregister_menu_items() {
88
		// Constant is not defined until parse_request.
89
		if ( ! $this->is_api_request ) {
90
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
91
		}
92
93
		/*
94
		 * Whether links should point to Calypso or wp-admin.
95
		 *
96
		 * Options:
97
		 * false - Calypso (Default).
98
		 * true  - wp-admin.
99
		 */
100
		$wp_admin = $this->should_link_to_wp_admin();
101
102
		// Remove separators.
103
		remove_menu_page( 'separator1' );
104
105
		$this->add_stats_menu();
106
		$this->add_upgrades_menu();
107
		$this->add_posts_menu( $wp_admin );
108
		$this->add_media_menu( $wp_admin );
109
		$this->add_page_menu( $wp_admin );
110
		$this->add_testimonials_menu( $wp_admin );
111
		$this->add_portfolio_menu( $wp_admin );
112
		$this->add_comments_menu( $wp_admin );
113
		$this->add_appearance_menu( $wp_admin );
114
		$this->add_plugins_menu( $wp_admin );
115
		$this->add_users_menu( $wp_admin );
116
117
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
118
		$wp_admin_import = $wp_admin;
119
		$wp_admin_export = $wp_admin;
120
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
121
122
		$this->add_options_menu( $wp_admin );
123
		$this->add_jetpack_menu();
124
125
		ksort( $GLOBALS['menu'] );
126
	}
127
128
	/**
129
	 * Adds My Home menu.
130
	 *
131
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
132
	 */
133
	public function add_my_home_menu( $wp_admin = false ) {
134
		global $submenu;
135
136
		$menu_slug = $wp_admin ? 'index.php' : 'https://wordpress.com/home/' . $this->domain;
137
		$cap       = $wp_admin ? 'read' : 'manage_options'; // Calypso's My Home is only available for admins.
138
139
		remove_menu_page( 'index.php' );
140
		remove_submenu_page( 'index.php', 'index.php' );
141
142
		add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 'dashicons-admin-home', 2 );
143
144
		// Only add submenu when there are other submenu items.
145 View Code Duplication
		if ( ! empty( $submenu['index.php'] ) ) {
146
			add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 1 );
147
		}
148
149
		$this->migrate_submenus( 'index.php', $menu_slug );
150
		add_filter(
151
			'parent_file',
152
			function ( $parent_file ) use ( $menu_slug ) {
153
				return 'index.php' === $parent_file ? $menu_slug : $parent_file;
154
			}
155
		);
156
	}
157
158
	/**
159
	 * Adds Stats menu.
160
	 */
161
	public function add_stats_menu() {
162
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
163
	}
164
165
	/**
166
	 * Adds Upgrades menu.
167
	 */
168 View Code Duplication
	public function add_upgrades_menu() {
169
		remove_menu_page( 'paid-upgrades.php' );
170
171
		$menu_slug = 'https://wordpress.com/plans/' . $this->domain;
172
173
		add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 );
174
		add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 5 );
175
		add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
176
177
		$this->migrate_submenus( 'paid-upgrades.php', $menu_slug );
178
		add_filter(
179
			'parent_file',
180
			function ( $parent_file ) use ( $menu_slug ) {
181
				return 'paid-upgrades.php' === $parent_file ? $menu_slug : $parent_file;
182
			}
183
		);
184
	}
185
186
	/**
187
	 * Adds Posts menu.
188
	 *
189
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
190
	 */
191 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
192
		if ( $wp_admin ) {
193
			return;
194
		}
195
196
		$ptype_obj = get_post_type_object( 'post' );
197
		$menu_slug = 'https://wordpress.com/posts/' . $this->domain;
198
199
		remove_menu_page( 'edit.php' );
200
		remove_submenu_page( 'edit.php', 'edit.php' );
201
		remove_submenu_page( 'edit.php', 'post-new.php' );
202
203
		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 );
204
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
205
		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 );
206
207
		$this->migrate_submenus( 'edit.php', $menu_slug );
208
		add_filter(
209
			'parent_file',
210
			function ( $parent_file ) use ( $menu_slug ) {
211
				return 'edit.php' === $parent_file ? $menu_slug : $parent_file;
212
			}
213
		);
214
	}
215
216
	/**
217
	 * Adds Media menu.
218
	 *
219
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
220
	 */
221
	public function add_media_menu( $wp_admin = false ) {
222
		remove_submenu_page( 'upload.php', 'upload.php' );
223
		remove_submenu_page( 'upload.php', 'media-new.php' );
224
225
		if ( ! $wp_admin ) {
226
			$menu_slug = 'https://wordpress.com/media/' . $this->domain;
227
228
			remove_menu_page( 'upload.php' );
229
			add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 );
230
			$this->migrate_submenus( 'upload.php', $menu_slug );
231
232
			add_filter(
233
				'parent_file',
234
				function ( $parent_file ) use ( $menu_slug ) {
235
					return 'upload.php' === $parent_file ? $menu_slug : $parent_file;
236
				}
237
			);
238
		}
239
	}
240
241
	/**
242
	 * Adds Page menu.
243
	 *
244
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
245
	 */
246 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
247
		if ( $wp_admin ) {
248
			return;
249
		}
250
251
		$ptype_obj = get_post_type_object( 'page' );
252
		$menu_slug = 'https://wordpress.com/pages/' . $this->domain;
253
254
		remove_menu_page( 'edit.php?post_type=page' );
255
		remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
256
		remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
257
258
		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 );
259
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
260
		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 );
261
262
		$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
263
		add_filter(
264
			'parent_file',
265
			function ( $parent_file ) use ( $menu_slug ) {
266
				return 'edit.php?post_type=page' === $parent_file ? $menu_slug : $parent_file;
267
			}
268
		);
269
	}
270
271
	/**
272
	 * Adds Testimonials menu.
273
	 *
274
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
275
	 */
276
	public function add_testimonials_menu( $wp_admin = false ) {
277
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
278
	}
279
280
	/**
281
	 * Adds Portfolio menu.
282
	 *
283
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
284
	 */
285
	public function add_portfolio_menu( $wp_admin = false ) {
286
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
287
	}
288
289
	/**
290
	 * Adds a custom post type menu.
291
	 *
292
	 * @param string $post_type Custom post type.
293
	 * @param bool   $wp_admin  Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
294
	 */
295
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
296
		if ( $wp_admin ) {
297
			return;
298
		}
299
300
		$ptype_obj = get_post_type_object( $post_type );
301
		if ( empty( $ptype_obj ) ) {
302
			return;
303
		}
304
305
		$cpt_slug  = 'edit.php?post_type=' . $post_type;
306
		$menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain;
307
308
		remove_menu_page( $cpt_slug );
309
		remove_submenu_page( $cpt_slug, $cpt_slug );
310
		remove_submenu_page( $cpt_slug, 'post-new.php?post_type=' . $post_type );
311
312
		// Menu icon.
313
		$menu_icon = 'dashicons-admin-post';
314
		if ( is_string( $ptype_obj->menu_icon ) ) {
315
			// Special handling for data:image/svg+xml and Dashicons.
316
			if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
317
				$menu_icon = $ptype_obj->menu_icon;
318
			} else {
319
				$menu_icon = esc_url( $ptype_obj->menu_icon );
320
			}
321
		}
322
323
		/*
324
		 * Menu position.
325
		 *
326
		 * If $ptype_menu_position is already populated or will be populated
327
		 * by a hard-coded value below, increment the position.
328
		 */
329
		$ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$GLOBALS['_wp_last_object_menu'];
330
		$core_menu_positions = array( 59, 60, 65, 70, 75, 80, 85, 99 );
331
		while ( isset( $GLOBALS['menu'][ $ptype_menu_position ] ) || in_array( $ptype_menu_position, $core_menu_positions, true ) ) {
332
			$ptype_menu_position++;
333
		}
334
335
		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 );
336
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
337
		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 );
338
		$this->migrate_submenus( $cpt_slug, $menu_slug );
339
340
		add_filter(
341
			'parent_file',
342
			function ( $parent_file ) use ( $cpt_slug, $menu_slug ) {
343
				return $cpt_slug === $parent_file ? $menu_slug : $parent_file;
344
			}
345
		);
346
	}
347
348
	/**
349
	 * Adds Comments menu.
350
	 *
351
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
352
	 */
353
	public function add_comments_menu( $wp_admin = false ) {
354
		if ( $wp_admin || ! current_user_can( 'edit_posts' ) ) {
355
			return;
356
		}
357
358
		$awaiting_mod      = wp_count_comments();
359
		$awaiting_mod      = $awaiting_mod->moderated;
360
		$awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
361
		/* translators: %s: Number of comments. */
362
		$awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n );
363
364
		/* translators: %s: Number of comments. */
365
		$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>' );
366
		$menu_slug  = 'https://wordpress.com/comments/all/' . $this->domain;
367
368
		remove_menu_page( 'edit-comments.php' );
369
		remove_submenu_page( 'edit-comments.php', 'edit-comments.php' );
370
371
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 );
372
373
		$this->migrate_submenus( 'edit-comments.php', $menu_slug );
374
		add_filter(
375
			'parent_file',
376
			function ( $parent_file ) use ( $menu_slug ) {
377
				return 'edit-comments.php' === $parent_file ? $menu_slug : $parent_file;
378
			}
379
		);
380
	}
381
382
	/**
383
	 * Adds Appearance menu.
384
	 *
385
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
386
	 */
387
	public function add_appearance_menu( $wp_admin = false ) {
388
		$user_can_customize = current_user_can( 'customize' );
389
		$appearance_cap     = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
390
		$themes_slug        = $wp_admin ? 'themes.php' : 'https://wordpress.com/themes/' . $this->domain;
391
		$customize_url      = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore
392
		remove_menu_page( 'themes.php' );
393
		remove_submenu_page( 'themes.php', 'themes.php' );
394
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
395
		remove_submenu_page( 'themes.php', $customize_url );
396
		remove_submenu_page( 'themes.php', 'custom-header' );
397
		remove_submenu_page( 'themes.php', 'custom-background' );
398
399
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 );
400
		add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 0 );
401
		add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $this->customize_slug, null, 1 );
402
403
		// Maintain id as JS selector.
404
		$GLOBALS['menu'][60][5] = 'menu-appearance'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
405
406
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
407
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
408
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
409
410
			// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
411
			$customize_header_url = admin_url( 'themes.php?page=custom-header' );
412
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
413
414
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $this->customize_slug );
415
			add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 );
416
		}
417
418
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
419
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
420
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
421
422
			// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
423
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
424
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
425
426
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $this->customize_slug );
427
			add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 );
428
		}
429
430 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
431
			remove_submenu_page( 'themes.php', 'widgets.php' );
432
433
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $this->customize_slug );
434
			add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
435
		}
436
437 View Code Duplication
		if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
438
			remove_submenu_page( 'themes.php', 'nav-menus.php' );
439
440
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $this->customize_slug );
441
			add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menu_url ), null, 20 );
442
		}
443
444
		// Register menu for the Custom CSS Jetpack module, but don't add it as a menu item.
445
		$GLOBALS['_registered_pages']['admin_page_editcss'] = true; // phpcs:ignore
446
447
		$this->migrate_submenus( 'themes.php', $themes_slug );
448
		add_filter(
449
			'parent_file',
450
			function ( $parent_file ) use ( $themes_slug ) {
451
				return 'themes.php' === $parent_file ? $themes_slug : $parent_file;
452
			}
453
		);
454
	}
455
456
	/**
457
	 * Adds Plugins menu.
458
	 *
459
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
460
	 */
461
	public function add_plugins_menu( $wp_admin = false ) {
462
		$menu_slug = $wp_admin ? 'plugins.php' : 'https://wordpress.com/plugins/' . $this->domain;
463
464
		remove_menu_page( 'plugins.php' );
465
		remove_submenu_page( 'plugins.php', 'plugins.php' );
466
		remove_submenu_page( 'plugins.php', 'plugin-install.php' );
467
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
468
469
		$count = '';
470
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
471
			$update_data = wp_get_update_data();
472
			$count       = sprintf(
473
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
474
				$update_data['counts']['plugins'],
475
				number_format_i18n( $update_data['counts']['plugins'] )
476
			);
477
		}
478
479
		/* translators: %s: Number of pending plugin updates. */
480
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', $menu_slug, null, 'dashicons-admin-plugins', 65 );
481
482
		$this->migrate_submenus( 'plugins.php', $menu_slug );
483
		add_filter(
484
			'parent_file',
485
			function ( $parent_file ) use ( $menu_slug ) {
486
				return 'jetpack' === $parent_file ? $menu_slug : $parent_file;
487
			}
488
		);
489
	}
490
491
	/**
492
	 * Adds Users menu.
493
	 *
494
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
495
	 */
496
	public function add_users_menu( $wp_admin = false ) {
497
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
498
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
499
		$profile_slug = $wp_admin ? 'profile.php' : 'https://wordpress.com/me';
500
501
		if ( current_user_can( 'list_users' ) ) {
502
			remove_menu_page( 'users.php' );
503
			remove_submenu_page( 'users.php', 'users.php' );
504
			remove_submenu_page( 'users.php', 'user-new.php' );
505
			remove_submenu_page( 'users.php', 'profile.php' );
506
			remove_submenu_page( 'users.php', 'grofiles-editor' );
507
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
508
509
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
510
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
511
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
512
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
513
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account', null, 20 );
514
515
			$this->migrate_submenus( 'users.php', $users_slug );
516
			add_filter(
517
				'parent_file',
518
				function ( $parent_file ) use ( $users_slug ) {
519
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
520
				}
521
			);
522
		}
523
	}
524
525
	/**
526
	 * Adds Tools menu.
527
	 *
528
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
529
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
530
	 */
531
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
532
		$admin_slug = 'tools.php';
533
		$menu_slug  = 'https://wordpress.com/marketing/tools/' . $this->domain;
534
535
		remove_menu_page( $admin_slug );
536
		remove_submenu_page( $admin_slug, $admin_slug );
537
		remove_submenu_page( $admin_slug, 'delete-blog' );
538
		remove_submenu_page( $admin_slug, 'import.php' );
539
		remove_submenu_page( $admin_slug, 'export.php' );
540
541
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'publish_posts', $menu_slug, null, 'dashicons-admin-tools', 75 );
542
		add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', $menu_slug );
543
		add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain );
544
		add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', $wp_admin_import ? 'import.php' : 'https://wordpress.com/import/' . $this->domain );
545
		add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', $wp_admin_export ? 'export.php' : 'https://wordpress.com/export/' . $this->domain );
546
547
		$this->migrate_submenus( $admin_slug, $menu_slug );
548
549
		add_submenu_page( $menu_slug, esc_attr__( 'Other tools', 'jetpack' ), __( 'Other tools', 'jetpack' ), 'manage_options', 'tools.php' );
550
551
		add_filter(
552
			'parent_file',
553
			function ( $parent_file ) use ( $menu_slug ) {
554
				return 'tools.php' === $parent_file ? $menu_slug : $parent_file;
555
			}
556
		);
557
	}
558
559
	/**
560
	 * Adds Settings menu.
561
	 *
562
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
563
	 */
564 View Code Duplication
	public function add_options_menu( $wp_admin = false ) {
565
		if ( $wp_admin ) {
566
			return;
567
		}
568
569
		$options_slug = 'https://wordpress.com/settings/general/' . $this->domain;
570
571
		remove_menu_page( 'options-general.php' );
572
		remove_submenu_page( 'options-general.php', 'options-general.php' );
573
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
574
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
575
576
		add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', $options_slug, null, 'dashicons-admin-settings', 80 );
577
		add_submenu_page( $options_slug, esc_attr__( 'General', 'jetpack' ), __( 'General', 'jetpack' ), 'manage_options', $options_slug, null, 10 );
578
579
		$this->migrate_submenus( 'options-general.php', $options_slug );
580
		add_filter(
581
			'parent_file',
582
			function ( $parent_file ) use ( $options_slug ) {
583
				return 'options-general.php' === $parent_file ? $options_slug : $parent_file;
584
			}
585
		);
586
	}
587
588
	/**
589
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
590
	 *
591
	 * @param string $old_slug WP-Admin menu slug.
592
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
593
	 */
594
	public function migrate_submenus( $old_slug, $new_slug ) {
595
		global $submenu;
596
597
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
598
			if ( ! empty( $submenu[ $new_slug ] ) ) {
599
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
600
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
601
			} else {
602
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
603
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
604
			}
605
			unset( $submenu[ $old_slug ] );
606
		}
607
	}
608
609
	/**
610
	 * Adds Jetpack menu.
611
	 */
612
	public function add_jetpack_menu() {
613
		global $menu;
614
615
		$position = 50;
616
		while ( isset( $menu[ $position ] ) ) {
617
			$position++;
618
		}
619
620
		// TODO: Replace with proper SVG data url.
621
		$jetpack_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";
622
		$jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain;
623
624
		$this->add_admin_menu_separator( $position++, 'manage_options' );
625
		add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position );
626
627
		// Maintain id for jQuery selector.
628
		$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
629
630
		remove_menu_page( 'jetpack' );
631
		remove_submenu_page( 'jetpack', 'stats' );
632
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
633
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
634
635
		$this->migrate_submenus( 'jetpack', $jetpack_slug );
636
637
		add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', $jetpack_slug, null, 2 );
638
		add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
639
		/* translators: Jetpack sidebar menu item. */
640
		add_submenu_page( $jetpack_slug, esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
641
642
		add_filter(
643
			'parent_file',
644
			function ( $parent_file ) use ( $jetpack_slug ) {
645
				return 'jetpack' === $parent_file ? $jetpack_slug : $parent_file;
646
			}
647
		);
648
	}
649
650
	/**
651
	 * Adds a menu separator.
652
	 *
653
	 * @param int    $position The position in the menu order this item should appear.
654
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
655
	 *                         Default: 'read'.
656
	 */
657
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
658
		global $menu;
659
660
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
661
		$menu[ $position ] = array(
662
			'',                                  // Menu title (ignored).
663
			$cap,                                // Required capability.
664
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
665
			'',                                  // Page title (ignored).
666
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
667
		);
668
		ksort( $menu );
669
	}
670
671
	/**
672
	 * Enqueues scripts and styles.
673
	 */
674
	public function enqueue_scripts() {
675
		$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...
676
		$rtl                = is_rtl() ? '-rtl' : '';
677 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
678
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
679
		} else {
680
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
681
		}
682
		wp_enqueue_style(
683
			'jetpack-admin-menu',
684
			plugins_url( 'admin-menu.css', __FILE__ ),
685
			$style_dependencies,
686
			JETPACK__VERSION
687
		);
688
		wp_enqueue_script(
689
			'jetpack-admin-menu',
690
			plugins_url( 'admin-menu.js', __FILE__ ),
691
			array(),
692
			JETPACK__VERSION,
693
			true
694
		);
695
	}
696
697
	/**
698
	 * Dequeues unnecessary scripts.
699
	 */
700
	public function dequeue_scripts() {
701
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
702
	}
703
704
	/**
705
	 * Whether to use wp-admin pages rather than Calypso.
706
	 *
707
	 * @return bool
708
	 */
709
	public function should_link_to_wp_admin() {
710
		return get_user_option( 'jetpack_admin_menu_link_destination' );
711
	}
712
}
713