Completed
Push — try/repurpose-masterbar-module ( 47e6c3...e7989f )
by
unknown
15:19 queued 06:28
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 // 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 Jetpack;
11
use Jetpack_AMP_Support;
12
use Jetpack_Plan;
13
use GP_Locales;
14
15
require_once __DIR__ . '/rtl-admin-bar.php';
16
17
/**
18
 * Provides custom admin bar instead of the default WordPress admin bar.
19
 */
20
class Masterbar {
21
	/**
22
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
23
	 * If not set it will default to loading the ones from WordPress.com.
24
	 *
25
	 * @var string $sandbox_url
26
	 */
27
	private $sandbox_url = '';
28
29
	/**
30
	 * Current locale.
31
	 *
32
	 * @var string
33
	 */
34
	private $locale;
35
36
	/**
37
	 * Current User ID.
38
	 *
39
	 * @var int
40
	 */
41
	private $user_id;
42
	/**
43
	 * WordPress.com user data of the connected user.
44
	 *
45
	 * @var array
46
	 */
47
	private $user_data;
48
	/**
49
	 * WordPress.com username for the connected user.
50
	 *
51
	 * @var string
52
	 */
53
	private $user_login;
54
	/**
55
	 * WordPress.com email address for the connected user.
56
	 *
57
	 * @var string
58
	 */
59
	private $user_email;
60
	/**
61
	 * WordPress.com display name for the connected user.
62
	 *
63
	 * @var string
64
	 */
65
	private $display_name;
66
	/**
67
	 * Site URL sanitized for usage in WordPress.com slugs.
68
	 *
69
	 * @var string
70
	 */
71
	private $primary_site_slug;
72
	/**
73
	 * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.
74
	 *
75
	 * @var string
76
	 */
77
	private $user_text_direction;
78
	/**
79
	 * Number of sites owned by connected WordPress.com user.
80
	 *
81
	 * @var int
82
	 */
83
	private $user_site_count;
84
85
	/**
86
	 * Constructor
87
	 */
88
	public function __construct() {
89
		add_action( 'admin_bar_init', array( $this, 'init' ) );
90
91
		// Post logout on the site, also log the user out of WordPress.com.
92
		add_filter( 'logout_redirect', array( $this, 'maybe_logout_user_from_wpcom' ), 10, 3 );
93
	}
94
95
	/**
96
	 * Initialize our masterbar.
97
	 */
98
	public function init() {
99
		$this->locale  = $this->get_locale();
100
		$this->user_id = get_current_user_id();
101
102
		// Limit the masterbar to be shown only to connected Jetpack users.
103
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
104
			return;
105
		}
106
107
		// Don't show the masterbar on WordPress mobile apps.
108
		if ( User_Agent_Info::is_mobile_app() ) {
109
			add_filter( 'show_admin_bar', '__return_false' );
110
			return;
111
		}
112
113
		// Disable the Masterbar on AMP views.
114
		if (
115
			class_exists( 'Jetpack_AMP_Support' )
116
			&& Jetpack_AMP_Support::is_amp_request()
117
		) {
118
			return;
119
		}
120
121
		Assets::add_resource_hint(
122
			array(
123
				'//s0.wp.com',
124
				'//s1.wp.com',
125
				'//s2.wp.com',
126
				'//0.gravatar.com',
127
				'//1.gravatar.com',
128
				'//2.gravatar.com',
129
			),
130
			'dns-prefetch'
131
		);
132
133
		// Atomic only.
134
		if ( jetpack_is_atomic_site() ) {
135
			/*
136
			 * override user setting that hides masterbar from site's front.
137
			 * https://github.com/Automattic/jetpack/issues/7667
138
			 */
139
			add_filter( 'show_admin_bar', '__return_true' );
140
		}
141
142
		$this->user_data       = Jetpack::get_connected_user_data( $this->user_id );
143
		$this->user_login      = $this->user_data['login'];
144
		$this->user_email      = $this->user_data['email'];
145
		$this->display_name    = $this->user_data['display_name'];
146
		$this->user_site_count = $this->user_data['site_count'];
147
148
		// Used to build menu links that point directly to Calypso.
149
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
150
151
		// Used for display purposes and for building WP Admin links.
152
		$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...
153
154
		// We need to use user's setting here, instead of relying on current blog's text direction.
155
		$this->user_text_direction = $this->user_data['text_direction'];
156
157
		if ( $this->is_rtl() ) {
158
			// Extend core WP_Admin_Bar class in order to add rtl styles.
159
			add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );
160
		}
161
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
162
163
		add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );
164
165
		add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
166
		add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
167
168
		add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );
169
		add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );
170
171
		if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) {
172
			// Override Notification module to include RTL styles.
173
			add_action( '_enqueue_rtl_notification_styles', '__return_true' );
174
		}
175
	}
176
177
	/**
178
	 * Log out from WordPress.com when logging out of the local site.
179
	 *
180
	 * @param string  $redirect_to           The redirect destination URL.
181
	 * @param string  $requested_redirect_to The requested redirect destination URL passed as a parameter.
182
	 * @param WP_User $user                  The WP_User object for the user that's logging out.
183
	 */
184
	public function maybe_logout_user_from_wpcom( $redirect_to, $requested_redirect_to, $user ) {
185
		/**
186
		 * Whether we should sign out from wpcom too when signing out from the masterbar.
187
		 *
188
		 * @since 5.9.0
189
		 *
190
		 * @param bool $masterbar_should_logout_from_wpcom True by default.
191
		 */
192
		$masterbar_should_logout_from_wpcom = apply_filters( 'jetpack_masterbar_should_logout_from_wpcom', true );
193
		if (
194
			// No need to check for a nonce here, it happens further up.
195
			isset( $_GET['context'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
196
			&& 'masterbar' === $_GET['context'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
197
			&& $masterbar_should_logout_from_wpcom
198
		) {
199
			/*
200
			 * Get the associated WordPress.com User ID, if the user is connected.
201
			 */
202
			$connection_manager = new Connection_Manager();
203
			if ( $connection_manager->is_user_connected( $user->ID ) ) {
204
				$wpcom_user_data = $connection_manager->get_connected_user_data( $user->ID );
205
				if ( ! empty( $wpcom_user_data['ID'] ) ) {
206
					/**
207
					 * Hook into the log out event happening from the Masterbar.
208
					 *
209
					 * @since 5.1.0
210
					 * @since 7.9.0 Added the $wpcom_user_id parameter to the action.
211
					 *
212
					 * @module masterbar
213
					 *
214
					 * @param int $wpcom_user_id WordPress.com User ID.
215
					 */
216
					do_action( 'wp_masterbar_logout', $wpcom_user_data['ID'] );
217
				}
218
			}
219
		}
220
221
		return $redirect_to;
222
	}
223
224
	/**
225
	 * Get class name for RTL sites.
226
	 */
227
	public function get_rtl_admin_bar_class() {
228
		return 'RTL_Admin_Bar';
229
	}
230
231
	/**
232
	 * Adds CSS classes to admin body tag.
233
	 *
234
	 * @since 5.1
235
	 *
236
	 * @param string $admin_body_classes CSS classes that will be added.
237
	 *
238
	 * @return string
239
	 */
240
	public function admin_body_class( $admin_body_classes ) {
241
		return "$admin_body_classes jetpack-masterbar";
242
	}
243
244
	/**
245
	 * Remove the default Admin Bar CSS.
246
	 */
247
	public function remove_core_styles() {
248
		/*
249
		 * Notifications need the admin bar styles,
250
		 * so let's not remove them when the module is active.
251
		 */
252
		if ( ! Jetpack::is_module_active( 'notes' ) ) {
253
			wp_dequeue_style( 'admin-bar' );
254
		}
255
	}
256
257
	/**
258
	 * Check if the user settings are for an RTL language or not.
259
	 */
260
	public function is_rtl() {
261
		return 'rtl' === $this->user_text_direction ? true : false;
262
	}
263
264
	/**
265
	 * Enqueue our own CSS and JS to display our custom admin bar.
266
	 */
267
	public function add_styles_and_scripts() {
268
269
		if ( $this->is_rtl() ) {
270
			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 );
271
			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 );
272
		} else {
273
			wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );
274
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );
275
		}
276
277
		// Local overrides.
278
		wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION );
279
280
		if ( ! Jetpack::is_module_active( 'notes ' ) ) {
281
			// Masterbar is relying on some icons from noticons.css.
282
			wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
283
		}
284
285
		wp_enqueue_script(
286
			'jetpack-accessible-focus',
287
			Assets::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
288
			array(),
289
			JETPACK__VERSION,
290
			false
291
		);
292
		wp_enqueue_script(
293
			'_tracks_events',
294
			Assets::get_file_url_for_environment(
295
				'_inc/build/masterbar/tracks-events.min.js',
296
				'modules/masterbar/tracks-events.js'
297
			),
298
			array( 'jquery' ),
299
			JETPACK__VERSION,
300
			false
301
		);
302
303
		wp_enqueue_script(
304
			'_overrides',
305
			$this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ),
306
			array( 'jquery' ),
307
			JETPACK__VERSION,
308
			false
309
		);
310
	}
311
312
	/**
313
	 * Get base URL where our CSS and JS will come from.
314
	 *
315
	 * @param string $file File path for a static resource.
316
	 */
317
	private function wpcom_static_url( $file ) {
318
		if ( ! empty( $this->sandbox_url ) ) {
319
			// For testing undeployed changes to remotely enqueued scripts and styles.
320
			return set_url_scheme( $this->sandbox_url . $file, 'https' );
321
		}
322
323
		$i   = hexdec( substr( md5( $file ), - 1 ) ) % 2;
324
		$url = 'https://s' . $i . '.wp.com' . $file;
325
326
		return set_url_scheme( $url, 'https' );
327
	}
328
329
	/**
330
	 * Remove the default admin bar items and replace it with our own admin bar.
331
	 */
332
	public function replace_core_masterbar() {
333
		global $wp_admin_bar;
334
335
		if ( ! is_object( $wp_admin_bar ) ) {
336
			return false;
337
		}
338
339
		$this->clear_core_masterbar( $wp_admin_bar );
340
		$this->build_wpcom_masterbar( $wp_admin_bar );
341
	}
342
343
	/**
344
	 * Remove all existing toolbar entries from core Masterbar
345
	 *
346
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
347
	 */
348
	public function clear_core_masterbar( $wp_admin_bar ) {
349
		foreach ( $wp_admin_bar->get_nodes() as $node ) {
350
			$wp_admin_bar->remove_node( $node->id );
351
		}
352
	}
353
354
	/**
355
	 * Add entries corresponding to WordPress.com Masterbar
356
	 *
357
	 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
358
	 */
359
	public function build_wpcom_masterbar( $wp_admin_bar ) {
360
		// Menu groups.
361
		$this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
362
363
		// Left part.
364
		$this->add_my_sites_submenu( $wp_admin_bar );
365
		$this->add_reader_submenu( $wp_admin_bar );
366
367
		// Right part.
368
		if ( Jetpack::is_module_active( 'notes' ) ) {
369
			$this->add_notifications( $wp_admin_bar );
370
		}
371
372
		$this->add_me_submenu( $wp_admin_bar );
373
		$this->add_write_button( $wp_admin_bar );
374
375
		// Recovery mode exit.
376
		if ( function_exists( 'wp_admin_bar_recovery_mode_menu' ) ) {
377
			wp_admin_bar_recovery_mode_menu( $wp_admin_bar );
378
		}
379
380
		if ( class_exists( 'Automattic\Jetpack\Scan\Admin_Bar_Notice' ) ) {
381
			$scan_admin_bar_notice = Admin_Bar_Notice::instance();
382
			$scan_admin_bar_notice->add_threats_to_toolbar( $wp_admin_bar );
383
		}
384
	}
385
386
	/**
387
	 * Get WordPress.com current locale name.
388
	 */
389
	public function get_locale() {
390
		$wpcom_locale = get_locale();
391
392
		if ( ! class_exists( 'GP_Locales' ) ) {
393
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
394
				require JETPACK__GLOTPRESS_LOCALES_PATH;
395
			}
396
		}
397
398
		if ( class_exists( 'GP_Locales' ) ) {
399
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
400
			if ( $wpcom_locale_object instanceof GP_Locale ) {
0 ignored issues
show
Bug introduced by
The class Automattic\Jetpack\Dashb...ustomizations\GP_Locale does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

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