Completed
Push — update/recurring-payments-canv... ( dc9c98...30355e )
by Jeremy
54:47 queued 46:34
created

Masterbar::wpcom_adminbar_add_secondary_groups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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