| @@ -6,19 +6,27 @@ | ||
| 6 | 6 | * @package automattic/jetpack |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | use Automattic\Jetpack\Assets; |
| 10 | +use Automattic\Jetpack\Assets\Shared_Stores_Assets; | |
| 10 | 11 | use Automattic\Jetpack\Blocks; |
| 11 | 12 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 12 | 13 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 13 | 14 | use Automattic\Jetpack\Constants; |
| 14 | 15 | use Automattic\Jetpack\Current_Plan as Jetpack_Plan; |
| 15 | -use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Dismissed_Notices; | |
| 16 | +use Automattic\Jetpack\Modules; | |
| 17 | +use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer; | |
| 16 | 18 | use Automattic\Jetpack\Status; |
| 17 | 19 | use Automattic\Jetpack\Status\Host; |
| 18 | 20 | |
| 19 | -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move the functions and such to some other file. | |
| 21 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 22 | + exit( 0 ); | |
| 23 | +} | |
| 20 | 24 | |
| 25 | +// Required directly so the AI master-gate helper is available regardless of | |
| 26 | +// which loader pulled this class in. | |
| 27 | +require_once __DIR__ . '/_inc/lib/class-jetpack-ai-settings.php'; | |
| 28 | + | |
| 21 | 29 | /** |
| 22 | 30 | * General Gutenberg editor specific functionality |
| 23 | 31 | */ |
| 24 | 32 | class Jetpack_Gutenberg { |
| @@ -63,8 +71,133 @@ | ||
| 63 | 71 | 'jetpack/revue', |
| 64 | 72 | ); |
| 65 | 73 | |
| 66 | 74 | /** |
| 75 | + * Display-only blocks whose registration PHP can be deferred until the block | |
| 76 | + * actually appears on a front-end page. | |
| 77 | + * | |
| 78 | + * Every block listed here has been verified to be "pure": the callback it hooks | |
| 79 | + * to `init` does nothing but call Blocks::jetpack_register_block() (plus trivial | |
| 80 | + * connection/module guards). It registers exactly one block type named | |
| 81 | + * `jetpack/<dir>` (matching its directory), and any front-end hooks it adds (asset | |
| 82 | + * enqueues, wp_footer, filters, …) live inside its render callback, so they only | |
| 83 | + * run when the block is rendered. | |
| 84 | + * | |
| 85 | + * On plain front-end requests these blocks are NOT loaded on `init`. Instead they | |
| 86 | + * are registered just-in-time the first time the block (or a block whose subtree | |
| 87 | + * contains it) is encountered while rendering, via self::lazy_register_deferred_block() | |
| 88 | + * on `pre_render_block`. On admin/REST/cron/CLI/XML-RPC (block-editor) requests they | |
| 89 | + * keep loading eagerly so the editor, the block-types REST endpoint and server-side | |
| 90 | + * rendering are unaffected. | |
| 91 | + * | |
| 92 | + * One class of front-end request is NOT safe to defer on: front-end block editors | |
| 93 | + * (e.g. P2) render the inserter on a plain front-end page, so is_block_editor_context() | |
| 94 | + * is false, yet the block must be registered at `init` for get_availability() to report | |
| 95 | + * it as available. self::load_independent_blocks() therefore never defers a block that | |
| 96 | + * ships in the `no-post-editor` preset (extensions/index.json) — those are exactly the | |
| 97 | + * blocks available in editors other than the post editor. A block listed here that is | |
| 98 | + * also in `no-post-editor` simply keeps loading eagerly. | |
| 99 | + * | |
| 100 | + * A block must NOT be added here if: | |
| 101 | + * - its `init` callback registers any other hook, post meta, REST route, | |
| 102 | + * shortcode, block pattern or hooked-block; | |
| 103 | + * - it registers more than one block type, or a block name that differs from its | |
| 104 | + * directory name (e.g. videopress registers `jetpack/videopress-block`); or | |
| 105 | + * - it uses `plan_check` (its availability is computed from the init-time | |
| 106 | + * `jetpack_register_gutenberg_extensions` hook, which lazy registration bypasses, | |
| 107 | + * so the front-end availability nudge/render could read a stale value); or | |
| 108 | + * - a front-end path reads its entry from get_cached_availability() before the | |
| 109 | + * block renders. Deferred blocks appear unavailable there until the lazy | |
| 110 | + * registration callback runs; or | |
| 111 | + * - its block file can be `require`d by another runtime code path after `init` | |
| 112 | + * (e.g. slideshow is included by modules/shortcodes/slideshow.php). The lazy | |
| 113 | + * loader registers a block by running the `init` callback its include adds; if | |
| 114 | + * the file was already included elsewhere, the include is a no-op and the | |
| 115 | + * callback would never run, so the block would silently fail to register; or | |
| 116 | + * - another runtime code path calls a function defined in its block file | |
| 117 | + * (e.g. button defines Button\render_email(), called by subscriptions and | |
| 118 | + * memberships for WooCommerce e-mail rendering). Deferring the file would leave | |
| 119 | + * that function undefined when the dependent path runs; or | |
| 120 | + * - it registers a `render_email_callback`. That callback is read off the | |
| 121 | + * registered block type by the WooCommerce e-mail editor — an out-of-band | |
| 122 | + * renderer that does not go through `pre_render_block`/`do_blocks` — so the block | |
| 123 | + * must already be registered when an e-mail containing it is rendered, which can | |
| 124 | + * happen on a front-end request (e.g. a transactional e-mail sent during checkout). | |
| 125 | + * | |
| 126 | + * When in doubt, leave it out: omitted blocks simply keep their current eager | |
| 127 | + * behavior. | |
| 128 | + * | |
| 129 | + * @since 16.0 | |
| 130 | + * @var string[] Block feature names (directory names, without the `jetpack/` prefix). | |
| 131 | + */ | |
| 132 | + private static $lazy_blocks = array( | |
| 133 | + 'blog-stats', | |
| 134 | + 'blogging-prompt', | |
| 135 | + 'business-hours', | |
| 136 | + 'eventbrite', | |
| 137 | + 'gif', | |
| 138 | + 'goodreads', | |
| 139 | + 'google-calendar', | |
| 140 | + 'google-docs-embed', | |
| 141 | + 'image-compare', | |
| 142 | + 'like', | |
| 143 | + 'markdown', | |
| 144 | + 'nextdoor', | |
| 145 | + 'payments-intro', | |
| 146 | + 'pinterest', | |
| 147 | + 'related-posts', | |
| 148 | + 'repeat-visitor', | |
| 149 | + 'sharing-buttons', | |
| 150 | + 'story', | |
| 151 | + 'tock', | |
| 152 | + 'top-posts', | |
| 153 | + 'voice-to-content', | |
| 154 | + ); | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Blocks that were deferred on the current request and still need to be | |
| 158 | + * registered just-in-time when first rendered. Keyed by block feature name. | |
| 159 | + * | |
| 160 | + * @since 16.0 | |
| 161 | + * @var array<string,bool> | |
| 162 | + */ | |
| 163 | + private static $deferred_blocks = array(); | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * Fallback minimum plan requirements for WordPress.com/Atomic sites. | |
| 167 | + * | |
| 168 | + * Used when features have conditional availability (e.g., sticker-based gating) | |
| 169 | + * and don't appear in features_data['available']. This only affects the upsell | |
| 170 | + * message shown to users. | |
| 171 | + * | |
| 172 | + * @since 15.5 | |
| 173 | + * @var array Feature slug => minimum WordPress.com plan slug. | |
| 174 | + */ | |
| 175 | + private static $wpcom_minimum_plan_fallbacks = array( | |
| 176 | + 'donations' => 'value_bundle', | |
| 177 | + 'payment-buttons' => 'value_bundle', | |
| 178 | + 'paypal-payment-buttons' => 'value_bundle', | |
| 179 | + ); | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Storing the contents of the preset file. | |
| 183 | + * | |
| 184 | + * Already been json_decode. | |
| 185 | + * | |
| 186 | + * @var null|object JSON decoded object after first usage. | |
| 187 | + */ | |
| 188 | + private static $preset_cache = null; | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Keep track of JS loading strategies for each block that needs it. | |
| 192 | + * | |
| 193 | + * @var array<string, array|bool> | |
| 194 | + * | |
| 195 | + * @since 15.0 | |
| 196 | + */ | |
| 197 | + private static $block_js_loading_strategies = array(); | |
| 198 | + | |
| 199 | + /** | |
| 67 | 200 | * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in |
| 68 | 201 | * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can |
| 69 | 202 | * optionally fall back to that. |
| 70 | 203 | * |
| @@ -98,8 +231,9 @@ | ||
| 98 | 231 | $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' ); |
| 99 | 232 | } |
| 100 | 233 | |
| 101 | 234 | if ( ! $version_available ) { |
| 235 | + $slug = self::remove_extension_prefix( $slug ); | |
| 102 | 236 | self::set_extension_unavailable( |
| 103 | 237 | $slug, |
| 104 | 238 | 'incorrect_gutenberg_version', |
| 105 | 239 | array( |
| @@ -158,9 +292,10 @@ | ||
| 158 | 292 | * |
| 159 | 293 | * @param string $slug Slug of the extension. |
| 160 | 294 | */ |
| 161 | 295 | public static function set_extension_available( $slug ) { |
| 162 | - self::$availability[ self::remove_extension_prefix( $slug ) ] = true; | |
| 296 | + $slug = self::remove_extension_prefix( $slug ); | |
| 297 | + self::$availability[ $slug ] = true; | |
| 163 | 298 | } |
| 164 | 299 | |
| 165 | 300 | /** |
| 166 | 301 | * Set the reason why an extension (block or plugin) is unavailable |
| @@ -193,10 +328,10 @@ | ||
| 193 | 328 | // The block editor may apply an upgrade nudge if `missing_plan` is the reason. |
| 194 | 329 | // Add a descriptive suffix to disable behavior but provide informative reason. |
| 195 | 330 | $reason .= '__nudge_disabled'; |
| 196 | 331 | } |
| 197 | - | |
| 198 | - self::$availability[ self::remove_extension_prefix( $slug ) ] = array( | |
| 332 | + $slug = self::remove_extension_prefix( $slug ); | |
| 333 | + self::$availability[ $slug ] = array( | |
| 199 | 334 | 'reason' => $reason, |
| 200 | 335 | 'details' => $details, |
| 201 | 336 | ); |
| 202 | 337 | } |
| @@ -218,11 +353,13 @@ | ||
| 218 | 353 | * |
| 219 | 354 | * @return void |
| 220 | 355 | */ |
| 221 | 356 | public static function reset() { |
| 222 | - self::$extensions = null; | |
| 223 | - self::$availability = array(); | |
| 224 | - self::$cached_availability = null; | |
| 357 | + self::$extensions = null; | |
| 358 | + self::$availability = array(); | |
| 359 | + self::$cached_availability = null; | |
| 360 | + self::$block_js_loading_strategies = array(); | |
| 361 | + self::$deferred_blocks = array(); | |
| 225 | 362 | } |
| 226 | 363 | |
| 227 | 364 | /** |
| 228 | 365 | * Return the Gutenberg extensions (blocks and plugins) directory |
| @@ -242,28 +379,49 @@ | ||
| 242 | 379 | |
| 243 | 380 | /** |
| 244 | 381 | * Checks for a given .json file in the blocks folder. |
| 245 | 382 | * |
| 383 | + * @deprecated 14.3 | |
| 384 | + * | |
| 246 | 385 | * @param string $preset The name of the .json file to look for. |
| 247 | 386 | * |
| 248 | 387 | * @return bool True if the file is found. |
| 249 | 388 | */ |
| 250 | 389 | public static function preset_exists( $preset ) { |
| 390 | + _deprecated_function( __METHOD__, '14.3' ); | |
| 251 | 391 | return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ); |
| 252 | 392 | } |
| 253 | 393 | |
| 254 | 394 | /** |
| 255 | - * Decodes JSON loaded from a preset file in the blocks folder | |
| 395 | + * Decodes JSON loaded from the preset file in the blocks folder | |
| 256 | 396 | * |
| 257 | - * @param string $preset The name of the .json file to load. | |
| 397 | + * @since 14.3 Deprecated argument. Only one value is ever used. | |
| 258 | 398 | * |
| 399 | + * @param null $deprecated No longer used. | |
| 400 | + * | |
| 259 | 401 | * @return mixed Returns an object if the file is present, or false if a valid .json file is not present. |
| 260 | 402 | */ |
| 261 | - public static function get_preset( $preset ) { | |
| 262 | - return json_decode( | |
| 263 | - // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 264 | - file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' ) | |
| 265 | - ); | |
| 403 | + public static function get_preset( $deprecated = null ) { | |
| 404 | + if ( $deprecated ) { | |
| 405 | + _deprecated_argument( __METHOD__, '14.3', 'The $preset argument is no longer needed or used.' ); | |
| 406 | + } | |
| 407 | + | |
| 408 | + if ( self::$preset_cache ) { | |
| 409 | + return self::$preset_cache; | |
| 410 | + } | |
| 411 | + | |
| 412 | + /* | |
| 413 | + * The manifest is a build artifact and is absent in a source checkout (e.g. when | |
| 414 | + * running the test suite). Return false — as documented — rather than calling | |
| 415 | + * wp_json_file_decode() on a missing file, which triggers _doing_it_wrong(). | |
| 416 | + */ | |
| 417 | + $preset_file = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . 'index.json'; | |
| 418 | + if ( ! file_exists( $preset_file ) ) { | |
| 419 | + return false; | |
| 420 | + } | |
| 421 | + | |
| 422 | + self::$preset_cache = wp_json_file_decode( $preset_file ); | |
| 423 | + return self::$preset_cache; | |
| 266 | 424 | } |
| 267 | 425 | |
| 268 | 426 | /** |
| 269 | 427 | * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json |
| @@ -270,11 +428,9 @@ | ||
| 270 | 428 | * |
| 271 | 429 | * @return array A list of blocks: eg [ 'publicize', 'markdown' ] |
| 272 | 430 | */ |
| 273 | 431 | public static function get_jetpack_gutenberg_extensions_allowed_list() { |
| 274 | - $preset_extensions_manifest = self::preset_exists( 'index' ) | |
| 275 | - ? self::get_preset( 'index' ) | |
| 276 | - : (object) array(); | |
| 432 | + $preset_extensions_manifest = ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) ? array() : self::get_preset(); | |
| 277 | 433 | $blocks_variation = self::blocks_variation(); |
| 278 | 434 | |
| 279 | 435 | return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ); |
| 280 | 436 | } |
| @@ -289,8 +445,13 @@ | ||
| 289 | 445 | public static function get_available_extensions( $allowed_extensions = null ) { |
| 290 | 446 | $exclusions = get_option( 'jetpack_excluded_extensions', array() ); |
| 291 | 447 | $allowed_extensions = $allowed_extensions === null ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions; |
| 292 | 448 | |
| 449 | + // Avoid errors if option data is not as expected. | |
| 450 | + if ( ! is_array( $exclusions ) ) { | |
| 451 | + $exclusions = array(); | |
| 452 | + } | |
| 453 | + | |
| 293 | 454 | return array_diff( $allowed_extensions, $exclusions ); |
| 294 | 455 | } |
| 295 | 456 | |
| 296 | 457 | /** |
| @@ -386,8 +547,13 @@ | ||
| 386 | 547 | * |
| 387 | 548 | * @param array |
| 388 | 549 | */ |
| 389 | 550 | self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() ); |
| 551 | + | |
| 552 | + if ( ! is_array( self::$extensions ) ) { | |
| 553 | + _doing_it_wrong( __METHOD__, esc_html__( 'The jetpack_set_available_extensions filter must return an array.', 'jetpack' ), '14.9' ); | |
| 554 | + self::$extensions = array(); | |
| 555 | + } | |
| 390 | 556 | } |
| 391 | 557 | |
| 392 | 558 | return self::$extensions; |
| 393 | 559 | } |
| @@ -430,20 +596,25 @@ | ||
| 430 | 596 | if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) { |
| 431 | 597 | return false; |
| 432 | 598 | } |
| 433 | 599 | |
| 434 | - if ( get_option( 'jetpack_blocks_disabled', false ) ) { | |
| 435 | - return false; | |
| 600 | + $return = true; | |
| 601 | + | |
| 602 | + if ( ! ( new Modules() )->is_active( 'blocks' ) ) { | |
| 603 | + $return = false; | |
| 436 | 604 | } |
| 437 | 605 | |
| 438 | 606 | /** |
| 439 | - * Filter to disable Gutenberg blocks | |
| 607 | + * Filter to enable Gutenberg blocks. | |
| 440 | 608 | * |
| 609 | + * Defaults to true if (connected or in offline mode) and the Blocks module is active. | |
| 610 | + * | |
| 441 | 611 | * @since 6.5.0 |
| 612 | + * @since 13.9 Filter is able to activate or deactivate Gutenberg blocks. | |
| 442 | 613 | * |
| 443 | 614 | * @param bool true Whether to load Gutenberg blocks |
| 444 | 615 | */ |
| 445 | - return (bool) apply_filters( 'jetpack_gutenberg', true ); | |
| 616 | + return (bool) apply_filters( 'jetpack_gutenberg', $return ); | |
| 446 | 617 | } |
| 447 | 618 | |
| 448 | 619 | /** |
| 449 | 620 | * Queue a script to set `Jetpack_Block_Assets_Base_Url`. |
| @@ -554,9 +725,8 @@ | ||
| 554 | 725 | |
| 555 | 726 | // Enqueue script. |
| 556 | 727 | $script_relative_path = self::get_blocks_directory() . $type . '/view.js'; |
| 557 | 728 | $script_deps_path = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php'; |
| 558 | - $script_dependencies[] = 'wp-polyfill'; | |
| 559 | 729 | $script_dependencies[] = 'jetpack-blocks-assets-base-url'; |
| 560 | 730 | if ( file_exists( $script_deps_path ) ) { |
| 561 | 731 | $asset_manifest = include $script_deps_path; |
| 562 | 732 | $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) ); |
| @@ -565,11 +735,12 @@ | ||
| 565 | 735 | if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) { |
| 566 | 736 | $script_version = self::get_asset_version( $script_relative_path ); |
| 567 | 737 | $view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE ); |
| 568 | 738 | $view_script = add_query_arg( 'minify', 'false', $view_script ); |
| 739 | + $strategy = self::get_block_js_loading_strategy( $type ); | |
| 569 | 740 | |
| 570 | 741 | // Enqueue dependencies. |
| 571 | - wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false ); | |
| 742 | + wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, $strategy ); | |
| 572 | 743 | |
| 573 | 744 | // If this is a customizer preview, enqueue the dependencies and render the script directly to the preview after autosave. |
| 574 | 745 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 575 | 746 | if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) { |
| @@ -575,9 +746,9 @@ | ||
| 575 | 746 | if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) { |
| 576 | 747 | // The Map block is dependent on wp-element, and it doesn't appear to to be possible to load |
| 577 | 748 | // this dynamically into the customizer iframe currently. |
| 578 | 749 | if ( 'map' === $type ) { |
| 579 | - echo '<div>' . esc_html_e( 'No map preview available. Publish and refresh to see this widget.', 'jetpack' ) . '</div>'; | |
| 750 | + echo '<div>' . esc_html__( 'No map preview available. Publish and refresh to see this widget.', 'jetpack' ) . '</div>'; | |
| 580 | 751 | echo '<script>'; |
| 581 | 752 | echo 'Array.from(document.getElementsByClassName(\'wp-block-jetpack-map\')).forEach(function(element){element.style.display = \'none\';})'; |
| 582 | 753 | echo '</script>'; |
| 583 | 754 | } else { |
| @@ -622,18 +793,40 @@ | ||
| 622 | 793 | if ( ! self::should_load() ) { |
| 623 | 794 | return; |
| 624 | 795 | } |
| 625 | 796 | |
| 626 | - /** | |
| 627 | - * This can be called multiple times per page load in the admin, during the `enqueue_block_assets` action. | |
| 628 | - * These assets are necessary for the admin for editing but are not necessary for each pattern preview. | |
| 629 | - * Therefore we dequeue them, so they don't load for each pattern preview iframe. | |
| 797 | + /* | |
| 798 | + * When the user returns to the editor right after a successful plan | |
| 799 | + * purchase (signalled by the `plan_upgraded` redirect argument), refresh | |
| 800 | + * the locally cached plan from WordPress.com before block availability is | |
| 801 | + * computed below. Otherwise `available_blocks` is derived from the stale | |
| 802 | + * `jetpack_active_plan` option (only refreshed by the daily heartbeat) and | |
| 803 | + * paid blocks keep showing their upgrade nudge even though the plan is now | |
| 804 | + * active. Simple sites gate features live via `wpcom_site_has_feature()`, | |
| 805 | + * so they neither need nor benefit from this. | |
| 806 | + * | |
| 807 | + * The refresh is a blocking WordPress.com request, so it is guarded to run | |
| 808 | + * only on a connected, non-WPCOM site, throttled to once per minute (a | |
| 809 | + * repeated or bookmarked `?plan_upgraded` URL cannot trigger a request on | |
| 810 | + * every load), and time-boxed so a slow origin cannot hang the editor. The | |
| 811 | + * client-side reload fallback covers a skipped or failed refresh. The value | |
| 812 | + * is only used to trigger a cache refresh from an authoritative source, so | |
| 813 | + * no nonce is required. See FORMS-712. | |
| 630 | 814 | */ |
| 631 | - if ( ! wp_should_load_block_editor_scripts_and_styles() ) { | |
| 632 | - wp_dequeue_script( 'jp-tracks' ); | |
| 633 | - wp_dequeue_script( 'jetpack-blocks-editor' ); | |
| 815 | + if ( | |
| 816 | + ! empty( $_GET['plan_upgraded'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 817 | + && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) | |
| 818 | + && Jetpack::is_connection_ready() | |
| 819 | + && ! get_transient( 'jetpack_plan_upgraded_refresh' ) | |
| 820 | + ) { | |
| 821 | + set_transient( 'jetpack_plan_upgraded_refresh', 1, MINUTE_IN_SECONDS ); | |
| 634 | 822 | |
| 635 | - return; | |
| 823 | + $cap_plan_refresh_timeout = static function () { | |
| 824 | + return 5; | |
| 825 | + }; | |
| 826 | + add_filter( 'http_request_timeout', $cap_plan_refresh_timeout, PHP_INT_MAX ); | |
| 827 | + Jetpack_Plan::refresh_from_wpcom(); | |
| 828 | + remove_filter( 'http_request_timeout', $cap_plan_refresh_timeout, PHP_INT_MAX ); | |
| 636 | 829 | } |
| 637 | 830 | |
| 638 | 831 | $status = new Status(); |
| 639 | 832 | |
| @@ -662,12 +855,28 @@ | ||
| 662 | 855 | 'dependencies' => array( 'jetpack-blocks-assets-base-url' ), |
| 663 | 856 | ) |
| 664 | 857 | ); |
| 665 | 858 | |
| 859 | + /** | |
| 860 | + * This can be called multiple times per page load in the admin, during the `enqueue_block_assets` action. | |
| 861 | + * These assets are necessary for the admin for editing but are not necessary for each pattern preview. | |
| 862 | + * Therefore we dequeue them, so they don't load for each pattern preview iframe. | |
| 863 | + */ | |
| 864 | + if ( ! wp_should_load_block_editor_scripts_and_styles() ) { | |
| 865 | + wp_dequeue_script( 'jp-tracks' ); | |
| 866 | + wp_dequeue_script( 'jetpack-blocks-editor' ); | |
| 867 | + | |
| 868 | + return; | |
| 869 | + } | |
| 870 | + | |
| 666 | 871 | // Hack around #20357 (specifically, that the editor bundle depends on |
| 667 | 872 | // wp-edit-post but wp-edit-post's styles break the Widget Editor and |
| 668 | 873 | // Site Editor) until a real fix gets unblocked. |
| 669 | 874 | // @todo Remove this once #20357 is properly fixed. |
| 875 | + $wp_styles_fix = wp_styles()->query( 'jetpack-blocks-editor', 'registered' ); | |
| 876 | + if ( empty( $wp_styles_fix ) ) { | |
| 877 | + wp_die( 'Your installation of Jetpack is incomplete. Please run "jetpack build plugins/jetpack" in your dev env.' ); | |
| 878 | + } | |
| 670 | 879 | wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array(); |
| 671 | 880 | |
| 672 | 881 | Assets::enqueue_script( 'jetpack-blocks-editor' ); |
| 673 | 882 | |
| @@ -690,14 +899,16 @@ | ||
| 690 | 899 | wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Constants::get_constant( 'JETPACK__VERSION' ) ); |
| 691 | 900 | } |
| 692 | 901 | // AI Assistant |
| 693 | 902 | $ai_assistant_state = array( |
| 694 | - 'is-enabled' => apply_filters( 'jetpack_ai_enabled', true ), | |
| 903 | + 'is-enabled' => Jetpack_AI_Settings::is_ai_enabled(), | |
| 904 | + 'is-seo-enabled' => Jetpack_AI_Settings::is_ai_seo_enabled(), | |
| 695 | 905 | ); |
| 696 | 906 | |
| 697 | 907 | $screen_base = null; |
| 698 | 908 | if ( function_exists( 'get_current_screen' ) ) { |
| 699 | - $screen_base = get_current_screen()->base; | |
| 909 | + $current_screen = get_current_screen(); | |
| 910 | + $screen_base = $current_screen ? $current_screen->base : null; | |
| 700 | 911 | } |
| 701 | 912 | |
| 702 | 913 | $modules = array(); |
| 703 | 914 | if ( class_exists( 'Jetpack_Core_API_Module_List_Endpoint' ) ) { |
| @@ -704,8 +915,9 @@ | ||
| 704 | 915 | $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint(); |
| 705 | 916 | $modules = $module_list_endpoint->get_modules(); |
| 706 | 917 | } |
| 707 | 918 | |
| 919 | + $jetpack_plan = Jetpack_Plan::get(); | |
| 708 | 920 | $initial_state = array( |
| 709 | 921 | 'available_blocks' => self::get_availability(), |
| 710 | 922 | 'blocks_variation' => $blocks_variation, |
| 711 | 923 | 'modules' => $modules, |
| @@ -717,8 +929,21 @@ | ||
| 717 | 929 | 'is_private_site' => $status->is_private_site(), |
| 718 | 930 | 'is_coming_soon' => $status->is_coming_soon(), |
| 719 | 931 | 'is_offline_mode' => $status->is_offline_mode(), |
| 720 | 932 | 'is_newsletter_feature_enabled' => class_exists( '\Jetpack_Memberships' ), |
| 933 | + // Whether the current user may send a newsletter test email to an | |
| 934 | + // address other than their own. The wpcom guard enforces this on send | |
| 935 | + // (also checking add_users and site stickers); this flag only controls | |
| 936 | + // whether the editor's recipient field is editable. Approximated with | |
| 937 | + // manage_options so editors, who can only test-send to themselves, | |
| 938 | + // aren't shown an editable field that would always be rejected. | |
| 939 | + 'can_send_test_email_to_others' => current_user_can( 'manage_options' ), | |
| 940 | + // this is the equivalent of JP initial state siteData.showMyJetpack (class-jetpack-redux-state-helper) | |
| 941 | + // used to determine if we can link to My Jetpack from the block editor | |
| 942 | + 'is_my_jetpack_available' => My_Jetpack_Initializer::should_initialize(), | |
| 943 | + 'jetpack_plan' => array( | |
| 944 | + 'data' => $jetpack_plan['product_slug'], | |
| 945 | + ), | |
| 721 | 946 | /** |
| 722 | 947 | * Enable the RePublicize UI in the block editor context. |
| 723 | 948 | * |
| 724 | 949 | * @module publicize |
| @@ -723,9 +948,9 @@ | ||
| 723 | 948 | * |
| 724 | 949 | * @module publicize |
| 725 | 950 | * |
| 726 | 951 | * @since 10.3.0 |
| 727 | - * @deprecated $$next_version$$ This is a feature flag that is no longer used. | |
| 952 | + * @deprecated 11.5 This is a feature flag that is no longer used. | |
| 728 | 953 | * |
| 729 | 954 | * @param bool true Enable the RePublicize UI in the block editor context. Defaults to true. |
| 730 | 955 | */ |
| 731 | 956 | 'republicize_enabled' => apply_filters( 'jetpack_block_editor_republicize_feature', true ), |
| @@ -737,41 +962,23 @@ | ||
| 737 | 962 | 'allowedMimeTypes' => wp_get_mime_types(), |
| 738 | 963 | 'siteLocale' => str_replace( '_', '-', get_locale() ), |
| 739 | 964 | 'ai-assistant' => $ai_assistant_state, |
| 740 | 965 | 'screenBase' => $screen_base, |
| 966 | + /** | |
| 967 | + * Add your own feature flags to the block editor. | |
| 968 | + * | |
| 969 | + * You can access the feature flags in the block editor via hasFeatureFlag( 'your-feature-flag' ) function. | |
| 970 | + * | |
| 971 | + * @since 14.8 | |
| 972 | + * | |
| 973 | + * @param array true Enable the RePublicize UI in the block editor context. Defaults to true. | |
| 974 | + */ | |
| 975 | + 'feature_flags' => apply_filters( 'jetpack_block_editor_feature_flags', array() ), | |
| 741 | 976 | 'pluginBasePath' => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ), |
| 742 | 977 | ); |
| 743 | 978 | |
| 744 | - if ( Jetpack::is_module_active( 'publicize' ) && function_exists( 'publicize_init' ) ) { | |
| 745 | - $publicize = publicize_init(); | |
| 746 | - $jetpack_social_settings = new Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings(); | |
| 747 | - $social_initial_state = $jetpack_social_settings->get_initial_state(); | |
| 748 | - | |
| 749 | - $initial_state['social'] = array( | |
| 750 | - 'sharesData' => $publicize->get_publicize_shares_info( $blog_id ), | |
| 751 | - 'hasPaidPlan' => $publicize->has_paid_plan(), | |
| 752 | - 'hasPaidFeatures' => $publicize->has_paid_features(), | |
| 753 | - 'isEnhancedPublishingEnabled' => $publicize->has_enhanced_publishing_feature(), | |
| 754 | - 'isSocialImageGeneratorAvailable' => $social_initial_state['socialImageGeneratorSettings']['available'], | |
| 755 | - 'isSocialImageGeneratorEnabled' => $social_initial_state['socialImageGeneratorSettings']['enabled'], | |
| 756 | - 'dismissedNotices' => Dismissed_Notices::get_dismissed_notices(), | |
| 757 | - 'supportedAdditionalConnections' => $publicize->get_supported_additional_connections(), | |
| 758 | - 'autoConversionSettings' => $social_initial_state['autoConversionSettings'], | |
| 759 | - 'jetpackSharingSettingsUrl' => esc_url_raw( admin_url( 'admin.php?page=jetpack#/sharing' ) ), | |
| 760 | - 'userConnectionUrl' => esc_url_raw( admin_url( 'admin.php?page=my-jetpack#/connection' ) ), | |
| 761 | - 'useAdminUiV1' => $social_initial_state['useAdminUiV1'], | |
| 762 | - ); | |
| 763 | - | |
| 764 | - // Add connectionData if we are using the new Connection UI. | |
| 765 | - if ( $social_initial_state['useAdminUiV1'] ) { | |
| 766 | - $initial_state['social']['connectionData'] = $social_initial_state['connectionData']; | |
| 767 | - | |
| 768 | - $initial_state['social']['connectionRefreshPath'] = $social_initial_state['connectionRefreshPath']; | |
| 769 | - } | |
| 770 | - } | |
| 771 | - | |
| 772 | 979 | wp_localize_script( |
| 773 | - 'jetpack-blocks-editor', | |
| 980 | + Shared_Stores_Assets::SCRIPT_HANDLE, | |
| 774 | 981 | 'Jetpack_Editor_Initial_State', |
| 775 | 982 | $initial_state |
| 776 | 983 | ); |
| 777 | 984 | |
| @@ -779,13 +986,33 @@ | ||
| 779 | 986 | Connection_Initial_State::render_script( 'jetpack-blocks-editor' ); |
| 780 | 987 | } |
| 781 | 988 | |
| 782 | 989 | /** |
| 990 | + * Block feature names in the `no-post-editor` preset (extensions/index.json): blocks | |
| 991 | + * whose editor bundle is usable outside the post editor, so they appear in front-end | |
| 992 | + * block editors such as P2. These must never be deferred (see self::$lazy_blocks and | |
| 993 | + * self::load_independent_blocks()). | |
| 994 | + * | |
| 995 | + * @since 16.2 | |
| 996 | + * | |
| 997 | + * @return string[] Feature names, or an empty array when the preset is unavailable. | |
| 998 | + */ | |
| 999 | + private static function get_no_post_editor_extensions() { | |
| 1000 | + $preset = self::get_preset(); | |
| 1001 | + if ( is_object( $preset ) && isset( $preset->{'no-post-editor'} ) && is_array( $preset->{'no-post-editor'} ) ) { | |
| 1002 | + return $preset->{'no-post-editor'}; | |
| 1003 | + } | |
| 1004 | + return array(); | |
| 1005 | + } | |
| 1006 | + | |
| 1007 | + /** | |
| 783 | 1008 | * Some blocks do not depend on a specific module, |
| 784 | 1009 | * and can consequently be loaded outside of the usual modules. |
| 785 | 1010 | * We will look for such modules in the extensions/ directory. |
| 786 | 1011 | * |
| 787 | 1012 | * @since 7.1.0 |
| 1013 | + * @since 16.0 Pure display blocks are deferred on front-end requests and registered on first render. | |
| 1014 | + * @since 16.2 Blocks in the `no-post-editor` preset are never deferred, so front-end editors (e.g. P2) keep them. | |
| 788 | 1015 | * @see wp_common_block_scripts_and_styles() |
| 789 | 1016 | */ |
| 790 | 1017 | public static function load_independent_blocks() { |
| 791 | 1018 | if ( self::should_load() ) { |
| @@ -792,11 +1019,38 @@ | ||
| 792 | 1019 | /** |
| 793 | 1020 | * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins). |
| 794 | 1021 | * If available, load them. |
| 795 | 1022 | */ |
| 796 | - $directories = array( 'blocks', 'plugins', 'extended-blocks', 'shared', 'store' ); | |
| 1023 | + $directories = array( 'blocks', 'plugins', 'extended-blocks' ); | |
| 797 | 1024 | |
| 1025 | + /* | |
| 1026 | + * On plain front-end requests, defer the registration PHP of pure display | |
| 1027 | + * blocks (see self::$lazy_blocks) until the block is actually encountered | |
| 1028 | + * while rendering. The block editor, the block-types REST endpoint and | |
| 1029 | + * server-side rendering all run in a "block-editor context" and keep loading | |
| 1030 | + * every block eagerly, so their behavior is unchanged. | |
| 1031 | + */ | |
| 1032 | + $defer = ! self::is_block_editor_context(); | |
| 1033 | + | |
| 1034 | + /* | |
| 1035 | + * Front-end block editors (e.g. P2) render the inserter on a plain front-end | |
| 1036 | + * request, so is_block_editor_context() is false there, yet a block must be | |
| 1037 | + * registered on `init` for get_availability() to report it and keep it in the | |
| 1038 | + * inserter. Never defer a block that ships in the `no-post-editor` preset — | |
| 1039 | + * those are precisely the blocks usable outside the post editor. | |
| 1040 | + */ | |
| 1041 | + $no_post_editor_blocks = $defer ? self::get_no_post_editor_extensions() : array(); | |
| 1042 | + | |
| 798 | 1043 | foreach ( static::get_extensions() as $extension ) { |
| 1044 | + if ( | |
| 1045 | + $defer | |
| 1046 | + && in_array( $extension, self::$lazy_blocks, true ) | |
| 1047 | + && ! in_array( $extension, $no_post_editor_blocks, true ) | |
| 1048 | + ) { | |
| 1049 | + self::$deferred_blocks[ $extension ] = true; | |
| 1050 | + continue; | |
| 1051 | + } | |
| 1052 | + | |
| 799 | 1053 | foreach ( $directories as $dirname ) { |
| 800 | 1054 | $path = JETPACK__PLUGIN_DIR . "extensions/{$dirname}/{$extension}/{$extension}.php"; |
| 801 | 1055 | |
| 802 | 1056 | if ( file_exists( $path ) ) { |
| @@ -804,12 +1058,314 @@ | ||
| 804 | 1058 | continue 2; |
| 805 | 1059 | } |
| 806 | 1060 | } |
| 807 | 1061 | } |
| 1062 | + | |
| 1063 | + if ( ! empty( self::$deferred_blocks ) ) { | |
| 1064 | + add_filter( 'pre_render_block', array( __CLASS__, 'lazy_register_deferred_block' ), 10, 3 ); | |
| 1065 | + } | |
| 808 | 1066 | } |
| 809 | 1067 | } |
| 810 | 1068 | |
| 811 | 1069 | /** |
| 1070 | + * Register deferred blocks present in a top-level block's subtree before it renders. | |
| 1071 | + * | |
| 1072 | + * Hooked to `pre_render_block`. For a top-level block ($parent_block is null) the | |
| 1073 | + * filter fires before core builds the block's WP_Block object, so we walk the whole | |
| 1074 | + * parsed subtree and register every deferred Jetpack block it contains. This must | |
| 1075 | + * happen at the top level: core resolves an inner block's `block_type` when it | |
| 1076 | + * constructs that inner WP_Block, which is *before* the inner block's own | |
| 1077 | + * `pre_render_block` fires — so registering a deferred dynamic block only when its | |
| 1078 | + * own inner filter fires would be too late and its render_callback would be skipped. | |
| 1079 | + * Inner-block invocations (non-null $parent_block) are ignored because the top-level | |
| 1080 | + * walk has already handled the whole tree. Returns $pre_render untouched. | |
| 1081 | + * | |
| 1082 | + * @since 16.0 | |
| 1083 | + * | |
| 1084 | + * @param string|null $pre_render The pre-rendered content. Default null. | |
| 1085 | + * @param array $parsed_block The parsed block being rendered. | |
| 1086 | + * @param \WP_Block|null $parent_block Parent block, or null for a top-level block. | |
| 1087 | + * | |
| 1088 | + * @return string|null Unchanged $pre_render. | |
| 1089 | + */ | |
| 1090 | + public static function lazy_register_deferred_block( $pre_render, $parsed_block, $parent_block = null ) { | |
| 1091 | + // Respect any earlier short-circuit, only act on top-level blocks, and stop | |
| 1092 | + // once every deferred block on the page has been registered. | |
| 1093 | + if ( null !== $pre_render || null !== $parent_block || empty( self::$deferred_blocks ) ) { | |
| 1094 | + return $pre_render; | |
| 1095 | + } | |
| 1096 | + | |
| 1097 | + self::register_deferred_blocks_in_subtree( $parsed_block ); | |
| 1098 | + | |
| 1099 | + return $pre_render; | |
| 1100 | + } | |
| 1101 | + | |
| 1102 | + /** | |
| 1103 | + * Recursively register any deferred Jetpack blocks found in a parsed block subtree. | |
| 1104 | + * | |
| 1105 | + * @since 16.0 | |
| 1106 | + * | |
| 1107 | + * @param array $parsed_block A parsed block (with optional `innerBlocks`). | |
| 1108 | + * @param array $seen_refs Reusable-block IDs already visited, to guard against cycles. | |
| 1109 | + * | |
| 1110 | + * @return void | |
| 1111 | + */ | |
| 1112 | + private static function register_deferred_blocks_in_subtree( $parsed_block, &$seen_refs = array() ) { | |
| 1113 | + if ( empty( self::$deferred_blocks ) ) { | |
| 1114 | + return; | |
| 1115 | + } | |
| 1116 | + | |
| 1117 | + $block_name = $parsed_block['blockName'] ?? ''; | |
| 1118 | + if ( '' !== $block_name && str_starts_with( $block_name, 'jetpack/' ) ) { | |
| 1119 | + $feature = substr( $block_name, strlen( 'jetpack/' ) ); | |
| 1120 | + if ( ! empty( self::$deferred_blocks[ $feature ] ) ) { | |
| 1121 | + // Only attempt registration once per block, whether or not it succeeds | |
| 1122 | + // (a block guarded by a connection/module check may intentionally not register). | |
| 1123 | + unset( self::$deferred_blocks[ $feature ] ); | |
| 1124 | + | |
| 1125 | + if ( ! self::is_registered( $block_name ) ) { | |
| 1126 | + self::load_and_register_deferred_block( $feature ); | |
| 1127 | + } | |
| 1128 | + } | |
| 1129 | + } | |
| 1130 | + | |
| 1131 | + /* | |
| 1132 | + * A synced pattern / reusable block (core/block) keeps its content in a separate | |
| 1133 | + * wp_block post that core only parses at render time (render_block_core_block), | |
| 1134 | + * so it is absent from this parsed tree. Resolve the reference and recurse so a | |
| 1135 | + * deferred block inside the pattern is registered before core builds its WP_Block. | |
| 1136 | + * | |
| 1137 | + * core/navigation has the same ref-based hidden-content shape (a wp_navigation | |
| 1138 | + * post) but is intentionally not handled: none of the deferred blocks can be | |
| 1139 | + * inserted into a navigation menu through the editor, and resolving the ref would | |
| 1140 | + * add a get_post()/parse_blocks() on essentially every front-end page (menus are | |
| 1141 | + * near-ubiquitous) for a case that cannot occur without hand-authored markup. | |
| 1142 | + */ | |
| 1143 | + if ( 'core/block' === $block_name && ! empty( $parsed_block['attrs']['ref'] ) ) { | |
| 1144 | + $ref = (int) $parsed_block['attrs']['ref']; | |
| 1145 | + if ( ! isset( $seen_refs[ $ref ] ) ) { | |
| 1146 | + $seen_refs[ $ref ] = true; | |
| 1147 | + $reusable_block = get_post( $ref ); | |
| 1148 | + if ( $reusable_block instanceof \WP_Post && 'wp_block' === $reusable_block->post_type ) { | |
| 1149 | + foreach ( parse_blocks( $reusable_block->post_content ) as $inner_block ) { | |
| 1150 | + self::register_deferred_blocks_in_subtree( $inner_block, $seen_refs ); | |
| 1151 | + } | |
| 1152 | + } | |
| 1153 | + } | |
| 1154 | + } | |
| 1155 | + | |
| 1156 | + if ( ! empty( $parsed_block['innerBlocks'] ) ) { | |
| 1157 | + foreach ( $parsed_block['innerBlocks'] as $inner_block ) { | |
| 1158 | + self::register_deferred_blocks_in_subtree( $inner_block, $seen_refs ); | |
| 1159 | + } | |
| 1160 | + } | |
| 1161 | + } | |
| 1162 | + | |
| 1163 | + /** | |
| 1164 | + * Include a deferred block's registration PHP and run the `init` callback it | |
| 1165 | + * adds, immediately. | |
| 1166 | + * | |
| 1167 | + * The block files register themselves with `add_action( 'init', … )`. By render | |
| 1168 | + * time `init` has long since fired, so including the file is not enough on its | |
| 1169 | + * own: we capture the callback(s) the include adds to `init` and invoke them now. | |
| 1170 | + * Only blocks in self::$lazy_blocks reach this path, and each adds exactly its | |
| 1171 | + * own registration callback to `init`, so this runs that single registration. | |
| 1172 | + * | |
| 1173 | + * @since 16.0 | |
| 1174 | + * | |
| 1175 | + * @param string $feature Block feature name (directory name without the `jetpack/` prefix). | |
| 1176 | + * | |
| 1177 | + * @return void | |
| 1178 | + */ | |
| 1179 | + private static function load_and_register_deferred_block( $feature ) { | |
| 1180 | + $path = JETPACK__PLUGIN_DIR . "extensions/blocks/{$feature}/{$feature}.php"; | |
| 1181 | + if ( ! file_exists( $path ) ) { | |
| 1182 | + self::warn_about_deferred_block_registration_failure( $feature, 'missing block registration file' ); | |
| 1183 | + return; | |
| 1184 | + } | |
| 1185 | + | |
| 1186 | + global $wp_filter; | |
| 1187 | + | |
| 1188 | + $before = isset( $wp_filter['init'] ) ? $wp_filter['init']->callbacks : array(); | |
| 1189 | + | |
| 1190 | + include_once $path; | |
| 1191 | + | |
| 1192 | + if ( ! isset( $wp_filter['init'] ) ) { | |
| 1193 | + self::warn_about_deferred_block_registration_failure( $feature, 'block file did not add an init callback' ); | |
| 1194 | + return; | |
| 1195 | + } | |
| 1196 | + | |
| 1197 | + $registered_callback = false; | |
| 1198 | + | |
| 1199 | + // Run (and then detach) any callback the include just added to `init`. | |
| 1200 | + foreach ( $wp_filter['init']->callbacks as $priority => $callbacks ) { | |
| 1201 | + foreach ( $callbacks as $id => $callback ) { | |
| 1202 | + if ( isset( $before[ $priority ][ $id ] ) ) { | |
| 1203 | + continue; | |
| 1204 | + } | |
| 1205 | + $registered_callback = true; | |
| 1206 | + if ( is_callable( $callback['function'] ) ) { | |
| 1207 | + call_user_func( $callback['function'] ); | |
| 1208 | + } | |
| 1209 | + remove_action( 'init', $callback['function'], $priority ); | |
| 1210 | + } | |
| 1211 | + } | |
| 1212 | + | |
| 1213 | + if ( ! $registered_callback ) { | |
| 1214 | + self::warn_about_deferred_block_registration_failure( $feature, 'block file did not add a new init callback' ); | |
| 1215 | + } | |
| 1216 | + } | |
| 1217 | + | |
| 1218 | + /** | |
| 1219 | + * Surface lazy-registration mistakes during debugging without adding front-end noise. | |
| 1220 | + * | |
| 1221 | + * @since 16.0 | |
| 1222 | + * | |
| 1223 | + * @param string $feature Block feature name (directory name without the `jetpack/` prefix). | |
| 1224 | + * @param string $reason Short reason for the failure. | |
| 1225 | + * | |
| 1226 | + * @return void | |
| 1227 | + */ | |
| 1228 | + private static function warn_about_deferred_block_registration_failure( $feature, $reason ) { | |
| 1229 | + if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ! function_exists( '_doing_it_wrong' ) ) { | |
| 1230 | + return; | |
| 1231 | + } | |
| 1232 | + | |
| 1233 | + _doing_it_wrong( | |
| 1234 | + __METHOD__, | |
| 1235 | + sprintf( | |
| 1236 | + /* translators: 1: Jetpack block feature name. 2: Failure reason. */ | |
| 1237 | + esc_html__( 'Lazy Jetpack block registration failed for "%1$s": %2$s.', 'jetpack' ), | |
| 1238 | + esc_html( $feature ), | |
| 1239 | + esc_html( $reason ) | |
| 1240 | + ), | |
| 1241 | + '16.0' | |
| 1242 | + ); | |
| 1243 | + } | |
| 1244 | + | |
| 1245 | + /** | |
| 1246 | + * Determine whether the current request is a block-editor context that needs | |
| 1247 | + * every Jetpack block loaded eagerly on `init`. | |
| 1248 | + * | |
| 1249 | + * Returns true for admin, REST, cron, WP-CLI and XML-RPC requests so the editor, | |
| 1250 | + * the `/wp/v2/block-types` endpoint and server-side rendering keep seeing the full | |
| 1251 | + * set of blocks. Returns false only for plain front-end web requests, where pure | |
| 1252 | + * display blocks are registered just-in-time as they render. | |
| 1253 | + * | |
| 1254 | + * This runs at module-load time (around after_setup_theme), before core defines | |
| 1255 | + * REST_REQUEST during parse_request, so self-hosted and Atomic REST requests are | |
| 1256 | + * detected from the request URL instead of the constant. That URL check cannot | |
| 1257 | + * work on WordPress.com Simple: its public API filters `rest_url_prefix` to an | |
| 1258 | + * empty string, so rest_get_url_prefix() returns '' and the REST roots computed | |
| 1259 | + * below collapse to '//', which no request path can match. Simple's requests are | |
| 1260 | + * detected via REST_API_REQUEST instead, which its API entry points define before | |
| 1261 | + * wp-load.php runs. | |
| 1262 | + * | |
| 1263 | + * @since 16.0 | |
| 1264 | + * | |
| 1265 | + * @return bool True for block-editor (non-front-end) contexts, false for plain front-end requests. | |
| 1266 | + */ | |
| 1267 | + private static function is_block_editor_context() { | |
| 1268 | + if ( is_admin() ) { | |
| 1269 | + return true; | |
| 1270 | + } | |
| 1271 | + | |
| 1272 | + /* | |
| 1273 | + * Treat any non-front-end execution context as block-editor. These are not the | |
| 1274 | + * front-end hot path this gate optimizes, and some still render block content | |
| 1275 | + * (e.g. cron-generated subscription e-mails) that depends on full registration. | |
| 1276 | + * | |
| 1277 | + * Core defines REST_REQUEST during parse_request, after this runs, so it is | |
| 1278 | + * normally still unset here; it is checked anyway so the result stays correct | |
| 1279 | + * if this is ever called later in the request. REST_API_REQUEST is what catches | |
| 1280 | + * WordPress.com Simple, where the URL check below cannot work at all (see the | |
| 1281 | + * method docblock). | |
| 1282 | + */ | |
| 1283 | + if ( | |
| 1284 | + Constants::is_true( 'DOING_CRON' ) | |
| 1285 | + || Constants::is_true( 'WP_CLI' ) | |
| 1286 | + || Constants::is_true( 'XMLRPC_REQUEST' ) | |
| 1287 | + || Constants::is_true( 'REST_REQUEST' ) | |
| 1288 | + || Constants::is_true( 'REST_API_REQUEST' ) | |
| 1289 | + ) { | |
| 1290 | + return true; | |
| 1291 | + } | |
| 1292 | + | |
| 1293 | + /* | |
| 1294 | + * No request URI means a non-web execution context (WP-CLI without it, test | |
| 1295 | + * suites, etc.). A genuine front-end page request always carries one, so it | |
| 1296 | + * costs nothing on the hot path to treat the empty case as "load eagerly". | |
| 1297 | + */ | |
| 1298 | + $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; | |
| 1299 | + if ( '' === $request_uri ) { | |
| 1300 | + return true; | |
| 1301 | + } | |
| 1302 | + | |
| 1303 | + /* | |
| 1304 | + * Anchor the REST root (home path + prefix) at the start of the request path, | |
| 1305 | + * so a front-end URL that merely carries the prefix in a query value or a | |
| 1306 | + * deeper path segment is not misread as a REST request. home_url() is used | |
| 1307 | + * rather than rest_url() so detection does not depend on permalink structure. | |
| 1308 | + * Both the rewritten `/wp-json/` form and the index-permalink | |
| 1309 | + * `/index.php/wp-json/` form (used when the site lacks pretty permalinks) are | |
| 1310 | + * matched. | |
| 1311 | + */ | |
| 1312 | + $path = (string) wp_parse_url( $request_uri, PHP_URL_PATH ); | |
| 1313 | + if ( '' !== $path ) { | |
| 1314 | + $home_path = trailingslashit( (string) wp_parse_url( home_url(), PHP_URL_PATH ) ); | |
| 1315 | + $rest_prefix = trailingslashit( rest_get_url_prefix() ); | |
| 1316 | + $rest_roots = array( | |
| 1317 | + $home_path . $rest_prefix, | |
| 1318 | + $home_path . 'index.php/' . $rest_prefix, | |
| 1319 | + ); | |
| 1320 | + foreach ( $rest_roots as $rest_root ) { | |
| 1321 | + if ( str_starts_with( trailingslashit( $path ), $rest_root ) ) { | |
| 1322 | + return true; | |
| 1323 | + } | |
| 1324 | + } | |
| 1325 | + } | |
| 1326 | + | |
| 1327 | + // Plain-permalink REST uses a `rest_route` query var; match the exact key. | |
| 1328 | + $query = (string) wp_parse_url( $request_uri, PHP_URL_QUERY ); | |
| 1329 | + if ( '' !== $query ) { | |
| 1330 | + parse_str( $query, $query_vars ); | |
| 1331 | + if ( ! empty( $query_vars['rest_route'] ) ) { | |
| 1332 | + return true; | |
| 1333 | + } | |
| 1334 | + } | |
| 1335 | + | |
| 1336 | + return false; | |
| 1337 | + } | |
| 1338 | + | |
| 1339 | + /** | |
| 1340 | + * Editor-oriented extensions that nonetheless have front-end side effects and must | |
| 1341 | + * therefore keep loading on every request, even outside the block editor. | |
| 1342 | + * | |
| 1343 | + * Keyed by directory ('plugins' / 'extended-blocks') for an exact, intentional match. | |
| 1344 | + * | |
| 1345 | + * @since 16.0 | |
| 1346 | + * | |
| 1347 | + * @var array | |
| 1348 | + */ | |
| 1349 | + private static $frontend_editor_extensions = array( | |
| 1350 | + 'plugins' => array( | |
| 1351 | + // Mounts the Reader Chat widget on the front end (wp_enqueue_scripts) and | |
| 1352 | + // wires the AI sidebar/provider registration AI Assistant depends on. | |
| 1353 | + 'ai-assistant-plugin', | |
| 1354 | + // Signals Big Sky via the jetpack_image_studio_enabled filter on `init`, | |
| 1355 | + // which can run on the front end. | |
| 1356 | + 'image-studio', | |
| 1357 | + ), | |
| 1358 | + 'extended-blocks' => array( | |
| 1359 | + // Registers the videopress/video block on `init`, required to render it on the front end. | |
| 1360 | + 'videopress-video', | |
| 1361 | + // Registers the `premium-content/container` plan availability that the Premium Content | |
| 1362 | + // block's front-end render reads via required_plan_checks(); skipping it breaks the paywall. | |
| 1363 | + 'premium-content-container', | |
| 1364 | + ), | |
| 1365 | + ); | |
| 1366 | + | |
| 1367 | + /** | |
| 812 | 1368 | * Loads PHP components of block editor extensions. |
| 813 | 1369 | * |
| 814 | 1370 | * @since 8.9.0 |
| 815 | 1371 | */ |
| @@ -820,15 +1376,30 @@ | ||
| 820 | 1376 | 'extended-blocks', |
| 821 | 1377 | 'plugins', |
| 822 | 1378 | ); |
| 823 | 1379 | |
| 1380 | + $is_editor_context = self::is_block_editor_context(); | |
| 1381 | + | |
| 824 | 1382 | // Collect the extension paths. |
| 825 | 1383 | foreach ( $extensions_to_load as $extension_to_load ) { |
| 826 | 1384 | $extensions_folder = glob( JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/*' ); |
| 827 | 1385 | |
| 1386 | + $frontend_allow_list = self::$frontend_editor_extensions[ $extension_to_load ] ?? array(); | |
| 1387 | + | |
| 828 | 1388 | // Require each of the extension files, in case it exists. |
| 829 | 1389 | foreach ( $extensions_folder as $extension_folder ) { |
| 830 | - $name = basename( $extension_folder ); | |
| 1390 | + $name = basename( $extension_folder ); | |
| 1391 | + | |
| 1392 | + /* | |
| 1393 | + * On plain front-end requests, only load extensions that have known | |
| 1394 | + * front-end side effects. Editor-only extensions are skipped here and | |
| 1395 | + * loaded on admin/REST (block-editor) requests instead, reducing the | |
| 1396 | + * per-front-end-request PHP/opcache footprint. | |
| 1397 | + */ | |
| 1398 | + if ( ! $is_editor_context && ! in_array( $name, $frontend_allow_list, true ) ) { | |
| 1399 | + continue; | |
| 1400 | + } | |
| 1401 | + | |
| 831 | 1402 | $extension_file_path = JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/' . $name . '/' . $name . '.php'; |
| 832 | 1403 | |
| 833 | 1404 | if ( file_exists( $extension_file_path ) ) { |
| 834 | 1405 | include_once $extension_file_path; |
| @@ -1009,11 +1580,11 @@ | ||
| 1009 | 1580 | |
| 1010 | 1581 | // Normalize URL. |
| 1011 | 1582 | $url = sprintf( |
| 1012 | 1583 | '%s://%s%s%s', |
| 1013 | - isset( $url_components['scheme'] ) ? $url_components['scheme'] : 'https', | |
| 1584 | + $url_components['scheme'] ?? 'https', | |
| 1014 | 1585 | $url_components['host'], |
| 1015 | - isset( $url_components['path'] ) ? $url_components['path'] : '/', | |
| 1586 | + $url_components['path'] ?? '/', | |
| 1016 | 1587 | isset( $url_components['query'] ) ? '?' . $url_components['query'] : '' |
| 1017 | 1588 | ); |
| 1018 | 1589 | |
| 1019 | 1590 | if ( ! empty( $url_components['fragment'] ) ) { |
| @@ -1201,8 +1772,12 @@ | ||
| 1201 | 1772 | } |
| 1202 | 1773 | |
| 1203 | 1774 | if ( ! empty( $features_data['available'][ $slug ] ) ) { |
| 1204 | 1775 | $plan = $features_data['available'][ $slug ][0]; |
| 1776 | + } elseif ( isset( self::$wpcom_minimum_plan_fallbacks[ $slug ] ) ) { | |
| 1777 | + // Fallback for features with conditional availability (e.g., sticker-based gating) | |
| 1778 | + // that don't appear in features_data['available']. | |
| 1779 | + $plan = self::$wpcom_minimum_plan_fallbacks[ $slug ]; | |
| 1205 | 1780 | } |
| 1206 | 1781 | } else { |
| 1207 | 1782 | // Jetpack sites. |
| 1208 | 1783 | $plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug ); |
| @@ -1229,15 +1804,15 @@ | ||
| 1229 | 1804 | return function ( $prepared_attributes, $block_content, $block ) use ( $render_callback, $slug ) { |
| 1230 | 1805 | $availability = self::get_cached_availability(); |
| 1231 | 1806 | $bare_slug = self::remove_extension_prefix( $slug ); |
| 1232 | 1807 | if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) { |
| 1233 | - return call_user_func( $render_callback, $prepared_attributes, $block_content ); | |
| 1808 | + return call_user_func( $render_callback, $prepared_attributes, $block_content, $block ); | |
| 1234 | 1809 | } |
| 1235 | 1810 | |
| 1236 | 1811 | // A preview of the block is rendered for admins on the frontend with an upgrade nudge. |
| 1237 | 1812 | if ( isset( $availability[ $bare_slug ] ) ) { |
| 1238 | 1813 | if ( self::should_show_frontend_preview( $availability[ $bare_slug ] ) ) { |
| 1239 | - $block_preview = call_user_func( $render_callback, $prepared_attributes, $block_content ); | |
| 1814 | + $block_preview = call_user_func( $render_callback, $prepared_attributes, $block_content, $block ); | |
| 1240 | 1815 | |
| 1241 | 1816 | // If the upgrade nudge isn't already being displayed by a parent block, display the nudge. |
| 1242 | 1817 | if ( isset( $block->attributes['shouldDisplayFrontendBanner'] ) && $block->attributes['shouldDisplayFrontendBanner'] ) { |
| 1243 | 1818 | $upgrade_nudge = self::upgrade_nudge( $availability[ $bare_slug ]['details']['required_plan'] ); |
| @@ -1263,9 +1838,9 @@ | ||
| 1263 | 1838 | * |
| 1264 | 1839 | * @return string |
| 1265 | 1840 | */ |
| 1266 | 1841 | public static function display_deprecated_block_message( $block_content, $block ) { |
| 1267 | - if ( in_array( $block['blockName'], self::$deprecated_blocks, true ) ) { | |
| 1842 | + if ( isset( $block['blockName'] ) && in_array( $block['blockName'], self::$deprecated_blocks, true ) ) { | |
| 1268 | 1843 | if ( current_user_can( 'edit_posts' ) ) { |
| 1269 | 1844 | $block_content = self::notice( |
| 1270 | 1845 | __( 'This block is no longer supported. Its contents will no longer be displayed to your visitors and as such this block should be removed.', 'jetpack' ), |
| 1271 | 1846 | 'warning', |
| @@ -1276,8 +1851,67 @@ | ||
| 1276 | 1851 | } |
| 1277 | 1852 | } |
| 1278 | 1853 | |
| 1279 | 1854 | return $block_content; |
| 1855 | + } | |
| 1856 | + | |
| 1857 | + /** | |
| 1858 | + * Register block metadata collection for Jetpack blocks. | |
| 1859 | + * This allows for more efficient block metadata loading by avoiding | |
| 1860 | + * individual block.json file reads at runtime. | |
| 1861 | + * | |
| 1862 | + * Uses wp_register_block_metadata_collection() if the manifest file | |
| 1863 | + * exists. The manifest file is auto-generated during the build process. | |
| 1864 | + * | |
| 1865 | + * Runs on plugins_loaded to ensure registration happens before individual | |
| 1866 | + * blocks register themselves on init. | |
| 1867 | + * | |
| 1868 | + * @static | |
| 1869 | + * @since 14.1 | |
| 1870 | + * @return void | |
| 1871 | + */ | |
| 1872 | + public static function register_block_metadata_collection() { | |
| 1873 | + $meta_file_path = JETPACK__PLUGIN_DIR . '_inc/blocks/blocks-manifest.php'; | |
| 1874 | + if ( file_exists( $meta_file_path ) ) { | |
| 1875 | + wp_register_block_metadata_collection( | |
| 1876 | + JETPACK__PLUGIN_DIR . '_inc/blocks/', | |
| 1877 | + $meta_file_path | |
| 1878 | + ); | |
| 1879 | + } | |
| 1880 | + } | |
| 1881 | + | |
| 1882 | + /** | |
| 1883 | + * Set the JS loading strategy for a block. | |
| 1884 | + * | |
| 1885 | + * @param string $block_name The block name. | |
| 1886 | + * @param array|bool $strategy The JS loading strategy. | |
| 1887 | + * | |
| 1888 | + * @since 15.0 | |
| 1889 | + */ | |
| 1890 | + public static function set_block_js_loading_strategy( $block_name, $strategy ) { | |
| 1891 | + self::$block_js_loading_strategies[ $block_name ] = $strategy; | |
| 1892 | + } | |
| 1893 | + | |
| 1894 | + /** | |
| 1895 | + * Get the JS loading strategy for a block. | |
| 1896 | + * | |
| 1897 | + * @param string $block_name The block name. | |
| 1898 | + * | |
| 1899 | + * @return array|bool The JS loading strategy for the block. | |
| 1900 | + * | |
| 1901 | + * @since 15.0 | |
| 1902 | + */ | |
| 1903 | + public static function get_block_js_loading_strategy( $block_name ) { | |
| 1904 | + $strategy = array( | |
| 1905 | + 'strategy' => 'defer', | |
| 1906 | + 'in_footer' => true, | |
| 1907 | + ); | |
| 1908 | + | |
| 1909 | + if ( isset( self::$block_js_loading_strategies[ $block_name ] ) ) { | |
| 1910 | + $strategy = self::$block_js_loading_strategies[ $block_name ]; | |
| 1911 | + } | |
| 1912 | + | |
| 1913 | + return $strategy; | |
| 1280 | 1914 | } |
| 1281 | 1915 | } |
| 1282 | 1916 | |
| 1283 | 1917 | if ( ( new Host() )->is_woa_site() ) { |