Completed
Push — master-stable ( ea10e6...53f101 )
by
unknown
26:27 queued 18:04
created

A8C_WPCOM_Masterbar::add_me_submenu()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 154
Code Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 105
nc 5
nop 1
dl 0
loc 154
rs 8.1935
c 0
b 0
f 0

How to fix   Long Method   

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