Completed
Push — fix/undefined_blog_name ( 3f4435...c044a3 )
by
unknown
143:52 queued 132:58
created

Admin_Menu::add_users_menu()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

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