Completed
Push — add/masterbar-site-preview ( 101353 )
by
unknown
13:02
created

A8C_WPCOM_Masterbar::create_menu_item_pair()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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
		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
		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
		// Site Preview
567 View Code Duplication
		if ( is_admin() ) {
568
			$wp_admin_bar->add_menu( array(
569
				'parent' => 'blog',
570
				'id'     => 'site-preview',
571
				'title'  => __( 'Site Preview' ),
572
				'href'   => home_url(),
573
				'meta'   => array(
574
					'class' => 'mb-icon',
575
					'target' => '_blank',
576
				),
577
			) );
578
		}
579
580
		// Stats
581 View Code Duplication
		if ( Jetpack::is_module_active( 'stats' ) ) {
582
			$wp_admin_bar->add_menu( array(
583
				'parent' => 'blog',
584
				'id'     => 'blog-stats',
585
				'title'  => __( 'Stats', 'jetpack' ),
586
				'href'   => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
587
				'meta'   => array(
588
					'class' => 'mb-icon',
589
				),
590
			) );
591
		}
592
593
		// Add Calypso plans link and plan type indicator
594
		if ( is_user_member_of_blog( $current_user->ID ) ) {
595
			$plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
596
			$label = __( 'Plan', 'jetpack' );
597
			$plan = Jetpack::get_active_plan();
598
599
			$plan_title = $this->create_menu_item_pair(
600
				array(
601
					'url'   => $plans_url,
602
					'id'    => 'wp-admin-bar-plan',
603
					'label' => $label,
604
				),
605
				array(
606
					'url'   => $plans_url,
607
					'id'    => 'wp-admin-bar-plan-badge',
608
					'label' => $plan['product_name_short']
609
				)
610
			);
611
612
			$wp_admin_bar->add_menu( array(
613
				'parent' => 'blog',
614
				'id'     => 'plan',
615
				'title'  => $plan_title,
616
				'meta'   => array(
617
					'class' => 'inline-action',
618
				),
619
			) );
620
		}
621
622
		// Publish group
623
		$wp_admin_bar->add_group( array(
624
			'parent' => 'blog',
625
			'id'     => 'publish',
626
		) );
627
628
		// Publish header
629
		$wp_admin_bar->add_menu( array(
630
			'parent' => 'publish',
631
			'id'     => 'publish-header',
632
			'title'  => _x( 'Publish', 'admin bar menu group label', 'jetpack' ),
633
			'meta'   => array(
634
				'class' => 'ab-submenu-header',
635
			),
636
		) );
637
638
		// Blog Posts
639
		$posts_title = $this->create_menu_item_pair(
640
			array(
641
				'url'   => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
642
				'id'    => 'wp-admin-bar-edit-post',
643
				'label' => __( 'Blog Posts', 'jetpack' ),
644
			),
645
			array(
646
				'url'   => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
647
				'id'    => 'wp-admin-bar-new-post',
648
				'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ),
649
			)
650
		);
651
652
		if ( ! current_user_can( 'edit_posts' ) ) {
653
			$posts_title = $this->create_menu_item_anchor(
654
				'ab-item ab-primary mb-icon',
655
				'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
656
				__( 'Blog Posts', 'jetpack' ),
657
				'wp-admin-bar-edit-post'
658
			);
659
		}
660
661
		$wp_admin_bar->add_menu( array(
662
			'parent' => 'publish',
663
			'id'     => 'new-post',
664
			'title'  => $posts_title,
665
			'meta'   => array(
666
				'class' => 'inline-action',
667
			),
668
		) );
669
670
		// Pages
671
		$pages_title = $this->create_menu_item_pair(
672
			array(
673
				'url'   => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
674
				'id'    => 'wp-admin-bar-edit-page',
675
				'label' => __( 'Pages', 'jetpack' ),
676
			),
677
			array(
678
				'url'   => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
679
				'id'    => 'wp-admin-bar-new-page',
680
				'label' => _x( 'Add', 'admin bar menu new item label', 'jetpack' ),
681
			)
682
		);
683
684
		if ( ! current_user_can( 'edit_pages' ) ) {
685
			$pages_title = $this->create_menu_item_anchor(
686
				'ab-item ab-primary mb-icon',
687
				'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
688
				__( 'Pages', 'jetpack' ),
689
				'wp-admin-bar-edit-page'
690
			);
691
		}
692
693
		$wp_admin_bar->add_menu( array(
694
			'parent' => 'publish',
695
			'id'     => 'new-page',
696
			'title'  => $pages_title,
697
			'meta'   => array(
698
				'class' => 'inline-action',
699
			),
700
		) );
701
702
		// Testimonials
703 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
704
			$testimonials_title = $this->create_menu_item_pair(
705
				array(
706
					'url'   => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
707
					'id'    => 'wp-admin-bar-edit-testimonial',
708
					'label' => __( 'Testimonials', 'jetpack' ),
709
				),
710
				array(
711
					'url'   => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
712
					'id'    => 'wp-admin-bar-new-testimonial',
713
					'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
714
				)
715
			);
716
717
			if ( ! current_user_can( 'edit_pages' ) ) {
718
				$testimonials_title = $this->create_menu_item_anchor(
719
					'ab-item ab-primary mb-icon',
720
					'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
721
					__( 'Testimonials', 'jetpack' ),
722
					'wp-admin-bar-edit-testimonial'
723
				);
724
			}
725
726
			$wp_admin_bar->add_menu( array(
727
				'parent' => 'publish',
728
				'id'     => 'new-jetpack-testimonial',
729
				'title'  => $testimonials_title,
730
				'meta'   => array(
731
					'class' => 'inline-action',
732
				),
733
			) );
734
		}
735
736
		// Portfolio
737 View Code Duplication
		if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
738
			$portfolios_title = $this->create_menu_item_pair(
739
				array(
740
					'url'   => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
741
					'id'    => 'wp-admin-bar-edit-portfolio',
742
					'label' => __( 'Portfolio', 'jetpack' ),
743
				),
744
				array(
745
					'url'   => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
746
					'id'    => 'wp-admin-bar-new-portfolio',
747
					'label' => _x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
748
				)
749
			);
750
751
			if ( ! current_user_can( 'edit_pages' ) ) {
752
				$portfolios_title = $this->create_menu_item_anchor(
753
					'ab-item ab-primary mb-icon',
754
					'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
755
					__( 'Portfolio', 'jetpack' ),
756
					'wp-admin-bar-edit-portfolio'
757
				);
758
			}
759
760
			$wp_admin_bar->add_menu( array(
761
				'parent' => 'publish',
762
				'id'     => 'new-jetpack-portfolio',
763
				'title'  => $portfolios_title,
764
				'meta'   => array(
765
					'class' => 'inline-action',
766
				),
767
			) );
768
		}
769
770
		if ( current_user_can( 'edit_theme_options' ) ) {
771
			// Look and Feel group
772
			$wp_admin_bar->add_group( array(
773
				'parent' => 'blog',
774
				'id'     => 'look-and-feel',
775
			) );
776
777
			// Look and Feel header
778
			$wp_admin_bar->add_menu( array(
779
				'parent' => 'look-and-feel',
780
				'id'     => 'look-and-feel-header',
781
				'title'  => _x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
782
				'meta'   => array(
783
					'class' => 'ab-submenu-header',
784
				),
785
			) );
786
787
			if ( is_admin() ) {
788
				// In wp-admin the `return` query arg will return to that page after closing the Customizer
789
				$customizer_url = add_query_arg( array( 'return' => urlencode( site_url( $_SERVER['REQUEST_URI'] ) ) ), wp_customize_url() );
790
			} else {
791
				// On the frontend the `url` query arg will load that page in the Customizer and also return to it after closing
792
				// non-home URLs won't work unless we undo domain mapping since the Customizer preview is unmapped to always have HTTPS
793
				$current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
794
				$customizer_url = add_query_arg( array( 'url' => urlencode( $current_page ) ), wp_customize_url() );
795
			}
796
797
			$theme_title = $this->create_menu_item_pair(
798
				array(
799
					'url'   => 'https://wordpress.com/design/' . esc_attr( $this->primary_site_slug ),
800
					'id'    => 'wp-admin-bar-themes',
801
					'label' => __( 'Themes', 'jetpack' ),
802
				),
803
				array(
804
					'url'   => $customizer_url,
805
					'id'    => 'wp-admin-bar-cmz',
806
					'label' => _x( 'Customize', 'admin bar customize item label', 'jetpack' ),
807
				)
808
			);
809
			$meta = array( 'class' => 'mb-icon', 'class' => 'inline-action' );
810
			$href = false;
811
812
			$wp_admin_bar->add_menu( array(
813
				'parent' => 'look-and-feel',
814
				'id'     => 'themes',
815
				'title'  => $theme_title,
816
				'href'   => $href,
817
				'meta'   => $meta
818
			) );
819
		}
820
821
		if ( current_user_can( 'manage_options' ) ) {
822
			// Configuration group
823
			$wp_admin_bar->add_group( array(
824
				'parent' => 'blog',
825
				'id'     => 'configuration',
826
			) );
827
828
			// Configuration header
829
			$wp_admin_bar->add_menu( array(
830
				'parent' => 'configuration',
831
				'id'     => 'configuration-header',
832
				'title'  => __( 'Configure', 'admin bar menu group label', 'jetpack' ),
833
				'meta'   => array(
834
					'class' => 'ab-submenu-header',
835
				),
836
			) );
837
838 View Code Duplication
			if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
839
				$wp_admin_bar->add_menu( array(
840
					'parent' => 'configuration',
841
					'id'     => 'sharing',
842
					'title'  => __( 'Sharing', 'jetpack' ),
843
					'href'   => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
844
					'meta'   => array(
845
						'class' => 'mb-icon',
846
					),
847
				) );
848
			}
849
850
			$people_title = $this->create_menu_item_pair(
851
				array(
852
					'url'   => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
853
					'id'    => 'wp-admin-bar-people',
854
					'label' => __( 'People', 'jetpack' ),
855
				),
856
				array(
857
					'url'   => admin_url( 'user-new.php' ),
858
					'id'    => 'wp-admin-bar-people-add',
859
					'label' => _x( 'Add', 'admin bar people item label', 'jetpack' ),
860
				)
861
			);
862
863
			$wp_admin_bar->add_menu( array(
864
				'parent' => 'configuration',
865
				'id'     => 'users-toolbar',
866
				'title'  => $people_title,
867
				'href'   => false,
868
				'meta'   => array(
869
					'class' => 'inline-action',
870
				),
871
			) );
872
873
			$plugins_title = $this->create_menu_item_pair(
874
				array(
875
					'url'   => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
876
					'id'    => 'wp-admin-bar-plugins',
877
					'label' => __( 'Plugins', 'jetpack' ),
878
				),
879
				array(
880
					'url'   => 'https://wordpress.com/plugins/browse/' . esc_attr( $this->primary_site_slug ),
881
					'id'    => 'wp-admin-bar-plugins-add',
882
					'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new plugin', 'jetpack' ),
883
				)
884
			);
885
886
			$wp_admin_bar->add_menu( array(
887
				'parent' => 'configuration',
888
				'id'     => 'plugins',
889
				'title'  => $plugins_title,
890
				'href'   => false,
891
				'meta'   => array(
892
					'class' => 'inline-action',
893
				),
894
			) );
895
896
			if ( $this->is_automated_transfer_site() ) {
897
				$domain_title = $this->create_menu_item_pair(
898
					array(
899
						'url'   => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
900
						'id'    => 'wp-admin-bar-domains',
901
						'label' => __( 'Domains', 'jetpack' ),
902
					),
903
					array(
904
						'url'   => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
905
						'id'    => 'wp-admin-bar-domains-add',
906
						'label' => _x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
907
					)
908
				);
909
				$wp_admin_bar->add_menu( array(
910
					'parent' => 'configuration',
911
					'id'     => 'domains',
912
					'title'  => $domain_title,
913
					'href'   => false,
914
					'meta'   => array(
915
						'class' => 'inline-action',
916
					),
917
				) );
918
			}
919
920
			$wp_admin_bar->add_menu( array(
921
				'parent' => 'configuration',
922
				'id'     => 'blog-settings',
923
				'title'  => __( 'Settings', 'jetpack' ),
924
				'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
925
				'meta'   => array(
926
					'class' => 'mb-icon',
927
				),
928
			) );
929
930 View Code Duplication
			if ( ! is_admin() ) {
931
				$wp_admin_bar->add_menu( array(
932
					'parent' => 'configuration',
933
					'id'     => 'legacy-dashboard',
934
					'title'  => __( 'Dashboard', 'jetpack' ),
935
					'href'   => admin_url(),
936
					'meta'   => array(
937
						'class' => 'mb-icon',
938
					),
939
				) );
940
			}
941
942
			// Restore dashboard menu toggle that is needed on mobile views.
943
			if ( is_admin() ) {
944
				$wp_admin_bar->add_menu( array(
945
				'id'    => 'menu-toggle',
946
				'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . __( 'Menu', 'jetpack' ) . '</span>',
947
				'href'  => '#',
948
				) );
949
			}
950
		}
951
	}
952
}
953