Completed
Push — feature/reorg ( 6b724c...2e8e86 )
by
unknown
09:57 queued 10s
created

Admin_Menu::set_browse_sites_link_class()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
695
		$rtl                = is_rtl() ? '-rtl' : '';
696 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
697
			$style_dependencies = array( 'wpcom-admin-bar', 'wpcom-masterbar-css' );
698
		} else {
699
			$style_dependencies = array( 'a8c-wpcom-masterbar' . $rtl, 'a8c-wpcom-masterbar-overrides' . $rtl );
700
		}
701
		wp_enqueue_style(
702
			'jetpack-admin-menu',
703
			plugins_url( 'admin-menu.css', __FILE__ ),
704
			$style_dependencies,
705
			JETPACK__VERSION
706
		);
707
		wp_enqueue_script(
708
			'jetpack-admin-menu',
709
			plugins_url( 'admin-menu.js', __FILE__ ),
710
			array(),
711
			JETPACK__VERSION,
712
			true
713
		);
714
	}
715
716
	/**
717
	 * Dequeues unnecessary scripts.
718
	 */
719
	public function dequeue_scripts() {
720
		wp_dequeue_script( 'a8c_wpcom_masterbar_overrides' ); // Initially loaded in modules/masterbar/masterbar/class-masterbar.php.
721
	}
722
723
	/**
724
	 * Whether we're in a WordPress.com context.
725
	 *
726
	 * @return bool
727
	 */
728
	private function is_wpcom_site() {
729
		/**
730
		 * Filters whether this request is executed in a WordPress.com environment.
731
		 *
732
		 * Filterable to make it easier to unit test other parts of this class.
733
		 *
734
		 * @module masterbar
735
		 *
736
		 * @since 9.3.0
737
		 *
738
		 * @param bool $is_wpcom Whether this is a WordPress.com request. Defaults to the value of IS_WPCOM,
739
		 */
740
		return apply_filters( 'jetpack_admin_menu_is_wpcom', defined( 'IS_WPCOM' ) && IS_WPCOM );
741
	}
742
}
743