Completed
Push — add/hide-settings-for-unavaila... ( 7255d2...605ccc )
by
unknown
516:40 queued 507:36
created

A8C_WPCOM_Masterbar   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 922
Duplicated Lines 12.36 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 114
loc 922
rs 5
c 0
b 0
f 0
wmc 66
lcom 1
cbo 3

19 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 36 5
A get_rtl_admin_bar_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 is_automated_transfer_site() 0 13 3
A wpcom_static_url() 0 6 1
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 153 4
B add_write_button() 0 23 4
F add_my_sites_submenu() 108 441 24

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