PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9
Jetpack – WP Security, Backup, Speed, & Growth v7.9
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / masterbar / masterbar.php

masterbar.php in Jetpack – WP Security, Backup, Speed, & Growth 7.9, at modules/masterbar/masterbar.php

1,330 lines 35.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4
5 require_once dirname( __FILE__ ) . '/rtl-admin-bar.php';
6
7 /**
8 * Custom Admin bar displayed instead of the default WordPress admin bar.
9 */
10 class A8C_WPCOM_Masterbar {
11 /**
12 * Use for testing changes made to remotely enqueued scripts and styles on your sandbox.
13 * If not set it will default to loading the ones from WordPress.com.
14 *
15 * @var string $sandbox_url
16 */
17 private $sandbox_url = '';
18
19 /**
20 * Current locale.
21 *
22 * @var string
23 */
24 private $locale;
25
26 /**
27 * Current User ID.
28 *
29 * @var int
30 */
31 private $user_id;
32 /**
33 * WordPress.com user data of the connected user.
34 *
35 * @var array
36 */
37 private $user_data;
38 /**
39 * WordPress.com username for the connected user.
40 *
41 * @var string
42 */
43 private $user_login;
44 /**
45 * WordPress.com email address for the connected user.
46 *
47 * @var string
48 */
49 private $user_email;
50 /**
51 * WordPress.com display name for the connected user.
52 *
53 * @var string
54 */
55 private $display_name;
56 /**
57 * Site URL sanitized for usage in WordPress.com slugs.
58 *
59 * @var string
60 */
61 private $primary_site_slug;
62 /**
63 * Text direction (ltr or rtl) based on connected WordPress.com user's interface settings.
64 *
65 * @var string
66 */
67 private $user_text_direction;
68 /**
69 * Number of sites owned by connected WordPress.com user.
70 *
71 * @var int
72 */
73 private $user_site_count;
74
75 /**
76 * Constructor
77 */
78 public function __construct() {
79 add_action( 'admin_bar_init', array( $this, 'init' ) );
80
81 // Post logout on the site, also log the user out of WordPress.com.
82 add_action( 'wp_logout', array( $this, 'maybe_logout_user_from_wpcom' ) );
83 }
84
85 /**
86 * Initialize our masterbar.
87 */
88 public function init() {
89 $this->locale = $this->get_locale();
90 $this->user_id = get_current_user_id();
91
92 // Limit the masterbar to be shown only to connected Jetpack users.
93 if ( ! Jetpack::is_user_connected( $this->user_id ) ) {
94 return;
95 }
96
97 // Don't show the masterbar on WordPress mobile apps.
98 if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
99 add_filter( 'show_admin_bar', '__return_false' );
100 return;
101 }
102
103 // Disable the Masterbar on AMP views.
104 if (
105 class_exists( 'Jetpack_AMP_Support' )
106 && Jetpack_AMP_Support::is_amp_request()
107 ) {
108 return;
109 }
110
111 Jetpack::dns_prefetch(
112 array(
113 '//s0.wp.com',
114 '//s1.wp.com',
115 '//s2.wp.com',
116 '//0.gravatar.com',
117 '//1.gravatar.com',
118 '//2.gravatar.com',
119 )
120 );
121
122 // Atomic only.
123 if ( jetpack_is_atomic_site() ) {
124 /*
125 * override user setting that hides masterbar from site's front.
126 * https://github.com/Automattic/jetpack/issues/7667
127 */
128 add_filter( 'show_admin_bar', '__return_true' );
129 }
130
131 $this->user_data = Jetpack::get_connected_user_data( $this->user_id );
132 $this->user_login = $this->user_data['login'];
133 $this->user_email = $this->user_data['email'];
134 $this->display_name = $this->user_data['display_name'];
135 $this->user_site_count = $this->user_data['site_count'];
136
137 // Used to build menu links that point directly to Calypso.
138 $this->primary_site_slug = Jetpack::build_raw_urls( get_home_url() );
139
140 // Used for display purposes and for building WP Admin links.
141 $this->primary_site_url = str_replace( '::', '/', $this->primary_site_slug );
142
143 // We need to use user's setting here, instead of relying on current blog's text direction.
144 $this->user_text_direction = $this->user_data['text_direction'];
145
146 if ( $this->is_rtl() ) {
147 // Extend core WP_Admin_Bar class in order to add rtl styles.
148 add_filter( 'wp_admin_bar_class', array( $this, 'get_rtl_admin_bar_class' ) );
149 }
150 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
151
152 add_action( 'wp_before_admin_bar_render', array( $this, 'replace_core_masterbar' ), 99999 );
153
154 add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
155 add_action( 'admin_enqueue_scripts', array( $this, 'add_styles_and_scripts' ) );
156
157 add_action( 'wp_enqueue_scripts', array( $this, 'remove_core_styles' ) );
158 add_action( 'admin_enqueue_scripts', array( $this, 'remove_core_styles' ) );
159
160 if ( Jetpack::is_module_active( 'notes' ) && $this->is_rtl() ) {
161 // Override Notification module to include RTL styles.
162 add_action( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', '__return_true' );
163 }
164 }
165
166 /**
167 * Log out from WordPress.com when logging out of the local site.
168 */
169 public function maybe_logout_user_from_wpcom() {
170 /**
171 * Whether we should sign out from wpcom too when signing out from the masterbar.
172 *
173 * @since 5.9.0
174 *
175 * @param bool $masterbar_should_logout_from_wpcom True by default.
176 */
177 $masterbar_should_logout_from_wpcom = apply_filters( 'jetpack_masterbar_should_logout_from_wpcom', true );
178 if (
179 // No need to check for a nonce here, it happens further up.
180 isset( $_GET['context'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
181 && 'masterbar' === $_GET['context'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended
182 && $masterbar_should_logout_from_wpcom
183 ) {
184 do_action( 'wp_masterbar_logout' );
185 }
186 }
187
188 /**
189 * Get class name for RTL sites.
190 */
191 public function get_rtl_admin_bar_class() {
192 return 'RTL_Admin_Bar';
193 }
194
195 /**
196 * Adds CSS classes to admin body tag.
197 *
198 * @since 5.1
199 *
200 * @param string $admin_body_classes CSS classes that will be added.
201 *
202 * @return string
203 */
204 public function admin_body_class( $admin_body_classes ) {
205 return "$admin_body_classes jetpack-masterbar";
206 }
207
208 /**
209 * Remove the default Admin Bar CSS.
210 */
211 public function remove_core_styles() {
212 /*
213 * Notifications need the admin bar styles,
214 * so let's not remove them when the module is active.
215 */
216 if ( ! Jetpack::is_module_active( 'notes' ) ) {
217 wp_dequeue_style( 'admin-bar' );
218 }
219 }
220
221 /**
222 * Check if the user settings are for an RTL language or not.
223 */
224 public function is_rtl() {
225 return 'rtl' === $this->user_text_direction ? true : false;
226 }
227
228 /**
229 * Enqueue our own CSS and JS to display our custom admin bar.
230 */
231 public function add_styles_and_scripts() {
232
233 if ( $this->is_rtl() ) {
234 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 );
235 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 );
236 } else {
237 wp_enqueue_style( 'a8c-wpcom-masterbar', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/wpcom-admin-bar.css' ), array(), JETPACK__VERSION );
238 wp_enqueue_style( 'a8c-wpcom-masterbar-overrides', $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.css' ), array(), JETPACK__VERSION );
239 }
240
241 // Local overrides.
242 wp_enqueue_style( 'a8c_wpcom_css_override', plugins_url( 'overrides.css', __FILE__ ), array(), JETPACK__VERSION );
243
244 if ( ! Jetpack::is_module_active( 'notes ' ) ) {
245 // Masterbar is relying on some icons from noticons.css.
246 wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK__VERSION . '-' . gmdate( 'oW' ) );
247 }
248
249 wp_enqueue_script(
250 'jetpack-accessible-focus',
251 Assets::get_file_url_for_environment( '_inc/build/accessible-focus.min.js', '_inc/accessible-focus.js' ),
252 array(),
253 JETPACK__VERSION,
254 false
255 );
256 wp_enqueue_script(
257 'a8c_wpcom_masterbar_tracks_events',
258 Assets::get_file_url_for_environment(
259 '_inc/build/masterbar/tracks-events.min.js',
260 'modules/masterbar/tracks-events.js'
261 ),
262 array( 'jquery' ),
263 JETPACK__VERSION,
264 false
265 );
266
267 wp_enqueue_script(
268 'a8c_wpcom_masterbar_overrides',
269 $this->wpcom_static_url( '/wp-content/mu-plugins/admin-bar/masterbar-overrides/masterbar.js' ),
270 array( 'jquery' ),
271 JETPACK__VERSION,
272 false
273 );
274 }
275
276 /**
277 * Get base URL where our CSS and JS will come from.
278 *
279 * @param string $file File path for a static resource.
280 */
281 private function wpcom_static_url( $file ) {
282 if ( ! empty( $this->sandbox_url ) ) {
283 // For testing undeployed changes to remotely enqueued scripts and styles.
284 return set_url_scheme( $this->sandbox_url . $file, 'https' );
285 }
286
287 $i = hexdec( substr( md5( $file ), - 1 ) ) % 2;
288 $url = 'https://s' . $i . '.wp.com' . $file;
289
290 return set_url_scheme( $url, 'https' );
291 }
292
293 /**
294 * Remove the default admin bar items and replace it with our own admin bar.
295 */
296 public function replace_core_masterbar() {
297 global $wp_admin_bar;
298
299 if ( ! is_object( $wp_admin_bar ) ) {
300 return false;
301 }
302
303 $this->clear_core_masterbar( $wp_admin_bar );
304 $this->build_wpcom_masterbar( $wp_admin_bar );
305 }
306
307 /**
308 * Remove all existing toolbar entries from core Masterbar
309 *
310 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
311 */
312 public function clear_core_masterbar( $wp_admin_bar ) {
313 foreach ( $wp_admin_bar->get_nodes() as $node ) {
314 $wp_admin_bar->remove_node( $node->id );
315 }
316 }
317
318 /**
319 * Add entries corresponding to WordPress.com Masterbar
320 *
321 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
322 */
323 public function build_wpcom_masterbar( $wp_admin_bar ) {
324 // Menu groups.
325 $this->wpcom_adminbar_add_secondary_groups( $wp_admin_bar );
326
327 // Left part.
328 $this->add_my_sites_submenu( $wp_admin_bar );
329 $this->add_reader_submenu( $wp_admin_bar );
330
331 // Right part.
332 if ( Jetpack::is_module_active( 'notes' ) ) {
333 $this->add_notifications( $wp_admin_bar );
334 }
335
336 $this->add_me_submenu( $wp_admin_bar );
337 $this->add_write_button( $wp_admin_bar );
338
339 // Recovery mode exit.
340 if ( function_exists( 'wp_admin_bar_recovery_mode_menu' ) ) {
341 wp_admin_bar_recovery_mode_menu( $wp_admin_bar );
342 }
343 }
344
345 /**
346 * Get WordPress.com current locale name.
347 */
348 public function get_locale() {
349 $wpcom_locale = get_locale();
350
351 if ( ! class_exists( 'GP_Locales' ) ) {
352 if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
353 require JETPACK__GLOTPRESS_LOCALES_PATH;
354 }
355 }
356
357 if ( class_exists( 'GP_Locales' ) ) {
358 $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() );
359 if ( $wpcom_locale_object instanceof GP_Locale ) {
360 $wpcom_locale = $wpcom_locale_object->slug;
361 }
362 }
363
364 return $wpcom_locale;
365 }
366
367 /**
368 * Add the Notifications menu item.
369 *
370 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
371 */
372 public function add_notifications( $wp_admin_bar ) {
373 $wp_admin_bar->add_node(
374 array(
375 'id' => 'notes',
376 'title' => '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span>
377 <span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>
378 <span class="noticon noticon-bell"></span>',
379 'meta' => array(
380 'html' => '<div id="wpnt-notes-panel2" style="display:none" lang="' . esc_attr( $this->locale ) . '" dir="' . ( $this->is_rtl() ? 'rtl' : 'ltr' ) . '">' .
381 '<div class="wpnt-notes-panel-header">' .
382 '<span class="wpnt-notes-header">' .
383 esc_html__( 'Notifications', 'jetpack' ) .
384 '</span>' .
385 '<span class="wpnt-notes-panel-link">' .
386 '</span>' .
387 '</div>' .
388 '</div>',
389 'class' => 'menupop mb-trackable',
390 ),
391 'parent' => 'top-secondary',
392 )
393 );
394 }
395
396 /**
397 * Add the "Reader" menu item in the root default group.
398 *
399 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
400 */
401 public function add_reader_submenu( $wp_admin_bar ) {
402 $wp_admin_bar->add_menu(
403 array(
404 'parent' => 'root-default',
405 'id' => 'newdash',
406 'title' => esc_html__( 'Reader', 'jetpack' ),
407 'href' => '#',
408 'meta' => array(
409 'class' => 'mb-trackable',
410 ),
411 )
412 );
413
414 $wp_admin_bar->add_menu(
415 array(
416 'parent' => 'newdash',
417 'id' => 'streams-header',
418 'title' => esc_html_x(
419 'Streams',
420 'Title for Reader sub-menu that contains followed sites, likes, and recommendations',
421 'jetpack'
422 ),
423 'meta' => array(
424 'class' => 'ab-submenu-header',
425 ),
426 )
427 );
428
429 $following_title = $this->create_menu_item_pair(
430 array(
431 'url' => 'https://wordpress.com/',
432 'id' => 'wp-admin-bar-followed-sites',
433 'label' => esc_html__( 'Followed Sites', 'jetpack' ),
434 ),
435 array(
436 'url' => 'https://wordpress.com/following/edit',
437 'id' => 'wp-admin-bar-reader-followed-sites-manage',
438 'label' => esc_html__( 'Manage', 'jetpack' ),
439 )
440 );
441
442 $wp_admin_bar->add_menu(
443 array(
444 'parent' => 'newdash',
445 'id' => 'following',
446 'title' => $following_title,
447 'meta' => array( 'class' => 'inline-action' ),
448 )
449 );
450
451 $wp_admin_bar->add_menu(
452 array(
453 'parent' => 'newdash',
454 'id' => 'discover-discover',
455 'title' => esc_html__( 'Discover', 'jetpack' ),
456 'href' => 'https://wordpress.com/discover',
457 'meta' => array(
458 'class' => 'mb-icon-spacer',
459 ),
460 )
461 );
462
463 $wp_admin_bar->add_menu(
464 array(
465 'parent' => 'newdash',
466 'id' => 'discover-search',
467 'title' => esc_html__( 'Search', 'jetpack' ),
468 'href' => 'https://wordpress.com/read/search',
469 'meta' => array(
470 'class' => 'mb-icon-spacer',
471 ),
472 )
473 );
474
475 $wp_admin_bar->add_menu(
476 array(
477 'parent' => 'newdash',
478 'id' => 'discover-recommended-blogs',
479 'title' => esc_html__( 'Recommendations', 'jetpack' ),
480 'href' => 'https://wordpress.com/recommendations',
481 'meta' => array(
482 'class' => 'mb-icon-spacer',
483 ),
484 )
485 );
486
487 $wp_admin_bar->add_menu(
488 array(
489 'parent' => 'newdash',
490 'id' => 'my-activity-my-likes',
491 'title' => esc_html__( 'My Likes', 'jetpack' ),
492 'href' => 'https://wordpress.com/activities/likes',
493 'meta' => array(
494 'class' => 'mb-icon-spacer',
495 ),
496 )
497 );
498
499 }
500
501 /**
502 * Merge 2 menu items together into 2 link tags.
503 *
504 * @param array $primary Array of menu information.
505 * @param array $secondary Array of menu information.
506 */
507 public function create_menu_item_pair( $primary, $secondary ) {
508 $primary_class = 'ab-item ab-primary mb-icon';
509 $secondary_class = 'ab-secondary';
510
511 $primary_anchor = $this->create_menu_item_anchor( $primary_class, $primary['url'], $primary['label'], $primary['id'] );
512 $secondary_anchor = $this->create_menu_item_anchor( $secondary_class, $secondary['url'], $secondary['label'], $secondary['id'] );
513
514 return $primary_anchor . $secondary_anchor;
515 }
516
517 /**
518 * Create a link tag based on information about a menu item.
519 *
520 * @param string $class Menu item CSS class.
521 * @param string $url URL you go to when clicking on the menu item.
522 * @param string $label Menu item title.
523 * @param string $id Menu item slug.
524 */
525 public function create_menu_item_anchor( $class, $url, $label, $id ) {
526 return '<a href="' . $url . '" class="' . $class . '" id="' . $id . '">' . $label . '</a>';
527 }
528
529 /**
530 * Add Secondary groups for submenu items.
531 *
532 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
533 */
534 public function wpcom_adminbar_add_secondary_groups( $wp_admin_bar ) {
535 $wp_admin_bar->add_group(
536 array(
537 'id' => 'root-default',
538 'meta' => array(
539 'class' => 'ab-top-menu',
540 ),
541 )
542 );
543
544 $wp_admin_bar->add_group(
545 array(
546 'parent' => 'blog',
547 'id' => 'blog-secondary',
548 'meta' => array(
549 'class' => 'ab-sub-secondary',
550 ),
551 )
552 );
553
554 $wp_admin_bar->add_group(
555 array(
556 'id' => 'top-secondary',
557 'meta' => array(
558 'class' => 'ab-top-secondary',
559 ),
560 )
561 );
562 }
563
564 /**
565 * Add User info menu item.
566 *
567 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
568 */
569 public function add_me_submenu( $wp_admin_bar ) {
570 $user_id = get_current_user_id();
571 if ( empty( $user_id ) ) {
572 return;
573 }
574
575 $avatar = get_avatar( $this->user_email, 32, 'mm', '', array( 'force_display' => true ) );
576 $class = empty( $avatar ) ? 'mb-trackable' : 'with-avatar mb-trackable';
577
578 // Add the 'Me' menu.
579 $wp_admin_bar->add_menu(
580 array(
581 'id' => 'my-account',
582 'parent' => 'top-secondary',
583 'title' => $avatar . '<span class="ab-text">' . esc_html__( 'Me', 'jetpack' ) . '</span>',
584 'href' => '#',
585 'meta' => array(
586 'class' => $class,
587 ),
588 )
589 );
590
591 $id = 'user-actions';
592 $wp_admin_bar->add_group(
593 array(
594 'parent' => 'my-account',
595 'id' => $id,
596 )
597 );
598
599 $settings_url = 'https://wordpress.com/me/account';
600
601 $logout_url = wp_logout_url();
602 $logout_url = add_query_arg( 'context', 'masterbar', $logout_url );
603
604 $user_info = get_avatar( $this->user_email, 128, 'mm', '', array( 'force_display' => true ) );
605 $user_info .= '<span class="display-name">' . $this->display_name . '</span>';
606 $user_info .= '<a class="username" href="https://gravatar.com/' . $this->user_login . '">@' . $this->user_login . '</a>';
607
608 $user_info .= sprintf(
609 '<div><a href="%s" class="ab-sign-out">%s</a></div>',
610 $logout_url,
611 esc_html__( 'Sign Out', 'jetpack' )
612 );
613
614 $wp_admin_bar->add_menu(
615 array(
616 'parent' => $id,
617 'id' => 'user-info',
618 'title' => $user_info,
619 'meta' => array(
620 'class' => 'user-info user-info-item',
621 'tabindex' => -1,
622 ),
623 )
624 );
625
626 $wp_admin_bar->add_menu(
627 array(
628 'parent' => $id,
629 'id' => 'profile-header',
630 'title' => esc_html__( 'Profile', 'jetpack' ),
631 'meta' => array(
632 'class' => 'ab-submenu-header',
633 ),
634 )
635 );
636
637 $wp_admin_bar->add_menu(
638 array(
639 'parent' => $id,
640 'id' => 'my-profile',
641 'title' => esc_html__( 'My Profile', 'jetpack' ),
642 'href' => 'https://wordpress.com/me',
643 'meta' => array(
644 'class' => 'mb-icon',
645 ),
646 )
647 );
648
649 $wp_admin_bar->add_menu(
650 array(
651 'parent' => $id,
652 'id' => 'account-settings',
653 'title' => esc_html__( 'Account Settings', 'jetpack' ),
654 'href' => $settings_url,
655 'meta' => array(
656 'class' => 'mb-icon',
657 ),
658 )
659 );
660
661 $wp_admin_bar->add_menu(
662 array(
663 'parent' => $id,
664 'id' => 'billing',
665 'title' => esc_html__( 'Manage Purchases', 'jetpack' ),
666 'href' => 'https://wordpress.com/me/purchases',
667 'meta' => array(
668 'class' => 'mb-icon',
669 ),
670 )
671 );
672
673 $wp_admin_bar->add_menu(
674 array(
675 'parent' => $id,
676 'id' => 'security',
677 'title' => esc_html__( 'Security', 'jetpack' ),
678 'href' => 'https://wordpress.com/me/security',
679 'meta' => array(
680 'class' => 'mb-icon',
681 ),
682 )
683 );
684
685 $wp_admin_bar->add_menu(
686 array(
687 'parent' => $id,
688 'id' => 'notifications',
689 'title' => esc_html__( 'Notifications', 'jetpack' ),
690 'href' => 'https://wordpress.com/me/notifications',
691 'meta' => array(
692 'class' => 'mb-icon',
693 ),
694 )
695 );
696
697 $wp_admin_bar->add_menu(
698 array(
699 'parent' => $id,
700 'id' => 'special-header',
701 'title' => esc_html_x(
702 'Special',
703 'Title for Me sub-menu that contains Get Apps, Next Steps, and Help options',
704 'jetpack'
705 ),
706 'meta' => array(
707 'class' => 'ab-submenu-header',
708 ),
709 )
710 );
711
712 $wp_admin_bar->add_menu(
713 array(
714 'parent' => $id,
715 'id' => 'get-apps',
716 'title' => esc_html__( 'Get Apps', 'jetpack' ),
717 'href' => 'https://wordpress.com/me/get-apps',
718 'meta' => array(
719 'class' => 'mb-icon user-info-item',
720 ),
721 )
722 );
723
724 $help_link = 'https://jetpack.com/support/';
725
726 if ( jetpack_is_atomic_site() ) {
727 $help_link = 'https://wordpress.com/help';
728 }
729
730 $wp_admin_bar->add_menu(
731 array(
732 'parent' => $id,
733 'id' => 'help',
734 'title' => esc_html__( 'Help', 'jetpack' ),
735 'href' => $help_link,
736 'meta' => array(
737 'class' => 'mb-icon user-info-item',
738 ),
739 )
740 );
741 }
742
743 /**
744 * Add Write Menu item.
745 *
746 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
747 */
748 public function add_write_button( $wp_admin_bar ) {
749 $current_user = wp_get_current_user();
750
751 $posting_blog_id = get_current_blog_id();
752 if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
753 $posting_blog_id = $current_user->primary_blog;
754 }
755
756 $user_can_post = current_user_can_for_blog( $posting_blog_id, 'publish_posts' );
757
758 if ( ! $posting_blog_id || ! $user_can_post ) {
759 return;
760 }
761
762 $blog_post_page = 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug );
763
764 $wp_admin_bar->add_menu(
765 array(
766 'parent' => 'top-secondary',
767 'id' => 'ab-new-post',
768 'href' => $blog_post_page,
769 'title' => '<span>' . esc_html__( 'Write', 'jetpack' ) . '</span>',
770 'meta' => array(
771 'class' => 'mb-trackable',
772 ),
773 )
774 );
775 }
776
777 /**
778 * Add the "My Site" menu item in the root default group.
779 *
780 * @param WP_Admin_Bar $wp_admin_bar Admin Bar instance.
781 */
782 public function add_my_sites_submenu( $wp_admin_bar ) {
783 $current_user = wp_get_current_user();
784
785 $blog_name = get_bloginfo( 'name' );
786 if ( empty( $blog_name ) ) {
787 $blog_name = $this->primary_site_slug;
788 }
789
790 if ( mb_strlen( $blog_name ) > 20 ) {
791 $blog_name = mb_substr( html_entity_decode( $blog_name, ENT_QUOTES ), 0, 20 ) . '&hellip;';
792 }
793
794 $wp_admin_bar->add_menu(
795 array(
796 'parent' => 'root-default',
797 'id' => 'blog',
798 'title' => _n( 'My Site', 'My Sites', $this->user_site_count, 'jetpack' ),
799 'href' => '#',
800 'meta' => array(
801 'class' => 'my-sites mb-trackable',
802 ),
803 )
804 );
805
806 if ( $this->user_site_count > 1 ) {
807 $wp_admin_bar->add_menu(
808 array(
809 'parent' => 'blog',
810 'id' => 'switch-site',
811 'title' => esc_html__( 'Switch Site', 'jetpack' ),
812 'href' => 'https://wordpress.com/sites',
813 )
814 );
815 } else {
816 $wp_admin_bar->add_menu(
817 array(
818 'parent' => 'blog',
819 'id' => 'new-site',
820 'title' => esc_html__( '+ Add New WordPress', 'jetpack' ),
821 'href' => 'https://wordpress.com/start?ref=admin-bar-logged-in',
822 )
823 );
824 }
825
826 if ( is_user_member_of_blog( $current_user->ID ) ) {
827 $blavatar = '';
828 $class = 'current-site';
829
830 if ( has_site_icon() ) {
831 $src = get_site_icon_url();
832 $blavatar = '<img class="avatar" src="' . esc_attr( $src ) . '" alt="Current site avatar">';
833 $class = 'has-blavatar';
834 }
835
836 $blog_info = '<div class="ab-site-icon">' . $blavatar . '</div>';
837 $blog_info .= '<span class="ab-site-title">' . esc_html( $blog_name ) . '</span>';
838 $blog_info .= '<span class="ab-site-description">' . esc_html( $this->primary_site_url ) . '</span>';
839
840 $wp_admin_bar->add_menu(
841 array(
842 'parent' => 'blog',
843 'id' => 'blog-info',
844 'title' => $blog_info,
845 'href' => esc_url( trailingslashit( $this->primary_site_url ) ),
846 'meta' => array(
847 'class' => $class,
848 ),
849 )
850 );
851 }
852
853 // Site Preview.
854 if ( is_admin() ) {
855 $wp_admin_bar->add_menu(
856 array(
857 'parent' => 'blog',
858 'id' => 'site-view',
859 'title' => __( 'View Site', 'jetpack' ),
860 'href' => home_url(),
861 'meta' => array(
862 'class' => 'mb-icon',
863 'target' => '_blank',
864 ),
865 )
866 );
867 }
868
869 // Stats.
870 if ( Jetpack::is_module_active( 'stats' ) && current_user_can( 'view_stats' ) ) {
871 $wp_admin_bar->add_menu(
872 array(
873 'parent' => 'blog',
874 'id' => 'blog-stats',
875 'title' => esc_html__( 'Stats', 'jetpack' ),
876 'href' => 'https://wordpress.com/stats/' . esc_attr( $this->primary_site_slug ),
877 'meta' => array(
878 'class' => 'mb-icon',
879 ),
880 )
881 );
882 }
883
884 if ( current_user_can( 'manage_options' ) ) {
885 $wp_admin_bar->add_menu(
886 array(
887 'parent' => 'blog',
888 'id' => 'activity',
889 'title' => esc_html__( 'Activity', 'jetpack' ),
890 'href' => 'https://wordpress.com/activity-log/' . esc_attr( $this->primary_site_slug ),
891 'meta' => array(
892 'class' => 'mb-icon',
893 ),
894 )
895 );
896 }
897
898 // Add Calypso plans link and plan type indicator.
899 if ( is_user_member_of_blog( $current_user->ID ) && current_user_can( 'manage_options' ) ) {
900 $plans_url = 'https://wordpress.com/plans/' . esc_attr( $this->primary_site_slug );
901 $label = esc_html__( 'Plan', 'jetpack' );
902 $plan = Jetpack_Plan::get();
903
904 $plan_title = $this->create_menu_item_pair(
905 array(
906 'url' => $plans_url,
907 'id' => 'wp-admin-bar-plan',
908 'label' => $label,
909 ),
910 array(
911 'url' => $plans_url,
912 'id' => 'wp-admin-bar-plan-badge',
913 'label' => $plan['product_name_short'],
914 )
915 );
916
917 $wp_admin_bar->add_menu(
918 array(
919 'parent' => 'blog',
920 'id' => 'plan',
921 'title' => $plan_title,
922 'meta' => array(
923 'class' => 'inline-action',
924 ),
925 )
926 );
927 }
928
929 // Publish group.
930 $wp_admin_bar->add_group(
931 array(
932 'parent' => 'blog',
933 'id' => 'publish',
934 )
935 );
936
937 // Publish header.
938 $wp_admin_bar->add_menu(
939 array(
940 'parent' => 'publish',
941 'id' => 'publish-header',
942 'title' => esc_html_x( 'Manage', 'admin bar menu group label', 'jetpack' ),
943 'meta' => array(
944 'class' => 'ab-submenu-header',
945 ),
946 )
947 );
948
949 // Pages.
950 $pages_title = $this->create_menu_item_pair(
951 array(
952 'url' => 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
953 'id' => 'wp-admin-bar-edit-page',
954 'label' => esc_html__( 'Site Pages', 'jetpack' ),
955 ),
956 array(
957 'url' => 'https://wordpress.com/page/' . esc_attr( $this->primary_site_slug ),
958 'id' => 'wp-admin-bar-new-page-badge',
959 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
960 )
961 );
962
963 if ( ! current_user_can( 'edit_pages' ) ) {
964 $pages_title = $this->create_menu_item_anchor(
965 'ab-item ab-primary mb-icon',
966 'https://wordpress.com/pages/' . esc_attr( $this->primary_site_slug ),
967 esc_html__( 'Site Pages', 'jetpack' ),
968 'wp-admin-bar-edit-page'
969 );
970 }
971
972 $wp_admin_bar->add_menu(
973 array(
974 'parent' => 'publish',
975 'id' => 'new-page',
976 'title' => $pages_title,
977 'meta' => array(
978 'class' => 'inline-action',
979 ),
980 )
981 );
982
983 // Blog Posts.
984 $posts_title = $this->create_menu_item_pair(
985 array(
986 'url' => 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
987 'id' => 'wp-admin-bar-edit-post',
988 'label' => esc_html__( 'Blog Posts', 'jetpack' ),
989 ),
990 array(
991 'url' => 'https://wordpress.com/post/' . esc_attr( $this->primary_site_slug ),
992 'id' => 'wp-admin-bar-new-post-badge',
993 'label' => esc_html_x( 'Add', 'admin bar menu new item label', 'jetpack' ),
994 )
995 );
996
997 if ( ! current_user_can( 'edit_posts' ) ) {
998 $posts_title = $this->create_menu_item_anchor(
999 'ab-item ab-primary mb-icon',
1000 'https://wordpress.com/posts/' . esc_attr( $this->primary_site_slug ),
1001 esc_html__( 'Blog Posts', 'jetpack' ),
1002 'wp-admin-bar-edit-post'
1003 );
1004 }
1005
1006 $wp_admin_bar->add_menu(
1007 array(
1008 'parent' => 'publish',
1009 'id' => 'new-post',
1010 'title' => $posts_title,
1011 'meta' => array(
1012 'class' => 'inline-action mb-trackable',
1013 ),
1014 )
1015 );
1016
1017 // Comments.
1018 if ( current_user_can( 'moderate_comments' ) ) {
1019 $wp_admin_bar->add_menu(
1020 array(
1021 'parent' => 'publish',
1022 'id' => 'comments',
1023 'title' => __( 'Comments', 'jetpack' ),
1024 'href' => 'https://wordpress.com/comments/' . esc_attr( $this->primary_site_slug ),
1025 'meta' => array(
1026 'class' => 'mb-icon',
1027 ),
1028 )
1029 );
1030 }
1031
1032 // Testimonials.
1033 if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_testimonial' ) ) {
1034 $testimonials_title = $this->create_menu_item_pair(
1035 array(
1036 'url' => 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1037 'id' => 'wp-admin-bar-edit-testimonial',
1038 'label' => esc_html__( 'Testimonials', 'jetpack' ),
1039 ),
1040 array(
1041 'url' => 'https://wordpress.com/edit/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1042 'id' => 'wp-admin-bar-new-testimonial',
1043 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1044 )
1045 );
1046
1047 if ( ! current_user_can( 'edit_pages' ) ) {
1048 $testimonials_title = $this->create_menu_item_anchor(
1049 'ab-item ab-primary mb-icon',
1050 'https://wordpress.com/types/jetpack-testimonial/' . esc_attr( $this->primary_site_slug ),
1051 esc_html__( 'Testimonials', 'jetpack' ),
1052 'wp-admin-bar-edit-testimonial'
1053 );
1054 }
1055
1056 $wp_admin_bar->add_menu(
1057 array(
1058 'parent' => 'publish',
1059 'id' => 'new-jetpack-testimonial',
1060 'title' => $testimonials_title,
1061 'meta' => array(
1062 'class' => 'inline-action',
1063 ),
1064 )
1065 );
1066 }
1067
1068 // Portfolio.
1069 if ( Jetpack::is_module_active( 'custom-content-types' ) && get_option( 'jetpack_portfolio' ) ) {
1070 $portfolios_title = $this->create_menu_item_pair(
1071 array(
1072 'url' => 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1073 'id' => 'wp-admin-bar-edit-portfolio',
1074 'label' => esc_html__( 'Portfolio', 'jetpack' ),
1075 ),
1076 array(
1077 'url' => 'https://wordpress.com/edit/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1078 'id' => 'wp-admin-bar-new-portfolio',
1079 'label' => esc_html_x( 'Add', 'Button label for adding a new item via the toolbar menu', 'jetpack' ),
1080 )
1081 );
1082
1083 if ( ! current_user_can( 'edit_pages' ) ) {
1084 $portfolios_title = $this->create_menu_item_anchor(
1085 'ab-item ab-primary mb-icon',
1086 'https://wordpress.com/types/jetpack-portfolio/' . esc_attr( $this->primary_site_slug ),
1087 esc_html__( 'Portfolio', 'jetpack' ),
1088 'wp-admin-bar-edit-portfolio'
1089 );
1090 }
1091
1092 $wp_admin_bar->add_menu(
1093 array(
1094 'parent' => 'publish',
1095 'id' => 'new-jetpack-portfolio',
1096 'title' => $portfolios_title,
1097 'meta' => array(
1098 'class' => 'inline-action',
1099 ),
1100 )
1101 );
1102 }
1103
1104 if ( current_user_can( 'edit_theme_options' ) ) {
1105 // Look and Feel group.
1106 $wp_admin_bar->add_group(
1107 array(
1108 'parent' => 'blog',
1109 'id' => 'look-and-feel',
1110 )
1111 );
1112
1113 // Look and Feel header.
1114 $wp_admin_bar->add_menu(
1115 array(
1116 'parent' => 'look-and-feel',
1117 'id' => 'look-and-feel-header',
1118 'title' => esc_html_x( 'Personalize', 'admin bar menu group label', 'jetpack' ),
1119 'meta' => array(
1120 'class' => 'ab-submenu-header',
1121 ),
1122 )
1123 );
1124
1125 if ( is_admin() ) {
1126 // In wp-admin the `return` query arg will return to that page after closing the Customizer.
1127 $customizer_url = add_query_arg(
1128 array(
1129 'return' => rawurlencode( site_url( $_SERVER['REQUEST_URI'] ) ),
1130 ),
1131 wp_customize_url()
1132 );
1133 } else {
1134 /*
1135 * On the frontend the `url` query arg will load that page in the Customizer
1136 * and also return to it after closing
1137 * non-home URLs won't work unless we undo domain mapping
1138 * since the Customizer preview is unmapped to always have HTTPS.
1139 */
1140 $current_page = '//' . $this->primary_site_slug . $_SERVER['REQUEST_URI'];
1141 $customizer_url = add_query_arg( array( 'url' => rawurlencode( $current_page ) ), wp_customize_url() );
1142 }
1143
1144 $theme_title = $this->create_menu_item_pair(
1145 array(
1146 'url' => $customizer_url,
1147 'id' => 'wp-admin-bar-cmz',
1148 'label' => esc_html_x( 'Customize', 'admin bar customize item label', 'jetpack' ),
1149 ),
1150 array(
1151 'url' => 'https://wordpress.com/themes/' . esc_attr( $this->primary_site_slug ),
1152 'id' => 'wp-admin-bar-themes',
1153 'label' => esc_html__( 'Themes', 'jetpack' ),
1154 )
1155 );
1156 $meta = array(
1157 'class' => 'mb-icon',
1158 'class' => 'inline-action',
1159 );
1160 $href = false;
1161
1162 $wp_admin_bar->add_menu(
1163 array(
1164 'parent' => 'look-and-feel',
1165 'id' => 'themes',
1166 'title' => $theme_title,
1167 'href' => $href,
1168 'meta' => $meta,
1169 )
1170 );
1171 }
1172
1173 if ( current_user_can( 'manage_options' ) ) {
1174 // Configuration group.
1175 $wp_admin_bar->add_group(
1176 array(
1177 'parent' => 'blog',
1178 'id' => 'configuration',
1179 )
1180 );
1181
1182 // Configuration header.
1183 $wp_admin_bar->add_menu(
1184 array(
1185 'parent' => 'configuration',
1186 'id' => 'configuration-header',
1187 'title' => esc_html_x( 'Configure', 'admin bar menu group label', 'jetpack' ),
1188 'meta' => array(
1189 'class' => 'ab-submenu-header',
1190 ),
1191 )
1192 );
1193
1194 if ( Jetpack::is_module_active( 'publicize' ) || Jetpack::is_module_active( 'sharedaddy' ) ) {
1195 $wp_admin_bar->add_menu(
1196 array(
1197 'parent' => 'configuration',
1198 'id' => 'sharing',
1199 'title' => esc_html__( 'Sharing', 'jetpack' ),
1200 'href' => 'https://wordpress.com/sharing/' . esc_attr( $this->primary_site_slug ),
1201 'meta' => array(
1202 'class' => 'mb-icon',
1203 ),
1204 )
1205 );
1206 }
1207
1208 $people_title = $this->create_menu_item_pair(
1209 array(
1210 'url' => 'https://wordpress.com/people/team/' . esc_attr( $this->primary_site_slug ),
1211 'id' => 'wp-admin-bar-people',
1212 'label' => esc_html__( 'People', 'jetpack' ),
1213 ),
1214 array(
1215 'url' => admin_url( 'user-new.php' ),
1216 'id' => 'wp-admin-bar-people-add',
1217 'label' => esc_html_x( 'Add', 'admin bar people item label', 'jetpack' ),
1218 )
1219 );
1220
1221 $wp_admin_bar->add_menu(
1222 array(
1223 'parent' => 'configuration',
1224 'id' => 'users-toolbar',
1225 'title' => $people_title,
1226 'href' => false,
1227 'meta' => array(
1228 'class' => 'inline-action',
1229 ),
1230 )
1231 );
1232
1233 $plugins_title = $this->create_menu_item_pair(
1234 array(
1235 'url' => 'https://wordpress.com/plugins/' . esc_attr( $this->primary_site_slug ),
1236 'id' => 'wp-admin-bar-plugins',
1237 'label' => esc_html__( 'Plugins', 'jetpack' ),
1238 ),
1239 array(
1240 'url' => 'https://wordpress.com/plugins/manage/' . esc_attr( $this->primary_site_slug ),
1241 'id' => 'wp-admin-bar-plugins-add',
1242 'label' => esc_html_x( 'Manage', 'Label for the button on the Masterbar to manage plugins', 'jetpack' ),
1243 )
1244 );
1245
1246 $wp_admin_bar->add_menu(
1247 array(
1248 'parent' => 'configuration',
1249 'id' => 'plugins',
1250 'title' => $plugins_title,
1251 'href' => false,
1252 'meta' => array(
1253 'class' => 'inline-action',
1254 ),
1255 )
1256 );
1257
1258 if ( jetpack_is_atomic_site() ) {
1259 $domain_title = $this->create_menu_item_pair(
1260 array(
1261 'url' => 'https://wordpress.com/domains/' . esc_attr( $this->primary_site_slug ),
1262 'id' => 'wp-admin-bar-domains',
1263 'label' => esc_html__( 'Domains', 'jetpack' ),
1264 ),
1265 array(
1266 'url' => 'https://wordpress.com/domains/add/' . esc_attr( $this->primary_site_slug ),
1267 'id' => 'wp-admin-bar-domains-add',
1268 'label' => esc_html_x( 'Add', 'Label for the button on the Masterbar to add a new domain', 'jetpack' ),
1269 )
1270 );
1271 $wp_admin_bar->add_menu(
1272 array(
1273 'parent' => 'configuration',
1274 'id' => 'domains',
1275 'title' => $domain_title,
1276 'href' => false,
1277 'meta' => array(
1278 'class' => 'inline-action',
1279 ),
1280 )
1281 );
1282 }
1283
1284 $wp_admin_bar->add_menu(
1285 array(
1286 'parent' => 'configuration',
1287 'id' => 'blog-settings',
1288 'title' => esc_html__( 'Settings', 'jetpack' ),
1289 'href' => 'https://wordpress.com/settings/general/' . esc_attr( $this->primary_site_slug ),
1290 'meta' => array(
1291 'class' => 'mb-icon',
1292 ),
1293 )
1294 );
1295
1296 if ( ! is_admin() ) {
1297 $wp_admin_bar->add_menu(
1298 array(
1299 'parent' => 'configuration',
1300 'id' => 'legacy-dashboard',
1301 'title' => esc_html__( 'Dashboard', 'jetpack' ),
1302 'href' => admin_url(),
1303 'meta' => array(
1304 'class' => 'mb-icon',
1305 ),
1306 )
1307 );
1308 }
1309
1310 // Restore dashboard menu toggle that is needed on mobile views.
1311 if ( is_admin() ) {
1312 $wp_admin_bar->add_menu(
1313 array(
1314 'id' => 'menu-toggle',
1315 'title' => '<span class="ab-icon"></span><span class="screen-reader-text">' . esc_html__( 'Menu', 'jetpack' ) . '</span>',
1316 'href' => '#',
1317 )
1318 );
1319 }
1320
1321 /**
1322 * Fires when menu items are added to the masterbar "My Sites" menu.
1323 *
1324 * @since 5.4.0
1325 */
1326 do_action( 'jetpack_masterbar' );
1327 }
1328 }
1329 }
1330