Completed
Push — update/travis-74 ( 386fcf...69a56a )
by
unknown
224:04 queued 215:38
created

Masterbar   F

Complexity

Total Complexity 85

Size/Duplication

Total Lines 1354
Duplicated Lines 7.39 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 100
loc 1354
rs 0.8
c 0
b 0
f 0
wmc 85
lcom 1
cbo 11

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B init() 0 74 8
B maybe_logout_user_from_wpcom() 0 39 6
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 26 4
A get_locale() 6 18 6
A add_notifications() 0 23 2
B add_reader_submenu() 0 87 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() 94 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 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 Masterbar, and based on these observations, apply Extract Interface, too.

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