Completed
Push — fix/add-people-link ( 0f6c6a...c735e5 )
by
unknown
486:12 queued 476:02
created

A8C_WPCOM_Masterbar   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 935
Duplicated Lines 12.19 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

19 Methods

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