Skip to content

Commit

Permalink
Plugin: Ensure that Block Hooks work correctly after landing in WP core
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 25, 2023
1 parent 2b06db0 commit 7a04903
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
5 changes: 4 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 4 additions & 4 deletions packages/block-library/src/pattern/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 7a04903

Please sign in to comment.