Completed
Push — branch-5.1 ( 342237...fa821c )
by
unknown
14:20 queued 04:41
created

A8C_WPCOM_Masterbar::admin_body_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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