Completed
Push — update/google-translate-layout ( 38cf64...f54e3d )
by Jeremy
14:26
created

A8C_WPCOM_Masterbar::add_notifications()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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