Completed
Push — add/double-encode-message ( 8b6530...2d4e84 )
by
unknown
14:26 queued 05:57
created

A8C_WPCOM_Masterbar::add_my_sites_submenu()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 481

Duplication

Lines 86
Ratio 17.88 %

Importance

Changes 0
Metric Value
cc 27
nc 1410048
nop 1
dl 86
loc 481
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
		$help_link = 'https://jetpack.com/support/';
514
515
		if ( jetpack_is_atomic_site() ) {
516
			$help_link = 'https://wordpress.com/help';
517
		}
518
519
		$wp_admin_bar->add_menu( array(
520
			'parent' => $id,
521
			'id'     => 'help',
522
			'title'  => esc_html__( 'Help', 'jetpack' ),
523
			'href'   => $help_link,
524
			'meta'   => array(
525
				'class' => 'mb-icon user-info-item',
526
			),
527
		) );
528
	}
529
530
	public function add_write_button( $wp_admin_bar ) {
531
		$current_user = wp_get_current_user();
532
533
		$posting_blog_id = get_current_blog_id();
534
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
535
			$posting_blog_id = $current_user->primary_blog;
536
		}
537
538
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
539
540
		if ( ! $posting_blog_id || ! $user_can_post ) {
541
			return;
542
		}
543
544
		$blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
545
546
		$wp_admin_bar->add_menu( array(
547
			'parent'    => 'top-secondary',
548
			'id' => 'ab-new-post',
549
			'href' => $blog_post_page,
550
			'title' => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
551
			'meta'  => array(
552
				'class' => 'mb-trackable',
553
			)
554
		) );
555
	}
556
557
	public function add_my_sites_submenu( $wp_admin_bar ) {
558
		$current_user = wp_get_current_user();
559
560
		$blog_name = get_bloginfo( 'name' );
561
		if ( empty( $blog_name ) ) {
562
			$blog_name = $this->primary_site_slug;
563
		}
564
565
		if ( mb_strlen( $blog_name ) > 20 ) {
566
			$blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
567
		}
568
569
		$wp_admin_bar->add_menu( array(
570
			'parent' => 'root-default',
571
			'id'    => 'blog',
572
			'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
573
			'href'  => '#',
574
			'meta'  => array(
575
				'class' => 'my-sites mb-trackable',
576
			),
577
		) );
578
579
		if ( $this->user_site_count > 1 ) {
580
			$wp_admin_bar->add_menu( array(
581
				'parent' => 'blog',
582
				'id'     => 'switch-site',
583
				'title'  => esc_html__( 'Switch Site', 'jetpack' ),
584
				'href'   => 'https://wordpress.com/sites',
585
			) );
586
		} else {
587
			$wp_admin_bar->add_menu( array(
588
				'parent' => 'blog',
589
				'id'     => 'new-site',
590
				'title'  => esc_html__( '+ Add New WordPress', 'jetpack' ),
591
				'href'   => 'https://wordpress.com/start?ref=admin-bar-logged-in',
592
			) );
593
		}
594
595
		if ( is_user_member_of_blog( $current_user->ID ) ) {
596
			$blavatar = '';
597
			$class    = 'current-site';
598
599
			if ( has_site_icon() ) {
600
				$src = get_site_icon_url();
601
				$blavatar = '<img class="avatar" src="'. esc_attr( $src ) . '" alt="Current site avatar">';
602
				$class = 'has-blavatar';
603
			}
604
605
			$blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>';
606
			$blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
607
			$blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
608
609
			$wp_admin_bar->add_menu( array(
610
				'parent' => 'blog',
611
				'id'     => 'blog-info',
612
				'title'  => $blog_info,
613
				'href'   => esc_url( trailingslashit( $this->primary_site_url ) ),
614
				'meta'   => array(
615
					'class' => $class,
616
				),
617
			) );
618
		}
619
620
		// Site Preview
621
		if ( is_admin() ) {
622
			$wp_admin_bar->add_menu( array(
623
				'parent' => 'blog',
624
				'id'     => 'site-view',
625
				'title'  => __( 'View Site', 'jetpack' ),
626
				'href'   => home_url(),
627
				'meta'   => array(
628
					'class' => 'mb-icon',
629
					'target' => '_blank',
630
				),
631
			) );
632
		}
633
634
		// Stats
635 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) ) {
636
			$wp_admin_bar->add_menu( array(
637
				'parent' => 'blog',
638
				'id'     => 'blog-stats',
639
				'title'  => esc_html__( 'Stats', 'jetpack' ),
640
				'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
641
				'meta'   => array(
642
					'class' => 'mb-icon',
643
				),
644
			) );
645
		}
646
647
		if ( current_user_can( 'manage_options' ) ) {
648
			$wp_admin_bar->add_menu( array(
649
				'parent' => 'blog',
650
				'id'     => 'activity',
651
				'title'  => esc_html__( 'Activity', 'jetpack' ),
652
				'href'   => 'https://wordpress.com/activity-log/' . esc_attr( $this->primary_site_slug ),
653
				'meta'   => array(
654
					'class' => 'mb-icon',
655
				),
656
			) );
657
		}
658
659
		// Add Calypso plans link and plan type indicator
660
		if ( is_user_member_of_blog( $current_user->ID ) ) {
661
			$plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
662
			$label = esc_html__( 'Plan', 'jetpack' );
663
			$plan = Jetpack::get_active_plan();
664
665
			$plan_title = $this->create_menu_item_pair(
666
				array(
667
					'url'   => $plans_url,
668
					'id'    => 'wp-admin-bar-plan',
669
					'label' => $label,
670
				),
671
				array(
672
					'url'   => $plans_url,
673
					'id'    => 'wp-admin-bar-plan-badge',
674
					'label' => $plan['product_name_short']
675
				)
676
			);
677
678
			$wp_admin_bar->add_menu( array(
679
				'parent' => 'blog',
680
				'id'     => 'plan',
681
				'title'  => $plan_title,
682
				'meta'   => array(
683
					'class' => 'inline-action',
684
				),
685
			) );
686
		}
687
688
		// Publish group
689
		$wp_admin_bar->add_group( array(
690
			'parent' => 'blog',
691
			'id'     => 'publish',
692
		) );
693
694
		// Publish header
695
		$wp_admin_bar->add_menu( array(
696
			'parent' => 'publish',
697
			'id'     => 'publish-header',
698
			'title'  => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ),
699
			'meta'   => array(
700
				'class' => 'ab-submenu-header',
701
			),
702
		) );
703
704
		// Pages
705
		$pages_title = $this->create_menu_item_pair(
706
			array(
707
				'url'   => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
708
				'id'    => 'wp-admin-bar-edit-page',
709
				'label' => esc_html__( 'Site Pages', 'jetpack' ),
710
			),
711
			array(
712
				'url'   => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
713
				'id'    => 'wp-admin-bar-new-page-badge',
714
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
715
			)
716
		);
717
718
		if ( ! current_user_can( 'edit_pages' ) ) {
719
			$pages_title = $this->create_menu_item_anchor(
720
				'ab-item ab-primary mb-icon',
721
				'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
722
				esc_html__( 'Site Pages', 'jetpack' ),
723
				'wp-admin-bar-edit-page'
724
			);
725
		}
726
727
		$wp_admin_bar->add_menu( array(
728
			'parent' => 'publish',
729
			'id'     => 'new-page',
730
			'title'  => $pages_title,
731
			'meta'   => array(
732
				'class' => 'inline-action',
733
			),
734
		) );
735
736
		// Blog Posts
737
		$posts_title = $this->create_menu_item_pair(
738
			array(
739
				'url'   => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
740
				'id'    => 'wp-admin-bar-edit-post',
741
				'label' => esc_html__( 'Blog Posts', 'jetpack' ),
742
			),
743
			array(
744
				'url'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
745
				'id'    => 'wp-admin-bar-new-post-badge',
746
				'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
747
			)
748
		);
749
750
		if ( ! current_user_can( 'edit_posts' ) ) {
751
			$posts_title = $this->create_menu_item_anchor(
752
				'ab-item ab-primary mb-icon',
753
				'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
754
				esc_html__( 'Blog Posts', 'jetpack' ),
755
				'wp-admin-bar-edit-post'
756
			);
757
		}
758
759
		$wp_admin_bar->add_menu( array(
760
			'parent' => 'publish',
761
			'id'     => 'new-post',
762
			'title'  => $posts_title,
763
			'meta'   => array(
764
				'class' => 'inline-action mb-trackable',
765
			),
766
		) );
767
768
		// Comments
769
		if ( current_user_can( 'moderate_comments' ) ) {
770
			$wp_admin_bar->add_menu( array(
771
				'parent' => 'publish',
772
				'id'     => 'comments',
773
				'title'  => __( 'Comments' ),
774
				'href'   => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ),
775
				'meta'   => array(
776
					'class' => 'mb-icon',
777
				),
778
			) );
779
		}
780
781
		// Testimonials
782 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
783
			$testimonials_title = $this->create_menu_item_pair(
784
				array(
785
					'url'   => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
786
					'id'    => 'wp-admin-bar-edit-testimonial',
787
					'label' => esc_html__( 'Testimonials', 'jetpack' ),
788
				),
789
				array(
790
					'url'   => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
791
					'id'    => 'wp-admin-bar-new-testimonial',
792
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
793
				)
794
			);
795
796
			if ( ! current_user_can( 'edit_pages' ) ) {
797
				$testimonials_title = $this->create_menu_item_anchor(
798
					'ab-item ab-primary mb-icon',
799
					'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
800
					esc_html__( 'Testimonials', 'jetpack' ),
801
					'wp-admin-bar-edit-testimonial'
802
				);
803
			}
804
805
			$wp_admin_bar->add_menu( array(
806
				'parent' => 'publish',
807
				'id'     => 'new-jetpack-testimonial',
808
				'title'  => $testimonials_title,
809
				'meta'   => array(
810
					'class' => 'inline-action',
811
				),
812
			) );
813
		}
814
815
		// Portfolio
816 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
817
			$portfolios_title = $this->create_menu_item_pair(
818
				array(
819
					'url'   => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
820
					'id'    => 'wp-admin-bar-edit-portfolio',
821
					'label' => esc_html__( 'Portfolio', 'jetpack' ),
822
				),
823
				array(
824
					'url'   => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
825
					'id'    => 'wp-admin-bar-new-portfolio',
826
					'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
827
				)
828
			);
829
830
			if ( ! current_user_can( 'edit_pages' ) ) {
831
				$portfolios_title = $this->create_menu_item_anchor(
832
					'ab-item ab-primary mb-icon',
833
					'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
834
					esc_html__( 'Portfolio', 'jetpack' ),
835
					'wp-admin-bar-edit-portfolio'
836
				);
837
			}
838
839
			$wp_admin_bar->add_menu( array(
840
				'parent' => 'publish',
841
				'id'     => 'new-jetpack-portfolio',
842
				'title'  => $portfolios_title,
843
				'meta'   => array(
844
					'class' => 'inline-action',
845
				),
846
			) );
847
		}
848
849
		if ( current_user_can( 'edit_theme_options' ) ) {
850
			// Look and Feel group
851
			$wp_admin_bar->add_group( array(
852
				'parent' => 'blog',
853
				'id'     => 'look-and-feel',
854
			) );
855
856
			// Look and Feel header
857
			$wp_admin_bar->add_menu( array(
858
				'parent' => 'look-and-feel',
859
				'id'     => 'look-and-feel-header',
860
				'title'  => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
861
				'meta'   => array(
862
					'class' => 'ab-submenu-header',
863
				),
864
			) );
865
866
			if ( is_admin() ) {
867
				// In wp-admin the `return` query arg will return to that page after closing the Customizer
868
				$customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() );
869
			} else {
870
				// On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing
871
				// non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS
872
				$current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
873
				$customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() );
874
			}
875
876
			$theme_title = $this->create_menu_item_pair(
877
				array(
878
					'url'   => $customizer_url,
879
					'id'    => 'wp-admin-bar-cmz',
880
					'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ),
881
				),
882
				array(
883
					'url'   => 'https://wordpress.com/themes/' . esc_attr( $this->primary_site_slug ),
884
					'id'    => 'wp-admin-bar-themes',
885
					'label' => esc_html__( 'Themes', 'jetpack' ),
886
				)
887
			);
888
			$meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' );
889
			$href = false;
890
891
			$wp_admin_bar->add_menu( array(
892
				'parent' => 'look-and-feel',
893
				'id'     => 'themes',
894
				'title'  => $theme_title,
895
				'href'   => $href,
896
				'meta'   => $meta
897
			) );
898
		}
899
900
		if ( current_user_can( 'manage_options' ) ) {
901
			// Configuration group
902
			$wp_admin_bar->add_group( array(
903
				'parent' => 'blog',
904
				'id'     => 'configuration',
905
			) );
906
907
			// Configuration header
908
			$wp_admin_bar->add_menu( array(
909
				'parent' => 'configuration',
910
				'id'     => 'configuration-header',
911
				'title'  => esc_html__( 'Configure', 'admin bar menu group label', 'jetpack' ),
912
				'meta'   => array(
913
					'class' => 'ab-submenu-header',
914
				),
915
			) );
916
917 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
918
				$wp_admin_bar->add_menu( array(
919
					'parent' => 'configuration',
920
					'id'     => 'sharing',
921
					'title'  => esc_html__( 'Sharing', 'jetpack' ),
922
					'href'   => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
923
					'meta'   => array(
924
						'class' => 'mb-icon',
925
					),
926
				) );
927
			}
928
929
			$people_title = $this->create_menu_item_pair(
930
				array(
931
					'url'   => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
932
					'id'    => 'wp-admin-bar-people',
933
					'label' => esc_html__( 'People', 'jetpack' ),
934
				),
935
				array(
936
					'url'   => admin_url( 'user-new.php' ),
937
					'id'    => 'wp-admin-bar-people-add',
938
					'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ),
939
				)
940
			);
941
942
			$wp_admin_bar->add_menu( array(
943
				'parent' => 'configuration',
944
				'id'     => 'users-toolbar',
945
				'title'  => $people_title,
946
				'href'   => false,
947
				'meta'   => array(
948
					'class' => 'inline-action',
949
				),
950
			) );
951
952
			$plugins_title = $this->create_menu_item_pair(
953
				array(
954
					'url'   => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
955
					'id'    => 'wp-admin-bar-plugins',
956
					'label' => esc_html__( 'Plugins', 'jetpack' ),
957
				),
958
				array(
959
					'url'   => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ),
960
					'id'    => 'wp-admin-bar-plugins-add',
961
					'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ),
962
				)
963
			);
964
965
			$wp_admin_bar->add_menu( array(
966
				'parent' => 'configuration',
967
				'id'     => 'plugins',
968
				'title'  => $plugins_title,
969
				'href'   => false,
970
				'meta'   => array(
971
					'class' => 'inline-action',
972
				),
973
			) );
974
975
			if ( jetpack_is_atomic_site() ) {
976
				$domain_title = $this->create_menu_item_pair(
977
					array(
978
						'url'   => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
979
						'id'    => 'wp-admin-bar-domains',
980
						'label' => esc_html__( 'Domains', 'jetpack' ),
981
					),
982
					array(
983
						'url'   => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
984
						'id'    => 'wp-admin-bar-domains-add',
985
						'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
986
					)
987
				);
988
				$wp_admin_bar->add_menu( array(
989
					'parent' => 'configuration',
990
					'id'     => 'domains',
991
					'title'  => $domain_title,
992
					'href'   => false,
993
					'meta'   => array(
994
						'class' => 'inline-action',
995
					),
996
				) );
997
			}
998
999
			$wp_admin_bar->add_menu( array(
1000
				'parent' => 'configuration',
1001
				'id'     => 'blog-settings',
1002
				'title'  => esc_html__( 'Settings', 'jetpack' ),
1003
				'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
1004
				'meta'   => array(
1005
					'class' => 'mb-icon',
1006
				),
1007
			) );
1008
1009
			if ( ! is_admin() ) {
1010
				$wp_admin_bar->add_menu( array(
1011
					'parent' => 'configuration',
1012
					'id'     => 'legacy-dashboard',
1013
					'title'  => esc_html__( 'Dashboard', 'jetpack' ),
1014
					'href'   => admin_url(),
1015
					'meta'   => array(
1016
						'class' => 'mb-icon',
1017
					),
1018
				) );
1019
			}
1020
1021
			// Restore dashboard menu toggle that is needed on mobile views.
1022
			if ( is_admin() ) {
1023
				$wp_admin_bar->add_menu( array(
1024
				'id'    => 'menu-toggle',
1025
				'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>',
1026
				'href'  => '#',
1027
				) );
1028
			}
1029
1030
			/**
1031
			 * Fires when menu items are added to the masterbar "My Sites" menu.
1032
			 *
1033
			 * @since 5.4.0
1034
			 */
1035
			do_action( 'jetpack_masterbar' );
1036
		}
1037
	}
1038
}
1039