Completed
Push — add/masterbar-module ( 01ef07...f7c143 )
by
unknown
10:05
created

A8C_WPCOM_Masterbar::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
841
				'href'   => false,
842
				'meta'   => array(
843
					'class' => 'inline-action',
844
				),
845
			) );
846
847
			$wp_admin_bar->add_menu( array(
848
				'parent' => 'configuration',
849
				'id'     => 'blog-settings',
850
				'title'  => __( 'Settings', 'jetpack' ),
851
				'href'   => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
852
				'meta'   => array(
853
					'class' => 'mb-icon',
854
				),
855
			) );
856
857
			if ( $this->isAutomatedTransferSite() ) {
858
				$wp_admin_bar->add_menu( array(
859
					'parent' => 'configuration',
860
					'id'     => 'legacy-dashboard',
861
					'title'  => __( 'WP Admin', 'jetpack' ),
862
					'href'   => '//' . esc_attr( $this->primary_site_slug ) . '/wp-admin/',
863
					'meta'   => array(
864
						'class' => 'mb-icon',
865
					),
866
				) );
867
			}
868
		}
869
	}
870
}
871