From 236b4e6a7913edec1227a6d3e40586fec6c57eb3 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Thu, 11 Jan 2024 23:03:27 -0500 Subject: [PATCH 01/16] Move bindings logic to Block Bindings class --- .../block-bindings/block-bindings.php | 44 ++++++++++++++ ...essing.php => class-wp-block-bindings.php} | 60 ++++++++++++++++--- lib/experimental/block-bindings/index.php | 5 +- .../block-bindings/sources/pattern.php | 5 +- .../block-bindings/sources/post-meta.php | 5 +- lib/experimental/blocks.php | 20 ++----- 6 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 lib/experimental/block-bindings/block-bindings.php rename lib/experimental/block-bindings/{html-processing.php => class-wp-block-bindings.php} (69%) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php new file mode 100644 index 0000000000000..d36ea3a01fa37 --- /dev/null +++ b/lib/experimental/block-bindings/block-bindings.php @@ -0,0 +1,44 @@ +register_source( $source_name, $label, $apply ); + } +} + +if ( ! function_exists( 'wp_block_bindings_get_allowed_blocks' ) ) { + function wp_block_bindings_get_allowed_blocks() { + echo 'getting allowed blocks'; + return wp_block_bindings()->get_allowed_blocks(); + } +} + +if ( ! function_exists( 'wp_block_bindings_get_sources' ) ) { + function wp_block_bindings_get_sources() { + return wp_block_bindings()->get_sources(); + } +} + +if ( ! function_exists( 'wp_block_bindings_replace_html' ) ) { + function wp_block_bindings_replace_html( $block_content, $block_name, $block_attr, $source_value ) { + wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value ); + } +} diff --git a/lib/experimental/block-bindings/html-processing.php b/lib/experimental/block-bindings/class-wp-block-bindings.php similarity index 69% rename from lib/experimental/block-bindings/html-processing.php rename to lib/experimental/block-bindings/class-wp-block-bindings.php index 515749d0a8e75..1a1d65fdf0a2b 100644 --- a/lib/experimental/block-bindings/html-processing.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -1,11 +1,49 @@ array( 'content' ), + 'core/heading' => array( 'content' ), + 'core/image' => array( 'url', 'title', 'alt' ), + 'core/button' => array( 'url', 'text' ), + ); + + /** + * Function to register a new source. + * + * @param string $source_name The name of the source. + * @param string $label The label of the source. + * @param callable $apply The callback executed when the source is processed during block rendering. The callable should have the following signature: + * function (object $source_attrs, object $block_instance, string $attribute_name): string + * - object $source_attrs: Object containing source ID used to look up the override value, i.e. {"value": "{ID}"}. + * - object $block_instance: The block instance. + * - string $attribute_name: The name of an attribute used to retrieve an override value from the block context. + * The callable should return a string that will be used to override the block's original value. + * + * @return void + */ + public function register_source( $source_name, $label, $apply ) { + $this->block_bindings_sources[ $source_name ] = array( + 'label' => $label, + 'apply' => $apply, + ); + } + /** * Depending on the block attributes, replace the proper HTML based on the value returned by the source. * @@ -14,7 +52,7 @@ * @param string $block_attr The attribute of the block we want to process. * @param string $source_value The value used to replace the HTML. */ - function block_bindings_replace_html( $block_content, $block_name, $block_attr, $source_value ) { + public function replace_html( $block_content, $block_name, $block_attr, $source_value ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); if ( null === $block_type ) { return; @@ -107,4 +145,12 @@ function block_bindings_replace_html( $block_content, $block_name, $block_attr, } return; } + + public function get_sources() { + return $this->sources; + } + + public function get_allowed_blocks() { + return $this->allowed_blocks; + } } diff --git a/lib/experimental/block-bindings/index.php b/lib/experimental/block-bindings/index.php index cca857e93702f..dc9a6c9b96957 100644 --- a/lib/experimental/block-bindings/index.php +++ b/lib/experimental/block-bindings/index.php @@ -5,9 +5,8 @@ * @package gutenberg */ -require_once __DIR__ . '/sources/index.php'; -require_once __DIR__ . '/html-processing.php'; - +require_once __DIR__ . '/class-wp-block-bindings.php'; +require_once __DIR__ . '/block-bindings.php'; // Register the sources. $gutenberg_experiments = get_option( 'gutenberg-experiments' ); if ( $gutenberg_experiments ) { diff --git a/lib/experimental/block-bindings/sources/pattern.php b/lib/experimental/block-bindings/sources/pattern.php index e3456aa468d3e..16bf46f4f06ab 100644 --- a/lib/experimental/block-bindings/sources/pattern.php +++ b/lib/experimental/block-bindings/sources/pattern.php @@ -4,8 +4,7 @@ * * @package gutenberg */ - -if ( function_exists( 'register_block_bindings_source' ) ) { +if ( function_exists( 'wp_block_bindings_register_source' ) ) { $pattern_source_callback = function ( $source_attrs, $block_instance, $attribute_name ) { if ( ! _wp_array_get( $block_instance->attributes, array( 'metadata', 'id' ), false ) ) { return null; @@ -13,7 +12,7 @@ $block_id = $block_instance->attributes['metadata']['id']; return _wp_array_get( $block_instance->context, array( 'pattern/overrides', $block_id, $attribute_name ), null ); }; - register_block_bindings_source( + wp_block_bindings_register_source( 'pattern_attributes', __( 'Pattern Attributes', 'gutenberg' ), $pattern_source_callback diff --git a/lib/experimental/block-bindings/sources/post-meta.php b/lib/experimental/block-bindings/sources/post-meta.php index 99b6afc03c0d4..2d53dab6d321a 100644 --- a/lib/experimental/block-bindings/sources/post-meta.php +++ b/lib/experimental/block-bindings/sources/post-meta.php @@ -4,8 +4,7 @@ * * @package gutenberg */ - -if ( function_exists( 'register_block_bindings_source' ) ) { +if ( function_exists( 'wp_block_bindings_register_source' ) ) { $post_meta_source_callback = function ( $source_attrs ) { // Use the postId attribute if available if ( isset( $source_attrs['postId'] ) ) { @@ -17,7 +16,7 @@ return get_post_meta( $post_id, $source_attrs['value'], true ); }; - register_block_bindings_source( + wp_block_bindings_register_source( 'post_meta', __( 'Post Meta', 'gutenberg' ), $post_meta_source_callback diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 42663e127870c..89ae112509a70 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -88,15 +88,7 @@ function wp_enqueue_block_view_script( $block_name, $args ) { ) ) { require_once __DIR__ . '/block-bindings/index.php'; - // Allowed blocks that support block bindings. - // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? - global $block_bindings_allowed_blocks; - $block_bindings_allowed_blocks = array( - 'core/paragraph' => array( 'content' ), - 'core/heading' => array( 'content' ), - 'core/image' => array( 'url', 'title', 'alt' ), - 'core/button' => array( 'url', 'text' ), - ); + if ( ! function_exists( 'process_block_bindings' ) ) { /** * Process the block bindings attribute. @@ -128,9 +120,9 @@ function process_block_bindings( $block_content, $block, $block_instance ) { // } // } // - global $block_bindings_allowed_blocks; - global $block_bindings_sources; - $modified_block_content = $block_content; + $block_bindings_allowed_blocks = wp_block_bindings_get_allowed_blocks(); + $block_bindings_sources = wp_block_bindings_get_sources(); + $modified_block_content = $block_content; foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) { // If the block is not in the list, stop processing. if ( ! isset( $block_bindings_allowed_blocks[ $block['blockName'] ] ) ) { @@ -159,13 +151,13 @@ function process_block_bindings( $block_content, $block, $block_instance ) { } // Process the HTML based on the block and the attribute. - $modified_block_content = block_bindings_replace_html( $modified_block_content, $block['blockName'], $binding_attribute, $source_value ); + $modified_block_content = wp_block_bindings_replace_html( $modified_block_content, $block['blockName'], $binding_attribute, $source_value ); } return $modified_block_content; } // Add filter only to the blocks in the list. - foreach ( $block_bindings_allowed_blocks as $block_name => $attributes ) { + foreach ( wp_block_bindings_get_allowed_blocks() as $block_name => $attributes ) { add_filter( 'render_block_' . $block_name, 'process_block_bindings', 20, 3 ); } } From 6e1eb1c387669e3cf25f87c8c98a19709059513d Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Wed, 10 Jan 2024 16:55:28 -0500 Subject: [PATCH 02/16] Remove erroneous echo statement that was breaking UI --- lib/experimental/block-bindings/block-bindings.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index d36ea3a01fa37..087026bdca86b 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -26,7 +26,6 @@ function wp_block_bindings_register_source( $source_name, $label, $apply ) { if ( ! function_exists( 'wp_block_bindings_get_allowed_blocks' ) ) { function wp_block_bindings_get_allowed_blocks() { - echo 'getting allowed blocks'; return wp_block_bindings()->get_allowed_blocks(); } } From 37e8dbd534c4270bd71266c6fe8a179dba4a4662 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Thu, 11 Jan 2024 12:32:19 -0500 Subject: [PATCH 03/16] Fix error in registering block bindings sources --- lib/experimental/block-bindings/class-wp-block-bindings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index 1a1d65fdf0a2b..effd74838f37d 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -38,7 +38,7 @@ class WP_Block_Bindings { * @return void */ public function register_source( $source_name, $label, $apply ) { - $this->block_bindings_sources[ $source_name ] = array( + $this->sources[ $source_name ] = array( 'label' => $label, 'apply' => $apply, ); From 27cfb493f0464235d0ab0d54fd56d843b8b5f2df Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Thu, 11 Jan 2024 22:59:50 -0500 Subject: [PATCH 04/16] Add missing return statement --- lib/experimental/block-bindings/block-bindings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index 087026bdca86b..506cd1d721c27 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -38,6 +38,6 @@ function wp_block_bindings_get_sources() { if ( ! function_exists( 'wp_block_bindings_replace_html' ) ) { function wp_block_bindings_replace_html( $block_content, $block_name, $block_attr, $source_value ) { - wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value ); + return wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value ); } } From 892aa172d81a2a6dad024cdec08627185b6deee9 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Thu, 11 Jan 2024 23:51:28 -0500 Subject: [PATCH 05/16] Add docblocks --- .../block-bindings/block-bindings.php | 35 ++++++++++++++++++- .../class-wp-block-bindings.php | 23 ++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index 506cd1d721c27..9324ac88fdc99 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -2,12 +2,18 @@ /** * Block Bindings API * - * @since 6.5.0 + * This file contains functions for managing block bindings in WordPress. * + * @since 6.5.0 * @package WordPress * @subpackage Script Modules */ +/** + * Retrieves the singleton instance of WP_Block_Bindings. + * + * @return WP_Block_Bindings The WP_Block_Bindings instance. + */ if ( ! function_exists( 'wp_block_bindings' ) ) { function wp_block_bindings() { static $instance = null; @@ -18,24 +24,51 @@ function wp_block_bindings() { } } +/** + * Registers a new source for block bindings. + * + * @param string $source_name The name of the source. + * @param string $label The label of the source. + * @param callable $apply The callback function to be executed with the source. + * @return void + */ if ( ! function_exists( 'wp_block_bindings_register_source' ) ) { function wp_block_bindings_register_source( $source_name, $label, $apply ) { wp_block_bindings()->register_source( $source_name, $label, $apply ); } } +/** + * Retrieves the list of allowed blocks. + * + * @return array The list of allowed blocks. + */ if ( ! function_exists( 'wp_block_bindings_get_allowed_blocks' ) ) { function wp_block_bindings_get_allowed_blocks() { return wp_block_bindings()->get_allowed_blocks(); } } +/** + * Retrieves the list of registered block sources. + * + * @return array The list of registered block sources. + */ if ( ! function_exists( 'wp_block_bindings_get_sources' ) ) { function wp_block_bindings_get_sources() { return wp_block_bindings()->get_sources(); } } +/** + * Replaces the HTML content of a block based on the provided source value. + * + * @param string $block_content The original content of the block. + * @param string $block_name The name of the block. + * @param array $block_attr Attributes of the block. + * @param mixed $source_value The value from the source to replace in the block content. + * @return string The modified block content. + */ if ( ! function_exists( 'wp_block_bindings_replace_html' ) ) { function wp_block_bindings_replace_html( $block_content, $block_name, $block_attr, $source_value ) { return wp_block_bindings()->replace_html( $block_content, $block_name, $block_attr, $source_value ); diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index effd74838f37d..e8ce52d95df35 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -1,9 +1,22 @@ sources; } + /** + * Retrieves the list of allowed blocks that support block bindings. + * + * @return array The array of allowed blocks. + */ public function get_allowed_blocks() { return $this->allowed_blocks; } From 1319d1e606551e1141430a164fd57c7471543c76 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Thu, 11 Jan 2024 23:52:56 -0500 Subject: [PATCH 06/16] Remove obsolete file --- .../block-bindings/sources/index.php | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 lib/experimental/block-bindings/sources/index.php diff --git a/lib/experimental/block-bindings/sources/index.php b/lib/experimental/block-bindings/sources/index.php deleted file mode 100644 index 6d14bc96d86ba..0000000000000 --- a/lib/experimental/block-bindings/sources/index.php +++ /dev/null @@ -1,32 +0,0 @@ - $label, - 'apply' => $apply, - ); - } -} From c0e4407b872ae695a3419d14b6e820973af0bbc4 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:19:13 -0500 Subject: [PATCH 07/16] Fix docblock --- lib/experimental/block-bindings/block-bindings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index 9324ac88fdc99..adccffec0092b 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -63,10 +63,10 @@ function wp_block_bindings_get_sources() { /** * Replaces the HTML content of a block based on the provided source value. * - * @param string $block_content The original content of the block. - * @param string $block_name The name of the block. - * @param array $block_attr Attributes of the block. - * @param mixed $source_value The value from the source to replace in the block content. + * @param string $block_content Block Content. + * @param string $block_name The name of the block to process. + * @param string $block_attr The attribute of the block we want to process. + * @param string $source_value The value used to replace the HTML. * @return string The modified block content. */ if ( ! function_exists( 'wp_block_bindings_replace_html' ) ) { From d596b2305a02ea2382b994c8d9505d21593ec51c Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:24:23 -0500 Subject: [PATCH 08/16] Sync register_source docblock --- lib/experimental/block-bindings/block-bindings.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index adccffec0092b..1f411265678fe 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -29,7 +29,12 @@ function wp_block_bindings() { * * @param string $source_name The name of the source. * @param string $label The label of the source. - * @param callable $apply The callback function to be executed with the source. + * @param callable $apply The callback executed when the source is processed during block rendering. The callable should have the following signature: + * function (object $source_attrs, object $block_instance, string $attribute_name): string + * - object $source_attrs: Object containing source ID used to look up the override value, i.e. {"value": "{ID}"}. + * - object $block_instance: The block instance. + * - string $attribute_name: The name of an attribute used to retrieve an override value from the block context. + * The callable should return a string that will be used to override the block's original value. * @return void */ if ( ! function_exists( 'wp_block_bindings_register_source' ) ) { From e607d959144437f874a6c2ff4f947f6f3b220947 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:27:31 -0500 Subject: [PATCH 09/16] Remove erroneous subpackage declaration --- lib/experimental/block-bindings/block-bindings.php | 1 - lib/experimental/block-bindings/class-wp-block-bindings.php | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index 1f411265678fe..cb4e1641a9f7a 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -6,7 +6,6 @@ * * @since 6.5.0 * @package WordPress - * @subpackage Script Modules */ /** diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index e8ce52d95df35..6f27cec5c40a7 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -5,7 +5,6 @@ * Support for custom fields in blocks. * * @package WordPress - * @subpackage Script Modules */ if ( class_exists( 'WP_Block_Bindings' ) ) { From 0a317f110a3f214a371354f30227b46c63ee0a6e Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:28:49 -0500 Subject: [PATCH 10/16] Update package name --- lib/experimental/block-bindings/block-bindings.php | 2 +- lib/experimental/block-bindings/class-wp-block-bindings.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index cb4e1641a9f7a..d5d3ef4f83878 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -5,7 +5,7 @@ * This file contains functions for managing block bindings in WordPress. * * @since 6.5.0 - * @package WordPress + * @package Gutenberg */ /** diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index 6f27cec5c40a7..25c5735cb990d 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -4,7 +4,7 @@ * * Support for custom fields in blocks. * - * @package WordPress + * @package Gutenberg */ if ( class_exists( 'WP_Block_Bindings' ) ) { From aafcc0fc27adad4a3011be57c68e76e9598ee165 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:31:00 -0500 Subject: [PATCH 11/16] Fix gutenberg package declarations --- lib/experimental/block-bindings/block-bindings.php | 4 ++-- lib/experimental/block-bindings/class-wp-block-bindings.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index d5d3ef4f83878..06ec2e34dc5f1 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -4,8 +4,8 @@ * * This file contains functions for managing block bindings in WordPress. * - * @since 6.5.0 - * @package Gutenberg + * @since 17.6.0 + * @package gutenberg */ /** diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index 25c5735cb990d..0603054db22ef 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -4,7 +4,8 @@ * * Support for custom fields in blocks. * - * @package Gutenberg + * @since 17.6.0 + * @package gutenberg */ if ( class_exists( 'WP_Block_Bindings' ) ) { From 7252c01c54df85de3dfea320b336bbcb9751dc72 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Fri, 12 Jan 2024 00:34:10 -0500 Subject: [PATCH 12/16] Remove extraneous comments --- lib/experimental/block-bindings/class-wp-block-bindings.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index 0603054db22ef..2f291825de989 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -14,15 +14,12 @@ /** * Core class used to define supported blocks, register sources, and populate HTML with content from those sources. - * - * @since 6.5.0 */ class WP_Block_Bindings { /** * Holds the registered block bindings sources, keyed by source identifier. * - * @since 6.5.0 * @var array */ private $sources = array(); From d7e1c93bd852bd754561d7f6d7e6a2fba92c01dd Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Mon, 15 Jan 2024 18:07:17 -0500 Subject: [PATCH 13/16] Move allowed_blocks property to filter function --- .../block-bindings/block-bindings.php | 11 ------ .../class-wp-block-bindings.php | 17 --------- lib/experimental/blocks.php | 36 ++++++++++--------- 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php index 06ec2e34dc5f1..83a1d6132f5f4 100644 --- a/lib/experimental/block-bindings/block-bindings.php +++ b/lib/experimental/block-bindings/block-bindings.php @@ -42,17 +42,6 @@ function wp_block_bindings_register_source( $source_name, $label, $apply ) { } } -/** - * Retrieves the list of allowed blocks. - * - * @return array The list of allowed blocks. - */ -if ( ! function_exists( 'wp_block_bindings_get_allowed_blocks' ) ) { - function wp_block_bindings_get_allowed_blocks() { - return wp_block_bindings()->get_allowed_blocks(); - } -} - /** * Retrieves the list of registered block sources. * diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index 2f291825de989..cbbc4cbef4abe 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -24,15 +24,6 @@ class WP_Block_Bindings { */ private $sources = array(); - // Allowed blocks that support block bindings. - // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? - private $allowed_blocks = array( - 'core/paragraph' => array( 'content' ), - 'core/heading' => array( 'content' ), - 'core/image' => array( 'url', 'title', 'alt' ), - 'core/button' => array( 'url', 'text' ), - ); - /** * Function to register a new source. * @@ -165,12 +156,4 @@ public function get_sources() { return $this->sources; } - /** - * Retrieves the list of allowed blocks that support block bindings. - * - * @return array The array of allowed blocks. - */ - public function get_allowed_blocks() { - return $this->allowed_blocks; - } } diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 89ae112509a70..2558161a0c49a 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -98,8 +98,18 @@ function wp_enqueue_block_view_script( $block_name, $args ) { * @param WP_Block $block_instance The block instance. */ function process_block_bindings( $block_content, $block, $block_instance ) { - // If the block doesn't have the bindings property, return. - if ( ! isset( $block['attrs']['metadata']['bindings'] ) ) { + + // Allowed blocks that support block bindings. + // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? + $allowed_blocks = array( + 'core/paragraph' => array( 'content' ), + 'core/heading' => array( 'content' ), + 'core/image' => array( 'url', 'title', 'alt' ), + 'core/button' => array( 'url', 'text' ), + ); + + // If the block doesn't have the bindings property or isn't one of the allowed block types, return. + if ( ! isset( $block['attrs']['metadata']['bindings'] ) || ! isset( $allowed_blocks[ $block_instance->name ] ) ) { return $block_content; } @@ -120,16 +130,13 @@ function process_block_bindings( $block_content, $block, $block_instance ) { // } // } // - $block_bindings_allowed_blocks = wp_block_bindings_get_allowed_blocks(); - $block_bindings_sources = wp_block_bindings_get_sources(); - $modified_block_content = $block_content; + + $block_bindings_sources = wp_block_bindings_get_sources(); + $modified_block_content = $block_content; foreach ( $block['attrs']['metadata']['bindings'] as $binding_attribute => $binding_source ) { - // If the block is not in the list, stop processing. - if ( ! isset( $block_bindings_allowed_blocks[ $block['blockName'] ] ) ) { - return $block_content; - } + // If the attribute is not in the list, process next attribute. - if ( ! in_array( $binding_attribute, $block_bindings_allowed_blocks[ $block['blockName'] ], true ) ) { + if ( ! in_array( $binding_attribute, $allowed_blocks[ $block_instance->name ], true ) ) { continue; } // If no source is provided, or that source is not registered, process next attribute. @@ -151,14 +158,11 @@ function process_block_bindings( $block_content, $block, $block_instance ) { } // Process the HTML based on the block and the attribute. - $modified_block_content = wp_block_bindings_replace_html( $modified_block_content, $block['blockName'], $binding_attribute, $source_value ); + $modified_block_content = wp_block_bindings_replace_html( $modified_block_content, $block_instance->name, $binding_attribute, $source_value ); } return $modified_block_content; } - - // Add filter only to the blocks in the list. - foreach ( wp_block_bindings_get_allowed_blocks() as $block_name => $attributes ) { - add_filter( 'render_block_' . $block_name, 'process_block_bindings', 20, 3 ); - } } + + add_filter( 'render_block', process_block_bindings, 20, 3 ); } From aa566cc8eeb8dd67234e3dc07023832fc1cf68c7 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Mon, 15 Jan 2024 18:12:41 -0500 Subject: [PATCH 14/16] Update description of Block Bindings class --- lib/experimental/block-bindings/class-wp-block-bindings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index cbbc4cbef4abe..e5d1028f8658c 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -2,7 +2,7 @@ /** * Block Bindings API: WP_Block_Bindings class. * - * Support for custom fields in blocks. + * Support for overriding content in blocks by connecting them to different sources. * * @since 17.6.0 * @package gutenberg From 9c8bf557bc4da4126ecfb124d50bdbe9adb7db08 Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Mon, 15 Jan 2024 18:15:17 -0500 Subject: [PATCH 15/16] Address PHPCS spacing issue --- lib/experimental/block-bindings/class-wp-block-bindings.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/experimental/block-bindings/class-wp-block-bindings.php b/lib/experimental/block-bindings/class-wp-block-bindings.php index e5d1028f8658c..7eb443dd367a6 100644 --- a/lib/experimental/block-bindings/class-wp-block-bindings.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -155,5 +155,4 @@ public function replace_html( $block_content, $block_name, $block_attr, $source_ public function get_sources() { return $this->sources; } - } From e6473a7af412a1a9d06d77e2309c38a1b02cc50a Mon Sep 17 00:00:00 2001 From: Ricardo Artemio Morales Date: Tue, 16 Jan 2024 09:38:59 -0500 Subject: [PATCH 16/16] Rename function call and call using string in filter --- lib/experimental/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index 2558161a0c49a..93b65f95fc61a 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -89,7 +89,7 @@ function wp_enqueue_block_view_script( $block_name, $args ) { require_once __DIR__ . '/block-bindings/index.php'; - if ( ! function_exists( 'process_block_bindings' ) ) { + if ( ! function_exists( 'gutenberg_process_block_bindings' ) ) { /** * Process the block bindings attribute. * @@ -97,7 +97,7 @@ function wp_enqueue_block_view_script( $block_name, $args ) { * @param array $block Block attributes. * @param WP_Block $block_instance The block instance. */ - function process_block_bindings( $block_content, $block, $block_instance ) { + function gutenberg_process_block_bindings( $block_content, $block, $block_instance ) { // Allowed blocks that support block bindings. // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? @@ -164,5 +164,5 @@ function process_block_bindings( $block_content, $block, $block_instance ) { } } - add_filter( 'render_block', process_block_bindings, 20, 3 ); + add_filter( 'render_block', 'gutenberg_process_block_bindings', 20, 3 ); }