Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.56% covered (danger)
30.56%
77 / 252
4.76% covered (danger)
4.76%
1 / 21
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Admin
30.92% covered (danger)
30.92%
77 / 249
4.76% covered (danger)
4.76%
1 / 21
3263.48
0.00% covered (danger)
0.00%
0 / 1
 init
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 add_no_store_header
n/a
0 / 0
n/a
0 / 0
1
 __construct
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
30
 additional_css_menu
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
90
 customizer_redirect
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 customizer_link
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 sort_requires_connection_last
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_modules
91.30% covered (success)
91.30%
42 / 46
0.00% covered (danger)
0.00%
0 / 1
13.11
 is_module_available
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
12.04
 get_module_unavailable_reason
90.00% covered (success)
90.00%
18 / 20
0.00% covered (danger)
0.00%
0 / 1
15.22
 handle_unrecognized_action
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
110
 fix_redirect
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 admin_menu_debugger
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 wrap_debugger_page
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 debugger_page
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 should_display_jitms_on_screen
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 is_jetpack_admin_page
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
 add_jetpack_admin_body_class
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 add_footer_removal_styles
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 maybe_enqueue_design_tokens
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 maybe_remove_admin_footer_text
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 maybe_remove_admin_footer_version
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Build the Jetpack admin menu as a whole.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Admin_UI\Admin_Menu;
9use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
10use Automattic\Jetpack\Partner_Coupon as Jetpack_Partner_Coupon;
11use Automattic\Jetpack\Status;
12use Automattic\Jetpack\Status\Host;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * Build the Jetpack admin menu as a whole.
20 */
21class Jetpack_Admin {
22
23    /**
24     * Static instance.
25     *
26     * @var Jetpack_Admin
27     */
28    private static $instance = null;
29
30    /**
31     * Initialize and fetch the static instance.
32     *
33     * @return self
34     */
35    public static function init() {
36        if ( self::$instance === null ) {
37            self::$instance = new Jetpack_Admin();
38        }
39        return self::$instance;
40    }
41
42    /**
43     * Filter callback to add `no-store` to the `Cache-Control` header.
44     *
45     * @deprecated 14.9
46     * @param array $headers Headers array.
47     * @return array Modified headers array.
48     */
49    public static function add_no_store_header( $headers ) {
50        _deprecated_function( __METHOD__, '14.9' );
51        $headers['Cache-Control'] .= ', no-store';
52        return $headers;
53    }
54
55    /** Constructor. */
56    private function __construct() {
57        require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php';
58        $jetpack_react = new Jetpack_React_Page();
59
60        require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-settings-page.php';
61        $fallback_page = new Jetpack_Settings_Page();
62
63        require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-about-page.php';
64        $jetpack_about = new Jetpack_About_Page();
65
66        require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-ai-page.php';
67        $jetpack_ai = new Jetpack_AI_Page();
68
69        add_action( 'admin_init', array( $jetpack_react, 'react_redirects' ), 0 );
70        add_action( 'admin_menu', array( $jetpack_react, 'add_actions' ), 998 );
71        add_action( 'admin_menu', array( $jetpack_react, 'remove_jetpack_menu' ), 2000 );
72        add_action( 'jetpack_admin_menu', array( $jetpack_react, 'jetpack_add_settings_sub_nav_item' ) );
73        add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) );
74        add_action( 'jetpack_admin_menu', array( $fallback_page, 'add_actions' ) );
75        add_action( 'jetpack_admin_menu', array( $jetpack_about, 'add_actions' ) );
76        add_action( 'jetpack_admin_menu', array( $jetpack_ai, 'add_actions' ) );
77
78        // Add redirect to current page for activation/deactivation of modules.
79        add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 );
80        add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ), 10, 2 );
81
82        // Add module bulk actions handler.
83        add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) );
84
85        if ( class_exists( 'Akismet_Admin' ) ) {
86            // If the site has Jetpack Anti-spam, change the Akismet menu label and logo accordingly.
87            $site_products         = array_column( Jetpack_Plan::get_products(), 'product_slug' );
88            $has_anti_spam_product = count( array_intersect( array( 'jetpack_anti_spam', 'jetpack_anti_spam_monthly' ), $site_products ) ) > 0;
89
90            if ( Jetpack_Plan::supports( 'akismet' ) || Jetpack_Plan::supports( 'antispam' ) || $has_anti_spam_product ) {
91                // Prevent Akismet from adding a menu item.
92                add_action(
93                    'admin_menu',
94                    function () {
95                        remove_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 );
96                    },
97                    4
98                );
99
100                // Add an Anti-spam menu item for Jetpack. This is handled automatically by the Admin_Menu as long as it has been initialized.
101                Admin_Menu::init();
102            }
103
104            // Render the unified Jetpack header/footer chrome on Akismet's admin pages by
105            // consuming Akismet's `akismet_header` / `akismet_footer` action hooks. This does
106            // not modify the Akismet plugin; it only registers callbacks on its own hooks.
107            require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-akismet-admin-chrome.php';
108            $akismet_admin_chrome = new Akismet_Admin_Chrome();
109            $akismet_admin_chrome->init_hooks();
110        }
111
112        // Ensure an Additional CSS menu item is added to the Appearance menu whenever Jetpack is connected.
113        add_action( 'admin_menu', array( $this, 'additional_css_menu' ) );
114
115        add_filter( 'jetpack_display_jitms_on_screen', array( $this, 'should_display_jitms_on_screen' ), 10, 2 );
116
117        // Register Jetpack partner coupon hooks.
118        Jetpack_Partner_Coupon::register_coupon_admin_hooks( 'jetpack', Jetpack::admin_url() );
119
120        // Remove default WordPress admin footer on Jetpack pages only.
121        add_filter( 'admin_footer_text', array( $this, 'maybe_remove_admin_footer_text' ) );
122        add_filter( 'update_footer', array( $this, 'maybe_remove_admin_footer_version' ), 11 );
123        add_filter( 'admin_body_class', array( $this, 'add_jetpack_admin_body_class' ) );
124        add_action( 'admin_head', array( $this, 'add_footer_removal_styles' ) );
125
126        // Make WPDS design tokens resolve at runtime on the legacy/wrap_ui Jetpack
127        // admin pages (Dashboard, Settings, Debugger) that don't ship their own
128        // `:root{--wpds-*}` source. Delegates to Admin_Menu's shared enqueue API.
129        add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_design_tokens' ) );
130    }
131
132    /**
133     * Handle our Additional CSS menu item and legacy page declaration.
134     *
135     * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php).
136     */
137    public static function additional_css_menu() {
138        /*
139         * Custom CSS for the Customizer is deprecated for block themes as of WP 6.1, so we only expose it with a menu
140         * if the site already has existing CSS code.
141         */
142        if ( wp_is_block_theme() ) {
143            $styles = wp_get_custom_css();
144            if ( ! $styles ) {
145                return;
146            }
147        }
148
149        // If the site is a WoA site and the custom-css feature is not available, return.
150        // See https://github.com/Automattic/jetpack/pull/19965 for more on how this menu item is dealt with on WoA sites.
151        if ( ( new Host() )->is_woa_site() && ! ( in_array( 'custom-css', Jetpack::get_available_modules(), true ) ) ) {
152            return;
153        } elseif (
154            class_exists( 'Jetpack' ) && (
155                Jetpack::is_module_active( 'custom-css' ) || // If the Custom CSS module is enabled, add the Additional CSS menu item and link to the Customizer.
156                ( wp_is_block_theme() && ! empty( wp_get_custom_css() ) ) // Do the same if the theme is block-based but has existing custom CSS.
157            )
158        ) {
159            // Add in our legacy page to support old bookmarks and such.
160            add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) );
161
162            // Add in our new page slug that will redirect to the customizer.
163            $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) );
164            add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) );
165        }
166    }
167
168    /**
169     * Handle the redirect for the customizer.  This is necessary because
170     * we can't directly add customizer links to the admin menu.
171     *
172     * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php).
173     *
174     * There is a core patch in trac that would make this unnecessary.
175     *
176     * @link https://core.trac.wordpress.org/ticket/39050
177     *
178     * @return never
179     */
180    public static function customizer_redirect() {
181        wp_safe_redirect(
182            self::customizer_link(
183                array(
184                    'return_url' => wp_get_referer(),
185                )
186            )
187        );
188        exit( 0 );
189    }
190
191    /**
192     * Build the URL to deep link to the Customizer.
193     *
194     * You can modify the return url via $args.
195     *
196     * @since 11.0 in this file. This method is also located in custom-css-4.7.php to cover legacy scenarios.
197     *
198     * @param array $args Array of parameters.
199     * @return string
200     */
201    public static function customizer_link( $args = array() ) {
202        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
203            $args = wp_parse_args(
204                $args,
205                array(
206                    'return_url' => rawurlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
207                )
208            );
209        }
210
211        return add_query_arg(
212            array(
213                array(
214                    'autofocus' => array(
215                        'section' => 'custom_css',
216                    ),
217                ),
218                'return' => $args['return_url'],
219            ),
220            admin_url( 'customize.php' )
221        );
222    }
223
224    /**
225     * Sort callback to put modules with `requires_connection` last.
226     *
227     * @param array $module1 Module data.
228     * @param array $module2 Module data.
229     * @return int Indicating the relative ordering of module1 and module2.
230     */
231    public static function sort_requires_connection_last( $module1, $module2 ) {
232        return ( (bool) $module1['requires_connection'] ) <=> ( (bool) $module2['requires_connection'] );
233    }
234
235    /**
236     * Produce JS understandable objects of modules containing information for
237     * presentation like description, name, configuration url, etc.
238     */
239    public function get_modules() {
240        include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php';
241        $available_modules = Jetpack::get_available_modules();
242        $active_modules    = Jetpack::get_active_modules();
243        $modules           = array();
244        $jetpack_active    = Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode();
245        $overrides         = Jetpack_Modules_Overrides::instance();
246        foreach ( $available_modules as $module ) {
247            $module_array = Jetpack::get_module( $module );
248            if ( $module_array ) {
249                /**
250                 * Filters each module's short description.
251                 *
252                 * @since 3.0.0
253                 *
254                 * @param string $module_array['description'] Module description.
255                 * @param string $module Module slug.
256                 */
257                $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
258                // Fix: correct multibyte strings truncate with checking for mbstring extension.
259                $short_desc_trunc = ( function_exists( 'mb_strlen' ) )
260                            ? ( ( mb_strlen( $short_desc ) > 143 )
261                                ? mb_substr( $short_desc, 0, 140 ) . '...'
262                                : $short_desc )
263                            : ( ( strlen( $short_desc ) > 143 )
264                                ? substr( $short_desc, 0, 140 ) . '...'
265                                : $short_desc );
266
267                $module_array['module'] = $module;
268
269                $is_available = self::is_module_available( $module_array );
270
271                $module_array['activated']          = ( $jetpack_active ? in_array( $module, $active_modules, true ) : false );
272                $module_array['deactivate_nonce']   = wp_create_nonce( 'jetpack_deactivate-' . $module );
273                $module_array['activate_nonce']     = wp_create_nonce( 'jetpack_activate-' . $module );
274                $module_array['available']          = $is_available;
275                $module_array['unavailable_reason'] = $is_available ? false : self::get_module_unavailable_reason( $module_array );
276                $module_array['short_description']  = $short_desc_trunc;
277                $module_array['configure_url']      = Jetpack::module_configuration_url( $module );
278                $module_array['override']           = $overrides->get_module_override( $module );
279                $module_array['disabled']           = $is_available ? '' : 'disabled="disabled"';
280
281                ob_start();
282                /**
283                 * Allow the display of a "Learn More" button.
284                 * The dynamic part of the action, $module, is the module slug.
285                 *
286                 * @since 3.0.0
287                 */
288                do_action( 'jetpack_learn_more_button_' . $module );
289                $module_array['learn_more_button'] = ob_get_clean();
290
291                ob_start();
292                /**
293                 * Allow the display of information text when Jetpack is connected to WordPress.com.
294                 * The dynamic part of the action, $module, is the module slug.
295                 *
296                 * @since 3.0.0
297                 */
298                do_action( 'jetpack_module_more_info_' . $module );
299
300                /**
301                * Filter the long description of a module.
302                *
303                * @since 3.5.0
304                *
305                * @param string ob_get_clean() The module long description.
306                * @param string $module The module name.
307                */
308                $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
309
310                ob_start();
311                /**
312                 * Filter the search terms for a module
313                 *
314                 * Search terms are typically added to the module headers, under "Additional Search Queries".
315                 *
316                 * Use syntax:
317                 * function jetpack_$module_search_terms( $terms ) {
318                 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
319                 *  return $terms;
320                 * }
321                 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
322                 *
323                 * @since 3.5.0
324                 *
325                 * @param string The search terms (comma-separated).
326                 */
327                echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
328                $module_array['search_terms'] = ob_get_clean();
329
330                $module_array['configurable'] = false;
331                if (
332                    current_user_can( 'manage_options' ) &&
333                    /**
334                     * Allow the display of a configuration link in the Jetpack Settings screen.
335                     *
336                     * @since 3.0.0
337                     *
338                     * @param string $module Module name.
339                     * @param bool false Should the Configure module link be displayed? Default to false.
340                     */
341                    apply_filters( 'jetpack_module_configurable_' . $module, false )
342                ) {
343                    $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
344                }
345
346                $modules[ $module ] = $module_array;
347            }
348        }
349
350        uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
351
352        if ( ! Jetpack::is_connection_ready() ) {
353            uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
354        }
355
356        return $modules;
357    }
358
359    /**
360     * Check if a module is available.
361     *
362     * @param array $module Module data.
363     */
364    public static function is_module_available( $module ) {
365        if ( ! is_array( $module ) || empty( $module ) ) {
366            return false;
367        }
368
369        /**
370         * We never want to show VaultPress as activatable through Jetpack.
371         */
372        if ( 'vaultpress' === $module['module'] ) {
373            return false;
374        }
375
376        /*
377         * WooCommerce Analytics should only be available
378         * when running WooCommerce 3+
379         */
380        if (
381            'woocommerce-analytics' === $module['module']
382            && (
383                ! class_exists( 'WooCommerce' )
384                || version_compare( WC_VERSION, '3.0', '<' )
385            )
386        ) {
387            return false;
388        }
389
390        /*
391         * In Offline mode, modules that require a site or user
392         * level connection should be unavailable.
393         */
394        if ( ( new Status() )->is_offline_mode() ) {
395            return ! ( $module['requires_connection'] || $module['requires_user_connection'] );
396        }
397
398        /*
399         * Jetpack not connected.
400         */
401        if ( ! Jetpack::is_connection_ready() ) {
402            return false;
403        }
404
405        /*
406         * Jetpack connected at a site level only. Make sure to make
407         * modules that require a user connection unavailable.
408         */
409        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
410            return false;
411        }
412
413        return Jetpack_Plan::supports( $module['module'] );
414    }
415
416    /**
417     * Returns why a module is unavailable.
418     *
419     * @param  array $module The module.
420     * @return string|false A string stating why the module is not available or false if the module is available.
421     */
422    public static function get_module_unavailable_reason( $module ) {
423        if ( ! is_array( $module ) || empty( $module ) ) {
424            return false;
425        }
426
427        if ( self::is_module_available( $module ) ) {
428            return false;
429        }
430
431        /**
432         * We never want to show VaultPress as activatable through Jetpack so return an empty string.
433         */
434        if ( 'vaultpress' === $module['module'] ) {
435            return '';
436        }
437
438        /*
439         * WooCommerce Analytics should only be available
440         * when running WooCommerce 3+
441         */
442        if (
443            'woocommerce-analytics' === $module['module']
444            && (
445                    ! class_exists( 'WooCommerce' )
446                    || version_compare( WC_VERSION, '3.0', '<' )
447                )
448            ) {
449            return __( 'Requires WooCommerce 3+ plugin', 'jetpack' );
450        }
451
452        /*
453         * In Offline mode, modules that require a site or user
454         * level connection should be unavailable.
455         */
456        if ( ( new Status() )->is_offline_mode() ) {
457            if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
458                return __( 'Offline mode', 'jetpack' );
459            }
460        }
461
462        /*
463         * Jetpack not connected.
464         */
465        if ( ! Jetpack::is_connection_ready() ) {
466            return __( 'Jetpack is not connected', 'jetpack' );
467        }
468
469        /*
470         * Jetpack connected at a site level only and module requires a user connection.
471         */
472        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
473            return __( 'Requires a connected WordPress.com account', 'jetpack' );
474        }
475
476        /*
477         * Plan restrictions.
478         */
479        if ( ! Jetpack_Plan::supports( $module['module'] ) ) {
480            return __( 'Not supported by current plan', 'jetpack' );
481        }
482
483        return '';
484    }
485
486    /**
487     * Handle an unrecognized action.
488     *
489     * @param string $action Action.
490     */
491    public function handle_unrecognized_action( $action ) {
492        switch ( $action ) {
493            case 'bulk-activate':
494                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
495                if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
496                    break;
497                }
498
499                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
500                foreach ( $modules as $module ) {
501                    Jetpack::log( 'activate', $module );
502                    Jetpack::activate_module( $module, false );
503                }
504                // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
505                wp_safe_redirect( wp_get_referer() );
506                exit( 0 );
507            case 'bulk-deactivate':
508                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
509                if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
510                    break;
511                }
512
513                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
514                foreach ( $modules as $module ) {
515                    Jetpack::log( 'deactivate', $module );
516                    Jetpack::deactivate_module( $module );
517                    Jetpack::state( 'message', 'module_deactivated' );
518                }
519                Jetpack::state( 'module', $modules );
520                wp_safe_redirect( wp_get_referer() );
521                exit( 0 );
522            default:
523                return;
524        }
525    }
526
527    /**
528     * Fix redirect.
529     *
530     * Apparently we redirect to the referrer instead of whatever WordPress
531     * wants to redirect to when activating and deactivating modules.
532     *
533     * @param string $module Module slug.
534     * @param bool   $redirect Should we exit after the module has been activated. Default to true.
535     */
536    public function fix_redirect( $module, $redirect = true ) {
537        if ( ! $redirect ) {
538            return;
539        }
540        if ( wp_get_referer() ) {
541            add_filter( 'wp_redirect', 'wp_get_referer' );
542        }
543    }
544
545    /**
546     * Add debugger admin menu.
547     */
548    public function admin_menu_debugger() {
549        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
550        Jetpack_Debugger::disconnect_and_redirect();
551        $debugger_hook = add_submenu_page(
552            '',
553            __( 'Debugging Center', 'jetpack' ),
554            '',
555            'manage_options',
556            'jetpack-debugger',
557            array( $this, 'wrap_debugger_page' )
558        );
559        add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
560    }
561
562    /**
563     * Wrap debugger page.
564     */
565    public function wrap_debugger_page() {
566        nocache_headers();
567        if ( ! current_user_can( 'manage_options' ) ) {
568            die( '-1' );
569        }
570        Jetpack_Admin_Page::wrap_ui(
571            array( $this, 'debugger_page' ),
572            array(
573                'is-wide'  => true,
574                'show-nav' => false,
575            )
576        );
577    }
578
579    /**
580     * Display debugger page.
581     */
582    public function debugger_page() {
583        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
584        Jetpack_Debugger::jetpack_debug_display_handler();
585    }
586
587    /**
588     * Determines if JITMs should display on a particular screen.
589     *
590     * @param bool   $value The default value of the filter.
591     * @param string $screen_id The ID of the screen being tested for JITM display.
592     *
593     * @return bool True if JITMs should display, false otherwise.
594     */
595    public function should_display_jitms_on_screen( $value, $screen_id ) {
596        // Disable all JITMs on these pages.
597        if (
598        in_array(
599            $screen_id,
600            array(
601                'jetpack_page_akismet-key-config',
602                'admin_page_jetpack_modules',
603            ),
604            true
605        ) ) {
606            return false;
607        }
608
609        return $value;
610    }
611
612    /**
613     * Check if we're on a Jetpack admin page.
614     *
615     * Similar to how WooCommerce checks for its admin pages by comparing
616     * against known screen ID patterns.
617     *
618     * @return bool True if on a Jetpack admin page, false otherwise.
619     */
620    private function is_jetpack_admin_page() {
621        $screen = get_current_screen();
622        if ( ! $screen ) {
623            return false;
624        }
625
626        // Check for Jetpack admin pages:
627        // - toplevel_page_jetpack (main Jetpack menu page)
628        // - toplevel_page_jetpack-network (Jetpack Network Admin menu page)
629        // - jetpack_page_* (Jetpack submenu pages)
630        // - admin_page_jetpack* (legacy/special Jetpack pages)
631        // Or check if parent_base is 'jetpack' or 'jetpack-network' (submenu pages)
632        return (
633        $screen->id === 'toplevel_page_jetpack' ||
634        $screen->id === 'toplevel_page_jetpack-network' ||
635        str_starts_with( $screen->id, 'jetpack_page_' ) ||
636        str_starts_with( $screen->id, 'admin_page_jetpack' ) ||
637        $screen->parent_base === 'jetpack' ||
638        $screen->parent_base === 'jetpack-network'
639        );
640    }
641
642    /**
643     * Add a body class to Jetpack admin pages.
644     *
645     * @param string $classes Space-separated list of CSS classes.
646     * @return string Modified class list.
647     */
648    public function add_jetpack_admin_body_class( $classes ) {
649        if ( $this->is_jetpack_admin_page() ) {
650            return trim( $classes ) . ' jetpack-admin-page ';
651        }
652        return $classes;
653    }
654
655    /**
656     * Add inline styles to remove footer padding on Jetpack pages.
657     *
658     * This needs to be inline because jetpack-admin.css is not loaded on
659     * React-powered admin pages (they use load_wrapper_styles instead).
660     */
661    public function add_footer_removal_styles() {
662        if ( ! $this->is_jetpack_admin_page() ) {
663            return;
664        }
665        echo '<style>.jetpack-admin-page #wpbody-content { padding-bottom: 0; } .jetpack-admin-page #wpfooter { display: none; }</style>';
666    }
667
668    /**
669     * Enqueues the shared WPDS design-tokens stylesheet on the legacy/wrap_ui pages.
670     *
671     * This is the admin_enqueue_scripts callback for the legacy Jetpack admin
672     * pages. The admin-ui package owns the handle and enqueues it on the
673     * modernized dashboards it registers; the legacy/wrap_ui pages (Dashboard,
674     * Settings, Debugger) aren't registered through Admin_Menu, so we cover them
675     * here via the central is_jetpack_admin_page() gate. The actual enqueue is
676     * delegated to the reusable Admin_Menu::enqueue_design_tokens() API so there
677     * is a single owner of the handle and no duplicated register/enqueue logic.
678     *
679     * @return void
680     */
681    public function maybe_enqueue_design_tokens() {
682        if ( ! $this->is_jetpack_admin_page() ) {
683            return;
684        }
685
686        // Guard against an older admin-ui being loaded ahead of this one by the
687        // package autoloader's version-precedence resolution.
688        if ( ! method_exists( Admin_Menu::class, 'enqueue_design_tokens' ) ) {
689            return;
690        }
691
692        Admin_Menu::enqueue_design_tokens();
693    }
694
695    /**
696     * Remove the admin footer text on Jetpack pages.
697     *
698     * @param string $content The default footer text.
699     * @return string Empty string on Jetpack pages, original content otherwise.
700     */
701    public function maybe_remove_admin_footer_text( $content ) {
702        return $this->is_jetpack_admin_page() ? '' : $content;
703    }
704
705    /**
706     * Remove the admin footer version on Jetpack pages.
707     *
708     * @param string $content The default footer version text.
709     * @return string Empty string on Jetpack pages, original content otherwise.
710     */
711    public function maybe_remove_admin_footer_version( $content ) {
712        return $this->is_jetpack_admin_page() ? '' : $content;
713    }
714}
715Jetpack_Admin::init();