Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
64.48% covered (warning)
64.48%
167 / 259
31.82% covered (danger)
31.82%
7 / 22
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Admin
64.45% covered (warning)
64.45%
165 / 256
31.82% covered (danger)
31.82%
7 / 22
559.19
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
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
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            add_submenu_page( '', __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss', array( __CLASS__, 'customizer_redirect' ) );
159
160            // Add in our new page slug that will redirect to the customizer.
161            $hook = add_theme_page( __( 'CSS', 'jetpack' ), __( 'Additional CSS', 'jetpack' ), 'edit_theme_options', 'editcss-customizer-redirect', array( __CLASS__, 'customizer_redirect' ) );
162            add_action( "load-{$hook}", array( __CLASS__, 'customizer_redirect' ) );
163        }
164    }
165
166    /**
167     * Handle the redirect for the customizer.  This is necessary because
168     * we can't directly add customizer links to the admin menu.
169     *
170     * @since 11.0 . Prior to that, this function was located in custom-css-4.7.php (now custom-css.php).
171     *
172     * There is a core patch in trac that would make this unnecessary.
173     *
174     * @link https://core.trac.wordpress.org/ticket/39050
175     *
176     * @return never
177     */
178    public static function customizer_redirect() {
179        wp_safe_redirect(
180            self::customizer_link(
181                array(
182                    'return_url' => wp_get_referer(),
183                )
184            )
185        );
186        exit( 0 );
187    }
188
189    /**
190     * Build the URL to deep link to the Customizer.
191     *
192     * You can modify the return url via $args.
193     *
194     * @since 11.0 in this file. This method is also located in custom-css-4.7.php to cover legacy scenarios.
195     *
196     * @param array $args Array of parameters.
197     * @return string
198     */
199    public static function customizer_link( $args = array() ) {
200        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
201            $args = wp_parse_args(
202                $args,
203                array(
204                    'return_url' => rawurlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
205                )
206            );
207        }
208
209        return add_query_arg(
210            array(
211                array(
212                    'autofocus' => array(
213                        'section' => 'custom_css',
214                    ),
215                ),
216                'return' => $args['return_url'],
217            ),
218            admin_url( 'customize.php' )
219        );
220    }
221
222    /**
223     * Sort callback to put modules with `requires_connection` last.
224     *
225     * @param array $module1 Module data.
226     * @param array $module2 Module data.
227     * @return int Indicating the relative ordering of module1 and module2.
228     */
229    public static function sort_requires_connection_last( $module1, $module2 ) {
230        return ( (bool) $module1['requires_connection'] ) <=> ( (bool) $module2['requires_connection'] );
231    }
232
233    /**
234     * Produce JS understandable objects of modules containing information for
235     * presentation like description, name, configuration url, etc.
236     */
237    public function get_modules() {
238        include_once JETPACK__PLUGIN_DIR . 'modules/module-info.php';
239        $available_modules = Jetpack::get_available_modules();
240        $active_modules    = Jetpack::get_active_modules();
241        $modules           = array();
242        $jetpack_active    = Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode();
243        $overrides         = Jetpack_Modules_Overrides::instance();
244        $protect_page_urls = self::get_protect_page_module_urls();
245        foreach ( $available_modules as $module ) {
246            $module_array = Jetpack::get_module( $module );
247            if ( $module_array ) {
248                /**
249                 * Filters each module's short description.
250                 *
251                 * @since 3.0.0
252                 *
253                 * @param string $module_array['description'] Module description.
254                 * @param string $module Module slug.
255                 */
256                $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module );
257                // Fix: correct multibyte strings truncate with checking for mbstring extension.
258                $short_desc_trunc = ( function_exists( 'mb_strlen' ) )
259                            ? ( ( mb_strlen( $short_desc ) > 143 )
260                                ? mb_substr( $short_desc, 0, 140 ) . '...'
261                                : $short_desc )
262                            : ( ( strlen( $short_desc ) > 143 )
263                                ? substr( $short_desc, 0, 140 ) . '...'
264                                : $short_desc );
265
266                $module_array['module'] = $module;
267
268                $is_available = self::is_module_available( $module_array );
269
270                $module_array['activated']          = ( $jetpack_active ? in_array( $module, $active_modules, true ) : false );
271                $module_array['deactivate_nonce']   = wp_create_nonce( 'jetpack_deactivate-' . $module );
272                $module_array['activate_nonce']     = wp_create_nonce( 'jetpack_activate-' . $module );
273                $module_array['available']          = $is_available;
274                $module_array['unavailable_reason'] = $is_available ? false : self::get_module_unavailable_reason( $module_array );
275                $module_array['short_description']  = $short_desc_trunc;
276                $module_array['configure_url']      = Jetpack::module_configuration_url( $module );
277                $module_array['override']           = $overrides->get_module_override( $module );
278                $module_array['disabled']           = $is_available ? '' : 'disabled="disabled"';
279                $module_array['deactivation_note']  = Jetpack::get_module_deactivation_note( $module );
280
281                ob_start();
282                /**
283                 * Allow the display of a "Learn More" button.
284                 * The dynamic part of the action, $module, is the module slug.
285                 *
286                 * @since 3.0.0
287                 */
288                do_action( 'jetpack_learn_more_button_' . $module );
289                $module_array['learn_more_button'] = ob_get_clean();
290
291                ob_start();
292                /**
293                 * Allow the display of information text when Jetpack is connected to WordPress.com.
294                 * The dynamic part of the action, $module, is the module slug.
295                 *
296                 * @since 3.0.0
297                 */
298                do_action( 'jetpack_module_more_info_' . $module );
299
300                /**
301                * Filter the long description of a module.
302                *
303                * @since 3.5.0
304                *
305                * @param string ob_get_clean() The module long description.
306                * @param string $module The module name.
307                */
308                $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module );
309
310                ob_start();
311                /**
312                 * Filter the search terms for a module
313                 *
314                 * Search terms are typically added to the module headers, under "Additional Search Queries".
315                 *
316                 * Use syntax:
317                 * function jetpack_$module_search_terms( $terms ) {
318                 *  $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' );
319                 *  return $terms;
320                 * }
321                 * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' );
322                 *
323                 * @since 3.5.0
324                 *
325                 * @param string The search terms (comma-separated).
326                 */
327                echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
328                $module_array['search_terms'] = ob_get_clean();
329
330                $module_array['configurable'] = false;
331                if (
332                    current_user_can( 'manage_options' ) &&
333                    /**
334                     * Allow the display of a configuration link in the Jetpack Settings screen.
335                     *
336                     * @since 3.0.0
337                     *
338                     * @param string $module Module name.
339                     * @param bool false Should the Configure module link be displayed? Default to false.
340                     */
341                    apply_filters( 'jetpack_module_configurable_' . $module, false )
342                ) {
343                    $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $module_array['configure_url'] ), __( 'Configure', 'jetpack' ) );
344                }
345
346                // The Protect page's link replaces both the switch and Configure.
347                $module_array['protect_page_url'] = $protect_page_urls[ $module ] ?? '';
348                if ( $module_array['protect_page_url'] ) {
349                    $module_array['configurable'] = false;
350                }
351
352                $modules[ $module ] = $module_array;
353            }
354        }
355
356        uasort( $modules, array( 'Jetpack', 'sort_modules' ) );
357
358        if ( ! Jetpack::is_connection_ready() ) {
359            uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) );
360        }
361
362        return $modules;
363    }
364
365    /**
366     * Check if a module is available.
367     *
368     * @param array $module Module data.
369     */
370    public static function is_module_available( $module ) {
371        if ( ! is_array( $module ) || empty( $module ) ) {
372            return false;
373        }
374
375        /**
376         * We never want to show VaultPress as activatable through Jetpack.
377         */
378        if ( 'vaultpress' === $module['module'] ) {
379            return false;
380        }
381
382        /*
383         * WooCommerce Analytics should only be available
384         * when running WooCommerce 3+
385         */
386        if (
387            'woocommerce-analytics' === $module['module']
388            && (
389                ! class_exists( 'WooCommerce' )
390                || version_compare( WC_VERSION, '3.0', '<' )
391            )
392        ) {
393            return false;
394        }
395
396        /*
397         * In Offline mode, modules that require a site or user
398         * level connection should be unavailable.
399         */
400        if ( ( new Status() )->is_offline_mode() ) {
401            return ! ( $module['requires_connection'] || $module['requires_user_connection'] );
402        }
403
404        /*
405         * Jetpack not connected.
406         */
407        if ( ! Jetpack::is_connection_ready() ) {
408            return false;
409        }
410
411        /*
412         * Jetpack connected at a site level only. Make sure to make
413         * modules that require a user connection unavailable.
414         */
415        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
416            return false;
417        }
418
419        return Jetpack_Plan::supports( $module['module'] );
420    }
421
422    /**
423     * Returns why a module is unavailable.
424     *
425     * @param  array $module The module.
426     * @return string|false A string stating why the module is not available or false if the module is available.
427     */
428    public static function get_module_unavailable_reason( $module ) {
429        if ( ! is_array( $module ) || empty( $module ) ) {
430            return false;
431        }
432
433        if ( self::is_module_available( $module ) ) {
434            return false;
435        }
436
437        /**
438         * We never want to show VaultPress as activatable through Jetpack so return an empty string.
439         */
440        if ( 'vaultpress' === $module['module'] ) {
441            return '';
442        }
443
444        /*
445         * WooCommerce Analytics should only be available
446         * when running WooCommerce 3+
447         */
448        if (
449            'woocommerce-analytics' === $module['module']
450            && (
451                    ! class_exists( 'WooCommerce' )
452                    || version_compare( WC_VERSION, '3.0', '<' )
453                )
454            ) {
455            return __( 'Requires WooCommerce 3+ plugin', 'jetpack' );
456        }
457
458        /*
459         * In Offline mode, modules that require a site or user
460         * level connection should be unavailable.
461         */
462        if ( ( new Status() )->is_offline_mode() ) {
463            if ( $module['requires_connection'] || $module['requires_user_connection'] ) {
464                return __( 'Unavailable in Offline Mode', 'jetpack' );
465            }
466        }
467
468        /*
469         * Jetpack not connected.
470         */
471        if ( ! Jetpack::is_connection_ready() ) {
472            return __( 'Jetpack is not connected', 'jetpack' );
473        }
474
475        /*
476         * Jetpack connected at a site level only and module requires a user connection.
477         */
478        if ( ! Jetpack::connection()->has_connected_owner() && $module['requires_user_connection'] ) {
479            return __( 'Requires a connected WordPress.com account', 'jetpack' );
480        }
481
482        /*
483         * Plan restrictions.
484         */
485        if ( ! Jetpack_Plan::supports( $module['module'] ) ) {
486            return __( 'Not supported by current plan', 'jetpack' );
487        }
488
489        return '';
490    }
491
492    /**
493     * Where each security module's settings live while the Protect page owns them, keyed by module slug.
494     *
495     * @since 16.4
496     *
497     * @return array<string, string> Empty while the Protect page is off.
498     */
499    public static function get_protect_page_module_urls() {
500        // method_exists, not class_exists: another plugin's older copy of the package can win the autoloader.
501        if ( ! did_action( 'jetpack_protect_dashboard_initialized' ) || ! method_exists( Protect_Dashboard::class, 'get_module_settings_urls' ) ) {
502            return array();
503        }
504
505        return Protect_Dashboard::get_module_settings_urls();
506    }
507
508    /**
509     * Handle an unrecognized action.
510     *
511     * @param string $action Action.
512     */
513    public function handle_unrecognized_action( $action ) {
514        switch ( $action ) {
515            case 'bulk-activate':
516                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
517                if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
518                    break;
519                }
520
521                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
522                $modules = array_diff( $modules, array_keys( self::get_protect_page_module_urls() ) );
523                foreach ( $modules as $module ) {
524                    Jetpack::log( 'activate', $module );
525                    Jetpack::activate_module( $module, false );
526                }
527                // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
528                wp_safe_redirect( wp_get_referer() );
529                exit( 0 );
530            case 'bulk-deactivate':
531                check_admin_referer( 'bulk-jetpack_page_jetpack_modules' );
532                if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
533                    break;
534                }
535
536                $modules = isset( $_GET['modules'] ) ? array_map( 'sanitize_key', wp_unslash( (array) $_GET['modules'] ) ) : array();
537                $modules = array_diff( $modules, array_keys( self::get_protect_page_module_urls() ) );
538                foreach ( $modules as $module ) {
539                    Jetpack::log( 'deactivate', $module );
540                    Jetpack::deactivate_module( $module );
541                    Jetpack::state( 'message', 'module_deactivated' );
542                }
543                Jetpack::state( 'module', $modules );
544                wp_safe_redirect( wp_get_referer() );
545                exit( 0 );
546            default:
547                return;
548        }
549    }
550
551    /**
552     * Fix redirect.
553     *
554     * Apparently we redirect to the referrer instead of whatever WordPress
555     * wants to redirect to when activating and deactivating modules.
556     *
557     * @param string $module Module slug.
558     * @param bool   $redirect Should we exit after the module has been activated. Default to true.
559     */
560    public function fix_redirect( $module, $redirect = true ) {
561        if ( ! $redirect ) {
562            return;
563        }
564        if ( wp_get_referer() ) {
565            add_filter( 'wp_redirect', 'wp_get_referer' );
566        }
567    }
568
569    /**
570     * Add debugger admin menu.
571     */
572    public function admin_menu_debugger() {
573        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
574        Jetpack_Debugger::disconnect_and_redirect();
575        $debugger_hook = add_submenu_page(
576            '',
577            __( 'Debugging Center', 'jetpack' ),
578            '',
579            'manage_options',
580            'jetpack-debugger',
581            array( $this, 'wrap_debugger_page' )
582        );
583        add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) );
584    }
585
586    /**
587     * Wrap debugger page.
588     */
589    public function wrap_debugger_page() {
590        nocache_headers();
591        if ( ! current_user_can( 'manage_options' ) ) {
592            die( '-1' );
593        }
594        Jetpack_Admin_Page::wrap_ui(
595            array( $this, 'debugger_page' ),
596            array(
597                'is-wide'  => true,
598                'show-nav' => false,
599            )
600        );
601    }
602
603    /**
604     * Display debugger page.
605     */
606    public function debugger_page() {
607        require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
608        Jetpack_Debugger::jetpack_debug_display_handler();
609    }
610
611    /**
612     * Determines if JITMs should display on a particular screen.
613     *
614     * @param bool   $value The default value of the filter.
615     * @param string $screen_id The ID of the screen being tested for JITM display.
616     *
617     * @return bool True if JITMs should display, false otherwise.
618     */
619    public function should_display_jitms_on_screen( $value, $screen_id ) {
620        // Disable all JITMs on these pages.
621        if (
622        in_array(
623            $screen_id,
624            array(
625                'jetpack_page_akismet-key-config',
626                'admin_page_jetpack_modules',
627            ),
628            true
629        ) ) {
630            return false;
631        }
632
633        return $value;
634    }
635
636    /**
637     * Check if we're on a Jetpack admin page.
638     *
639     * Similar to how WooCommerce checks for its admin pages by comparing
640     * against known screen ID patterns.
641     *
642     * @return bool True if on a Jetpack admin page, false otherwise.
643     */
644    private function is_jetpack_admin_page() {
645        $screen = get_current_screen();
646        if ( ! $screen ) {
647            return false;
648        }
649
650        // Check for Jetpack admin pages:
651        // - toplevel_page_jetpack (main Jetpack menu page)
652        // - toplevel_page_jetpack-network (Jetpack Network Admin menu page)
653        // - jetpack_page_* (Jetpack submenu pages)
654        // - admin_page_jetpack* (legacy/special Jetpack pages)
655        // Or check if parent_base is 'jetpack' or 'jetpack-network' (submenu pages)
656        return (
657        $screen->id === 'toplevel_page_jetpack' ||
658        $screen->id === 'toplevel_page_jetpack-network' ||
659        str_starts_with( $screen->id, 'jetpack_page_' ) ||
660        str_starts_with( $screen->id, 'admin_page_jetpack' ) ||
661        $screen->parent_base === 'jetpack' ||
662        $screen->parent_base === 'jetpack-network'
663        );
664    }
665
666    /**
667     * Add a body class to Jetpack admin pages.
668     *
669     * @param string $classes Space-separated list of CSS classes.
670     * @return string Modified class list.
671     */
672    public function add_jetpack_admin_body_class( $classes ) {
673        if ( $this->is_jetpack_admin_page() ) {
674            return ltrim( trim( $classes ) . ' jetpack-admin-page ' );
675        }
676        return $classes;
677    }
678
679    /**
680     * Add inline styles to remove footer padding on Jetpack pages.
681     *
682     * This needs to be inline because jetpack-admin.css is not loaded on
683     * React-powered admin pages (they use load_wrapper_styles instead).
684     */
685    public function add_footer_removal_styles() {
686        if ( ! $this->is_jetpack_admin_page() ) {
687            return;
688        }
689        echo '<style>.jetpack-admin-page #wpbody-content { padding-bottom: 0; } .jetpack-admin-page #wpfooter { display: none; }</style>';
690    }
691
692    /**
693     * Enqueues the shared WPDS design-tokens stylesheet on the legacy/wrap_ui pages.
694     *
695     * This is the admin_enqueue_scripts callback for the legacy Jetpack admin
696     * pages. The admin-ui package owns the handle and enqueues it on the
697     * modernized dashboards it registers; the legacy/wrap_ui pages (Dashboard,
698     * Settings, Debugger) aren't registered through Admin_Menu, so we cover them
699     * here via the central is_jetpack_admin_page() gate. The actual enqueue is
700     * delegated to the reusable Admin_Menu::enqueue_design_tokens() API so there
701     * is a single owner of the handle and no duplicated register/enqueue logic.
702     *
703     * @return void
704     */
705    public function maybe_enqueue_design_tokens() {
706        if ( ! $this->is_jetpack_admin_page() ) {
707            return;
708        }
709
710        // Guard against an older admin-ui being loaded ahead of this one by the
711        // package autoloader's version-precedence resolution.
712        if ( ! method_exists( Admin_Menu::class, 'enqueue_design_tokens' ) ) {
713            return;
714        }
715
716        Admin_Menu::enqueue_design_tokens();
717    }
718
719    /**
720     * Remove the admin footer text on Jetpack pages.
721     *
722     * @param string $content The default footer text.
723     * @return string Empty string on Jetpack pages, original content otherwise.
724     */
725    public function maybe_remove_admin_footer_text( $content ) {
726        return $this->is_jetpack_admin_page() ? '' : $content;
727    }
728
729    /**
730     * Remove the admin footer version on Jetpack pages.
731     *
732     * @param string $content The default footer version text.
733     * @return string Empty string on Jetpack pages, original content otherwise.
734     */
735    public function maybe_remove_admin_footer_version( $content ) {
736        return $this->is_jetpack_admin_page() ? '' : $content;
737    }
738}
739Jetpack_Admin::init();