Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 353
0.00% covered (danger)
0.00%
0 / 28
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Mu_Wpcom
0.00% covered (danger)
0.00%
0 / 352
0.00% covered (danger)
0.00%
0 / 28
13572
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
42
 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 / 61
0.00% covered (danger)
0.00%
0 / 1
420
 clear_translation_destination
0.00% covered (danger)
0.00%
0 / 15
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 / 43
0.00% covered (danger)
0.00%
0 / 1
6
 load_wpcom_user_features
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
56
 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 / 26
0.00% covered (danger)
0.00%
0 / 1
90
 load_newspack_blocks
0.00% covered (danger)
0.00%
0 / 7
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
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 set_wpcom_blog_id_script_data
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 add_jetpack_script_data_for_p2
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
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__ . '/utils.php';
33
34        // Load features that don't need any special loading considerations.
35        add_action( 'plugins_loaded', array( __CLASS__, 'load_features' ) );
36
37        // Load features that only apply to WordPress.com-connected users.
38        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_user_features' ) );
39        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features' ) );
40
41        // Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
42        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_sites_features' ) );
43
44        // Load ETK features flag to turn off the features in the ETK plugin.
45        // It needs higher priority than the ETK plugin.
46        add_action( 'plugins_loaded', array( __CLASS__, 'load_etk_features_flags' ), 0 );
47
48        /*
49         * Please double-check whether you really need to load your feature separately.
50         * Chances are you can just add it to the `load_features` method.
51         */
52        add_action( 'plugins_loaded', array( __CLASS__, 'load_launchpad' ), 0 );
53        add_action( 'plugins_loaded', array( __CLASS__, 'load_coming_soon' ) );
54        add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_rest_api_endpoints' ) );
55        add_action( 'plugins_loaded', array( __CLASS__, 'load_newspack_blocks' ) );
56
57        // These features run only on simple sites.
58        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
59            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_comments' ) );
60            add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_moderate' ) );
61            add_action( 'wp_loaded', array( __CLASS__, 'load_verbum_comments_admin' ) );
62            add_action( 'admin_menu', array( __CLASS__, 'load_wpcom_simple_odyssey_stats' ) );
63            add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_random_redirect' ) );
64        }
65
66        // These features run only on atomic sites.
67        if ( defined( 'IS_ATOMIC' ) && IS_ATOMIC ) {
68            add_action( 'plugins_loaded', array( __CLASS__, 'load_custom_css' ) );
69            add_action( 'init', array( __CLASS__, 'schedule_translation_updates' ) );
70        }
71
72        // Unified navigation fix for changes in WordPress 6.2.
73        add_action( 'admin_enqueue_scripts', array( __CLASS__, 'unbind_focusout_on_wp_admin_bar_menu_toggle' ) );
74
75        // Load the Map block settings.
76        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_jetpack_mu_wpcom_settings' ), 999 );
77
78        // Load the Map block settings.
79        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_map_block_settings' ), 999 );
80
81        // Load the Newsletter category settings.
82        add_action( 'enqueue_block_assets', array( __CLASS__, 'load_newsletter_categories_settings' ), 999 );
83
84        // Load the Social Links feature.
85        add_action( 'init', array( __CLASS__, 'load_social_links' ), 30 );
86
87        // Filter to ensure JetpackScriptData.site.host and is_wpcom_platform is set, to ensure Jetpack blocks work as expected via P2.
88        add_filter( 'jetpack_public_js_script_data', array( __CLASS__, 'add_jetpack_script_data_for_p2' ), 10, 1 );
89
90        // Filter to populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
91        add_filter( 'jetpack_admin_js_script_data', array( __CLASS__, 'set_wpcom_blog_id_script_data' ), 10, 1 );
92
93        /**
94         * Runs right after the Jetpack_Mu_Wpcom package is initialized.
95         *
96         * @since 0.1.2
97         */
98        do_action( 'jetpack_mu_wpcom_initialized' );
99    }
100
101    /**
102     * Schedules translation updates for Jetpack MU WPCOM.
103     *
104     * This function sets up the necessary cron jobs to ensure that translation files
105     * are regularly updated.
106     *
107     * @return void
108     */
109    public static function schedule_translation_updates() {
110        add_action( 'wpcomsh_translation_update', array( __CLASS__, 'maybe_update_translations' ) );
111
112        if ( ! wp_next_scheduled( 'wpcomsh_translation_update' ) ) {
113            wp_schedule_event( time(), 'twicedaily', 'wpcomsh_translation_update' );
114        }
115    }
116
117    /**
118     * Fetches and installs Jetpack-mu-wpcom package translations when needed.
119     */
120    public static function maybe_update_translations() {
121        global $wp_filesystem;
122        if ( ! $wp_filesystem ) {
123            require_once ABSPATH . 'wp-admin/includes/file.php';
124            WP_Filesystem();
125        }
126
127        $locales = self::get_all_active_locales();
128        if ( empty( $locales ) ) {
129            return;
130        }
131
132        $plugins_request_data              = array();
133        $plugin_language_pack_destinations = array(
134            'jetpack-mu-wpcom' => WP_LANG_DIR . '/mu-plugins/',
135            'wpcomsh'          => WP_LANG_DIR . '/mu-plugins/',
136        );
137
138        foreach ( array_keys( $plugin_language_pack_destinations ) as $plugin_slug ) {
139            $plugins_request_data[ $plugin_slug ] = array( 'version' => 'latest' );
140        }
141
142        $response = wp_remote_post(
143            'https://translate.wordpress.com/api/translations-updates/wpcom/plugins',
144            array(
145                'body'    => wp_json_encode(
146                    array(
147                        'locales' => $locales,
148                        'plugins' => $plugins_request_data,
149                    ),
150                    JSON_UNESCAPED_SLASHES
151                ),
152                'headers' => array( 'Content-Type' => 'application/json' ),
153                'timeout' => 10,
154            )
155        );
156
157        if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
158            return;
159        }
160
161        $data = json_decode( wp_remote_retrieve_body( $response ), true );
162
163        // API error, api returned but something was wrong.
164        if ( array_key_exists( 'success', $data ) && false === $data['success'] ) {
165            return;
166        }
167
168        if ( ! is_array( $data ) || ! is_array( $data['data'] ) ) {
169            return;
170        }
171
172        foreach ( $data['data'] as $plugin_name => $language_packs ) {
173            if ( ! isset( $plugin_language_pack_destinations[ $plugin_name ] ) ) {
174                continue;
175            }
176
177            $destination = $plugin_language_pack_destinations[ $plugin_name ];
178
179            foreach ( $language_packs as $translation ) {
180                $locale        = $translation['wp_locale'] ?? '';
181                $package_url   = $translation['package'] ?? '';
182                $last_modified = $translation['last_modified'] ?? '';
183
184                if ( ! $locale || ! $package_url || ! $last_modified ) {
185                    continue;
186                }
187
188                $local_po_file = "{$destination}/$plugin_name-{$locale}.po";
189                if ( file_exists( $local_po_file ) ) {
190                    $local_po_data                       = wp_get_pomo_file_data( $local_po_file );
191                    $installed_translation_revision_time = new \DateTime( $local_po_data['PO-Revision-Date'] );
192                    $new_translation_revision_time       = new \DateTime( $last_modified );
193
194                    // Skip if translation language pack is not newer than what is installed already.
195                    if ( $new_translation_revision_time <= $installed_translation_revision_time ) {
196                        continue;
197                    }
198                }
199
200                $translation_zip_file = download_url( $package_url );
201                if ( is_wp_error( $translation_zip_file ) ) {
202                    continue;
203                }
204
205                static::clear_translation_destination( $destination, $plugin_name, $locale );
206
207                $unzip_result = unzip_file( $translation_zip_file, $destination );
208                if ( is_wp_error( $unzip_result ) ) {
209                    wp_delete_file( $translation_zip_file );
210                    continue;
211                }
212
213                wp_delete_file( $translation_zip_file );
214
215            }
216        }
217    }
218
219    /**
220     * Clears the translation destination by deleting existing translation files.
221     *
222     * @param string $local_destination The local destination path.
223     * @param string $plugin_slug The plugin slug.
224     * @param string $locale The locale.
225     */
226    public static function clear_translation_destination( $local_destination, $plugin_slug, $locale ) {
227        global $wp_filesystem;
228
229        if ( ! $wp_filesystem ) {
230            require_once ABSPATH . 'wp-admin/includes/file.php';
231            WP_Filesystem();
232        }
233
234        $files = array(
235            "{$local_destination}{$plugin_slug}-{$locale}.po",
236            "{$local_destination}{$plugin_slug}-{$locale}.mo",
237            "{$local_destination}{$plugin_slug}-{$locale}.l10n.php",
238        );
239
240        $json_files = glob( "{$local_destination}{$plugin_slug}-{$locale}-*.json" );
241        if ( $json_files ) {
242            $files = array_merge( $files, $json_files );
243        }
244
245        foreach ( $files as $file ) {
246            if ( $wp_filesystem->exists( $file ) ) {
247                $wp_filesystem->delete( $file );
248            }
249        }
250    }
251
252    /**
253     * Retrieves all active locales for the site.
254     */
255    public static function get_all_active_locales() {
256        $locales = array( get_locale() );
257
258        $available_languages = get_available_languages();
259        if ( ! empty( $available_languages ) ) {
260            $locales = array_merge( $locales, $available_languages );
261        }
262        return array_values( array_unique( $locales ) );
263    }
264
265    /**
266     * Load features that don't need any special loading considerations.
267     */
268    public static function load_features() {
269        \Automattic\Jetpack\ExPlat::init();
270
271        // Please keep the features in alphabetical order.
272        require_once __DIR__ . '/features/100-year-plan/enhanced-ownership.php';
273        require_once __DIR__ . '/features/100-year-plan/locked-mode.php';
274        require_once __DIR__ . '/features/admin-color-schemes/admin-color-schemes.php';
275        require_once __DIR__ . '/features/block-patterns/block-patterns.php';
276        require_once __DIR__ . '/features/blog-privacy/blog-privacy.php';
277        require_once __DIR__ . '/features/cloudflare-analytics/cloudflare-analytics.php';
278        require_once __DIR__ . '/features/code-editor/class-code-editor.php';
279        require_once __DIR__ . '/features/wpcom-blocks/code/class-code-block.php';
280        require_once __DIR__ . '/features/css-monkey-patches/index.php';
281        require_once __DIR__ . '/features/error-reporting/error-reporting.php';
282        require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
283        require_once __DIR__ . '/features/font-smoothing-antialiased/font-smoothing-antialiased.php';
284        require_once __DIR__ . '/features/google-analytics/google-analytics.php';
285        require_once __DIR__ . '/features/holiday-snow/class-holiday-snow.php';
286        require_once __DIR__ . '/features/launch-button/index.php';
287        require_once __DIR__ . '/features/logo-tool/logo-tool.php';
288        require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php';
289        require_once __DIR__ . '/features/media/heif-support.php';
290        require_once __DIR__ . '/features/post-categories/quick-actions.php';
291        require_once __DIR__ . '/features/post-like-from-email/post-like-from-email.php';
292        require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php';
293        require_once __DIR__ . '/features/wpcom-admin-dashboard/wpcom-admin-dashboard.php';
294        require_once __DIR__ . '/features/wpcom-attachment-pages/wpcom-attachment-pages.php';
295        require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php';
296        require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php';
297        require_once __DIR__ . '/features/wpcom-dashboard/class-wpcom-dashboard.php';
298        require_once __DIR__ . '/features/wpcom-hotfixes/wpcom-hotfixes.php';
299        require_once __DIR__ . '/features/wpcom-logout/wpcom-logout.php';
300        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';
301        require_once __DIR__ . '/features/wpcom-post-list/wpcom-post-types-tracking.php';
302        require_once __DIR__ . '/features/wpcom-widgets/wpcom-widgets.php';
303        require_once __DIR__ . '/features/wpcom-wpadmin-page-view/wpcom-wpadmin-page-view.php';
304
305        /*
306         * Temporarily disable client-side media processing.
307         *
308         * Client-side media processing enables cross-origin isolation (COEP/COOP headers)
309         * which can break authenticated API requests. This should be removed once client-side
310         * media processing is compatible with Dotcom's infrastructure.
311         *
312         * @see gutenberg_set_up_cross_origin_isolation() in Gutenberg's lib/media/load.php
313         * @see https://a8c.slack.com/archives/CBTN58FTJ/p1771950744814189
314         */
315        add_filter( 'wp_client_side_media_processing_enabled', '__return_false' );
316
317        // Initializers, if needed.
318        \Marketplace_Products_Updater::init();
319        \Automattic\Jetpack\Code_Editor::setup();
320        \Automattic\Jetpack\Code_Block::setup();
321        \Automattic\Jetpack\Classic_Theme_Helper\Main::init();
322        \Automattic\Jetpack\Classic_Theme_Helper\Featured_Content::setup();
323
324        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Holiday_Snow::init();
325        \Automattic\Jetpack\Jetpack_Mu_Wpcom\Wpcom_Dashboard::init();
326
327        // Gets autoloaded from the Scheduled_Updates package.
328        if ( class_exists( 'Automattic\Jetpack\Scheduled_Updates' ) ) {
329            Scheduled_Updates::init();
330        }
331    }
332
333    /**
334     * Load features that only apply to WordPress.com-connected users.
335     */
336    public static function load_wpcom_user_features() {
337        // To avoid potential collisions with ETK.
338        if ( ! class_exists( 'A8C\FSE\Help_Center' ) ) {
339            require_once __DIR__ . '/features/help-center/class-help-center.php';
340        }
341
342        if ( ! is_wpcom_user() ) {
343            require_once __DIR__ . '/features/replace-site-visibility/hide-site-visibility.php';
344            return;
345        }
346        if ( ! class_exists( 'A8C\FSE\Agents_Manager' ) ) {
347            require_once __DIR__ . '/features/agents-manager/class-agents-manager.php';
348        }
349        if ( ! class_exists( 'A8C\FSE\Survicate' ) ) {
350            require_once __DIR__ . '/features/survicate/class-survicate.php';
351        }
352        require_once __DIR__ . '/features/ai-assistant-banner/ai-assistant-banner.php';
353        require_once __DIR__ . '/features/html-block-restricted-tags/html-block-restricted-tags.php';
354        require_once __DIR__ . '/features/marketing/marketing.php';
355        require_once __DIR__ . '/features/pages/pages.php';
356        require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
357        require_once __DIR__ . '/features/stats/stats.php';
358        require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
359        require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
360        require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
361        require_once __DIR__ . '/features/wpcom-colourlovers-deprecate/wpcom-colourlovers-deprecate.php';
362        require_once __DIR__ . '/features/wpcom-comments/wpcom-comments.php';
363        require_once __DIR__ . '/features/wpcom-dashboard-widgets/wpcom-dashboard-widgets.php';
364        require_once __DIR__ . '/features/wpcom-imports/wpcom-imports.php';
365        require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
366        require_once __DIR__ . '/features/wpcom-media/wpcom-media-url-upload.php';
367        require_once __DIR__ . '/features/wpcom-media/wpcom-export-media-files.php';
368        require_once __DIR__ . '/features/wpcom-options-general/options-general.php';
369        require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
370        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
371        require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
372        require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
373        require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-tracking.php';
374        require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
375        require_once __DIR__ . '/features/wpcom-user-edit/wpcom-user-edit.php';
376
377        // Enable newsletter settings for sites with the newsletter-package-202603 sticker.
378        add_filter( 'jetpack_wp_admin_newsletter_settings_enabled', 'wpcom_maybe_enable_newsletter_settings' );
379
380        // Initialize Newsletter Settings so hooks like the Reading page notice
381        // are registered on Simple sites (where load-jetpack.php doesn't run).
382        \Automattic\Jetpack\Newsletter\Settings::init();
383
384        // Only load the Masterbar features on WoA sites.
385        if ( class_exists( '\Automattic\Jetpack\Status\Host' ) && ( new \Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
386            // This is temporary. After we cleanup Masterbar on WPCOM we should load Masterbar for Simple sites too.
387            \Automattic\Jetpack\Masterbar\Main::init();
388        }
389    }
390
391    /**
392     * Load features that only apply to WordPress.com sites, regardless of whether the users are connected.
393     */
394    public static function load_wpcom_sites_features() {
395        if ( is_fully_managed_agency_site() ) {
396            return;
397        }
398
399        require_once __DIR__ . '/features/gutenberg-rtc/gutenberg-rtc.php';
400        require_once __DIR__ . '/features/wpcom-contact-form-flags/wpcom-contact-form-flags.php';
401    }
402
403    /**
404     * Define the flags to turn off features in the ETK plugin.
405     * Can be removed once the feature no longer exists in the ETK plugin.
406     */
407    public static function load_etk_features_flags() {
408        // Don't load on agency sites.
409        if ( is_fully_managed_agency_site() ) {
410            return;
411        }
412
413        // Don't load if the user is not a wpcom user on WP Admin.
414        // The features is still required on the frontend page regardless of the user.
415        if ( is_admin() && ! is_wpcom_user() ) {
416            return;
417        }
418
419        define( 'MU_WPCOM_COBLOCKS_GALLERY', true );
420        define( 'MU_WPCOM_CUSTOM_LINE_HEIGHT', true );
421        define( 'MU_WPCOM_BLOCK_INSERTER_MODIFICATIONS', true );
422        define( 'MU_WPCOM_HOMEPAGE_TITLE_HIDDEN', true );
423        define( 'MU_WPCOM_JETPACK_GLOBAL_STYLES', true );
424        define( 'A8C_USE_FONT_SMOOTHING_ANTIALIASED', false );
425        define( 'MU_WPCOM_MAILERLITE_WIDGET', true );
426        define( 'MU_WPCOM_OVERRIDE_PREVIEW_BUTTON_URL', true );
427        define( 'MU_WPCOM_PARAGRAPH_BLOCK', true );
428        define( 'MU_WPCOM_STARTER_PAGE_TEMPLATES', true );
429        define( 'MU_WPCOM_TAGS_EDUCATION', true );
430        define( 'MU_WPCOM_BLOCK_DESCRIPTION_LINKS', true );
431        define( 'MU_WPCOM_BLOCK_EDITOR_NUX', true );
432        define( 'MU_WPCOM_POSTS_LIST_BLOCK', true );
433        define( 'MU_WPCOM_JETPACK_COUNTDOWN_BLOCK', true );
434        define( 'MU_WPCOM_JETPACK_TIMELINE_BLOCK', true );
435        define( 'MU_WPCOM_DOCUMENTATION_LINKS', true );
436        define( 'MU_WPCOM_GLOBAL_STYLES', true );
437        define( 'MU_WPCOM_FSE', true );
438        define( 'MU_WPCOM_TEMPLATE_INSERTER', true );
439        define( 'MU_WPCOM_WHATS_NEW', true );
440    }
441
442    /**
443     * Load ETK features.
444     * Can be moved back to load_features() once the feature no longer exists in the ETK plugin.
445     */
446    public static function load_etk_features() {
447        // Don't load on agency sites.
448        if ( is_fully_managed_agency_site() ) {
449            return;
450        }
451
452        // Don't load if the user is not a wpcom user on WP Admin.
453        // The features is still required on the frontend page regardless of the user.
454        if ( is_admin() && ! is_wpcom_user() ) {
455            return;
456        }
457
458        require_once __DIR__ . '/features/jetpack-global-styles/class-global-styles.php';
459        require_once __DIR__ . '/features/mailerlite/subscriber-popup.php';
460        require_once __DIR__ . '/features/wpcom-fse/wpcom-fse.php';
461
462        /**
463         * Load features for the editor and the frontend pages.
464         */
465        global $pagenow;
466        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
467        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
468            require_once __DIR__ . '/features/block-editor/custom-line-height.php';
469            require_once __DIR__ . '/features/block-inserter-modifications/block-inserter-modifications.php';
470            require_once __DIR__ . '/features/hide-homepage-title/hide-homepage-title.php';
471            require_once __DIR__ . '/features/override-preview-button-url/override-preview-button-url.php';
472            require_once __DIR__ . '/features/paragraph-block-placeholder/paragraph-block-placeholder.php';
473            require_once __DIR__ . '/features/tags-education/tags-education.php';
474            require_once __DIR__ . '/features/wpcom-block-description-links/wpcom-block-description-links.php';
475            require_once __DIR__ . '/features/wpcom-block-editor-nux/class-wpcom-block-editor-nux.php';
476            require_once __DIR__ . '/features/wpcom-blocks/a8c-posts-list/a8c-posts-list.php';
477            require_once __DIR__ . '/features/wpcom-blocks/event-countdown/event-countdown.php';
478            require_once __DIR__ . '/features/wpcom-blocks/timeline/timeline.php';
479            require_once __DIR__ . '/features/wpcom-documentation-links/wpcom-documentation-links.php';
480            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
481            require_once __DIR__ . '/features/wpcom-legacy-fse/wpcom-legacy-fse.php';
482        } elseif ( isset( $pagenow ) && 'customize.php' === $pagenow ) {
483            // Load wpcom-global-styles on the customizer so access to additional css can be checked there.
484            require_once __DIR__ . '/features/wpcom-global-styles/index.php';
485        }
486    }
487
488    /**
489     * Load the newspack blocks feature for the editor and the frontend pages.
490     */
491    public static function load_newspack_blocks() {
492        /**
493         * Avoid potential collisions with newspack-blocks plugin.
494         */
495        if ( class_exists( '\Newspack_Blocks', false ) ) {
496            return;
497        }
498
499        global $pagenow;
500        $allowed_pages = array( 'post.php', 'post-new.php', 'site-editor.php' );
501        if ( ( isset( $pagenow ) && in_array( $pagenow, $allowed_pages, true ) ) || ! is_admin() ) {
502            define( 'MU_WPCOM_NEWSPACK_BLOCKS', true );
503            require_once __DIR__ . '/features/newspack-blocks/index.php';
504        }
505    }
506
507    /**
508     * Load the Coming Soon feature.
509     */
510    public static function load_coming_soon() {
511        /**
512         * On WoA sites, users may be using non-symlinked older versions of the FSE plugin.
513         * If they are, check the active version to avoid redeclaration errors.
514         */
515        if ( ! function_exists( 'is_plugin_active' ) ) {
516            require_once ABSPATH . 'wp-admin/includes/plugin.php';
517        }
518
519        /**
520         * Explicitly pass $markup = false in get_plugin_data to avoid indirectly calling wptexturize that could cause unintended side effects.
521         * See: https://developer.wordpress.org/reference/functions/get_plugin_data/
522         */
523        $fse_plugin                 = 'full-site-editing/full-site-editing-plugin.php';
524        $fse_plugin_path            = WP_PLUGIN_DIR . '/' . $fse_plugin;
525        $invalid_fse_version_active =
526            file_exists( $fse_plugin_path ) &&
527            is_file( $fse_plugin_path ) &&
528            is_plugin_active( $fse_plugin ) &&
529            version_compare( get_plugin_data( $fse_plugin_path, false )['Version'], '3.56084', '<' );
530
531        if ( $invalid_fse_version_active ) {
532            return;
533        }
534
535        if (
536            ( defined( 'WPCOM_PUBLIC_COMING_SOON' ) && WPCOM_PUBLIC_COMING_SOON ) ||
537            apply_filters( 'a8c_enable_public_coming_soon', false )
538        ) {
539            require_once __DIR__ . '/features/coming-soon/coming-soon.php';
540        }
541    }
542
543    /**
544     * Load the Launchpad feature.
545     */
546    public static function load_launchpad() {
547        require_once __DIR__ . '/features/launchpad/launchpad.php';
548    }
549
550    /**
551     * Load WP REST API plugins for wpcom.
552     */
553    public static function load_wpcom_rest_api_endpoints() {
554        if ( ! function_exists( 'wpcom_rest_api_v2_load_plugin' ) ) {
555            return;
556        }
557
558        // We don't use `wpcom_rest_api_v2_load_plugin_files` because it operates inconsisently.
559        $plugins = glob( __DIR__ . '/features/wpcom-endpoints/*.php' );
560
561        if ( ! is_array( $plugins ) ) {
562            return;
563        }
564
565        foreach ( array_filter( $plugins, 'is_file' ) as $plugin ) {
566            require_once $plugin;
567        }
568    }
569
570    /**
571     * Adds a global variable containing the config of the plugin to the window object.
572     */
573    public static function load_jetpack_mu_wpcom_settings() {
574        $handle = 'jetpack-mu-wpcom-settings';
575
576        // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
577        wp_register_script(
578            $handle,
579            false,
580            array(),
581            true
582        );
583
584        $data = wp_json_encode(
585            array(
586                'assetsUrl' => plugins_url( 'build/', self::BASE_FILE ),
587            ),
588            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
589        );
590
591        wp_add_inline_script(
592            $handle,
593            "var JETPACK_MU_WPCOM_SETTINGS = $data;",
594            'before'
595        );
596
597        wp_enqueue_script( $handle );
598    }
599
600    /**
601     * Adds a global variable containing the map provider in a map_block_settings object to the window object.
602     */
603    public static function load_map_block_settings() {
604        if (
605            ! function_exists( 'get_current_screen' )
606            || \get_current_screen() === null
607        ) {
608            return;
609        }
610
611        // Return early if we are not in the block editor.
612        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
613            return;
614        }
615
616        $map_provider = apply_filters( 'wpcom_map_block_map_provider', 'mapbox' );
617        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Maps', array( 'provider' => $map_provider ) );
618    }
619
620    /**
621     * Adds a global variable containing where the newsletter categories should be shown.
622     */
623    public static function load_newsletter_categories_settings() {
624        if (
625            ! function_exists( 'get_current_screen' )
626            || \get_current_screen() === null
627        ) {
628            return;
629        }
630
631        // Return early if we are not in the block editor.
632        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
633            return;
634        }
635
636        $newsletter_categories_location = apply_filters( 'wpcom_newsletter_categories_location', 'block' );
637        wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_Subscriptions', array( 'newsletter_categories_location' => $newsletter_categories_location ) );
638    }
639
640    /**
641     * Unbinds focusout event handler on #wp-admin-bar-menu-toggle introduced in WordPress 6.2.
642     *
643     * The focusout event handler is preventing the unified navigation from being closed on mobile.
644     */
645    public static function unbind_focusout_on_wp_admin_bar_menu_toggle() {
646        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) );' );
647    }
648
649    /**
650     * Determine whether to disable the comment experience.
651     *
652     * @param int $blog_id The blog ID.
653     * @return boolean
654     */
655    private static function should_disable_comment_experience( $blog_id ) {
656        $path_wp_for_teams = WP_CONTENT_DIR . '/lib/wpforteams/functions.php';
657
658        if ( file_exists( $path_wp_for_teams ) ) {
659            require_once $path_wp_for_teams;
660        }
661
662        // This covers both P2 and P2020 themes.
663        $is_p2     = str_contains( get_stylesheet(), 'pub/p2' ) || function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( $blog_id );
664        $is_forums = str_contains( get_stylesheet(), 'a8c/supportforums' ); // Not in /forums.
665
666        $verbum_option_enabled = get_blog_option( $blog_id, 'enable_verbum_commenting', true );
667
668        if ( empty( $verbum_option_enabled ) ) {
669            return true;
670        }
671
672        // Don't load any comment experience in the Reader, GlotPress, wp-admin, or P2.
673        return ( 1 === $blog_id || TRANSLATE_BLOG_ID === $blog_id || is_admin() || $is_p2 || $is_forums );
674    }
675
676    /**
677     * Load Verbum Comments.
678     */
679    public static function load_verbum_comments() {
680        if ( class_exists( 'Verbum_Comments' ) ) {
681            return;
682        } else {
683            $blog_id = get_current_blog_id();
684            // Jetpack loads Verbum though an iframe from jetpack.wordpress.com.
685            // So we need to check the GET request for the blogid.
686            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
687            if ( isset( $_GET['blogid'] ) ) {
688                $blog_id = intval( $_GET['blogid'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
689            }
690            if ( self::should_disable_comment_experience( $blog_id ) ) {
691                return;
692            }
693            require_once __DIR__ . '/features/verbum-comments/class-verbum-comments.php';
694            new \Automattic\Jetpack\Verbum_Comments();
695        }
696    }
697
698    /**
699     * Load Verbum Comments Settings.
700     */
701    public static function load_verbum_comments_admin() {
702        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-admin.php';
703        new \Automattic\Jetpack\Verbum_Admin();
704    }
705
706    /**
707     * Load Verbum Moderate.
708     */
709    public static function load_verbum_moderate() {
710        require_once __DIR__ . '/features/verbum-comments/assets/class-verbum-moderate.php';
711        new \Automattic\Jetpack\Verbum_Moderate();
712    }
713
714    /**
715     * Load Odyssey Stats in Simple sites.
716     */
717    public static function load_wpcom_simple_odyssey_stats() {
718        require_once __DIR__ . '/features/wpcom-simple-odyssey-stats/wpcom-simple-odyssey-stats.php';
719    }
720
721    /**
722     * Load the Jetpack Custom CSS feature.
723     */
724    public static function load_custom_css() {
725        require_once __DIR__ . '/features/custom-css/custom-css/preprocessors.php';
726        require_once __DIR__ . '/features/custom-css/custom-css.php';
727    }
728
729    /**
730     * Load the Random Redirect feature.
731     */
732    public static function load_wpcom_random_redirect() {
733        require_once __DIR__ . '/features/random-redirect/random-redirect.php';
734    }
735
736    /**
737     * Load the Social Links feature.
738     */
739    public static function load_social_links() {
740        if ( class_exists( 'Automattic\Jetpack\Classic_Theme_Helper\Social_Links' ) ) {
741            new \Automattic\Jetpack\Classic_Theme_Helper\Social_Links();
742        }
743    }
744
745    /**
746     * Populate JetpackScriptData.site.wpcom.blog_id with the actual WP.com blog ID.
747     *
748     * @param array $data The script data.
749     * @return array
750     */
751    public static function set_wpcom_blog_id_script_data( $data ) {
752        $blog_id = get_wpcom_blog_id();
753        if ( $blog_id ) {
754            $data['site']['wpcom']['blog_id'] = $blog_id;
755        }
756        return $data;
757    }
758
759    /**
760     * Add Jetpack script data with host information on P2
761     *
762     * @param array $data - The Jetpack script data.
763     * @return array - The modified Jetpack script data.
764     */
765    public static function add_jetpack_script_data_for_p2( $data ) {
766        if (
767        str_contains( get_stylesheet(), 'pub/p2' ) ||
768        ( function_exists( '\WPForTeams\is_wpforteams_site' ) && is_wpforteams_site( get_current_blog_id() ) )
769        ) {
770            $host = new \Automattic\Jetpack\Status\Host();
771            if ( ! isset( $data['site']['host'] ) ) {
772                $data['site']['host'] = $host->get_known_host_guess();
773            }
774            if ( ! isset( $data['site']['is_wpcom_platform'] ) ) {
775                $data['site']['is_wpcom_platform'] = $host->is_wpcom_platform();
776            }
777        }
778        return $data;
779    }
780}