Completed
Push — update/do-not-signout-wpcom-by... ( 0b5e19...a09205 )
by
unknown
13:19 queued 04:15
created

A8C_WPCOM_Masterbar::add_styles_and_scripts()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
4
5
class A8C_WPCOM_Masterbar {
6
	/**
7
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
8
	 * If not set it will default to loading the ones from WordPress.com.
9
	 *
10
	 * @var string $sandbox_url
11
	 */
12
	private $sandbox_url = '';
13
14
	private $locale;
15
16
	private $user_id;
17
	private $user_data;
18
	private $user_login;
19
	private $user_email;
20
	private $display_name;
21
	private $primary_site_slug;
22
	private $user_text_direction;
23
	private $user_site_count;
24
25
	function __construct() {
26
		$this->locale  = $this->get_locale();
27
		$this->user_id = get_current_user_id();
28
29
		// Limit the masterbar to be shown only to connected Jetpack users.
30
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
31
			return;
32
		}
33
34
		Jetpack::dns_prefetch( array(
35
			'//s0.wp.com',
36
			'//s1.wp.com',
37
			'//s2.wp.com',
38
			'//0.gravatar.com',
39
			'//1.gravatar.com',
40
			'//2.gravatar.com',
41
		) );
42
43
		// Atomic only
44
		if ( jetpack_is_atomic_site() ) {
45
			// override user setting that hides masterbar from site's front.
46
			// https://github.com/Automattic/jetpack/issues/7667
47
			add_filter( 'show_admin_bar', '__return_true' );
48
			// Always sign out from .com from the masterbar
49
			add_filter( 'jetpack_masterbar_should_logout_from_wpcom', '__return_true' );
50
		}
51
52
		$this->user_data = Jetpack::get_connected_user_data( $this->user_id );
53
		$this->user_login = $this->user_data['login'];
54
		$this->user_email = $this->user_data['email'];
55
		$this->display_name = $this->user_data['display_name'];
56
		$this->user_site_count = $this->user_data['site_count'];
57
58
		// Used to build menu links that point directly to Calypso.
59
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
60
61
		// Used for display purposes and for building WP Admin links.
62
		$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...
63
64
		// We need to use user's setting here, instead of relying on current blog's text direction
65
		$this->user_text_direction = $this->user_data['text_direction'];
66
67
		if ( $this->is_rtl() ) {
68
			// Extend core WP_Admin_Bar class in order to add rtl styles
69
			add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );
70
		}
71
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
72
73
		add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );
74
75
		add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
76
		add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
77
78
		add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );
79
		add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );
80
81
		if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) {
82
			// Override Notification module to include RTL styles
83
			add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' );
84
		}
85
86
		add_action( 'wp_logout', array( $this, 'maybe_logout_user_from_wpcom' ) );
87
	}
88
89
	public function maybe_logout_user_from_wpcom() {
90
		 /**
91
		  * Whether we should sign out from wpcom too when signing out from the masterbar.
92
		  *
93
		  * @since 5.6.0
94
		  *
95
		  * @param bool $masterbar_should_logout_from_wpcom False by default.
96
		  */
97
		$masterbar_should_logout_from_wpcom = apply_filters( 'jetpack_masterbar_should_logout_from_wpcom', false );
98
		if (
99
			isset( $_GET['context'] ) &&
100
			'masterbar' === $_GET['context'] &&
101
			$masterbar_should_logout_from_wpcom
102
		) {
103
			do_action( 'wp_masterbar_logout' );
104
		}
105
	}
106
107
	public function get_rtl_admin_bar_class() {
108
		return 'RTL_Admin_Bar';
109
	}
110
111
	/**
112
	 * Adds CSS classes to admin body tag.
113
	 *
114
	 * @since 5.1
115
	 *
116
	 * @param string $admin_body_classes CSS classes that will be added.
117
	 *
118
	 * @return string
119
	 */
120
	public function admin_body_class( $admin_body_classes ) {
121
		return "$admin_body_classes jetpack-masterbar";
122
	}
123
124
	public function remove_core_styles() {
125
		wp_dequeue_style( 'admin-bar' );
126
	}
127
128
	public function is_rtl() {
129
		return $this->user_text_direction === 'rtl' ? true : false;
130
	}
131
132
	public function add_styles_and_scripts() {
133
134
		if ( $this->is_rtl() ) {
135
			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 );
136
			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 );
137
		} else {
138
			wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );
139
			wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );
140
		}
141
142
		// Local overrides
143
		wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION );
144
145
		if ( ! Jetpack::is_module_active( 'notes ' ) ) {
146
			// Masterbar is relying on some icons from noticons.css
147
			wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
148
		}
149
150
		wp_enqueue_script(
151
			'jetpack-accessible-focus',
152
			Jetpack::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
153
			array(),
154
			JETPACK__VERSION
155
		);
156
		wp_enqueue_script( 'a8c_wpcom_masterbar_tracks_events', plugins_url( 'tracks-events.js', __FILE__ ), array( 'jquery' ), JETPACK__VERSION );
157
158
		wp_enqueue_script( 'a8c_wpcom_masterbar_overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ), array( 'jquery' ), JETPACK__VERSION );
159
	}
160
161
	function wpcom_static_url( $file ) {
162
		if ( ! empty( $this->sandbox_url ) ) {
163
			// For testing undeployed changes to remotely enqueued scripts and styles.
164
			return set_url_scheme( $this->sandbox_url . $file, 'https');
165
		}
166
167
		$i   = hexdec( substr( md5( $file ), - 1 ) ) % 2;
168
		$url = 'https://s' . $i . '.wp.com' . $file;
169
170
		return set_url_scheme( $url, 'https');
171
	}
172
173
	public function replace_core_masterbar() {
174
		global $wp_admin_bar;
175
176
		if ( ! is_object( $wp_admin_bar ) ) {
177
			return false;
178
		}
179
180
		$this->clear_core_masterbar( $wp_admin_bar );
181
		$this->build_wpcom_masterbar( $wp_admin_bar );
182
	}
183
184
	// Remove all existing toolbar entries from core Masterbar
185
	public function clear_core_masterbar( $wp_admin_bar ) {
186
		foreach ( $wp_admin_bar->get_nodes() as $node ) {
187
			$wp_admin_bar->remove_node( $node->id );
188
		}
189
	}
190
191
	// Add entries corresponding to WordPress.com Masterbar
192
	public function build_wpcom_masterbar( $wp_admin_bar ) {
193
		// Menu groups
194
		$this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
195
196
		// Left part
197
		$this->add_my_sites_submenu( $wp_admin_bar );
198
		$this->add_reader_submenu( $wp_admin_bar );
199
200
		// Right part
201
		if ( Jetpack::is_module_active( 'notes' ) ) {
202
			$this->add_notifications( $wp_admin_bar );
203
		}
204
205
		$this->add_me_submenu( $wp_admin_bar );
206
		$this->add_write_button( $wp_admin_bar );
207
	}
208
209
	public function get_locale() {
210
		$wpcom_locale = get_locale();
211
212
		if ( ! class_exists( 'GP_Locales' ) ) {
213
			if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
214
				require JETPACK__GLOTPRESS_LOCALES_PATH;
215
			}
216
		}
217
218 View Code Duplication
		if ( class_exists( 'GP_Locales' ) ) {
219
			$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
220
			if ( $wpcom_locale_object instanceof GP_Locale ) {
221
				$wpcom_locale = $wpcom_locale_object->slug;
222
			}
223
		}
224
225
		return $wpcom_locale;
226
	}
227
228
	public function add_notifications( $wp_admin_bar ) {
229
		$wp_admin_bar->add_node( array(
230
			'id'     => 'notes',
231
			'title'  => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span>
232
						 <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>
233
						 <span class="noticon noticon-bell"></span>',
234
			'meta'   => array(
235
				'html'  => '<div id="wpnt-notes-panel2" style="display:none" lang="'. esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' .
236
				           '<div class="wpnt-notes-panel-header">' .
237
				           '<span class="wpnt-notes-header">' .
238
				           esc_html__( 'Notifications', 'jetpack' ) .
239
				           '</span>' .
240
				           '<span class="wpnt-notes-panel-link">' .
241
				           '</span>' .
242
				           '</div>' .
243
				           '</div>',
244
				'class' => 'menupop mb-trackable',
245
			),
246
			'parent' => 'top-secondary',
247
		) );
248
	}
249
250
	public function add_reader_submenu( $wp_admin_bar ) {
251
		$wp_admin_bar->add_menu( array(
252
			'parent' => 'root-default',
253
			'id'    => 'newdash',
254
			'title' => esc_html__( 'Reader', 'jetpack' ),
255
			'href'  => '#',
256
			'meta'  => array(
257
				'class' => 'mb-trackable',
258
			)
259
		) );
260
261
		$wp_admin_bar->add_menu( array(
262
			'parent' => 'newdash',
263
			'id'     => 'streams-header',
264
			'title'  => esc_html_x(
265
				'Streams',
266
				'Title for Reader sub-menu that contains followed sites, likes, and recommendations',
267
				'jetpack'
268
			),
269
			'meta'   => array(
270
				'class' => 'ab-submenu-header',
271
			)
272
		) );
273
274
		$following_title = $this->create_menu_item_pair(
275
			array(
276
				'url'   => 'https://wordpress.com/',
277
				'id'    => 'wp-admin-bar-followed-sites',
278
				'label' => esc_html__( 'Followed Sites', 'jetpack' ),
279
			),
280
			array(
281
				'url'   => 'https://wordpress.com/following/edit',
282
				'id'    => 'wp-admin-bar-reader-followed-sites-manage',
283
				'label' => esc_html__( 'Manage', 'jetpack' ),
284
			)
285
		);
286
287
		$wp_admin_bar->add_menu( array(
288
			'parent' => 'newdash',
289
			'id'     => 'following',
290
			'title'  => $following_title,
291
			'meta'	 => array( 'class' => 'inline-action' )
292
		) );
293
294
		$wp_admin_bar->add_menu( array(
295
			'parent' => 'newdash',
296
			'id'     => 'discover-discover',
297
			'title'  => esc_html__( 'Discover', 'jetpack' ),
298
			'href'   => 'https://wordpress.com/discover',
299
			'meta'   => array(
300
				'class' => 'mb-icon-spacer',
301
			)
302
		) );
303
304
		$wp_admin_bar->add_menu( array(
305
			'parent' => 'newdash',
306
			'id'     => 'discover-search',
307
			'title'  => esc_html__( 'Search', 'jetpack' ),
308
			'href'   => 'https://wordpress.com/read/search',
309
			'meta'   => array(
310
				'class' => 'mb-icon-spacer',
311
			)
312
		) );
313
314
		$wp_admin_bar->add_menu( array(
315
			'parent' => 'newdash',
316
			'id'     => 'discover-recommended-blogs',
317
			'title'  => esc_html__( 'Recommendations', 'jetpack' ),
318
			'href'   => 'https://wordpress.com/recommendations',
319
			'meta'   => array(
320
				'class' => 'mb-icon-spacer',
321
			)
322
		) );
323
324
		$wp_admin_bar->add_menu( array(
325
			'parent' => 'newdash',
326
			'id'     => 'my-activity-my-likes',
327
			'title'  => esc_html__( 'My Likes', 'jetpack' ),
328
			'href'   => 'https://wordpress.com/activities/likes',
329
			'meta'   => array(
330
				'class' => 'mb-icon-spacer',
331
			)
332
		) );
333
334
	}
335
336
	public function create_menu_item_pair( $primary, $secondary ) {
337
		$primary_class   = 'ab-item ab-primary mb-icon';
338
		$secondary_class = 'ab-secondary';
339
340
		$primary_anchor   = $this->create_menu_item_anchor( $primary_class, $primary['url'], $primary['label'], $primary['id'] );
341
		$secondary_anchor = $this->create_menu_item_anchor( $secondary_class, $secondary['url'], $secondary['label'], $secondary['id'] );
342
343
		return $primary_anchor . $secondary_anchor;
344
	}
345
346
	public function create_menu_item_anchor( $class, $url, $label, $id ) {
347
		return '<a href="' . $url . '" class="' . $class . '" id="' . $id . '">' . $label . '</a>';
348
	}
349
350
	public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
351
		$wp_admin_bar->add_group( array(
352
			'id'     => 'root-default',
353
			'meta'   => array(
354
				'class' => 'ab-top-menu',
355
			),
356
		) );
357
358
		$wp_admin_bar->add_group( array(
359
			'parent' => 'blog',
360
			'id'     => 'blog-secondary',
361
			'meta'   => array(
362
				'class' => 'ab-sub-secondary',
363
			),
364
		) );
365
366
		$wp_admin_bar->add_group( array(
367
			'id'     => 'top-secondary',
368
			'meta'   => array(
369
				'class' => 'ab-top-secondary',
370
			),
371
		) );
372
	}
373
374
	public function add_me_submenu( $wp_admin_bar ) {
375
		$user_id = get_current_user_id();
376
		if ( empty( $user_id ) ) {
377
			return;
378
		}
379
380
		$avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
381
		$class  = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
382
383
		// Add the 'Me' menu
384
		$wp_admin_bar->add_menu( array(
385
			'id'     => 'my-account',
386
			'parent' => 'top-secondary',
387
			'title'  => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
388
			'href'   => '#',
389
			'meta'   => array(
390
				'class' => $class,
391
			),
392
		) );
393
394
		$id = 'user-actions';
395
		$wp_admin_bar->add_group( array(
396
			'parent' => 'my-account',
397
			'id'     => $id,
398
		) );
399
400
		$settings_url = 'https://wordpress.com/me/account';
401
402
		$logout_url = wp_logout_url();
403
		$logout_url = add_query_arg( 'context', 'masterbar', $logout_url );
404
405
		$user_info  = get_avatar( $this->user_email, 128, 'mm', '', array( 'force_display' => true ) );
406
		$user_info .= '<span class="display-name">' . $this->display_name . '</span>';
407
		$user_info .= '<a class="username" href="http://gravatar.com/' . $this->user_login . '">@' . $this->user_login . '</a>';
408
409
		$user_info .= sprintf(
410
			'<div><a href="%s" class="ab-sign-out">%s</a></div>',
411
			$logout_url,
412
			esc_html__( 'Sign Out', 'jetpack' )
413
		);
414
415
		$wp_admin_bar->add_menu( array(
416
			'parent' => $id,
417
			'id'     => 'user-info',
418
			'title'  => $user_info,
419
			'meta'   => array(
420
				'class' => 'user-info user-info-item',
421
				'tabindex' => -1,
422
			),
423
		) );
424
425
		$wp_admin_bar->add_menu( array(
426
			'parent' => $id,
427
			'id'     => 'profile-header',
428
			'title'  => esc_html__( 'Profile', 'jetpack' ),
429
			'meta'   => array(
430
				'class' => 'ab-submenu-header',
431
			),
432
		) );
433
434
		$wp_admin_bar->add_menu( array(
435
			'parent' => $id,
436
			'id'     => 'my-profile',
437
			'title'  => esc_html__( 'My Profile', 'jetpack' ),
438
			'href'   => 'https://wordpress.com/me',
439
			'meta'   => array(
440
				'class' => 'mb-icon',
441
			),
442
		) );
443
444
		$wp_admin_bar->add_menu( array(
445
			'parent' => $id,
446
			'id'     => 'account-settings',
447
			'title'  => esc_html__( 'Account Settings', 'jetpack' ),
448
			'href'   => $settings_url,
449
			'meta'   => array(
450
				'class' => 'mb-icon',
451
			),
452
		) );
453
454
		$wp_admin_bar->add_menu( array(
455
			'parent' => $id,
456
			'id'     => 'billing',
457
			'title'  => esc_html__( 'Manage Purchases', 'jetpack' ),
458
			'href'   => 'https://wordpress.com/me/purchases',
459
			'meta'   => array(
460
				'class' => 'mb-icon',
461
			),
462
		) );
463
464
		$wp_admin_bar->add_menu( array(
465
			'parent' => $id,
466
			'id'     => 'security',
467
			'title'  => esc_html__( 'Security', 'jetpack' ),
468
			'href'   => 'https://wordpress.com/me/security',
469
			'meta'   => array(
470
				'class' => 'mb-icon',
471
			),
472
		) );
473
474
		$wp_admin_bar->add_menu( array(
475
			'parent' => $id,
476
			'id'     => 'notifications',
477
			'title'  => esc_html__( 'Notifications', 'jetpack' ),
478
			'href'   => 'https://wordpress.com/me/notifications',
479
			'meta'   => array(
480
				'class' => 'mb-icon',
481
			),
482
		) );
483
484
		$wp_admin_bar->add_menu( array(
485
			'parent' => $id,
486
			'id'     => 'special-header',
487
			'title'  => esc_html_x(
488
				'Special',
489
				'Title for Me sub-menu that contains Get Apps, Next Steps, and Help options',
490
				'jetpack'
491
			),
492
			'meta'   => array(
493
				'class' => 'ab-submenu-header',
494
			),
495
		) );
496
497
		$wp_admin_bar->add_menu( array(
498
			'parent' => $id,
499
			'id'     => 'get-apps',
500
			'title'  => esc_html__( 'Get Apps', 'jetpack' ),
501
			'href'   => 'https://wordpress.com/me/get-apps',
502
			'meta'   => array(
503
				'class' => 'mb-icon user-info-item',
504
			),
505
		) );
506
507
		$wp_admin_bar->add_menu( array(
508
			'parent' => $id,
509
			'id'     => 'next-steps',
510
			'title'  => esc_html__( 'Next Steps', 'jetpack' ),
511
			'href'   => 'https://wordpress.com/me/next',
512
			'meta'   => array(
513
				'class' => 'mb-icon user-info-item',
514
			),
515
		) );
516
517
		$help_link = 'https://jetpack.com/support/';
518
519
		if ( jetpack_is_atomic_site() ) {
520
			$help_link = 'https://wordpress.com/help';
521
		}
522
523
		$wp_admin_bar->add_menu( array(
524
			'parent' => $id,
525
			'id'     => 'help',
526
			'title'  => esc_html__( 'Help', 'jetpack' ),
527
			'href'   => $help_link,
528
			'meta'   => array(
529
				'class' => 'mb-icon user-info-item',
530
			),
531
		) );
532
	}
533
534
	public function add_write_button( $wp_admin_bar ) {
535
		$current_user = wp_get_current_user();
536
537
		$posting_blog_id = get_current_blog_id();
538
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
539
			$posting_blog_id = $current_user->primary_blog;
540
		}
541
542
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
543
544
		if ( ! $posting_blog_id || ! $user_can_post ) {
545
			return;
546
		}
547
548
		$blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
549
550
		$wp_admin_bar->add_menu( array(
551
			'parent'    => 'top-secondary',
552
			'id' => 'ab-new-post',
553
			'href' => $blog_post_page,
554
			'title' => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
555
			'meta'  => array(
556
				'class' => 'mb-trackable',
557
			)
558
		) );
559
	}
560
561
	public function add_my_sites_submenu( $wp_admin_bar ) {
562
		$current_user = wp_get_current_user();
563
564
		$blog_name = get_bloginfo( 'name' );
565
		if ( empty( $blog_name ) ) {
566
			$blog_name = $this->primary_site_slug;
567
		}
568
569
		if ( mb_strlen( $blog_name ) > 20 ) {
570
			$blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
571
		}
572
573
		$wp_admin_bar->add_menu( array(
574
			'parent' => 'root-default',
575
			'id'    => 'blog',
576
			'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
577
			'href'  => '#',
578
			'meta'  => array(
579
				'class' => 'my-sites mb-trackable',
580
			),
581
		) );
582
583
		if ( $this->user_site_count > 1 ) {
584
			$wp_admin_bar->add_menu( array(
585
				'parent' => 'blog',
586
				'id'     => 'switch-site',
587
				'title'  => esc_html__( 'Switch Site', 'jetpack' ),
588
				'href'   => 'https://wordpress.com/sites',
589
			) );
590
		} else {
591
			$wp_admin_bar->add_menu( array(
592
				'parent' => 'blog',
593
				'id'     => 'new-site',
594
				'title'  => esc_html__( '+ Add New WordPress', 'jetpack' ),
595
				'href'   => 'https://wordpress.com/start?ref=admin-bar-logged-in',
596
			) );
597
		}
598
599
		if ( is_user_member_of_blog( $current_user->ID ) ) {
600
			$blavatar = '';
601
			$class    = 'current-site';
602
603
			if ( has_site_icon() ) {
604
				$src = get_site_icon_url();
605
				$blavatar = '<img class="avatar" src="'. esc_attr( $src ) . '" alt="Current site avatar">';
606
				$class = 'has-blavatar';
607
			}
608
609
			$blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>';
610
			$blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
611
			$blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
612
613
			$wp_admin_bar->add_menu( array(
614
				'parent' => 'blog',
615
				'id'     => 'blog-info',
616
				'title'  => $blog_info,
617
				'href'   => esc_url( trailingslashit( $this->primary_site_url ) ),
618
				'meta'   => array(
619
					'class' => $class,
620
				),
621
			) );
622
		}
623
624
		// Site Preview
625
		if ( is_admin() ) {
626
			$wp_admin_bar->add_menu( array(
627
				'parent' => 'blog',
628
				'id'     => 'site-view',
629
				'title'  => __( 'View Site', 'jetpack' ),
630
				'href'   => home_url(),
631
				'meta'   => array(
632
					'class' => 'mb-icon',
633
					'target' => '_blank',
634
				),
635
			) );
636
		}
637
638
		// Stats
639 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) ) {
640
			$wp_admin_bar->add_menu( array(
641
				'parent' => 'blog',
642
				'id'     => 'blog-stats',
643
				'title'  => esc_html__( 'Stats', 'jetpack' ),
644
				'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
645
				'meta'   => array(
646
					'class' => 'mb-icon',
647
				),
648
			) );
649
		}
650
651
		// Add Calypso plans link and plan type indicator
652
		if ( is_user_member_of_blog( $current_user->ID ) ) {
653
			$plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
654
			$label = esc_html__( 'Plan', 'jetpack' );
655
			$plan = Jetpack::get_active_plan();
656
657
			$plan_title = $this->create_menu_item_pair(
658
				array(
659
					'url'   => $plans_url,
660
					'id'    => 'wp-admin-bar-plan',
661
					'label' => $label,
662
				),
663
				array(
664
					'url'   => $plans_url,
665
					'id'    => 'wp-admin-bar-plan-badge',
666
					'label' => $plan['product_name_short']
667
				)
668
			);
669
670
			$wp_admin_bar->add_menu( array(
671
				'parent' => 'blog',
672
				'id'     => 'plan',
673
				'title'  => $plan_title,
674
				'meta'   => array(
675
					'class' => 'inline-action',
676
				),
677
			) );
678
		}
679
680
		// Publish group
681
		$wp_admin_bar->add_group( array(
682
			'parent' => 'blog',
683
			'id'     => 'publish',
684
		) );
685
686
		// Publish header
687
		$wp_admin_bar->add_menu( array(
688
			'parent' => 'publish',
689
			'id'     => 'publish-header',
690
			'title'  => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ),
691
			'meta'   => array(
692
				'class' => 'ab-submenu-header',
693
			),
694
		) );
695
696
		// Pages
697
		$pages_title = $this->create_menu_item_pair(
698
			array(
699
				'url'   => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
700
				'id'    => 'wp-admin-bar-edit-page',
701
				'label' => esc_html__( 'Site Pages', 'jetpack' ),
702
			),
703
			array(
704
				'url'   => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
705
				'id'    => 'wp-admin-bar-new-page-badge',
706
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
707
			)
708
		);
709
710
		if ( ! current_user_can( 'edit_pages' ) ) {
711
			$pages_title = $this->create_menu_item_anchor(
712
				'ab-item ab-primary mb-icon',
713
				'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
714
				esc_html__( 'Site Pages', 'jetpack' ),
715
				'wp-admin-bar-edit-page'
716
			);
717
		}
718
719
		$wp_admin_bar->add_menu( array(
720
			'parent' => 'publish',
721
			'id'     => 'new-page',
722
			'title'  => $pages_title,
723
			'meta'   => array(
724
				'class' => 'inline-action',
725
			),
726
		) );
727
728
		// Blog Posts
729
		$posts_title = $this->create_menu_item_pair(
730
			array(
731
				'url'   => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
732
				'id'    => 'wp-admin-bar-edit-post',
733
				'label' => esc_html__( 'Blog Posts', 'jetpack' ),
734
			),
735
			array(
736
				'url'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
737
				'id'    => 'wp-admin-bar-new-post-badge',
738
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
739
			)
740
		);
741
742
		if ( ! current_user_can( 'edit_posts' ) ) {
743
			$posts_title = $this->create_menu_item_anchor(
744
				'ab-item ab-primary mb-icon',
745
				'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
746
				esc_html__( 'Blog Posts', 'jetpack' ),
747
				'wp-admin-bar-edit-post'
748
			);
749
		}
750
751
		$wp_admin_bar->add_menu( array(
752
			'parent' => 'publish',
753
			'id'     => 'new-post',
754
			'title'  => $posts_title,
755
			'meta'   => array(
756
				'class' => 'inline-action mb-trackable',
757
			),
758
		) );
759
760
		// Comments
761
		if ( current_user_can( 'moderate_comments' ) ) {
762
			$wp_admin_bar->add_menu( array(
763
				'parent' => 'publish',
764
				'id'     => 'comments',
765
				'title'  => __( 'Comments' ),
766
				'href'   => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ),
767
				'meta'   => array(
768
					'class' => 'mb-icon',
769
				),
770
			) );
771
		}
772
773
		// Testimonials
774 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
775
			$testimonials_title = $this->create_menu_item_pair(
776
				array(
777
					'url'   => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
778
					'id'    => 'wp-admin-bar-edit-testimonial',
779
					'label' => esc_html__( 'Testimonials', 'jetpack' ),
780
				),
781
				array(
782
					'url'   => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
783
					'id'    => 'wp-admin-bar-new-testimonial',
784
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
785
				)
786
			);
787
788
			if ( ! current_user_can( 'edit_pages' ) ) {
789
				$testimonials_title = $this->create_menu_item_anchor(
790
					'ab-item ab-primary mb-icon',
791
					'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
792
					esc_html__( 'Testimonials', 'jetpack' ),
793
					'wp-admin-bar-edit-testimonial'
794
				);
795
			}
796
797
			$wp_admin_bar->add_menu( array(
798
				'parent' => 'publish',
799
				'id'     => 'new-jetpack-testimonial',
800
				'title'  => $testimonials_title,
801
				'meta'   => array(
802
					'class' => 'inline-action',
803
				),
804
			) );
805
		}
806
807
		// Portfolio
808 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
809
			$portfolios_title = $this->create_menu_item_pair(
810
				array(
811
					'url'   => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
812
					'id'    => 'wp-admin-bar-edit-portfolio',
813
					'label' => esc_html__( 'Portfolio', 'jetpack' ),
814
				),
815
				array(
816
					'url'   => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
817
					'id'    => 'wp-admin-bar-new-portfolio',
818
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
819
				)
820
			);
821
822
			if ( ! current_user_can( 'edit_pages' ) ) {
823
				$portfolios_title = $this->create_menu_item_anchor(
824
					'ab-item ab-primary mb-icon',
825
					'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
826
					esc_html__( 'Portfolio', 'jetpack' ),
827
					'wp-admin-bar-edit-portfolio'
828
				);
829
			}
830
831
			$wp_admin_bar->add_menu( array(
832
				'parent' => 'publish',
833
				'id'     => 'new-jetpack-portfolio',
834
				'title'  => $portfolios_title,
835
				'meta'   => array(
836
					'class' => 'inline-action',
837
				),
838
			) );
839
		}
840
841
		if ( current_user_can( 'edit_theme_options' ) ) {
842
			// Look and Feel group
843
			$wp_admin_bar->add_group( array(
844
				'parent' => 'blog',
845
				'id'     => 'look-and-feel',
846
			) );
847
848
			// Look and Feel header
849
			$wp_admin_bar->add_menu( array(
850
				'parent' => 'look-and-feel',
851
				'id'     => 'look-and-feel-header',
852
				'title'  => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
853
				'meta'   => array(
854
					'class' => 'ab-submenu-header',
855
				),
856
			) );
857
858
			if ( is_admin() ) {
859
				// In wp-admin the `return` query arg will return to that page after closing the Customizer
860
				$customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() );
861
			} else {
862
				// On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing
863
				// non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS
864
				$current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
865
				$customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() );
866
			}
867
868
			$theme_title = $this->create_menu_item_pair(
869
				array(
870
					'url'   => 'https://wordpress.com/design/' . esc_attr( $this->primary_site_slug ),
871
					'id'    => 'wp-admin-bar-themes',
872
					'label' => esc_html__( 'Themes', 'jetpack' ),
873
				),
874
				array(
875
					'url'   => $customizer_url,
876
					'id'    => 'wp-admin-bar-cmz',
877
					'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ),
878
				)
879
			);
880
			$meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' );
881
			$href = false;
882
883
			$wp_admin_bar->add_menu( array(
884
				'parent' => 'look-and-feel',
885
				'id'     => 'themes',
886
				'title'  => $theme_title,
887
				'href'   => $href,
888
				'meta'   => $meta
889
			) );
890
		}
891
892
		if ( current_user_can( 'manage_options' ) ) {
893
			// Configuration group
894
			$wp_admin_bar->add_group( array(
895
				'parent' => 'blog',
896
				'id'     => 'configuration',
897
			) );
898
899
			// Configuration header
900
			$wp_admin_bar->add_menu( array(
901
				'parent' => 'configuration',
902
				'id'     => 'configuration-header',
903
				'title'  => esc_html__( 'Configure', 'admin bar menu group label', 'jetpack' ),
904
				'meta'   => array(
905
					'class' => 'ab-submenu-header',
906
				),
907
			) );
908
909 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
910
				$wp_admin_bar->add_menu( array(
911
					'parent' => 'configuration',
912
					'id'     => 'sharing',
913
					'title'  => esc_html__( 'Sharing', 'jetpack' ),
914
					'href'   => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
915
					'meta'   => array(
916
						'class' => 'mb-icon',
917
					),
918
				) );
919
			}
920
921
			$people_title = $this->create_menu_item_pair(
922
				array(
923
					'url'   => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
924
					'id'    => 'wp-admin-bar-people',
925
					'label' => esc_html__( 'People', 'jetpack' ),
926
				),
927
				array(
928
					'url'   => admin_url( 'user-new.php' ),
929
					'id'    => 'wp-admin-bar-people-add',
930
					'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ),
931
				)
932
			);
933
934
			$wp_admin_bar->add_menu( array(
935
				'parent' => 'configuration',
936
				'id'     => 'users-toolbar',
937
				'title'  => $people_title,
938
				'href'   => false,
939
				'meta'   => array(
940
					'class' => 'inline-action',
941
				),
942
			) );
943
944
			$plugins_title = $this->create_menu_item_pair(
945
				array(
946
					'url'   => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
947
					'id'    => 'wp-admin-bar-plugins',
948
					'label' => esc_html__( 'Plugins', 'jetpack' ),
949
				),
950
				array(
951
					'url'   => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ),
952
					'id'    => 'wp-admin-bar-plugins-add',
953
					'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ),
954
				)
955
			);
956
957
			$wp_admin_bar->add_menu( array(
958
				'parent' => 'configuration',
959
				'id'     => 'plugins',
960
				'title'  => $plugins_title,
961
				'href'   => false,
962
				'meta'   => array(
963
					'class' => 'inline-action',
964
				),
965
			) );
966
967
			if ( jetpack_is_atomic_site() ) {
968
				$domain_title = $this->create_menu_item_pair(
969
					array(
970
						'url'   => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
971
						'id'    => 'wp-admin-bar-domains',
972
						'label' => esc_html__( 'Domains', 'jetpack' ),
973
					),
974
					array(
975
						'url'   => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
976
						'id'    => 'wp-admin-bar-domains-add',
977
						'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
978
					)
979
				);
980
				$wp_admin_bar->add_menu( array(
981
					'parent' => 'configuration',
982
					'id'     => 'domains',
983
					'title'  => $domain_title,
984
					'href'   => false,
985
					'meta'   => array(
986
						'class' => 'inline-action',
987
					),
988
				) );
989
			}
990
991
			$wp_admin_bar->add_menu( array(
992
				'parent' => 'configuration',
993
				'id'     => 'blog-settings',
994
				'title'  => esc_html__( 'Settings', 'jetpack' ),
995
				'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
996
				'meta'   => array(
997
					'class' => 'mb-icon',
998
				),
999
			) );
1000
1001
			if ( ! is_admin() ) {
1002
				$wp_admin_bar->add_menu( array(
1003
					'parent' => 'configuration',
1004
					'id'     => 'legacy-dashboard',
1005
					'title'  => esc_html__( 'Dashboard', 'jetpack' ),
1006
					'href'   => admin_url(),
1007
					'meta'   => array(
1008
						'class' => 'mb-icon',
1009
					),
1010
				) );
1011
			}
1012
1013
			// Restore dashboard menu toggle that is needed on mobile views.
1014
			if ( is_admin() ) {
1015
				$wp_admin_bar->add_menu( array(
1016
				'id'    => 'menu-toggle',
1017
				'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>',
1018
				'href'  => '#',
1019
				) );
1020
			}
1021
1022
			/**
1023
			 * Fires when menu items are added to the masterbar "My Sites" menu.
1024
			 *
1025
			 * @since 5.4
1026
			 */
1027
			do_action( 'jetpack_masterbar' );
1028
		}
1029
	}
1030
}
1031