Completed
Push — fix/site-editor-menu ( 5f4451 )
by
unknown
282:19 queued 272:41
created

Admin_Menu::add_gutenberg_menus()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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