Completed
Push — renovate/major-wordpress-monor... ( fa07ab...6cd1fe )
by
unknown
93:31 queued 87:25
created

A8C_WPCOM_Masterbar   F

Complexity

Total Complexity 86

Size/Duplication

Total Lines 1371
Duplicated Lines 8.9 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 86
lcom 1
cbo 8
dl 122
loc 1371
rs 0.8
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C init() 0 77 9
B maybe_logout_user_from_wpcom() 0 39 6
A get_rtl_admin_bar_class() 0 3 1
A admin_body_class() 0 3 1
A remove_core_styles() 0 9 2
A is_rtl() 0 3 2
A add_styles_and_scripts() 0 44 3
A wpcom_static_url() 0 11 2
A replace_core_masterbar() 0 10 2
A clear_core_masterbar() 0 5 2
A build_wpcom_masterbar() 0 21 3
A get_locale() 6 18 6
A add_notifications() 0 23 2
B add_reader_submenu() 0 99 1
A create_menu_item_pair() 0 9 1
A create_menu_item_anchor() 0 3 1
A wpcom_adminbar_add_secondary_groups() 0 29 1
B add_me_submenu() 0 173 4
A add_write_button() 0 28 4
F add_my_sites_submenu() 116 549 29
A add_my_home_submenu_item() 0 17 3

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 A8C_WPCOM_Masterbar 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 A8C_WPCOM_Masterbar, and based on these observations, apply Extract Interface, too.

1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
use Automattic\Jetpack\Assets;
4
use Automattic\Jetpack\Connection\Client;
5
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
6
7
require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
8
9
/**
10
 * Custom Admin bar displayed instead of the default WordPress admin bar.
11
 */
12
class A8C_WPCOM_Masterbar {
13
	/**
14
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
15
	 * If not set it will default to loading the ones from WordPress.com.
16
	 *
17
	 * @var string $sandbox_url
18
	 */
19
	private $sandbox_url = '';
20
21
	/**
22
	 * Current locale.
23
	 *
24
	 * @var string
25
	 */
26
	private $locale;
27
28
	/**
29
	 * Current User ID.
30
	 *
31
	 * @var int
32
	 */
33
	private $user_id;
34
	/**
35
	 * WordPress.com user data of the connected user.
36
	 *
37
	 * @var array
38
	 */
39
	private $user_data;
40
	/**
41
	 * WordPress.com username for the connected user.
42
	 *
43
	 * @var string
44
	 */
45
	private $user_login;
46
	/**
47
	 * WordPress.com email address for the connected user.
48
	 *
49
	 * @var string
50
	 */
51
	private $user_email;
52
	/**
53
	 * WordPress.com display name for the connected user.
54
	 *
55
	 * @var string
56
	 */
57
	private $display_name;
58
	/**
59
	 * Site URL sanitized for usage in WordPress.com slugs.
60
	 *
61
	 * @var string
62
	 */
63
	private $primary_site_slug;
64
	/**
65
	 * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.
66
	 *
67
	 * @var string
68
	 */
69
	private $user_text_direction;
70
	/**
71
	 * Number of sites owned by connected WordPress.com user.
72
	 *
73
	 * @var int
74
	 */
75
	private $user_site_count;
76
77
	/**
78
	 * Constructor
79
	 */
80
	public function __construct() {
81
		add_action( 'admin_bar_init', array( $this, 'init' ) );
82
83
		// Post logout on the site, also log the user out of WordPress.com.
84
		add_filter( 'logout_redirect', array( $this, 'maybe_logout_user_from_wpcom' ), 10, 3 );
85
	}
86
87
	/**
88
	 * Initialize our masterbar.
89
	 */
90
	public function init() {
91
		$this->locale  = $this->get_locale();
92
		$this->user_id = get_current_user_id();
93
94
		// Limit the masterbar to be shown only to connected Jetpack users.
95
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
96
			return;
97
		}
98
99
		// Don't show the masterbar on WordPress mobile apps.
100
		if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
101
			add_filter( 'show_admin_bar', '__return_false' );
102
			return;
103
		}
104
105
		// Disable the Masterbar on AMP views.
106
		if (
107
			class_exists( 'Jetpack_AMP_Support' )
108
			&& Jetpack_AMP_Support::is_amp_request()
109
		) {
110
			return;
111
		}
112
113
		Jetpack::dns_prefetch(
114
			array(
115
				'//s0.wp.com',
116
				'//s1.wp.com',
117
				'//s2.wp.com',
118
				'//0.gravatar.com',
119
				'//1.gravatar.com',
120
				'//2.gravatar.com',
121
			)
122
		);
123
124
		// Atomic only.
125
		if ( jetpack_is_atomic_site() ) {
126
			/*
127
			 * override user setting that hides masterbar from site's front.
128
			 * https://github.com/Automattic/jetpack/issues/7667
129
			 */
130
			add_filter( 'show_admin_bar', '__return_true' );
131
		}
132
133
		$this->user_data       = Jetpack::get_connected_user_data( $this->user_id );
134
		$this->user_login      = $this->user_data['login'];
135
		$this->user_email      = $this->user_data['email'];
136
		$this->display_name    = $this->user_data['display_name'];
137
		$this->user_site_count = $this->user_data['site_count'];
138
139
		// Used to build menu links that point directly to Calypso.
140
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
141
142
		// Used for display purposes and for building WP Admin links.
143
		$this->primary_site_url = str_replace( '::', '/', $this->primary_site_slug );
0 ignored issues
show
Bug introduced by
The property primary_site_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
144
145
		// We need to use user's setting here, instead of relying on current blog's text direction.
146
		$this->user_text_direction = $this->user_data['text_direction'];
147
148
		if ( $this->is_rtl() ) {
149
			// Extend core WP_Admin_Bar class in order to add rtl styles.
150
			add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );
151
		}
152
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
153
154
		add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );
155
156
		add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
157
		add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
158
159
		add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );
160
		add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );
161
162
		if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) {
163
			// Override Notification module to include RTL styles.
164
			add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' );
165
		}
166
	}
167
168
	/**
169
	 * Log out from WordPress.com when logging out of the local site.
170
	 *
171
	 * @param string  $redirect_to           The redirect destination URL.
172
	 * @param string  $requested_redirect_to The requested redirect destination URL passed as a parameter.
173
	 * @param WP_User $user                  The WP_User object for the user that's logging out.
174
	 */
175
	public function maybe_logout_user_from_wpcom( $redirect_to, $requested_redirect_to, $user ) {
176
		/**
177
		 * Whether we should sign out from wpcom too when signing out from the masterbar.
178
		 *
179
		 * @since 5.9.0
180
		 *
181
		 * @param bool $masterbar_should_logout_from_wpcom True by default.
182
		 */
183
		$masterbar_should_logout_from_wpcom = apply_filters( 'jetpack_masterbar_should_logout_from_wpcom', true );
184
		if (
185
			// No need to check for a nonce here, it happens further up.
186
			isset( $_GET['context'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
187
			&& 'masterbar' === $_GET['context'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
188
			&& $masterbar_should_logout_from_wpcom
189
		) {
190
			/*
191
			 * Get the associated WordPress.com User ID, if the user is connected.
192
			 */
193
			$connection_manager = new Connection_Manager();
194
			if ( $connection_manager->is_user_connected( $user->ID ) ) {
195
				$wpcom_user_data = $connection_manager->get_connected_user_data( $user->ID );
196
				if ( ! empty( $wpcom_user_data['ID'] ) ) {
197
					/**
198
					 * Hook into the log out event happening from the Masterbar.
199
					 *
200
					 * @since 5.1.0
201
					 * @since 7.9.0 Added the $wpcom_user_id parameter to the action.
202
					 *
203
					 * @module masterbar
204
					 *
205
					 * @param int $wpcom_user_id WordPress.com User ID.
206
					 */
207
					do_action( 'wp_masterbar_logout', $wpcom_user_data['ID'] );
208
				}
209
			}
210
		}
211
212
		return $redirect_to;
213
	}
214
215
	/**
216
	 * Get class name for RTL sites.
217
	 */
218
	public function get_rtl_admin_bar_class() {
219
		return 'RTL_Admin_Bar';
220
	}
221
222
	/**
223
	 * Adds CSS classes to admin body tag.
224
	 *
225
	 * @since 5.1
226
	 *
227
	 * @param string $admin_body_classes CSS classes that will be added.
228
	 *
229
	 * @return string
230
	 */
231
	public function admin_body_class( $admin_body_classes ) {
232
		return "$admin_body_classes jetpack-masterbar";
233
	}
234
235
	/**
236
	 * Remove the default Admin Bar CSS.
237
	 */
238
	public function remove_core_styles() {
239
		/*
240
		 * Notifications need the admin bar styles,
241
		 * so let's not remove them when the module is active.
242
		 */
243
		if ( ! Jetpack::is_module_active( 'notes' ) ) {
244
			wp_dequeue_style( 'admin-bar' );
245
		}
246
	}
247
248
	/**
249
	 * Check if the user settings are for an RTL language or not.
250
	 */
251
	public function is_rtl() {
252
		return 'rtl' === $this->user_text_direction ? true : false;
253
	}
254
255
	/**
256
	 * Enqueue our own CSS and JS to display our custom admin bar.
257
	 */
258
	public function add_styles_and_scripts() {
259
260
		if ( $this->is_rtl() ) {
261
			wp_enqueue_style( 'a8c-wpcom-masterbar-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/rtl/wpcom-admin-bar-rtl.css' ), array(), JETPACK__VERSION );
262
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides-rtl', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/rtl/masterbar-rtl.css' ), array(), JETPACK__VERSION );
263
		} else {
264
			wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );
265
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );
266
		}
267
268
		// Local overrides.
269
		wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION );
270
271
		if ( ! Jetpack::is_module_active( 'notes ' ) ) {
272
			// Masterbar is relying on some icons from noticons.css.
273
			wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
274
		}
275
276
		wp_enqueue_script(
277
			'jetpack-accessible-focus',
278
			Assets::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
279
			array(),
280
			JETPACK__VERSION,
281
			false
282
		);
283
		wp_enqueue_script(
284
			'a8c_wpcom_masterbar_tracks_events',
285
			Assets::get_file_url_for_environment(
286
				'_inc/build/masterbar/tracks-events.min.js',
287
				'modules/masterbar/tracks-events.js'
288
			),
289
			array( 'jquery' ),
290
			JETPACK__VERSION,
291
			false
292
		);
293
294
		wp_enqueue_script(
295
			'a8c_wpcom_masterbar_overrides',
296
			$this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ),
297
			array( 'jquery' ),
298
			JETPACK__VERSION,
299
			false
300
		);
301
	}
302
303
	/**
304
	 * Get base URL where our CSS and JS will come from.
305
	 *
306
	 * @param string $file File path for a static resource.
307
	 */
308
	private function wpcom_static_url( $file ) {
309
		if ( ! empty( $this->sandbox_url ) ) {
310
			// For testing undeployed changes to remotely enqueued scripts and styles.
311
			return set_url_scheme( $this->sandbox_url . $file, 'https' );
312
		}
313
314
		$i   = hexdec( substr( md5( $file ), - 1 ) ) % 2;
315
		$url = 'https://s' . $i . '.wp.com' . $file;
316
317
		return set_url_scheme( $url, 'https' );
318
	}
319
320
	/**
321
	 * Remove the default admin bar items and replace it with our own admin bar.
322
	 */
323
	public function replace_core_masterbar() {
324
		global $wp_admin_bar;
325
326
		if ( ! is_object( $wp_admin_bar ) ) {
327
			return false;
328
		}
329
330
		$this->clear_core_masterbar( $wp_admin_bar );
331
		$this->build_wpcom_masterbar( $wp_admin_bar );
332
	}
333
334
	/**
335
	 * Remove all existing toolbar entries from core Masterbar
336
	 *
337
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
338
	 */
339
	public function clear_core_masterbar( $wp_admin_bar ) {
340
		foreach ( $wp_admin_bar->get_nodes() as $node ) {
341
			$wp_admin_bar->remove_node( $node->id );
342
		}
343
	}
344
345
	/**
346
	 * Add entries corresponding to WordPress.com Masterbar
347
	 *
348
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
349
	 */
350
	public function build_wpcom_masterbar( $wp_admin_bar ) {
351
		// Menu groups.
352
		$this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
353
354
		// Left part.
355
		$this->add_my_sites_submenu( $wp_admin_bar );
356
		$this->add_reader_submenu( $wp_admin_bar );
357
358
		// Right part.
359
		if ( Jetpack::is_module_active( 'notes' ) ) {
360
			$this->add_notifications( $wp_admin_bar );
361
		}
362
363
		$this->add_me_submenu( $wp_admin_bar );
364
		$this->add_write_button( $wp_admin_bar );
365
366
		// Recovery mode exit.
367
		if ( function_exists( 'wp_admin_bar_recovery_mode_menu' ) ) {
368
			wp_admin_bar_recovery_mode_menu( $wp_admin_bar );
369
		}
370
	}
371
372
	/**
373
	 * Get WordPress.com current locale name.
374
	 */
375
	public function get_locale() {
376
		$wpcom_locale = get_locale();
377
378
		if ( ! class_exists( 'GP_Locales' ) ) {
379
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
380
				require JETPACK__GLOTPRESS_LOCALES_PATH;
381
			}
382
		}
383
384 View Code Duplication
		if ( class_exists( 'GP_Locales' ) ) {
385
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
386
			if ( $wpcom_locale_object instanceof GP_Locale ) {
387
				$wpcom_locale = $wpcom_locale_object->slug;
388
			}
389
		}
390
391
		return $wpcom_locale;
392
	}
393
394
	/**
395
	 * Add the Notifications menu item.
396
	 *
397
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
398
	 */
399
	public function add_notifications( $wp_admin_bar ) {
400
		$wp_admin_bar->add_node(
401
			array(
402
				'id'     => 'notes',
403
				'title'  => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span>
404
						 <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>
405
						 <span class="noticon noticon-bell"></span>',
406
				'meta'   => array(
407
					'html'  => '<div id="wpnt-notes-panel2" style="display:none" lang="' . esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' .
408
								'<div class="wpnt-notes-panel-header">' .
409
								'<span class="wpnt-notes-header">' .
410
								esc_html__( 'Notifications', 'jetpack' ) .
411
								'</span>' .
412
								'<span class="wpnt-notes-panel-link">' .
413
								'</span>' .
414
								'</div>' .
415
								'</div>',
416
					'class' => 'menupop mb-trackable',
417
				),
418
				'parent' => 'top-secondary',
419
			)
420
		);
421
	}
422
423
	/**
424
	 * Add the "Reader" menu item in the root default group.
425
	 *
426
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
427
	 */
428
	public function add_reader_submenu( $wp_admin_bar ) {
429
		$wp_admin_bar->add_menu(
430
			array(
431
				'parent' => 'root-default',
432
				'id'     => 'newdash',
433
				'title'  => esc_html__( 'Reader', 'jetpack' ),
434
				'href'   => '#',
435
				'meta'   => array(
436
					'class' => 'mb-trackable',
437
				),
438
			)
439
		);
440
441
		$wp_admin_bar->add_menu(
442
			array(
443
				'parent' => 'newdash',
444
				'id'     => 'streams-header',
445
				'title'  => esc_html_x(
446
					'Streams',
447
					'Title for Reader sub-menu that contains followed sites, likes, and recommendations',
448
					'jetpack'
449
				),
450
				'meta'   => array(
451
					'class' => 'ab-submenu-header',
452
				),
453
			)
454
		);
455
456
		$following_title = $this->create_menu_item_pair(
457
			array(
458
				'url'   => 'https://wordpress.com/read',
459
				'id'    => 'wp-admin-bar-followed-sites',
460
				'label' => esc_html__( 'Followed Sites', 'jetpack' ),
461
			),
462
			array(
463
				'url'   => 'https://wordpress.com/following/edit',
464
				'id'    => 'wp-admin-bar-reader-followed-sites-manage',
465
				'label' => esc_html__( 'Manage', 'jetpack' ),
466
			)
467
		);
468
469
		$wp_admin_bar->add_menu(
470
			array(
471
				'parent' => 'newdash',
472
				'id'     => 'following',
473
				'title'  => $following_title,
474
				'meta'   => array( 'class' => 'inline-action' ),
475
			)
476
		);
477
478
		$wp_admin_bar->add_menu(
479
			array(
480
				'parent' => 'newdash',
481
				'id'     => 'discover-discover',
482
				'title'  => esc_html__( 'Discover', 'jetpack' ),
483
				'href'   => 'https://wordpress.com/discover',
484
				'meta'   => array(
485
					'class' => 'mb-icon-spacer',
486
				),
487
			)
488
		);
489
490
		$wp_admin_bar->add_menu(
491
			array(
492
				'parent' => 'newdash',
493
				'id'     => 'discover-search',
494
				'title'  => esc_html__( 'Search', 'jetpack' ),
495
				'href'   => 'https://wordpress.com/read/search',
496
				'meta'   => array(
497
					'class' => 'mb-icon-spacer',
498
				),
499
			)
500
		);
501
502
		$wp_admin_bar->add_menu(
503
			array(
504
				'parent' => 'newdash',
505
				'id'     => 'discover-recommended-blogs',
506
				'title'  => esc_html__( 'Recommendations', 'jetpack' ),
507
				'href'   => 'https://wordpress.com/recommendations',
508
				'meta'   => array(
509
					'class' => 'mb-icon-spacer',
510
				),
511
			)
512
		);
513
514
		$wp_admin_bar->add_menu(
515
			array(
516
				'parent' => 'newdash',
517
				'id'     => 'my-activity-my-likes',
518
				'title'  => esc_html__( 'My Likes', 'jetpack' ),
519
				'href'   => 'https://wordpress.com/activities/likes',
520
				'meta'   => array(
521
					'class' => 'mb-icon-spacer',
522
				),
523
			)
524
		);
525
526
	}
527
528
	/**
529
	 * Merge 2 menu items together into 2 link tags.
530
	 *
531
	 * @param array $primary   Array of menu information.
532
	 * @param array $secondary Array of menu information.
533
	 */
534
	public function create_menu_item_pair( $primary, $secondary ) {
535
		$primary_class   = 'ab-item ab-primary mb-icon';
536
		$secondary_class = 'ab-secondary';
537
538
		$primary_anchor   = $this->create_menu_item_anchor( $primary_class, $primary['url'], $primary['label'], $primary['id'] );
539
		$secondary_anchor = $this->create_menu_item_anchor( $secondary_class, $secondary['url'], $secondary['label'], $secondary['id'] );
540
541
		return $primary_anchor . $secondary_anchor;
542
	}
543
544
	/**
545
	 * Create a link tag based on information about a menu item.
546
	 *
547
	 * @param string $class Menu item CSS class.
548
	 * @param string $url   URL you go to when clicking on the menu item.
549
	 * @param string $label Menu item title.
550
	 * @param string $id    Menu item slug.
551
	 */
552
	public function create_menu_item_anchor( $class, $url, $label, $id ) {
553
		return '<a href="' . $url . '" class="' . $class . '" id="' . $id . '">' . $label . '</a>';
554
	}
555
556
	/**
557
	 * Add Secondary groups for submenu items.
558
	 *
559
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
560
	 */
561
	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
562
		$wp_admin_bar->add_group(
563
			array(
564
				'id'   => 'root-default',
565
				'meta' => array(
566
					'class' => 'ab-top-menu',
567
				),
568
			)
569
		);
570
571
		$wp_admin_bar->add_group(
572
			array(
573
				'parent' => 'blog',
574
				'id'     => 'blog-secondary',
575
				'meta'   => array(
576
					'class' => 'ab-sub-secondary',
577
				),
578
			)
579
		);
580
581
		$wp_admin_bar->add_group(
582
			array(
583
				'id'   => 'top-secondary',
584
				'meta' => array(
585
					'class' => 'ab-top-secondary',
586
				),
587
			)
588
		);
589
	}
590
591
	/**
592
	 * Add User info menu item.
593
	 *
594
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
595
	 */
596
	public function add_me_submenu( $wp_admin_bar ) {
597
		$user_id = get_current_user_id();
598
		if ( empty( $user_id ) ) {
599
			return;
600
		}
601
602
		$avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
603
		$class  = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
604
605
		// Add the 'Me' menu.
606
		$wp_admin_bar->add_menu(
607
			array(
608
				'id'     => 'my-account',
609
				'parent' => 'top-secondary',
610
				'title'  => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
611
				'href'   => '#',
612
				'meta'   => array(
613
					'class' => $class,
614
				),
615
			)
616
		);
617
618
		$id = 'user-actions';
619
		$wp_admin_bar->add_group(
620
			array(
621
				'parent' => 'my-account',
622
				'id'     => $id,
623
			)
624
		);
625
626
		$settings_url = 'https://wordpress.com/me/account';
627
628
		$logout_url = wp_logout_url();
629
		$logout_url = add_query_arg( 'context', 'masterbar', $logout_url );
630
631
		$user_info  = get_avatar( $this->user_email, 128, 'mm', '', array( 'force_display' => true ) );
632
		$user_info .= '<span class="display-name">' . $this->display_name . '</span>';
633
		$user_info .= '<a class="username" href="https://gravatar.com/' . $this->user_login . '">@' . $this->user_login . '</a>';
634
635
		$user_info .= sprintf(
636
			'<div><a href="%s" class="ab-sign-out">%s</a></div>',
637
			$logout_url,
638
			esc_html__( 'Sign Out', 'jetpack' )
639
		);
640
641
		$wp_admin_bar->add_menu(
642
			array(
643
				'parent' => $id,
644
				'id'     => 'user-info',
645
				'title'  => $user_info,
646
				'meta'   => array(
647
					'class'    => 'user-info user-info-item',
648
					'tabindex' => -1,
649
				),
650
			)
651
		);
652
653
		$wp_admin_bar->add_menu(
654
			array(
655
				'parent' => $id,
656
				'id'     => 'profile-header',
657
				'title'  => esc_html__( 'Profile', 'jetpack' ),
658
				'meta'   => array(
659
					'class' => 'ab-submenu-header',
660
				),
661
			)
662
		);
663
664
		$wp_admin_bar->add_menu(
665
			array(
666
				'parent' => $id,
667
				'id'     => 'my-profile',
668
				'title'  => esc_html__( 'My Profile', 'jetpack' ),
669
				'href'   => 'https://wordpress.com/me',
670
				'meta'   => array(
671
					'class' => 'mb-icon',
672
				),
673
			)
674
		);
675
676
		$wp_admin_bar->add_menu(
677
			array(
678
				'parent' => $id,
679
				'id'     => 'account-settings',
680
				'title'  => esc_html__( 'Account Settings', 'jetpack' ),
681
				'href'   => $settings_url,
682
				'meta'   => array(
683
					'class' => 'mb-icon',
684
				),
685
			)
686
		);
687
688
		$wp_admin_bar->add_menu(
689
			array(
690
				'parent' => $id,
691
				'id'     => 'billing',
692
				'title'  => esc_html__( 'Manage Purchases', 'jetpack' ),
693
				'href'   => 'https://wordpress.com/me/purchases',
694
				'meta'   => array(
695
					'class' => 'mb-icon',
696
				),
697
			)
698
		);
699
700
		$wp_admin_bar->add_menu(
701
			array(
702
				'parent' => $id,
703
				'id'     => 'security',
704
				'title'  => esc_html__( 'Security', 'jetpack' ),
705
				'href'   => 'https://wordpress.com/me/security',
706
				'meta'   => array(
707
					'class' => 'mb-icon',
708
				),
709
			)
710
		);
711
712
		$wp_admin_bar->add_menu(
713
			array(
714
				'parent' => $id,
715
				'id'     => 'notifications',
716
				'title'  => esc_html__( 'Notifications', 'jetpack' ),
717
				'href'   => 'https://wordpress.com/me/notifications',
718
				'meta'   => array(
719
					'class' => 'mb-icon',
720
				),
721
			)
722
		);
723
724
		$wp_admin_bar->add_menu(
725
			array(
726
				'parent' => $id,
727
				'id'     => 'special-header',
728
				'title'  => esc_html_x(
729
					'Special',
730
					'Title for Me sub-menu that contains Get Apps, Next Steps, and Help options',
731
					'jetpack'
732
				),
733
				'meta'   => array(
734
					'class' => 'ab-submenu-header',
735
				),
736
			)
737
		);
738
739
		$wp_admin_bar->add_menu(
740
			array(
741
				'parent' => $id,
742
				'id'     => 'get-apps',
743
				'title'  => esc_html__( 'Get Apps', 'jetpack' ),
744
				'href'   => 'https://wordpress.com/me/get-apps',
745
				'meta'   => array(
746
					'class' => 'mb-icon user-info-item',
747
				),
748
			)
749
		);
750
751
		$help_link = 'https://jetpack.com/support/';
752
753
		if ( jetpack_is_atomic_site() ) {
754
			$help_link = 'https://wordpress.com/help';
755
		}
756
757
		$wp_admin_bar->add_menu(
758
			array(
759
				'parent' => $id,
760
				'id'     => 'help',
761
				'title'  => esc_html__( 'Help', 'jetpack' ),
762
				'href'   => $help_link,
763
				'meta'   => array(
764
					'class' => 'mb-icon user-info-item',
765
				),
766
			)
767
		);
768
	}
769
770
	/**
771
	 * Add Write Menu item.
772
	 *
773
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
774
	 */
775
	public function add_write_button( $wp_admin_bar ) {
776
		$current_user = wp_get_current_user();
777
778
		$posting_blog_id = get_current_blog_id();
779
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
780
			$posting_blog_id = $current_user->primary_blog;
781
		}
782
783
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
784
785
		if ( ! $posting_blog_id || ! $user_can_post ) {
786
			return;
787
		}
788
789
		$blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
790
791
		$wp_admin_bar->add_menu(
792
			array(
793
				'parent' => 'top-secondary',
794
				'id'     => 'ab-new-post',
795
				'href'   => $blog_post_page,
796
				'title'  => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
797
				'meta'   => array(
798
					'class' => 'mb-trackable',
799
				),
800
			)
801
		);
802
	}
803
804
	/**
805
	 * Add the "My Site" menu item in the root default group.
806
	 *
807
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
808
	 */
809
	public function add_my_sites_submenu( $wp_admin_bar ) {
810
		$current_user = wp_get_current_user();
811
812
		$blog_name = get_bloginfo( 'name' );
813
		if ( empty( $blog_name ) ) {
814
			$blog_name = $this->primary_site_slug;
815
		}
816
817
		if ( mb_strlen( $blog_name ) > 20 ) {
818
			$blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
819
		}
820
821
		$wp_admin_bar->add_menu(
822
			array(
823
				'parent' => 'root-default',
824
				'id'     => 'blog',
825
				'title'  => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
826
				'href'   => '#',
827
				'meta'   => array(
828
					'class' => 'my-sites mb-trackable',
829
				),
830
			)
831
		);
832
833
		if ( $this->user_site_count > 1 ) {
834
			$wp_admin_bar->add_menu(
835
				array(
836
					'parent' => 'blog',
837
					'id'     => 'switch-site',
838
					'title'  => esc_html__( 'Switch Site', 'jetpack' ),
839
					'href'   => 'https://wordpress.com/sites',
840
				)
841
			);
842
		} else {
843
			$wp_admin_bar->add_menu(
844
				array(
845
					'parent' => 'blog',
846
					'id'     => 'new-site',
847
					'title'  => esc_html__( '+ Add New WordPress', 'jetpack' ),
848
					'href'   => 'https://wordpress.com/start?ref=admin-bar-logged-in',
849
				)
850
			);
851
		}
852
853
		if ( is_user_member_of_blog( $current_user->ID ) ) {
854
			$blavatar = '';
855
			$class    = 'current-site';
856
857
			if ( has_site_icon() ) {
858
				$src      = get_site_icon_url();
859
				$blavatar = '<img class="avatar" src="' . esc_attr( $src ) . '" alt="Current site avatar">';
860
				$class    = 'has-blavatar';
861
			}
862
863
			$blog_info  = '<div class="ab-site-icon">' . $blavatar . '</div>';
864
			$blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
865
			$blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
866
867
			$wp_admin_bar->add_menu(
868
				array(
869
					'parent' => 'blog',
870
					'id'     => 'blog-info',
871
					'title'  => $blog_info,
872
					'href'   => esc_url( trailingslashit( $this->primary_site_url ) ),
873
					'meta'   => array(
874
						'class' => $class,
875
					),
876
				)
877
			);
878
		}
879
880
		// Site Preview.
881
		if ( is_admin() ) {
882
			$wp_admin_bar->add_menu(
883
				array(
884
					'parent' => 'blog',
885
					'id'     => 'site-view',
886
					'title'  => __( 'View Site', 'jetpack' ),
887
					'href'   => home_url(),
888
					'meta'   => array(
889
						'class'  => 'mb-icon',
890
						'target' => '_blank',
891
					),
892
				)
893
			);
894
		}
895
896
		$this->add_my_home_submenu_item( $wp_admin_bar );
897
898
		// Stats.
899 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) && current_user_can( 'view_stats' ) ) {
900
			$wp_admin_bar->add_menu(
901
				array(
902
					'parent' => 'blog',
903
					'id'     => 'blog-stats',
904
					'title'  => esc_html__( 'Stats', 'jetpack' ),
905
					'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
906
					'meta'   => array(
907
						'class' => 'mb-icon',
908
					),
909
				)
910
			);
911
		}
912
913 View Code Duplication
		if ( current_user_can( 'manage_options' ) ) {
914
			$wp_admin_bar->add_menu(
915
				array(
916
					'parent' => 'blog',
917
					'id'     => 'activity',
918
					'title'  => esc_html__( 'Activity', 'jetpack' ),
919
					'href'   => 'https://wordpress.com/activity-log/' . esc_attr( $this->primary_site_slug ),
920
					'meta'   => array(
921
						'class' => 'mb-icon',
922
					),
923
				)
924
			);
925
		}
926
927
		// Add Calypso plans link and plan type indicator.
928
		if ( is_user_member_of_blog( $current_user->ID ) && current_user_can( 'manage_options' ) ) {
929
			$plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
930
			$label     = esc_html__( 'Plan', 'jetpack' );
931
			$plan      = Jetpack_Plan::get();
932
933
			$plan_title = $this->create_menu_item_pair(
934
				array(
935
					'url'   => $plans_url,
936
					'id'    => 'wp-admin-bar-plan',
937
					'label' => $label,
938
				),
939
				array(
940
					'url'   => $plans_url,
941
					'id'    => 'wp-admin-bar-plan-badge',
942
					'label' => $plan['product_name_short'],
943
				)
944
			);
945
946
			$wp_admin_bar->add_menu(
947
				array(
948
					'parent' => 'blog',
949
					'id'     => 'plan',
950
					'title'  => $plan_title,
951
					'meta'   => array(
952
						'class' => 'inline-action',
953
					),
954
				)
955
			);
956
		}
957
958
		// Publish group.
959
		$wp_admin_bar->add_group(
960
			array(
961
				'parent' => 'blog',
962
				'id'     => 'publish',
963
			)
964
		);
965
966
		// Publish header.
967
		$wp_admin_bar->add_menu(
968
			array(
969
				'parent' => 'publish',
970
				'id'     => 'publish-header',
971
				'title'  => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ),
972
				'meta'   => array(
973
					'class' => 'ab-submenu-header',
974
				),
975
			)
976
		);
977
978
		// Pages.
979
		$pages_title = $this->create_menu_item_pair(
980
			array(
981
				'url'   => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
982
				'id'    => 'wp-admin-bar-edit-page',
983
				'label' => esc_html__( 'Site Pages', 'jetpack' ),
984
			),
985
			array(
986
				'url'   => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
987
				'id'    => 'wp-admin-bar-new-page-badge',
988
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
989
			)
990
		);
991
992
		if ( ! current_user_can( 'edit_pages' ) ) {
993
			$pages_title = $this->create_menu_item_anchor(
994
				'ab-item ab-primary mb-icon',
995
				'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
996
				esc_html__( 'Site Pages', 'jetpack' ),
997
				'wp-admin-bar-edit-page'
998
			);
999
		}
1000
1001
		$wp_admin_bar->add_menu(
1002
			array(
1003
				'parent' => 'publish',
1004
				'id'     => 'new-page',
1005
				'title'  => $pages_title,
1006
				'meta'   => array(
1007
					'class' => 'inline-action',
1008
				),
1009
			)
1010
		);
1011
1012
		// Blog Posts.
1013
		$posts_title = $this->create_menu_item_pair(
1014
			array(
1015
				'url'   => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
1016
				'id'    => 'wp-admin-bar-edit-post',
1017
				'label' => esc_html__( 'Blog Posts', 'jetpack' ),
1018
			),
1019
			array(
1020
				'url'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
1021
				'id'    => 'wp-admin-bar-new-post-badge',
1022
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
1023
			)
1024
		);
1025
1026
		if ( ! current_user_can( 'edit_posts' ) ) {
1027
			$posts_title = $this->create_menu_item_anchor(
1028
				'ab-item ab-primary mb-icon',
1029
				'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
1030
				esc_html__( 'Blog Posts', 'jetpack' ),
1031
				'wp-admin-bar-edit-post'
1032
			);
1033
		}
1034
1035
		$wp_admin_bar->add_menu(
1036
			array(
1037
				'parent' => 'publish',
1038
				'id'     => 'new-post',
1039
				'title'  => $posts_title,
1040
				'meta'   => array(
1041
					'class' => 'inline-action mb-trackable',
1042
				),
1043
			)
1044
		);
1045
1046
		// Comments.
1047 View Code Duplication
		if ( current_user_can( 'moderate_comments' ) ) {
1048
			$wp_admin_bar->add_menu(
1049
				array(
1050
					'parent' => 'publish',
1051
					'id'     => 'comments',
1052
					'title'  => __( 'Comments', 'jetpack' ),
1053
					'href'   => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ),
1054
					'meta'   => array(
1055
						'class' => 'mb-icon',
1056
					),
1057
				)
1058
			);
1059
		}
1060
1061
		// Testimonials.
1062 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
1063
			$testimonials_title = $this->create_menu_item_pair(
1064
				array(
1065
					'url'   => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1066
					'id'    => 'wp-admin-bar-edit-testimonial',
1067
					'label' => esc_html__( 'Testimonials', 'jetpack' ),
1068
				),
1069
				array(
1070
					'url'   => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1071
					'id'    => 'wp-admin-bar-new-testimonial',
1072
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1073
				)
1074
			);
1075
1076
			if ( ! current_user_can( 'edit_pages' ) ) {
1077
				$testimonials_title = $this->create_menu_item_anchor(
1078
					'ab-item ab-primary mb-icon',
1079
					'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1080
					esc_html__( 'Testimonials', 'jetpack' ),
1081
					'wp-admin-bar-edit-testimonial'
1082
				);
1083
			}
1084
1085
			$wp_admin_bar->add_menu(
1086
				array(
1087
					'parent' => 'publish',
1088
					'id'     => 'new-jetpack-testimonial',
1089
					'title'  => $testimonials_title,
1090
					'meta'   => array(
1091
						'class' => 'inline-action',
1092
					),
1093
				)
1094
			);
1095
		}
1096
1097
		// Portfolio.
1098 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
1099
			$portfolios_title = $this->create_menu_item_pair(
1100
				array(
1101
					'url'   => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1102
					'id'    => 'wp-admin-bar-edit-portfolio',
1103
					'label' => esc_html__( 'Portfolio', 'jetpack' ),
1104
				),
1105
				array(
1106
					'url'   => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1107
					'id'    => 'wp-admin-bar-new-portfolio',
1108
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1109
				)
1110
			);
1111
1112
			if ( ! current_user_can( 'edit_pages' ) ) {
1113
				$portfolios_title = $this->create_menu_item_anchor(
1114
					'ab-item ab-primary mb-icon',
1115
					'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1116
					esc_html__( 'Portfolio', 'jetpack' ),
1117
					'wp-admin-bar-edit-portfolio'
1118
				);
1119
			}
1120
1121
			$wp_admin_bar->add_menu(
1122
				array(
1123
					'parent' => 'publish',
1124
					'id'     => 'new-jetpack-portfolio',
1125
					'title'  => $portfolios_title,
1126
					'meta'   => array(
1127
						'class' => 'inline-action',
1128
					),
1129
				)
1130
			);
1131
		}
1132
1133
		if ( current_user_can( 'edit_theme_options' ) ) {
1134
			// Look and Feel group.
1135
			$wp_admin_bar->add_group(
1136
				array(
1137
					'parent' => 'blog',
1138
					'id'     => 'look-and-feel',
1139
				)
1140
			);
1141
1142
			// Look and Feel header.
1143
			$wp_admin_bar->add_menu(
1144
				array(
1145
					'parent' => 'look-and-feel',
1146
					'id'     => 'look-and-feel-header',
1147
					'title'  => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
1148
					'meta'   => array(
1149
						'class' => 'ab-submenu-header',
1150
					),
1151
				)
1152
			);
1153
1154
			if ( is_admin() ) {
1155
				// In wp-admin the `return` query arg will return to that page after closing the Customizer.
1156
				$customizer_url = add_query_arg(
1157
					array(
1158
						'return' => rawurlencode( site_url( $_SERVER['REQUEST_URI'] ) ),
1159
					),
1160
					wp_customize_url()
1161
				);
1162
			} else {
1163
				/*
1164
				 * On the frontend the `url` query arg will load that page in the Customizer
1165
				 * and also return to it after closing
1166
				 * non-home URLs won't work unless we undo domain mapping
1167
				 * since the Customizer preview is unmapped to always have HTTPS.
1168
				 */
1169
				$current_page   = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
1170
				$customizer_url = add_query_arg( array( 'url' => rawurlencode( $current_page ) ), wp_customize_url() );
1171
			}
1172
1173
			$theme_title = $this->create_menu_item_pair(
1174
				array(
1175
					'url'   => $customizer_url,
1176
					'id'    => 'wp-admin-bar-cmz',
1177
					'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ),
1178
				),
1179
				array(
1180
					'url'   => 'https://wordpress.com/themes/' . esc_attr( $this->primary_site_slug ),
1181
					'id'    => 'wp-admin-bar-themes',
1182
					'label' => esc_html__( 'Themes', 'jetpack' ),
1183
				)
1184
			);
1185
			$meta        = array(
1186
				'class' => 'mb-icon',
1187
				'class' => 'inline-action',
1188
			);
1189
			$href        = false;
1190
1191
			$wp_admin_bar->add_menu(
1192
				array(
1193
					'parent' => 'look-and-feel',
1194
					'id'     => 'themes',
1195
					'title'  => $theme_title,
1196
					'href'   => $href,
1197
					'meta'   => $meta,
1198
				)
1199
			);
1200
		}
1201
1202
		if ( current_user_can( 'manage_options' ) ) {
1203
			// Configuration group.
1204
			$wp_admin_bar->add_group(
1205
				array(
1206
					'parent' => 'blog',
1207
					'id'     => 'configuration',
1208
				)
1209
			);
1210
1211
			// Configuration header.
1212
			$wp_admin_bar->add_menu(
1213
				array(
1214
					'parent' => 'configuration',
1215
					'id'     => 'configuration-header',
1216
					'title'  => esc_html_x( 'Configure', 'admin bar menu group label', 'jetpack' ),
1217
					'meta'   => array(
1218
						'class' => 'ab-submenu-header',
1219
					),
1220
				)
1221
			);
1222
1223 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
1224
				$wp_admin_bar->add_menu(
1225
					array(
1226
						'parent' => 'configuration',
1227
						'id'     => 'sharing',
1228
						'title'  => esc_html__( 'Sharing', 'jetpack' ),
1229
						'href'   => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
1230
						'meta'   => array(
1231
							'class' => 'mb-icon',
1232
						),
1233
					)
1234
				);
1235
			}
1236
1237
			$people_title = $this->create_menu_item_pair(
1238
				array(
1239
					'url'   => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
1240
					'id'    => 'wp-admin-bar-people',
1241
					'label' => esc_html__( 'People', 'jetpack' ),
1242
				),
1243
				array(
1244
					'url'   => admin_url( 'user-new.php' ),
1245
					'id'    => 'wp-admin-bar-people-add',
1246
					'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ),
1247
				)
1248
			);
1249
1250
			$wp_admin_bar->add_menu(
1251
				array(
1252
					'parent' => 'configuration',
1253
					'id'     => 'users-toolbar',
1254
					'title'  => $people_title,
1255
					'href'   => false,
1256
					'meta'   => array(
1257
						'class' => 'inline-action',
1258
					),
1259
				)
1260
			);
1261
1262
			$plugins_title = $this->create_menu_item_pair(
1263
				array(
1264
					'url'   => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
1265
					'id'    => 'wp-admin-bar-plugins',
1266
					'label' => esc_html__( 'Plugins', 'jetpack' ),
1267
				),
1268
				array(
1269
					'url'   => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ),
1270
					'id'    => 'wp-admin-bar-plugins-add',
1271
					'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ),
1272
				)
1273
			);
1274
1275
			$wp_admin_bar->add_menu(
1276
				array(
1277
					'parent' => 'configuration',
1278
					'id'     => 'plugins',
1279
					'title'  => $plugins_title,
1280
					'href'   => false,
1281
					'meta'   => array(
1282
						'class' => 'inline-action',
1283
					),
1284
				)
1285
			);
1286
1287
			if ( jetpack_is_atomic_site() ) {
1288
				$domain_title = $this->create_menu_item_pair(
1289
					array(
1290
						'url'   => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
1291
						'id'    => 'wp-admin-bar-domains',
1292
						'label' => esc_html__( 'Domains', 'jetpack' ),
1293
					),
1294
					array(
1295
						'url'   => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
1296
						'id'    => 'wp-admin-bar-domains-add',
1297
						'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
1298
					)
1299
				);
1300
				$wp_admin_bar->add_menu(
1301
					array(
1302
						'parent' => 'configuration',
1303
						'id'     => 'domains',
1304
						'title'  => $domain_title,
1305
						'href'   => false,
1306
						'meta'   => array(
1307
							'class' => 'inline-action',
1308
						),
1309
					)
1310
				);
1311
			}
1312
1313
			$wp_admin_bar->add_menu(
1314
				array(
1315
					'parent' => 'configuration',
1316
					'id'     => 'blog-settings',
1317
					'title'  => esc_html__( 'Settings', 'jetpack' ),
1318
					'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
1319
					'meta'   => array(
1320
						'class' => 'mb-icon',
1321
					),
1322
				)
1323
			);
1324
1325
			if ( ! is_admin() ) {
1326
				$wp_admin_bar->add_menu(
1327
					array(
1328
						'parent' => 'configuration',
1329
						'id'     => 'legacy-dashboard',
1330
						'title'  => esc_html__( 'Dashboard', 'jetpack' ),
1331
						'href'   => admin_url(),
1332
						'meta'   => array(
1333
							'class' => 'mb-icon',
1334
						),
1335
					)
1336
				);
1337
			}
1338
1339
			// Restore dashboard menu toggle that is needed on mobile views.
1340
			if ( is_admin() ) {
1341
				$wp_admin_bar->add_menu(
1342
					array(
1343
						'id'    => 'menu-toggle',
1344
						'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>',
1345
						'href'  => '#',
1346
					)
1347
				);
1348
			}
1349
1350
			/**
1351
			 * Fires when menu items are added to the masterbar "My Sites" menu.
1352
			 *
1353
			 * @since 5.4.0
1354
			 */
1355
			do_action( 'jetpack_masterbar' );
1356
		}
1357
	}
1358
1359
	/**
1360
	 * Adds "My Home" submenu item to sites that are eligible.
1361
	 *
1362
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
1363
	 * @return void
1364
	 */
1365
	private function add_my_home_submenu_item( &$wp_admin_bar ) {
1366
		if ( ! current_user_can( 'manage_options' ) || ! jetpack_is_atomic_site() ) {
1367
			return;
1368
		}
1369
1370
		$wp_admin_bar->add_menu(
1371
			array(
1372
				'parent' => 'blog',
1373
				'id'     => 'my-home',
1374
				'title'  => __( 'My Home', 'jetpack' ),
1375
				'href'   => 'https://wordpress.com/home/' . esc_attr( $this->primary_site_slug ),
1376
				'meta'   => array(
1377
					'class' => 'mb-icon',
1378
				),
1379
			)
1380
		);
1381
	}
1382
}
1383