Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

Admin_Menu::add_stats_menu()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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