Admin_Menu   F
last analyzed

Complexity

Total Complexity 61

Size/Duplication

Total Lines 548
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 61
lcom 1
cbo 2
dl 0
loc 548
rs 3.52
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A add_stats_menu() 0 3 1
A reregister_menu_items() 0 46 2
A should_disable_links_manager() 0 22 3
A add_my_home_menu() 0 3 1
A add_upsell_nudge() 0 20 1
A set_site_notices_menu_class() 0 17 4
B add_upgrades_menu() 0 35 6
A add_posts_menu() 0 13 2
A add_media_menu() 0 9 2
A add_page_menu() 0 11 2
A add_testimonials_menu() 0 3 1
A add_portfolio_menu() 0 3 1
A add_custom_post_type_menu() 0 11 2
A add_comments_menu() 0 7 2
B add_appearance_menu() 0 44 6
A add_plugins_menu() 0 10 2
A add_users_menu() 0 24 4
A add_tools_menu() 0 16 3
A add_options_menu() 0 19 2
A add_jetpack_menu() 0 26 3
A add_gutenberg_menus() 0 23 5
A should_link_to_wp_admin() 0 3 1
A register_dashboard_switcher() 0 22 2
A get_current_slug() 0 11 3

How to fix   Complexity   

Complex Class

Complex classes like Admin_Menu often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Admin_Menu, and based on these observations, apply Extract Interface, too.

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
12
require_once __DIR__ . '/class-base-admin-menu.php';
13
14
/**
15
 * Class Admin_Menu.
16
 */
17
class Admin_Menu extends Base_Admin_Menu {
18
19
	/**
20
	 * Create the desired menu output.
21
	 */
22
	public function reregister_menu_items() {
23
		/*
24
		 * Whether links should point to Calypso or wp-admin.
25
		 *
26
		 * Options:
27
		 * false - Calypso (Default).
28
		 * true  - wp-admin.
29
		 */
30
		$wp_admin = $this->should_link_to_wp_admin();
31
32
		// Remove separators.
33
		remove_menu_page( 'separator1' );
34
35
		$this->add_stats_menu();
36
		$this->add_upgrades_menu();
37
		$this->add_posts_menu( $wp_admin );
38
		$this->add_media_menu( $wp_admin );
39
		$this->add_page_menu( $wp_admin );
40
		$this->add_testimonials_menu( $wp_admin );
41
		$this->add_portfolio_menu( $wp_admin );
42
		$this->add_comments_menu( $wp_admin );
43
44
		// Whether Themes/Customize links should point to Calypso (false) or wp-admin (true).
45
		$wp_admin_themes    = $wp_admin;
46
		$wp_admin_customize = $wp_admin;
47
		$this->add_appearance_menu( $wp_admin_themes, $wp_admin_customize );
48
		$this->add_plugins_menu( $wp_admin );
49
		$this->add_users_menu( $wp_admin );
50
51
		// Whether Import/Export links should point to Calypso (false) or wp-admin (true).
52
		$wp_admin_import = $wp_admin;
53
		$wp_admin_export = $wp_admin;
54
		$this->add_tools_menu( $wp_admin_import, $wp_admin_export );
55
56
		$this->add_options_menu( $wp_admin );
57
		$this->add_jetpack_menu();
58
		$this->add_gutenberg_menus( $wp_admin );
59
60
		// Remove Links Manager menu since its usage is discouraged. https://github.com/Automattic/wp-calypso/issues/51188.
61
		// @see https://core.trac.wordpress.org/ticket/21307#comment:73.
62
		if ( $this->should_disable_links_manager() ) {
63
			remove_menu_page( 'link-manager.php' );
64
		}
65
66
		ksort( $GLOBALS['menu'] );
67
	}
68
69
	/**
70
	 * Check if Links Manager is being used.
71
	 */
72
	public function should_disable_links_manager() {
73
		// The max ID number of the auto-generated links.
74
		// See /wp-content/mu-plugins/wpcom-wp-install-defaults.php in WP.com.
75
		$max_default_id = 10;
76
77
		// We are only checking the latest entry link_id so are limiting the query to 1.
78
		$link_manager_links = get_bookmarks(
79
			array(
80
				'orderby'        => 'link_id',
81
				'order'          => 'DESC',
82
				'limit'          => 1,
83
				'hide_invisible' => 0,
84
			)
85
		);
86
87
		// Ordered links by ID descending, check if the first ID is more than $max_default_id.
88
		if ( count( $link_manager_links ) > 0 && $link_manager_links[0]->link_id > $max_default_id ) {
89
			return false;
90
		}
91
92
		return true;
93
	}
94
95
	/**
96
	 * Adds My Home menu.
97
	 */
98
	public function add_my_home_menu() {
99
		$this->update_menu( 'index.php', 'https://wordpress.com/home/' . $this->domain, __( 'My Home', 'jetpack' ), 'manage_options', 'dashicons-admin-home' );
100
	}
101
102
	/**
103
	 * Adds upsell nudge as a menu.
104
	 *
105
	 * @param object $nudge The $nudge object containing the content, CTA, link and tracks.
106
	 */
107
	public function add_upsell_nudge( $nudge ) {
108
		$message = '
109
<div class="upsell_banner">
110
	<div class="banner__info">
111
		<div class="banner__title">%1$s</div>
112
	</div>
113
	<div class="banner__action">
114
		<button type="button" class="button">%2$s</button>
115
	</div>
116
</div>';
117
118
		$message = sprintf(
119
			$message,
120
			wp_kses( $nudge['content'], array() ),
121
			wp_kses( $nudge['cta'], array() )
122
		);
123
124
		add_menu_page( 'site-notices', $message, 'read', 'https://wordpress.com' . $nudge['link'], null, null, 1 );
125
		add_filter( 'add_menu_classes', array( $this, 'set_site_notices_menu_class' ) );
126
	}
127
128
	/**
129
	 * Adds a custom element class and id for Site Notices's menu item.
130
	 *
131
	 * @param array $menu Associative array of administration menu items.
132
	 * @return array
133
	 */
134
	public function set_site_notices_menu_class( array $menu ) {
135
		foreach ( $menu as $key => $menu_item ) {
136
			if ( 'site-notices' !== $menu_item[3] ) {
137
				continue;
138
			}
139
140
			$classes = ' toplevel_page_site-notices';
141
142
			if ( isset( $menu_item[4] ) ) {
143
				$menu[ $key ][4] = $menu_item[4] . $classes;
144
				$menu[ $key ][5] = 'toplevel_page_site-notices';
145
				break;
146
			}
147
		}
148
149
		return $menu;
150
	}
151
152
	/**
153
	 * Adds Stats menu.
154
	 */
155
	public function add_stats_menu() {
156
		add_menu_page( __( 'Stats', 'jetpack' ), __( 'Stats', 'jetpack' ), 'view_stats', 'https://wordpress.com/stats/day/' . $this->domain, null, 'dashicons-chart-bar', 3 );
157
	}
158
159
	/**
160
	 * Adds Upgrades menu.
161
	 *
162
	 * @param string $plan The current WPCOM plan of the blog.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $plan not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
163
	 */
164
	public function add_upgrades_menu( $plan = null ) {
165
		global $menu;
166
167
		$menu_exists = false;
168
		foreach ( $menu as $item ) {
169
			if ( 'paid-upgrades.php' === $item[2] ) {
170
				$menu_exists = true;
171
				break;
172
			}
173
		}
174
175
		if ( ! $menu_exists ) {
176
			if ( $plan ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plan of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
177
				// Add display:none as a default for cases when CSS is not loaded.
178
				$site_upgrades = '%1$s<span class="inline-text" style="display:none">%2$s</span>';
179
				$site_upgrades = sprintf(
180
					$site_upgrades,
181
					__( 'Upgrades', 'jetpack' ),
182
					$plan
183
				);
184
			} else {
185
				$site_upgrades = __( 'Upgrades', 'jetpack' );
186
			}
187
188
			add_menu_page( __( 'Upgrades', 'jetpack' ), $site_upgrades, 'manage_options', 'paid-upgrades.php', null, 'dashicons-cart', 4 );
189
		}
190
191
		add_submenu_page( 'paid-upgrades.php', __( 'Plans', 'jetpack' ), __( 'Plans', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/my-plan/' . $this->domain, null, 1 );
192
		add_submenu_page( 'paid-upgrades.php', __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/purchases/subscriptions/' . $this->domain, null, 2 );
193
194
		if ( ! $menu_exists ) {
195
			// Remove the submenu auto-created by Core.
196
			$this->hide_submenu_page( 'paid-upgrades.php', 'paid-upgrades.php' );
197
		}
198
	}
199
200
	/**
201
	 * Adds Posts menu.
202
	 *
203
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
204
	 */
205
	public function add_posts_menu( $wp_admin = false ) {
206
		if ( $wp_admin ) {
207
			return;
208
		}
209
210
		$submenus_to_update = array(
211
			'edit.php'                        => 'https://wordpress.com/posts/' . $this->domain,
212
			'post-new.php'                    => 'https://wordpress.com/post/' . $this->domain,
213
			'edit-tags.php?taxonomy=category' => 'https://wordpress.com/settings/taxonomies/category/' . $this->domain,
214
			'edit-tags.php?taxonomy=post_tag' => 'https://wordpress.com/settings/taxonomies/post_tag/' . $this->domain,
215
		);
216
		$this->update_submenus( 'edit.php', $submenus_to_update );
217
	}
218
219
	/**
220
	 * Adds Media menu.
221
	 *
222
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
223
	 */
224
	public function add_media_menu( $wp_admin = false ) {
225
		if ( $wp_admin ) {
226
			return;
227
		}
228
229
		$this->hide_submenu_page( 'upload.php', 'media-new.php' );
230
231
		$this->update_menu( 'upload.php', 'https://wordpress.com/media/' . $this->domain );
232
	}
233
234
	/**
235
	 * Adds Page 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_page_menu( $wp_admin = false ) {
240
		if ( $wp_admin ) {
241
			return;
242
		}
243
244
		$submenus_to_update = array(
245
			'edit.php?post_type=page'     => 'https://wordpress.com/pages/' . $this->domain,
246
			'post-new.php?post_type=page' => 'https://wordpress.com/page/' . $this->domain,
247
		);
248
		$this->update_submenus( 'edit.php?post_type=page', $submenus_to_update );
249
	}
250
251
	/**
252
	 * Adds Testimonials menu.
253
	 *
254
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
255
	 */
256
	public function add_testimonials_menu( $wp_admin = false ) {
257
		$this->add_custom_post_type_menu( 'jetpack-testimonial', $wp_admin );
258
	}
259
260
	/**
261
	 * Adds Portfolio menu.
262
	 *
263
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
264
	 */
265
	public function add_portfolio_menu( $wp_admin = false ) {
266
		$this->add_custom_post_type_menu( 'jetpack-portfolio', $wp_admin );
267
	}
268
269
	/**
270
	 * Adds a custom post type menu.
271
	 *
272
	 * @param string $post_type Custom post type.
273
	 * @param bool   $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
274
	 */
275
	public function add_custom_post_type_menu( $post_type, $wp_admin = false ) {
276
		if ( $wp_admin ) {
277
			return;
278
		}
279
280
		$submenus_to_update = array(
281
			'edit.php?post_type=' . $post_type     => 'https://wordpress.com/types/' . $post_type . '/' . $this->domain,
282
			'post-new.php?post_type=' . $post_type => 'https://wordpress.com/edit/' . $post_type . '/' . $this->domain,
283
		);
284
		$this->update_submenus( 'edit.php?post_type=' . $post_type, $submenus_to_update );
285
	}
286
287
	/**
288
	 * Adds Comments menu.
289
	 *
290
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
291
	 */
292
	public function add_comments_menu( $wp_admin = false ) {
293
		if ( $wp_admin ) {
294
			return;
295
		}
296
297
		$this->update_menu( 'edit-comments.php', 'https://wordpress.com/comments/all/' . $this->domain );
298
	}
299
300
	/**
301
	 * Adds Appearance menu.
302
	 *
303
	 * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso).
304
	 * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso).
305
	 * @return string The Customizer URL.
306
	 */
307
	public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) {
308
		$request_uri                     = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
309
		$default_customize_slug          = add_query_arg( 'return', rawurlencode( remove_query_arg( wp_removable_query_args(), $request_uri ) ), 'customize.php' );
310
		$default_customize_header_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $default_customize_slug );
311
		// TODO: Remove WPCom_Theme_Customizer::modify_header_menu_links() and WPcom_Custom_Header::modify_admin_menu_links().
312
		$default_customize_header_slug_2     = admin_url( 'themes.php?page=custom-header' );
313
		$default_customize_background_slug_1 = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $default_customize_slug );
314
		// TODO: Remove Colors_Manager::modify_header_menu_links() and Colors_Manager_Common::modify_header_menu_links().
315
		$default_customize_background_slug_2 = add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), admin_url( 'customize.php' ) );
316
317
		if ( ! $wp_admin_customize ) {
318
			$customize_url = 'https://wordpress.com/customize/' . $this->domain;
319
		} elseif ( $this->is_api_request ) {
320
			// In case this is an api request we will have to add the 'return' querystring via JS.
321
			$customize_url = 'customize.php';
322
		} else {
323
			$customize_url = $default_customize_slug;
324
		}
325
326
		$submenus_to_update = array(
327
			$default_customize_slug              => $customize_url,
328
			$default_customize_header_slug_1     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
329
			$default_customize_header_slug_2     => add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url ),
330
			$default_customize_background_slug_1 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
331
			$default_customize_background_slug_2 => add_query_arg( array( 'autofocus' => array( 'section' => 'colors_manager_tool' ) ), $customize_url ),
332
		);
333
334
		if ( ! $wp_admin_themes ) {
335
			$submenus_to_update['themes.php'] = 'https://wordpress.com/themes/' . $this->domain;
336
		}
337
338
		if ( ! $wp_admin_customize ) {
339
			$submenus_to_update['widgets.php']       = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
340
			$submenus_to_update['gutenberg-widgets'] = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $customize_url );
341
			$submenus_to_update['nav-menus.php']     = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $customize_url );
342
		}
343
344
		$this->update_submenus( 'themes.php', $submenus_to_update );
345
346
		$this->hide_submenu_page( 'themes.php', 'custom-header' );
347
		$this->hide_submenu_page( 'themes.php', 'custom-background' );
348
349
		return $customize_url;
350
	}
351
352
	/**
353
	 * Adds Plugins menu.
354
	 *
355
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
356
	 */
357
	public function add_plugins_menu( $wp_admin = false ) {
358
		if ( $wp_admin ) {
359
			return;
360
		}
361
362
		$this->hide_submenu_page( 'plugins.php', 'plugin-install.php' );
363
		$this->hide_submenu_page( 'plugins.php', 'plugin-editor.php' );
364
365
		$this->update_menu( 'plugins.php', 'https://wordpress.com/plugins/' . $this->domain );
366
	}
367
368
	/**
369
	 * Adds Users menu.
370
	 *
371
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
372
	 */
373
	public function add_users_menu( $wp_admin = false ) {
374
		if ( current_user_can( 'list_users' ) ) {
375
			// We shall add the Calypso user management & add new user screens at all cases ( Calypso & Atomic ).
376
			$submenus_to_update = array(
377
				'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
378
				'users.php'    => 'https://wordpress.com/people/team/' . $this->domain,
379
			);
380
			if ( ! $wp_admin ) {
381
				$submenus_to_update['profile.php'] = 'https://wordpress.com/me';
382
			}
383
			$this->update_submenus( 'users.php', $submenus_to_update );
384
			add_submenu_page( 'users.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
385
		} else {
386
			if ( ! $wp_admin ) {
387
				$submenus_to_update = array(
388
					'user-new.php' => 'https://wordpress.com/people/new/' . $this->domain,
389
					'profile.php'  => 'https://wordpress.com/me',
390
				);
391
				$this->update_submenus( 'profile.php', $submenus_to_update );
392
			}
393
394
			add_submenu_page( 'profile.php', esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', 'https://wordpress.com/me/account' );
395
		}
396
	}
397
398
	/**
399
	 * Adds Tools menu.
400
	 *
401
	 * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso).
402
	 * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso).
403
	 */
404
	public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) {
405
		$submenus_to_update = array();
406
		if ( ! $wp_admin_import ) {
407
			$submenus_to_update['import.php'] = 'https://wordpress.com/import/' . $this->domain;
408
		}
409
		if ( ! $wp_admin_export ) {
410
			$submenus_to_update['export.php'] = 'https://wordpress.com/export/' . $this->domain;
411
		}
412
		$this->update_submenus( 'tools.php', $submenus_to_update );
413
414
		$this->hide_submenu_page( 'tools.php', 'tools.php' );
415
		$this->hide_submenu_page( 'tools.php', 'delete-blog' );
416
417
		add_submenu_page( 'tools.php', esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'publish_posts', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 0 );
418
		add_submenu_page( 'tools.php', esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 1 );
419
	}
420
421
	/**
422
	 * Adds Settings menu.
423
	 *
424
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
425
	 */
426
	public function add_options_menu( $wp_admin = false ) {
427
		$this->hide_submenu_page( 'options-general.php', 'sharing' );
428
429
		// There is not complete feature parity between WP Admin and Calypso settings https://github.com/Automattic/wp-calypso/issues/51189.
430
		$this->update_submenus( 'options-general.php', array( 'options-general.php' => 'https://wordpress.com/settings/general/' . $this->domain ) );
431
		add_submenu_page( 'options-general.php', esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php', null, 1 );
432
433
		add_submenu_page( 'options-general.php', esc_attr__( 'Performance', 'jetpack' ), __( 'Performance', 'jetpack' ), 'manage_options', 'https://wordpress.com/settings/performance/' . $this->domain, null, 2 );
434
435
		if ( $wp_admin ) {
436
			return;
437
		}
438
439
		$submenus_to_update = array(
440
			'options-writing.php'    => 'https://wordpress.com/settings/writing/' . $this->domain,
441
			'options-discussion.php' => 'https://wordpress.com/settings/discussion/' . $this->domain,
442
		);
443
		$this->update_submenus( 'options-general.php', $submenus_to_update );
444
	}
445
446
	/**
447
	 * Adds Jetpack menu.
448
	 */
449
	public function add_jetpack_menu() {
450
		$this->add_admin_menu_separator( 50, 'manage_options' );
451
452
		// TODO: Replace with proper SVG data url.
453
		$icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 40 40' %3E%3Cpath fill='%23a0a5aa' d='M20 0c11.046 0 20 8.954 20 20s-8.954 20-20 20S0 31.046 0 20 8.954 0 20 0zm11 17H21v19l10-19zM19 4L9 23h10V4z'/%3E%3C/svg%3E";
454
455
		$is_menu_updated = $this->update_menu( 'jetpack', null, null, null, $icon, 51 );
456
		if ( ! $is_menu_updated ) {
457
			add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', 'jetpack', null, $icon, 51 );
458
		}
459
460
		add_submenu_page( 'jetpack', esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $this->domain, null, 2 );
461
		add_submenu_page( 'jetpack', esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 3 );
462
		/* translators: Jetpack sidebar menu item. */
463
		add_submenu_page( 'jetpack', esc_attr__( 'Search', 'jetpack' ), __( 'Search', 'jetpack' ), 'manage_options', 'https://wordpress.com/jetpack-search/' . $this->domain, null, 4 );
464
465
		$this->hide_submenu_page( 'jetpack', 'jetpack#/settings' );
466
		$this->hide_submenu_page( 'jetpack', 'stats' );
467
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
468
		$this->hide_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-scanner' ) ) );
469
470
		if ( ! $is_menu_updated ) {
471
			// Remove the submenu auto-created by Core just to be sure that there no issues on non-admin roles.
472
			remove_submenu_page( 'jetpack', 'jetpack' );
473
		}
474
	}
475
476
	/**
477
	 * Re-adds the Site Editor menu without the (beta) tag, and where we want it.
478
	 *
479
	 * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso).
480
	 */
481
	public function add_gutenberg_menus( $wp_admin = false ) {
482
		// We can bail if we don't meet the conditions of the Site Editor.
483
		if ( ! ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) ) {
484
			return;
485
		}
486
487
		// Core Gutenberg registers without an explicit position, and we don't want the (beta) tag.
488
		remove_menu_page( 'gutenberg-edit-site' );
489
		// Core Gutenberg tries to manage its position, foiling our best laid plans. Unfoil.
490
		remove_filter( 'menu_order', 'gutenberg_menu_order' );
491
492
		$link = $wp_admin ? 'gutenberg-edit-site' : 'https://wordpress.com/site-editor/' . $this->domain;
493
494
		add_menu_page(
495
			__( 'Site Editor', 'jetpack' ),
496
			__( 'Site Editor', 'jetpack' ),
497
			'edit_theme_options',
498
			$link,
499
			$wp_admin ? 'gutenberg_edit_site_page' : null,
500
			'dashicons-layout',
501
			61 // Just under Appearance.
502
		);
503
	}
504
505
	/**
506
	 * Whether to use wp-admin pages rather than Calypso.
507
	 *
508
	 * @return bool
509
	 */
510
	public function should_link_to_wp_admin() {
511
		return get_user_option( 'jetpack_admin_menu_link_destination' );
512
	}
513
514
	/**
515
	 * Returns the current slug from the URL.
516
	 *
517
	 * @param object $screen Screen object (undocumented).
518
	 *
519
	 * @return string
520
	 */
521
	public function get_current_slug( $screen ) {
522
		$slug = "{$screen->base}.php";
523
		if ( '' !== $screen->post_type ) {
524
			$slug = add_query_arg( 'post_type', $screen->post_type, $slug );
525
		}
526
		if ( '' !== $screen->taxonomy ) {
527
			$slug = add_query_arg( 'taxonomy', $screen->taxonomy, $slug );
528
		}
529
530
		return $slug;
531
	}
532
533
	/**
534
	 * Prepend a dashboard swithcer to the "Screen Options" box of the current page.
535
	 * Callback for the 'screen_settings' filter (available in WP 3.0 and up).
536
	 *
537
	 * @param string $current The currently added panels in screen options.
538
	 * @param object $screen Screen object (undocumented).
539
	 *
540
	 * @return string The HTML code to append to "Screen Options"
541
	 */
542
	public function register_dashboard_switcher( $current, $screen ) {
543
		$menu_mappings = require __DIR__ . '/menu-mappings.php';
544
		$slug          = $this->get_current_slug( $screen );
545
546
		// Let's show the switcher only in screens that we have a Calypso mapping to switch to.
547
		if ( ! isset( $menu_mappings[ $slug ] ) ) {
548
			return;
549
		}
550
551
		$contents = sprintf(
552
			'<div id="dashboard-switcher"><h5>%s</h5><p class="dashboard-switcher-text">%s</p><a class="button button-primary dashboard-switcher-button" href="%s">%s</a></div>',
553
			__( 'Screen features', 'jetpack' ),
554
			__( 'Currently you are seeing the classic WP-Admin view of this page. Would you like to see the default WordPress.com view?', 'jetpack' ),
555
			$menu_mappings[ $slug ] . $this->domain,
556
			__( 'Use WordPress.com view', 'jetpack' )
557
		);
558
559
		// Prepend the Dashboard swither to the other custom panels.
560
		$current = $contents . $current;
561
562
		return $current;
563
	}
564
}
565