Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.48% covered (danger)
46.48%
251 / 540
32.61% covered (danger)
32.61%
15 / 46
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Gutenberg
46.83% covered (danger)
46.83%
251 / 536
32.61% covered (danger)
32.61%
15 / 46
7563.18
0.00% covered (danger)
0.00%
0 / 1
 is_gutenberg_version_available
87.50% covered (warning)
87.50%
21 / 24
0.00% covered (danger)
0.00%
0 / 1
8.12
 prepend_block_prefix
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 remove_extension_prefix
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 share_items
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_extension_available
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set_extension_unavailable
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 init
n/a
0 / 0
n/a
0 / 0
1
 reset
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get_blocks_directory
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 preset_exists
n/a
0 / 0
n/a
0 / 0
1
 get_preset
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 get_jetpack_gutenberg_extensions_allowed_list
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 get_available_extensions
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 is_registered_and_no_entry_in_availability
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_cached_availability
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_availability
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
 get_extensions
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
4.25
 is_registered
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_gutenberg_available
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 should_load
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 register_blocks_assets_base_url
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 load_assets_as_required
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 load_styles_as_required
27.27% covered (danger)
27.27%
3 / 11
0.00% covered (danger)
0.00%
0 / 1
19.85
 load_scripts_as_required
31.82% covered (danger)
31.82%
7 / 22
0.00% covered (danger)
0.00%
0 / 1
28.29
 block_has_asset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_asset_version
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 enqueue_block_editor_assets
0.00% covered (danger)
0.00%
0 / 90
0.00% covered (danger)
0.00%
0 / 1
210
 load_independent_blocks
64.29% covered (warning)
64.29%
9 / 14
0.00% covered (danger)
0.00%
0 / 1
10.92
 lazy_register_deferred_block
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 register_deferred_blocks_in_subtree
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
14
 load_and_register_deferred_block
85.00% covered (warning)
85.00%
17 / 20
0.00% covered (danger)
0.00%
0 / 1
9.27
 warn_about_deferred_block_registration_failure
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
 is_block_editor_context
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
0.00%
0 / 1
15.10
 load_block_editor_extensions
6.25% covered (danger)
6.25%
1 / 16
0.00% covered (danger)
0.00%
0 / 1
47.37
 blocks_variation
88.00% covered (warning)
88.00%
22 / 25
0.00% covered (danger)
0.00%
0 / 1
9.14
 get_extensions_preset_for_variation
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
17.58
 validate_block_embed_url
95.83% covered (success)
95.83%
23 / 24
0.00% covered (danger)
0.00%
0 / 1
12
 should_show_frontend_preview
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 upgrade_nudge
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 notice
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
72
 get_site_specific_features
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 set_availability_for_plan
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
 get_render_callback_with_availability_check
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
 display_deprecated_block_message
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 register_block_metadata_collection
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
3.19
 set_block_js_loading_strategy
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_block_js_loading_strategy
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Handles server-side registration and use of all blocks and plugins available in Jetpack for the block editor, aka Gutenberg.
4 * Works in tandem with client-side block registration via `index.json`
5 *
6 * @package automattic/jetpack
7 */
8
9use Automattic\Jetpack\Assets;
10use Automattic\Jetpack\Blocks;
11use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Constants;
14use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
15use Automattic\Jetpack\Modules;
16use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
17use Automattic\Jetpack\Status;
18use Automattic\Jetpack\Status\Host;
19
20if ( ! defined( 'ABSPATH' ) ) {
21    exit( 0 );
22}
23
24/**
25 * General Gutenberg editor specific functionality
26 */
27class Jetpack_Gutenberg {
28
29    /**
30     * Only these extensions can be registered. Used to control availability of beta blocks.
31     *
32     * @var array|null Extensions allowed list or `null` if not initialized yet.
33     * @see static::get_extensions()
34     */
35    private static $extensions = null;
36
37    /**
38     * Keeps track of the reasons why a given extension is unavailable.
39     *
40     * @var array Extensions availability information
41     */
42    private static $availability = array();
43
44    /**
45     * A cached array of the fully processed availability data. Keeps track of
46     * reasons why an extension is unavailable or missing.
47     *
48     * @var array Extensions availability information.
49     */
50    private static $cached_availability = null;
51
52    /**
53     * Site-specific features available.
54     * Their calculation can be expensive and slow, so we're caching it for the request.
55     *
56     * @var array Site-specific features
57     */
58    private static $site_specific_features = array();
59
60    /**
61     * List of deprecated blocks.
62     *
63     * @var array List of deprecated blocks.
64     */
65    private static $deprecated_blocks = array(
66        'jetpack/revue',
67    );
68
69    /**
70     * Display-only blocks whose registration PHP can be deferred until the block
71     * actually appears on a front-end page.
72     *
73     * Every block listed here has been verified to be "pure": the callback it hooks
74     * to `init` does nothing but call Blocks::jetpack_register_block() (plus trivial
75     * connection/module guards). It registers exactly one block type named
76     * `jetpack/<dir>` (matching its directory), and any front-end hooks it adds (asset
77     * enqueues, wp_footer, filters, â€¦) live inside its render callback, so they only
78     * run when the block is rendered.
79     *
80     * On plain front-end requests these blocks are NOT loaded on `init`. Instead they
81     * are registered just-in-time the first time the block (or a block whose subtree
82     * contains it) is encountered while rendering, via self::lazy_register_deferred_block()
83     * on `pre_render_block`. On admin/REST/cron/CLI/XML-RPC (block-editor) requests they
84     * keep loading eagerly so the editor, the block-types REST endpoint and server-side
85     * rendering are unaffected.
86     *
87     * A block must NOT be added here if:
88     *   - its `init` callback registers any other hook, post meta, REST route,
89     *     shortcode, block pattern or hooked-block;
90     *   - it registers more than one block type, or a block name that differs from its
91     *     directory name (e.g. videopress registers `jetpack/videopress-block`); or
92     *   - it uses `plan_check` (its availability is computed from the init-time
93     *     `jetpack_register_gutenberg_extensions` hook, which lazy registration bypasses,
94     *     so the front-end availability nudge/render could read a stale value); or
95     *   - a front-end path reads its entry from get_cached_availability() before the
96     *     block renders. Deferred blocks appear unavailable there until the lazy
97     *     registration callback runs; or
98     *   - its block file can be `require`d by another runtime code path after `init`
99     *     (e.g. slideshow is included by modules/shortcodes/slideshow.php). The lazy
100     *     loader registers a block by running the `init` callback its include adds; if
101     *     the file was already included elsewhere, the include is a no-op and the
102     *     callback would never run, so the block would silently fail to register; or
103     *   - another runtime code path calls a function defined in its block file
104     *     (e.g. button defines Button\render_email(), called by subscriptions and
105     *     memberships for WooCommerce e-mail rendering). Deferring the file would leave
106     *     that function undefined when the dependent path runs; or
107     *   - it registers a `render_email_callback`. That callback is read off the
108     *     registered block type by the WooCommerce e-mail editor â€” an out-of-band
109     *     renderer that does not go through `pre_render_block`/`do_blocks` â€” so the block
110     *     must already be registered when an e-mail containing it is rendered, which can
111     *     happen on a front-end request (e.g. a transactional e-mail sent during checkout).
112     *
113     * When in doubt, leave it out: omitted blocks simply keep their current eager
114     * behavior.
115     *
116     * @since $$next-version$$
117     * @var string[] Block feature names (directory names, without the `jetpack/` prefix).
118     */
119    private static $lazy_blocks = array(
120        'blog-stats',
121        'blogging-prompt',
122        'business-hours',
123        'eventbrite',
124        'gif',
125        'goodreads',
126        'google-calendar',
127        'google-docs-embed',
128        'image-compare',
129        'like',
130        'markdown',
131        'nextdoor',
132        'payments-intro',
133        'pinterest',
134        'related-posts',
135        'repeat-visitor',
136        'sharing-buttons',
137        'story',
138        'tock',
139        'top-posts',
140        'voice-to-content',
141    );
142
143    /**
144     * Blocks that were deferred on the current request and still need to be
145     * registered just-in-time when first rendered. Keyed by block feature name.
146     *
147     * @since $$next-version$$
148     * @var array<string,bool>
149     */
150    private static $deferred_blocks = array();
151
152    /**
153     * Fallback minimum plan requirements for WordPress.com/Atomic sites.
154     *
155     * Used when features have conditional availability (e.g., sticker-based gating)
156     * and don't appear in features_data['available']. This only affects the upsell
157     * message shown to users.
158     *
159     * @since 15.5
160     * @var array Feature slug => minimum WordPress.com plan slug.
161     */
162    private static $wpcom_minimum_plan_fallbacks = array(
163        'donations'              => 'value_bundle',
164        'payment-buttons'        => 'value_bundle',
165        'paypal-payment-buttons' => 'value_bundle',
166    );
167
168    /**
169     * Storing the contents of the preset file.
170     *
171     * Already been json_decode.
172     *
173     * @var null|object JSON decoded object after first usage.
174     */
175    private static $preset_cache = null;
176
177    /**
178     * Keep track of JS loading strategies for each block that needs it.
179     *
180     * @var array<string, array|bool>
181     *
182     * @since 15.0
183     */
184    private static $block_js_loading_strategies = array();
185
186    /**
187     * Check to see if a minimum version of Gutenberg is available. Because a Gutenberg version is not available in
188     * php if the Gutenberg plugin is not installed, if we know which minimum WP release has the required version we can
189     * optionally fall back to that.
190     *
191     * @param array  $version_requirements An array containing the required Gutenberg version and, if known, the WordPress version that was released with this minimum version.
192     * @param string $slug The slug of the block or plugin that has the gutenberg version requirement.
193     *
194     * @since 8.3.0
195     *
196     * @return boolean True if the version of gutenberg required by the block or plugin is available.
197     */
198    public static function is_gutenberg_version_available( $version_requirements, $slug ) {
199        global $wp_version;
200
201        // Bail if we don't at least have the gutenberg version requirement, the WP version is optional.
202        if ( empty( $version_requirements['gutenberg'] ) ) {
203            return false;
204        }
205
206        // If running a local dev build of gutenberg plugin GUTENBERG_DEVELOPMENT_MODE is set so assume correct version.
207        if ( defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE ) {
208            return true;
209        }
210
211        $version_available = false;
212
213        // If running a production build of the gutenberg plugin then GUTENBERG_VERSION is set, otherwise if WP version
214        // with required version of Gutenberg is known check that.
215        if ( defined( 'GUTENBERG_VERSION' ) ) {
216            $version_available = version_compare( GUTENBERG_VERSION, $version_requirements['gutenberg'], '>=' );
217        } elseif ( ! empty( $version_requirements['wp'] ) ) {
218            $version_available = version_compare( $wp_version, $version_requirements['wp'], '>=' );
219        }
220
221        if ( ! $version_available ) {
222            $slug = self::remove_extension_prefix( $slug );
223            self::set_extension_unavailable(
224                $slug,
225                'incorrect_gutenberg_version',
226                array(
227                    'required_feature' => $slug,
228                    'required_version' => $version_requirements,
229                    'current_version'  => array(
230                        'wp'        => $wp_version,
231                        'gutenberg' => defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null,
232                    ),
233                )
234            );
235        }
236
237        return $version_available;
238    }
239
240    /**
241     * Prepend the 'jetpack/' prefix to a block name
242     *
243     * @param string $block_name The block name.
244     *
245     * @return string The prefixed block name.
246     */
247    private static function prepend_block_prefix( $block_name ) {
248        return 'jetpack/' . $block_name;
249    }
250
251    /**
252     * Remove the 'jetpack/' or jetpack-' prefix from an extension name
253     *
254     * @param string $extension_name The extension name.
255     *
256     * @return string The unprefixed extension name.
257     */
258    public static function remove_extension_prefix( $extension_name ) {
259        if ( str_starts_with( $extension_name, 'jetpack/' ) || str_starts_with( $extension_name, 'jetpack-' ) ) {
260            return substr( $extension_name, strlen( 'jetpack/' ) );
261        }
262        return $extension_name;
263    }
264
265    /**
266     * Whether two arrays share at least one item
267     *
268     * @param array $a An array.
269     * @param array $b Another array.
270     *
271     * @return boolean True if $a and $b share at least one item
272     */
273    protected static function share_items( $a, $b ) {
274        return array_intersect( $a, $b ) !== array();
275    }
276
277    /**
278     * Set a (non-block) extension as available
279     *
280     * @param string $slug Slug of the extension.
281     */
282    public static function set_extension_available( $slug ) {
283        $slug                        = self::remove_extension_prefix( $slug );
284        self::$availability[ $slug ] = true;
285    }
286
287    /**
288     * Set the reason why an extension (block or plugin) is unavailable
289     *
290     * @param string $slug Slug of the extension.
291     * @param string $reason A string representation of why the extension is unavailable.
292     * @param array  $details A free-form array containing more information on why the extension is unavailable.
293     */
294    public static function set_extension_unavailable( $slug, $reason, $details = array() ) {
295        if (
296            // Extensions that require a plan may be eligible for upgrades.
297            'missing_plan' === $reason
298            && (
299                /**
300                 * Filter 'jetpack_block_editor_enable_upgrade_nudge' with `true` to enable or `false`
301                 * to disable paid feature upgrade nudges in the block editor.
302                 *
303                 * When this is changed to default to `true`, you should also update `modules/memberships/class-jetpack-memberships.php`
304                 * See https://github.com/Automattic/jetpack/pull/13394#pullrequestreview-293063378
305                 *
306                 * @since 7.7.0
307                 *
308                 * @param boolean
309                 */
310                ! apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false )
311                /** This filter is documented in _inc/lib/admin-pages/class.jetpack-react-page.php */
312                || ! apply_filters( 'jetpack_show_promotions', true )
313            )
314        ) {
315            // The block editor may apply an upgrade nudge if `missing_plan` is the reason.
316            // Add a descriptive suffix to disable behavior but provide informative reason.
317            $reason .= '__nudge_disabled';
318        }
319        $slug                        = self::remove_extension_prefix( $slug );
320        self::$availability[ $slug ] = array(
321            'reason'  => $reason,
322            'details' => $details,
323        );
324    }
325
326    /**
327     * Used to initialize the class, no longer in use.
328     *
329     * @return void
330     * @deprecated 12.2 No longer needed.
331     */
332    public static function init() {
333        _deprecated_function( __METHOD__, '12.2' );
334    }
335
336    /**
337     * Resets the class to its original state
338     *
339     * Used in unit tests
340     *
341     * @return void
342     */
343    public static function reset() {
344        self::$extensions                  = null;
345        self::$availability                = array();
346        self::$cached_availability         = null;
347        self::$block_js_loading_strategies = array();
348        self::$deferred_blocks             = array();
349    }
350
351    /**
352     * Return the Gutenberg extensions (blocks and plugins) directory
353     *
354     * @return string The Gutenberg extensions directory
355     */
356    public static function get_blocks_directory() {
357        /**
358         * Filter to select Gutenberg blocks directory
359         *
360         * @since 6.9.0
361         *
362         * @param string default: '_inc/blocks/'
363         */
364        return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
365    }
366
367    /**
368     * Checks for a given .json file in the blocks folder.
369     *
370     * @deprecated 14.3
371     *
372     * @param string $preset The name of the .json file to look for.
373     *
374     * @return bool True if the file is found.
375     */
376    public static function preset_exists( $preset ) {
377        _deprecated_function( __METHOD__, '14.3' );
378        return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
379    }
380
381    /**
382     * Decodes JSON loaded from the preset file in the blocks folder
383     *
384     * @since 14.3 Deprecated argument. Only one value is ever used.
385     *
386     * @param null $deprecated No longer used.
387     *
388     * @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
389     */
390    public static function get_preset( $deprecated = null ) {
391        if ( $deprecated ) {
392            _deprecated_argument( __METHOD__, '14.3', 'The $preset argument is no longer needed or used.' );
393        }
394
395        if ( self::$preset_cache ) {
396            return self::$preset_cache;
397        }
398
399        self::$preset_cache = json_decode(
400        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
401            file_get_contents( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . 'index.json' )
402        );
403        return self::$preset_cache;
404    }
405
406    /**
407     * Returns a list of Jetpack Gutenberg extensions (blocks and plugins), based on index.json
408     *
409     * @return array A list of blocks: eg [ 'publicize', 'markdown' ]
410     */
411    public static function get_jetpack_gutenberg_extensions_allowed_list() {
412        $preset_extensions_manifest = ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) ? array() : self::get_preset();
413        $blocks_variation           = self::blocks_variation();
414
415        return self::get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation );
416    }
417
418    /**
419     * Returns a diff from a combined list of allowed extensions and extensions determined to be excluded
420     *
421     * @param  array $allowed_extensions An array of allowed extensions.
422     *
423     * @return array A list of blocks: eg array( 'publicize', 'markdown' )
424     */
425    public static function get_available_extensions( $allowed_extensions = null ) {
426        $exclusions         = get_option( 'jetpack_excluded_extensions', array() );
427        $allowed_extensions = $allowed_extensions === null ? self::get_jetpack_gutenberg_extensions_allowed_list() : $allowed_extensions;
428
429        // Avoid errors if option data is not as expected.
430        if ( ! is_array( $exclusions ) ) {
431            $exclusions = array();
432        }
433
434        return array_diff( $allowed_extensions, $exclusions );
435    }
436
437    /**
438     * Return true if the extension has been registered and there's nothing in the availablilty array.
439     *
440     * @param string $extension The name of the extension.
441     *
442     * @return bool whether the extension has been registered and there's nothing in the availablilty array.
443     */
444    public static function is_registered_and_no_entry_in_availability( $extension ) {
445        return self::is_registered( 'jetpack/' . $extension ) && ! isset( self::$availability[ $extension ] );
446    }
447
448    /**
449     * Return true if the extension has a true entry in the availablilty array.
450     *
451     * @param string $extension The name of the extension.
452     *
453     * @return bool whether the extension has a true entry in the availablilty array.
454     */
455    public static function is_available( $extension ) {
456        return isset( self::$availability[ $extension ] ) && true === self::$availability[ $extension ];
457    }
458
459    /**
460     * Get the availability of each block / plugin, or return the cached availability
461     * if it has already been calculated. Avoids re-registering extensions when not
462     * necessary.
463     *
464     * @return array A list of block and plugins and their availability status.
465     */
466    public static function get_cached_availability() {
467        if ( null === self::$cached_availability ) {
468            self::$cached_availability = self::get_availability();
469        }
470        return self::$cached_availability;
471    }
472
473    /**
474     * Get availability of each block / plugin.
475     *
476     * @return array A list of block and plugins and their availablity status
477     */
478    public static function get_availability() {
479        /**
480         * Fires before Gutenberg extensions availability is computed.
481         *
482         * In the function call you supply, use `Blocks::jetpack_register_block()` to set a block as available.
483         * Alternatively, use `Jetpack_Gutenberg::set_extension_available()` (for a non-block plugin), and
484         * `Jetpack_Gutenberg::set_extension_unavailable()` (if the block or plugin should not be registered
485         * but marked as unavailable).
486         *
487         * @since 7.0.0
488         */
489        do_action( 'jetpack_register_gutenberg_extensions' );
490
491        $available_extensions = array();
492
493        foreach ( static::get_extensions() as $extension ) {
494            $is_available                       = self::is_registered_and_no_entry_in_availability( $extension ) || self::is_available( $extension );
495            $available_extensions[ $extension ] = array(
496                'available' => $is_available,
497            );
498
499            if ( ! $is_available ) {
500                $reason  = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['reason'] : 'missing_module';
501                $details = isset( self::$availability[ $extension ] ) ? self::$availability[ $extension ]['details'] : array();
502                $available_extensions[ $extension ]['unavailable_reason'] = $reason;
503                $available_extensions[ $extension ]['details']            = $details;
504            }
505        }
506
507        return $available_extensions;
508    }
509
510    /**
511     * Return the list of extensions that are available.
512     *
513     * @since 11.9
514     *
515     * @return array A list of block and plugins and their availability status.
516     */
517    public static function get_extensions() {
518        if ( ! static::should_load() ) {
519            return array();
520        }
521
522        if ( null === self::$extensions ) {
523            /**
524             * Filter the list of block editor extensions that are available through Jetpack.
525             *
526             * @since 7.0.0
527             *
528             * @param array
529             */
530            self::$extensions = apply_filters( 'jetpack_set_available_extensions', self::get_available_extensions() );
531
532            if ( ! is_array( self::$extensions ) ) {
533                _doing_it_wrong( __METHOD__, esc_html__( 'The jetpack_set_available_extensions filter must return an array.', 'jetpack' ), '14.9' );
534                self::$extensions = array();
535            }
536        }
537
538        return self::$extensions;
539    }
540
541    /**
542     * Check if an extension/block is already registered
543     *
544     * @since 7.2
545     *
546     * @param string $slug Name of extension/block to check.
547     *
548     * @return bool
549     */
550    public static function is_registered( $slug ) {
551        return WP_Block_Type_Registry::get_instance()->is_registered( $slug );
552    }
553
554    /**
555     * Check if Gutenberg editor is available
556     *
557     * @since 6.7.0
558     *
559     * @return bool
560     */
561    public static function is_gutenberg_available() {
562        return true;
563    }
564
565    /**
566     * Check whether conditions indicate Gutenberg Extensions (blocks and plugins) should be loaded
567     *
568     * Loading blocks and plugins is enabled by default and may be disabled via filter:
569     *   add_filter( 'jetpack_gutenberg', '__return_false' );
570     *
571     * @since 6.9.0
572     *
573     * @return bool
574     */
575    public static function should_load() {
576        if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
577            return false;
578        }
579
580        $return = true;
581
582        if ( ! ( new Modules() )->is_active( 'blocks' ) ) {
583            $return = false;
584        }
585
586        /**
587         * Filter to enable Gutenberg blocks.
588         *
589         * Defaults to true if (connected or in offline mode) and the Blocks module is active.
590         *
591         * @since 6.5.0
592         * @since 13.9 Filter is able to activate or deactivate Gutenberg blocks.
593         *
594         * @param bool true Whether to load Gutenberg blocks
595         */
596        return (bool) apply_filters( 'jetpack_gutenberg', $return );
597    }
598
599    /**
600     * Queue a script to set `Jetpack_Block_Assets_Base_Url`.
601     *
602     * In certain cases Webpack needs to know a base to load additional assets from.
603     * Normally it can determine that itself, but when JS concatenation is involved that tends to confuse it.
604     * We work around that by explicitly outputting a variable with the correct URL.
605     * We set that as its own "script" so we can reliably only output it once.
606     */
607    private static function register_blocks_assets_base_url() {
608        if ( ! wp_script_is( 'jetpack-blocks-assets-base-url', 'registered' ) ) {
609            // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- No actual script, so no version needed.
610            wp_register_script( 'jetpack-blocks-assets-base-url', false, array(), null, array( 'in_footer' => false ) );
611            $json_encode_flags = JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP;
612            if ( get_option( 'blog_charset' ) === 'UTF-8' ) {
613                $json_encode_flags |= JSON_UNESCAPED_UNICODE;
614            }
615            wp_add_inline_script(
616                'jetpack-blocks-assets-base-url',
617                'var Jetpack_Block_Assets_Base_Url=' . wp_json_encode( plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE ), $json_encode_flags ) . ';',
618                'before'
619            );
620        }
621    }
622
623    /**
624     * Only enqueue block assets when needed.
625     *
626     * @param string $type Slug of the block or absolute path to the block source code directory.
627     * @param array  $script_dependencies Script dependencies. Will be merged with automatically
628     *                                    detected script dependencies from the webpack build.
629     *
630     * @return void
631     */
632    public static function load_assets_as_required( $type, $script_dependencies = array() ) {
633        if ( is_admin() ) {
634            // A block's view assets will not be required in wp-admin.
635            return;
636        }
637
638        // Retrieve the feature from block.json if a path is passed.
639        if ( path_is_absolute( $type ) ) {
640            $metadata = Blocks::get_block_metadata_from_file( Blocks::get_path_to_block_metadata( $type ) );
641            $feature  = Blocks::get_block_feature_from_metadata( $metadata );
642
643            if ( ! empty( $feature ) ) {
644                $type = $feature;
645            }
646        }
647
648        $type = sanitize_title_with_dashes( $type );
649        self::load_styles_as_required( $type );
650        self::load_scripts_as_required( $type, $script_dependencies );
651    }
652
653    /**
654     * Only enqueue block sytles when needed.
655     *
656     * @param string $type Slug of the block.
657     *
658     * @since 7.2.0
659     *
660     * @return void
661     */
662    public static function load_styles_as_required( $type ) {
663        if ( is_admin() ) {
664            // A block's view assets will not be required in wp-admin.
665            return;
666        }
667
668        // Enqueue styles.
669        $style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
670        if ( self::block_has_asset( $style_relative_path ) ) {
671            $style_version = self::get_asset_version( $style_relative_path );
672            $view_style    = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
673            $view_style    = add_query_arg( 'minify', 'false', $view_style );
674
675            // If this is a customizer preview, render the style directly to the preview after autosave.
676            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
677            if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
678                // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
679                echo '<link rel="stylesheet" id="jetpack-block-' . esc_attr( $type ) . '" href="' . esc_attr( $view_style ) . '&amp;ver=' . esc_attr( $style_version ) . '" media="all">';
680            } else {
681                wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
682                wp_style_add_data( 'jetpack-block-' . $type, 'path', JETPACK__PLUGIN_DIR . $style_relative_path );
683            }
684        }
685    }
686
687    /**
688     * Only enqueue block scripts when needed.
689     *
690     * @param string $type Slug of the block.
691     * @param array  $script_dependencies Script dependencies. Will be merged with automatically
692     *                             detected script dependencies from the webpack build.
693     *
694     * @since 7.2.0
695     *
696     * @return void
697     */
698    public static function load_scripts_as_required( $type, $script_dependencies = array() ) {
699        if ( is_admin() ) {
700            // A block's view assets will not be required in wp-admin.
701            return;
702        }
703
704        self::register_blocks_assets_base_url();
705
706        // Enqueue script.
707        $script_relative_path  = self::get_blocks_directory() . $type . '/view.js';
708        $script_deps_path      = JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $type . '/view.asset.php';
709        $script_dependencies[] = 'jetpack-blocks-assets-base-url';
710        if ( file_exists( $script_deps_path ) ) {
711            $asset_manifest      = include $script_deps_path;
712            $script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) );
713        }
714
715        if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) {
716            $script_version = self::get_asset_version( $script_relative_path );
717            $view_script    = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
718            $view_script    = add_query_arg( 'minify', 'false', $view_script );
719            $strategy       = self::get_block_js_loading_strategy( $type );
720
721            // Enqueue dependencies.
722            wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, $strategy );
723
724            // If this is a customizer preview, enqueue the dependencies and render the script directly to the preview after autosave.
725            // phpcs:ignore WordPress.Security.NonceVerification.Recommended
726            if ( is_customize_preview() && ! empty( $_GET['customize_autosaved'] ) ) {
727                // The Map block is dependent on wp-element, and it doesn't appear to to be possible to load
728                // this dynamically into the customizer iframe currently.
729                if ( 'map' === $type ) {
730                    echo '<div>' . esc_html__( 'No map preview available. Publish and refresh to see this widget.', 'jetpack' ) . '</div>';
731                    echo '<script>';
732                    echo 'Array.from(document.getElementsByClassName(\'wp-block-jetpack-map\')).forEach(function(element){element.style.display = \'none\';})';
733                    echo '</script>';
734                } else {
735                    echo '<script id="jetpack-block-' . esc_attr( $type ) . '" src="' . esc_attr( $view_script ) . '&amp;ver=' . esc_attr( $script_version ) . '"></script>';
736                }
737            }
738        }
739    }
740
741    /**
742     * Check if an asset exists for a block.
743     *
744     * @param string $file Path of the file we are looking for.
745     *
746     * @return bool $block_has_asset Does the file exist.
747     */
748    public static function block_has_asset( $file ) {
749        return file_exists( JETPACK__PLUGIN_DIR . $file );
750    }
751
752    /**
753     * Get the version number to use when loading the file. Allows us to bypass cache when developing.
754     *
755     * @param string $file Path of the file we are looking for.
756     *
757     * @return string $script_version Version number.
758     */
759    public static function get_asset_version( $file ) {
760        return Jetpack::is_development_version() && self::block_has_asset( $file )
761            ? filemtime( JETPACK__PLUGIN_DIR . $file )
762            : JETPACK__VERSION;
763    }
764
765    /**
766     * Load Gutenberg editor assets
767     *
768     * @since 6.7.0
769     *
770     * @return void
771     */
772    public static function enqueue_block_editor_assets() {
773        if ( ! self::should_load() ) {
774            return;
775        }
776
777        $status = new Status();
778
779        // Required for Analytics. See _inc/lib/admin-pages/class.jetpack-admin-page.php.
780        if ( ! $status->is_offline_mode() && Jetpack::is_connection_ready() ) {
781            wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
782        }
783
784        $blocks_dir       = self::get_blocks_directory();
785        $blocks_variation = self::blocks_variation();
786
787        if ( 'production' !== $blocks_variation ) {
788            $blocks_env = '-' . esc_attr( $blocks_variation );
789        } else {
790            $blocks_env = '';
791        }
792
793        self::register_blocks_assets_base_url();
794
795        Assets::register_script(
796            'jetpack-blocks-editor',
797            "{$blocks_dir}editor{$blocks_env}.js",
798            JETPACK__PLUGIN_FILE,
799            array(
800                'textdomain'   => 'jetpack',
801                'dependencies' => array( 'jetpack-blocks-assets-base-url' ),
802            )
803        );
804
805        /**
806         * This can be called multiple times per page load in the admin, during the `enqueue_block_assets` action.
807         * These assets are necessary for the admin for editing but are not necessary for each pattern preview.
808         * Therefore we dequeue them, so they don't load for each pattern preview iframe.
809         */
810        if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
811            wp_dequeue_script( 'jp-tracks' );
812            wp_dequeue_script( 'jetpack-blocks-editor' );
813
814            return;
815        }
816
817        // Hack around #20357 (specifically, that the editor bundle depends on
818        // wp-edit-post but wp-edit-post's styles break the Widget Editor and
819        // Site Editor) until a real fix gets unblocked.
820        // @todo Remove this once #20357 is properly fixed.
821        $wp_styles_fix = wp_styles()->query( 'jetpack-blocks-editor', 'registered' );
822        if ( empty( $wp_styles_fix ) ) {
823            wp_die( 'Your installation of Jetpack is incomplete. Please run "jetpack build plugins/jetpack" in your dev env.' );
824        }
825        wp_styles()->query( 'jetpack-blocks-editor', 'registered' )->deps = array();
826
827        Assets::enqueue_script( 'jetpack-blocks-editor' );
828
829        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
830            $user                      = wp_get_current_user();
831            $user_data                 = array(
832                'email'    => $user->user_email,
833                'userid'   => $user->ID,
834                'username' => $user->user_login,
835            );
836            $blog_id                   = get_current_blog_id();
837            $is_current_user_connected = true;
838        } else {
839            $user_data                 = Jetpack_Tracks_Client::get_connected_user_tracks_identity();
840            $blog_id                   = Jetpack_Options::get_option( 'id', 0 );
841            $is_current_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected();
842        }
843
844        if ( $blocks_variation === 'beta' && $is_current_user_connected ) {
845            wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Constants::get_constant( 'JETPACK__VERSION' ) );
846        }
847        // AI Assistant
848        $ai_assistant_state = array(
849            'is-enabled' => apply_filters( 'jetpack_ai_enabled', true ),
850        );
851
852        $screen_base = null;
853        if ( function_exists( 'get_current_screen' ) ) {
854            $current_screen = get_current_screen();
855            $screen_base    = $current_screen ? $current_screen->base : null;
856        }
857
858        $modules = array();
859        if ( class_exists( 'Jetpack_Core_API_Module_List_Endpoint' ) ) {
860            $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint();
861            $modules              = $module_list_endpoint->get_modules();
862        }
863
864        $jetpack_plan  = Jetpack_Plan::get();
865        $initial_state = array(
866            'available_blocks' => self::get_availability(),
867            'blocks_variation' => $blocks_variation,
868            'modules'          => $modules,
869            'jetpack'          => array(
870                'is_active'                     => Jetpack::is_connection_ready(),
871                'is_current_user_connected'     => $is_current_user_connected,
872                /** This filter is documented in class.jetpack-gutenberg.php */
873                'enable_upgrade_nudge'          => apply_filters( 'jetpack_block_editor_enable_upgrade_nudge', false ),
874                'is_private_site'               => $status->is_private_site(),
875                'is_coming_soon'                => $status->is_coming_soon(),
876                'is_offline_mode'               => $status->is_offline_mode(),
877                'is_newsletter_feature_enabled' => class_exists( '\Jetpack_Memberships' ),
878                // this is the equivalent of JP initial state siteData.showMyJetpack (class-jetpack-redux-state-helper)
879                // used to determine if we can link to My Jetpack from the block editor
880                'is_my_jetpack_available'       => My_Jetpack_Initializer::should_initialize(),
881                'jetpack_plan'                  => array(
882                    'data' => $jetpack_plan['product_slug'],
883                ),
884                /**
885                 * Enable the RePublicize UI in the block editor context.
886                 *
887                 * @module publicize
888                 *
889                 * @since 10.3.0
890                 * @deprecated 11.5 This is a feature flag that is no longer used.
891                 *
892                 * @param bool true Enable the RePublicize UI in the block editor context. Defaults to true.
893                 */
894                'republicize_enabled'           => apply_filters( 'jetpack_block_editor_republicize_feature', true ),
895            ),
896            'siteFragment'     => $status->get_site_suffix(),
897            'adminUrl'         => esc_url( admin_url() ),
898            'tracksUserData'   => $user_data,
899            'wpcomBlogId'      => $blog_id,
900            'allowedMimeTypes' => wp_get_mime_types(),
901            'siteLocale'       => str_replace( '_', '-', get_locale() ),
902            'ai-assistant'     => $ai_assistant_state,
903            'screenBase'       => $screen_base,
904            /**
905             * Add your own feature flags to the block editor.
906             *
907             * You can access the feature flags in the block editor via hasFeatureFlag( 'your-feature-flag' ) function.
908             *
909             * @since 14.8
910             *
911             * @param array true Enable the RePublicize UI in the block editor context. Defaults to true.
912             */
913            'feature_flags'    => apply_filters( 'jetpack_block_editor_feature_flags', array() ),
914            'pluginBasePath'   => plugins_url( '', Constants::get_constant( 'JETPACK__PLUGIN_FILE' ) ),
915        );
916
917        wp_localize_script(
918            'jetpack-blocks-editor',
919            'Jetpack_Editor_Initial_State',
920            $initial_state
921        );
922
923        // Adds Connection package initial state.
924        Connection_Initial_State::render_script( 'jetpack-blocks-editor' );
925    }
926
927    /**
928     * Some blocks do not depend on a specific module,
929     * and can consequently be loaded outside of the usual modules.
930     * We will look for such modules in the extensions/ directory.
931     *
932     * @since 7.1.0
933     * @since $$next-version$$ Pure display blocks are deferred on front-end requests and registered on first render.
934     * @see wp_common_block_scripts_and_styles()
935     */
936    public static function load_independent_blocks() {
937        if ( self::should_load() ) {
938            /**
939             * Look for files that match our list of available Jetpack Gutenberg extensions (blocks and plugins).
940             * If available, load them.
941             */
942            $directories = array( 'blocks', 'plugins', 'extended-blocks' );
943
944            /*
945             * On plain front-end requests, defer the registration PHP of pure display
946             * blocks (see self::$lazy_blocks) until the block is actually encountered
947             * while rendering. The block editor, the block-types REST endpoint and
948             * server-side rendering all run in a "block-editor context" and keep loading
949             * every block eagerly, so their behavior is unchanged.
950             */
951            $defer = ! self::is_block_editor_context();
952
953            foreach ( static::get_extensions() as $extension ) {
954                if ( $defer && in_array( $extension, self::$lazy_blocks, true ) ) {
955                    self::$deferred_blocks[ $extension ] = true;
956                    continue;
957                }
958
959                foreach ( $directories as $dirname ) {
960                    $path = JETPACK__PLUGIN_DIR . "extensions/{$dirname}/{$extension}/{$extension}.php";
961
962                    if ( file_exists( $path ) ) {
963                        include_once $path;
964                        continue 2;
965                    }
966                }
967            }
968
969            if ( ! empty( self::$deferred_blocks ) ) {
970                add_filter( 'pre_render_block', array( __CLASS__, 'lazy_register_deferred_block' ), 10, 3 );
971            }
972        }
973    }
974
975    /**
976     * Register deferred blocks present in a top-level block's subtree before it renders.
977     *
978     * Hooked to `pre_render_block`. For a top-level block ($parent_block is null) the
979     * filter fires before core builds the block's WP_Block object, so we walk the whole
980     * parsed subtree and register every deferred Jetpack block it contains. This must
981     * happen at the top level: core resolves an inner block's `block_type` when it
982     * constructs that inner WP_Block, which is *before* the inner block's own
983     * `pre_render_block` fires â€” so registering a deferred dynamic block only when its
984     * own inner filter fires would be too late and its render_callback would be skipped.
985     * Inner-block invocations (non-null $parent_block) are ignored because the top-level
986     * walk has already handled the whole tree. Returns $pre_render untouched.
987     *
988     * @since 16.0
989     *
990     * @param string|null    $pre_render   The pre-rendered content. Default null.
991     * @param array          $parsed_block The parsed block being rendered.
992     * @param \WP_Block|null $parent_block Parent block, or null for a top-level block.
993     *
994     * @return string|null Unchanged $pre_render.
995     */
996    public static function lazy_register_deferred_block( $pre_render, $parsed_block, $parent_block = null ) {
997        // Respect any earlier short-circuit, only act on top-level blocks, and stop
998        // once every deferred block on the page has been registered.
999        if ( null !== $pre_render || null !== $parent_block || empty( self::$deferred_blocks ) ) {
1000            return $pre_render;
1001        }
1002
1003        self::register_deferred_blocks_in_subtree( $parsed_block );
1004
1005        return $pre_render;
1006    }
1007
1008    /**
1009     * Recursively register any deferred Jetpack blocks found in a parsed block subtree.
1010     *
1011     * @since $$next-version$$
1012     *
1013     * @param array $parsed_block A parsed block (with optional `innerBlocks`).
1014     * @param array $seen_refs    Reusable-block IDs already visited, to guard against cycles.
1015     *
1016     * @return void
1017     */
1018    private static function register_deferred_blocks_in_subtree( $parsed_block, &$seen_refs = array() ) {
1019        if ( empty( self::$deferred_blocks ) ) {
1020            return;
1021        }
1022
1023        $block_name = $parsed_block['blockName'] ?? '';
1024        if ( '' !== $block_name && str_starts_with( $block_name, 'jetpack/' ) ) {
1025            $feature = substr( $block_name, strlen( 'jetpack/' ) );
1026            if ( ! empty( self::$deferred_blocks[ $feature ] ) ) {
1027                // Only attempt registration once per block, whether or not it succeeds
1028                // (a block guarded by a connection/module check may intentionally not register).
1029                unset( self::$deferred_blocks[ $feature ] );
1030
1031                if ( ! self::is_registered( $block_name ) ) {
1032                    self::load_and_register_deferred_block( $feature );
1033                }
1034            }
1035        }
1036
1037        /*
1038         * A synced pattern / reusable block (core/block) keeps its content in a separate
1039         * wp_block post that core only parses at render time (render_block_core_block),
1040         * so it is absent from this parsed tree. Resolve the reference and recurse so a
1041         * deferred block inside the pattern is registered before core builds its WP_Block.
1042         *
1043         * core/navigation has the same ref-based hidden-content shape (a wp_navigation
1044         * post) but is intentionally not handled: none of the deferred blocks can be
1045         * inserted into a navigation menu through the editor, and resolving the ref would
1046         * add a get_post()/parse_blocks() on essentially every front-end page (menus are
1047         * near-ubiquitous) for a case that cannot occur without hand-authored markup.
1048         */
1049        if ( 'core/block' === $block_name && ! empty( $parsed_block['attrs']['ref'] ) ) {
1050            $ref = (int) $parsed_block['attrs']['ref'];
1051            if ( ! isset( $seen_refs[ $ref ] ) ) {
1052                $seen_refs[ $ref ] = true;
1053                $reusable_block    = get_post( $ref );
1054                if ( $reusable_block instanceof \WP_Post && 'wp_block' === $reusable_block->post_type ) {
1055                    foreach ( parse_blocks( $reusable_block->post_content ) as $inner_block ) {
1056                        self::register_deferred_blocks_in_subtree( $inner_block, $seen_refs );
1057                    }
1058                }
1059            }
1060        }
1061
1062        if ( ! empty( $parsed_block['innerBlocks'] ) ) {
1063            foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
1064                self::register_deferred_blocks_in_subtree( $inner_block, $seen_refs );
1065            }
1066        }
1067    }
1068
1069    /**
1070     * Include a deferred block's registration PHP and run the `init` callback it
1071     * adds, immediately.
1072     *
1073     * The block files register themselves with `add_action( 'init', â€¦ )`. By render
1074     * time `init` has long since fired, so including the file is not enough on its
1075     * own: we capture the callback(s) the include adds to `init` and invoke them now.
1076     * Only blocks in self::$lazy_blocks reach this path, and each adds exactly its
1077     * own registration callback to `init`, so this runs that single registration.
1078     *
1079     * @since $$next-version$$
1080     *
1081     * @param string $feature Block feature name (directory name without the `jetpack/` prefix).
1082     *
1083     * @return void
1084     */
1085    private static function load_and_register_deferred_block( $feature ) {
1086        $path = JETPACK__PLUGIN_DIR . "extensions/blocks/{$feature}/{$feature}.php";
1087        if ( ! file_exists( $path ) ) {
1088            self::warn_about_deferred_block_registration_failure( $feature, 'missing block registration file' );
1089            return;
1090        }
1091
1092        global $wp_filter;
1093
1094        $before = isset( $wp_filter['init'] ) ? $wp_filter['init']->callbacks : array();
1095
1096        include_once $path;
1097
1098        if ( ! isset( $wp_filter['init'] ) ) {
1099            self::warn_about_deferred_block_registration_failure( $feature, 'block file did not add an init callback' );
1100            return;
1101        }
1102
1103        $registered_callback = false;
1104
1105        // Run (and then detach) any callback the include just added to `init`.
1106        foreach ( $wp_filter['init']->callbacks as $priority => $callbacks ) {
1107            foreach ( $callbacks as $id => $callback ) {
1108                if ( isset( $before[ $priority ][ $id ] ) ) {
1109                    continue;
1110                }
1111                $registered_callback = true;
1112                if ( is_callable( $callback['function'] ) ) {
1113                    call_user_func( $callback['function'] );
1114                }
1115                remove_action( 'init', $callback['function'], $priority );
1116            }
1117        }
1118
1119        if ( ! $registered_callback ) {
1120            self::warn_about_deferred_block_registration_failure( $feature, 'block file did not add a new init callback' );
1121        }
1122    }
1123
1124    /**
1125     * Surface lazy-registration mistakes during debugging without adding front-end noise.
1126     *
1127     * @since $$next-version$$
1128     *
1129     * @param string $feature Block feature name (directory name without the `jetpack/` prefix).
1130     * @param string $reason  Short reason for the failure.
1131     *
1132     * @return void
1133     */
1134    private static function warn_about_deferred_block_registration_failure( $feature, $reason ) {
1135        if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ! function_exists( '_doing_it_wrong' ) ) {
1136            return;
1137        }
1138
1139        _doing_it_wrong(
1140            __METHOD__,
1141            sprintf(
1142                /* translators: 1: Jetpack block feature name. 2: Failure reason. */
1143                esc_html__( 'Lazy Jetpack block registration failed for "%1$s": %2$s.', 'jetpack' ),
1144                esc_html( $feature ),
1145                esc_html( $reason )
1146            ),
1147            '16.0'
1148        );
1149    }
1150
1151    /**
1152     * Determine whether the current request is a block-editor context that needs
1153     * every Jetpack block loaded eagerly on `init`.
1154     *
1155     * Returns true for admin, REST, cron, WP-CLI and XML-RPC requests so the editor,
1156     * the `/wp/v2/block-types` endpoint and server-side rendering keep seeing the full
1157     * set of blocks. Returns false only for plain front-end web requests, where pure
1158     * display blocks are registered just-in-time as they render.
1159     *
1160     * This runs at module-load time (around after_setup_theme), before core defines
1161     * REST_REQUEST during parse_request, so REST requests are detected from the
1162     * request URL instead of the constant.
1163     *
1164     * @since $$next-version$$
1165     *
1166     * @return bool True for block-editor (non-front-end) contexts, false for plain front-end requests.
1167     */
1168    private static function is_block_editor_context() {
1169        if ( is_admin() ) {
1170            return true;
1171        }
1172
1173        /*
1174         * Treat any non-front-end execution context as block-editor. These are not the
1175         * front-end hot path this gate optimizes, and some still render block content
1176         * (e.g. cron-generated subscription e-mails) that depends on full registration.
1177         */
1178        if (
1179            ( defined( 'DOING_CRON' ) && DOING_CRON )
1180            || ( defined( 'WP_CLI' ) && WP_CLI )
1181            || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
1182        ) {
1183            return true;
1184        }
1185
1186        /*
1187         * No request URI means a non-web execution context (WP-CLI without it, test
1188         * suites, etc.). A genuine front-end page request always carries one, so it
1189         * costs nothing on the hot path to treat the empty case as "load eagerly".
1190         */
1191        $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
1192        if ( '' === $request_uri ) {
1193            return true;
1194        }
1195
1196        /*
1197         * Anchor the REST root (home path + prefix) at the start of the request path,
1198         * so a front-end URL that merely carries the prefix in a query value or a
1199         * deeper path segment is not misread as a REST request. home_url() is used
1200         * rather than rest_url() so detection does not depend on permalink structure.
1201         * Both the rewritten `/wp-json/` form and the index-permalink
1202         * `/index.php/wp-json/` form (used when the site lacks pretty permalinks) are
1203         * matched.
1204         */
1205        $path = (string) wp_parse_url( $request_uri, PHP_URL_PATH );
1206        if ( '' !== $path ) {
1207            $home_path   = trailingslashit( (string) wp_parse_url( home_url(), PHP_URL_PATH ) );
1208            $rest_prefix = trailingslashit( rest_get_url_prefix() );
1209            $rest_roots  = array(
1210                $home_path . $rest_prefix,
1211                $home_path . 'index.php/' . $rest_prefix,
1212            );
1213            foreach ( $rest_roots as $rest_root ) {
1214                if ( str_starts_with( trailingslashit( $path ), $rest_root ) ) {
1215                    return true;
1216                }
1217            }
1218        }
1219
1220        // Plain-permalink REST uses a `rest_route` query var; match the exact key.
1221        $query = (string) wp_parse_url( $request_uri, PHP_URL_QUERY );
1222        if ( '' !== $query ) {
1223            parse_str( $query, $query_vars );
1224            if ( ! empty( $query_vars['rest_route'] ) ) {
1225                return true;
1226            }
1227        }
1228
1229        return false;
1230    }
1231
1232    /**
1233     * Editor-oriented extensions that nonetheless have front-end side effects and must
1234     * therefore keep loading on every request, even outside the block editor.
1235     *
1236     * Keyed by directory ('plugins' / 'extended-blocks') for an exact, intentional match.
1237     *
1238     * @since 16.0
1239     *
1240     * @var array
1241     */
1242    private static $frontend_editor_extensions = array(
1243        'plugins'         => array(
1244            // Mounts the Reader Chat widget on the front end (wp_enqueue_scripts) and
1245            // wires the AI sidebar/provider registration AI Assistant depends on.
1246            'ai-assistant-plugin',
1247            // Signals Big Sky via the jetpack_image_studio_enabled filter on `init`,
1248            // which can run on the front end.
1249            'image-studio',
1250            // Filters get_avatar_data on the front end to customize AI-authored note avatars.
1251            'block-notes',
1252        ),
1253        'extended-blocks' => array(
1254            // Registers the videopress/video block on `init`, required to render it on the front end.
1255            'videopress-video',
1256            // Registers the `premium-content/container` plan availability that the Premium Content
1257            // block's front-end render reads via required_plan_checks(); skipping it breaks the paywall.
1258            'premium-content-container',
1259        ),
1260    );
1261
1262    /**
1263     * Loads PHP components of block editor extensions.
1264     *
1265     * @since 8.9.0
1266     */
1267    public static function load_block_editor_extensions() {
1268        if ( self::should_load() ) {
1269            // Block editor extensions to load.
1270            $extensions_to_load = array(
1271                'extended-blocks',
1272                'plugins',
1273            );
1274
1275            $is_editor_context = self::is_block_editor_context();
1276
1277            // Collect the extension paths.
1278            foreach ( $extensions_to_load as $extension_to_load ) {
1279                $extensions_folder = glob( JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/*' );
1280
1281                $frontend_allow_list = self::$frontend_editor_extensions[ $extension_to_load ] ?? array();
1282
1283                // Require each of the extension files, in case it exists.
1284                foreach ( $extensions_folder as $extension_folder ) {
1285                    $name = basename( $extension_folder );
1286
1287                    /*
1288                     * On plain front-end requests, only load extensions that have known
1289                     * front-end side effects. Editor-only extensions are skipped here and
1290                     * loaded on admin/REST (block-editor) requests instead, reducing the
1291                     * per-front-end-request PHP/opcache footprint.
1292                     */
1293                    if ( ! $is_editor_context && ! in_array( $name, $frontend_allow_list, true ) ) {
1294                        continue;
1295                    }
1296
1297                    $extension_file_path = JETPACK__PLUGIN_DIR . 'extensions/' . $extension_to_load . '/' . $name . '/' . $name . '.php';
1298
1299                    if ( file_exists( $extension_file_path ) ) {
1300                        include_once $extension_file_path;
1301                    }
1302                }
1303            }
1304        }
1305    }
1306
1307    /**
1308     * Determine whether a site should use the default set of blocks, or a custom set.
1309     * Possible variations are currently beta, experimental, and production.
1310     *
1311     * @since 8.1.0
1312     *
1313     * @return string $block_varation production|beta|experimental
1314     */
1315    public static function blocks_variation() {
1316        // Default to production blocks.
1317        $block_varation = 'production';
1318
1319        /*
1320         * Prefer to use this JETPACK_BLOCKS_VARIATION constant
1321         * or the jetpack_blocks_variation filter
1322         * to set the block variation in your code.
1323         */
1324        $default = Constants::get_constant( 'JETPACK_BLOCKS_VARIATION' );
1325        if ( ! empty( $default ) && in_array( $default, array( 'beta', 'experimental', 'production' ), true ) ) {
1326            $block_varation = $default;
1327        }
1328
1329        /**
1330        * Alternative to `JETPACK_BETA_BLOCKS`, set to `true` to load Beta Blocks.
1331        *
1332        * @since 6.9.0
1333        * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
1334        *
1335        * @param boolean
1336        */
1337        $is_beta = apply_filters_deprecated(
1338            'jetpack_load_beta_blocks',
1339            array( false ),
1340            'jetpack-11.8.0',
1341            'jetpack_blocks_variation'
1342        );
1343
1344        /*
1345         * Switch to beta blocks if you use the JETPACK_BETA_BLOCKS constant
1346         * or the deprecated jetpack_load_beta_blocks filter.
1347         * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
1348         */
1349        if (
1350            empty( $default )
1351            && (
1352                $is_beta
1353                || Constants::is_true( 'JETPACK_BETA_BLOCKS' )
1354            )
1355        ) {
1356            $block_varation = 'beta';
1357        }
1358
1359        /**
1360        * Alternative to `JETPACK_EXPERIMENTAL_BLOCKS`, set to `true` to load Experimental Blocks.
1361        *
1362        * @since 6.9.0
1363        * @deprecated 11.8.0 Use jetpack_blocks_variation filter instead.
1364        *
1365        * @param boolean
1366        */
1367        $is_experimental = apply_filters_deprecated(
1368            'jetpack_load_experimental_blocks',
1369            array( false ),
1370            'jetpack-11.8.0',
1371            'jetpack_blocks_variation'
1372        );
1373
1374        /*
1375         * Switch to experimental blocks if you use the JETPACK_EXPERIMENTAL_BLOCKS constant
1376         * or the deprecated jetpack_load_experimental_blocks filter.
1377         * This only applies when not using the newer JETPACK_BLOCKS_VARIATION constant.
1378         */
1379        if (
1380            empty( $default )
1381            && (
1382                $is_experimental
1383                || Constants::is_true( 'JETPACK_EXPERIMENTAL_BLOCKS' )
1384            )
1385        ) {
1386            $block_varation = 'experimental';
1387        }
1388
1389        /**
1390         * Allow customizing the variation of blocks in use on a site.
1391         * Overwrites any previously set values, whether by constant or filter.
1392         *
1393         * @since 8.1.0
1394         *
1395         * @param string $block_variation Can be beta, experimental, and production. Defaults to production.
1396         */
1397        return apply_filters( 'jetpack_blocks_variation', $block_varation );
1398    }
1399
1400    /**
1401     * Get a list of extensions available for the variation you chose.
1402     *
1403     * @since 8.1.0
1404     *
1405     * @param object $preset_extensions_manifest List of extensions available in Jetpack.
1406     * @param string $blocks_variation           Subset of blocks. production|beta|experimental.
1407     *
1408     * @return array $preset_extensions Array of extensions for that variation
1409     */
1410    public static function get_extensions_preset_for_variation( $preset_extensions_manifest, $blocks_variation ) {
1411        $preset_extensions = isset( $preset_extensions_manifest->{ $blocks_variation } )
1412                ? (array) $preset_extensions_manifest->{ $blocks_variation }
1413                : array();
1414
1415        /*
1416         * Experimental and Beta blocks need the production blocks as well.
1417         */
1418        if (
1419            'experimental' === $blocks_variation
1420            || 'beta' === $blocks_variation
1421        ) {
1422            $production_extensions = isset( $preset_extensions_manifest->production )
1423                ? (array) $preset_extensions_manifest->production
1424                : array();
1425
1426            $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
1427        }
1428
1429        /*
1430         * Beta blocks need the experimental blocks as well.
1431         *
1432         * If you've chosen to see Beta blocks,
1433         * we want to make all blocks available to you:
1434         * - Production
1435         * - Experimental
1436         * - Beta
1437         */
1438        if ( 'beta' === $blocks_variation ) {
1439            $production_extensions = isset( $preset_extensions_manifest->experimental )
1440                ? (array) $preset_extensions_manifest->experimental
1441                : array();
1442
1443            $preset_extensions = array_unique( array_merge( $preset_extensions, $production_extensions ) );
1444        }
1445
1446        return $preset_extensions;
1447    }
1448
1449    /**
1450     * Validate a URL used in a SSR block.
1451     *
1452     * @since 8.3.0
1453     *
1454     * @param string $url      URL saved as an attribute in block.
1455     * @param array  $allowed  Array of allowed hosts for that block, or regexes to check against.
1456     * @param bool   $is_regex Array of regexes matching the URL that could be used in block.
1457     *
1458     * @return bool|string
1459     */
1460    public static function validate_block_embed_url( $url, $allowed = array(), $is_regex = false ) {
1461        if (
1462            empty( $url )
1463            || ! is_array( $allowed )
1464            || empty( $allowed )
1465        ) {
1466            return false;
1467        }
1468
1469        $url_components = wp_parse_url( $url );
1470
1471        // Bail early if we cannot find a host.
1472        if ( empty( $url_components['host'] ) ) {
1473            return false;
1474        }
1475
1476        // Normalize URL.
1477        $url = sprintf(
1478            '%s://%s%s%s',
1479            $url_components['scheme'] ?? 'https',
1480            $url_components['host'],
1481            $url_components['path'] ?? '/',
1482            isset( $url_components['query'] ) ? '?' . $url_components['query'] : ''
1483        );
1484
1485        if ( ! empty( $url_components['fragment'] ) ) {
1486            $url = $url . '#' . rawurlencode( $url_components['fragment'] );
1487        }
1488
1489        /*
1490         * If we're using an allowed list of hosts,
1491         * check if the URL belongs to one of the domains allowed for that block.
1492         */
1493        if (
1494            false === $is_regex
1495            && in_array( $url_components['host'], $allowed, true )
1496        ) {
1497            return $url;
1498        }
1499
1500        /*
1501         * If we are using an array of regexes to check against,
1502         * loop through that.
1503         */
1504        if ( true === $is_regex ) {
1505            foreach ( $allowed as $regex ) {
1506                if ( 1 === preg_match( $regex, $url ) ) {
1507                    return $url;
1508                }
1509            }
1510        }
1511
1512        return false;
1513    }
1514
1515    /**
1516     * Determines whether a preview of the block with an upgrade nudge should
1517     * be displayed for admins on the site frontend.
1518     *
1519     * @since 8.4.0
1520     *
1521     * @param array $availability_for_block The availability for the block.
1522     *
1523     * @return bool
1524     */
1525    public static function should_show_frontend_preview( $availability_for_block ) {
1526        return (
1527            isset( $availability_for_block['details']['required_plan'] )
1528            && current_user_can( 'manage_options' )
1529            && ! is_feed()
1530        );
1531    }
1532
1533    /**
1534     * Output an UpgradeNudge Component on the frontend of a site.
1535     *
1536     * @since 8.4.0
1537     *
1538     * @param string $plan The plan that users need to purchase to make the block work.
1539     *
1540     * @return string
1541     */
1542    public static function upgrade_nudge( $plan ) {
1543        require_once JETPACK__PLUGIN_DIR . '_inc/lib/components.php';
1544        return Jetpack_Components::render_upgrade_nudge(
1545            array(
1546                'plan' => $plan,
1547            )
1548        );
1549    }
1550
1551    /**
1552     * Output a notice within a block.
1553     *
1554     * @since 8.6.0
1555     *
1556     * @param string $message Notice we want to output.
1557     * @param string $status  Status of the notice. Can be one of success, info, warning, error. info by default.
1558     * @param string $classes List of CSS classes.
1559     *
1560     * @return string
1561     */
1562    public static function notice( $message, $status = 'info', $classes = '' ) {
1563        if (
1564            empty( $message )
1565            || ! in_array( $status, array( 'success', 'info', 'warning', 'error' ), true )
1566        ) {
1567            return '';
1568        }
1569
1570        $color = '';
1571        switch ( $status ) {
1572            case 'success':
1573                $color = '#00a32a';
1574                break;
1575            case 'warning':
1576                $color = '#dba617';
1577                break;
1578            case 'error':
1579                $color = '#d63638';
1580                break;
1581            case 'info':
1582            default:
1583                $color = '#72aee6';
1584                break;
1585        }
1586
1587        return sprintf(
1588            '<div class="jetpack-block__notice %1$s %3$s" style="border-left:5px solid %4$s;padding:1em;background-color:#f8f9f9;">%2$s</div>',
1589            esc_attr( $status ),
1590            wp_kses(
1591                $message,
1592                array(
1593                    'br' => array(),
1594                    'p'  => array(),
1595                    'a'  => array(
1596                        'href'   => array(),
1597                        'target' => array(),
1598                        'rel'    => array(),
1599                    ),
1600                )
1601            ),
1602            esc_attr( $classes ),
1603            sanitize_hex_color( $color )
1604        );
1605    }
1606
1607    /**
1608     * Retrieve site-specific features for Simple sites.
1609     *
1610     * We're caching the data for the lifetime of the request, because it can be slow to calculate,
1611     * and it can be called multiple times per single request.
1612     *
1613     * We intentionally don't use object caching or any other type of persistent caching,
1614     * in order to avoid complex cache invalidation on subscription addition or removal.
1615     *
1616     * @since 10.7
1617     *
1618     * @return array
1619     */
1620    private static function get_site_specific_features() {
1621        $current_blog_id = get_current_blog_id();
1622
1623        if ( isset( self::$site_specific_features[ $current_blog_id ] ) ) {
1624            return self::$site_specific_features[ $current_blog_id ];
1625        }
1626
1627        if ( ! class_exists( 'Store_Product_List' ) ) {
1628            require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
1629        }
1630
1631        $site_specific_features                           = Store_Product_List::get_site_specific_features_data( $current_blog_id );
1632        self::$site_specific_features[ $current_blog_id ] = $site_specific_features;
1633
1634        return $site_specific_features;
1635    }
1636
1637    /**
1638     * Set the availability of the block as the editor
1639     * is loaded.
1640     *
1641     * @param string $slug Slug of the block.
1642     */
1643    public static function set_availability_for_plan( $slug ) {
1644        $slug = self::remove_extension_prefix( $slug );
1645
1646        if ( Jetpack_Plan::supports( $slug ) ) {
1647            self::set_extension_available( $slug );
1648            return;
1649        }
1650
1651        // Check what's the minimum plan where the feature is available.
1652        $plan           = '';
1653        $features_data  = array();
1654        $is_simple_site = defined( 'IS_WPCOM' ) && IS_WPCOM;
1655        $is_atomic_site = ( new Host() )->is_woa_site();
1656
1657        if ( $is_simple_site || $is_atomic_site ) {
1658            // Simple sites.
1659            if ( $is_simple_site ) {
1660                $features_data = self::get_site_specific_features();
1661            } else {
1662                // Atomic sites.
1663                $option = get_option( 'jetpack_active_plan' );
1664                if ( isset( $option['features'] ) ) {
1665                    $features_data = $option['features'];
1666                }
1667            }
1668
1669            if ( ! empty( $features_data['available'][ $slug ] ) ) {
1670                $plan = $features_data['available'][ $slug ][0];
1671            } elseif ( isset( self::$wpcom_minimum_plan_fallbacks[ $slug ] ) ) {
1672                // Fallback for features with conditional availability (e.g., sticker-based gating)
1673                // that don't appear in features_data['available'].
1674                $plan = self::$wpcom_minimum_plan_fallbacks[ $slug ];
1675            }
1676        } else {
1677            // Jetpack sites.
1678            $plan = Jetpack_Plan::get_minimum_plan_for_feature( $slug );
1679        }
1680
1681        self::set_extension_unavailable(
1682            $slug,
1683            'missing_plan',
1684            array(
1685                'required_feature' => $slug,
1686                'required_plan'    => $plan,
1687            )
1688        );
1689    }
1690
1691    /**
1692     * Wraps the suplied render_callback in a function to check
1693     * the availability of the block before rendering it.
1694     *
1695     * @param string   $slug The block slug, used to check for availability.
1696     * @param callable $render_callback The render_callback that will be called if the block is available.
1697     */
1698    public static function get_render_callback_with_availability_check( $slug, $render_callback ) {
1699        return function ( $prepared_attributes, $block_content, $block ) use ( $render_callback, $slug ) {
1700            $availability = self::get_cached_availability();
1701            $bare_slug    = self::remove_extension_prefix( $slug );
1702            if ( isset( $availability[ $bare_slug ] ) && $availability[ $bare_slug ]['available'] ) {
1703                return call_user_func( $render_callback, $prepared_attributes, $block_content, $block );
1704            }
1705
1706            // A preview of the block is rendered for admins on the frontend with an upgrade nudge.
1707            if ( isset( $availability[ $bare_slug ] ) ) {
1708                if ( self::should_show_frontend_preview( $availability[ $bare_slug ] ) ) {
1709                    $block_preview = call_user_func( $render_callback, $prepared_attributes, $block_content, $block );
1710
1711                    // If the upgrade nudge isn't already being displayed by a parent block, display the nudge.
1712                    if ( isset( $block->attributes['shouldDisplayFrontendBanner'] ) && $block->attributes['shouldDisplayFrontendBanner'] ) {
1713                        $upgrade_nudge = self::upgrade_nudge( $availability[ $bare_slug ]['details']['required_plan'] );
1714                        return $upgrade_nudge . $block_preview;
1715                    }
1716
1717                    return $block_preview;
1718                }
1719            }
1720
1721            return null;
1722        };
1723    }
1724
1725    /**
1726     * Display a message to site editors and roles above when a block is no longer supported.
1727     * This is only displayed on the frontend.
1728     *
1729     * @since 12.3
1730     *
1731     * @param string $block_content The block content.
1732     * @param array  $block         The full block, including name and attributes.
1733     *
1734     * @return string
1735     */
1736    public static function display_deprecated_block_message( $block_content, $block ) {
1737        if ( isset( $block['blockName'] ) && in_array( $block['blockName'], self::$deprecated_blocks, true ) ) {
1738            if ( current_user_can( 'edit_posts' ) ) {
1739                $block_content = self::notice(
1740                    __( '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' ),
1741                    'warning',
1742                    'jetpack-block-deprecated'
1743                );
1744            } else {
1745                $block_content = '';
1746            }
1747        }
1748
1749        return $block_content;
1750    }
1751
1752    /**
1753     * Register block metadata collection for Jetpack blocks.
1754     * This allows for more efficient block metadata loading by avoiding
1755     * individual block.json file reads at runtime.
1756     *
1757     * Uses wp_register_block_metadata_collection() if the manifest file
1758     * exists. The manifest file is auto-generated during the build process.
1759     *
1760     * Runs on plugins_loaded to ensure registration happens before individual
1761     * blocks register themselves on init.
1762     *
1763     * @static
1764     * @since 14.1
1765     * @return void
1766     */
1767    public static function register_block_metadata_collection() {
1768        $meta_file_path = JETPACK__PLUGIN_DIR . '_inc/blocks/blocks-manifest.php';
1769        if ( file_exists( $meta_file_path ) ) {
1770            wp_register_block_metadata_collection(
1771                JETPACK__PLUGIN_DIR . '_inc/blocks/',
1772                $meta_file_path
1773            );
1774        }
1775    }
1776
1777    /**
1778     * Set the JS loading strategy for a block.
1779     *
1780     * @param string     $block_name The block name.
1781     * @param array|bool $strategy   The JS loading strategy.
1782     *
1783     * @since 15.0
1784     */
1785    public static function set_block_js_loading_strategy( $block_name, $strategy ) {
1786        self::$block_js_loading_strategies[ $block_name ] = $strategy;
1787    }
1788
1789    /**
1790     * Get the JS loading strategy for a block.
1791     *
1792     * @param string $block_name The block name.
1793     *
1794     * @return array|bool The JS loading strategy for the block.
1795     *
1796     * @since 15.0
1797     */
1798    public static function get_block_js_loading_strategy( $block_name ) {
1799        $strategy = array(
1800            'strategy'  => 'defer',
1801            'in_footer' => true,
1802        );
1803
1804        if ( isset( self::$block_js_loading_strategies[ $block_name ] ) ) {
1805            $strategy = self::$block_js_loading_strategies[ $block_name ];
1806        }
1807
1808        return $strategy;
1809    }
1810}
1811
1812if ( ( new Host() )->is_woa_site() ) {
1813    /**
1814    * Enable upgrade nudge for Atomic sites.
1815     * This feature is false as default,
1816     * so let's enable it through this filter.
1817     *
1818     * More doc: https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#upgrades-for-blocks
1819     */
1820    add_filter( 'jetpack_block_editor_enable_upgrade_nudge', '__return_true' );
1821}