Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.20% covered (warning)
57.20%
151 / 264
31.82% covered (danger)
31.82%
7 / 22
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Admin
57.09% covered (warning)
57.09%
149 / 261
31.82% covered (danger)
31.82%
7 / 22
924.12
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.00% covered (success)
90.00%
45 / 50
0.00% covered (danger)
0.00%
0 / 1
14.20
 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
287                ob_start();
288                /**
289                 * Allow the display of a "Learn More" button.
290                 * The dynamic part of the action, $module, is the module slug.
291                 *
292                 * @since 3.0.0
293                 */
294                do_action( 'jetpack_learn_more_button_' . $module );
295                $module_array['learn_more_button'] = ob_get_clean();
296
297                ob_start();
298                /**
299                 * Allow the display of information text when Jetpack is connected to WordPress.com.
300                 * The dynamic part of the action, $module, is the module slug.
301                 *
302                 * @since 3.0.0
303                 */
304                do_action( 'jetpack_module_more_info_' . $module );
305
306                /**
307                * Filter the long description of a module.
308                *
309                * @since 3.5.0
310                *
311                * @param string ob_get_clean() The module long description.
312                * @param string $module The module name.
313                */
314                $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
315
316                ob_start();
317                /**
318                 * Filter the search terms for a module
319                 *
320                 * Search terms are typically added to the module headers, under "Additional Search Queries".
321                 *
322                 * Use syntax:
323                 * function jetpack_$module_search_terms( $terms ) {
324                 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
325                 *  return $terms;
326                 * }
327                 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
328                 *
329                 * @since 3.5.0
330                 *
331                 * @param string The search terms (comma-separated).
332                 */
333                echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
334                $module_array['search_terms'] = ob_get_clean();
335
336                $module_array['configurable'] = false;
337                if (
338                    current_user_can( 'manage_options' ) &&
339                    /**
340                     * Allow the display of a configuration link in the Jetpack Settings screen.
341                     *
342                     * @since 3.0.0
343                     *
344                     * @param string $module Module name.
345                     * @param bool false Should the Configure module link be displayed? Default to false.
346                     */
347                    apply_filters( 'jetpack_module_configurable_' . $module, false )
348                ) {
349                    $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
350                }
351
352                // The Protect page's link replaces both the switch and Configure.
353                $module_array['protect_page_url'] = $protect_page_urls[ $module ] ?? '';
354                if ( $module_array['protect_page_url'] ) {
355                    $module_array['configurable'] = false;
356                }
357
358                $modules[ $module ] = $module_array;
359            }
360        }
361
362        uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
363
364        if ( ! Jetpack::is_connection_ready() ) {
365            uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
366        }
367
368        return $modules;
369    }
370
371    /**
372     * Check if a module is available.
373     *
374     * @param array $module Module data.
375     */
376    public static function is_module_available( $module ) {
377        if ( ! is_array( $module ) || empty( $module ) ) {
378            return false;
379        }
380
381        /**
382         * We never want to show VaultPress as activatable through Jetpack.
383         */
384        if ( 'vaultpress' === $module['module'] ) {
385            return false;
386        }
387
388        /*
389         * WooCommerce Analytics should only be available
390         * when running WooCommerce 3+
391         */
392        if (
393            'woocommerce-analytics' === $module['module']
394            && (
395                ! class_exists( 'WooCommerce' )
396                || version_compare( WC_VERSION, '3.0', '<' )
397            )
398        ) {
399            return false;
400        }
401
402        /*
403         * In Offline mode, modules that require a site or user
404         * level connection should be unavailable.
405         */
406        if ( ( new Status() )->is_offline_mode() ) {
407            return ! ( $module['requires_connection'] || $module['requires_user_connection'] );
408        }
409
410        /*
411         * Jetpack not connected.
412         */
413        if ( ! Jetpack::is_connection_ready() ) {
414            return false;
415        }
416
417        /*
418         * Jetpack connected at a site level only. Make sure to make
419         * modules that require a user connection unavailable.
420         */
421        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
422            return false;
423        }
424
425        return Jetpack_Plan::supports( $module['module'] );
426    }
427
428    /**
429     * Returns why a module is unavailable.
430     *
431     * @param  array $module The module.
432     * @return string|false A string stating why the module is not available or false if the module is available.
433     */
434    public static function get_module_unavailable_reason( $module ) {
435        if ( ! is_array( $module ) || empty( $module ) ) {
436            return false;
437        }
438
439        if ( self::is_module_available( $module ) ) {
440            return false;
441        }
442
443        /**
444         * We never want to show VaultPress as activatable through Jetpack so return an empty string.
445         */
446        if ( 'vaultpress' === $module['module'] ) {
447            return '';
448        }
449
450        /*
451         * WooCommerce Analytics should only be available
452         * when running WooCommerce 3+
453         */
454        if (
455            'woocommerce-analytics' === $module['module']
456            && (
457                    ! class_exists( 'WooCommerce' )
458                    || version_compare( WC_VERSION, '3.0', '<' )
459                )
460            ) {
461            return __( 'Requires WooCommerce 3+ plugin', 'jetpack' );
462        }
463
464        /*
465         * In Offline mode, modules that require a site or user
466         * level connection should be unavailable.
467         */
468        if ( ( new Status() )->is_offline_mode() ) {
469            if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
470                return __( 'Unavailable in Offline mode', 'jetpack' );
471            }
472        }
473
474        /*
475         * Jetpack not connected.
476         */
477        if ( ! Jetpack::is_connection_ready() ) {
478            return __( 'Jetpack is not connected', 'jetpack' );
479        }
480
481        /*
482         * Jetpack connected at a site level only and module requires a user connection.
483         */
484        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
485            return __( 'Requires a connected WordPress.com account', 'jetpack' );
486        }
487
488        /*
489         * Plan restrictions.
490         */
491        if ( ! Jetpack_Plan::supports( $module['module'] ) ) {
492            return __( 'Not supported by current plan', 'jetpack' );
493        }
494
495        return '';
496    }
497
498    /**
499     * Where each security module's settings live while the Protect page owns them, keyed by module slug.
500     *
501     * @since $$next-version$$
502     *
503     * @return array<string, string> Empty while the Protect page is off.
504     */
505    public static function get_protect_page_module_urls() {
506        // method_exists, not class_exists: another plugin's older copy of the package can win the autoloader.
507        if ( ! did_action( 'jetpack_protect_dashboard_initialized' ) || ! method_exists( Protect_Dashboard::class, 'get_module_settings_urls' ) ) {
508            return array();
509        }
510
511        return Protect_Dashboard::get_module_settings_urls();
512    }
513
514    /**
515     * Handle an unrecognized action.
516     *
517     * @param string $action Action.
518     */
519    public function handle_unrecognized_action( $action ) {
520        switch ( $action ) {
521            case 'bulk-activate':
522                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
523                if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
524                    break;
525                }
526
527                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
528                $modules = array_diff( $modules, array_keys( self::get_protect_page_module_urls() ) );
529                foreach ( $modules as $module ) {
530                    Jetpack::log( 'activate', $module );
531                    Jetpack::activate_module( $module, false );
532                }
533                // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
534                wp_safe_redirect( wp_get_referer() );
535                exit( 0 );
536            case 'bulk-deactivate':
537                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
538                if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
539                    break;
540                }
541
542                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
543                $modules = array_diff( $modules, array_keys( self::get_protect_page_module_urls() ) );
544                foreach ( $modules as $module ) {
545                    Jetpack::log( 'deactivate', $module );
546                    Jetpack::deactivate_module( $module );
547                    Jetpack::state( 'message', 'module_deactivated' );
548                }
549                Jetpack::state( 'module', $modules );
550                wp_safe_redirect( wp_get_referer() );
551                exit( 0 );
552            default:
553                return;
554        }
555    }
556
557    /**
558     * Fix redirect.
559     *
560     * Apparently we redirect to the referrer instead of whatever WordPress
561     * wants to redirect to when activating and deactivating modules.
562     *
563     * @param string $module Module slug.
564     * @param bool   $redirect Should we exit after the module has been activated. Default to true.
565     */
566    public function fix_redirect( $module, $redirect = true ) {
567        if ( ! $redirect ) {
568            return;
569        }
570        if ( wp_get_referer() ) {
571            add_filter( 'wp_redirect', 'wp_get_referer' );
572        }
573    }
574
575    /**
576     * Add debugger admin menu.
577     */
578    public function admin_menu_debugger() {
579        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
580        Jetpack_Debugger::disconnect_and_redirect();
581        $debugger_hook = add_submenu_page(
582            '',
583            __( 'Debugging Center', 'jetpack' ),
584            '',
585            'manage_options',
586            'jetpack-debugger',
587            array( $this, 'wrap_debugger_page' )
588        );
589        add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
590    }
591
592    /**
593     * Wrap debugger page.
594     */
595    public function wrap_debugger_page() {
596        nocache_headers();
597        if ( ! current_user_can( 'manage_options' ) ) {
598            die( '-1' );
599        }
600        Jetpack_Admin_Page::wrap_ui(
601            array( $this, 'debugger_page' ),
602            array(
603                'is-wide'  => true,
604                'show-nav' => false,
605            )
606        );
607    }
608
609    /**
610     * Display debugger page.
611     */
612    public function debugger_page() {
613        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
614        Jetpack_Debugger::jetpack_debug_display_handler();
615    }
616
617    /**
618     * Determines if JITMs should display on a particular screen.
619     *
620     * @param bool   $value The default value of the filter.
621     * @param string $screen_id The ID of the screen being tested for JITM display.
622     *
623     * @return bool True if JITMs should display, false otherwise.
624     */
625    public function should_display_jitms_on_screen( $value, $screen_id ) {
626        // Disable all JITMs on these pages.
627        if (
628        in_array(
629            $screen_id,
630            array(
631                'jetpack_page_akismet-key-config',
632                'admin_page_jetpack_modules',
633            ),
634            true
635        ) ) {
636            return false;
637        }
638
639        return $value;
640    }
641
642    /**
643     * Check if we're on a Jetpack admin page.
644     *
645     * Similar to how WooCommerce checks for its admin pages by comparing
646     * against known screen ID patterns.
647     *
648     * @return bool True if on a Jetpack admin page, false otherwise.
649     */
650    private function is_jetpack_admin_page() {
651        $screen = get_current_screen();
652        if ( ! $screen ) {
653            return false;
654        }
655
656        // Check for Jetpack admin pages:
657        // - toplevel_page_jetpack (main Jetpack menu page)
658        // - toplevel_page_jetpack-network (Jetpack Network Admin menu page)
659        // - jetpack_page_* (Jetpack submenu pages)
660        // - admin_page_jetpack* (legacy/special Jetpack pages)
661        // Or check if parent_base is 'jetpack' or 'jetpack-network' (submenu pages)
662        return (
663        $screen->id === 'toplevel_page_jetpack' ||
664        $screen->id === 'toplevel_page_jetpack-network' ||
665        str_starts_with( $screen->id, 'jetpack_page_' ) ||
666        str_starts_with( $screen->id, 'admin_page_jetpack' ) ||
667        $screen->parent_base === 'jetpack' ||
668        $screen->parent_base === 'jetpack-network'
669        );
670    }
671
672    /**
673     * Add a body class to Jetpack admin pages.
674     *
675     * @param string $classes Space-separated list of CSS classes.
676     * @return string Modified class list.
677     */
678    public function add_jetpack_admin_body_class( $classes ) {
679        if ( $this->is_jetpack_admin_page() ) {
680            return ltrim( trim( $classes ) . ' jetpack-admin-page ' );
681        }
682        return $classes;
683    }
684
685    /**
686     * Add inline styles to remove footer padding on Jetpack pages.
687     *
688     * This needs to be inline because jetpack-admin.css is not loaded on
689     * React-powered admin pages (they use load_wrapper_styles instead).
690     */
691    public function add_footer_removal_styles() {
692        if ( ! $this->is_jetpack_admin_page() ) {
693            return;
694        }
695        echo '<style>.jetpack-admin-page #wpbody-content { padding-bottom: 0; } .jetpack-admin-page #wpfooter { display: none; }</style>';
696    }
697
698    /**
699     * Enqueues the shared WPDS design-tokens stylesheet on the legacy/wrap_ui pages.
700     *
701     * This is the admin_enqueue_scripts callback for the legacy Jetpack admin
702     * pages. The admin-ui package owns the handle and enqueues it on the
703     * modernized dashboards it registers; the legacy/wrap_ui pages (Dashboard,
704     * Settings, Debugger) aren't registered through Admin_Menu, so we cover them
705     * here via the central is_jetpack_admin_page() gate. The actual enqueue is
706     * delegated to the reusable Admin_Menu::enqueue_design_tokens() API so there
707     * is a single owner of the handle and no duplicated register/enqueue logic.
708     *
709     * @return void
710     */
711    public function maybe_enqueue_design_tokens() {
712        if ( ! $this->is_jetpack_admin_page() ) {
713            return;
714        }
715
716        // Guard against an older admin-ui being loaded ahead of this one by the
717        // package autoloader's version-precedence resolution.
718        if ( ! method_exists( Admin_Menu::class, 'enqueue_design_tokens' ) ) {
719            return;
720        }
721
722        Admin_Menu::enqueue_design_tokens();
723    }
724
725    /**
726     * Remove the admin footer text on Jetpack pages.
727     *
728     * @param string $content The default footer text.
729     * @return string Empty string on Jetpack pages, original content otherwise.
730     */
731    public function maybe_remove_admin_footer_text( $content ) {
732        return $this->is_jetpack_admin_page() ? '' : $content;
733    }
734
735    /**
736     * Remove the admin footer version on Jetpack pages.
737     *
738     * @param string $content The default footer version text.
739     * @return string Empty string on Jetpack pages, original content otherwise.
740     */
741    public function maybe_remove_admin_footer_version( $content ) {
742        return $this->is_jetpack_admin_page() ? '' : $content;
743    }
744}
745Jetpack_Admin::init();