Completed
Push — update/_inc_minify_v2 ( d7c168 )
by
unknown
08:21
created

A8C_WPCOM_Masterbar::get_locale()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 18
Code Lines 10

Duplication

Lines 6
Ratio 33.33 %

Importance

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