Completed
Push — add/connect-splash-content ( ec1611...3bc2bd )
by
unknown
22:10 queued 11:17
created

A8C_WPCOM_Masterbar::add_reader_submenu()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 59
nc 1
nop 1
dl 0
loc 85
rs 8.6875
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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