Completed
Push — kraftbj-patch-2 ( f1a4bb...ba1272 )
by
unknown
25:04 queued 15:38
created

Admin_Menu::add_appearance_menu()   F

Complexity

Conditions 15
Paths 432

Size

Total Lines 74

Duplication

Lines 13
Ratio 17.57 %

Importance

Changes 0
Metric Value
cc 15
nc 432
nop 2
dl 13
loc 74
rs 2.5983
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		if ( ! $wp_admin_customize ) {
390
			$customize_slug = 'https://wordpress.com/customize/' . $this->domain;
391
		} else {
392
			// In case this is an api request we will have to add the 'return' querystring via JS.
393
			$customize_slug = $this->is_api_request ? 'customize.php' : add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' );
394
		}
395
		remove_menu_page( 'themes.php' );
396
		remove_submenu_page( 'themes.php', 'themes.php' );
397
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
398
		remove_submenu_page( 'themes.php', add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ) );
399
		remove_submenu_page( 'themes.php', 'custom-header' );
400
		remove_submenu_page( 'themes.php', 'custom-background' );
401
402
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $themes_slug, null, 'dashicons-admin-appearance', 60 );
403
		add_submenu_page( $themes_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 0 );
404
		add_submenu_page( $themes_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 1 );
405
406
		// Maintain id as JS selector.
407
		$GLOBALS['menu'][60][5] = 'menu-appearance'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
408
409
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
410
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug );
411
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
412
413
			// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
414
			$customize_header_url = admin_url( 'themes.php?page=custom-header' );
415
			remove_submenu_page( 'themes.php', esc_url( $customize_header_url ) );
416
417
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_slug );
418
			add_submenu_page( $themes_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), 'customize', esc_url( $customize_header_url ), null, 15 );
419
		}
420
421
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
422
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_slug );
423
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
424
425
			// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
426
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
427
			remove_submenu_page( 'themes.php', esc_url( $customize_background_url ) );
428
429
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_slug );
430
			add_submenu_page( $themes_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), 'customize', esc_url( $customize_background_url ), null, 20 );
431
		}
432
433 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
434
			remove_submenu_page( 'themes.php', 'widgets.php' );
435
			remove_submenu_page( 'themes.php', 'gutenberg-widgets' );
436
437
			$customize_widgets_url = $wp_admin_customize ? 'widgets.php' : add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_slug );
438
			add_submenu_page( $themes_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), 'customize', esc_url( $customize_widgets_url ), null, 20 );
439
		}
440
441 View Code Duplication
		if ( current_theme_supports( 'menus' ) || current_theme_supports( 'widgets' ) ) {
442
			remove_submenu_page( 'themes.php', 'nav-menus.php' );
443
444
			$customize_menus_url = $wp_admin_customize ? 'nav-menus.php' : add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_slug );
445
			add_submenu_page( $themes_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), 'customize', esc_url( $customize_menus_url ), null, 20 );
446
		}
447
448
		// Register menu for the Custom CSS Jetpack module, but don't add it as a menu item.
449
		$GLOBALS['_registered_pages']['admin_page_editcss'] = true; // phpcs:ignore
450
451
		$this->migrate_submenus( 'themes.php', $themes_slug );
452
		add_filter(
453
			'parent_file',
454
			function ( $parent_file ) use ( $themes_slug ) {
455
				return 'themes.php' === $parent_file ? $themes_slug : $parent_file;
456
			}
457
		);
458
	}
459
460
	/**
461
	 * Adds Plugins menu.
462
	 *
463
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
464
	 */
465
	public function add_plugins_menu( $wp_admin = false ) {
466
		$menu_slug = $wp_admin ? 'plugins.php' : 'https://wordpress.com/plugins/' . $this->domain;
467
468
		remove_menu_page( 'plugins.php' );
469
470
		// Keep submenus when links point to WP Admin.
471
		if ( ! $wp_admin ) {
472
			remove_submenu_page( 'plugins.php', 'plugins.php' );
473
			remove_submenu_page( 'plugins.php', 'plugin-install.php' );
474
			remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
475
		}
476
477
		$count = '';
478
		if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
479
			$update_data = wp_get_update_data();
480
			$count       = sprintf(
481
				'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
482
				$update_data['counts']['plugins'],
483
				number_format_i18n( $update_data['counts']['plugins'] )
484
			);
485
		}
486
487
		/* translators: %s: Number of pending plugin updates. */
488
		add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', $menu_slug, null, 'dashicons-admin-plugins', 65 );
489
490
		$this->migrate_submenus( 'plugins.php', $menu_slug );
491
		add_filter(
492
			'parent_file',
493
			function ( $parent_file ) use ( $menu_slug ) {
494
				return 'jetpack' === $parent_file ? $menu_slug : $parent_file;
495
			}
496
		);
497
	}
498
499
	/**
500
	 * Adds Users menu.
501
	 *
502
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
503
	 */
504
	public function add_users_menu( $wp_admin = false ) {
505
		$users_slug   = $wp_admin ? 'users.php' : 'https://wordpress.com/people/team/' . $this->domain;
506
		$add_new_slug = $wp_admin ? 'user-new.php' : 'https://wordpress.com/people/new/' . $this->domain;
507
		$profile_slug = $wp_admin ? 'profile.php' : 'https://wordpress.com/me';
508
		$account_slug = 'https://wordpress.com/me/account';
509
510
		if ( current_user_can( 'list_users' ) ) {
511
			remove_menu_page( 'users.php' );
512
			remove_submenu_page( 'users.php', 'users.php' );
513
			remove_submenu_page( 'users.php', 'user-new.php' );
514
			remove_submenu_page( 'users.php', 'profile.php' );
515
			remove_submenu_page( 'users.php', 'grofiles-editor' );
516
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
517
518
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
519
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug );
520
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug );
521
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug );
522
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug );
523
524
			$this->migrate_submenus( 'users.php', $users_slug );
525
			add_filter(
526
				'parent_file',
527
				function ( $parent_file ) use ( $users_slug ) {
528
					return 'users.php' === $parent_file ? $users_slug : $parent_file;
529
				}
530
			);
531
		} else {
532
			remove_menu_page( 'profile.php' );
533
			remove_submenu_page( 'profile.php', 'grofiles-editor' );
534
			remove_submenu_page( 'profile.php', 'grofiles-user-settings' );
535
536
			add_menu_page( esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 'dashicons-admin-users', 70 );
537
			add_submenu_page( $profile_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 5 );
538
539
			$this->migrate_submenus( 'profile.php', $profile_slug );
540
			add_filter(
541
				'parent_file',
542
				function ( $parent_file ) use ( $profile_slug ) {
543
					return 'profile.php' === $parent_file ? $profile_slug : $parent_file;
544
				}
545
			);
546
		}
547
	}
548
549
	/**
550
	 * Adds Tools menu.
551
	 *
552
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
553
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
554
	 */
555
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
556
		$admin_slug = 'tools.php';
557
		$menu_slug  = 'https://wordpress.com/marketing/tools/' . $this->domain;
558
559
		remove_menu_page( $admin_slug );
560
		remove_submenu_page( $admin_slug, $admin_slug );
561
		remove_submenu_page( $admin_slug, 'delete-blog' );
562
		remove_submenu_page( $admin_slug, 'import.php' );
563
		remove_submenu_page( $admin_slug, 'export.php' );
564
565
		add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'publish_posts', $menu_slug, null, 'dashicons-admin-tools', 75 );
566
		add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', $menu_slug );
567
		add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain );
568
		add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', $wp_admin_import ? 'import.php' : 'https://wordpress.com/import/' . $this->domain );
569
		add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', $wp_admin_export ? 'export.php' : 'https://wordpress.com/export/' . $this->domain );
570
571
		$this->migrate_submenus( $admin_slug, $menu_slug );
572
573
		add_submenu_page( $menu_slug, esc_attr__( 'Other tools', 'jetpack' ), __( 'Other tools', 'jetpack' ), 'manage_options', 'tools.php' );
574
575
		add_filter(
576
			'parent_file',
577
			function ( $parent_file ) use ( $menu_slug ) {
578
				return 'tools.php' === $parent_file ? $menu_slug : $parent_file;
579
			}
580
		);
581
	}
582
583
	/**
584
	 * Adds Settings menu.
585
	 *
586
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
587
	 */
588 View Code Duplication
	public function add_options_menu( $wp_admin = false ) {
589
		if ( $wp_admin ) {
590
			return;
591
		}
592
593
		$options_slug = 'https://wordpress.com/settings/general/' . $this->domain;
594
595
		remove_menu_page( 'options-general.php' );
596
		remove_submenu_page( 'options-general.php', 'options-general.php' );
597
		remove_submenu_page( 'options-general.php', 'options-discussion.php' );
598
		remove_submenu_page( 'options-general.php', 'options-writing.php' );
599
600
		add_menu_page( esc_attr__( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'manage_options', $options_slug, null, 'dashicons-admin-settings', 80 );
601
		add_submenu_page( $options_slug, esc_attr__( 'General', 'jetpack' ), __( 'General', 'jetpack' ), 'manage_options', $options_slug, null, 10 );
602
603
		$this->migrate_submenus( 'options-general.php', $options_slug );
604
		add_filter(
605
			'parent_file',
606
			function ( $parent_file ) use ( $options_slug ) {
607
				return 'options-general.php' === $parent_file ? $options_slug : $parent_file;
608
			}
609
		);
610
	}
611
612
	/**
613
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
614
	 *
615
	 * @param string $old_slug WP-Admin menu slug.
616
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
617
	 */
618
	public function migrate_submenus( $old_slug, $new_slug ) {
619
		global $submenu;
620
621
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
622
			if ( ! empty( $submenu[ $new_slug ] ) ) {
623
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
624
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
625
			} else {
626
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
627
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
628
			}
629
			unset( $submenu[ $old_slug ] );
630
		}
631
	}
632
633
	/**
634
	 * Remove submenu items from given menu slug.
635
	 *
636
	 * @param string $slug Menu slug.
637
	 */
638
	public function remove_submenus( $slug ) {
639
		global $submenu;
640
641
		if ( isset( $submenu[ $slug ] ) ) {
642
			// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
643
			$submenu[ $slug ] = array();
644
		}
645
	}
646
647
	/**
648
	 * Adds Jetpack menu.
649
	 */
650
	public function add_jetpack_menu() {
651
		global $menu;
652
653
		$position = 50;
654
		while ( isset( $menu[ $position ] ) ) {
655
			$position++;
656
		}
657
658
		// TODO: Replace with proper SVG data url.
659
		$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";
660
		$jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain;
661
662
		$this->add_admin_menu_separator( $position++, 'manage_options' );
663
		add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position );
664
665
		// Maintain id for jQuery selector.
666
		$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
667
668
		remove_menu_page( 'jetpack' );
669
		remove_submenu_page( 'jetpack', 'stats' );
670
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
671
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
672
673
		$this->migrate_submenus( 'jetpack', $jetpack_slug );
674
675
		add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', $jetpack_slug, null, 2 );
676
		add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
677
		/* translators: Jetpack sidebar menu item. */
678
		add_submenu_page( $jetpack_slug, esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'read', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
679
680
		add_filter(
681
			'parent_file',
682
			function ( $parent_file ) use ( $jetpack_slug ) {
683
				return 'jetpack' === $parent_file ? $jetpack_slug : $parent_file;
684
			}
685
		);
686
	}
687
688
	/**
689
	 * Adds a menu separator.
690
	 *
691
	 * @param int    $position The position in the menu order this item should appear.
692
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
693
	 *                         Default: 'read'.
694
	 */
695
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
696
		global $menu;
697
698
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
699
		$menu[ $position ] = array(
700
			'',                                  // Menu title (ignored).
701
			$cap,                                // Required capability.
702
			wp_unique_id( 'separator-custom-' ), // URL or file (ignored, but must be unique).
703
			'',                                  // Page title (ignored).
704
			'wp-menu-separator',                 // CSS class. Identifies this item as a separator.
705
		);
706
		ksort( $menu );
707
	}
708
709
	/**
710
	 * Enqueues scripts and styles.
711
	 */
712
	public function enqueue_scripts() {
713
		$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...
714
		$rtl                = is_rtl() ? '-rtl' : '';
715 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
716
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
717
		} else {
718
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
719
		}
720
		wp_enqueue_style(
721
			'jetpack-admin-menu',
722
			plugins_url( 'admin-menu.css', __FILE__ ),
723
			$style_dependencies,
724
			JETPACK__VERSION
725
		);
726
		wp_enqueue_script(
727
			'jetpack-admin-menu',
728
			plugins_url( 'admin-menu.js', __FILE__ ),
729
			array(),
730
			JETPACK__VERSION,
731
			true
732
		);
733
	}
734
735
	/**
736
	 * Dequeues unnecessary scripts.
737
	 */
738
	public function dequeue_scripts() {
739
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
740
	}
741
742
	/**
743
	 * Whether to use wp-admin pages rather than Calypso.
744
	 *
745
	 * @return bool
746
	 */
747
	public function should_link_to_wp_admin() {
748
		return get_user_option( 'jetpack_admin_menu_link_destination' );
749
	}
750
}
751