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