Completed
Push — fix/admin-menu-rtl-styles ( 20fd4e...fb884e )
by
unknown
11:25
created

Masterbar::is_rtl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 3
rs 10
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
23
/**
24
 * Provides custom admin bar instead of the default WordPress admin bar.
25
 */
26
class Masterbar {
27
	/**
28
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
29
	 * If not set it will default to loading the ones from WordPress.com.
30
	 *
31
	 * @var string $sandbox_url
32
	 */
33
	private $sandbox_url = '';
34
35
	/**
36
	 * Current locale.
37
	 *
38
	 * @var string
39
	 */
40
	private $locale;
41
42
	/**
43
	 * Current User ID.
44
	 *
45
	 * @var int
46
	 */
47
	private $user_id;
48
	/**
49
	 * WordPress.com user data of the connected user.
50
	 *
51
	 * @var array
52
	 */
53
	private $user_data;
54
	/**
55
	 * WordPress.com username for the connected user.
56
	 *
57
	 * @var string
58
	 */
59
	private $user_login;
60
	/**
61
	 * WordPress.com email address for the connected user.
62
	 *
63
	 * @var string
64
	 */
65
	private $user_email;
66
	/**
67
	 * WordPress.com display name for the connected user.
68
	 *
69
	 * @var string
70
	 */
71
	private $display_name;
72
	/**
73
	 * Site URL sanitized for usage in WordPress.com slugs.
74
	 *
75
	 * @var string
76
	 */
77
	private $primary_site_slug;
78
	/**
79
	 * Whether the text direction is RLT (based on connected WordPress.com user's interface settings).
80
	 *
81
	 * @var string
82
	 */
83
	private $is_rtl;
84
	/**
85
	 * Number of sites owned by connected WordPress.com user.
86
	 *
87
	 * @var int
88
	 */
89
	private $user_site_count;
90
91
	/**
92
	 * Constructor
93
	 */
94
	public function __construct() {
95
		$this->user_id      = get_current_user_id();
96
		$connection_manager = new Connection_Manager( 'jetpack' );
97
98
		if ( ! $connection_manager->is_user_connected( $this->user_id ) ) {
99
			return;
100
		}
101
102
		$this->user_data       = $connection_manager->get_connected_user_data( $this->user_id );
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection_manager->get...er_data($this->user_id) of type object 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...
103
		$this->user_login      = $this->user_data['login'];
104
		$this->user_email      = $this->user_data['email'];
105
		$this->display_name    = $this->user_data['display_name'];
106
		$this->user_site_count = $this->user_data['site_count'];
107
		$this->is_rtl          = 'rtl' === $this->user_data['text_direction'];
0 ignored issues
show
Documentation Bug introduced by
The property $is_rtl was declared of type string, but 'rtl' === $this->user_data['text_direction'] is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

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