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