Completed
Push — add/podcast-episode-selector--... ( c034f2...c45f74 )
by
unknown
09:58
created

Masterbar::maybe_logout_user_from_wpcom()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 3
dl 0
loc 39
rs 8.6737
c 0
b 0
f 0
1
<?php
2
/**
3
 * Masterbar file.
4
 *
5
 * @package automattic/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 );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Jetpack::get_connected_user_data($this->user_id) of type * is incompatible with the declared type array of property $user_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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/masterbar/tracks-events.min.js',
292
				'modules/masterbar/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'   => 'https://wordpress.com/read',
445
				'meta'   => array(
446
					'class' => 'mb-trackable',
447
				),
448
			)
449
		);
450
451
		/** This filter is documented in modules/masterbar.php */
452
		if ( apply_filters( 'jetpack_load_admin_menu_class', false ) ) {
453
			return;
454
		}
455
456
		$wp_admin_bar->add_menu(
457
			array(
458
				'parent' => 'newdash',
459
				'id'     => 'streams-header',
460
				'title'  => esc_html_x(
461
					'Streams',
462
					'Title for Reader sub-menu that contains followed sites, likes, and search',
463
					'jetpack'
464
				),
465
				'meta'   => array(
466
					'class' => 'ab-submenu-header',
467
				),
468
			)
469
		);
470
471
		$following_title = $this->create_menu_item_pair(
472
			array(
473
				'url'   => Redirect::get_url( 'calypso-read' ),
474
				'id'    => 'wp-admin-bar-followed-sites',
475
				'label' => esc_html__( 'Followed Sites', 'jetpack' ),
476
			),
477
			array(
478
				'url'   => Redirect::get_url( 'calypso-following-edit' ),
479
				'id'    => 'wp-admin-bar-reader-followed-sites-manage',
480
				'label' => esc_html__( 'Manage', 'jetpack' ),
481
			)
482
		);
483
484
		$wp_admin_bar->add_menu(
485
			array(
486
				'parent' => 'newdash',
487
				'id'     => 'following',
488
				'title'  => $following_title,
489
				'meta'   => array( 'class' => 'inline-action' ),
490
			)
491
		);
492
493
		$wp_admin_bar->add_menu(
494
			array(
495
				'parent' => 'newdash',
496
				'id'     => 'discover-discover',
497
				'title'  => esc_html__( 'Discover', 'jetpack' ),
498
				'href'   => Redirect::get_url( 'calypso-discover' ),
499
				'meta'   => array(
500
					'class' => 'mb-icon-spacer',
501
				),
502
			)
503
		);
504
505
		$wp_admin_bar->add_menu(
506
			array(
507
				'parent' => 'newdash',
508
				'id'     => 'discover-search',
509
				'title'  => esc_html__( 'Search', 'jetpack' ),
510
				'href'   => Redirect::get_url( 'calypso-read-search' ),
511
				'meta'   => array(
512
					'class' => 'mb-icon-spacer',
513
				),
514
			)
515
		);
516
517
		$wp_admin_bar->add_menu(
518
			array(
519
				'parent' => 'newdash',
520
				'id'     => 'my-activity-my-likes',
521
				'title'  => esc_html__( 'My Likes', 'jetpack' ),
522
				'href'   => Redirect::get_url( 'calypso-activities-likes' ),
523
				'meta'   => array(
524
					'class' => 'mb-icon-spacer',
525
				),
526
			)
527
		);
528
529
	}
530
531
	/**
532
	 * Merge 2 menu items together into 2 link tags.
533
	 *
534
	 * @param array $primary   Array of menu information.
535
	 * @param array $secondary Array of menu information.
536
	 */
537
	public function create_menu_item_pair( $primary, $secondary ) {
538
		$primary_class   = 'ab-item ab-primary mb-icon';
539
		$secondary_class = 'ab-secondary';
540
541
		$primary_anchor   = $this->create_menu_item_anchor( $primary_class, $primary['url'], $primary['label'], $primary['id'] );
542
		$secondary_anchor = $this->create_menu_item_anchor( $secondary_class, $secondary['url'], $secondary['label'], $secondary['id'] );
543
544
		return $primary_anchor . $secondary_anchor;
545
	}
546
547
	/**
548
	 * Create a link tag based on information about a menu item.
549
	 *
550
	 * @param string $class Menu item CSS class.
551
	 * @param string $url   URL you go to when clicking on the menu item.
552
	 * @param string $label Menu item title.
553
	 * @param string $id    Menu item slug.
554
	 */
555
	public function create_menu_item_anchor( $class, $url, $label, $id ) {
556
		return '<a href="' . $url . '" class="' . $class . '" id="' . $id . '">' . $label . '</a>';
557
	}
558
559
	/**
560
	 * Add Secondary groups for submenu items.
561
	 *
562
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
563
	 */
564
	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
565
		$wp_admin_bar->add_group(
566
			array(
567
				'id'   => 'root-default',
568
				'meta' => array(
569
					'class' => 'ab-top-menu',
570
				),
571
			)
572
		);
573
574
		$wp_admin_bar->add_group(
575
			array(
576
				'parent' => 'blog',
577
				'id'     => 'blog-secondary',
578
				'meta'   => array(
579
					'class' => 'ab-sub-secondary',
580
				),
581
			)
582
		);
583
584
		$wp_admin_bar->add_group(
585
			array(
586
				'id'   => 'top-secondary',
587
				'meta' => array(
588
					'class' => 'ab-top-secondary',
589
				),
590
			)
591
		);
592
	}
593
594
	/**
595
	 * Add User info menu item.
596
	 *
597
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
598
	 */
599
	public function add_me_submenu( $wp_admin_bar ) {
600
		$user_id = get_current_user_id();
601
		if ( empty( $user_id ) ) {
602
			return;
603
		}
604
605
		$avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
606
		$class  = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
607
608
		// Add the 'Me' menu.
609
		$wp_admin_bar->add_menu(
610
			array(
611
				'id'     => 'my-account',
612
				'parent' => 'top-secondary',
613
				'title'  => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
614
				'href'   => 'https://wordpress.com/me',
615
				'meta'   => array(
616
					'class' => $class,
617
				),
618
			)
619
		);
620
621
		/** This filter is documented in modules/masterbar.php */
622
		if ( apply_filters( 'jetpack_load_admin_menu_class', false ) ) {
623
			return;
624
		}
625
626
		$id = 'user-actions';
627
		$wp_admin_bar->add_group(
628
			array(
629
				'parent' => 'my-account',
630
				'id'     => $id,
631
			)
632
		);
633
634
		$settings_url = Redirect::get_url( 'calypso-me-account' );
635
636
		$logout_url = wp_logout_url();
637
		$logout_url = add_query_arg( 'context', 'masterbar', $logout_url );
638
639
		$user_info  = get_avatar( $this->user_email, 128, 'mm', '', array( 'force_display' => true ) );
640
		$user_info .= '<span class="display-name">' . $this->display_name . '</span>';
641
		$user_info .= '<a class="username" href="https://gravatar.com/' . $this->user_login . '">@' . $this->user_login . '</a>';
642
643
		$user_info .= sprintf(
644
			'<div><a href="%s" class="ab-sign-out">%s</a></div>',
645
			$logout_url,
646
			esc_html__( 'Sign Out', 'jetpack' )
647
		);
648
649
		$wp_admin_bar->add_menu(
650
			array(
651
				'parent' => $id,
652
				'id'     => 'user-info',
653
				'title'  => $user_info,
654
				'meta'   => array(
655
					'class'    => 'user-info user-info-item',
656
					'tabindex' => -1,
657
				),
658
			)
659
		);
660
661
		$wp_admin_bar->add_menu(
662
			array(
663
				'parent' => $id,
664
				'id'     => 'profile-header',
665
				'title'  => esc_html__( 'Profile', 'jetpack' ),
666
				'meta'   => array(
667
					'class' => 'ab-submenu-header',
668
				),
669
			)
670
		);
671
672
		$wp_admin_bar->add_menu(
673
			array(
674
				'parent' => $id,
675
				'id'     => 'my-profile',
676
				'title'  => esc_html__( 'My Profile', 'jetpack' ),
677
				'href'   => Redirect::get_url( 'calypso-me' ),
678
				'meta'   => array(
679
					'class' => 'mb-icon',
680
				),
681
			)
682
		);
683
684
		$wp_admin_bar->add_menu(
685
			array(
686
				'parent' => $id,
687
				'id'     => 'account-settings',
688
				'title'  => esc_html__( 'Account Settings', 'jetpack' ),
689
				'href'   => $settings_url,
690
				'meta'   => array(
691
					'class' => 'mb-icon',
692
				),
693
			)
694
		);
695
696
		$wp_admin_bar->add_menu(
697
			array(
698
				'parent' => $id,
699
				'id'     => 'billing',
700
				'title'  => esc_html__( 'Manage Purchases', 'jetpack' ),
701
				'href'   => Redirect::get_url( 'calypso-me-purchases' ),
702
				'meta'   => array(
703
					'class' => 'mb-icon',
704
				),
705
			)
706
		);
707
708
		$wp_admin_bar->add_menu(
709
			array(
710
				'parent' => $id,
711
				'id'     => 'security',
712
				'title'  => esc_html__( 'Security', 'jetpack' ),
713
				'href'   => Redirect::get_url( 'calypso-me-security' ),
714
				'meta'   => array(
715
					'class' => 'mb-icon',
716
				),
717
			)
718
		);
719
720
		$wp_admin_bar->add_menu(
721
			array(
722
				'parent' => $id,
723
				'id'     => 'notifications',
724
				'title'  => esc_html__( 'Notifications', 'jetpack' ),
725
				'href'   => Redirect::get_url( 'calypso-me-notifications' ),
726
				'meta'   => array(
727
					'class' => 'mb-icon',
728
				),
729
			)
730
		);
731
732
		$wp_admin_bar->add_menu(
733
			array(
734
				'parent' => $id,
735
				'id'     => 'special-header',
736
				'title'  => esc_html_x(
737
					'Special',
738
					'Title for Me sub-menu that contains Get Apps, Next Steps, and Help options',
739
					'jetpack'
740
				),
741
				'meta'   => array(
742
					'class' => 'ab-submenu-header',
743
				),
744
			)
745
		);
746
747
		$wp_admin_bar->add_menu(
748
			array(
749
				'parent' => $id,
750
				'id'     => 'get-apps',
751
				'title'  => esc_html__( 'Get Apps', 'jetpack' ),
752
				'href'   => Redirect::get_url( 'calypso-me-get-apps' ),
753
				'meta'   => array(
754
					'class' => 'mb-icon user-info-item',
755
				),
756
			)
757
		);
758
759
		$help_link = Redirect::get_url( 'jetpack-support' );
760
761
		if ( jetpack_is_atomic_site() ) {
762
			$help_link = Redirect::get_url( 'calypso-help' );
763
		}
764
765
		$wp_admin_bar->add_menu(
766
			array(
767
				'parent' => $id,
768
				'id'     => 'help',
769
				'title'  => esc_html__( 'Help', 'jetpack' ),
770
				'href'   => $help_link,
771
				'meta'   => array(
772
					'class' => 'mb-icon user-info-item',
773
				),
774
			)
775
		);
776
	}
777
778
	/**
779
	 * Add Write Menu item.
780
	 *
781
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
782
	 */
783
	public function add_write_button( $wp_admin_bar ) {
784
		$current_user = wp_get_current_user();
785
786
		$posting_blog_id = get_current_blog_id();
787
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
788
			$posting_blog_id = $current_user->primary_blog;
789
		}
790
791
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
792
793
		if ( ! $posting_blog_id || ! $user_can_post ) {
794
			return;
795
		}
796
797
		$blog_post_page = Redirect::get_url( 'calypso-edit-post' );
798
799
		$wp_admin_bar->add_menu(
800
			array(
801
				'parent' => 'top-secondary',
802
				'id'     => 'ab-new-post',
803
				'href'   => $blog_post_page,
804
				'title'  => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
805
				'meta'   => array(
806
					'class' => 'mb-trackable',
807
				),
808
			)
809
		);
810
	}
811
812
	/**
813
	 * Add the "My Site" menu item in the root default group.
814
	 *
815
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
816
	 */
817
	public function add_my_sites_submenu( $wp_admin_bar ) {
818
		$current_user = wp_get_current_user();
819
820
		$blog_name = get_bloginfo( 'name' );
821
		if ( empty( $blog_name ) ) {
822
			$blog_name = $this->primary_site_slug;
823
		}
824
825
		if ( mb_strlen( $blog_name ) > 20 ) {
826
			$blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
827
		}
828
829
		$wp_admin_bar->add_menu(
830
			array(
831
				'parent' => 'root-default',
832
				'id'     => 'blog',
833
				'title'  => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
834
				'href'   => 'https://wordpress.com/sites/' . $this->primary_site_url,
835
				'meta'   => array(
836
					'class' => 'my-sites mb-trackable',
837
				),
838
			)
839
		);
840
841
		/** This filter is documented in modules/masterbar.php */
842
		if ( apply_filters( 'jetpack_load_admin_menu_class', false ) ) {
843
			return;
844
		}
845
846
		if ( $this->user_site_count > 1 ) {
847
			$wp_admin_bar->add_menu(
848
				array(
849
					'parent' => 'blog',
850
					'id'     => 'switch-site',
851
					'title'  => esc_html__( 'Switch Site', 'jetpack' ),
852
					'href'   => Redirect::get_url( 'calypso-sites' ),
853
				)
854
			);
855
		} else {
856
			$wp_admin_bar->add_menu(
857
				array(
858
					'parent' => 'blog',
859
					'id'     => 'new-site',
860
					'title'  => esc_html__( '+ Add New WordPress', 'jetpack' ),
861
					'href'   => Redirect::get_url( 'calypso-start', array( 'query' => 'ref=admin-bar-logged-in' ) ),
862
				)
863
			);
864
		}
865
866
		if ( is_user_member_of_blog( $current_user->ID ) ) {
867
			$blavatar = '';
868
			$class    = 'current-site';
869
870
			if ( has_site_icon() ) {
871
				$src      = get_site_icon_url();
872
				$blavatar = '<img class="avatar" src="' . esc_attr( $src ) . '" alt="Current site avatar">';
873
				$class    = 'has-blavatar';
874
			}
875
876
			$blog_info  = '<div class="ab-site-icon">' . $blavatar . '</div>';
877
			$blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
878
			$blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
879
880
			$wp_admin_bar->add_menu(
881
				array(
882
					'parent' => 'blog',
883
					'id'     => 'blog-info',
884
					'title'  => $blog_info,
885
					'href'   => esc_url( trailingslashit( $this->primary_site_url ) ),
886
					'meta'   => array(
887
						'class' => $class,
888
					),
889
				)
890
			);
891
		}
892
893
		// Site Preview.
894
		if ( is_admin() ) {
895
			$wp_admin_bar->add_menu(
896
				array(
897
					'parent' => 'blog',
898
					'id'     => 'site-view',
899
					'title'  => __( 'View Site', 'jetpack' ),
900
					'href'   => home_url(),
901
					'meta'   => array(
902
						'class'  => 'mb-icon',
903
						'target' => '_blank',
904
					),
905
				)
906
			);
907
		}
908
909
		$this->add_my_home_submenu_item( $wp_admin_bar );
910
911
		// Stats.
912 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) && current_user_can( 'view_stats' ) ) {
913
			$wp_admin_bar->add_menu(
914
				array(
915
					'parent' => 'blog',
916
					'id'     => 'blog-stats',
917
					'title'  => esc_html__( 'Stats', 'jetpack' ),
918
					'href'   => Redirect::get_url( 'calypso-stats' ),
919
					'meta'   => array(
920
						'class' => 'mb-icon',
921
					),
922
				)
923
			);
924
		}
925
926
		if ( current_user_can( 'manage_options' ) ) {
927
			$wp_admin_bar->add_menu(
928
				array(
929
					'parent' => 'blog',
930
					'id'     => 'activity',
931
					'title'  => esc_html__( 'Activity', 'jetpack' ),
932
					'href'   => Redirect::get_url( 'calypso-activity-log' ),
933
					'meta'   => array(
934
						'class' => 'mb-icon',
935
					),
936
				)
937
			);
938
		}
939
940
		// Add Calypso plans link and plan type indicator.
941
		if ( is_user_member_of_blog( $current_user->ID ) && current_user_can( 'manage_options' ) ) {
942
			$plans_url = Redirect::get_url( 'calypso-plans' );
943
			$label     = esc_html__( 'Plan', 'jetpack' );
944
			$plan      = Jetpack_Plan::get();
945
946
			$plan_title = $this->create_menu_item_pair(
947
				array(
948
					'url'   => $plans_url,
949
					'id'    => 'wp-admin-bar-plan',
950
					'label' => $label,
951
				),
952
				array(
953
					'url'   => $plans_url,
954
					'id'    => 'wp-admin-bar-plan-badge',
955
					'label' => ! empty( $plan['product_name_short'] ) ? $plan['product_name_short'] : esc_html__( 'Free', 'jetpack' ),
956
				)
957
			);
958
959
			$wp_admin_bar->add_menu(
960
				array(
961
					'parent' => 'blog',
962
					'id'     => 'plan',
963
					'title'  => $plan_title,
964
					'meta'   => array(
965
						'class' => 'inline-action',
966
					),
967
				)
968
			);
969
		}
970
971
		// Publish group.
972
		$wp_admin_bar->add_group(
973
			array(
974
				'parent' => 'blog',
975
				'id'     => 'publish',
976
			)
977
		);
978
979
		// Publish header.
980
		$wp_admin_bar->add_menu(
981
			array(
982
				'parent' => 'publish',
983
				'id'     => 'publish-header',
984
				'title'  => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ),
985
				'meta'   => array(
986
					'class' => 'ab-submenu-header',
987
				),
988
			)
989
		);
990
991
		// Pages.
992
		$pages_title = $this->create_menu_item_pair(
993
			array(
994
				'url'   => Redirect::get_url( 'calypso-edit-pages' ),
995
				'id'    => 'wp-admin-bar-edit-page',
996
				'label' => esc_html__( 'Site Pages', 'jetpack' ),
997
			),
998
			array(
999
				'url'   => Redirect::get_url( 'calypso-edit-page' ),
1000
				'id'    => 'wp-admin-bar-new-page-badge',
1001
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
1002
			)
1003
		);
1004
1005
		if ( ! current_user_can( 'edit_pages' ) ) {
1006
			$pages_title = $this->create_menu_item_anchor(
1007
				'ab-item ab-primary mb-icon',
1008
				Redirect::get_url( 'calypso-edit-pages' ),
1009
				esc_html__( 'Site Pages', 'jetpack' ),
1010
				'wp-admin-bar-edit-page'
1011
			);
1012
		}
1013
1014
		$wp_admin_bar->add_menu(
1015
			array(
1016
				'parent' => 'publish',
1017
				'id'     => 'new-page',
1018
				'title'  => $pages_title,
1019
				'meta'   => array(
1020
					'class' => 'inline-action',
1021
				),
1022
			)
1023
		);
1024
1025
		// Blog Posts.
1026
		$posts_title = $this->create_menu_item_pair(
1027
			array(
1028
				'url'   => Redirect::get_url( 'calypso-edit-posts' ),
1029
				'id'    => 'wp-admin-bar-edit-post',
1030
				'label' => esc_html__( 'Blog Posts', 'jetpack' ),
1031
			),
1032
			array(
1033
				'url'   => Redirect::get_url( 'calypso-edit-post' ),
1034
				'id'    => 'wp-admin-bar-new-post-badge',
1035
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
1036
			)
1037
		);
1038
1039
		if ( ! current_user_can( 'edit_posts' ) ) {
1040
			$posts_title = $this->create_menu_item_anchor(
1041
				'ab-item ab-primary mb-icon',
1042
				Redirect::get_url( 'calypso-edit-posts' ),
1043
				esc_html__( 'Blog Posts', 'jetpack' ),
1044
				'wp-admin-bar-edit-post'
1045
			);
1046
		}
1047
1048
		$wp_admin_bar->add_menu(
1049
			array(
1050
				'parent' => 'publish',
1051
				'id'     => 'new-post',
1052
				'title'  => $posts_title,
1053
				'meta'   => array(
1054
					'class' => 'inline-action mb-trackable',
1055
				),
1056
			)
1057
		);
1058
1059
		// Comments.
1060
		if ( current_user_can( 'moderate_comments' ) ) {
1061
			$wp_admin_bar->add_menu(
1062
				array(
1063
					'parent' => 'publish',
1064
					'id'     => 'comments',
1065
					'title'  => __( 'Comments', 'jetpack' ),
1066
					'href'   => Redirect::get_url( 'calypso-comments' ),
1067
					'meta'   => array(
1068
						'class' => 'mb-icon',
1069
					),
1070
				)
1071
			);
1072
		}
1073
1074
		// Testimonials.
1075 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
1076
			$testimonials_title = $this->create_menu_item_pair(
1077
				array(
1078
					'url'   => Redirect::get_url( 'calypso-list-jetpack-testimonial' ),
1079
					'id'    => 'wp-admin-bar-edit-testimonial',
1080
					'label' => esc_html__( 'Testimonials', 'jetpack' ),
1081
				),
1082
				array(
1083
					'url'   => Redirect::get_url( 'calypso-edit-jetpack-testimonial' ),
1084
					'id'    => 'wp-admin-bar-new-testimonial',
1085
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1086
				)
1087
			);
1088
1089
			if ( ! current_user_can( 'edit_pages' ) ) {
1090
				$testimonials_title = $this->create_menu_item_anchor(
1091
					'ab-item ab-primary mb-icon',
1092
					Redirect::get_url( 'calypso-list-jetpack-testimonial' ),
1093
					esc_html__( 'Testimonials', 'jetpack' ),
1094
					'wp-admin-bar-edit-testimonial'
1095
				);
1096
			}
1097
1098
			$wp_admin_bar->add_menu(
1099
				array(
1100
					'parent' => 'publish',
1101
					'id'     => 'new-jetpack-testimonial',
1102
					'title'  => $testimonials_title,
1103
					'meta'   => array(
1104
						'class' => 'inline-action',
1105
					),
1106
				)
1107
			);
1108
		}
1109
1110
		// Portfolio.
1111 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
1112
			$portfolios_title = $this->create_menu_item_pair(
1113
				array(
1114
					'url'   => Redirect::get_url( 'calypso-list-jetpack-portfolio' ),
1115
					'id'    => 'wp-admin-bar-edit-portfolio',
1116
					'label' => esc_html__( 'Portfolio', 'jetpack' ),
1117
				),
1118
				array(
1119
					'url'   => Redirect::get_url( 'calypso-edit-jetpack-portfolio' ),
1120
					'id'    => 'wp-admin-bar-new-portfolio',
1121
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1122
				)
1123
			);
1124
1125
			if ( ! current_user_can( 'edit_pages' ) ) {
1126
				$portfolios_title = $this->create_menu_item_anchor(
1127
					'ab-item ab-primary mb-icon',
1128
					Redirect::get_url( 'calypso-list-jetpack-portfolio' ),
1129
					esc_html__( 'Portfolio', 'jetpack' ),
1130
					'wp-admin-bar-edit-portfolio'
1131
				);
1132
			}
1133
1134
			$wp_admin_bar->add_menu(
1135
				array(
1136
					'parent' => 'publish',
1137
					'id'     => 'new-jetpack-portfolio',
1138
					'title'  => $portfolios_title,
1139
					'meta'   => array(
1140
						'class' => 'inline-action',
1141
					),
1142
				)
1143
			);
1144
		}
1145
1146
		if ( current_user_can( 'edit_theme_options' ) ) {
1147
			// Look and Feel group.
1148
			$wp_admin_bar->add_group(
1149
				array(
1150
					'parent' => 'blog',
1151
					'id'     => 'look-and-feel',
1152
				)
1153
			);
1154
1155
			// Look and Feel header.
1156
			$wp_admin_bar->add_menu(
1157
				array(
1158
					'parent' => 'look-and-feel',
1159
					'id'     => 'look-and-feel-header',
1160
					'title'  => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
1161
					'meta'   => array(
1162
						'class' => 'ab-submenu-header',
1163
					),
1164
				)
1165
			);
1166
1167
			if ( is_admin() ) {
1168
				// In wp-admin the `return` query arg will return to that page after closing the Customizer.
1169
				$customizer_url = add_query_arg(
1170
					array(
1171
						'return' => rawurlencode( site_url( $_SERVER['REQUEST_URI'] ) ),
1172
					),
1173
					wp_customize_url()
1174
				);
1175
			} else {
1176
				/*
1177
				 * On the frontend the `url` query arg will load that page in the Customizer
1178
				 * and also return to it after closing
1179
				 * non-home URLs won't work unless we undo domain mapping
1180
				 * since the Customizer preview is unmapped to always have HTTPS.
1181
				 */
1182
				$current_page   = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
1183
				$customizer_url = add_query_arg( array( 'url' => rawurlencode( $current_page ) ), wp_customize_url() );
1184
			}
1185
1186
			$theme_title = $this->create_menu_item_pair(
1187
				array(
1188
					'url'   => $customizer_url,
1189
					'id'    => 'wp-admin-bar-cmz',
1190
					'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ),
1191
				),
1192
				array(
1193
					'url'   => Redirect::get_url( 'calypso-themes' ),
1194
					'id'    => 'wp-admin-bar-themes',
1195
					'label' => esc_html__( 'Themes', 'jetpack' ),
1196
				)
1197
			);
1198
			$meta        = array(
1199
				'class' => 'mb-icon',
1200
				'class' => 'inline-action',
1201
			);
1202
			$href        = false;
1203
1204
			$wp_admin_bar->add_menu(
1205
				array(
1206
					'parent' => 'look-and-feel',
1207
					'id'     => 'themes',
1208
					'title'  => $theme_title,
1209
					'href'   => $href,
1210
					'meta'   => $meta,
1211
				)
1212
			);
1213
		}
1214
1215
		if ( current_user_can( 'manage_options' ) ) {
1216
			// Configuration group.
1217
			$wp_admin_bar->add_group(
1218
				array(
1219
					'parent' => 'blog',
1220
					'id'     => 'configuration',
1221
				)
1222
			);
1223
1224
			// Configuration header.
1225
			$wp_admin_bar->add_menu(
1226
				array(
1227
					'parent' => 'configuration',
1228
					'id'     => 'configuration-header',
1229
					'title'  => esc_html_x( 'Configure', 'admin bar menu group label', 'jetpack' ),
1230
					'meta'   => array(
1231
						'class' => 'ab-submenu-header',
1232
					),
1233
				)
1234
			);
1235
1236 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
1237
				$wp_admin_bar->add_menu(
1238
					array(
1239
						'parent' => 'configuration',
1240
						'id'     => 'sharing',
1241
						'title'  => esc_html__( 'Sharing', 'jetpack' ),
1242
						'href'   => Redirect::get_url( 'calypso-sharing' ),
1243
						'meta'   => array(
1244
							'class' => 'mb-icon',
1245
						),
1246
					)
1247
				);
1248
			}
1249
1250
			$people_title = $this->create_menu_item_pair(
1251
				array(
1252
					'url'   => Redirect::get_url( 'calypso-people-team' ),
1253
					'id'    => 'wp-admin-bar-people',
1254
					'label' => esc_html__( 'People', 'jetpack' ),
1255
				),
1256
				array(
1257
					'url'   => admin_url( 'user-new.php' ),
1258
					'id'    => 'wp-admin-bar-people-add',
1259
					'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ),
1260
				)
1261
			);
1262
1263
			$wp_admin_bar->add_menu(
1264
				array(
1265
					'parent' => 'configuration',
1266
					'id'     => 'users-toolbar',
1267
					'title'  => $people_title,
1268
					'href'   => false,
1269
					'meta'   => array(
1270
						'class' => 'inline-action',
1271
					),
1272
				)
1273
			);
1274
1275
			$plugins_title = $this->create_menu_item_pair(
1276
				array(
1277
					'url'   => Redirect::get_url( 'calypso-plugins' ),
1278
					'id'    => 'wp-admin-bar-plugins',
1279
					'label' => esc_html__( 'Plugins', 'jetpack' ),
1280
				),
1281
				array(
1282
					'url'   => Redirect::get_url( 'calypso-plugins-manage' ),
1283
					'id'    => 'wp-admin-bar-plugins-add',
1284
					'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ),
1285
				)
1286
			);
1287
1288
			$wp_admin_bar->add_menu(
1289
				array(
1290
					'parent' => 'configuration',
1291
					'id'     => 'plugins',
1292
					'title'  => $plugins_title,
1293
					'href'   => false,
1294
					'meta'   => array(
1295
						'class' => 'inline-action',
1296
					),
1297
				)
1298
			);
1299
1300
			if ( jetpack_is_atomic_site() ) {
1301
				$domain_title = $this->create_menu_item_pair(
1302
					array(
1303
						'url'   => Redirect::get_url( 'calypso-domains' ),
1304
						'id'    => 'wp-admin-bar-domains',
1305
						'label' => esc_html__( 'Domains', 'jetpack' ),
1306
					),
1307
					array(
1308
						'url'   => Redirect::get_url( 'calypso-domains-add' ),
1309
						'id'    => 'wp-admin-bar-domains-add',
1310
						'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
1311
					)
1312
				);
1313
				$wp_admin_bar->add_menu(
1314
					array(
1315
						'parent' => 'configuration',
1316
						'id'     => 'domains',
1317
						'title'  => $domain_title,
1318
						'href'   => false,
1319
						'meta'   => array(
1320
							'class' => 'inline-action',
1321
						),
1322
					)
1323
				);
1324
			}
1325
1326
			$wp_admin_bar->add_menu(
1327
				array(
1328
					'parent' => 'configuration',
1329
					'id'     => 'blog-settings',
1330
					'title'  => esc_html__( 'Settings', 'jetpack' ),
1331
					'href'   => Redirect::get_url( 'calypso-settings-general' ),
1332
					'meta'   => array(
1333
						'class' => 'mb-icon',
1334
					),
1335
				)
1336
			);
1337
1338
			if ( ! is_admin() ) {
1339
				$wp_admin_bar->add_menu(
1340
					array(
1341
						'parent' => 'configuration',
1342
						'id'     => 'legacy-dashboard',
1343
						'title'  => esc_html__( 'Dashboard', 'jetpack' ),
1344
						'href'   => admin_url(),
1345
						'meta'   => array(
1346
							'class' => 'mb-icon',
1347
						),
1348
					)
1349
				);
1350
			}
1351
1352
			// Restore dashboard menu toggle that is needed on mobile views.
1353
			if ( is_admin() ) {
1354
				$wp_admin_bar->add_menu(
1355
					array(
1356
						'id'    => 'menu-toggle',
1357
						'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>',
1358
						'href'  => '#',
1359
					)
1360
				);
1361
			}
1362
1363
			/**
1364
			 * Fires when menu items are added to the masterbar "My Sites" menu.
1365
			 *
1366
			 * @since 5.4.0
1367
			 */
1368
			do_action( 'jetpack_masterbar' );
1369
		}
1370
	}
1371
1372
	/**
1373
	 * Adds "My Home" submenu item to sites that are eligible.
1374
	 *
1375
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
1376
	 * @return void
1377
	 */
1378
	private function add_my_home_submenu_item( &$wp_admin_bar ) {
1379
		if ( ! current_user_can( 'manage_options' ) || ! jetpack_is_atomic_site() ) {
1380
			return;
1381
		}
1382
1383
		$wp_admin_bar->add_menu(
1384
			array(
1385
				'parent' => 'blog',
1386
				'id'     => 'my-home',
1387
				'title'  => __( 'My Home', 'jetpack' ),
1388
				'href'   => Redirect::get_url( 'calypso-home' ),
1389
				'meta'   => array(
1390
					'class' => 'mb-icon',
1391
				),
1392
			)
1393
		);
1394
	}
1395
}
1396