Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.31% covered (danger)
19.31%
107 / 554
4.17% covered (danger)
4.17%
2 / 48
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Mu_Wpcom
19.35% covered (danger)
19.35%
107 / 553
4.17% covered (danger)
4.17%
2 / 48
21821.29
0.00% covered (danger)
0.00%
0 / 1
 init
91.18% covered (success)
91.18%
62 / 68
0.00% covered (danger)
0.00%
0 / 1
17.20
 load_wpcom_simple_jetpack_ai
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 schedule_translation_updates
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 maybe_update_translations
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 1
420
 clear_translation_destination
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
 get_all_active_locales
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 load_features
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 1
110
 load_wpcom_user_features
16.28% covered (danger)
16.28%
7 / 43
0.00% covered (danger)
0.00%
0 / 1
68.68
 load_wpcom_sites_features
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 load_etk_features_flags
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
 load_etk_features
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
90
 load_newspack_blocks
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 load_coming_soon
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
90
 load_launchpad
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_wpcom_rest_api_endpoints
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 load_jetpack_mu_wpcom_settings
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
 load_map_block_settings
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 load_newsletter_categories_settings
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 unbind_focusout_on_wp_admin_bar_menu_toggle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 should_disable_comment_experience
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
90
 jetpack_comments_enabled_for_blog
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 load_verbum_comments
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
42
 load_jetpack_comments_routes
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 load_verbum_comments_admin
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 load_verbum_moderate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 load_wpcom_backup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_wpcom_scan
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_wpcom_my_jetpack
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_admin_shell
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 load_monetize
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 load_wpcom_simple_odyssey_stats
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_premium_analytics_wordads_section
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_premium_analytics_videopress_widgets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_premium_analytics_stats_settings_fields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 load_custom_css
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 load_wpcom_random_redirect
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 load_social_links
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 set_wpcom_blog_id_script_data
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 enable_gutenberg_react_19_experiment
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
 enable_woocommerce_block_editor_unified_assets
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 has_react_19_incompatible_gutenberg
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 enable_extensible_site_editor_experiment
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 has_react_19_incompatible_extension
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 is_react_19_incompatible_version
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 add_jetpack_script_data_for_p2
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 log2logstash
71.43% covered (warning)
71.43%
15 / 21
0.00% covered (danger)
0.00%
0 / 1
9.49
 resolve_logstash_blog_id
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
14.79
 queue_logstash_http
42.11% covered (danger)
42.11%
8 / 19
0.00% covered (danger)
0.00%
0 / 1
7.10
1<?php
2/**
3 * Enhances your site with features powered by WordPress.com
4 * This package is intended for internal use on WordPress.com sites only (simple and Atomic).
5 * Internal PT Reference: p9dueE-6jY-p2
6 *
7 * @package automattic/jetpack-mu-wpcom
8 */
9
10namespace Automattic\Jetpack;
11
12define( 'WPCOM_ADMIN_BAR_UNIFICATION', true );
13/**
14 * Jetpack_Mu_Wpcom main class.
15 */
16class Jetpack_Mu_Wpcom {
17    const PACKAGE_VERSION = '6.11.0';
18    const PKG_DIR         = __DIR__ . '/../';
19    const BASE_DIR        = __DIR__ . '/';
20    const BASE_FILE       = __FILE__;
21
22    // Gutenberg plugin releases known to break with React 19.
23    const REACT_19_INCOMPATIBLE_GUTENBERG = array( '23.9.0' );
24
25    /*
26     * Themes (by template slug) and plugins (by basename) known to break with React 19,
27     * each mapped to the release that fixed it. Null means no fixed release exists yet.
28     */
29    const REACT_19_INCOMPATIBLE_THEMES  = array(
30        'divi'     => null,
31        'woodmart' => '8.6.1',
32    );
33    const REACT_19_INCOMPATIBLE_PLUGINS = array(
34        'wp-table-builder/wp-table-builder.php'          => null,
35        'ultimate-blocks/ultimate-blocks.php'            => '3.6.0',
36        'beehive-analytics/beehive-analytics.php'        => null,
37        'xspeed/xspeed.php'                              => null,
38        'classified-listing/classified-listing.php'      => null,
39        'advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce-free.php' => null,
40        'advanced-coupons-for-woocommerce/advanced-coupons-for-woocommerce.php' => null,
41        'sb-analytics/sb-analytics-pro.php'              => null,
42        'brave-popup-builder/index.php'                  => null,
43        'bravepopup-pro/index.php'                       => null,
44        'astra-sites/astra-sites.php'                    => null,
45        'llms-full-txt-generator/llms-txt-generator.php' => null,
46        'wp-post-author/aft-wp-post-author.php'          => null,
47        'adminify/adminify.php'                          => null,
48        'meetinghub/meetinghub.php'                      => null,
49        'mail-mint/mail-mint.php'                        => null,
50        'wp-letsencrypt-ssl-pro/wp-letsencrypt.php'      => null,
51        'astra-pro-sites/astra-pro-sites.php'            => null,
52        'echo-rewards/echo-rewards.php'                  => null,
53        'forminator/forminator.php'                      => null,
54        'iron-security/iron-security.php'                => null,
55        'stackable-ultimate-gutenberg-blocks/plugin.php' => null,
56    );
57
58    /**
59     * Initialize the class.
60     */
61    public static function init() {
62        if ( did_action( 'jetpack_mu_wpcom_initialized' ) ) {
63            return;
64        }
65
66        // Shared code for src/features.
67        require_once self::PKG_DIR . 'src/common/index.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
68        require_once __DIR__ . '/common/fatal-error-signature.php';
69        require_once __DIR__ . '/utils.php';
70
71        // Atomic only — Simple sites can't install plugins. The confirmation
72        // probe wires its `pre_option_active_plugins` filter at mu-plugin
73        // time, before WP loads active plugins.
74        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
75            require_once __DIR__ . '/features/plugin-conflicts-guardian/probe-confirm-bootstrap.php';
76        }
77
78        /*
79         * Feature flag overrides answer the jetpack-feature-flags resolution
80         * filter, and a flag can be checked well before plugins_loaded, so this
81         * is wired here at mu-plugin time rather than from load_features().
82         * Registering the filter reads nothing — the option is only touched when
83         * a flag is actually resolved.
84         *
85         * The overrides themselves are site-wide, but the screen that sets them
86         * is Automatticians-only internal tooling and gates itself accordingly.
87         */
88        require_once __DIR__ . '/features/wpcom-feature-flags/class-wpcom-feature-flags.php';
89        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Feature_Flags::init();
90        require_once __DIR__ . '/features/wpcom-feature-flags/class-wpcom-admin-shell-rollout.php';
91        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Admin_Shell_Rollout::init();
92
93        // Load features that don't need any special loading considerations.
94        add_action( 'plugins_loaded', array( __CLASS__, 'load_features' ) );
95
96        // Load features that only apply to WordPress.com-connected users.
97        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_user_features' ) );
98        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features' ) );
99
100        // Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
101        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_sites_features' ) );
102
103        // Load ETK features flag to turn off the features in the ETK plugin.
104        // It needs higher priority than the ETK plugin.
105        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features_flags' ), 0 );
106
107        /*
108         * Please double-check whether you really need to load your feature separately.
109         * Chances are you can just add it to the `load_features` method.
110         */
111        add_action( 'plugins_loaded', array( __CLASS__, 'load_launchpad' ), 0 );
112        add_action( 'plugins_loaded', array( __CLASS__, 'load_coming_soon' ) );
113        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_rest_api_endpoints' ) );
114        add_action( 'plugins_loaded', array( __CLASS__, 'load_newspack_blocks' ) );
115
116        // Jetpack builds cut before Jetpack Comments went on by default only load it when this returns true. Remove once Atomic is past them.
117        add_filter( 'jetpack_comments_new_hotness', '__return_true' );
118
119        // These features run only on simple sites.
120        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
121            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_simple_jetpack_ai' ) );
122            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_comments' ) );
123            add_action( 'plugins_loaded', array( __CLASS__, 'load_jetpack_comments_routes' ) );
124            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_moderate' ) );
125            add_action( 'wp_loaded', array( __CLASS__, 'load_verbum_comments_admin' ) );
126            add_action( 'admin_menu', array( __CLASS__, 'load_wpcom_simple_odyssey_stats' ) );
127            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_random_redirect' ) );
128            add_action( 'plugins_loaded', array( __CLASS__, 'load_premium_analytics_stats_settings_fields' ) );
129
130            // Not on admin_menu: wpcom checks this function exists before then.
131            require_once __DIR__ . '/features/wpcom-simple-odyssey-stats/odyssey-stats-replaced.php';
132        }
133
134        // The Backup and Protect pages serve both platforms: they offer the transfer on Simple
135        // and the plan upgrade on WoA, and step aside once the feature is live.
136        if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Constants::is_true( 'IS_ATOMIC' ) ) {
137            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_backup' ) );
138            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_scan' ) );
139        }
140
141        // A lightweight My Jetpack page for Simple, which steps aside wherever the real My Jetpack registers.
142        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
143            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_my_jetpack' ) );
144            // Elsewhere load-jetpack.php starts the admin shell, and Simple never loads that file.
145            add_action( 'plugins_loaded', array( __CLASS__, 'load_admin_shell' ) );
146            // Elsewhere the Jetpack plugin's `monetize` module loads the page.
147            add_action( 'plugins_loaded', array( __CLASS__, 'load_monetize' ) );
148        }
149
150        if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Constants::is_true( 'IS_ATOMIC' ) ) {
151            add_filter( 'jetpack_feature_upgrade', array( \Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Feature_Upgrades::class, 'filter_monetize_upgrade' ), 10, 2 );
152        }
153
154        // At mu-plugin scope, because the Jetpack plugin can resolve these filters before plugins_loaded loads the pages.
155        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
156            add_filter( 'jetpack_backup_dashboard_enabled', array( \Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Backup::class, 'filter_jetpack_backup_dashboard' ) );
157            add_filter( 'jetpack_get_available_modules', array( \Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Scan::class, 'filter_available_modules' ) );
158            add_filter( 'jetpack_feature_upgrade', array( \Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Feature_Upgrades::class, 'filter_feature_upgrade' ), 10, 3 );
159            add_filter( 'jetpack_my_jetpack_plan_urls', array( \Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Feature_Upgrades::class, 'filter_plan_urls' ) );
160            // WoA runs a platform firewall, which is why Jetpack's stays off there.
161            add_filter( 'jetpack_protect_firewall_provided_by_host', '__return_true' );
162            // WoA sites should list threats, but not announce them.
163            add_filter( 'jetpack_protect_threat_notifications_enabled', '__return_false' );
164        }
165
166        // These features run only on atomic sites.
167        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
168            add_action( 'plugins_loaded', array( __CLASS__, 'load_custom_css' ) );
169            add_action( 'init', array( __CLASS__, 'schedule_translation_updates' ) );
170            // Match WPCOM site dashboard functionality for WordPress.com sites.
171            add_filter( 'jetpack_protect_threat_ignoring_enabled', '__return_false' );
172        }
173
174        // Premium Analytics offers the Ads tab on Simple and Atomic sites that have WordAds on.
175        add_action( 'plugins_loaded', array( __CLASS__, 'load_premium_analytics_wordads_section' ) );
176
177        // Premium Analytics seeds the Top videos widget wherever the plan includes VideoPress, on Simple and Atomic.
178        add_action( 'plugins_loaded', array( __CLASS__, 'load_premium_analytics_videopress_widgets' ) );
179
180        // Unified navigation fix for changes in WordPress 6.2.
181        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'unbind_focusout_on_wp_admin_bar_menu_toggle' ) );
182
183        // Load the Map block settings.
184        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_jetpack_mu_wpcom_settings' ), 999 );
185
186        // Load the Map block settings.
187        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_map_block_settings' ), 999 );
188
189        // Load the Newsletter category settings.
190        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_newsletter_categories_settings' ), 999 );
191
192        // Load the Social Links feature.
193        add_action( 'init', array( __CLASS__, 'load_social_links' ), 30 );
194
195        // Filter to ensure JetpackScriptData.site.host and is_wpcom_platform is set, to ensure Jetpack blocks work as expected via P2.
196        add_filter( 'jetpack_public_js_script_data', array( __CLASS__, 'add_jetpack_script_data_for_p2' ), 10, 1 );
197
198        // Filter to populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
199        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_wpcom_blog_id_script_data' ), 10, 1 );
200
201        // Enable the `gutenberg-react-19` Gutenberg experiment on selected sites.
202        add_filter( 'option_gutenberg-experiments', array( __CLASS__, 'enable_gutenberg_react_19_experiment' ) );
203        add_filter( 'default_option_gutenberg-experiments', array( __CLASS__, 'enable_gutenberg_react_19_experiment' ) );
204
205        // Allow blog stickers to override the WooCommerce unified block editor assets option.
206        add_filter( 'option_woocommerce_feature_block_editor_unified_assets_enabled', array( __CLASS__, 'enable_woocommerce_block_editor_unified_assets' ) );
207        add_filter( 'default_option_woocommerce_feature_block_editor_unified_assets_enabled', array( __CLASS__, 'enable_woocommerce_block_editor_unified_assets' ) );
208
209        if ( wpcom_has_blog_sticker( 'gutenberg-extensible-site-editor', get_wpcom_blog_id() ) ) {
210            add_filter( 'option_gutenberg-experiments', array( __CLASS__, 'enable_extensible_site_editor_experiment' ) );
211
212            // Priority 20 because register_setting() hooks core's filter_default_option() at 10,
213            // and that callback discards the value it is handed and returns the registered default.
214            add_filter( 'default_option_gutenberg-experiments', array( __CLASS__, 'enable_extensible_site_editor_experiment' ), 20 );
215        }
216
217        /**
218         * Runs right after the Jetpack_Mu_Wpcom package is initialized.
219         *
220         * @since 0.1.2
221         */
222        do_action( 'jetpack_mu_wpcom_initialized' );
223    }
224
225    /**
226     * Load the Jetpack AI Hub integration on WordPress.com Simple sites.
227     */
228    public static function load_wpcom_simple_jetpack_ai() {
229        /**
230         * Filters whether WordPress.com Simple should register the Jetpack AI Hub.
231         *
232         * @since 6.11.0
233         *
234         * @param bool $load Whether to load the AI Hub integration.
235         */
236        if ( ! apply_filters( 'jetpack_mu_wpcom_load_jetpack_ai_hub', false ) ) {
237            return;
238        }
239
240        require_once __DIR__ . '/features/jetpack-ai-hub/jetpack-ai-hub.php';
241    }
242
243    /**
244     * Schedules translation updates for Jetpack MU WPCOM.
245     *
246     * This function sets up the necessary cron jobs to ensure that translation files
247     * are regularly updated.
248     *
249     * @return void
250     */
251    public static function schedule_translation_updates() {
252        add_action( 'wpcomsh_translation_update', array( __CLASS__, 'maybe_update_translations' ) );
253
254        if ( ! wp_next_scheduled( 'wpcomsh_translation_update' ) ) {
255            wp_schedule_event( time(), 'twicedaily', 'wpcomsh_translation_update' );
256        }
257    }
258
259    /**
260     * Fetches and installs Jetpack-mu-wpcom package translations when needed.
261     */
262    public static function maybe_update_translations() {
263        global $wp_filesystem;
264        if ( ! $wp_filesystem ) {
265            require_once ABSPATH . 'wp-admin/includes/file.php';
266            WP_Filesystem();
267        }
268
269        $locales = self::get_all_active_locales();
270        if ( empty( $locales ) ) {
271            return;
272        }
273
274        $plugins_request_data              = array();
275        $plugin_language_pack_destinations = array(
276            'jetpack-mu-wpcom' => WP_LANG_DIR . '/mu-plugins/',
277            'wpcomsh'          => WP_LANG_DIR . '/mu-plugins/',
278        );
279
280        foreach ( array_keys( $plugin_language_pack_destinations ) as $plugin_slug ) {
281            $plugins_request_data[ $plugin_slug ] = array( 'version' => 'latest' );
282        }
283
284        $response = wp_remote_post(
285            'https://translate.wordpress.com/api/translations-updates/wpcom/plugins',
286            array(
287                'body'    => wp_json_encode(
288                    array(
289                        'locales' => $locales,
290                        'plugins' => $plugins_request_data,
291                    ),
292                    JSON_UNESCAPED_SLASHES
293                ),
294                'headers' => array( 'Content-Type' => 'application/json' ),
295                'timeout' => 10,
296            )
297        );
298
299        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
300            return;
301        }
302
303        $data = json_decode( wp_remote_retrieve_body( $response ), true );
304
305        // API error, api returned but something was wrong.
306        if ( array_key_exists( 'success', $data ) && false === $data['success'] ) {
307            return;
308        }
309
310        if ( ! is_array( $data ) || ! is_array( $data['data'] ) ) {
311            return;
312        }
313
314        foreach ( $data['data'] as $plugin_name => $language_packs ) {
315            if ( ! isset( $plugin_language_pack_destinations[ $plugin_name ] ) ) {
316                continue;
317            }
318
319            $destination = $plugin_language_pack_destinations[ $plugin_name ];
320
321            foreach ( $language_packs as $translation ) {
322                $locale        = $translation['wp_locale'] ?? '';
323                $package_url   = $translation['package'] ?? '';
324                $last_modified = $translation['last_modified'] ?? '';
325
326                if ( ! $locale || ! $package_url || ! $last_modified ) {
327                    continue;
328                }
329
330                $local_po_file = "{$destination}/$plugin_name-{$locale}.po";
331                if ( file_exists( $local_po_file ) ) {
332                    $local_po_data                       = wp_get_pomo_file_data( $local_po_file );
333                    $installed_translation_revision_time = new \DateTime( $local_po_data['PO-Revision-Date'] );
334                    $new_translation_revision_time       = new \DateTime( $last_modified );
335
336                    // Skip if translation language pack is not newer than what is installed already.
337                    if ( $new_translation_revision_time <= $installed_translation_revision_time ) {
338                        continue;
339                    }
340                }
341
342                $translation_zip_file = download_url( $package_url );
343                if ( is_wp_error( $translation_zip_file ) ) {
344                    continue;
345                }
346
347                static::clear_translation_destination( $destination, $plugin_name, $locale );
348
349                $unzip_result = unzip_file( $translation_zip_file, $destination );
350                if ( is_wp_error( $unzip_result ) ) {
351                    wp_delete_file( $translation_zip_file );
352                    continue;
353                }
354
355                wp_delete_file( $translation_zip_file );
356
357            }
358        }
359    }
360
361    /**
362     * Clears the translation destination by deleting existing translation files.
363     *
364     * @param string $local_destination The local destination path.
365     * @param string $plugin_slug The plugin slug.
366     * @param string $locale The locale.
367     */
368    public static function clear_translation_destination( $local_destination, $plugin_slug, $locale ) {
369        global $wp_filesystem;
370
371        if ( ! $wp_filesystem ) {
372            require_once ABSPATH . 'wp-admin/includes/file.php';
373            WP_Filesystem();
374        }
375
376        $files = array(
377            "{$local_destination}{$plugin_slug}-{$locale}.po",
378            "{$local_destination}{$plugin_slug}-{$locale}.mo",
379            "{$local_destination}{$plugin_slug}-{$locale}.l10n.php",
380        );
381
382        $json_files = glob( "{$local_destination}{$plugin_slug}-{$locale}-*.json" );
383        if ( $json_files ) {
384            $files = array_merge( $files, $json_files );
385        }
386
387        foreach ( $files as $file ) {
388            if ( $wp_filesystem->exists( $file ) ) {
389                $wp_filesystem->delete( $file );
390            }
391        }
392    }
393
394    /**
395     * Retrieves all active locales for the site.
396     */
397    public static function get_all_active_locales() {
398        $locales = array( get_locale() );
399
400        $available_languages = get_available_languages();
401        if ( ! empty( $available_languages ) ) {
402            $locales = array_merge( $locales, $available_languages );
403        }
404        return array_values( array_unique( $locales ) );
405    }
406
407    /**
408     * Load features that don't need any special loading considerations.
409     */
410    public static function load_features() {
411        \Automattic\Jetpack\ExPlat::init();
412
413        // Please keep the features in alphabetical order.
414        require_once __DIR__ . '/features/100-year-plan/enhanced-ownership.php';
415        require_once __DIR__ . '/features/100-year-plan/locked-mode.php';
416        require_once __DIR__ . '/features/admin-color-schemes/admin-color-schemes.php';
417        require_once __DIR__ . '/features/ai-launchpad/ai-launchpad.php';
418        require_once __DIR__ . '/features/block-patterns/block-patterns.php';
419        require_once __DIR__ . '/features/blog-privacy/blog-privacy.php';
420        require_once __DIR__ . '/features/cloudflare-analytics/cloudflare-analytics.php';
421        require_once __DIR__ . '/features/code-editor/class-code-editor.php';
422        require_once __DIR__ . '/features/wpcom-blocks/code/class-code-block.php';
423        require_once __DIR__ . '/features/comment-page-canonical/comment-page-canonical.php';
424        require_once __DIR__ . '/features/css-monkey-patches/index.php';
425        require_once __DIR__ . '/features/editor-polyfills/editor-polyfills.php';
426        require_once __DIR__ . '/features/editor-welcome-guides/editor-welcome-guides.php';
427        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
428            require_once __DIR__ . '/features/editor-welcome-guides/editor-welcome-guides-atomic.php';
429        }
430        require_once __DIR__ . '/features/error-reporting/error-reporting.php';
431        require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
432        require_once __DIR__ . '/features/font-smoothing-antialiased/font-smoothing-antialiased.php';
433        require_once __DIR__ . '/features/google-analytics/google-analytics.php';
434        require_once __DIR__ . '/features/hide-non-breaking-space-markers/hide-non-breaking-space-markers.php';
435        require_once __DIR__ . '/features/holiday-snow/class-holiday-snow.php';
436        require_once __DIR__ . '/features/launch-button/index.php';
437        require_once __DIR__ . '/features/layout-grid-usage-tracking/layout-grid-usage-tracking.php';
438        require_once __DIR__ . '/features/logo-tool/logo-tool.php';
439        require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php';
440        require_once __DIR__ . '/features/media/ai-marker-image-editor.php';
441        require_once __DIR__ . '/features/media/heif-support.php';
442        require_once __DIR__ . '/features/media/m4a-upload-support.php';
443        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
444            require_once __DIR__ . '/features/plugin-conflicts-guardian/plugin-conflicts-guardian.php';
445        }
446        require_once __DIR__ . '/features/post-categories/quick-actions.php';
447        require_once __DIR__ . '/features/post-like-from-email/post-like-from-email.php';
448        require_once __DIR__ . '/features/podcast-feed-credit/podcast-feed-credit.php';
449        require_once __DIR__ . '/features/repost-notifications/repost-notifications.php';
450        require_once __DIR__ . '/features/repost-notifications/delayed-repost-pingbacks.php';
451        require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php';
452        if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Constants::is_true( 'IS_ATOMIC' ) ) {
453            require_once __DIR__ . '/features/webmcp-origin-trial/webmcp-origin-trial.php';
454        }
455        require_once __DIR__ . '/features/wpcom-attachment-pages/wpcom-attachment-pages.php';
456        require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php';
457        require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php';
458        require_once __DIR__ . '/features/wpcom-dashboard/class-wpcom-dashboard.php';
459        require_once __DIR__ . '/features/wpcom-dashboard-tracks/wpcom-dashboard-tracks.php';
460        require_once __DIR__ . '/features/wpcom-logout/wpcom-logout.php';
461        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';
462        require_once __DIR__ . '/features/wpcom-post-list/wpcom-post-types-tracking.php';
463        require_once __DIR__ . '/features/wpcom-unified-admin-page-view/wpcom-unified-admin-page-view.php';
464        require_once __DIR__ . '/features/wpcom-widgets/wpcom-widgets.php';
465        require_once __DIR__ . '/features/wpcom-wpadmin-page-view/wpcom-wpadmin-page-view.php';
466        if ( Constants::is_true( 'IS_ATOMIC' ) ) {
467            require_once __DIR__ . '/features/wpme-oembed/wpme-oembed.php';
468        }
469
470        require_once __DIR__ . '/features/write/write.php';
471
472        /*
473         * Temporarily disable client-side media processing.
474         *
475         * Client-side media processing enables cross-origin isolation (COEP/COOP headers)
476         * which can break authenticated API requests. This should be removed once client-side
477         * media processing is compatible with Dotcom's infrastructure.
478         *
479         * @see gutenberg_set_up_cross_origin_isolation() in Gutenberg's lib/media/load.php
480         * @see https://a8c.slack.com/archives/CBTN58FTJ/p1771950744814189
481         */
482        add_filter( 'wp_client_side_media_processing_enabled', '__return_false' );
483
484        // Initializers, if needed.
485        $activity_log_event_class = 'Automattic\\Jetpack\\Sync\\Activity_Log_Event';
486        if ( class_exists( $activity_log_event_class ) ) {
487            $activity_log_event_class::init();
488        }
489
490        \Marketplace_Products_Updater::init();
491        \Automattic\Jetpack\Code_Editor::setup();
492        \Automattic\Jetpack\Code_Block::setup();
493        \Automattic\Jetpack\Classic_Theme_Helper\Main::init();
494        \Automattic\Jetpack\Classic_Theme_Helper\Featured_Content::setup();
495
496        \Automattic\Jetpack\Jetpack_Mu_Wpcom\AI_Launchpad::init();
497        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Holiday_Snow::init();
498        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Dashboard::init();
499
500        // The front-end Action Bar lives in the jetpack-newsletter package, which mu-wpcom does not
501        // composer-require; the class comes from the sibling Jetpack autoloader. Not in
502        // load_wpcom_user_features(): logged-out visitors are the bar's main audience.
503        if ( class_exists( '\Automattic\Jetpack\Newsletter\Action_Bar' ) ) {
504            // @phan-suppress-next-line PhanUndeclaredClassMethod -- class_exists guarded above; provided by sibling autoloader.
505            \Automattic\Jetpack\Newsletter\Action_Bar::init();
506        }
507
508        // Gets autoloaded from the Scheduled_Updates package.
509        if ( class_exists( 'Automattic\Jetpack\Scheduled_Updates' ) ) {
510            Scheduled_Updates::init();
511        }
512    }
513
514    /**
515     * Load features that only apply to WordPress.com-connected users.
516     */
517    public static function load_wpcom_user_features() {
518        // To avoid potential collisions with ETK.
519        if ( ! class_exists( 'A8C\FSE\Help_Center' ) && class_exists( \Automattic\Jetpack\Help_Center\Help_Center::class ) ) {
520            add_action( 'init', array( \Automattic\Jetpack\Help_Center\Help_Center::class, 'init' ), 10, 0 );
521        }
522
523        // Every admin, not only WordPress.com users: one who cannot renew, such as
524        // the client of an agency-managed site, is told whose plan it is, and the
525        // legacy notice the feature replaces stands down only once this has loaded.
526        require_once __DIR__ . '/features/expiry-notices/expiry-notices.php';
527
528        // Its handlers check the user at event time: API and CLI requests set it after plugins_loaded.
529        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-tracking.php';
530
531        if ( ! is_wpcom_user() ) {
532            require_once __DIR__ . '/features/replace-site-visibility/hide-site-visibility.php';
533            return;
534        }
535        if ( ! class_exists( 'A8C\FSE\Survicate' ) ) {
536            require_once __DIR__ . '/features/survicate/class-survicate.php';
537        }
538        require_once __DIR__ . '/features/ai-assistant-banner/ai-assistant-banner.php';
539        require_once __DIR__ . '/features/html-block-restricted-tags/html-block-restricted-tags.php';
540        require_once __DIR__ . '/features/pages/pages.php';
541        require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
542        require_once __DIR__ . '/features/stats/stats.php';
543        require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
544        require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
545        require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
546        require_once __DIR__ . '/features/wpcom-comments/wpcom-comments.php';
547        require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php';
548        require_once __DIR__ . '/features/wpcom-imports/wpcom-imports.php';
549        require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
550        require_once __DIR__ . '/features/wpcom-media/wpcom-media-url-upload.php';
551        require_once __DIR__ . '/features/wpcom-media/wpcom-export-media-files.php';
552        require_once __DIR__ . '/features/wpcom-options-general/options-general.php';
553        require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
554        require_once __DIR__ . '/features/wpcom-plugins/wpcom-marketplace-tab.php';
555        require_once __DIR__ . '/features/wpcom-plugins/wpcom-marketplace-cards.php';
556        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
557        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
558        require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
559        require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
560        require_once __DIR__ . '/features/wpcom-themes/wpcom-themes-tab.php';
561        require_once __DIR__ . '/features/wpcom-user-edit/wpcom-user-edit.php';
562        require_once __DIR__ . '/features/wpcom-videopress/wpcom-videopress.php';
563
564        // Initialize Newsletter Settings so hooks like the Reading page notice
565        // are registered on Simple sites (where load-jetpack.php doesn't run).
566        // Guarded with class_exists since mu-wpcom no longer composer-requires
567        // the jetpack-newsletter package: the class is provided by the standalone
568        // Jetpack plugin on Atomic, or by the wpcom platform's bundled Jetpack
569        // source on Simple.
570        if ( class_exists( '\Automattic\Jetpack\Newsletter\Settings' ) ) {
571            // @phan-suppress-next-line PhanUndeclaredClassMethod -- class_exists guarded above; provided by sibling autoloader.
572            \Automattic\Jetpack\Newsletter\Settings::init();
573        }
574
575        // Register the Daily Writing Prompt dashboard widget, which now lives in
576        // the jetpack-newsletter package. Guarded with class_exists for the same
577        // reason as Settings above: mu-wpcom doesn't composer-require the package.
578        if ( class_exists( '\Automattic\Jetpack\Newsletter\Writing_Prompt_Widget' ) ) {
579            // @phan-suppress-next-line PhanUndeclaredClassMethod -- class_exists guarded above; provided by sibling autoloader.
580            \Automattic\Jetpack\Newsletter\Writing_Prompt_Widget::init();
581        }
582
583        // Only load the Masterbar features on WoA sites.
584        if ( class_exists( '\Automattic\Jetpack\Status\Host' ) && ( new \Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
585            // This is temporary. After we cleanup Masterbar on WPCOM we should load Masterbar for Simple sites too.
586            \Automattic\Jetpack\Masterbar\Main::init();
587        }
588
589        if ( class_exists( 'Automattic\Jetpack\Agents_Manager\Agents_Manager' ) ) {
590            \Automattic\Jetpack\Agents_Manager\Agents_Manager::init();
591            require_once __DIR__ . '/features/wpcom-agents-manager/wpcom-agents-manager.php';
592        }
593    }
594
595    /**
596     * Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
597     */
598    public static function load_wpcom_sites_features() {
599        if ( is_fully_managed_agency_site() ) {
600            return;
601        }
602
603        require_once __DIR__ . '/features/gutenberg-rtc/gutenberg-rtc.php';
604        require_once __DIR__ . '/features/wpcom-contact-form-flags/wpcom-contact-form-flags.php';
605    }
606
607    /**
608     * Define the flags to turn off features in the ETK plugin.
609     * Can be removed once the feature no longer exists in the ETK plugin.
610     */
611    public static function load_etk_features_flags() {
612        // Don't load on agency sites.
613        if ( is_fully_managed_agency_site() ) {
614            return;
615        }
616
617        // Don't load if the user is not a wpcom user on WP Admin.
618        // The features is still required on the frontend page regardless of the user.
619        if ( is_admin() && ! is_wpcom_user() ) {
620            return;
621        }
622
623        define( 'MU_WPCOM_COBLOCKS_GALLERY', true );
624        define( 'MU_WPCOM_CUSTOM_LINE_HEIGHT', true );
625        define( 'MU_WPCOM_BLOCK_INSERTER_MODIFICATIONS', true );
626        define( 'MU_WPCOM_HOMEPAGE_TITLE_HIDDEN', true );
627        define( 'MU_WPCOM_JETPACK_GLOBAL_STYLES', true );
628        define( 'A8C_USE_FONT_SMOOTHING_ANTIALIASED', false );
629        define( 'MU_WPCOM_MAILERLITE_WIDGET', true );
630        define( 'MU_WPCOM_OVERRIDE_PREVIEW_BUTTON_URL', true );
631        define( 'MU_WPCOM_PARAGRAPH_BLOCK', true );
632        define( 'MU_WPCOM_STARTER_PAGE_TEMPLATES', true );
633        define( 'MU_WPCOM_TAGS_EDUCATION', true );
634        define( 'MU_WPCOM_BLOCK_DESCRIPTION_LINKS', true );
635        define( 'MU_WPCOM_BLOCK_EDITOR_NUX', true );
636        define( 'MU_WPCOM_POSTS_LIST_BLOCK', true );
637        define( 'MU_WPCOM_JETPACK_COUNTDOWN_BLOCK', true );
638        define( 'MU_WPCOM_JETPACK_TIMELINE_BLOCK', true );
639        define( 'MU_WPCOM_DOCUMENTATION_LINKS', true );
640        define( 'MU_WPCOM_GLOBAL_STYLES', true );
641        define( 'MU_WPCOM_FSE', true );
642        define( 'MU_WPCOM_TEMPLATE_INSERTER', true );
643        define( 'MU_WPCOM_WHATS_NEW', true );
644    }
645
646    /**
647     * Load ETK features.
648     * Can be moved back to load_features() once the feature no longer exists in the ETK plugin.
649     */
650    public static function load_etk_features() {
651        // Don't load on agency sites.
652        if ( is_fully_managed_agency_site() ) {
653            return;
654        }
655
656        // Don't load if the user is not a wpcom user on WP Admin.
657        // The features is still required on the frontend page regardless of the user.
658        if ( is_admin() && ! is_wpcom_user() ) {
659            return;
660        }
661
662        require_once __DIR__ . '/features/jetpack-global-styles/class-global-styles.php';
663        require_once __DIR__ . '/features/mailerlite/subscriber-popup.php';
664        require_once __DIR__ . '/features/wpcom-fse/wpcom-fse.php';
665
666        /**
667         * Load features for the editor and the frontend pages.
668         */
669        global $pagenow;
670        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
671        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
672            require_once __DIR__ . '/features/block-editor/custom-line-height.php';
673            require_once __DIR__ . '/features/block-inserter-modifications/block-inserter-modifications.php';
674            require_once __DIR__ . '/features/hide-homepage-title/hide-homepage-title.php';
675            require_once __DIR__ . '/features/override-preview-button-url/override-preview-button-url.php';
676            require_once __DIR__ . '/features/paragraph-block-placeholder/paragraph-block-placeholder.php';
677            require_once __DIR__ . '/features/tags-education/tags-education.php';
678            require_once __DIR__ . '/features/wpcom-block-description-links/wpcom-block-description-links.php';
679            require_once __DIR__ . '/features/wpcom-block-editor-nux/class-wpcom-block-editor-nux.php';
680            require_once __DIR__ . '/features/wpcom-blocks/a8c-posts-list/a8c-posts-list.php';
681            require_once __DIR__ . '/features/wpcom-blocks/event-countdown/event-countdown.php';
682            require_once __DIR__ . '/features/wpcom-blocks/timeline/timeline.php';
683            require_once __DIR__ . '/features/wpcom-documentation-links/wpcom-documentation-links.php';
684            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
685            require_once __DIR__ . '/features/wpcom-legacy-fse/wpcom-legacy-fse.php';
686        } elseif ( isset( $pagenow ) && 'customize.php' === $pagenow ) {
687            // Load wpcom-global-styles on the customizer so access to additional css can be checked there.
688            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
689        }
690    }
691
692    /**
693     * Load the newspack blocks feature for the editor and the frontend pages.
694     */
695    public static function load_newspack_blocks() {
696        /**
697         * Avoid potential collisions with newspack-blocks plugin.
698         */
699        if ( class_exists( '\Newspack_Blocks', false ) ) {
700            return;
701        }
702
703        global $pagenow;
704        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
705        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
706            define( 'MU_WPCOM_NEWSPACK_BLOCKS', true );
707            require_once __DIR__ . '/features/newspack-blocks/index.php';
708        }
709    }
710
711    /**
712     * Load the Coming Soon feature.
713     */
714    public static function load_coming_soon() {
715        /**
716         * On WoA sites, users may be using non-symlinked older versions of the FSE plugin.
717         * If they are, check the active version to avoid redeclaration errors.
718         */
719        if ( ! function_exists( 'is_plugin_active' ) ) {
720            require_once ABSPATH . 'wp-admin/includes/plugin.php';
721        }
722
723        /**
724         * Explicitly pass $markup = false in get_plugin_data to avoid indirectly calling wptexturize that could cause unintended side effects.
725         * See: https://developer.wordpress.org/reference/functions/get_plugin_data/
726         */
727        $fse_plugin                 = 'full-site-editing/full-site-editing-plugin.php';
728        $fse_plugin_path            = WP_PLUGIN_DIR . '/' . $fse_plugin;
729        $invalid_fse_version_active =
730            file_exists( $fse_plugin_path ) &&
731            is_file( $fse_plugin_path ) &&
732            is_plugin_active( $fse_plugin ) &&
733            version_compare( get_plugin_data( $fse_plugin_path, false )['Version'], '3.56084', '<' );
734
735        if ( $invalid_fse_version_active ) {
736            return;
737        }
738
739        if (
740            ( defined( 'WPCOM_PUBLIC_COMING_SOON' ) && WPCOM_PUBLIC_COMING_SOON ) ||
741            apply_filters( 'a8c_enable_public_coming_soon', false )
742        ) {
743            require_once __DIR__ . '/features/coming-soon/coming-soon.php';
744        }
745    }
746
747    /**
748     * Load the Launchpad feature.
749     */
750    public static function load_launchpad() {
751        require_once __DIR__ . '/features/launchpad/launchpad.php';
752    }
753
754    /**
755     * Load WP REST API plugins for wpcom.
756     */
757    public static function load_wpcom_rest_api_endpoints() {
758        if ( ! function_exists( 'wpcom_rest_api_v2_load_plugin' ) ) {
759            return;
760        }
761
762        // We don't use `wpcom_rest_api_v2_load_plugin_files` because it operates inconsisently.
763        $plugins = glob( __DIR__ . '/features/wpcom-endpoints/*.php' );
764
765        if ( ! is_array( $plugins ) ) {
766            return;
767        }
768
769        foreach ( array_filter( $plugins, 'is_file' ) as $plugin ) {
770            require_once $plugin;
771        }
772    }
773
774    /**
775     * Adds a global variable containing the config of the plugin to the window object.
776     */
777    public static function load_jetpack_mu_wpcom_settings() {
778        $handle = 'jetpack-mu-wpcom-settings';
779
780        // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
781        wp_register_script(
782            $handle,
783            false,
784            array(),
785            true
786        );
787
788        $data = wp_json_encode(
789            array(
790                'assetsUrl' => plugins_url( 'build/', self::BASE_FILE ),
791            ),
792            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
793        );
794
795        wp_add_inline_script(
796            $handle,
797            "var JETPACK_MU_WPCOM_SETTINGS = $data;",
798            'before'
799        );
800
801        wp_enqueue_script( $handle );
802    }
803
804    /**
805     * Adds a global variable containing the map provider in a map_block_settings object to the window object.
806     */
807    public static function load_map_block_settings() {
808        if (
809            ! function_exists( 'get_current_screen' )
810            || \get_current_screen() === null
811        ) {
812            return;
813        }
814
815        // Return early if we are not in the block editor.
816        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
817            return;
818        }
819
820        $map_provider = apply_filters( 'wpcom_map_block_map_provider', 'mapbox' );
821        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Maps', array( 'provider' => $map_provider ) );
822    }
823
824    /**
825     * Adds a global variable containing where the newsletter categories should be shown.
826     */
827    public static function load_newsletter_categories_settings() {
828        if (
829            ! function_exists( 'get_current_screen' )
830            || \get_current_screen() === null
831        ) {
832            return;
833        }
834
835        // Return early if we are not in the block editor.
836        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
837            return;
838        }
839
840        $newsletter_categories_location = apply_filters( 'wpcom_newsletter_categories_location', 'block' );
841        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Subscriptions', array( 'newsletter_categories_location' => $newsletter_categories_location ) );
842    }
843
844    /**
845     * Unbinds focusout event handler on #wp-admin-bar-menu-toggle introduced in WordPress 6.2.
846     *
847     * The focusout event handler is preventing the unified navigation from being closed on mobile.
848     */
849    public static function unbind_focusout_on_wp_admin_bar_menu_toggle() {
850        wp_add_inline_script( 'common', '(function($){ $(document).on("wp-responsive-activate", function(){ $(".is-nav-unification #wp-admin-bar-menu-toggle, .is-nav-unification #adminmenumain").off("focusout"); } ); }(jQuery) );' );
851    }
852
853    /**
854     * Determine whether to disable the comment experience.
855     *
856     * @param int $blog_id The blog ID.
857     * @return boolean
858     */
859    private static function should_disable_comment_experience( $blog_id ) {
860        $path_wp_for_teams = WP_CONTENT_DIR . '/lib/wpforteams/functions.php';
861
862        if ( file_exists( $path_wp_for_teams ) ) {
863            require_once $path_wp_for_teams;
864        }
865
866        // This covers both P2 and P2020 themes.
867        $is_p2     = str_contains( get_stylesheet(), 'pub/p2' ) || function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( $blog_id );
868        $is_forums = str_contains( get_stylesheet(), 'a8c/supportforums' ); // Not in /forums.
869
870        $verbum_option_enabled = get_blog_option( $blog_id, 'enable_verbum_commenting', true );
871
872        if ( empty( $verbum_option_enabled ) ) {
873            return true;
874        }
875
876        // Don't load any comment experience in the Reader, GlotPress, wp-admin, or P2.
877        return ( 1 === $blog_id || TRANSLATE_BLOG_ID === $blog_id || is_admin() || $is_p2 || $is_forums );
878    }
879
880    /**
881     * Whether Jetpack Comments handles the current blog's comments.
882     *
883     * @param bool $enabled Whether Jetpack Comments is enabled.
884     * @return bool
885     */
886    public static function jetpack_comments_enabled_for_blog( $enabled ) {
887        static $by_blog = array();
888
889        $blog_id               = get_current_blog_id();
890        $by_blog[ $blog_id ] ??= ! self::should_disable_comment_experience( $blog_id );
891
892        return $enabled && $by_blog[ $blog_id ];
893    }
894
895    /**
896     * Load Verbum Comments.
897     */
898    public static function load_verbum_comments() {
899        // public-api boots on its own blog, so decide once the request has switched to its target.
900        if ( 'public-api.wordpress.com' === wp_parse_url( home_url(), PHP_URL_HOST ) ) {
901            // Now, for reads across sites that never switch: avatars only touch comments with our stored photo.
902            \Automattic\Jetpack\Comments\Avatars::init();
903            add_action( 'rest_api_switched_to_blog', array( __CLASS__, 'load_verbum_comments' ) );
904            return;
905        }
906
907        if ( class_exists( 'Verbum_Comments' ) ) {
908            return;
909        } else {
910            $blog_id = get_current_blog_id();
911            // Jetpack loads Verbum though an iframe from jetpack.wordpress.com.
912            // So we need to check the GET request for the blogid.
913            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
914            if ( isset( $_GET['blogid'] ) ) {
915                $blog_id = intval( $_GET['blogid'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
916            }
917            if ( self::should_disable_comment_experience( $blog_id ) ) {
918                return;
919            }
920
921            if ( \Automattic\Jetpack\Comments\Comments::is_enabled() ) {
922                add_filter( 'jetpack_comments_enabled', array( __CLASS__, 'jetpack_comments_enabled_for_blog' ) );
923                \Automattic\Jetpack\Comments\Comments::init();
924                return;
925            }
926
927            require_once __DIR__ . '/features/verbum-comments/class-verbum-comments.php';
928            new \Automattic\Jetpack\Verbum_Comments();
929        }
930    }
931
932    /**
933     * Register Jetpack Comments' browser-facing routes ahead of the comment
934     * experience gates. admin-ajax is is_admin(), so load_verbum_comments() skips it,
935     * and on public-api it waits for the blog switch, which comes after rest_api_init.
936     * Runs on every request, blog 1 and P2s included, on purpose: the routes
937     * gate themselves on Comments::is_enabled(), as Posts_To_Podcast_Endpoint does.
938     */
939    public static function load_jetpack_comments_routes() {
940        \Automattic\Jetpack\Comments\Checkpoint_Endpoint::init();
941        \Automattic\Jetpack\Comments\Embeds::init();
942    }
943
944    /**
945     * Load Verbum Comments Settings.
946     */
947    public static function load_verbum_comments_admin() {
948        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-admin.php';
949        new \Automattic\Jetpack\Verbum_Admin();
950    }
951
952    /**
953     * Load Verbum Moderate.
954     */
955    public static function load_verbum_moderate() {
956        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-moderate.php';
957        new \Automattic\Jetpack\Verbum_Moderate();
958    }
959
960    /**
961     * Load the Backup page on WordPress.com Simple and WoA sites.
962     *
963     * The file hooks its own `init`, where the plan lookup it gates on is ready.
964     */
965    public static function load_wpcom_backup() {
966        require_once __DIR__ . '/features/wpcom-backup/wpcom-backup.php';
967    }
968
969    /**
970     * Load the Protect page, which offers Scan, on WordPress.com Simple and WoA sites.
971     *
972     * The file hooks its own `init`, where the plan lookup it gates on is ready.
973     */
974    public static function load_wpcom_scan() {
975        require_once __DIR__ . '/features/wpcom-scan/wpcom-scan.php';
976    }
977
978    /**
979     * Load the lightweight My Jetpack page on WordPress.com Simple sites.
980     */
981    public static function load_wpcom_my_jetpack() {
982        require_once __DIR__ . '/features/wpcom-my-jetpack/wpcom-my-jetpack.php';
983    }
984
985    /**
986     * Start the Jetpack admin shell, which stays inert until its `jetpack-admin-shell` flag is on.
987     *
988     * @since $$next-version$$
989     *
990     * @return void
991     */
992    public static function load_admin_shell() {
993        // Admin UI ships with Jetpack, and an older copy may predate the shell.
994        if ( ! method_exists( \Automattic\Jetpack\Admin_UI\Admin_Shell::class, 'init' ) || ! method_exists( \Automattic\Jetpack\Admin_UI\Admin_Shell::class, 'register_feature_flag' ) ) {
995            return;
996        }
997
998        \Automattic\Jetpack\Admin_UI\Admin_Shell::register_feature_flag();
999        // Not left to Admin_Menu: admin-ajax requests never build the menu, and the guest endpoint lives there.
1000        \Automattic\Jetpack\Admin_UI\Admin_Shell::init();
1001    }
1002
1003    /**
1004     * Load the Monetize page on Simple while its feature flag is on.
1005     *
1006     * @since $$next-version$$
1007     *
1008     * @return void
1009     */
1010    public static function load_monetize() {
1011        // The package ships with Jetpack, which deploys separately from this one.
1012        if ( ! method_exists( \Automattic\Jetpack\Monetize\Dashboard::class, 'register_feature_flag' ) ) {
1013            return;
1014        }
1015
1016        \Automattic\Jetpack\Monetize\Dashboard::register_feature_flag();
1017        if ( \Automattic\Jetpack\Monetize\Dashboard::is_enabled() ) {
1018            \Automattic\Jetpack\Monetize\Dashboard::init();
1019        }
1020    }
1021
1022    /**
1023     * Load Odyssey Stats in Simple sites.
1024     */
1025    public static function load_wpcom_simple_odyssey_stats() {
1026        require_once __DIR__ . '/features/wpcom-simple-odyssey-stats/wpcom-simple-odyssey-stats.php';
1027    }
1028
1029    /**
1030     * Register the Ads tab of the Premium Analytics dashboard where WordAds is on.
1031     *
1032     * Hooks the dashboard's registry action, which only fires once the package boots, so this
1033     * is inert on a site without the dashboard.
1034     *
1035     * @since 6.11.0
1036     */
1037    public static function load_premium_analytics_wordads_section() {
1038        require_once __DIR__ . '/features/premium-analytics/wordads-section.php';
1039    }
1040
1041    /**
1042     * Register the Top videos widget of the Premium Analytics dashboard by plan feature.
1043     *
1044     * Same seam as the Ads tab: inert on a site without the dashboard.
1045     *
1046     * @since 6.11.0
1047     */
1048    public static function load_premium_analytics_videopress_widgets() {
1049        require_once __DIR__ . '/features/premium-analytics/videopress-widgets.php';
1050    }
1051
1052    /**
1053     * Limit the Premium Analytics Settings tab on Simple to the Stats settings WordPress.com honors there.
1054     *
1055     * @since $$next-version$$
1056     */
1057    public static function load_premium_analytics_stats_settings_fields() {
1058        require_once __DIR__ . '/features/premium-analytics/stats-settings-fields.php';
1059    }
1060
1061    /**
1062     * Load the Jetpack Custom CSS feature.
1063     */
1064    public static function load_custom_css() {
1065        require_once __DIR__ . '/features/custom-css/custom-css/preprocessors.php';
1066        require_once __DIR__ . '/features/custom-css/custom-css.php';
1067    }
1068
1069    /**
1070     * Load the Random Redirect feature.
1071     */
1072    public static function load_wpcom_random_redirect() {
1073        // WordPress 7.2 handles random content redirects (/random/, ?random) in core; every 7.2 beta includes it.
1074        if ( version_compare( $GLOBALS['wp_version'], '7.2-beta', '>=' ) ) {
1075            return;
1076        }
1077        require_once __DIR__ . '/features/random-redirect/random-redirect.php';
1078    }
1079
1080    /**
1081     * Load the Social Links feature.
1082     */
1083    public static function load_social_links() {
1084        if ( class_exists( 'Automattic\Jetpack\Classic_Theme_Helper\Social_Links' ) ) {
1085            new \Automattic\Jetpack\Classic_Theme_Helper\Social_Links();
1086        }
1087    }
1088
1089    /**
1090     * Populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
1091     *
1092     * @param array $data The script data.
1093     * @return array
1094     */
1095    public static function set_wpcom_blog_id_script_data( $data ) {
1096        $blog_id = get_wpcom_blog_id();
1097        if ( $blog_id ) {
1098            $data['site']['wpcom']['blog_id'] = $blog_id;
1099        }
1100        return $data;
1101    }
1102
1103    /**
1104     * Add `gutenberg-react-19` to the list of enabled Gutenberg experiments.
1105     *
1106     * The `disable-gutenberg-react-19` blog sticker force-disables the experiment,
1107     * the `gutenberg-react-19` sticker opts the site in. With neither sticker,
1108     * the experiment is enabled on a certain percentage of Atomic sites,
1109     * known incompatible plugins and themes are excluded.
1110     *
1111     * @param mixed $experiments The current value of the gutenberg-experiments option.
1112     * @return mixed Original option value or the filtered experiments.
1113     */
1114    public static function enable_gutenberg_react_19_experiment( $experiments ) {
1115        if ( self::has_react_19_incompatible_gutenberg() ) {
1116            return $experiments;
1117        }
1118
1119        $blog_id = get_wpcom_blog_id();
1120
1121        if ( wpcom_has_blog_sticker( 'disable-gutenberg-react-19', $blog_id ) ) {
1122            return $experiments;
1123        }
1124
1125        $is_enabled = wpcom_has_blog_sticker( 'gutenberg-react-19', $blog_id );
1126
1127        if ( ! $is_enabled && function_exists( 'wpcomsh_get_atomic_site_id' ) ) {
1128            $site_id = wpcomsh_get_atomic_site_id();
1129
1130            // Don't enable if the site ID is unknown (zero).
1131            if ( ! $site_id ) {
1132                $is_enabled = false;
1133            } elseif ( self::has_react_19_incompatible_extension() ) {
1134                $is_enabled = false;
1135            } else {
1136                $current_segment = 70; // Segment of Atomic sites in the experiment, in %.
1137                $site_segment    = $site_id % 100;
1138
1139                /*
1140                 * Sites whose Atomic site ID ends in digits < $current_segment are in the segment.
1141                 * Must stay in sync with the error reporting rollout in wpcomsh
1142                 * (wpcomsh_enable_error_reporting_for_react_19), so that every site in
1143                 * the experiment also reports JS errors.
1144                 */
1145                $is_enabled = $site_segment < $current_segment;
1146            }
1147        }
1148
1149        if ( ! $is_enabled ) {
1150            return $experiments;
1151        }
1152
1153        if ( ! is_array( $experiments ) ) {
1154            $experiments = array();
1155        }
1156
1157        $experiments['gutenberg-react-19'] = true;
1158        return $experiments;
1159    }
1160
1161    /**
1162     * Override the WooCommerce unified block editor assets option with blog stickers.
1163     *
1164     * The `wc-disable-block-editor-unified-assets` blog sticker force-disables the feature,
1165     * while the `wc-block-editor-unified-assets` sticker opts the site in.
1166     *
1167     * @since 6.11.0
1168     *
1169     * @param mixed $enabled The current feature option value.
1170     * @return mixed Original option value, `yes` when enabled, or `no` when force-disabled.
1171     */
1172    public static function enable_woocommerce_block_editor_unified_assets( $enabled ) {
1173        $blog_id = get_wpcom_blog_id();
1174
1175        if ( wpcom_has_blog_sticker( 'wc-disable-block-editor-unified-assets', $blog_id ) ) {
1176            return 'no';
1177        }
1178
1179        if ( wpcom_has_blog_sticker( 'wc-block-editor-unified-assets', $blog_id ) ) {
1180            return 'yes';
1181        }
1182
1183        // phpcs:disable Squiz.PHP.CommentedOutCode.Found,Squiz.Commenting.BlockComment.NoCapital -- Preserve the rollout for future activation.
1184
1185        /*
1186        if ( function_exists( 'wpcomsh_get_atomic_site_id' ) ) {
1187            $site_id = wpcomsh_get_atomic_site_id();
1188
1189            if ( $site_id ) {
1190                $current_segment = 1; // Segment of Atomic sites in the experiment, in %.
1191
1192                if ( $site_id % 100 < $current_segment ) {
1193                    return 'yes';
1194                }
1195            }
1196        }
1197        */
1198        // phpcs:enable
1199
1200        return $enabled;
1201    }
1202
1203    /**
1204     * Whether the site runs a Gutenberg plugin release that's known to break with React 19.
1205     *
1206     * Sites without the plugin run core's bundled Gutenberg, which the list doesn't cover.
1207     *
1208     * @return bool
1209     */
1210    private static function has_react_19_incompatible_gutenberg() {
1211        if ( ! defined( 'GUTENBERG_VERSION' ) ) {
1212            return false;
1213        }
1214
1215        // Match on the release number alone, so prereleases like 23.9.0-rc.1 count as 23.9.0.
1216        if ( ! preg_match( '/^(\d+(?:\.\d+)*)/', (string) GUTENBERG_VERSION, $matches ) ) {
1217            return false;
1218        }
1219
1220        return in_array( $matches[1], self::REACT_19_INCOMPATIBLE_GUTENBERG, true );
1221    }
1222
1223    /**
1224     * Add `gutenberg-extensible-site-editor` to the list of enabled Gutenberg experiments.
1225     *
1226     * Only registered on sites holding the sticker, so this does not re-check it.
1227     *
1228     * @param mixed $experiments The current value of the gutenberg-experiments option.
1229     * @return array The experiments, with the extensible site editor enabled.
1230     */
1231    public static function enable_extensible_site_editor_experiment( $experiments ) {
1232        if ( ! is_array( $experiments ) ) {
1233            $experiments = array();
1234        }
1235
1236        $experiments['gutenberg-extensible-site-editor'] = true;
1237        return $experiments;
1238    }
1239
1240    /**
1241     * Whether the site runs an extension that's known to break with React 19.
1242     *
1243     * @return bool
1244     */
1245    private static function has_react_19_incompatible_extension() {
1246        // Outside wp-admin the plugin check is unavailable, and the experiment isn't needed.
1247        if ( ! function_exists( 'is_plugin_active' ) ) {
1248            return true;
1249        }
1250
1251        $template = strtolower( get_template() );
1252        if ( array_key_exists( $template, self::REACT_19_INCOMPATIBLE_THEMES ) ) {
1253            $theme_version = (string) wp_get_theme( get_template() )->get( 'Version' );
1254
1255            if ( self::is_react_19_incompatible_version( $theme_version, self::REACT_19_INCOMPATIBLE_THEMES[ $template ] ) ) {
1256                return true;
1257            }
1258        }
1259
1260        foreach ( self::REACT_19_INCOMPATIBLE_PLUGINS as $plugin_file => $fixed_in ) {
1261            if ( ! is_plugin_active( $plugin_file ) ) {
1262                continue;
1263            }
1264
1265            $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file, false, false );
1266
1267            if ( self::is_react_19_incompatible_version( (string) $plugin_data['Version'], $fixed_in ) ) {
1268                return true;
1269            }
1270        }
1271
1272        return false;
1273    }
1274
1275    /**
1276     * Whether an installed version predates the release that fixed its React 19 incompatibility.
1277     *
1278     * An unreadable version counts as incompatible, so a missing header can't opt a site in.
1279     *
1280     * @param string      $version  The installed version.
1281     * @param string|null $fixed_in The release that fixed the incompatibility, or null if there is none.
1282     * @return bool
1283     */
1284    private static function is_react_19_incompatible_version( $version, $fixed_in ) {
1285        if ( null === $fixed_in || '' === $version ) {
1286            return true;
1287        }
1288
1289        return version_compare( $version, $fixed_in, '<' );
1290    }
1291
1292    /**
1293     * Add Jetpack script data with host information on P2
1294     *
1295     * @param array $data - The Jetpack script data.
1296     * @return array - The modified Jetpack script data.
1297     */
1298    public static function add_jetpack_script_data_for_p2( $data ) {
1299        if (
1300        str_contains( get_stylesheet(), 'pub/p2' ) ||
1301        ( function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( get_current_blog_id() ) )
1302        ) {
1303            $host = new \Automattic\Jetpack\Status\Host();
1304            if ( ! isset( $data['site']['host'] ) ) {
1305                $data['site']['host'] = $host->get_known_host_guess();
1306            }
1307            if ( ! isset( $data['site']['is_wpcom_platform'] ) ) {
1308                $data['site']['is_wpcom_platform'] = $host->is_wpcom_platform();
1309            }
1310        }
1311        return $data;
1312    }
1313
1314    /**
1315     * Emit an event to the wpcom logstash cluster.
1316     *
1317     * Uses the in-process `log2logstash()` on WP.com Simple, and falls back to
1318     * the public-api `/rest/v1.1/logstash` endpoint (fire-and-forget) on
1319     * Atomic, where `log2logstash()` isn't available.
1320     *
1321     * Best-effort: a logging failure must never escalate into a fatal for the caller.
1322     *
1323     * @param string $feature Logstash `feature` bucket; should start with the `atomic_` prefix (e.g. "atomic_plugin_conflicts_guardian").
1324     * @param string $message Event message slug.
1325     * @param array  $extra   Event-specific properties; JSON-encoded into the `extra` field.
1326     * @return void
1327     */
1328    public static function log2logstash( $feature, $message, array $extra = array() ) {
1329        // Resolve the dispatch path once per request — on upgrade flows this
1330        // can be called several times in a row (one per plugin) and the path
1331        // doesn't change mid-request.
1332        static $dispatch = null;
1333        if ( null === $dispatch ) {
1334            try {
1335                if ( ! function_exists( 'log2logstash' ) ) {
1336                    $log2logstash_path = WP_CONTENT_DIR . '/lib/log2logstash/log2logstash.php';
1337                    if ( is_readable( $log2logstash_path ) ) {
1338                        require_once $log2logstash_path;
1339                    }
1340                }
1341            } catch ( \Throwable $e ) { // require_once can still throw (parse error / top-level fatal in the included file); fall through to the HTTP dispatch.
1342                unset( $e );
1343            }
1344            $dispatch = function_exists( 'log2logstash' ) ? 'native' : 'http';
1345        }
1346
1347        try {
1348            $payload = array(
1349                'blog_id' => self::resolve_logstash_blog_id(),
1350                'feature' => (string) $feature,
1351                'message' => (string) $message,
1352                'extra'   => wp_json_encode( $extra, JSON_UNESCAPED_SLASHES ),
1353            );
1354
1355            if ( 'native' === $dispatch ) {
1356                log2logstash( $payload );
1357                return;
1358            }
1359
1360            // Defer the HTTP POST to shutdown. Dispatching inline as a
1361            // non-blocking request loses the event when the caller `exit`s
1362            // or `wp_safe_redirect`s right after (e.g. the activation-guard
1363            // block path), because the cURL handle is torn down before the
1364            // TLS handshake completes. Draining at shutdown with a blocking
1365            // POST guarantees delivery without adding latency to the
1366            // user-visible response.
1367            self::queue_logstash_http( $payload );
1368        } catch ( \Throwable $e ) { // best-effort: a logging failure must never escalate into a fatal for the caller.
1369            unset( $e );
1370        }
1371    }
1372
1373    /**
1374     * Resolve the WP.com blog ID for a logstash record.
1375     *
1376     * `get_wpcom_blog_id()` falls back to `get_current_blog_id()` on Atomic
1377     * when the WP.com blog ID isn't readable — that returns `1` on a
1378     * single-site install, which is a valid-looking but wrong WP.com blog ID
1379     * and makes log records impossible to attribute. Emit `0` instead when
1380     * the real WP.com blog ID is unknown, so the gap is obvious in Kibana.
1381     *
1382     * @return int WP.com blog ID, or 0 when it can't be determined.
1383     */
1384    private static function resolve_logstash_blog_id() {
1385        // WP.com Simple: the current blog ID *is* the WP.com blog ID.
1386        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1387            return (int) get_current_blog_id();
1388        }
1389        // Atomic / connected Jetpack: `Jetpack_Options` reads the platform's
1390        // external storage, then the `jetpack_options` row. It returns false
1391        // when both lack the ID, and the cast gives 0 — never the local blog ID.
1392        if ( class_exists( 'Jetpack_Options' ) ) {
1393            return (int) \Jetpack_Options::get_option( 'id' );
1394        }
1395        $jetpack_options = get_option( 'jetpack_options' );
1396        if ( is_array( $jetpack_options ) && ! empty( $jetpack_options['id'] ) ) {
1397            return (int) $jetpack_options['id'];
1398        }
1399        return 0;
1400    }
1401
1402    /**
1403     * Append a logstash payload to the shutdown drain queue, registering
1404     * the drain hook on first enqueue. See `log2logstash()` for why
1405     * dispatch is deferred.
1406     *
1407     * @param array $payload Logstash record (`blog_id`, `feature`, `message`, `extra`).
1408     * @return void
1409     */
1410    private static function queue_logstash_http( array $payload ) {
1411        static $queue = null;
1412        if ( null === $queue ) {
1413            $queue = array();
1414            register_shutdown_function(
1415                static function () use ( &$queue ) {
1416                    foreach ( $queue as $entry ) {
1417                        try {
1418                            wp_remote_post(
1419                                'https://public-api.wordpress.com/rest/v1.1/logstash',
1420                                array(
1421                                    'body'    => array( 'params' => wp_json_encode( $entry, JSON_UNESCAPED_SLASHES ) ),
1422                                    'timeout' => 5,
1423                                )
1424                            );
1425                        } catch ( \Throwable $e ) { // best-effort: a logging failure must never escalate into a fatal at shutdown.
1426                            unset( $e );
1427                        }
1428                    }
1429                    $queue = array();
1430                }
1431            );
1432        }
1433        $queue[] = $payload;
1434    }
1435}