Completed
Push — renovate/slack-web-api-6.x ( 1d54b7...bfe495 )
by
unknown
193:23 queued 183:13
created

Masterbar::maybe_logout_user_from_wpcom()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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