Completed
Push — add/apps-card ( 6b0d49...a7e330 )
by
unknown
58:41 queued 50:34
created

maybe_logout_user_from_wpcom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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