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