Completed
Push — remove/rtl-admin-bar ( c11519 )
by
unknown
42:42 queued 34:21
created

Masterbar::init()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 7
nop 0
dl 0
loc 74
rs 7.3228
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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