Completed
Push — fix/admin-menu-my-home ( 63dadd )
by
unknown
33:27 queued 23:07
created

Admin_Menu::add_my_home_menu()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

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