Completed
Push — add/sync-partial-sync-checksum... ( 661477...737c11 )
by
unknown
50:27 queued 41:58
created

Admin_Menu   F

Complexity

Total Complexity 99

Size/Duplication

Total Lines 654
Duplicated Lines 11.16 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 99
lcom 2
cbo 3
dl 73
loc 654
rs 1.946
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A get_instance() 0 7 2
B reregister_menu_items() 0 38 7
A add_browse_sites_link() 0 14 5
D add_site_card_menu() 12 52 18
B set_site_card_menu_class() 0 23 7
A add_my_home_menu() 0 17 3
A add_stats_menu() 0 13 3
A add_purchases_menu() 0 5 1
A add_posts_menu() 18 18 2
A add_media_menu() 0 12 2
A add_page_menu() 17 17 2
A add_comments_menu() 0 21 3
A add_jetpack_menu() 0 33 4
A jetpack_parent_file() 0 7 2
C add_appearance_menu() 26 49 12
A add_plugins_menu() 0 27 5
A add_users_menu() 0 30 5
A add_tools_menu() 0 21 3
A add_options_menu() 0 9 2
A migrate_submenus() 0 14 4
A add_admin_menu_separator() 0 14 1
A enqueue_scripts() 0 15 1
A dequeue_scripts() 0 3 1
A is_wpcom_site() 0 10 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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 Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Dashboard_Customizations;
9
10
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11
use Automattic\Jetpack\Redirect;
12
use Automattic\Jetpack\Status;
13
14
/**
15
 * Class Admin_Menu.
16
 */
17
class Admin_Menu {
18
	/**
19
	 * Holds class instance.
20
	 *
21
	 * @var Admin_Menu
22
	 */
23
	private static $instance;
24
25
	/**
26
	 * Whether the current request is a REST API request.
27
	 *
28
	 * @var bool
29
	 */
30
	private $is_api_request;
31
32
	/**
33
	 * Admin_Menu constructor.
34
	 */
35
	private function __construct() {
36
		if ( jetpack_is_atomic_site() ) {
37
			add_action(
38
				'admin_menu',
39
				function () {
40
					remove_action( 'admin_menu', 'gutenberg_menu', 9 );
41
				},
42
				0
43
			);
44
		}
45
46
		add_action( 'admin_menu', array( $this, 'reregister_menu_items' ), 99999 );
47
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
48
		add_action( 'wp_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
49
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_scripts' ), 20 );
50
	}
51
52
	/**
53
	 * Returns class instance.
54
	 *
55
	 * @return Admin_Menu
56
	 */
57
	public static function get_instance() {
58
		if ( null === static::$instance ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
59
			static::$instance = new self();
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
60
		}
61
62
		return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
63
	}
64
65
	/**
66
	 * Create the desired menu output.
67
	 */
68
	public function reregister_menu_items() {
69
		$this->is_api_request = ( defined( 'REST_API_PLUGINS' ) && REST_API_PLUGINS ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST );
70
71
		$domain = ( new Status() )->get_site_suffix();
72
73
		// Not needed outside of wp-admin.
74
		if ( ! $this->is_api_request && ( $this->is_wpcom_site() || jetpack_is_atomic_site() ) ) {
75
			$this->add_browse_sites_link();
76
			$this->add_site_card_menu( $domain );
77
		}
78
79
		/*
80
		 * Whether links should point to Calypso or wp-admin.
81
		 *
82
		 * true  - Calypso.
83
		 * false - wp-admin.
84
		 */
85
		$calypso = true;
86
87
		// Remove separators.
88
		remove_menu_page( 'separator1' );
89
90
		$this->add_my_home_menu( $domain, $calypso );
91
		$this->add_stats_menu( $domain );
92
		$this->add_purchases_menu( $domain );
93
		$this->add_posts_menu( $domain, $calypso );
94
		$this->add_media_menu( $domain, $calypso );
95
		$this->add_page_menu( $domain, $calypso );
96
		$this->add_comments_menu( $domain, $calypso );
97
		$this->add_jetpack_menu( $domain );
98
		$this->add_appearance_menu( $domain, $calypso );
99
		$this->add_plugins_menu( $domain );
100
		$this->add_users_menu( $domain, $calypso );
101
		$this->add_tools_menu( $domain, $calypso );
102
		$this->add_options_menu( $domain );
103
104
		ksort( $GLOBALS['menu'] );
105
	}
106
107
	/**
108
	 * Adds the site switcher link if user has more than one site.
109
	 */
110
	public function add_browse_sites_link() {
111
		if ( jetpack_is_atomic_site() ) {
112
			$wpcom_user_data = ( new Connection_Manager() )->get_connected_user_data();
113
114
			if ( $wpcom_user_data['site_count'] < 2 ) {
115
				return;
116
			}
117
		} elseif ( ! is_multisite() || count( get_blogs_of_user( get_current_user_id() ) ) < 2 ) {
118
			return;
119
		}
120
121
		// Add the menu item.
122
		add_menu_page( __( 'Browse sites', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 );
123
	}
124
125
	/**
126
	 * Adds site card component.
127
	 *
128
	 * @param string $domain Site domain.
129
	 */
130
	public function add_site_card_menu( $domain ) {
131
		$default = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' );
132
		$icon    = get_site_icon_url( 32, $default );
133
134
		if ( $default === $icon && function_exists( 'blavatar_exists' ) && blavatar_exists( $domain ) && function_exists( 'blavatar_url' ) ) {
135
			$icon = blavatar_url( $domain, 'img', 32 );
136
		}
137
138
		$badge = '';
139
		if ( $this->is_wpcom_site() ) {
140 View Code Duplication
			if ( function_exists( 'is_private_blog' ) && function_exists( 'wpcom_is_coming_soon' ) && is_private_blog() ) {
141
				$badge .= sprintf(
142
					'<span class="site__badge site__badge-private">%s</span>',
143
					wpcom_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
144
				);
145
			}
146
147
			if ( function_exists( 'is_redirected_domain' ) && is_redirected_domain( $domain ) ) {
148
				$badge .= '<span class="site__badge site__badge-redirect">' . esc_html__( 'Redirect', 'jetpack' ) . '</span>';
149
			}
150
151
			if ( ! empty( get_option( 'options' )['is_domain_only'] ) ) {
152
				$badge .= '<span class="site__badge site__badge-domain-only">' . esc_html__( 'Domain', 'jetpack' ) . '</span>';
153
			}
154
		}
155
156
		if ( jetpack_is_atomic_site() ) {
157 View Code Duplication
			if ( function_exists( 'site_is_private' ) && function_exists( 'site_is_coming_soon' ) && site_is_private() ) {
158
				$badge .= sprintf(
159
					'<span class="site__badge site__badge-private">%s</span>',
160
					site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' )
161
				);
162
			}
163
		}
164
165
		$site_card = '
166
<div class="site__info">
167
	<div class="site__title">%1$s</div>
168
	<div class="site__domain">%2$s</div>
169
	%3$s
170
</div>';
171
172
		$site_card = sprintf(
173
			$site_card,
174
			get_option( 'blogname' ),
175
			$domain,
176
			$badge
177
		);
178
179
		add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 );
180
		add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) );
181
	}
182
183
	/**
184
	 * Adds a custom element class and id for Site Card's menu item.
185
	 *
186
	 * @param array $menu Associative array of administration menu items.
187
	 * @return array
188
	 */
189
	public function set_site_card_menu_class( array $menu ) {
190
		foreach ( $menu as $key => $menu_item ) {
191
			if ( 'site-card' !== $menu_item[3] ) {
192
				continue;
193
			}
194
195
			$classes = ' toplevel_page_site-card';
196
197
			// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon.
198
			if (
199
				( function_exists( 'blavatar_exists' ) && blavatar_exists( ( new Status() )->get_site_suffix() ) ) ||
200
				( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) )
201
			) {
202
				$classes .= ' has-site-icon';
203
			}
204
205
			$menu[ $key ][4] = $menu_item[4] . $classes;
206
			$menu[ $key ][5] = 'toplevel_page_site_card';
207
			break;
208
		}
209
210
		return $menu;
211
	}
212
213
	/**
214
	 * Adds My Home menu.
215
	 *
216
	 * @param string $domain  Site domain.
217
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
218
	 */
219
	public function add_my_home_menu( $domain, $calypso = true ) {
220
		global $submenu;
221
222
		$menu_slug = $calypso ? 'https://wordpress.com/home/' . $domain : 'index.php';
223
224
		remove_menu_page( 'index.php' );
225
		remove_submenu_page( 'index.php', 'index.php' );
226
227
		add_menu_page( __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 'dashicons-admin-home', 2 );
228
229
		// Only add submenu when there are other submenu items.
230
		if ( ! empty( $submenu['index.php'] ) ) {
231
			add_submenu_page( $menu_slug, __( 'My Home', 'jetpack' ), __( 'My Home', 'jetpack' ), 'read', $menu_slug, null, 1 );
232
		}
233
234
		$this->migrate_submenus( 'index.php', $menu_slug );
235
	}
236
237
	/**
238
	 * Adds Stats menu.
239
	 *
240
	 * @param string $domain Site domain.
241
	 */
242
	public function add_stats_menu( $domain ) {
243
		$menu_title = __( 'Stats', 'jetpack' );
244
245
		if ( $this->is_wpcom_site() && ! $this->is_api_request ) {
246
			$menu_title .= sprintf(
247
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
248
				esc_url( home_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
249
				esc_attr__( 'Hourly views', 'jetpack' )
250
			);
251
		}
252
253
		add_menu_page( __( 'Stats', 'jetpack' ), $menu_title, 'edit_posts', 'https://wordpress.com/stats/day/' . $domain, null, 'dashicons-chart-bar', 3 );
254
	}
255
256
	/**
257
	 * Adds Purchases menu.
258
	 *
259
	 * @param string $domain Site domain.
260
	 */
261
	public function add_purchases_menu( $domain ) {
262
		remove_menu_page( 'paid-upgrades.php' );
263
		add_menu_page( __( 'Purchases', 'jetpack' ), __( 'Purchases', 'jetpack' ), 'manage_options', 'https://wordpress.com/plans/' . $domain, null, 'dashicons-cart', 4 );
264
		$this->migrate_submenus( 'paid-upgrades.php', 'https://wordpress.com/plans/' . $domain );
265
	}
266
267
	/**
268
	 * Adds Posts menu.
269
	 *
270
	 * @param string $domain  Site domain.
271
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
272
	 */
273 View Code Duplication
	public function add_posts_menu( $domain, $calypso = true ) {
274
		if ( ! $calypso ) {
275
			return;
276
		}
277
278
		$ptype_obj = get_post_type_object( 'post' );
279
		$menu_slug = 'https://wordpress.com/posts/' . $domain;
280
281
		remove_menu_page( 'edit.php' );
282
		remove_submenu_page( 'edit.php', 'edit.php' );
283
		remove_submenu_page( 'edit.php', 'post-new.php' );
284
285
		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 );
286
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
287
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/post/' . $domain, null, 10 );
288
289
		$this->migrate_submenus( 'edit.php', $menu_slug );
290
	}
291
292
	/**
293
	 * Adds Media menu.
294
	 *
295
	 * @param string $domain  Site domain.
296
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
297
	 */
298
	public function add_media_menu( $domain, $calypso = true ) {
299
		remove_submenu_page( 'upload.php', 'upload.php' );
300
		remove_submenu_page( 'upload.php', 'media-new.php' );
301
302
		if ( $calypso ) {
303
			$menu_slug = 'https://wordpress.com/media/' . $domain;
304
305
			remove_menu_page( 'upload.php' );
306
			add_menu_page( __( 'Media', 'jetpack' ), __( 'Media', 'jetpack' ), 'upload_files', $menu_slug, null, 'dashicons-admin-media', 10 );
307
			$this->migrate_submenus( 'upload.php', $menu_slug );
308
		}
309
	}
310
311
	/**
312
	 * Adds Page menu.
313
	 *
314
	 * @param string $domain  Site domain.
315
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
316
	 */
317 View Code Duplication
	public function add_page_menu( $domain, $calypso = true ) {
318
		if ( ! $calypso ) {
319
			return;
320
		}
321
322
		$ptype_obj = get_post_type_object( 'page' );
323
		$menu_slug = 'https://wordpress.com/pages/' . $domain;
324
325
		remove_menu_page( 'edit.php?post_type=page' );
326
		remove_submenu_page( 'edit.php?post_type=page', 'edit.php?post_type=page' );
327
		remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
328
329
		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 );
330
		add_submenu_page( $menu_slug, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $menu_slug, null, 5 );
331
		add_submenu_page( $menu_slug, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/page/' . $domain, null, 10 );
332
		$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
333
	}
334
335
	/**
336
	 * Adds Comments menu.
337
	 *
338
	 * @param string $domain  Site domain.
339
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
340
	 */
341
	public function add_comments_menu( $domain, $calypso = true ) {
342
		if ( ! $calypso || ! current_user_can( 'edit_posts' ) ) {
343
			return;
344
		}
345
346
		$awaiting_mod      = wp_count_comments();
347
		$awaiting_mod      = $awaiting_mod->moderated;
348
		$awaiting_mod_i18n = number_format_i18n( $awaiting_mod );
349
		/* translators: %s: Number of comments. */
350
		$awaiting_mod_text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $awaiting_mod, 'jetpack' ), $awaiting_mod_i18n );
351
352
		/* translators: %s: Number of comments. */
353
		$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>' );
354
		$menu_slug  = 'https://wordpress.com/comments/all/' . $domain;
355
356
		remove_menu_page( 'edit-comments.php' );
357
		remove_submenu_page( 'edit-comments.php', 'edit-comments.php' );
358
359
		add_menu_page( esc_attr__( 'Comments', 'jetpack' ), $menu_title, 'edit_posts', $menu_slug, null, 'dashicons-admin-comments', 25 );
360
		$this->migrate_submenus( 'edit-comments.php', $menu_slug );
361
	}
362
363
	/**
364
	 * Adds Jetpack menu.
365
	 *
366
	 * @param string $domain Site domain.
367
	 */
368
	public function add_jetpack_menu( $domain ) {
369
		if ( ! $this->is_wpcom_site() && ! jetpack_is_atomic_site() ) {
370
			return;
371
		}
372
373
		global $menu;
374
375
		$position = 50;
376
		while ( isset( $menu[ $position ] ) ) {
377
			$position++;
378
		}
379
380
		// TODO: Replace with proper SVG data url.
381
		$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";
382
		$jetpack_slug = 'https://wordpress.com/activity-log/' . $domain;
383
384
		$this->add_admin_menu_separator( $position++, 'manage_options' );
385
		add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position );
386
387
		// Maintain id for jQuery selector.
388
		$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
389
390
		remove_menu_page( 'jetpack' );
391
		remove_submenu_page( 'jetpack', esc_url( Redirect::get_url( 'calypso-backups' ) ) );
392
393
		$this->migrate_submenus( 'jetpack', $jetpack_slug );
394
395
		add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', 'https://wordpress.com/activity-log/' . $domain, null, 5 );
396
		add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $domain, null, 10 );
397
		add_submenu_page( $jetpack_slug, esc_attr__( 'Scan', 'jetpack' ), __( 'Scan', 'jetpack' ), 'manage_options', 'https://wordpress.com/scan/' . $domain, null, 15 );
398
399
		add_filter( 'parent_file', array( $this, 'jetpack_parent_file' ) );
400
	}
401
402
	/**
403
	 * Filters the parent file of an admin menu sub-menu item.
404
	 *
405
	 * @param string $parent_file The parent file.
406
	 * @return string Updated parent file.
407
	 */
408
	public function jetpack_parent_file( $parent_file ) {
409
		if ( 'jetpack' === $parent_file ) {
410
			$parent_file = 'https://wordpress.com/activity-log/' . ( new Status() )->get_site_suffix();
411
		}
412
413
		return $parent_file;
414
	}
415
416
	/**
417
	 * Adds Appearance menu.
418
	 *
419
	 * @param string $domain  Site domain.
420
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
421
	 */
422
	public function add_appearance_menu( $domain, $calypso = true ) {
423
		$user_can_customize = current_user_can( 'customize' );
424
		$appearance_cap     = $user_can_customize ? 'customize' : 'edit_theme_options';
425
		$customize_slug     = $calypso ? 'https://wordpress.com/customize/' . $domain : 'customize.php';
426
		$themes_slug        = $calypso ? 'https://wordpress.com/themes/' . $domain : 'themes.php';
427
		$appearance_slug    = $user_can_customize ? $customize_slug : $themes_slug;
428
		$customize_url      = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); // phpcs:ignore
429
430
		remove_menu_page( 'themes.php' );
431
		remove_submenu_page( 'themes.php', 'themes.php' );
432
		remove_submenu_page( 'themes.php', 'theme-editor.php' );
433
		remove_submenu_page( 'themes.php', $customize_url );
434
435
		add_menu_page( esc_attr__( 'Appearance', 'jetpack' ), __( 'Appearance', 'jetpack' ), $appearance_cap, $appearance_slug, null, 'dashicons-admin-appearance', 60 );
436
		add_submenu_page( $appearance_slug, esc_attr__( 'Customize', 'jetpack' ), __( 'Customize', 'jetpack' ), 'customize', $customize_slug, null, 5 );
437
		add_submenu_page( $appearance_slug, esc_attr__( 'Themes', 'jetpack' ), __( 'Themes', 'jetpack' ), 'switch_themes', $themes_slug, null, 10 );
438
439 View Code Duplication
		if ( current_theme_supports( 'custom-header' ) && $user_can_customize ) {
440
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $customize_url );
441
			remove_submenu_page( 'themes.php', $customize_header_url );
442
443
			$customize_header_url = add_query_arg( array( 'autofocus' => array( 'control' => 'header_image' ) ), $appearance_slug );
444
			add_submenu_page( $appearance_slug, __( 'Header', 'jetpack' ), __( 'Header', 'jetpack' ), $appearance_cap, esc_url( $customize_header_url ), null, 15 );
445
		}
446
447 View Code Duplication
		if ( current_theme_supports( 'custom-background' ) && $user_can_customize ) {
448
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $customize_url );
449
			remove_submenu_page( 'themes.php', $customize_background_url );
450
451
			$customize_background_url = add_query_arg( array( 'autofocus' => array( 'control' => 'background_image' ) ), $appearance_slug );
452
			add_submenu_page( $appearance_slug, esc_attr__( 'Background', 'jetpack' ), __( 'Background', 'jetpack' ), $appearance_cap, esc_url( $customize_background_url ), null, 20 );
453
		}
454
455 View Code Duplication
		if ( current_theme_supports( 'widgets' ) ) {
456
			remove_submenu_page( 'themes.php', 'widgets.php' );
457
458
			$customize_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'widgets' ) ), $appearance_slug );
459
			add_submenu_page( $appearance_slug, esc_attr__( 'Widgets', 'jetpack' ), __( 'Widgets', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_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_menu_url = add_query_arg( array( 'autofocus' => array( 'panel' => 'nav_menus' ) ), $appearance_slug );
466
			add_submenu_page( $appearance_slug, esc_attr__( 'Menus', 'jetpack' ), __( 'Menus', 'jetpack' ), $appearance_cap, esc_url( $customize_menu_url ), null, 20 );
467
		}
468
469
		$this->migrate_submenus( 'themes.php', $appearance_slug );
470
	}
471
472
	/**
473
	 * Adds Plugins menu.
474
	 *
475
	 * @param string $domain  Site domain.
476
	 */
477
	public function add_plugins_menu( $domain ) {
478
		$calypso = $this->is_wpcom_site();
479
480
		remove_submenu_page( 'plugins.php', 'plugin-editor.php' );
481
482
		if ( $calypso ) {
483
			remove_menu_page( 'plugins.php' );
484
485
			if ( $this->is_api_request ) {
486
				remove_submenu_page( 'plugins.php', 'plugins.php' );
487
			}
488
489
			$count = '';
490
			if ( ! is_multisite() && current_user_can( 'update_plugins' ) ) {
491
				$update_data = wp_get_update_data();
492
				$count       = sprintf(
493
					'<span class="update-plugins count-%s"><span class="plugin-count">%s</span></span>',
494
					$update_data['counts']['plugins'],
495
					number_format_i18n( $update_data['counts']['plugins'] )
496
				);
497
			}
498
499
			/* translators: %s: Number of pending plugin updates. */
500
			add_menu_page( esc_attr__( 'Plugins', 'jetpack' ), sprintf( __( 'Plugins %s', 'jetpack' ), $count ), 'activate_plugins', 'https://wordpress.com/plugins/' . $domain, null, 'dashicons-admin-plugins', 65 );
501
			$this->migrate_submenus( 'plugins.php', 'https://wordpress.com/plugins/' . $domain );
502
		}
503
	}
504
505
	/**
506
	 * Adds Users menu.
507
	 *
508
	 * @param string $domain  Site domain.
509
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
510
	 */
511
	public function add_users_menu( $domain, $calypso = true ) {
512
		$users_slug   = $calypso ? 'https://wordpress.com/people/team/' . $domain : 'users.php';
513
		$add_new_slug = 'https://wordpress.com/people/new/' . $domain;
514
		$profile_slug = $calypso ? 'https://wordpress.com/me' : 'grofiles-editor';
515
		$account_slug = $calypso ? 'https://wordpress.com/me/account' : 'grofiles-user-settings';
516
517
		if ( current_user_can( 'list_users' ) ) {
518
			remove_menu_page( 'users.php' );
519
			remove_submenu_page( 'users.php', 'users.php' );
520
			remove_submenu_page( 'users.php', 'user-new.php' );
521
			remove_submenu_page( 'users.php', 'profile.php' );
522
			remove_submenu_page( 'users.php', 'grofiles-editor' );
523
			remove_submenu_page( 'users.php', 'grofiles-user-settings' );
524
525
			add_menu_page( esc_attr__( 'Users', 'jetpack' ), __( 'Users', 'jetpack' ), 'list_users', $users_slug, null, 'dashicons-admin-users', 70 );
526
			add_submenu_page( $users_slug, esc_attr__( 'All People', 'jetpack' ), __( 'All People', 'jetpack' ), 'list_users', $users_slug, null, 5 );
527
			add_submenu_page( $users_slug, esc_attr__( 'Add New', 'jetpack' ), __( 'Add New', 'jetpack' ), 'promote_users', $add_new_slug, null, 10 );
528
			add_submenu_page( $users_slug, esc_attr__( 'My Profile', 'jetpack' ), __( 'My Profile', 'jetpack' ), 'read', $profile_slug, null, 15 );
529
			add_submenu_page( $users_slug, esc_attr__( 'Account Settings', 'jetpack' ), __( 'Account Settings', 'jetpack' ), 'read', $account_slug, null, 20 );
530
			$this->migrate_submenus( 'users.php', $users_slug );
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
			$this->migrate_submenus( 'profile.php', $profile_slug );
539
		}
540
	}
541
542
	/**
543
	 * Adds Tools menu.
544
	 *
545
	 * @param string $domain  Site domain.
546
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
547
	 */
548
	public function add_tools_menu( $domain, $calypso = true ) {
549
		$admin_slug = 'tools.php';
550
		$menu_slug  = $calypso ? 'https://wordpress.com/marketing/tools/' . $domain : $admin_slug;
551
552
		add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $domain, null, 5 );
553
		add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $domain, null, 10 );
554
555
		if ( $calypso ) {
556
			remove_menu_page( $admin_slug );
557
			remove_submenu_page( $admin_slug, $admin_slug );
558
			remove_submenu_page( $admin_slug, 'import.php' );
559
			remove_submenu_page( $admin_slug, 'export.php' );
560
			remove_submenu_page( $admin_slug, 'delete-blog' );
561
562
			add_menu_page( esc_attr__( 'Tools', 'jetpack' ), __( 'Tools', 'jetpack' ), 'manage_options', $menu_slug, null, 'dashicons-admin-tools', 75 );
563
			add_submenu_page( $menu_slug, esc_attr__( 'Import', 'jetpack' ), __( 'Import', 'jetpack' ), 'import', 'https://wordpress.com/import/' . $domain, null, 15 );
564
			add_submenu_page( $menu_slug, esc_attr__( 'Export', 'jetpack' ), __( 'Export', 'jetpack' ), 'export', 'https://wordpress.com/export/' . $domain, null, 20 );
565
566
			$this->migrate_submenus( $admin_slug, $menu_slug );
567
		}
568
	}
569
570
	/**
571
	 * Adds Settings menu.
572
	 *
573
	 * @param string $domain Site domain.
574
	 * @param bool   $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso).
575
	 */
576
	public function add_options_menu( $domain, $calypso = true ) {
577
		if ( $calypso ) {
578
			remove_submenu_page( 'options-general.php', 'options-discussion.php' );
579
			remove_submenu_page( 'options-general.php', 'options-writing.php' );
580
		}
581
582
		add_options_page( esc_attr__( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $domain, null, 1 );
583
		add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $domain, null, 6 );
584
	}
585
586
	/**
587
	 * Migrates submenu items from wp-admin menu slugs to Calypso menu slugs.
588
	 *
589
	 * @param string $old_slug WP-Admin menu slug.
590
	 * @param string $new_slug Calypso menu slug. (Calypso URL).
591
	 */
592
	public function migrate_submenus( $old_slug, $new_slug ) {
593
		global $submenu;
594
595
		if ( $old_slug !== $new_slug && ! empty( $submenu[ $old_slug ] ) ) {
596
			if ( ! empty( $submenu[ $new_slug ] ) ) {
597
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
598
				$submenu[ $new_slug ] = array_replace( $submenu[ $new_slug ], $submenu[ $old_slug ] );
599
			} else {
600
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
601
				$submenu[ $new_slug ] = $submenu[ $old_slug ];
602
			}
603
			unset( $submenu[ $old_slug ] );
604
		}
605
	}
606
607
	/**
608
	 * Adds a menu separator.
609
	 *
610
	 * @param int    $position The position in the menu order this item should appear.
611
	 * @param string $cap      Optional. The capability required for this menu to be displayed to the user.
612
	 *                         Default: 'read'.
613
	 */
614
	public function add_admin_menu_separator( $position, $cap = 'read' ) {
615
		global $menu;
616
		static $uid = 3;
617
618
		// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
619
		$menu[ $position ] = array(
620
			'',                               // Menu title (ignored).
621
			$cap,                             // Required capability.
622
			'separator-custom-' . ( ++$uid ), // URL or file (ignored, but must be unique).
623
			'',                               // Page title (ignored).
624
			'wp-menu-separator',              // CSS class. Identifies this item as a separator.
625
		);
626
		ksort( $menu );
627
	}
628
629
	/**
630
	 * Enqueues scripts and styles.
631
	 */
632
	public function enqueue_scripts() {
633
		wp_enqueue_style(
634
			'jetpack-admin-menu',
635
			plugins_url( 'admin-menu.css', __FILE__ ),
636
			array(),
637
			'1'
638
		);
639
		wp_enqueue_script(
640
			'jetpack-admin-menu',
641
			plugins_url( 'admin-menu.js', __FILE__ ),
642
			array(),
643
			'1',
644
			true
645
		);
646
	}
647
648
	/**
649
	 * Dequeues unnecessary scripts.
650
	 */
651
	public function dequeue_scripts() {
652
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
653
	}
654
655
	/**
656
	 * Whether we're in a WordPress.com context.
657
	 *
658
	 * @return bool
659
	 */
660
	private function is_wpcom_site() {
661
		/**
662
		 * Filters whether this request is executed in a WordPress.com environment.
663
		 *
664
		 * Filterable to make it easier to unit test other parts of this class.
665
		 *
666
		 * @param bool $is_wpcom Whether this is a WordPress.com request. Defaults to the value of IS_WPCOM,
667
		 */
668
		return apply_filters( 'jetpack_admin_menu_is_wpcom', defined( 'IS_WPCOM' ) && IS_WPCOM );
669
	}
670
}
671