PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 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 All 506 releases
← All changes | class.jetpack-admin.php +161 -75 12.4.2 → 16.3-a.7 View file →
@@ -4,13 +4,18 @@
4 4 *
5 5 * @package automattic/jetpack
6 6 */
7 7
8 +use Automattic\Jetpack\Admin_UI\Admin_Menu;
8 9 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
9 10 use Automattic\Jetpack\Partner_Coupon as Jetpack_Partner_Coupon;
10 11 use Automattic\Jetpack\Status;
11 12 use Automattic\Jetpack\Status\Host;
12 13
14 +if ( ! defined( 'ABSPATH' ) ) {
15 + exit( 0 );
16 +}
17 +
13 18 /**
14 19 * Build the Jetpack admin menu as a whole.
15 20 */
16 21 class Jetpack_Admin {
@@ -27,13 +32,8 @@
27 32 *
28 33 * @return self
29 34 */
30 35 public static function init() {
31 - // phpcs:ignore WordPress.Security.NonceVerification.Recommended
32 - if ( isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) {
33 - add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 );
34 - }
35 -
36 36 if ( self::$instance === null ) {
37 37 self::$instance = new Jetpack_Admin();
38 38 }
39 39 return self::$instance;
@@ -41,12 +41,14 @@
41 41
42 42 /**
43 43 * Filter callback to add `no-store` to the `Cache-Control` header.
44 44 *
45 + * @deprecated 14.9
45 46 * @param array $headers Headers array.
46 47 * @return array Modified headers array.
47 48 */
48 49 public static function add_no_store_header( $headers ) {
50 + _deprecated_function( __METHOD__, '14.9' );
49 51 $headers['Cache-Control'] .= ', no-store';
50 52 return $headers;
51 53 }
52 54
@@ -54,8 +56,11 @@
54 56 private function __construct() {
55 57 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php';
56 58 $jetpack_react = new Jetpack_React_Page();
57 59
60 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-settings-react-page.php';
61 + $jetpack_settings = new Jetpack_Settings_React_Page();
62 +
58 63 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-settings-page.php';
59 64 $fallback_page = new Jetpack_Settings_Page();
60 65
61 66 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php';
@@ -60,15 +65,20 @@
60 65
61 66 require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php';
62 67 $jetpack_about = new Jetpack_About_Page();
63 68
69 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-ai-page.php';
70 + $jetpack_ai = new Jetpack_AI_Page();
71 +
64 72 add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 );
65 73 add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 );
66 - add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) );
67 - add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
74 + add_action( 'admin_menu', array( $jetpack_react, 'remove_jetpack_menu' ), 2000 );
75 + add_action( 'admin_menu', array( $jetpack_settings, 'maybe_load_wp_build' ), 1 );
76 + add_action( 'jetpack_admin_menu', array( $jetpack_settings, 'add_actions' ) );
68 77 add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
69 78 add_action( 'jetpack_admin_menu', array( $fallback_page, 'add_actions' ) );
70 79 add_action( 'jetpack_admin_menu', array( $jetpack_about, 'add_actions' ) );
80 + add_action( 'jetpack_admin_menu', array( $jetpack_ai, 'add_actions' ) );
71 81
72 82 // Add redirect to current page for activation/deactivation of modules.
73 83 add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 );
74 84 add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ), 10, 2 );
@@ -76,13 +86,13 @@
76 86 // Add module bulk actions handler.
77 87 add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
78 88
79 89 if ( class_exists( 'Akismet_Admin' ) ) {
80 - // If the site has Jetpack Anti-Spam, change the Akismet menu label and logo accordingly.
90 + // If the site has Jetpack Anti-spam, change the Akismet menu label and logo accordingly.
81 91 $site_products = array_column( Jetpack_Plan::get_products(), 'product_slug' );
82 92 $has_anti_spam_product = count( array_intersect( array( 'jetpack_anti_spam', 'jetpack_anti_spam_monthly' ), $site_products ) ) > 0;
83 93
84 - if ( Jetpack_Plan::supports( 'antispam' ) || $has_anti_spam_product ) {
94 + if ( Jetpack_Plan::supports( 'akismet' ) || Jetpack_Plan::supports( 'antispam' ) || $has_anti_spam_product ) {
85 95 // Prevent Akismet from adding a menu item.
86 96 add_action(
87 97 'admin_menu',
88 98 function () {
@@ -90,17 +100,18 @@
90 100 },
91 101 4
92 102 );
93 103
94 - // Add an Anti-spam menu item for Jetpack.
95 - add_action(
96 - 'jetpack_admin_menu',
97 - function () {
98 - add_submenu_page( 'jetpack', __( 'Anti-Spam', 'jetpack' ), __( 'Anti-Spam', 'jetpack' ), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
99 - }
100 - );
101 - add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) );
104 + // Add an Anti-spam menu item for Jetpack. This is handled automatically by the Admin_Menu as long as it has been initialized.
105 + Admin_Menu::init();
102 106 }
107 +
108 + // Render the unified Jetpack header/footer chrome on Akismet's admin pages by
109 + // consuming Akismet's `akismet_header` / `akismet_footer` action hooks. This does
110 + // not modify the Akismet plugin; it only registers callbacks on its own hooks.
111 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-akismet-admin-chrome.php';
112 + $akismet_admin_chrome = new Akismet_Admin_Chrome();
113 + $akismet_admin_chrome->init_hooks();
103 114 }
104 115
105 116 // Ensure an Additional CSS menu item is added to the Appearance menu whenever Jetpack is connected.
106 117 add_action( 'admin_menu', array( $this, 'additional_css_menu' ) );
@@ -108,19 +119,19 @@
108 119 add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 );
109 120
110 121 // Register Jetpack partner coupon hooks.
111 122 Jetpack_Partner_Coupon::register_coupon_admin_hooks( 'jetpack', Jetpack::admin_url() );
112 - }
113 123
114 - /**
115 - * Generate styles to replace Akismet logo for the Jetpack Akismet Anti-Spam logo.
116 - Without this, we would have to change the logo from Akismet codebase and we want to avoid that.
117 - */
118 - public function akismet_logo_replacement_styles() {
119 - $logo_url = esc_url( plugins_url( 'images/products/logo-anti-spam.svg', JETPACK__PLUGIN_FILE ) );
120 - $style = ".akismet-masthead__logo-container { background: url({$logo_url}) no-repeat; min-height: 42px; margin: 20px 0; padding: 0 !important; } .akismet-masthead__logo { display: none; }";
121 - $style .= '@media screen and (max-width: 782px) { .akismet-masthead__logo-container { margin-left: 4px; } }';
122 - wp_add_inline_style( 'admin-bar', $style );
124 + // Remove default WordPress admin footer on Jetpack pages only.
125 + add_filter( 'admin_footer_text', array( $this, 'maybe_remove_admin_footer_text' ) );
126 + add_filter( 'update_footer', array( $this, 'maybe_remove_admin_footer_version' ), 11 );
127 + add_filter( 'admin_body_class', array( $this, 'add_jetpack_admin_body_class' ) );
128 + add_action( 'admin_head', array( $this, 'add_footer_removal_styles' ) );
129 +
130 + // Make WPDS design tokens resolve at runtime on the legacy/wrap_ui Jetpack
131 + // admin pages (Dashboard, Settings, Debugger) that don't ship their own
132 + // `:root{--wpds-*}` source. Delegates to Admin_Menu's shared enqueue API.
133 + add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_design_tokens' ) );
123 134 }
124 135
125 136 /**
126 137 * Handle our Additional CSS menu item and legacy page declaration.
@@ -131,9 +142,9 @@
131 142 /*
132 143 * Custom CSS for the Customizer is deprecated for block themes as of WP 6.1, so we only expose it with a menu
133 144 * if the site already has existing CSS code.
134 145 */
135 - if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
146 + if ( wp_is_block_theme() ) {
136 147 $styles = wp_get_custom_css();
137 148 if ( ! $styles ) {
138 149 return;
139 150 }
@@ -142,9 +153,14 @@
142 153 // If the site is a WoA site and the custom-css feature is not available, return.
143 154 // See https://github.com/Automattic/jetpack/pull/19965 for more on how this menu item is dealt with on WoA sites.
144 155 if ( ( new Host() )->is_woa_site() && ! ( in_array( 'custom-css', Jetpack::get_available_modules(), true ) ) ) {
145 156 return;
146 - } elseif ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'custom-css' ) ) { // If the Custom CSS module is enabled, add the Additional CSS menu item and link to the Customizer.
157 + } elseif (
158 + class_exists( 'Jetpack' ) && (
159 + Jetpack::is_module_active( 'custom-css' ) || // If the Custom CSS module is enabled, add the Additional CSS menu item and link to the Customizer.
160 + ( wp_is_block_theme() && ! empty( wp_get_custom_css() ) ) // Do the same if the theme is block-based but has existing custom CSS.
161 + )
162 + ) {
147 163 // Add in our legacy page to support old bookmarks and such.
148 164 add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) );
149 165
150 166 // Add in our new page slug that will redirect to the customizer.
@@ -149,13 +165,8 @@
149 165
150 166 // Add in our new page slug that will redirect to the customizer.
151 167 $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) );
152 168 add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) );
153 - } elseif ( class_exists( 'Jetpack' ) && Jetpack::is_connection_ready() ) { // Link to the Jetpack Settings > Writing page, highlighting the Custom CSS setting.
154 - add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'theme_enhancements_redirect' ) );
155 -
156 - $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-theme-enhancements-redirect', array( __CLASS__, 'theme_enhancements_redirect' ) );
157 - add_action( "load-{$hook}", array( __CLASS__, 'theme_enhancements_redirect' ) );
158 169 }
159 170 }
160 171
161 172 /**
@@ -166,8 +177,10 @@
166 177 *
167 178 * There is a core patch in trac that would make this unnecessary.
168 179 *
169 180 * @link https://core.trac.wordpress.org/ticket/39050
181 + *
182 + * @return never
170 183 */
171 184 public static function customizer_redirect() {
172 185 wp_safe_redirect(
173 186 self::customizer_link(
@@ -175,24 +188,12 @@
175 188 'return_url' => wp_get_referer(),
176 189 )
177 190 )
178 191 );
179 - exit;
192 + exit( 0 );
180 193 }
181 194
182 195 /**
183 - * Handle the Additional CSS redirect to the Jetpack settings Theme Enhancements section.
184 - *
185 - * @since 11.0
186 - */
187 - public static function theme_enhancements_redirect() {
188 - wp_safe_redirect(
189 - 'admin.php?page=jetpack#/writing?term=custom-css'
190 - );
191 - exit;
192 - }
193 -
194 - /**
195 196 * Build the URL to deep link to the Customizer.
196 197 *
197 198 * You can modify the return url via $args.
198 199 *
@@ -231,17 +232,9 @@
231 232 * @param array $module2 Module data.
232 233 * @return int Indicating the relative ordering of module1 and module2.
233 234 */
234 235 public static function sort_requires_connection_last( $module1, $module2 ) {
235 - if ( (bool) $module1['requires_connection'] === (bool) $module2['requires_connection'] ) {
236 - return 0;
237 - } elseif ( $module1['requires_connection'] ) {
238 - return 1;
239 - } elseif ( $module2['requires_connection'] ) {
240 - return -1;
241 - }
242 -
243 - return 0;
236 + return ( (bool) $module1['requires_connection'] ) <=> ( (bool) $module2['requires_connection'] );
244 237 }
245 238
246 239 /**
247 240 * Produce JS understandable objects of modules containing information for
@@ -332,9 +325,9 @@
332 325 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
333 326 *
334 327 * @since 3.5.0
335 328 *
336 - * @param string The search terms (comma separated).
329 + * @param string The search terms (comma-separated).
337 330 */
338 331 echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
339 332 $module_array['search_terms'] = ob_get_clean();
340 333
@@ -465,9 +458,9 @@
465 458 * level connection should be unavailable.
466 459 */
467 460 if ( ( new Status() )->is_offline_mode() ) {
468 461 if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
469 - return __( 'Offline mode', 'jetpack' );
462 + return __( 'Unavailable in Offline mode', 'jetpack' );
470 463 }
471 464 }
472 465
473 466 /*
@@ -513,9 +506,9 @@
513 506 Jetpack::activate_module( $module, false );
514 507 }
515 508 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
516 509 wp_safe_redirect( wp_get_referer() );
517 - exit;
510 + exit( 0 );
518 511 case 'bulk-deactivate':
519 512 check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
520 513 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
521 514 break;
@@ -528,9 +521,9 @@
528 521 Jetpack::state( 'message', 'module_deactivated' );
529 522 }
530 523 Jetpack::state( 'module', $modules );
531 524 wp_safe_redirect( wp_get_referer() );
532 - exit;
525 + exit( 0 );
533 526 default:
534 527 return;
535 528 }
536 529 }
@@ -577,9 +570,15 @@
577 570 nocache_headers();
578 571 if ( ! current_user_can( 'manage_options' ) ) {
579 572 die( '-1' );
580 573 }
581 - Jetpack_Admin_Page::wrap_ui( array( $this, 'debugger_page' ), array( 'is-wide' => true ) );
574 + Jetpack_Admin_Page::wrap_ui(
575 + array( $this, 'debugger_page' ),
576 + array(
577 + 'is-wide' => true,
578 + 'show-nav' => false,
579 + )
580 + );
582 581 }
583 582
584 583 /**
585 584 * Display debugger page.
@@ -610,24 +609,111 @@
610 609 ) ) {
611 610 return false;
612 611 }
613 612
614 - // Disable all JITMs on pages where the recommendations banner is displaying.
615 - if (
616 - in_array(
617 - $screen_id,
618 - array(
619 - 'dashboard',
620 - 'plugins',
621 - 'jetpack_page_stats',
622 - ),
623 - true
624 - )
625 - && \Jetpack_Recommendations_Banner::can_be_displayed()
626 - ) {
613 + return $value;
614 + }
615 +
616 + /**
617 + * Check if we're on a Jetpack admin page.
618 + *
619 + * Similar to how WooCommerce checks for its admin pages by comparing
620 + * against known screen ID patterns.
621 + *
622 + * @return bool True if on a Jetpack admin page, false otherwise.
623 + */
624 + private function is_jetpack_admin_page() {
625 + $screen = get_current_screen();
626 + if ( ! $screen ) {
627 627 return false;
628 628 }
629 629
630 - return $value;
630 + // Check for Jetpack admin pages:
631 + // - toplevel_page_jetpack (main Jetpack menu page)
632 + // - toplevel_page_jetpack-network (Jetpack Network Admin menu page)
633 + // - jetpack_page_* (Jetpack submenu pages)
634 + // - admin_page_jetpack* (legacy/special Jetpack pages)
635 + // Or check if parent_base is 'jetpack' or 'jetpack-network' (submenu pages)
636 + return (
637 + $screen->id === 'toplevel_page_jetpack' ||
638 + $screen->id === 'toplevel_page_jetpack-network' ||
639 + str_starts_with( $screen->id, 'jetpack_page_' ) ||
640 + str_starts_with( $screen->id, 'admin_page_jetpack' ) ||
641 + $screen->parent_base === 'jetpack' ||
642 + $screen->parent_base === 'jetpack-network'
643 + );
644 + }
645 +
646 + /**
647 + * Add a body class to Jetpack admin pages.
648 + *
649 + * @param string $classes Space-separated list of CSS classes.
650 + * @return string Modified class list.
651 + */
652 + public function add_jetpack_admin_body_class( $classes ) {
653 + if ( $this->is_jetpack_admin_page() ) {
654 + return ltrim( trim( $classes ) . ' jetpack-admin-page ' );
655 + }
656 + return $classes;
657 + }
658 +
659 + /**
660 + * Add inline styles to remove footer padding on Jetpack pages.
661 + *
662 + * This needs to be inline because jetpack-admin.css is not loaded on
663 + * React-powered admin pages (they use load_wrapper_styles instead).
664 + */
665 + public function add_footer_removal_styles() {
666 + if ( ! $this->is_jetpack_admin_page() ) {
667 + return;
668 + }
669 + echo '<style>.jetpack-admin-page #wpbody-content { padding-bottom: 0; } .jetpack-admin-page #wpfooter { display: none; }</style>';
670 + }
671 +
672 + /**
673 + * Enqueues the shared WPDS design-tokens stylesheet on the legacy/wrap_ui pages.
674 + *
675 + * This is the admin_enqueue_scripts callback for the legacy Jetpack admin
676 + * pages. The admin-ui package owns the handle and enqueues it on the
677 + * modernized dashboards it registers; the legacy/wrap_ui pages (Dashboard,
678 + * Settings, Debugger) aren't registered through Admin_Menu, so we cover them
679 + * here via the central is_jetpack_admin_page() gate. The actual enqueue is
680 + * delegated to the reusable Admin_Menu::enqueue_design_tokens() API so there
681 + * is a single owner of the handle and no duplicated register/enqueue logic.
682 + *
683 + * @return void
684 + */
685 + public function maybe_enqueue_design_tokens() {
686 + if ( ! $this->is_jetpack_admin_page() ) {
687 + return;
688 + }
689 +
690 + // Guard against an older admin-ui being loaded ahead of this one by the
691 + // package autoloader's version-precedence resolution.
692 + if ( ! method_exists( Admin_Menu::class, 'enqueue_design_tokens' ) ) {
693 + return;
694 + }
695 +
696 + Admin_Menu::enqueue_design_tokens();
697 + }
698 +
699 + /**
700 + * Remove the admin footer text on Jetpack pages.
701 + *
702 + * @param string $content The default footer text.
703 + * @return string Empty string on Jetpack pages, original content otherwise.
704 + */
705 + public function maybe_remove_admin_footer_text( $content ) {
706 + return $this->is_jetpack_admin_page() ? '' : $content;
707 + }
708 +
709 + /**
710 + * Remove the admin footer version on Jetpack pages.
711 + *
712 + * @param string $content The default footer version text.
713 + * @return string Empty string on Jetpack pages, original content otherwise.
714 + */
715 + public function maybe_remove_admin_footer_version( $content ) {
716 + return $this->is_jetpack_admin_page() ? '' : $content;
631 717 }
632 718 }
633 719 Jetpack_Admin::init();