Completed
Push — fix/search/clear-year-when-rem... ( 2fa4f5 )
by Alex
08:43
created

A8C_WPCOM_Masterbar::admin_body_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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