Completed
Push — add/tracking-for-blogid ( 4748d9...d9697b )
by
unknown
20:38 queued 12:38
created

A8C_WPCOM_Masterbar::add_my_sites_submenu()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 481

Duplication

Lines 108
Ratio 22.45 %

Importance

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