Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.55% covered (danger)
20.55%
67 / 326
7.69% covered (danger)
7.69%
2 / 26
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initializer
20.55% covered (danger)
20.55%
67 / 326
7.69% covered (danger)
7.69%
2 / 26
3453.90
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
6
 is_licensing_ui_enabled
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 add_my_jetpack_menu_item
52.63% covered (warning)
52.63%
10 / 19
0.00% covered (danger)
0.00%
0 / 1
2.43
 admin_init
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
90
 add_onboarding_admin_body_class
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 can_use_analytics
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 1
42
 get_installed_jetpack_plugins
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 get_active_modules
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 is_jetpack_user_new
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
 get_my_jetpack_flags
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 admin_page
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 register_rest_endpoints
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 permissions_callback
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 should_initialize
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 setup_historically_active_jetpack_modules_sync
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 get_site
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 get_site_info
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 is_commercial_site
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 is_registered
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 dismiss_welcome_banner
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 has_file_system_write_access
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
 get_idc_container_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_show_red_bubble
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
110
 enqueue_red_bubble_script
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 get_recommended_modules
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * WP Admin page with information and configuration shared among all Jetpack stand-alone plugins
4 *
5 * @package automattic/my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack;
9
10use Automattic\Jetpack\Admin_UI\Admin_Menu;
11use Automattic\Jetpack\Agents_Manager\WP_REST_Jetpack_AI_JWT;
12use Automattic\Jetpack\Assets;
13use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
14use Automattic\Jetpack\Boost_Speed_Score\Speed_Score_History;
15use Automattic\Jetpack\Connection\Client;
16use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
17use Automattic\Jetpack\Connection\Manager as Connection_Manager;
18use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
19use Automattic\Jetpack\Constants as Jetpack_Constants;
20use Automattic\Jetpack\ExPlat;
21use Automattic\Jetpack\JITMS\JITM;
22use Automattic\Jetpack\Licensing;
23use Automattic\Jetpack\Modules;
24use Automattic\Jetpack\Plugins_Installer;
25use Automattic\Jetpack\Status;
26use Automattic\Jetpack\Status\Host as Status_Host;
27use Automattic\Jetpack\Sync\Functions as Sync_Functions;
28use Automattic\Jetpack\Terms_Of_Service;
29use Automattic\Jetpack\Tracking;
30use Jetpack;
31use WP_Error;
32
33/**
34 * The main Initializer class that registers the admin menu and eneuque the assets.
35 */
36class Initializer {
37
38    /**
39     * My Jetpack package version
40     *
41     * @var string
42     */
43    const PACKAGE_VERSION = '5.38.1';
44
45    /**
46     * HTML container ID for the IDC screen on My Jetpack page.
47     */
48    private const IDC_CONTAINER_ID = 'my-jetpack-identity-crisis-container';
49
50    public const JETPACK_PLUGIN_SLUGS = array(
51        'jetpack-backup',
52        'jetpack-boost',
53        'zerobscrm',
54        'jetpack',
55        'jetpack-protect',
56        'jetpack-social',
57        'jetpack-videopress',
58        'jetpack-search',
59    );
60
61    private const MY_JETPACK_SITE_INFO_TRANSIENT_KEY = 'my-jetpack-site-info';
62
63    /**
64     * Holds info/data about the site (from the /sites/%d endpoint)
65     *
66     * @var object
67     */
68    public static $site_info;
69
70    /**
71     * Initialize My Jetpack
72     *
73     * @return void
74     */
75    public static function init() {
76        if ( ( new Status() )->is_offline_mode() ) {
77            if ( false === has_action( 'admin_menu', array( __CLASS__, 'add_my_jetpack_menu_item' ) ) ) {
78                add_action( 'admin_menu', array( __CLASS__, 'add_my_jetpack_menu_item' ) );
79            }
80
81            return;
82        }
83
84        if ( ! self::should_initialize() || did_action( 'my_jetpack_init' ) ) {
85            return;
86        }
87
88        // Extend jetpack plugins action links.
89        Products::extend_plugins_action_links();
90
91        // Set up the REST authentication hooks.
92        Connection_Rest_Authentication::init();
93
94        if ( self::is_licensing_ui_enabled() ) {
95            Licensing::instance()->initialize();
96        }
97
98        // Initialize Boost Speed Score
99        new Speed_Score( array(), 'jetpack-my-jetpack' );
100
101        // Add custom WP REST API endoints.
102        add_action( 'rest_api_init', array( __CLASS__, 'register_rest_endpoints' ) );
103
104        add_action( 'admin_menu', array( __CLASS__, 'add_my_jetpack_menu_item' ) );
105
106        add_action( 'admin_init', array( __CLASS__, 'setup_historically_active_jetpack_modules_sync' ) );
107        // This is later than the admin-ui package, which runs on 1000
108        add_action( 'admin_init', array( __CLASS__, 'maybe_show_red_bubble' ), 1001 );
109
110        // Set up the ExPlat package endpoints
111        ExPlat::init();
112
113        // Sets up JITMS.
114        JITM::configure();
115
116        // Add "Jetpack Manage" menu item.
117        Jetpack_Manage::init();
118
119        /**
120         * Fires after the My Jetpack package is initialized
121         *
122         * @since 0.1.0
123         */
124        do_action( 'my_jetpack_init' );
125    }
126
127    /**
128     * Acts as a feature flag, returning a boolean for whether we should show the licensing UI.
129     *
130     * @since 1.2.0
131     *
132     * @return boolean
133     */
134    public static function is_licensing_ui_enabled() {
135        // Default changed to true in 1.5.0.
136        $is_enabled = true;
137
138        /*
139         * Bail if My Jetpack is not enabled,
140         * and thus the licensing UI shouldn't be enabled either.
141         */
142        if ( ! self::should_initialize() ) {
143            $is_enabled = false;
144        }
145
146        /**
147         * Acts as a feature flag, returning a boolean for whether we should show the licensing UI.
148         *
149         * @param bool $is_enabled Defaults to true.
150         *
151         * @since 1.2.0
152         * @since 1.5.0 Update default value to true.
153         */
154        return apply_filters(
155            'jetpack_my_jetpack_should_enable_add_license_screen',
156            $is_enabled
157        );
158    }
159
160    /**
161     * Add My Jetpack menu item to the admin menu.
162     *
163     * @return void
164     */
165    public static function add_my_jetpack_menu_item() {
166        if ( ( new Status() )->is_offline_mode() ) {
167            Admin_Menu::add_menu(
168                __( 'Offline Mode', 'jetpack-my-jetpack' ),
169                __( 'Offline Mode', 'jetpack-my-jetpack' ),
170                'manage_options',
171                admin_url( 'admin.php?page=jetpack#/offline-mode' ),
172                null,
173                -1
174            );
175
176            return;
177        }
178
179        $page_suffix = Admin_Menu::add_menu(
180            __( 'My Jetpack', 'jetpack-my-jetpack' ),
181            __( 'My Jetpack', 'jetpack-my-jetpack' ),
182            'edit_posts',
183            'my-jetpack',
184            array( __CLASS__, 'admin_page' ),
185            -1
186        );
187        add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );
188    }
189
190    /**
191     * Callback for the load my jetpack page hook.
192     *
193     * @return void
194     */
195    public static function admin_init() {
196        $connection = new Connection_Manager();
197
198        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No nonce needed for redirect flow control
199        $step = isset( $_GET['step'] ) ? sanitize_text_field( wp_unslash( $_GET['step'] ) ) : '';
200
201        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checking for partner coupon redemption flow
202        $show_coupon_redemption = isset( $_GET['showCouponRedemption'] );
203
204        // Redirect to Jetpack dashboard for partner coupon redemption
205        if ( $show_coupon_redemption ) {
206            wp_safe_redirect( admin_url( 'admin.php?page=jetpack&showCouponRedemption=1#/dashboard' ) );
207            exit( 0 );
208        }
209
210        // Handle onboarding redirects based on connection status
211        $should_redirect = false;
212        $redirect_args   = array( 'page' => 'my-jetpack' );
213
214        if ( ! $connection->is_connected() && $step !== 'onboarding' ) {
215            // Redirect to onboarding if not connected
216            $redirect_args['step'] = 'onboarding';
217            $should_redirect       = true;
218        } elseif ( $connection->is_connected() && $step === 'onboarding' ) {
219            // Redirect away from onboarding if already connected
220            $should_redirect = true;
221        }
222
223        if ( $should_redirect ) {
224            $admin_page = add_query_arg( $redirect_args, admin_url( 'admin.php' ) );
225            $location   = wp_sanitize_redirect( $admin_page );
226
227            // Remove wp_get_referer filter applied in `fix_redirect` method of `Jetpack_Admin` class
228            remove_filter( 'wp_redirect', 'wp_get_referer' );
229            wp_safe_redirect( $location );
230
231            exit( 0 );
232        }
233
234        // If the user reaches the onboarding page, add a class to the body
235        if ( $step === 'onboarding' ) {
236            add_filter( 'admin_body_class', array( __CLASS__, 'add_onboarding_admin_body_class' ) );
237        }
238
239        self::$site_info = self::get_site_info();
240        add_filter( 'identity_crisis_container_id', array( static::class, 'get_idc_container_id' ) );
241        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
242    }
243
244    /**
245     * Add a body class to the My Jetpack onboarding page.
246     * This class hides the WP Admin toolbar and the sidebar menu.
247     *
248     * @param string $classes The body classes.
249     * @return string The modified body classes.
250     */
251    public static function add_onboarding_admin_body_class( $classes ) {
252        $classes .= 'jetpack-admin-full-screen';
253        return $classes;
254    }
255
256    /**
257     * Returns whether we are in condition to track to use
258     * Analytics functionality like Tracks, MC, or GA.
259     */
260    public static function can_use_analytics() {
261        $status     = new Status();
262        $connection = new Connection_Manager();
263        $tracking   = new Tracking( 'jetpack', $connection );
264
265        return $tracking->should_enable_tracking( new Terms_Of_Service(), $status );
266    }
267
268    /**
269     * Enqueue admin page assets.
270     *
271     * @return void
272     */
273    public static function enqueue_scripts() {
274        /**
275         * Fires after the My Jetpack page is initialized.
276         * Allows for enqueuing additional scripts only on the My Jetpack page.
277         *
278         * @since 4.35.7
279         */
280        do_action( 'myjetpack_enqueue_scripts' );
281        Assets::register_script(
282            'my_jetpack_main_app',
283            '../build/index.js',
284            __FILE__,
285            array(
286                'enqueue'    => true,
287                'in_footer'  => true,
288                'textdomain' => 'jetpack-my-jetpack',
289            )
290        );
291        $modules             = new Modules();
292        $connection          = new Connection_Manager();
293        $speed_score_history = new Speed_Score_History( get_site_url() );
294        $latest_score        = $speed_score_history->latest();
295        $previous_score      = array();
296        if ( $speed_score_history->count() > 1 ) {
297            $previous_score = $speed_score_history->latest( 1 );
298        }
299        $latest_score['previousScores'] = $previous_score['scores'] ?? array();
300
301        $sandboxed_domain = '';
302        $is_dev_version   = false;
303        if ( class_exists( 'Jetpack' ) ) {
304            $is_dev_version   = Jetpack::is_development_version();
305            $sandboxed_domain = defined( 'JETPACK__SANDBOX_DOMAIN' ) ? JETPACK__SANDBOX_DOMAIN : '';
306        }
307
308        wp_localize_script(
309            'my_jetpack_main_app',
310            'myJetpackInitialState',
311            array(
312                'products'               => array(
313                    'items' => Products::get_products(),
314                ),
315                'plugins'                => Plugins_Installer::get_plugins(),
316                'themes'                 => Sync_Functions::get_themes(),
317                'myJetpackUrl'           => admin_url( 'admin.php?page=my-jetpack' ),
318                'myJetpackCheckoutUri'   => admin_url( 'admin.php?page=my-jetpack' ),
319                'topJetpackMenuItemUrl'  => Admin_Menu::get_top_level_menu_item_url(),
320                'siteSuffix'             => ( new Status() )->get_site_suffix(),
321                'siteUrl'                => esc_url( get_site_url() ),
322                'blogID'                 => Connection_Manager::get_site_id( true ),
323                'myJetpackVersion'       => self::PACKAGE_VERSION,
324                'myJetpackFlags'         => self::get_my_jetpack_flags(),
325                'fileSystemWriteAccess'  => self::has_file_system_write_access(),
326                'loadAddLicenseScreen'   => self::is_licensing_ui_enabled(),
327                'adminUrl'               => esc_url( admin_url() ),
328                'IDCContainerID'         => static::get_idc_container_id(),
329                'userIsAdmin'            => current_user_can( 'manage_options' ),
330                'lifecycleStats'         => array(
331                    'jetpackPlugins'            => self::get_installed_jetpack_plugins(),
332                    'historicallyActiveModules' => \Jetpack_Options::get_option( 'historically_active_modules', array() ),
333                    'brokenModules'             => Red_Bubble_Notifications::check_for_broken_modules(),
334                    'isSiteConnected'           => $connection->is_connected(),
335                    'isUserConnected'           => $connection->is_user_connected(),
336                    'modules'                   => self::get_active_modules(),
337                ),
338                'recommendedModules'     => array(
339                    'modules'    => self::get_recommended_modules(),
340                    'isFirstRun' => \Jetpack_Options::get_option( 'recommendations_first_run', true ),
341                    'dismissed'  => \Jetpack_Options::get_option( 'dismissed_recommendations', false ),
342                ),
343                'isStatsModuleActive'    => $modules->is_active( 'stats' ),
344                'canUserViewStats'       => current_user_can( 'manage_options' ) || current_user_can( 'view_stats' ),
345                'sandboxedDomain'        => $sandboxed_domain,
346                'isDevVersion'           => $is_dev_version,
347                'isAtomic'               => ( new Status_Host() )->is_woa_site(),
348                'isJetpackPluginActive'  => class_exists( 'Jetpack' ),
349                'latestBoostSpeedScores' => $latest_score,
350            )
351        );
352
353        wp_localize_script(
354            'my_jetpack_main_app',
355            'myJetpackRest',
356            array(
357                'apiRoot'  => esc_url_raw( rest_url() ),
358                'apiNonce' => wp_create_nonce( 'wp_rest' ),
359            )
360        );
361
362        // Connection Initial State.
363        Connection_Initial_State::render_script( 'my_jetpack_main_app' );
364
365        // Required for Analytics.
366        if ( self::can_use_analytics() ) {
367            Tracking::register_tracks_functions_scripts( true );
368        }
369    }
370
371    /**
372     * Get installed Jetpack plugins
373     *
374     * @return array
375     */
376    public static function get_installed_jetpack_plugins() {
377        $plugin_slugs = array_keys( Plugins_Installer::get_plugins() );
378        $plugin_slugs = array_map(
379            static function ( $slug ) {
380                $parts = explode( '/', $slug );
381                // Return the last segment of the filepath without the PHP extension
382                return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] );
383            },
384            $plugin_slugs
385        );
386
387        return array_values( array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs ) );
388    }
389
390    /**
391     * Get active modules (except ones enabled by default)
392     *
393     * @return array
394     */
395    public static function get_active_modules() {
396        $modules        = new Modules();
397        $active_modules = $modules->get_active();
398
399        // if the Jetpack plugin is active, filter out the modules that are active by default
400        if ( class_exists( 'Jetpack' ) && ! empty( $active_modules ) ) {
401            $active_modules = array_diff( $active_modules, Jetpack::get_default_modules() );
402        }
403        return array_values( $active_modules );
404    }
405
406    /**
407     * Determine if the current user is "new" to Jetpack
408     * This is used to vary some messaging in My Jetpack
409     *
410     * On the front-end, purchases are also taken into account
411     *
412     * @return bool
413     */
414    public static function is_jetpack_user_new() {
415        // is the user connected?
416        $connection = new Connection_Manager();
417        if ( $connection->is_user_connected() ) {
418            return false;
419        }
420
421        // TODO: add a data point for the last known connection/ disconnection time
422
423        // are any modules active?
424        $active_modules = self::get_active_modules();
425        if ( ! empty( $active_modules ) ) {
426            return false;
427        }
428
429        // check for other Jetpack plugins that are installed on the site (active or not)
430        // If there's more than one Jetpack plugin active, this user is not "new"
431        $plugin_slugs              = array_keys( Plugins_Installer::get_plugins() );
432        $plugin_slugs              = array_map(
433            static function ( $slug ) {
434                $parts = explode( '/', $slug );
435                // Return the last segment of the filepath without the PHP extension
436                return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] );
437            },
438            $plugin_slugs
439        );
440        $installed_jetpack_plugins = array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs );
441        if ( is_countable( $installed_jetpack_plugins ) && count( $installed_jetpack_plugins ) >= 2 ) {
442            return false;
443        }
444
445        // Does the site have any purchases?
446        $purchases = Wpcom_Products::get_site_current_purchases();
447        if ( ! empty( $purchases ) && ! is_wp_error( $purchases ) ) {
448            return false;
449        }
450
451        return true;
452    }
453
454    /**
455     *  Build flags for My Jetpack UI
456     *
457     *  @return array
458     */
459    public static function get_my_jetpack_flags() {
460        $flags = array(
461            'videoPressStats'          => Jetpack_Constants::is_true( 'JETPACK_MY_JETPACK_VIDEOPRESS_STATS_ENABLED' ),
462            'showFullJetpackStatsCard' => class_exists( 'Jetpack' ),
463        );
464
465        return $flags;
466    }
467
468    /**
469     * Echoes the admin page content.
470     *
471     * @return void
472     */
473    public static function admin_page() {
474        $step          = isset( $_GET['step'] ) ? sanitize_text_field( wp_unslash( $_GET['step'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
475        $is_onboarding = $step === 'onboarding';
476
477        // Add data attribute for onboarding, otherwise render normal container
478        echo '<div id="my-jetpack-container" ' . ( $is_onboarding ? 'data-route="onboarding"' : '' ) . '></div>';
479    }
480
481    /**
482     * Register the REST API routes.
483     *
484     * @return void
485     */
486    public static function register_rest_endpoints() {
487        new REST_Products();
488        new REST_Purchases();
489        new REST_Zendesk_Chat();
490        ( new WP_REST_Jetpack_AI_JWT() )->register_rest_route();
491        new REST_Recommendations_Evaluation();
492
493        Products::register_product_endpoints();
494        Historically_Active_Modules::register_rest_endpoints();
495        Jetpack_Manage::register_rest_endpoints();
496        Red_Bubble_Notifications::register_rest_endpoints();
497
498        register_rest_route(
499            'my-jetpack/v1',
500            'site',
501            array(
502                'methods'             => \WP_REST_Server::READABLE,
503                'callback'            => __CLASS__ . '::get_site',
504                'permission_callback' => __CLASS__ . '::permissions_callback',
505            )
506        );
507
508        register_rest_route(
509            'my-jetpack/v1',
510            'site/dismiss-welcome-banner',
511            array(
512                'methods'             => \WP_REST_Server::EDITABLE,
513                'callback'            => __CLASS__ . '::dismiss_welcome_banner',
514                'permission_callback' => __CLASS__ . '::permissions_callback',
515            )
516        );
517    }
518
519    /**
520     * Check user capability to access the endpoint.
521     *
522     * @access public
523     * @static
524     *
525     * @return true|WP_Error
526     */
527    public static function permissions_callback() {
528        return current_user_can( 'manage_options' );
529    }
530
531    /**
532     * Return true if we should initialize the My Jetpack admin page.
533     */
534    public static function should_initialize() {
535        $should = true;
536
537        // All options presented in My Jetpack require a connection to WordPress.com.
538        if ( ( new Status() )->is_offline_mode() ) {
539            $should = false;
540        }
541
542        /**
543         * Allows filtering whether My Jetpack should be initialized.
544         *
545         * @since 0.5.0-alpha
546         *
547         * @param bool $shoud_initialize Should we initialize My Jetpack?
548         */
549        return apply_filters( 'jetpack_my_jetpack_should_initialize', $should );
550    }
551
552    /**
553     * Hook into several connection-based actions to update the historically active Jetpack modules
554     * If the transient that indicates the list needs to be synced, update it and delete the transient
555     *
556     * @return void
557     */
558    public static function setup_historically_active_jetpack_modules_sync() {
559        // yummmm. ham.
560        $ham = new Historically_Active_Modules();
561        if ( get_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY ) && ! wp_doing_ajax() ) {
562            $ham::update_historically_active_jetpack_modules();
563            delete_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY );
564        }
565
566        $actions = array(
567            'jetpack_site_registered',
568            'jetpack_user_authorized',
569            'activated_plugin',
570        );
571
572        foreach ( $actions as $action ) {
573            add_action( $action, array( $ham, 'queue_historically_active_jetpack_modules_update' ), 5 );
574        }
575
576        // Modules are often updated async, so we need to update them right away as there will sometimes be no page reload.
577        add_action( 'jetpack_activate_module', array( $ham, 'update_historically_active_jetpack_modules' ), 5 );
578    }
579
580    /**
581     * Site full-data endpoint.
582     *
583     * @return object Site data.
584     */
585    public static function get_site() {
586        $site_id           = \Jetpack_Options::get_option( 'id' );
587        $wpcom_endpoint    = sprintf( '/sites/%d?force=wpcom', $site_id );
588        $wpcom_api_version = '1.1';
589        $response          = Client::wpcom_json_api_request_as_blog( $wpcom_endpoint, $wpcom_api_version );
590        $response_code     = wp_remote_retrieve_response_code( $response );
591        $body              = json_decode( wp_remote_retrieve_body( $response ) );
592
593        if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
594            return new WP_Error( 'site_data_fetch_failed', 'Site data fetch failed', array( 'status' => $response_code ) );
595        }
596
597        return rest_ensure_response( $body );
598    }
599
600    /**
601     * Populates the self::$site_info var with site data from the /sites/%d endpoint
602     *
603     * @return object|WP_Error
604     */
605    public static function get_site_info() {
606        static $site_info = null;
607
608        if ( $site_info !== null ) {
609            return $site_info;
610        }
611
612        // Check for a cached value before doing lookup
613        $stored_site_info = get_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY );
614        if ( $stored_site_info !== false ) {
615            return $stored_site_info;
616        }
617
618        $response = self::get_site();
619        if ( is_wp_error( $response ) ) {
620            return $response;
621        }
622        $site_info = $response->data;
623        set_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY, $site_info, DAY_IN_SECONDS );
624
625        return $site_info;
626    }
627
628    /**
629     * Returns whether a site has been determined "commercial" or not.
630     *
631     * @return bool|null
632     */
633    public static function is_commercial_site() {
634        if ( is_wp_error( self::$site_info ) ) {
635            return null;
636        }
637
638        return empty( self::$site_info->options->is_commercial ) ? false : self::$site_info->options->is_commercial;
639    }
640
641    /**
642     * Check if site is registered (has been connected before).
643     *
644     * @return bool
645     */
646    public static function is_registered() {
647        return (bool) \Jetpack_Options::get_option( 'id' );
648    }
649
650    /**
651     * Dismiss the welcome banner.
652     *
653     * @return \WP_REST_Response
654     */
655    public static function dismiss_welcome_banner() {
656        \Jetpack_Options::update_option( 'dismissed_welcome_banner', true );
657        return rest_ensure_response( array( 'success' => true ) );
658    }
659
660    /**
661     * Returns "yes" if the site has file write access to the plugins folder, "no" otherwise.
662     *
663     * @return string
664     **/
665    public static function has_file_system_write_access() {
666
667        $cache = get_transient( 'my_jetpack_write_access' );
668
669        if ( false !== $cache ) {
670            return $cache;
671        }
672
673        if ( ! function_exists( 'get_filesystem_method' ) ) {
674            require_once ABSPATH . 'wp-admin/includes/file.php';
675        }
676
677        require_once ABSPATH . 'wp-admin/includes/template.php';
678
679        $write_access = 'no';
680
681        $filesystem_method = get_filesystem_method( array(), WP_PLUGIN_DIR );
682        if ( 'direct' === $filesystem_method ) {
683            $write_access = 'yes';
684        }
685
686        if ( 'no' === $write_access ) {
687            ob_start();
688            $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
689            ob_end_clean();
690
691            if ( $filesystem_credentials_are_stored ) {
692                $write_access = 'yes';
693            }
694        }
695
696        set_transient( 'my_jetpack_write_access', $write_access, 30 * MINUTE_IN_SECONDS );
697
698        return $write_access;
699    }
700
701    /**
702     * Get container IDC for the IDC screen.
703     *
704     * @return string
705     */
706    public static function get_idc_container_id() {
707        return static::IDC_CONTAINER_ID;
708    }
709
710    /**
711     * Conditionally append the red bubble notification to the "Jetpack" menu item if there are alerts to show.
712     *
713     * On My Jetpack page: Uses blocking behavior to fetch fresh data.
714     * On other admin pages: Uses cached data only to avoid blocking, with async JS fetch if cache is empty.
715     *
716     * @return void
717     */
718    public static function maybe_show_red_bubble() {
719        global $menu, $pagenow;
720
721        // Don't show red bubble alerts for non-admin users
722        // These alerts are generally only actionable for admins
723        if ( ! current_user_can( 'manage_options' ) ) {
724            return;
725        }
726
727        // Don't show any red bubbles when Jetpack is disconnected
728        // Users can't act on most alerts without a connection
729        $connection = new Connection_Manager();
730        if ( ! $connection->is_connected() ) {
731            return;
732        }
733
734        // Check if we're on the My Jetpack page.
735        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
736        $page               = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
737        $is_my_jetpack_page = $pagenow === 'admin.php' && $page === 'my-jetpack';
738
739        if ( $is_my_jetpack_page ) {
740            // On My Jetpack page: use blocking behavior for fresh data.
741            add_filter( 'my_jetpack_red_bubble_notification_slugs', array( Red_Bubble_Notifications::class, 'add_red_bubble_alerts' ) );
742            $red_bubble_alerts = Red_Bubble_Notifications::get_red_bubble_alerts();
743        } else {
744            // On other pages: use cached data only to avoid blocking.
745            $cached_alerts = Red_Bubble_Notifications::get_cached_alerts();
746
747            if ( false === $cached_alerts ) {
748                // No cache - fetch asynchronously via JS.
749                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_red_bubble_script' ) );
750                return;
751            }
752
753            $red_bubble_alerts = $cached_alerts;
754        }
755
756        // Filter out silent alerts.
757        $red_bubble_alerts = array_filter(
758            $red_bubble_alerts,
759            function ( $alert ) {
760                return empty( $alert['is_silent'] );
761            }
762        );
763
764        // The Jetpack menu item should be on index 3
765        if (
766            ! empty( $red_bubble_alerts ) &&
767            isset( $menu[3] ) &&
768            $menu[3][0] === 'Jetpack'
769        ) {
770            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
771            $menu[3][0] .= sprintf( ' <span class="awaiting-mod">%d</span>', count( $red_bubble_alerts ) );
772        }
773    }
774
775    /**
776     * Enqueue the notification bubble script.
777     * Fetches fresh alert data via REST API without blocking page load.
778     *
779     * @return void
780     */
781    public static function enqueue_red_bubble_script() {
782        Assets::register_script(
783            'my-jetpack-notification-bubble',
784            '../build/async-notification-bubble.js',
785            __FILE__,
786            array(
787                'enqueue'   => true,
788                'in_footer' => true,
789            )
790        );
791    }
792
793    /**
794     * Get list of module names sorted by their recommendation score
795     *
796     * @return array|null
797     */
798    public static function get_recommended_modules() {
799        $recommendations_evaluation = \Jetpack_Options::get_option( 'recommendations_evaluation', null );
800
801        if ( ! $recommendations_evaluation ) {
802            return null;
803        }
804
805        arsort( $recommendations_evaluation ); // Sort by scores in descending order
806
807        return array_keys( $recommendations_evaluation ); // Get only module names
808    }
809}