Completed
Push — add/masterbar-module ( a1b9ab...01ef07 )
by
unknown
38:58 queued 31:08
created

A8C_WPCOM_Masterbar::add_my_sites_submenu()   F

Complexity

Conditions 16
Paths 5760

Size

Total Lines 391
Code Lines 255

Duplication

Lines 57
Ratio 14.58 %

Importance

Changes 0
Metric Value
cc 16
eloc 255
nc 5760
nop 1
dl 57
loc 391
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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