Completed
Push — add/visibility-minor-filter ( d0cdd4...9b8b80 )
by
unknown
228:20 queued 215:17
created

A8C_WPCOM_Masterbar::add_my_sites_submenu()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 439
Code Lines 287

Duplication

Lines 108
Ratio 24.6 %

Importance

Changes 0
Metric Value
cc 24
eloc 287
nc 155520
nop 1
dl 108
loc 439
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
4
5
class A8C_WPCOM_Masterbar {
6
	/**
7
	 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
8
	 * If not set it will default to loading the ones from WordPress.com.
9
	 *
10
	 * @var string $sandbox_url
11
	 */
12
	private $sandbox_url = '';
13
14
	private $locale;
15
16
	private $user_id;
17
	private $user_data;
18
	private $user_login;
19
	private $user_email;
20
	private $display_name;
21
	private $primary_site_slug;
22
	private $user_text_direction;
23
	private $user_site_count;
24
25
	function __construct() {
26
		$this->locale  = $this->get_locale();
27
		$this->user_id = get_current_user_id();
28
29
		// Limit the masterbar to be shown only to connected Jetpack users.
30
		if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
31
			return;
32
		}
33
34
		$this->user_data = Jetpack::get_connected_user_data( $this->user_id );
35
		$this->user_login = $this->user_data['login'];
36
		$this->user_email = $this->user_data['email'];
37
		$this->display_name = $this->user_data['display_name'];
38
		$this->user_site_count = $this->user_data['site_count'];
39
40
		// Used to build menu links that point directly to Calypso.
41
		$this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
42
43
		// Used for display purposes and for building WP Admin links.
44
		$this->primary_site_url = str_replace( '::', '/', $this->primary_site_slug );
0 ignored issues
show
Bug introduced by
The property primary_site_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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