Completed
Push — update/jitm_jetpack_is_mobile ( 9d9d1e...df4eca )
by
unknown
106:06 queued 94:29
created

Masterbar::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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