diff --git a/lib/compat/wordpress-6.4/class-gutenberg-rest-block-patterns-controller.php b/lib/compat/wordpress-6.4/class-gutenberg-rest-block-patterns-controller.php index 8128934b9b011c..3fb7eef547e469 100644 --- a/lib/compat/wordpress-6.4/class-gutenberg-rest-block-patterns-controller.php +++ b/lib/compat/wordpress-6.4/class-gutenberg-rest-block-patterns-controller.php @@ -27,6 +27,11 @@ class Gutenberg_REST_Block_Patterns_Controller extends Gutenberg_REST_Block_Patt public function prepare_item_for_response( $item, $request ) { $response = parent::prepare_item_for_response( $item, $request ); + // Run the polyfill for Block Hooks only if it isn't already handled in WordPress core. + if ( function_exists( 'traverse_and_serialize_blocks' ) ) { + return $response; + } + $data = $response->get_data(); if ( empty( $data['content'] ) ) { diff --git a/lib/load.php b/lib/load.php index 40ab9c129af973..429bf6ce3257d1 100644 --- a/lib/load.php +++ b/lib/load.php @@ -94,7 +94,10 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.4 compat. require __DIR__ . '/compat/wordpress-6.4/blocks.php'; -require __DIR__ . '/compat/wordpress-6.4/block-hooks.php'; +if ( ! function_exists( 'traverse_and_serialize_blocks' ) ) { + // Install the polyfill for Block Hooks only if it isn't already handled in WordPress core. + require __DIR__ . '/compat/wordpress-6.4/block-hooks.php'; +} require __DIR__ . '/compat/wordpress-6.4/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.4/script-loader.php'; require __DIR__ . '/compat/wordpress-6.4/kses.php'; diff --git a/packages/block-library/src/pattern/index.php b/packages/block-library/src/pattern/index.php index b117e31e125cf2..fc4652a7c22e89 100644 --- a/packages/block-library/src/pattern/index.php +++ b/packages/block-library/src/pattern/index.php @@ -41,12 +41,12 @@ function render_block_core_pattern( $attributes ) { } $pattern = $registry->get_registered( $slug ); - $content = _inject_theme_attribute_in_block_template_content( $pattern['content'] ); + $content = $pattern['content']; + // Backward compatibility for handling Block Hooks and injecting the theme attribute in the Gutenberg plugin. // This can be removed when the minimum supported WordPress is >= 6.4. - if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { - // TODO: In the long run, we'd likely want to have a filter in the `WP_Block_Patterns_Registry` class - // instead to allow us plugging in code like this. + if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && ! function_exists( 'traverse_and_serialize_blocks' ) ) { + $content = _inject_theme_attribute_in_block_template_content( $content ); $blocks = parse_blocks( $content ); $content = gutenberg_serialize_blocks( $blocks ); }