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