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