Completed
Push — fix/admin_menu_api_title_marku... ( 9c23c5...275fdb )
by
unknown
109:22 queued 100:15
created

Admin_Menu::add_upgrades_menu()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

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