Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
7.02% covered (danger)
7.02%
29 / 413
3.12% covered (danger)
3.12%
1 / 32
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Mu_Wpcom
7.04% covered (danger)
7.04%
29 / 412
3.12% covered (danger)
3.12%
1 / 32
15660.52
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
56
 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 / 47
0.00% covered (danger)
0.00%
0 / 1
12
 load_wpcom_user_features
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
72
 load_wpcom_sites_features
0.00% covered (danger)
0.00%
0 / 5
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
 load_verbum_comments
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 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_simple_odyssey_stats
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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_classic_block_deprecation_experiment
0.00% covered (danger)
0.00%
0 / 6
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
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
5.93
 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.10.1';
18    const PKG_DIR         = __DIR__ . '/../';
19    const BASE_DIR        = __DIR__ . '/';
20    const BASE_FILE       = __FILE__;
21
22    /**
23     * Initialize the class.
24     */
25    public static function init() {
26        if ( did_action( 'jetpack_mu_wpcom_initialized' ) ) {
27            return;
28        }
29
30        // Shared code for src/features.
31        require_once self::PKG_DIR . 'src/common/index.php'; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
32        require_once __DIR__ . '/common/fatal-error-signature.php';
33        require_once __DIR__ . '/utils.php';
34
35        // Load features that don't need any special loading considerations.
36        add_action( 'plugins_loaded', array( __CLASS__, 'load_features' ) );
37
38        // Load features that only apply to WordPress.com-connected users.
39        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_user_features' ) );
40        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features' ) );
41
42        // Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
43        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_sites_features' ) );
44
45        // Load ETK features flag to turn off the features in the ETK plugin.
46        // It needs higher priority than the ETK plugin.
47        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features_flags' ), 0 );
48
49        /*
50         * Please double-check whether you really need to load your feature separately.
51         * Chances are you can just add it to the `load_features` method.
52         */
53        add_action( 'plugins_loaded', array( __CLASS__, 'load_launchpad' ), 0 );
54        add_action( 'plugins_loaded', array( __CLASS__, 'load_coming_soon' ) );
55        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_rest_api_endpoints' ) );
56        add_action( 'plugins_loaded', array( __CLASS__, 'load_newspack_blocks' ) );
57
58        // These features run only on simple sites.
59        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
60            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_comments' ) );
61            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_moderate' ) );
62            add_action( 'wp_loaded', array( __CLASS__, 'load_verbum_comments_admin' ) );
63            add_action( 'admin_menu', array( __CLASS__, 'load_wpcom_simple_odyssey_stats' ) );
64            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_random_redirect' ) );
65        }
66
67        // These features run only on atomic sites.
68        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
69            add_action( 'plugins_loaded', array( __CLASS__, 'load_custom_css' ) );
70            add_action( 'init', array( __CLASS__, 'schedule_translation_updates' ) );
71        }
72
73        // Unified navigation fix for changes in WordPress 6.2.
74        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'unbind_focusout_on_wp_admin_bar_menu_toggle' ) );
75
76        // Load the Map block settings.
77        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_jetpack_mu_wpcom_settings' ), 999 );
78
79        // Load the Map block settings.
80        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_map_block_settings' ), 999 );
81
82        // Load the Newsletter category settings.
83        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_newsletter_categories_settings' ), 999 );
84
85        // Load the Social Links feature.
86        add_action( 'init', array( __CLASS__, 'load_social_links' ), 30 );
87
88        // Filter to ensure JetpackScriptData.site.host and is_wpcom_platform is set, to ensure Jetpack blocks work as expected via P2.
89        add_filter( 'jetpack_public_js_script_data', array( __CLASS__, 'add_jetpack_script_data_for_p2' ), 10, 1 );
90
91        // Filter to populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
92        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_wpcom_blog_id_script_data' ), 10, 1 );
93
94        // Allow sites with the `classic-block-inserter-support` blog sticker to insert the Classic block.
95        if ( wpcom_has_blog_sticker( 'classic-block-inserter-support', get_wpcom_blog_id() ) ) {
96            add_filter( 'wp_classic_block_supports_inserter', '__return_true' );
97        }
98
99        // Enable the `gutenberg-classic-block-deprecation` Gutenberg experiment for all sites, with an opt-out via the `disable-classic-block-deprecation` blog sticker.
100        // Both filters are needed: `default_option_` fires when the option doesn't exist in the DB, `option_` fires when it does.
101        add_filter( 'option_gutenberg-experiments', array( __CLASS__, 'enable_gutenberg_classic_block_deprecation_experiment' ) );
102        add_filter( 'default_option_gutenberg-experiments', array( __CLASS__, 'enable_gutenberg_classic_block_deprecation_experiment' ) );
103
104        /**
105         * Runs right after the Jetpack_Mu_Wpcom package is initialized.
106         *
107         * @since 0.1.2
108         */
109        do_action( 'jetpack_mu_wpcom_initialized' );
110    }
111
112    /**
113     * Schedules translation updates for Jetpack MU WPCOM.
114     *
115     * This function sets up the necessary cron jobs to ensure that translation files
116     * are regularly updated.
117     *
118     * @return void
119     */
120    public static function schedule_translation_updates() {
121        add_action( 'wpcomsh_translation_update', array( __CLASS__, 'maybe_update_translations' ) );
122
123        if ( ! wp_next_scheduled( 'wpcomsh_translation_update' ) ) {
124            wp_schedule_event( time(), 'twicedaily', 'wpcomsh_translation_update' );
125        }
126    }
127
128    /**
129     * Fetches and installs Jetpack-mu-wpcom package translations when needed.
130     */
131    public static function maybe_update_translations() {
132        global $wp_filesystem;
133        if ( ! $wp_filesystem ) {
134            require_once ABSPATH . 'wp-admin/includes/file.php';
135            WP_Filesystem();
136        }
137
138        $locales = self::get_all_active_locales();
139        if ( empty( $locales ) ) {
140            return;
141        }
142
143        $plugins_request_data              = array();
144        $plugin_language_pack_destinations = array(
145            'jetpack-mu-wpcom' => WP_LANG_DIR . '/mu-plugins/',
146            'wpcomsh'          => WP_LANG_DIR . '/mu-plugins/',
147        );
148
149        foreach ( array_keys( $plugin_language_pack_destinations ) as $plugin_slug ) {
150            $plugins_request_data[ $plugin_slug ] = array( 'version' => 'latest' );
151        }
152
153        $response = wp_remote_post(
154            'https://translate.wordpress.com/api/translations-updates/wpcom/plugins',
155            array(
156                'body'    => wp_json_encode(
157                    array(
158                        'locales' => $locales,
159                        'plugins' => $plugins_request_data,
160                    ),
161                    JSON_UNESCAPED_SLASHES
162                ),
163                'headers' => array( 'Content-Type' => 'application/json' ),
164                'timeout' => 10,
165            )
166        );
167
168        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
169            return;
170        }
171
172        $data = json_decode( wp_remote_retrieve_body( $response ), true );
173
174        // API error, api returned but something was wrong.
175        if ( array_key_exists( 'success', $data ) && false === $data['success'] ) {
176            return;
177        }
178
179        if ( ! is_array( $data ) || ! is_array( $data['data'] ) ) {
180            return;
181        }
182
183        foreach ( $data['data'] as $plugin_name => $language_packs ) {
184            if ( ! isset( $plugin_language_pack_destinations[ $plugin_name ] ) ) {
185                continue;
186            }
187
188            $destination = $plugin_language_pack_destinations[ $plugin_name ];
189
190            foreach ( $language_packs as $translation ) {
191                $locale        = $translation['wp_locale'] ?? '';
192                $package_url   = $translation['package'] ?? '';
193                $last_modified = $translation['last_modified'] ?? '';
194
195                if ( ! $locale || ! $package_url || ! $last_modified ) {
196                    continue;
197                }
198
199                $local_po_file = "{$destination}/$plugin_name-{$locale}.po";
200                if ( file_exists( $local_po_file ) ) {
201                    $local_po_data                       = wp_get_pomo_file_data( $local_po_file );
202                    $installed_translation_revision_time = new \DateTime( $local_po_data['PO-Revision-Date'] );
203                    $new_translation_revision_time       = new \DateTime( $last_modified );
204
205                    // Skip if translation language pack is not newer than what is installed already.
206                    if ( $new_translation_revision_time <= $installed_translation_revision_time ) {
207                        continue;
208                    }
209                }
210
211                $translation_zip_file = download_url( $package_url );
212                if ( is_wp_error( $translation_zip_file ) ) {
213                    continue;
214                }
215
216                static::clear_translation_destination( $destination, $plugin_name, $locale );
217
218                $unzip_result = unzip_file( $translation_zip_file, $destination );
219                if ( is_wp_error( $unzip_result ) ) {
220                    wp_delete_file( $translation_zip_file );
221                    continue;
222                }
223
224                wp_delete_file( $translation_zip_file );
225
226            }
227        }
228    }
229
230    /**
231     * Clears the translation destination by deleting existing translation files.
232     *
233     * @param string $local_destination The local destination path.
234     * @param string $plugin_slug The plugin slug.
235     * @param string $locale The locale.
236     */
237    public static function clear_translation_destination( $local_destination, $plugin_slug, $locale ) {
238        global $wp_filesystem;
239
240        if ( ! $wp_filesystem ) {
241            require_once ABSPATH . 'wp-admin/includes/file.php';
242            WP_Filesystem();
243        }
244
245        $files = array(
246            "{$local_destination}{$plugin_slug}-{$locale}.po",
247            "{$local_destination}{$plugin_slug}-{$locale}.mo",
248            "{$local_destination}{$plugin_slug}-{$locale}.l10n.php",
249        );
250
251        $json_files = glob( "{$local_destination}{$plugin_slug}-{$locale}-*.json" );
252        if ( $json_files ) {
253            $files = array_merge( $files, $json_files );
254        }
255
256        foreach ( $files as $file ) {
257            if ( $wp_filesystem->exists( $file ) ) {
258                $wp_filesystem->delete( $file );
259            }
260        }
261    }
262
263    /**
264     * Retrieves all active locales for the site.
265     */
266    public static function get_all_active_locales() {
267        $locales = array( get_locale() );
268
269        $available_languages = get_available_languages();
270        if ( ! empty( $available_languages ) ) {
271            $locales = array_merge( $locales, $available_languages );
272        }
273        return array_values( array_unique( $locales ) );
274    }
275
276    /**
277     * Load features that don't need any special loading considerations.
278     */
279    public static function load_features() {
280        \Automattic\Jetpack\ExPlat::init();
281
282        // Please keep the features in alphabetical order.
283        require_once __DIR__ . '/features/100-year-plan/enhanced-ownership.php';
284        require_once __DIR__ . '/features/100-year-plan/locked-mode.php';
285        require_once __DIR__ . '/features/admin-color-schemes/admin-color-schemes.php';
286        require_once __DIR__ . '/features/block-patterns/block-patterns.php';
287        require_once __DIR__ . '/features/blog-privacy/blog-privacy.php';
288        require_once __DIR__ . '/features/cloudflare-analytics/cloudflare-analytics.php';
289        require_once __DIR__ . '/features/code-editor/class-code-editor.php';
290        require_once __DIR__ . '/features/wpcom-blocks/code/class-code-block.php';
291        require_once __DIR__ . '/features/css-monkey-patches/index.php';
292        require_once __DIR__ . '/features/error-reporting/error-reporting.php';
293        require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
294        require_once __DIR__ . '/features/font-smoothing-antialiased/font-smoothing-antialiased.php';
295        require_once __DIR__ . '/features/google-analytics/google-analytics.php';
296        require_once __DIR__ . '/features/holiday-snow/class-holiday-snow.php';
297        require_once __DIR__ . '/features/launch-button/index.php';
298        require_once __DIR__ . '/features/logo-tool/logo-tool.php';
299        require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php';
300        require_once __DIR__ . '/features/media/heif-support.php';
301        require_once __DIR__ . '/features/post-categories/quick-actions.php';
302        require_once __DIR__ . '/features/post-like-from-email/post-like-from-email.php';
303        require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php';
304        require_once __DIR__ . '/features/wpcom-admin-dashboard/wpcom-admin-dashboard.php';
305        require_once __DIR__ . '/features/wpcom-attachment-pages/wpcom-attachment-pages.php';
306        require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php';
307        require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php';
308        require_once __DIR__ . '/features/wpcom-dashboard/class-wpcom-dashboard.php';
309        require_once __DIR__ . '/features/wpcom-hotfixes/wpcom-hotfixes.php';
310        require_once __DIR__ . '/features/wpcom-logout/wpcom-logout.php';
311        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';
312        require_once __DIR__ . '/features/wpcom-post-list/wpcom-post-types-tracking.php';
313        require_once __DIR__ . '/features/wpcom-widgets/wpcom-widgets.php';
314        require_once __DIR__ . '/features/wpcom-wpadmin-page-view/wpcom-wpadmin-page-view.php';
315
316        require_once __DIR__ . '/features/write/write.php';
317
318        /*
319         * Temporarily disable client-side media processing.
320         *
321         * Client-side media processing enables cross-origin isolation (COEP/COOP headers)
322         * which can break authenticated API requests. This should be removed once client-side
323         * media processing is compatible with Dotcom's infrastructure.
324         *
325         * @see gutenberg_set_up_cross_origin_isolation() in Gutenberg's lib/media/load.php
326         * @see https://a8c.slack.com/archives/CBTN58FTJ/p1771950744814189
327         */
328        add_filter( 'wp_client_side_media_processing_enabled', '__return_false' );
329
330        // Initializers, if needed.
331        $activity_log_event_class = 'Automattic\\Jetpack\\Sync\\Activity_Log_Event';
332        if ( class_exists( $activity_log_event_class ) ) {
333            $activity_log_event_class::init();
334        }
335
336        \Marketplace_Products_Updater::init();
337        \Automattic\Jetpack\Code_Editor::setup();
338        \Automattic\Jetpack\Code_Block::setup();
339        \Automattic\Jetpack\Classic_Theme_Helper\Main::init();
340        \Automattic\Jetpack\Classic_Theme_Helper\Featured_Content::setup();
341
342        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Holiday_Snow::init();
343        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Dashboard::init();
344
345        // Gets autoloaded from the Scheduled_Updates package.
346        if ( class_exists( 'Automattic\Jetpack\Scheduled_Updates' ) ) {
347            Scheduled_Updates::init();
348        }
349    }
350
351    /**
352     * Load features that only apply to WordPress.com-connected users.
353     */
354    public static function load_wpcom_user_features() {
355        // To avoid potential collisions with ETK.
356        if ( ! class_exists( 'A8C\FSE\Help_Center' ) ) {
357            require_once __DIR__ . '/features/help-center/class-help-center.php';
358        }
359
360        if ( ! is_wpcom_user() ) {
361            require_once __DIR__ . '/features/replace-site-visibility/hide-site-visibility.php';
362            return;
363        }
364        if ( ! class_exists( 'A8C\FSE\Agents_Manager' ) ) {
365            require_once __DIR__ . '/features/agents-manager/class-agents-manager.php';
366        }
367        if ( ! class_exists( 'A8C\FSE\Survicate' ) ) {
368            require_once __DIR__ . '/features/survicate/class-survicate.php';
369        }
370        require_once __DIR__ . '/features/ai-assistant-banner/ai-assistant-banner.php';
371        require_once __DIR__ . '/features/html-block-restricted-tags/html-block-restricted-tags.php';
372        require_once __DIR__ . '/features/marketing/marketing.php';
373        require_once __DIR__ . '/features/pages/pages.php';
374        require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
375        require_once __DIR__ . '/features/stats/stats.php';
376        require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
377        require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
378        require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
379        require_once __DIR__ . '/features/wpcom-colourlovers-deprecate/wpcom-colourlovers-deprecate.php';
380        require_once __DIR__ . '/features/wpcom-comments/wpcom-comments.php';
381        require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php';
382        require_once __DIR__ . '/features/wpcom-imports/wpcom-imports.php';
383        require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
384        require_once __DIR__ . '/features/wpcom-media/wpcom-media-url-upload.php';
385        require_once __DIR__ . '/features/wpcom-media/wpcom-export-media-files.php';
386        require_once __DIR__ . '/features/wpcom-options-general/options-general.php';
387        require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
388        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
389        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
390        require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
391        require_once __DIR__ . '/features/wpcom-smart-dictation/class-wpcom-smart-dictation.php';
392        require_once __DIR__ . '/features/wpcom-content-research/class-wpcom-content-research.php';
393        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-tracking.php';
394        require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
395        require_once __DIR__ . '/features/wpcom-user-edit/wpcom-user-edit.php';
396
397        // Initialize Newsletter Settings so hooks like the Reading page notice
398        // are registered on Simple sites (where load-jetpack.php doesn't run).
399        // Guarded with class_exists since mu-wpcom no longer composer-requires
400        // the jetpack-newsletter package: the class is provided by the standalone
401        // Jetpack plugin on Atomic, or by the wpcom platform's bundled Jetpack
402        // source on Simple.
403        if ( class_exists( '\Automattic\Jetpack\Newsletter\Settings' ) ) {
404            // @phan-suppress-next-line PhanUndeclaredClassMethod -- class_exists guarded above; provided by sibling autoloader.
405            \Automattic\Jetpack\Newsletter\Settings::init();
406        }
407
408        // Only load the Masterbar features on WoA sites.
409        if ( class_exists( '\Automattic\Jetpack\Status\Host' ) && ( new \Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
410            // This is temporary. After we cleanup Masterbar on WPCOM we should load Masterbar for Simple sites too.
411            \Automattic\Jetpack\Masterbar\Main::init();
412        }
413    }
414
415    /**
416     * Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
417     */
418    public static function load_wpcom_sites_features() {
419        if ( is_fully_managed_agency_site() ) {
420            return;
421        }
422
423        require_once __DIR__ . '/features/gutenberg-rtc/gutenberg-rtc.php';
424        require_once __DIR__ . '/features/wpcom-contact-form-flags/wpcom-contact-form-flags.php';
425
426        // Initialize the Podcast package here (rather than in
427        // load_wpcom_user_features) so feed-customization hooks register
428        // for anonymous requests too — Apple Podcasts / Spotify crawlers
429        // aren't logged in. Podcast::init() gates itself on host
430        // (Simple/WoA) and `jetpack_podcast_untangle`, so the legacy
431        // podcasting code keeps running until the flag flips.
432        \Automattic\Jetpack\Podcast\Podcast::init();
433    }
434
435    /**
436     * Define the flags to turn off features in the ETK plugin.
437     * Can be removed once the feature no longer exists in the ETK plugin.
438     */
439    public static function load_etk_features_flags() {
440        // Don't load on agency sites.
441        if ( is_fully_managed_agency_site() ) {
442            return;
443        }
444
445        // Don't load if the user is not a wpcom user on WP Admin.
446        // The features is still required on the frontend page regardless of the user.
447        if ( is_admin() && ! is_wpcom_user() ) {
448            return;
449        }
450
451        define( 'MU_WPCOM_COBLOCKS_GALLERY', true );
452        define( 'MU_WPCOM_CUSTOM_LINE_HEIGHT', true );
453        define( 'MU_WPCOM_BLOCK_INSERTER_MODIFICATIONS', true );
454        define( 'MU_WPCOM_HOMEPAGE_TITLE_HIDDEN', true );
455        define( 'MU_WPCOM_JETPACK_GLOBAL_STYLES', true );
456        define( 'A8C_USE_FONT_SMOOTHING_ANTIALIASED', false );
457        define( 'MU_WPCOM_MAILERLITE_WIDGET', true );
458        define( 'MU_WPCOM_OVERRIDE_PREVIEW_BUTTON_URL', true );
459        define( 'MU_WPCOM_PARAGRAPH_BLOCK', true );
460        define( 'MU_WPCOM_STARTER_PAGE_TEMPLATES', true );
461        define( 'MU_WPCOM_TAGS_EDUCATION', true );
462        define( 'MU_WPCOM_BLOCK_DESCRIPTION_LINKS', true );
463        define( 'MU_WPCOM_BLOCK_EDITOR_NUX', true );
464        define( 'MU_WPCOM_POSTS_LIST_BLOCK', true );
465        define( 'MU_WPCOM_JETPACK_COUNTDOWN_BLOCK', true );
466        define( 'MU_WPCOM_JETPACK_TIMELINE_BLOCK', true );
467        define( 'MU_WPCOM_DOCUMENTATION_LINKS', true );
468        define( 'MU_WPCOM_GLOBAL_STYLES', true );
469        define( 'MU_WPCOM_FSE', true );
470        define( 'MU_WPCOM_TEMPLATE_INSERTER', true );
471        define( 'MU_WPCOM_WHATS_NEW', true );
472    }
473
474    /**
475     * Load ETK features.
476     * Can be moved back to load_features() once the feature no longer exists in the ETK plugin.
477     */
478    public static function load_etk_features() {
479        // Don't load on agency sites.
480        if ( is_fully_managed_agency_site() ) {
481            return;
482        }
483
484        // Don't load if the user is not a wpcom user on WP Admin.
485        // The features is still required on the frontend page regardless of the user.
486        if ( is_admin() && ! is_wpcom_user() ) {
487            return;
488        }
489
490        require_once __DIR__ . '/features/jetpack-global-styles/class-global-styles.php';
491        require_once __DIR__ . '/features/mailerlite/subscriber-popup.php';
492        require_once __DIR__ . '/features/wpcom-fse/wpcom-fse.php';
493
494        /**
495         * Load features for the editor and the frontend pages.
496         */
497        global $pagenow;
498        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
499        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
500            require_once __DIR__ . '/features/block-editor/custom-line-height.php';
501            require_once __DIR__ . '/features/block-inserter-modifications/block-inserter-modifications.php';
502            require_once __DIR__ . '/features/hide-homepage-title/hide-homepage-title.php';
503            require_once __DIR__ . '/features/override-preview-button-url/override-preview-button-url.php';
504            require_once __DIR__ . '/features/paragraph-block-placeholder/paragraph-block-placeholder.php';
505            require_once __DIR__ . '/features/tags-education/tags-education.php';
506            require_once __DIR__ . '/features/wpcom-block-description-links/wpcom-block-description-links.php';
507            require_once __DIR__ . '/features/wpcom-block-editor-nux/class-wpcom-block-editor-nux.php';
508            require_once __DIR__ . '/features/wpcom-blocks/a8c-posts-list/a8c-posts-list.php';
509            require_once __DIR__ . '/features/wpcom-blocks/event-countdown/event-countdown.php';
510            require_once __DIR__ . '/features/wpcom-blocks/timeline/timeline.php';
511            require_once __DIR__ . '/features/wpcom-documentation-links/wpcom-documentation-links.php';
512            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
513            require_once __DIR__ . '/features/wpcom-legacy-fse/wpcom-legacy-fse.php';
514        } elseif ( isset( $pagenow ) && 'customize.php' === $pagenow ) {
515            // Load wpcom-global-styles on the customizer so access to additional css can be checked there.
516            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
517        }
518    }
519
520    /**
521     * Load the newspack blocks feature for the editor and the frontend pages.
522     */
523    public static function load_newspack_blocks() {
524        /**
525         * Avoid potential collisions with newspack-blocks plugin.
526         */
527        if ( class_exists( '\Newspack_Blocks', false ) ) {
528            return;
529        }
530
531        global $pagenow;
532        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
533        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
534            define( 'MU_WPCOM_NEWSPACK_BLOCKS', true );
535            require_once __DIR__ . '/features/newspack-blocks/index.php';
536        }
537    }
538
539    /**
540     * Load the Coming Soon feature.
541     */
542    public static function load_coming_soon() {
543        /**
544         * On WoA sites, users may be using non-symlinked older versions of the FSE plugin.
545         * If they are, check the active version to avoid redeclaration errors.
546         */
547        if ( ! function_exists( 'is_plugin_active' ) ) {
548            require_once ABSPATH . 'wp-admin/includes/plugin.php';
549        }
550
551        /**
552         * Explicitly pass $markup = false in get_plugin_data to avoid indirectly calling wptexturize that could cause unintended side effects.
553         * See: https://developer.wordpress.org/reference/functions/get_plugin_data/
554         */
555        $fse_plugin                 = 'full-site-editing/full-site-editing-plugin.php';
556        $fse_plugin_path            = WP_PLUGIN_DIR . '/' . $fse_plugin;
557        $invalid_fse_version_active =
558            file_exists( $fse_plugin_path ) &&
559            is_file( $fse_plugin_path ) &&
560            is_plugin_active( $fse_plugin ) &&
561            version_compare( get_plugin_data( $fse_plugin_path, false )['Version'], '3.56084', '<' );
562
563        if ( $invalid_fse_version_active ) {
564            return;
565        }
566
567        if (
568            ( defined( 'WPCOM_PUBLIC_COMING_SOON' ) && WPCOM_PUBLIC_COMING_SOON ) ||
569            apply_filters( 'a8c_enable_public_coming_soon', false )
570        ) {
571            require_once __DIR__ . '/features/coming-soon/coming-soon.php';
572        }
573    }
574
575    /**
576     * Load the Launchpad feature.
577     */
578    public static function load_launchpad() {
579        require_once __DIR__ . '/features/launchpad/launchpad.php';
580    }
581
582    /**
583     * Load WP REST API plugins for wpcom.
584     */
585    public static function load_wpcom_rest_api_endpoints() {
586        if ( ! function_exists( 'wpcom_rest_api_v2_load_plugin' ) ) {
587            return;
588        }
589
590        // We don't use `wpcom_rest_api_v2_load_plugin_files` because it operates inconsisently.
591        $plugins = glob( __DIR__ . '/features/wpcom-endpoints/*.php' );
592
593        if ( ! is_array( $plugins ) ) {
594            return;
595        }
596
597        foreach ( array_filter( $plugins, 'is_file' ) as $plugin ) {
598            require_once $plugin;
599        }
600    }
601
602    /**
603     * Adds a global variable containing the config of the plugin to the window object.
604     */
605    public static function load_jetpack_mu_wpcom_settings() {
606        $handle = 'jetpack-mu-wpcom-settings';
607
608        // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
609        wp_register_script(
610            $handle,
611            false,
612            array(),
613            true
614        );
615
616        $data = wp_json_encode(
617            array(
618                'assetsUrl' => plugins_url( 'build/', self::BASE_FILE ),
619            ),
620            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
621        );
622
623        wp_add_inline_script(
624            $handle,
625            "var JETPACK_MU_WPCOM_SETTINGS = $data;",
626            'before'
627        );
628
629        wp_enqueue_script( $handle );
630    }
631
632    /**
633     * Adds a global variable containing the map provider in a map_block_settings object to the window object.
634     */
635    public static function load_map_block_settings() {
636        if (
637            ! function_exists( 'get_current_screen' )
638            || \get_current_screen() === null
639        ) {
640            return;
641        }
642
643        // Return early if we are not in the block editor.
644        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
645            return;
646        }
647
648        $map_provider = apply_filters( 'wpcom_map_block_map_provider', 'mapbox' );
649        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Maps', array( 'provider' => $map_provider ) );
650    }
651
652    /**
653     * Adds a global variable containing where the newsletter categories should be shown.
654     */
655    public static function load_newsletter_categories_settings() {
656        if (
657            ! function_exists( 'get_current_screen' )
658            || \get_current_screen() === null
659        ) {
660            return;
661        }
662
663        // Return early if we are not in the block editor.
664        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
665            return;
666        }
667
668        $newsletter_categories_location = apply_filters( 'wpcom_newsletter_categories_location', 'block' );
669        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Subscriptions', array( 'newsletter_categories_location' => $newsletter_categories_location ) );
670    }
671
672    /**
673     * Unbinds focusout event handler on #wp-admin-bar-menu-toggle introduced in WordPress 6.2.
674     *
675     * The focusout event handler is preventing the unified navigation from being closed on mobile.
676     */
677    public static function unbind_focusout_on_wp_admin_bar_menu_toggle() {
678        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) );' );
679    }
680
681    /**
682     * Determine whether to disable the comment experience.
683     *
684     * @param int $blog_id The blog ID.
685     * @return boolean
686     */
687    private static function should_disable_comment_experience( $blog_id ) {
688        $path_wp_for_teams = WP_CONTENT_DIR . '/lib/wpforteams/functions.php';
689
690        if ( file_exists( $path_wp_for_teams ) ) {
691            require_once $path_wp_for_teams;
692        }
693
694        // This covers both P2 and P2020 themes.
695        $is_p2     = str_contains( get_stylesheet(), 'pub/p2' ) || function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( $blog_id );
696        $is_forums = str_contains( get_stylesheet(), 'a8c/supportforums' ); // Not in /forums.
697
698        $verbum_option_enabled = get_blog_option( $blog_id, 'enable_verbum_commenting', true );
699
700        if ( empty( $verbum_option_enabled ) ) {
701            return true;
702        }
703
704        // Don't load any comment experience in the Reader, GlotPress, wp-admin, or P2.
705        return ( 1 === $blog_id || TRANSLATE_BLOG_ID === $blog_id || is_admin() || $is_p2 || $is_forums );
706    }
707
708    /**
709     * Load Verbum Comments.
710     */
711    public static function load_verbum_comments() {
712        if ( class_exists( 'Verbum_Comments' ) ) {
713            return;
714        } else {
715            $blog_id = get_current_blog_id();
716            // Jetpack loads Verbum though an iframe from jetpack.wordpress.com.
717            // So we need to check the GET request for the blogid.
718            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
719            if ( isset( $_GET['blogid'] ) ) {
720                $blog_id = intval( $_GET['blogid'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
721            }
722            if ( self::should_disable_comment_experience( $blog_id ) ) {
723                return;
724            }
725            require_once __DIR__ . '/features/verbum-comments/class-verbum-comments.php';
726            new \Automattic\Jetpack\Verbum_Comments();
727        }
728    }
729
730    /**
731     * Load Verbum Comments Settings.
732     */
733    public static function load_verbum_comments_admin() {
734        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-admin.php';
735        new \Automattic\Jetpack\Verbum_Admin();
736    }
737
738    /**
739     * Load Verbum Moderate.
740     */
741    public static function load_verbum_moderate() {
742        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-moderate.php';
743        new \Automattic\Jetpack\Verbum_Moderate();
744    }
745
746    /**
747     * Load Odyssey Stats in Simple sites.
748     */
749    public static function load_wpcom_simple_odyssey_stats() {
750        require_once __DIR__ . '/features/wpcom-simple-odyssey-stats/wpcom-simple-odyssey-stats.php';
751    }
752
753    /**
754     * Load the Jetpack Custom CSS feature.
755     */
756    public static function load_custom_css() {
757        require_once __DIR__ . '/features/custom-css/custom-css/preprocessors.php';
758        require_once __DIR__ . '/features/custom-css/custom-css.php';
759    }
760
761    /**
762     * Load the Random Redirect feature.
763     */
764    public static function load_wpcom_random_redirect() {
765        require_once __DIR__ . '/features/random-redirect/random-redirect.php';
766    }
767
768    /**
769     * Load the Social Links feature.
770     */
771    public static function load_social_links() {
772        if ( class_exists( 'Automattic\Jetpack\Classic_Theme_Helper\Social_Links' ) ) {
773            new \Automattic\Jetpack\Classic_Theme_Helper\Social_Links();
774        }
775    }
776
777    /**
778     * Populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
779     *
780     * @param array $data The script data.
781     * @return array
782     */
783    public static function set_wpcom_blog_id_script_data( $data ) {
784        $blog_id = get_wpcom_blog_id();
785        if ( $blog_id ) {
786            $data['site']['wpcom']['blog_id'] = $blog_id;
787        }
788        return $data;
789    }
790
791    /**
792     * Add `gutenberg-classic-block-deprecation` to the list of enabled Gutenberg experiments.
793     * Skip sites that have the `disable-classic-block-deprecation` sticker enabled.
794     *
795     * @param mixed $experiments The current value of the gutenberg-experiments option.
796     * @return mixed Original option value or the filtered experiments.
797     */
798    public static function enable_gutenberg_classic_block_deprecation_experiment( $experiments ) {
799        if ( wpcom_has_blog_sticker( 'disable-classic-block-deprecation', get_wpcom_blog_id() ) ) {
800            return $experiments;
801        }
802
803        if ( ! is_array( $experiments ) ) {
804            $experiments = array();
805        }
806
807        $experiments['gutenberg-classic-block-deprecation'] = true;
808        return $experiments;
809    }
810
811    /**
812     * Add Jetpack script data with host information on P2
813     *
814     * @param array $data - The Jetpack script data.
815     * @return array - The modified Jetpack script data.
816     */
817    public static function add_jetpack_script_data_for_p2( $data ) {
818        if (
819        str_contains( get_stylesheet(), 'pub/p2' ) ||
820        ( function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( get_current_blog_id() ) )
821        ) {
822            $host = new \Automattic\Jetpack\Status\Host();
823            if ( ! isset( $data['site']['host'] ) ) {
824                $data['site']['host'] = $host->get_known_host_guess();
825            }
826            if ( ! isset( $data['site']['is_wpcom_platform'] ) ) {
827                $data['site']['is_wpcom_platform'] = $host->is_wpcom_platform();
828            }
829        }
830        return $data;
831    }
832
833    /**
834     * Emit an event to the wpcom logstash cluster.
835     *
836     * Uses the in-process `log2logstash()` on WP.com Simple, and falls back to
837     * the public-api `/rest/v1.1/logstash` endpoint (fire-and-forget) on
838     * Atomic, where `log2logstash()` isn't available.
839     *
840     * Best-effort: a logging failure must never escalate into a fatal for the caller.
841     *
842     * @param string $feature Logstash `feature` bucket; should start with the `atomic_` prefix (e.g. "atomic_plugin_conflicts_guardian").
843     * @param string $message Event message slug.
844     * @param array  $extra   Event-specific properties; JSON-encoded into the `extra` field.
845     * @return void
846     */
847    public static function log2logstash( $feature, $message, array $extra = array() ) {
848        // Resolve the dispatch path once per request — on upgrade flows this
849        // can be called several times in a row (one per plugin) and the path
850        // doesn't change mid-request.
851        static $dispatch = null;
852        if ( null === $dispatch ) {
853            try {
854                if ( ! function_exists( 'log2logstash' ) ) {
855                    $log2logstash_path = WP_CONTENT_DIR . '/lib/log2logstash/log2logstash.php';
856                    if ( is_readable( $log2logstash_path ) ) {
857                        require_once $log2logstash_path;
858                    }
859                }
860            } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- require_once can still throw (parse error / top-level fatal in the included file); fall through to the HTTP dispatch.
861                unset( $e );
862            }
863            $dispatch = function_exists( 'log2logstash' ) ? 'native' : 'http';
864        }
865
866        try {
867            $payload = array(
868                'blog_id' => self::resolve_logstash_blog_id(),
869                'feature' => (string) $feature,
870                'message' => (string) $message,
871                'extra'   => wp_json_encode( $extra, JSON_UNESCAPED_SLASHES ),
872            );
873
874            if ( 'native' === $dispatch ) {
875                log2logstash( $payload );
876                return;
877            }
878
879            // Defer the HTTP POST to shutdown. Dispatching inline as a
880            // non-blocking request loses the event when the caller `exit`s
881            // or `wp_safe_redirect`s right after (e.g. the activation-guard
882            // block path), because the cURL handle is torn down before the
883            // TLS handshake completes. Draining at shutdown with a blocking
884            // POST guarantees delivery without adding latency to the
885            // user-visible response.
886            self::queue_logstash_http( $payload );
887        } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- best-effort: a logging failure must never escalate into a fatal for the caller.
888            unset( $e );
889        }
890    }
891
892    /**
893     * Resolve the WP.com blog ID for a logstash record.
894     *
895     * `get_wpcom_blog_id()` falls back to `get_current_blog_id()` on Atomic
896     * when `jetpack_options['id']` isn't readable — that returns `1` on a
897     * single-site install, which is a valid-looking but wrong WP.com blog ID
898     * and makes log records impossible to attribute. Emit `0` instead when
899     * the real WP.com blog ID is unknown, so the gap is obvious in Kibana.
900     *
901     * @return int WP.com blog ID, or 0 when it can't be determined.
902     */
903    private static function resolve_logstash_blog_id() {
904        // WP.com Simple: the current blog ID *is* the WP.com blog ID.
905        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
906            return (int) get_current_blog_id();
907        }
908        // Atomic / connected Jetpack: the WP.com blog ID lives in the
909        // `jetpack_options` option. Read it directly (no `Jetpack_Options`
910        // dependency) and return 0 — never the local blog ID — when absent.
911        $jetpack_options = get_option( 'jetpack_options' );
912        if ( is_array( $jetpack_options ) && ! empty( $jetpack_options['id'] ) ) {
913            return (int) $jetpack_options['id'];
914        }
915        return 0;
916    }
917
918    /**
919     * Append a logstash payload to the shutdown drain queue, registering
920     * the drain hook on first enqueue. See `log2logstash()` for why
921     * dispatch is deferred.
922     *
923     * @param array $payload Logstash record (`blog_id`, `feature`, `message`, `extra`).
924     * @return void
925     */
926    private static function queue_logstash_http( array $payload ) {
927        static $queue = null;
928        if ( null === $queue ) {
929            $queue = array();
930            register_shutdown_function(
931                static function () use ( &$queue ) {
932                    foreach ( $queue as $entry ) {
933                        try {
934                            wp_remote_post(
935                                'https://public-api.wordpress.com/rest/v1.1/logstash',
936                                array(
937                                    'body'    => array( 'params' => wp_json_encode( $entry, JSON_UNESCAPED_SLASHES ) ),
938                                    'timeout' => 5,
939                                )
940                            );
941                        } catch ( \Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- best-effort: a logging failure must never escalate into a fatal at shutdown.
942                            unset( $e );
943                        }
944                    }
945                    $queue = array();
946                }
947            );
948        }
949        $queue[] = $payload;
950    }
951}