Completed
Push — kraftbj-patch-1 ( 083d42 )
by
unknown
10:10
created

A8C_WPCOM_Masterbar   C

Complexity

Total Complexity 71

Size/Duplication

Total Lines 984
Duplicated Lines 10.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 103
loc 984
rs 5
c 0
b 0
f 0
wmc 71
lcom 1
cbo 3

20 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 51 6
A maybe_logout_user_from_wpcom() 0 5 3
A get_rtl_admin_bar_class() 0 3 1
A admin_body_class() 0 3 1
A remove_core_styles() 0 3 1
A is_rtl() 0 3 2
A add_styles_and_scripts() 0 21 3
A wpcom_static_url() 0 11 2
A replace_core_masterbar() 0 10 2
A clear_core_masterbar() 0 5 2
A build_wpcom_masterbar() 0 16 2
B get_locale() 6 18 6
A add_notifications() 0 21 2
B add_reader_submenu() 0 82 1
A create_menu_item_pair() 0 9 1
A create_menu_item_anchor() 0 3 1
A wpcom_adminbar_add_secondary_groups() 0 23 1
B add_me_submenu() 0 154 4
B add_write_button() 0 23 4
F add_my_sites_submenu() 97 469 26

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like A8C_WPCOM_Masterbar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use A8C_WPCOM_Masterbar, and based on these observations, apply Extract Interface, too.

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