Completed
Push — kraftbj-patch-2 ( 82c983...ae9d16 )
by
unknown
513:58 queued 503:20
created

A8C_WPCOM_Masterbar   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 926
Duplicated Lines 12.31 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

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 153 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' => 'mb-icon user-info-item',
437
			),
438
		) );
439
440
		$wp_admin_bar->add_menu( array(
441
			'parent' => $id,
442
			'id'     => 'next-steps',
443
			'title'  => __( 'Next Steps', 'jetpack' ),
444
			'href'   => 'https://wordpress.com/me/next',
445
			'meta'   => array(
446
				'class' => 'mb-icon user-info-item',
447
			),
448
		) );
449
450
		$help_link = 'https://jetpack.com/support/';
451
452
		if ( $this->is_automated_transfer_site() ) {
453
			$help_link = 'https://wordpress.com/help';
454
		}
455
456
		$wp_admin_bar->add_menu( array(
457
			'parent' => $id,
458
			'id'     => 'help',
459
			'title'  => __( 'Help', 'jetpack' ),
460
			'href'   => $help_link,
461
			'meta'   => array(
462
				'class' => 'mb-icon user-info-item',
463
			),
464
		) );
465
	}
466
467
	public function add_write_button( $wp_admin_bar ) {
468
		$current_user = wp_get_current_user();
469
470
		$posting_blog_id = get_current_blog_id();
471
		if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
472
			$posting_blog_id = $current_user->primary_blog;
473
		}
474
475
		$user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
476
477
		if ( ! $posting_blog_id || ! $user_can_post ) {
478
			return;
479
		}
480
481
		$blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
482
483
		$wp_admin_bar->add_menu( array(
484
			'parent'    => 'top-secondary',
485
			'id' => 'ab-new-post',
486
			'href' => $blog_post_page,
487
			'title' => '<span>' . __( 'Write', 'jetpack' ) . '</span>',
488
		) );
489
	}
490
491
	public function add_my_sites_submenu( $wp_admin_bar ) {
492
		$current_user = wp_get_current_user();
493
494
		$blog_name = get_bloginfo( 'name' );
495
		if ( empty( $blog_name ) ) {
496
			$blog_name = $this->primary_site_slug;
497
		}
498
499
		if ( mb_strlen( $blog_name ) > 20 ) {
500
			$blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
501
		}
502
503
		$wp_admin_bar->add_menu( array(
504
			'parent' => 'root-default',
505
			'id'    => 'blog',
506
			'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
507
			'href'  => '#',
508
			'meta'  => array(
509
				'class' => 'my-sites',
510
			),
511
		) );
512
513
		if ( $this->user_site_count > 1 ) {
514
			$wp_admin_bar->add_menu( array(
515
				'parent' => 'blog',
516
				'id'     => 'switch-site',
517
				'title'  => __( 'Switch Site', 'jetpack' ),
518
				'href'   => 'https://wordpress.com/sites',
519
			) );
520
		} else {
521
			$wp_admin_bar->add_menu( array(
522
				'parent' => 'blog',
523
				'id'     => 'new-site',
524
				'title'  => __( '+ Add New WordPress', 'jetpack' ),
525
				'href'   => 'https://wordpress.com/start?ref=admin-bar-logged-in',
526
			) );
527
		}
528
529
		if ( is_user_member_of_blog( $current_user->ID ) ) {
530
			$blavatar = '';
531
			$class    = 'current-site';
532
533
			if ( has_site_icon() ) {
534
				$src = get_site_icon_url();
535
				$blavatar = '<img class="avatar" src="'. esc_attr( $src ) . '" alt="Current site avatar">';
536
				$class = 'has-blavatar';
537
			}
538
539
			$blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>';
540
			$blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
541
			$blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
542
543
			$wp_admin_bar->add_menu( array(
544
				'parent' => 'blog',
545
				'id'     => 'blog-info',
546
				'title'  => $blog_info,
547
				'href'   => esc_url( trailingslashit( $this->primary_site_url ) ),
548
				'meta'   => array(
549
					'class' => $class,
550
				),
551
			) );
552
		}
553
554
		// Stats
555 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) ) {
556
			$wp_admin_bar->add_menu( array(
557
				'parent' => 'blog',
558
				'id'     => 'blog-stats',
559
				'title'  => __( 'Stats', 'jetpack' ),
560
				'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
561
				'meta'   => array(
562
					'class' => 'mb-icon',
563
				),
564
			) );
565
		}
566
567
		// Add Calypso plans link and plan type indicator
568
		if ( is_user_member_of_blog( $current_user->ID ) ) {
569
			$plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
570
			$label = __( 'Plan', 'jetpack' );
571
			$plan = Jetpack::get_active_plan();
572
573
			$plan_title = $this->create_menu_item_pair(
574
				array(
575
					'url'   => $plans_url,
576
					'id'    => 'wp-admin-bar-plan',
577
					'label' => $label,
578
				),
579
				array(
580
					'url'   => $plans_url,
581
					'id'    => 'wp-admin-bar-plan-badge',
582
					'label' => $plan['product_name_short']
583
				)
584
			);
585
586
			$wp_admin_bar->add_menu( array(
587
				'parent' => 'blog',
588
				'id'     => 'plan',
589
				'title'  => $plan_title,
590
				'meta'   => array(
591
					'class' => 'inline-action',
592
				),
593
			) );
594
		}
595
596
		// Publish group
597
		$wp_admin_bar->add_group( array(
598
			'parent' => 'blog',
599
			'id'     => 'publish',
600
		) );
601
602
		// Publish header
603
		$wp_admin_bar->add_menu( array(
604
			'parent' => 'publish',
605
			'id'     => 'publish-header',
606
			'title'  => _x( 'Publish', 'admin bar menu group label', 'jetpack' ),
607
			'meta'   => array(
608
				'class' => 'ab-submenu-header',
609
			),
610
		) );
611
612
		// Blog Posts
613
		$posts_title = $this->create_menu_item_pair(
614
			array(
615
				'url'   => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
616
				'id'    => 'wp-admin-bar-edit-post',
617
				'label' => __( 'Blog Posts', 'jetpack' ),
618
			),
619
			array(
620
				'url'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
621
				'id'    => 'wp-admin-bar-new-post',
622
				'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ),
623
			)
624
		);
625
626
		if ( ! current_user_can( 'edit_posts' ) ) {
627
			$posts_title = $this->create_menu_item_anchor(
628
				'ab-item ab-primary mb-icon',
629
				'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
630
				__( 'Blog Posts', 'jetpack' ),
631
				'wp-admin-bar-edit-post'
632
			);
633
		}
634
635
		$wp_admin_bar->add_menu( array(
636
			'parent' => 'publish',
637
			'id'     => 'new-post',
638
			'title'  => $posts_title,
639
			'meta'   => array(
640
				'class' => 'inline-action',
641
			),
642
		) );
643
644
		// Pages
645
		$pages_title = $this->create_menu_item_pair(
646
			array(
647
				'url'   => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
648
				'id'    => 'wp-admin-bar-edit-page',
649
				'label' => __( 'Pages', 'jetpack' ),
650
			),
651
			array(
652
				'url'   => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
653
				'id'    => 'wp-admin-bar-new-page',
654
				'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ),
655
			)
656
		);
657
658
		if ( ! current_user_can( 'edit_pages' ) ) {
659
			$pages_title = $this->create_menu_item_anchor(
660
				'ab-item ab-primary mb-icon',
661
				'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
662
				__( 'Pages', 'jetpack' ),
663
				'wp-admin-bar-edit-page'
664
			);
665
		}
666
667
		$wp_admin_bar->add_menu( array(
668
			'parent' => 'publish',
669
			'id'     => 'new-page',
670
			'title'  => $pages_title,
671
			'meta'   => array(
672
				'class' => 'inline-action',
673
			),
674
		) );
675
676
		// Testimonials
677 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
678
			$testimonials_title = $this->create_menu_item_pair(
679
				array(
680
					'url'   => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
681
					'id'    => 'wp-admin-bar-edit-testimonial',
682
					'label' => __( 'Testimonials', 'jetpack' ),
683
				),
684
				array(
685
					'url'   => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
686
					'id'    => 'wp-admin-bar-new-testimonial',
687
					'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
688
				)
689
			);
690
691
			if ( ! current_user_can( 'edit_pages' ) ) {
692
				$testimonials_title = $this->create_menu_item_anchor(
693
					'ab-item ab-primary mb-icon',
694
					'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
695
					__( 'Testimonials', 'jetpack' ),
696
					'wp-admin-bar-edit-testimonial'
697
				);
698
			}
699
700
			$wp_admin_bar->add_menu( array(
701
				'parent' => 'publish',
702
				'id'     => 'new-jetpack-testimonial',
703
				'title'  => $testimonials_title,
704
				'meta'   => array(
705
					'class' => 'inline-action',
706
				),
707
			) );
708
		}
709
710
		// Portfolio
711 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
712
			$portfolios_title = $this->create_menu_item_pair(
713
				array(
714
					'url'   => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
715
					'id'    => 'wp-admin-bar-edit-portfolio',
716
					'label' => __( 'Portfolio', 'jetpack' ),
717
				),
718
				array(
719
					'url'   => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
720
					'id'    => 'wp-admin-bar-new-portfolio',
721
					'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
722
				)
723
			);
724
725
			if ( ! current_user_can( 'edit_pages' ) ) {
726
				$portfolios_title = $this->create_menu_item_anchor(
727
					'ab-item ab-primary mb-icon',
728
					'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
729
					__( 'Portfolio', 'jetpack' ),
730
					'wp-admin-bar-edit-portfolio'
731
				);
732
			}
733
734
			$wp_admin_bar->add_menu( array(
735
				'parent' => 'publish',
736
				'id'     => 'new-jetpack-portfolio',
737
				'title'  => $portfolios_title,
738
				'meta'   => array(
739
					'class' => 'inline-action',
740
				),
741
			) );
742
		}
743
744
		if ( current_user_can( 'edit_theme_options' ) ) {
745
			// Look and Feel group
746
			$wp_admin_bar->add_group( array(
747
				'parent' => 'blog',
748
				'id'     => 'look-and-feel',
749
			) );
750
751
			// Look and Feel header
752
			$wp_admin_bar->add_menu( array(
753
				'parent' => 'look-and-feel',
754
				'id'     => 'look-and-feel-header',
755
				'title'  => _x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
756
				'meta'   => array(
757
					'class' => 'ab-submenu-header',
758
				),
759
			) );
760
761
			if ( is_admin() ) {
762
				// In wp-admin the `return` query arg will return to that page after closing the Customizer
763
				$customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() );
764
			} else {
765
				// On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing
766
				// non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS
767
				$current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
768
				$customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() );
769
			}
770
771
			$theme_title = $this->create_menu_item_pair(
772
				array(
773
					'url'   => 'https://wordpress.com/design/' . esc_attr( $this->primary_site_slug ),
774
					'id'    => 'wp-admin-bar-themes',
775
					'label' => __( 'Themes', 'jetpack' ),
776
				),
777
				array(
778
					'url'   => $customizer_url,
779
					'id'    => 'wp-admin-bar-cmz',
780
					'label' => _x( 'Customize', 'admin bar customize item label', 'jetpack' ),
781
				)
782
			);
783
			$meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' );
784
			$href = false;
785
786
			$wp_admin_bar->add_menu( array(
787
				'parent' => 'look-and-feel',
788
				'id'     => 'themes',
789
				'title'  => $theme_title,
790
				'href'   => $href,
791
				'meta'   => $meta
792
			) );
793
794 View Code Duplication
			if ( current_theme_supports( 'menus' ) ) {
795
				$wp_admin_bar->add_menu( array(
796
					'parent' => 'look-and-feel',
797
					'id'     => 'menus',
798
					'title'  => __( 'Menus', 'jetpack' ),
799
					'href'   => 'https://wordpress.com/menus/' . esc_attr( $this->primary_site_slug ),
800
					'meta' => array(
801
						'class' => 'mb-icon',
802
					),
803
				) );
804
			}
805
		}
806
807
		if ( current_user_can( 'manage_options' ) ) {
808
			// Configuration group
809
			$wp_admin_bar->add_group( array(
810
				'parent' => 'blog',
811
				'id'     => 'configuration',
812
			) );
813
814
			// Configuration header
815
			$wp_admin_bar->add_menu( array(
816
				'parent' => 'configuration',
817
				'id'     => 'configuration-header',
818
				'title'  => __( 'Configure', 'admin bar menu group label', 'jetpack' ),
819
				'meta'   => array(
820
					'class' => 'ab-submenu-header',
821
				),
822
			) );
823
824 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
825
				$wp_admin_bar->add_menu( array(
826
					'parent' => 'configuration',
827
					'id'     => 'sharing',
828
					'title'  => __( 'Sharing', 'jetpack' ),
829
					'href'   => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
830
					'meta'   => array(
831
						'class' => 'mb-icon',
832
					),
833
				) );
834
			}
835
836
			$people_title = $this->create_menu_item_pair(
837
				array(
838
					'url'   => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
839
					'id'    => 'wp-admin-bar-people',
840
					'label' => __( 'People', 'jetpack' ),
841
				),
842
				array(
843
					'url'   => '//' . esc_attr( $this->primary_site_url ) . '/wp-admin/user-new.php',
844
					'id'    => 'wp-admin-bar-people-add',
845
					'label' => _x( 'Add', 'admin bar people item label', 'jetpack' ),
846
				)
847
			);
848
849
			$wp_admin_bar->add_menu( array(
850
				'parent' => 'configuration',
851
				'id'     => 'users-toolbar',
852
				'title'  => $people_title,
853
				'href'   => false,
854
				'meta'   => array(
855
					'class' => 'inline-action',
856
				),
857
			) );
858
859
			$plugins_title = $this->create_menu_item_pair(
860
				array(
861
					'url'   => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
862
					'id'    => 'wp-admin-bar-plugins',
863
					'label' => __( 'Plugins', 'jetpack' ),
864
				),
865
				array(
866
					'url'   => 'https://wordpress.com/plugins/browse/' . esc_attr( $this->primary_site_slug ),
867
					'id'    => 'wp-admin-bar-plugins-add',
868
					'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new plugin', 'jetpack' ),
869
				)
870
			);
871
872
			$wp_admin_bar->add_menu( array(
873
				'parent' => 'configuration',
874
				'id'     => 'plugins',
875
				'title'  => $plugins_title,
876
				'href'   => false,
877
				'meta'   => array(
878
					'class' => 'inline-action',
879
				),
880
			) );
881
882
			if ( $this->is_automated_transfer_site() ) {
883
				$domain_title = $this->create_menu_item_pair(
884
					array(
885
						'url'   => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
886
						'id'    => 'wp-admin-bar-domains',
887
						'label' => __( 'Domains', 'jetpack' ),
888
					),
889
					array(
890
						'url'   => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
891
						'id'    => 'wp-admin-bar-domains-add',
892
						'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
893
					)
894
				);
895
				$wp_admin_bar->add_menu( array(
896
					'parent' => 'configuration',
897
					'id'     => 'domains',
898
					'title'  => $domain_title,
899
					'href'   => false,
900
					'meta'   => array(
901
						'class' => 'inline-action',
902
					),
903
				) );
904
			}
905
906
907
			$wp_admin_bar->add_menu( array(
908
				'parent' => 'configuration',
909
				'id'     => 'blog-settings',
910
				'title'  => __( 'Settings', 'jetpack' ),
911
				'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
912
				'meta'   => array(
913
					'class' => 'mb-icon',
914
				),
915
			) );
916
917 View Code Duplication
			if ( $this->is_automated_transfer_site() ) {
918
				$wp_admin_bar->add_menu( array(
919
					'parent' => 'configuration',
920
					'id'     => 'legacy-dashboard',
921
					'title'  => __( 'WP Admin', 'jetpack' ),
922
					'href'   => '//' . esc_attr( $this->primary_site_url ) . '/wp-admin/',
923
					'meta'   => array(
924
						'class' => 'mb-icon',
925
					),
926
				) );
927
			}
928
		}
929
	}
930
}
931