Completed
Push — instant-search-master ( 8be3b4...336413 )
by
unknown
06:37 queued 10s
created

A8C_WPCOM_Masterbar::is_my_home_enabled()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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