| @@ -10,8 +10,12 @@ | ||
| 10 | 10 | use Automattic\Jetpack\Partner_Coupon as Jetpack_Partner_Coupon; |
| 11 | 11 | use Automattic\Jetpack\Status; |
| 12 | 12 | use Automattic\Jetpack\Status\Host; |
| 13 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 14 | 18 | /** |
| 15 | 19 | * Build the Jetpack admin menu as a whole. |
| 16 | 20 | */ |
| 17 | 21 | class Jetpack_Admin { |
| @@ -28,13 +32,8 @@ | ||
| 28 | 32 | * |
| 29 | 33 | * @return self |
| 30 | 34 | */ |
| 31 | 35 | public static function init() { |
| 32 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 33 | - if ( isset( $_GET['page'] ) && 'jetpack' === $_GET['page'] ) { | |
| 34 | - add_filter( 'nocache_headers', array( 'Jetpack_Admin', 'add_no_store_header' ), 100 ); | |
| 35 | - } | |
| 36 | - | |
| 37 | 36 | if ( self::$instance === null ) { |
| 38 | 37 | self::$instance = new Jetpack_Admin(); |
| 39 | 38 | } |
| 40 | 39 | return self::$instance; |
| @@ -42,12 +41,14 @@ | ||
| 42 | 41 | |
| 43 | 42 | /** |
| 44 | 43 | * Filter callback to add `no-store` to the `Cache-Control` header. |
| 45 | 44 | * |
| 45 | + * @deprecated 14.9 | |
| 46 | 46 | * @param array $headers Headers array. |
| 47 | 47 | * @return array Modified headers array. |
| 48 | 48 | */ |
| 49 | 49 | public static function add_no_store_header( $headers ) { |
| 50 | + _deprecated_function( __METHOD__, '14.9' ); | |
| 50 | 51 | $headers['Cache-Control'] .= ', no-store'; |
| 51 | 52 | return $headers; |
| 52 | 53 | } |
| 53 | 54 | |
| @@ -55,8 +56,11 @@ | ||
| 55 | 56 | private function __construct() { |
| 56 | 57 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php'; |
| 57 | 58 | $jetpack_react = new Jetpack_React_Page(); |
| 58 | 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 | + | |
| 59 | 63 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-settings-page.php'; |
| 60 | 64 | $fallback_page = new Jetpack_Settings_Page(); |
| 61 | 65 | |
| 62 | 66 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php'; |
| @@ -61,15 +65,20 @@ | ||
| 61 | 65 | |
| 62 | 66 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php'; |
| 63 | 67 | $jetpack_about = new Jetpack_About_Page(); |
| 64 | 68 | |
| 69 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-ai-page.php'; | |
| 70 | + $jetpack_ai = new Jetpack_AI_Page(); | |
| 71 | + | |
| 65 | 72 | add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 ); |
| 66 | 73 | add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 ); |
| 67 | - add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_dashboard_sub_nav_item' ) ); | |
| 68 | - 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' ) ); | |
| 69 | 77 | add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) ); |
| 70 | 78 | add_action( 'jetpack_admin_menu', array( $fallback_page, 'add_actions' ) ); |
| 71 | 79 | add_action( 'jetpack_admin_menu', array( $jetpack_about, 'add_actions' ) ); |
| 80 | + add_action( 'jetpack_admin_menu', array( $jetpack_ai, 'add_actions' ) ); | |
| 72 | 81 | |
| 73 | 82 | // Add redirect to current page for activation/deactivation of modules. |
| 74 | 83 | add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 ); |
| 75 | 84 | add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ), 10, 2 ); |
| @@ -93,10 +102,16 @@ | ||
| 93 | 102 | ); |
| 94 | 103 | |
| 95 | 104 | // Add an Anti-spam menu item for Jetpack. This is handled automatically by the Admin_Menu as long as it has been initialized. |
| 96 | 105 | Admin_Menu::init(); |
| 97 | - add_action( 'admin_enqueue_scripts', array( $this, 'akismet_logo_replacement_styles' ) ); | |
| 98 | 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(); | |
| 99 | 114 | } |
| 100 | 115 | |
| 101 | 116 | // Ensure an Additional CSS menu item is added to the Appearance menu whenever Jetpack is connected. |
| 102 | 117 | add_action( 'admin_menu', array( $this, 'additional_css_menu' ) ); |
| @@ -104,19 +119,19 @@ | ||
| 104 | 119 | add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 ); |
| 105 | 120 | |
| 106 | 121 | // Register Jetpack partner coupon hooks. |
| 107 | 122 | Jetpack_Partner_Coupon::register_coupon_admin_hooks( 'jetpack', Jetpack::admin_url() ); |
| 108 | - } | |
| 109 | 123 | |
| 110 | - /** | |
| 111 | - * Generate styles to replace Akismet logo for the Jetpack Akismet Anti-spam logo. | |
| 112 | - Without this, we would have to change the logo from Akismet codebase and we want to avoid that. | |
| 113 | - */ | |
| 114 | - public function akismet_logo_replacement_styles() { | |
| 115 | - $logo_url = esc_url( plugins_url( 'images/products/logo-anti-spam.svg', JETPACK__PLUGIN_FILE ) ); | |
| 116 | - $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; }"; | |
| 117 | - $style .= '@media screen and (max-width: 782px) { .akismet-masthead__logo-container { margin-left: 4px; } }'; | |
| 118 | - 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' ) ); | |
| 119 | 134 | } |
| 120 | 135 | |
| 121 | 136 | /** |
| 122 | 137 | * Handle our Additional CSS menu item and legacy page declaration. |
| @@ -138,9 +153,14 @@ | ||
| 138 | 153 | // If the site is a WoA site and the custom-css feature is not available, return. |
| 139 | 154 | // See https://github.com/Automattic/jetpack/pull/19965 for more on how this menu item is dealt with on WoA sites. |
| 140 | 155 | if ( ( new Host() )->is_woa_site() && ! ( in_array( 'custom-css', Jetpack::get_available_modules(), true ) ) ) { |
| 141 | 156 | return; |
| 142 | - } 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 | + ) { | |
| 143 | 163 | // Add in our legacy page to support old bookmarks and such. |
| 144 | 164 | add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) ); |
| 145 | 165 | |
| 146 | 166 | // Add in our new page slug that will redirect to the customizer. |
| @@ -145,13 +165,8 @@ | ||
| 145 | 165 | |
| 146 | 166 | // Add in our new page slug that will redirect to the customizer. |
| 147 | 167 | $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) ); |
| 148 | 168 | add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) ); |
| 149 | - } elseif ( class_exists( 'Jetpack' ) && Jetpack::is_connection_ready() ) { // Link to the Jetpack Settings > Writing page, highlighting the Custom CSS setting. | |
| 150 | - add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'theme_enhancements_redirect' ) ); | |
| 151 | - | |
| 152 | - $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-theme-enhancements-redirect', array( __CLASS__, 'theme_enhancements_redirect' ) ); | |
| 153 | - add_action( "load-{$hook}", array( __CLASS__, 'theme_enhancements_redirect' ) ); | |
| 154 | 169 | } |
| 155 | 170 | } |
| 156 | 171 | |
| 157 | 172 | /** |
| @@ -162,8 +177,10 @@ | ||
| 162 | 177 | * |
| 163 | 178 | * There is a core patch in trac that would make this unnecessary. |
| 164 | 179 | * |
| 165 | 180 | * @link https://core.trac.wordpress.org/ticket/39050 |
| 181 | + * | |
| 182 | + * @return never | |
| 166 | 183 | */ |
| 167 | 184 | public static function customizer_redirect() { |
| 168 | 185 | wp_safe_redirect( |
| 169 | 186 | self::customizer_link( |
| @@ -171,24 +188,12 @@ | ||
| 171 | 188 | 'return_url' => wp_get_referer(), |
| 172 | 189 | ) |
| 173 | 190 | ) |
| 174 | 191 | ); |
| 175 | - exit; | |
| 192 | + exit( 0 ); | |
| 176 | 193 | } |
| 177 | 194 | |
| 178 | 195 | /** |
| 179 | - * Handle the Additional CSS redirect to the Jetpack settings Theme Enhancements section. | |
| 180 | - * | |
| 181 | - * @since 11.0 | |
| 182 | - */ | |
| 183 | - public static function theme_enhancements_redirect() { | |
| 184 | - wp_safe_redirect( | |
| 185 | - 'admin.php?page=jetpack#/writing?term=custom-css' | |
| 186 | - ); | |
| 187 | - exit; | |
| 188 | - } | |
| 189 | - | |
| 190 | - /** | |
| 191 | 196 | * Build the URL to deep link to the Customizer. |
| 192 | 197 | * |
| 193 | 198 | * You can modify the return url via $args. |
| 194 | 199 | * |
| @@ -320,9 +325,9 @@ | ||
| 320 | 325 | * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); |
| 321 | 326 | * |
| 322 | 327 | * @since 3.5.0 |
| 323 | 328 | * |
| 324 | - * @param string The search terms (comma separated). | |
| 329 | + * @param string The search terms (comma-separated). | |
| 325 | 330 | */ |
| 326 | 331 | echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 327 | 332 | $module_array['search_terms'] = ob_get_clean(); |
| 328 | 333 | |
| @@ -453,9 +458,9 @@ | ||
| 453 | 458 | * level connection should be unavailable. |
| 454 | 459 | */ |
| 455 | 460 | if ( ( new Status() )->is_offline_mode() ) { |
| 456 | 461 | if ( $module['requires_connection'] || $module['requires_user_connection'] ) { |
| 457 | - return __( 'Offline mode', 'jetpack' ); | |
| 462 | + return __( 'Unavailable in Offline mode', 'jetpack' ); | |
| 458 | 463 | } |
| 459 | 464 | } |
| 460 | 465 | |
| 461 | 466 | /* |
| @@ -501,9 +506,9 @@ | ||
| 501 | 506 | Jetpack::activate_module( $module, false ); |
| 502 | 507 | } |
| 503 | 508 | // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. |
| 504 | 509 | wp_safe_redirect( wp_get_referer() ); |
| 505 | - exit; | |
| 510 | + exit( 0 ); | |
| 506 | 511 | case 'bulk-deactivate': |
| 507 | 512 | check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); |
| 508 | 513 | if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { |
| 509 | 514 | break; |
| @@ -516,9 +521,9 @@ | ||
| 516 | 521 | Jetpack::state( 'message', 'module_deactivated' ); |
| 517 | 522 | } |
| 518 | 523 | Jetpack::state( 'module', $modules ); |
| 519 | 524 | wp_safe_redirect( wp_get_referer() ); |
| 520 | - exit; | |
| 525 | + exit( 0 ); | |
| 521 | 526 | default: |
| 522 | 527 | return; |
| 523 | 528 | } |
| 524 | 529 | } |
| @@ -565,9 +570,15 @@ | ||
| 565 | 570 | nocache_headers(); |
| 566 | 571 | if ( ! current_user_can( 'manage_options' ) ) { |
| 567 | 572 | die( '-1' ); |
| 568 | 573 | } |
| 569 | - 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 | + ); | |
| 570 | 581 | } |
| 571 | 582 | |
| 572 | 583 | /** |
| 573 | 584 | * Display debugger page. |
| @@ -599,7 +610,110 @@ | ||
| 599 | 610 | return false; |
| 600 | 611 | } |
| 601 | 612 | |
| 602 | 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 | + return false; | |
| 628 | + } | |
| 629 | + | |
| 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; | |
| 603 | 717 | } |
| 604 | 718 | } |
| 605 | 719 | Jetpack_Admin::init(); |