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