From 869d53de95ed87da86fc61c04a6f4f63dfa93819 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 31 May 2024 16:08:02 +0800 Subject: [PATCH] Make the PHP implementation of the replacement of default attributes more generic (not implemented in the pattern block) --- lib/compat/wordpress-6.6/blocks.php | 43 ++++++++++++++++++++++ lib/load.php | 1 + packages/block-library/src/block/index.php | 36 ------------------ 3 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 lib/compat/wordpress-6.6/blocks.php diff --git a/lib/compat/wordpress-6.6/blocks.php b/lib/compat/wordpress-6.6/blocks.php new file mode 100644 index 0000000000000..04582452cb0dc --- /dev/null +++ b/lib/compat/wordpress-6.6/blocks.php @@ -0,0 +1,43 @@ + array( 'content' ), + 'core/heading' => array( 'content' ), + 'core/image' => array( 'id', 'url', 'title', 'alt' ), + 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), + ); + + $bindings = $parsed_block['attrs']['metadata']['bindings'] ?? array(); + if ( + isset( $bindings[ '__default' ][ 'source' ] ) && + $bindings[ '__default' ][ 'source' ] === 'core/pattern-overrides' + ) { + // Build an binding array of all supported attributes. + foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { + $bindings[ $attribute_name ] = array( 'source' => 'core/pattern-overrides' ); + } + // Merge this into the parsed block's bindings & avoid overwriting existing bindings. + $parsed_block['attrs']['metadata']['bindings'] = array_merge( + $bindings, + $parsed_block['attrs']['metadata']['bindings'] + ); + } + + return $parsed_block; +} + +add_filter( 'render_block_data', 'gutenberg_replace_pattern_override_default_binding', 10, 1 ); diff --git a/lib/load.php b/lib/load.php index 6179ade9a2288..23985f9c8a92e 100644 --- a/lib/load.php +++ b/lib/load.php @@ -131,6 +131,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.6 compat. require __DIR__ . '/compat/wordpress-6.6/admin-bar.php'; +require __DIR__ . '/compat/wordpress-6.6/blocks.php'; require __DIR__ . '/compat/wordpress-6.6/compat.php'; require __DIR__ . '/compat/wordpress-6.6/resolve-patterns.php'; require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php'; diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 786615f58583d..8beef975fad6f 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -85,41 +85,6 @@ function render_block_core_block( $attributes ) { return $context; }; add_filter( 'render_block_context', $filter_block_context, 1 ); - - // Add bindings for the default pattern overrides source. - $apply_default_pattern_overrides_bindings = static function ( $parsed_block ) { - $supported_block_attrs = array( - 'core/paragraph' => array( 'content' ), - 'core/heading' => array( 'content' ), - 'core/image' => array( 'id', 'url', 'title', 'alt' ), - 'core/button' => array( 'url', 'text', 'linkTarget', 'rel' ), - ); - - if ( - // Return early if the block isn't one of the supported block types, - ! isset( $supported_block_attrs[ $parsed_block['blockName'] ] ) || - // or doesn't have a name, - ! isset( $parsed_block['attrs']['metadata']['name'] ) || - // or doesn't have the default binding. - empty( $parsed_block['attrs']['metadata']['bindings']['__default'] ) || - // or the default binding isn't the pattern overrides source. - 'core/pattern-overrides' !== $parsed_block['attrs']['metadata']['bindings']['__default']['source'] - ) { - return $parsed_block; - } - - $bindings = array(); - foreach ( $supported_block_attrs[ $parsed_block['blockName'] ] as $attribute_name ) { - $bindings[ $attribute_name ] = array( 'source' => 'core/pattern-overrides' ); - } - $parsed_block['attrs']['metadata']['bindings'] = array_merge( - $bindings, - $parsed_block['attrs']['metadata']['bindings'] - ); - - return $parsed_block; - }; - add_filter( 'render_block_data', $apply_default_pattern_overrides_bindings ); } $content = do_blocks( $content ); @@ -127,7 +92,6 @@ function render_block_core_block( $attributes ) { if ( $has_pattern_overrides ) { remove_filter( 'render_block_context', $filter_block_context, 1 ); - remove_filter( 'render_block_data', $apply_default_pattern_overrides_bindings ); } return $content;