Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.61% covered (danger)
16.61%
53 / 319
7.41% covered (danger)
7.41%
2 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
Initializer
16.61% covered (danger)
16.61%
53 / 319
7.41% covered (danger)
7.41%
2 / 27
3885.02
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 / 80
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
 get_seo_opt_in_state
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 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.40.4';
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                'seoOptIn'               => self::get_seo_opt_in_state(),
330            )
331        );
332
333        wp_localize_script(
334            'my_jetpack_main_app',
335            'myJetpackRest',
336            array(
337                'apiRoot'  => esc_url_raw( rest_url() ),
338                'apiNonce' => wp_create_nonce( 'wp_rest' ),
339            )
340        );
341
342        // Connection Initial State.
343        Connection_Initial_State::render_script( 'my_jetpack_main_app' );
344
345        // Required for Analytics.
346        if ( self::can_use_analytics() ) {
347            Tracking::register_tracks_functions_scripts( true );
348        }
349    }
350
351    /**
352     * Get installed Jetpack plugins
353     *
354     * @return array
355     */
356    public static function get_installed_jetpack_plugins() {
357        $plugin_slugs = array_keys( Plugins_Installer::get_plugins() );
358        $plugin_slugs = array_map(
359            static function ( $slug ) {
360                $parts = explode( '/', $slug );
361                // Return the last segment of the filepath without the PHP extension
362                return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] );
363            },
364            $plugin_slugs
365        );
366
367        return array_values( array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs ) );
368    }
369
370    /**
371     * Get active modules (except ones enabled by default)
372     *
373     * @return array
374     */
375    public static function get_active_modules() {
376        $modules        = new Modules();
377        $active_modules = $modules->get_active();
378
379        // if the Jetpack plugin is active, filter out the modules that are active by default
380        if ( class_exists( 'Jetpack' ) && ! empty( $active_modules ) ) {
381            $active_modules = array_diff( $active_modules, Jetpack::get_default_modules() );
382        }
383        return array_values( $active_modules );
384    }
385
386    /**
387     * Determine if the current user is "new" to Jetpack
388     * This is used to vary some messaging in My Jetpack
389     *
390     * On the front-end, purchases are also taken into account
391     *
392     * @return bool
393     */
394    public static function is_jetpack_user_new() {
395        // is the user connected?
396        $connection = new Connection_Manager();
397        if ( $connection->is_user_connected() ) {
398            return false;
399        }
400
401        // TODO: add a data point for the last known connection/ disconnection time
402
403        // are any modules active?
404        $active_modules = self::get_active_modules();
405        if ( ! empty( $active_modules ) ) {
406            return false;
407        }
408
409        // check for other Jetpack plugins that are installed on the site (active or not)
410        // If there's more than one Jetpack plugin active, this user is not "new"
411        $plugin_slugs              = array_keys( Plugins_Installer::get_plugins() );
412        $plugin_slugs              = array_map(
413            static function ( $slug ) {
414                $parts = explode( '/', $slug );
415                // Return the last segment of the filepath without the PHP extension
416                return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] );
417            },
418            $plugin_slugs
419        );
420        $installed_jetpack_plugins = array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs );
421        if ( is_countable( $installed_jetpack_plugins ) && count( $installed_jetpack_plugins ) >= 2 ) {
422            return false;
423        }
424
425        // Does the site have any purchases?
426        $purchases = Wpcom_Products::get_site_current_purchases();
427        if ( ! empty( $purchases ) && ! is_wp_error( $purchases ) ) {
428            return false;
429        }
430
431        return true;
432    }
433
434    /**
435     *  Build flags for My Jetpack UI
436     *
437     *  @return array
438     */
439    public static function get_my_jetpack_flags() {
440        $flags = array(
441            'videoPressStats'          => Jetpack_Constants::is_true( 'JETPACK_MY_JETPACK_VIDEOPRESS_STATS_ENABLED' ),
442            'showFullJetpackStatsCard' => class_exists( 'Jetpack' ),
443        );
444
445        return $flags;
446    }
447
448    /**
449     * Build the state the My Jetpack "try the new SEO experience" opt-in card hydrates from.
450     *
451     * The card invites an existing self-hosted install to switch over to the new Jetpack SEO
452     * dashboard (JETPACK-1700). Gating lives server-side, where the signals actually are. The
453     * card shows only when all of:
454     *
455     * - the new SEO product is available â€” the `rsm_jetpack_seo` feature filter is on (the SEO
456     *   package autoloads regardless, so `class_exists()` alone isn't enough; the filter is the
457     *   real availability switch and the same one the SEO package gates its own surface behind);
458     * - the site is self-hosted â€” WordPress.com (Simple + Atomic) decides its own SEO surface, so
459     *   the opt-in card is for self-hosted installs only;
460     * - the install hasn't opted in yet â€” `jetpack_seo_surface_visible` is still false. On wpcom
461     *   the SEO package's `is_seo_surface_visible()` short-circuits to `true`, so the
462     *   "not visible yet" check also doubles as the self-hosted guard, but we check the platform
463     *   explicitly for clarity.
464     *
465     * Referenced through `class_exists()` rather than a hard composer dependency: both packages
466     * ship inside the Jetpack plugin and the SEO surface is feature-flagged, so a guarded read of
467     * its public API keeps this from adding plumbing to consumers that don't load SEO.
468     *
469     * The on-success destination is computed by the opt-in endpoint itself; we only seed the card
470     * with the same admin URL so the button has a sensible fallback before the request resolves.
471     *
472     * @return array{showCard: bool, redirect: string}
473     */
474    public static function get_seo_opt_in_state() {
475        // Guard with method_exists rather than class_exists: an older bundled SEO package can ship
476        // the Initializer class without this method, and class_exists alone would still fatal.
477        $seo_initializer = 'Automattic\Jetpack\SEO\Initializer';
478        // @phan-suppress-next-line PhanUndeclaredClassReference -- optional SEO package, guarded by method_exists.
479        $show_card = method_exists( $seo_initializer, 'is_optin_available' ) && $seo_initializer::is_optin_available();
480
481        return array(
482            'showCard' => $show_card,
483            'redirect' => admin_url( 'admin.php?page=jetpack-seo' ),
484        );
485    }
486
487    /**
488     * Echoes the admin page content.
489     *
490     * @return void
491     */
492    public static function admin_page() {
493        $step          = isset( $_GET['step'] ) ? sanitize_text_field( wp_unslash( $_GET['step'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
494        $is_onboarding = $step === 'onboarding';
495
496        // Add data attribute for onboarding, otherwise render normal container
497        echo '<div id="my-jetpack-container" ' . ( $is_onboarding ? 'data-route="onboarding"' : '' ) . '></div>';
498    }
499
500    /**
501     * Register the REST API routes.
502     *
503     * @return void
504     */
505    public static function register_rest_endpoints() {
506        new REST_Products();
507        new REST_Purchases();
508        new REST_Zendesk_Chat();
509        ( new WP_REST_Jetpack_AI_JWT() )->register_rest_route();
510        new REST_Recommendations_Evaluation();
511
512        Products::register_product_endpoints();
513        Historically_Active_Modules::register_rest_endpoints();
514        Jetpack_Manage::register_rest_endpoints();
515        Red_Bubble_Notifications::register_rest_endpoints();
516
517        register_rest_route(
518            'my-jetpack/v1',
519            'site',
520            array(
521                'methods'             => \WP_REST_Server::READABLE,
522                'callback'            => __CLASS__ . '::get_site',
523                'permission_callback' => __CLASS__ . '::permissions_callback',
524            )
525        );
526
527        register_rest_route(
528            'my-jetpack/v1',
529            'site/dismiss-welcome-banner',
530            array(
531                'methods'             => \WP_REST_Server::EDITABLE,
532                'callback'            => __CLASS__ . '::dismiss_welcome_banner',
533                'permission_callback' => __CLASS__ . '::permissions_callback',
534            )
535        );
536    }
537
538    /**
539     * Check user capability to access the endpoint.
540     *
541     * @access public
542     * @static
543     *
544     * @return true|WP_Error
545     */
546    public static function permissions_callback() {
547        return current_user_can( 'manage_options' );
548    }
549
550    /**
551     * Return true if we should initialize the My Jetpack admin page.
552     */
553    public static function should_initialize() {
554        $should = true;
555
556        // All options presented in My Jetpack require a connection to WordPress.com.
557        if ( ( new Status() )->is_offline_mode() ) {
558            $should = false;
559        }
560
561        /**
562         * Allows filtering whether My Jetpack should be initialized.
563         *
564         * @since 0.5.0-alpha
565         *
566         * @param bool $shoud_initialize Should we initialize My Jetpack?
567         */
568        return apply_filters( 'jetpack_my_jetpack_should_initialize', $should );
569    }
570
571    /**
572     * Hook into several connection-based actions to update the historically active Jetpack modules
573     * If the transient that indicates the list needs to be synced, update it and delete the transient
574     *
575     * @return void
576     */
577    public static function setup_historically_active_jetpack_modules_sync() {
578        // yummmm. ham.
579        $ham = new Historically_Active_Modules();
580        if ( get_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY ) && ! wp_doing_ajax() ) {
581            $ham::update_historically_active_jetpack_modules();
582            delete_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY );
583        }
584
585        $actions = array(
586            'jetpack_site_registered',
587            'jetpack_user_authorized',
588            'activated_plugin',
589        );
590
591        foreach ( $actions as $action ) {
592            add_action( $action, array( $ham, 'queue_historically_active_jetpack_modules_update' ), 5 );
593        }
594
595        // Modules are often updated async, so we need to update them right away as there will sometimes be no page reload.
596        add_action( 'jetpack_activate_module', array( $ham, 'update_historically_active_jetpack_modules' ), 5 );
597    }
598
599    /**
600     * Site full-data endpoint.
601     *
602     * @return object Site data.
603     */
604    public static function get_site() {
605        $site_id           = \Jetpack_Options::get_option( 'id' );
606        $wpcom_endpoint    = sprintf( '/sites/%d?force=wpcom', $site_id );
607        $wpcom_api_version = '1.1';
608        $response          = Client::wpcom_json_api_request_as_blog( $wpcom_endpoint, $wpcom_api_version );
609        $response_code     = wp_remote_retrieve_response_code( $response );
610        $body              = json_decode( wp_remote_retrieve_body( $response ) );
611
612        if ( is_wp_error( $response ) || empty( $response['body'] ) ) {
613            return new WP_Error( 'site_data_fetch_failed', 'Site data fetch failed', array( 'status' => $response_code ) );
614        }
615
616        return rest_ensure_response( $body );
617    }
618
619    /**
620     * Populates the self::$site_info var with site data from the /sites/%d endpoint
621     *
622     * @return object|WP_Error
623     */
624    public static function get_site_info() {
625        static $site_info = null;
626
627        if ( $site_info !== null ) {
628            return $site_info;
629        }
630
631        // Check for a cached value before doing lookup
632        $stored_site_info = get_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY );
633        if ( $stored_site_info !== false ) {
634            return $stored_site_info;
635        }
636
637        $response = self::get_site();
638        if ( is_wp_error( $response ) ) {
639            return $response;
640        }
641        $site_info = $response->data;
642        set_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY, $site_info, DAY_IN_SECONDS );
643
644        return $site_info;
645    }
646
647    /**
648     * Returns whether a site has been determined "commercial" or not.
649     *
650     * @return bool|null
651     */
652    public static function is_commercial_site() {
653        if ( is_wp_error( self::$site_info ) ) {
654            return null;
655        }
656
657        return empty( self::$site_info->options->is_commercial ) ? false : self::$site_info->options->is_commercial;
658    }
659
660    /**
661     * Check if site is registered (has been connected before).
662     *
663     * @return bool
664     */
665    public static function is_registered() {
666        return (bool) \Jetpack_Options::get_option( 'id' );
667    }
668
669    /**
670     * Dismiss the welcome banner.
671     *
672     * @return \WP_REST_Response
673     */
674    public static function dismiss_welcome_banner() {
675        \Jetpack_Options::update_option( 'dismissed_welcome_banner', true );
676        return rest_ensure_response( array( 'success' => true ) );
677    }
678
679    /**
680     * Returns "yes" if the site has file write access to the plugins folder, "no" otherwise.
681     *
682     * @return string
683     **/
684    public static function has_file_system_write_access() {
685
686        $cache = get_transient( 'my_jetpack_write_access' );
687
688        if ( false !== $cache ) {
689            return $cache;
690        }
691
692        if ( ! function_exists( 'get_filesystem_method' ) ) {
693            require_once ABSPATH . 'wp-admin/includes/file.php';
694        }
695
696        require_once ABSPATH . 'wp-admin/includes/template.php';
697
698        $write_access = 'no';
699
700        $filesystem_method = get_filesystem_method( array(), WP_PLUGIN_DIR );
701        if ( 'direct' === $filesystem_method ) {
702            $write_access = 'yes';
703        }
704
705        if ( 'no' === $write_access ) {
706            ob_start();
707            $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
708            ob_end_clean();
709
710            if ( $filesystem_credentials_are_stored ) {
711                $write_access = 'yes';
712            }
713        }
714
715        set_transient( 'my_jetpack_write_access', $write_access, 30 * MINUTE_IN_SECONDS );
716
717        return $write_access;
718    }
719
720    /**
721     * Get container IDC for the IDC screen.
722     *
723     * @return string
724     */
725    public static function get_idc_container_id() {
726        return static::IDC_CONTAINER_ID;
727    }
728
729    /**
730     * Conditionally append the red bubble notification to the "Jetpack" menu item if there are alerts to show.
731     *
732     * On My Jetpack page: Uses blocking behavior to fetch fresh data.
733     * On other admin pages: Uses cached data only to avoid blocking, with async JS fetch if cache is empty.
734     *
735     * @return void
736     */
737    public static function maybe_show_red_bubble() {
738        global $menu, $pagenow;
739
740        // Don't show red bubble alerts for non-admin users
741        // These alerts are generally only actionable for admins
742        if ( ! current_user_can( 'manage_options' ) ) {
743            return;
744        }
745
746        // Don't show any red bubbles when Jetpack is disconnected
747        // Users can't act on most alerts without a connection
748        $connection = new Connection_Manager();
749        if ( ! $connection->is_connected() ) {
750            return;
751        }
752
753        // Check if we're on the My Jetpack page.
754        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
755        $page               = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
756        $is_my_jetpack_page = $pagenow === 'admin.php' && $page === 'my-jetpack';
757
758        if ( $is_my_jetpack_page ) {
759            // On My Jetpack page: use blocking behavior for fresh data.
760            add_filter( 'my_jetpack_red_bubble_notification_slugs', array( Red_Bubble_Notifications::class, 'add_red_bubble_alerts' ) );
761            $red_bubble_alerts = Red_Bubble_Notifications::get_red_bubble_alerts();
762        } else {
763            // On other pages: use cached data only to avoid blocking.
764            $cached_alerts = Red_Bubble_Notifications::get_cached_alerts();
765
766            if ( false === $cached_alerts ) {
767                // No cache - fetch asynchronously via JS.
768                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_red_bubble_script' ) );
769                return;
770            }
771
772            $red_bubble_alerts = $cached_alerts;
773        }
774
775        // Filter out silent alerts.
776        $red_bubble_alerts = array_filter(
777            $red_bubble_alerts,
778            function ( $alert ) {
779                return empty( $alert['is_silent'] );
780            }
781        );
782
783        // The Jetpack menu item should be on index 3
784        if (
785            ! empty( $red_bubble_alerts ) &&
786            isset( $menu[3] ) &&
787            $menu[3][0] === 'Jetpack'
788        ) {
789            // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
790            $menu[3][0] .= sprintf( ' <span class="awaiting-mod">%d</span>', count( $red_bubble_alerts ) );
791        }
792    }
793
794    /**
795     * Enqueue the notification bubble script.
796     * Fetches fresh alert data via REST API without blocking page load.
797     *
798     * @return void
799     */
800    public static function enqueue_red_bubble_script() {
801        Assets::register_script(
802            'my-jetpack-notification-bubble',
803            '../build/async-notification-bubble.js',
804            __FILE__,
805            array(
806                'enqueue'   => true,
807                'in_footer' => true,
808            )
809        );
810    }
811
812    /**
813     * Get list of module names sorted by their recommendation score
814     *
815     * @return array|null
816     */
817    public static function get_recommended_modules() {
818        $recommendations_evaluation = \Jetpack_Options::get_option( 'recommendations_evaluation', null );
819
820        if ( ! $recommendations_evaluation ) {
821            return null;
822        }
823
824        arsort( $recommendations_evaluation ); // Sort by scores in descending order
825
826        return array_keys( $recommendations_evaluation ); // Get only module names
827    }
828}