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