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