Completed
Push — kraftbj-patch-1 ( 0d731f )
by
unknown
09:45
created

A8C_WPCOM_Masterbar   C

Complexity

Total Complexity 71

Size/Duplication

Total Lines 992
Duplicated Lines 10.38 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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