Completed
Push — update/options-menu-advanced-s... ( b8045a )
by
unknown
13:13 queued 40s
created

Admin_Menu::add_my_home_menu()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 24

Duplication

Lines 3
Ratio 12.5 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 1
dl 3
loc 24
rs 9.2248
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Menu file.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Status;
11
12
/**
13
 * Class Admin_Menu.
14
 */
15
class Admin_Menu {
16
	/**
17
	 * Holds class instances.
18
	 *
19
	 * @var array
20
	 */
21
	protected static $instances;
22
23
	/**
24
	 * Whether the current request is a REST API request.
25
	 *
26
	 * @var bool
27
	 */
28
	protected $is_api_request = false;
29
30
	/**
31
	 * Domain of the current site.
32
	 *
33
	 * @var string
34
	 */
35
	protected $domain;
36
37
	/**
38
	 * The customizer default link.
39
	 *
40
	 * @var string
41
	 */
42
	protected $customize_slug = 'customize.php';
43
44
	/**
45
	 * Admin_Menu constructor.
46
	 */
47
	protected function __construct() {
48
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
49
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
50
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
51
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
52
		add_filter( 'rest_request_before_callbacks', array( $this, 'rest_api_init' ), 11 );
53
54
		$this->domain = ( new Status() )->get_site_suffix();
55
	}
56
57
	/**
58
	 * Returns class instance.
59
	 *
60
	 * @return Admin_Menu
61
	 */
62
	public static function get_instance() {
63
		$class = get_called_class();
64
65
		if ( empty( static::$instances[ $class ] ) ) {
66
			static::$instances[ $class ] = new $class();
67
		}
68
69
		return static::$instances[ $class ];
70
	}
71
72
	/**
73
	 * Sets up class properties for REST API requests.
74
	 *
75
	 * @param WP_REST_Response $response Response from the endpoint.
76
	 */
77
	public function rest_api_init( $response ) {
78
		$this->is_api_request = true;
79
80
		return $response;
81
	}
82
83
	/**
84
	 * Create the desired menu output.
85
	 */
86
	public function reregister_menu_items() {
87
		// Constant is not defined until parse_request.
88
		if ( ! $this->is_api_request ) {
89
			$this->is_api_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
90
		}
91
92
		/*
93
		 * Whether links should point to Calypso or wp-admin.
94
		 *
95
		 * Options:
96
		 * false - Calypso (Default).
97
		 * true  - wp-admin.
98
		 */
99
		$wp_admin = $this->should_link_to_wp_admin();
100
101
		// Remove separators.
102
		remove_menu_page( 'separator1' );
103
104
		$this->add_stats_menu();
105
		$this->add_upgrades_menu();
106
		$this->add_posts_menu( $wp_admin );
107
		$this->add_media_menu( $wp_admin );
108
		$this->add_page_menu( $wp_admin );
109
		$this->add_testimonials_menu( $wp_admin );
110
		$this->add_portfolio_menu( $wp_admin );
111
		$this->add_comments_menu( $wp_admin );
112
		$this->add_appearance_menu( $wp_admin );
113
		$this->add_plugins_menu( $wp_admin );
114
		$this->add_users_menu( $wp_admin );
115
116
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
117
		$wp_admin_import = $wp_admin;
118
		$wp_admin_export = $wp_admin;
119
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
120
121
		$this->add_options_menu( $wp_admin );
122
		$this->add_jetpack_menu();
123
124
		ksort( $GLOBALS['menu'] );
125
	}
126
127
	/**
128
	 * Adds My Home menu.
129
	 *
130
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
131
	 */
132
	public function add_my_home_menu( $wp_admin = false ) {
133
		global $submenu;
134
135
		$menu_slug = $wp_admin ? 'index.php' : 'https://wordpress.com/home/' . $this->domain;
136
		$cap       = $wp_admin ? 'read' : 'manage_options'; // Calypso's My Home is only available for admins.
137
138
		remove_menu_page( 'index.php' );
139
		remove_submenu_page( 'index.php', 'index.php' );
140
141
		add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 'dashicons-admin-home', 2 );
142
143
		// Only add submenu when there are other submenu items.
144 View Code Duplication
		if ( ! empty( $submenu['index.php'] ) ) {
145
			add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), $cap, $menu_slug, null, 1 );
146
		}
147
148
		$this->migrate_submenus( 'index.php', $menu_slug );
149
		add_filter(
150
			'parent_file',
151
			function ( $parent_file ) use ( $menu_slug ) {
152
				return 'index.php' === $parent_file ? $menu_slug : $parent_file;
153
			}
154
		);
155
	}
156
157
	/**
158
	 * Adds Stats menu.
159
	 */
160
	public function add_stats_menu() {
161
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
162
	}
163
164
	/**
165
	 * Adds Upgrades menu.
166
	 */
167
	public function add_upgrades_menu() {
168
		remove_menu_page( 'paid-upgrades.php' );
169
170
		$menu_slug = 'https://wordpress.com/plans/' . $this->domain;
171
172
		add_menu_page( __( 'Upgrades', 'jetpack' ), __( 'Upgrades', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-cart', 4 );
173
		add_submenu_page( $menu_slug, __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', $menu_slug, null, 5 );
174
		add_submenu_page( $menu_slug, __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 15 );
175
176
		$this->migrate_submenus( 'paid-upgrades.php', $menu_slug );
177
		add_filter(
178
			'parent_file',
179
			function ( $parent_file ) use ( $menu_slug ) {
180
				return 'paid-upgrades.php' === $parent_file ? $menu_slug : $parent_file;
181
			}
182
		);
183
	}
184
185
	/**
186
	 * Adds Posts menu.
187
	 *
188
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
189
	 */
190 View Code Duplication
	public function add_posts_menu( $wp_admin = false ) {
191
		if ( $wp_admin ) {
192
			return;
193
		}
194
195
		$ptype_obj = get_post_type_object( 'post' );
196
		$menu_slug = 'https://wordpress.com/posts/' . $this->domain;
197
198
		remove_menu_page( 'edit.php' );
199
		remove_submenu_page( 'edit.php', 'edit.php' );
200
		remove_submenu_page( 'edit.php', 'post-new.php' );
201
202
		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 );
203
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
204
		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 );
205
206
		$this->migrate_submenus( 'edit.php', $menu_slug );
207
		add_filter(
208
			'parent_file',
209
			function ( $parent_file ) use ( $menu_slug ) {
210
				return 'edit.php' === $parent_file ? $menu_slug : $parent_file;
211
			}
212
		);
213
	}
214
215
	/**
216
	 * Adds Media menu.
217
	 *
218
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
219
	 */
220
	public function add_media_menu( $wp_admin = false ) {
221
		remove_submenu_page( 'upload.php', 'upload.php' );
222
		remove_submenu_page( 'upload.php', 'media-new.php' );
223
224
		if ( ! $wp_admin ) {
225
			$menu_slug = 'https://wordpress.com/media/' . $this->domain;
226
227
			remove_menu_page( 'upload.php' );
228
			add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 );
229
			$this->migrate_submenus( 'upload.php', $menu_slug );
230
231
			add_filter(
232
				'parent_file',
233
				function ( $parent_file ) use ( $menu_slug ) {
234
					return 'upload.php' === $parent_file ? $menu_slug : $parent_file;
235
				}
236
			);
237
		}
238
	}
239
240
	/**
241
	 * Adds Page menu.
242
	 *
243
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
244
	 */
245 View Code Duplication
	public function add_page_menu( $wp_admin = false ) {
246
		if ( $wp_admin ) {
247
			return;
248
		}
249
250
		$ptype_obj = get_post_type_object( 'page' );
251
		$menu_slug = 'https://wordpress.com/pages/' . $this->domain;
252
253
		remove_menu_page( 'edit.php?post_type=page' );
254
		remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
255
		remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
256
257
		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 );
258
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
259
		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 );
260
261
		$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
262
		add_filter(
263
			'parent_file',
264
			function ( $parent_file ) use ( $menu_slug ) {
265
				return 'edit.php?post_type=page' === $parent_file ? $menu_slug : $parent_file;
266
			}
267
		);
268
	}
269
270
	/**
271
	 * Adds Testimonials menu.
272
	 *
273
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
274
	 */
275
	public function add_testimonials_menu( $wp_admin = false ) {
276
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
277
	}
278
279
	/**
280
	 * Adds Portfolio 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_portfolio_menu( $wp_admin = false ) {
285
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
286
	}
287
288
	/**
289
	 * Adds a custom post type menu.
290
	 *
291
	 * @param string $post_type Custom post type.
292
	 * @param bool   $wp_admin  Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
293
	 */
294
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
295
		if ( $wp_admin ) {
296
			return;
297
		}
298
299
		$ptype_obj = get_post_type_object( $post_type );
300
		if ( empty( $ptype_obj ) ) {
301
			return;
302
		}
303
304
		$cpt_slug  = 'edit.php?post_type=' . $post_type;
305
		$menu_slug = 'https://wordpress.com/types/' . $post_type . '/' . $this->domain;
306
307
		remove_menu_page( $cpt_slug );
308
		remove_submenu_page( $cpt_slug, $cpt_slug );
309
		remove_submenu_page( $cpt_slug, 'post-new.php?post_type=' . $post_type );
310
311
		// Menu icon.
312
		$menu_icon = 'dashicons-admin-post';
313
		if ( is_string( $ptype_obj->menu_icon ) ) {
314
			// Special handling for data:image/svg+xml and Dashicons.
315
			if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
316
				$menu_icon = $ptype_obj->menu_icon;
317
			} else {
318
				$menu_icon = esc_url( $ptype_obj->menu_icon );
319
			}
320
		}
321
322
		/*
323
		 * Menu position.
324
		 *
325
		 * If $ptype_menu_position is already populated or will be populated
326
		 * by a hard-coded value below, increment the position.
327
		 */
328
		$ptype_menu_position = is_int( $ptype_obj->menu_position ) ? $ptype_obj->menu_position : ++$GLOBALS['_wp_last_object_menu'];
329
		$core_menu_positions = array( 59, 60, 65, 70, 75, 80, 85, 99 );
330
		while ( isset( $GLOBALS['menu'][ $ptype_menu_position ] ) || in_array( $ptype_menu_position, $core_menu_positions, true ) ) {
331
			$ptype_menu_position++;
332
		}
333
334
		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 );
335
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
336
		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 );
337
		$this->migrate_submenus( $cpt_slug, $menu_slug );
338
339
		add_filter(
340
			'parent_file',
341
			function ( $parent_file ) use ( $cpt_slug, $menu_slug ) {
342
				return $cpt_slug === $parent_file ? $menu_slug : $parent_file;
343
			}
344
		);
345
	}
346
347
	/**
348
	 * Adds Comments menu.
349
	 *
350
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
351
	 */
352
	public function add_comments_menu( $wp_admin = false ) {
353
		if ( $wp_admin || ! current_user_can( 'edit_posts' ) ) {
354
			return;
355
		}
356
357
		$awaiting_mod      = wp_count_comments();
358
		$awaiting_mod      = $awaiting_mod->moderated;
359
		$awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
360
		/* translators: %s: Number of comments. */
361
		$awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n );
362
363
		/* translators: %s: Number of comments. */
364
		$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>' );
365
		$menu_slug  = 'https://wordpress.com/comments/all/' . $this->domain;
366
367
		remove_menu_page( 'edit-comments.php' );
368
		remove_submenu_page( 'edit-comments.php', 'edit-comments.php' );
369
370
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 );
371
372
		$this->migrate_submenus( 'edit-comments.php', $menu_slug );
373
		add_filter(
374
			'parent_file',
375
			function ( $parent_file ) use ( $menu_slug ) {
376
				return 'edit-comments.php' === $parent_file ? $menu_slug : $parent_file;
377
			}
378
		);
379
	}
380
381
	/**
382
	 * Adds Appearance menu.
383
	 *
384
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
385
	 */
386
	public function add_appearance_menu( $wp_admin = false ) {
387
		$user_can_customize = current_user_can( 'customize' );
388
		$appearance_cap     = current_user_can( 'switch_themes' ) ? 'switch_themes' : 'edit_theme_options';
389
		$themes_slug        = $wp_admin ? 'themes.php' : 'https://wordpress.com/themes/' . $this->domain;
390
		$customize_url      = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore
391
		remove_menu_page( 'themes.php' );
392
		remove_submenu_page( 'themes.php', 'themes.php' );
393
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
394
		remove_submenu_page( 'themes.php', $customize_url );
395
		remove_submenu_page( 'themes.php', 'custom-header' );
396
		remove_submenu_page( 'themes.php', 'custom-background' );
397
398
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 );
399
		add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 0 );
400
		add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $this->customize_slug, null, 1 );
401
402
		// Maintain id as JS selector.
403
		$GLOBALS['menu'][60][5] = 'menu-appearance'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
404
405
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
406
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
407
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
408
409
			// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
410
			$customize_header_url = admin_url( 'themes.php?page=custom-header' );
411
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
412
413
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $this->customize_slug );
414
			add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 );
415
		}
416
417
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
418
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
419
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
420
421
			// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
422
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
423
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
424
425
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $this->customize_slug );
426
			add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 );
427
		}
428
429 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
430
			remove_submenu_page( 'themes.php', 'widgets.php' );
431
432
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $this->customize_slug );
433
			add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_menu_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_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $this->customize_slug );
440
			add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menu_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
		remove_submenu_page( 'plugins.php', 'plugins.php' );
465
		remove_submenu_page( 'plugins.php', 'plugin-install.php' );
466
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
467
468
		$count = '';
469
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
470
			$update_data = wp_get_update_data();
471
			$count       = sprintf(
472
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
473
				$update_data['counts']['plugins'],
474
				number_format_i18n( $update_data['counts']['plugins'] )
475
			);
476
		}
477
478
		/* translators: %s: Number of pending plugin updates. */
479
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', $menu_slug, null, 'dashicons-admin-plugins', 65 );
480
481
		$this->migrate_submenus( 'plugins.php', $menu_slug );
482
		add_filter(
483
			'parent_file',
484
			function ( $parent_file ) use ( $menu_slug ) {
485
				return 'jetpack' === $parent_file ? $menu_slug : $parent_file;
486
			}
487
		);
488
	}
489
490
	/**
491
	 * Adds Users menu.
492
	 *
493
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
494
	 */
495
	public function add_users_menu( $wp_admin = false ) {
496
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
497
		$add_new_slug = 'https://wordpress.com/people/new/' . $this->domain;
498
		$profile_slug = $wp_admin ? 'profile.php' : 'https://wordpress.com/me';
499
500
		if ( current_user_can( 'list_users' ) ) {
501
			remove_menu_page( 'users.php' );
502
			remove_submenu_page( 'users.php', 'users.php' );
503
			remove_submenu_page( 'users.php', 'user-new.php' );
504
			remove_submenu_page( 'users.php', 'profile.php' );
505
			remove_submenu_page( 'users.php', 'grofiles-editor' );
506
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
507
508
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
509
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
510
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
511
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
512
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account', null, 20 );
513
514
			$this->migrate_submenus( 'users.php', $users_slug );
515
			add_filter(
516
				'parent_file',
517
				function ( $parent_file ) use ( $users_slug ) {
518
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
519
				}
520
			);
521
		}
522
	}
523
524
	/**
525
	 * Adds Tools menu.
526
	 *
527
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
528
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
529
	 */
530
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
531
		$admin_slug = 'tools.php';
532
		$menu_slug  = 'https://wordpress.com/marketing/tools/' . $this->domain;
533
534
		remove_menu_page( $admin_slug );
535
		remove_submenu_page( $admin_slug, $admin_slug );
536
		remove_submenu_page( $admin_slug, 'delete-blog' );
537
		remove_submenu_page( $admin_slug, 'import.php' );
538
		remove_submenu_page( $admin_slug, 'export.php' );
539
540
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'publish_posts', $menu_slug, null, 'dashicons-admin-tools', 75 );
541
		add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', $menu_slug );
542
		add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain );
543
		add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', $wp_admin_import ? 'import.php' : 'https://wordpress.com/import/' . $this->domain );
544
		add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', $wp_admin_export ? 'export.php' : 'https://wordpress.com/export/' . $this->domain );
545
546
		$this->migrate_submenus( $admin_slug, $menu_slug );
547
548
		add_submenu_page( $menu_slug, esc_attr__( 'Other tools', 'jetpack' ), __( 'Other tools', 'jetpack' ), 'manage_options', 'tools.php' );
549
550
		add_filter(
551
			'parent_file',
552
			function ( $parent_file ) use ( $menu_slug ) {
553
				return 'tools.php' === $parent_file ? $menu_slug : $parent_file;
554
			}
555
		);
556
	}
557
558
	/**
559
	 * Adds Settings menu.
560
	 *
561
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
562
	 */
563
	public function add_options_menu( $wp_admin = false ) {
564
		if ( $wp_admin ) {
565
			return;
566
		}
567
568
		$options_slug = 'https://wordpress.com/settings/general/' . $this->domain;
569
570
		remove_menu_page( 'options-general.php' );
571
		remove_submenu_page( 'options-general.php', 'options-general.php' );
572
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
573
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
574
575
		add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', $options_slug, null, 'dashicons-admin-settings', 80 );
576
		add_submenu_page( $options_slug, esc_attr__( 'General', 'jetpack' ), __( 'General', 'jetpack' ), 'manage_options', $options_slug, null, 10 );
577
578 View Code Duplication
		if ( isset( $wp_settings_fields['general']['default'] ) || isset( $wp_settings_sections['general'] ) ) {
0 ignored issues
show
Bug introduced by
The variable $wp_settings_fields seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Bug introduced by
The variable $wp_settings_sections seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
579
			add_submenu_page( $options_slug, esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php', null, 11 );
580
		}
581
		
582 View Code Duplication
		if ( isset( $wp_settings_fields['writing']['default'] ) || isset( $wp_settings_sections['writing'] ) ) {
583
			add_submenu_page( $options_slug, esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php', null, 13 );
584
		}
585
586
		$this->migrate_submenus( 'options-general.php', $options_slug );
587
		add_filter(
588
			'parent_file',
589
			function ( $parent_file ) use ( $options_slug ) {
590
				return 'options-general.php' === $parent_file ? $options_slug : $parent_file;
591
			}
592
		);
593
	}
594
595
	/**
596
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
597
	 *
598
	 * @param string $old_slug WP-Admin menu slug.
599
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
600
	 */
601
	public function migrate_submenus( $old_slug, $new_slug ) {
602
		global $submenu;
603
604
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
605
			if ( ! empty( $submenu[ $new_slug ] ) ) {
606
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
607
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
608
			} else {
609
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
610
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
611
			}
612
			unset( $submenu[ $old_slug ] );
613
		}
614
	}
615
616
	/**
617
	 * Adds Jetpack menu.
618
	 */
619
	public function add_jetpack_menu() {
620
		global $menu;
621
622
		$position = 50;
623
		while ( isset( $menu[ $position ] ) ) {
624
			$position++;
625
		}
626
627
		// TODO: Replace with proper SVG data url.
628
		$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";
629
		$jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain;
630
631
		$this->add_admin_menu_separator( $position++, 'manage_options' );
632
		add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position );
633
634
		// Maintain id for jQuery selector.
635
		$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
636
637
		remove_menu_page( 'jetpack' );
638
		remove_submenu_page( 'jetpack', 'stats' );
639
640
		$this->migrate_submenus( 'jetpack', $jetpack_slug );
641
642
		add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', $jetpack_slug, null, 0 );
643
		add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 1 );
644
		/* translators: Jetpack sidebar menu item. */
645
		add_submenu_page( $jetpack_slug, esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 2 );
646
647
		add_filter(
648
			'parent_file',
649
			function ( $parent_file ) use ( $jetpack_slug ) {
650
				return 'jetpack' === $parent_file ? $jetpack_slug : $parent_file;
651
			}
652
		);
653
	}
654
655
	/**
656
	 * Adds a menu separator.
657
	 *
658
	 * @param int    $position The position in the menu order this item should appear.
659
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
660
	 *                         Default: 'read'.
661
	 */
662
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
663
		global $menu;
664
665
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
666
		$menu[ $position ] = array(
667
			'',                                  // Menu title (ignored).
668
			$cap,                                // Required capability.
669
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
670
			'',                                  // Page title (ignored).
671
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
672
		);
673
		ksort( $menu );
674
	}
675
676
	/**
677
	 * Enqueues scripts and styles.
678
	 */
679
	public function enqueue_scripts() {
680
		$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...
681
		$rtl                = is_rtl() ? '-rtl' : '';
682 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
683
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
684
		} else {
685
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
686
		}
687
		wp_enqueue_style(
688
			'jetpack-admin-menu',
689
			plugins_url( 'admin-menu.css', __FILE__ ),
690
			$style_dependencies,
691
			JETPACK__VERSION
692
		);
693
		wp_enqueue_script(
694
			'jetpack-admin-menu',
695
			plugins_url( 'admin-menu.js', __FILE__ ),
696
			array(),
697
			JETPACK__VERSION,
698
			true
699
		);
700
	}
701
702
	/**
703
	 * Dequeues unnecessary scripts.
704
	 */
705
	public function dequeue_scripts() {
706
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
707
	}
708
709
	/**
710
	 * Whether to use wp-admin pages rather than Calypso.
711
	 *
712
	 * @return bool
713
	 */
714
	public function should_link_to_wp_admin() {
715
		return get_user_option( 'jetpack_admin_menu_link_destination' );
716
	}
717
}
718