diff --git a/lib/experimental/block-bindings/block-bindings.php b/lib/experimental/block-bindings/block-bindings.php new file mode 100644 index 00000000000000..83a1d6132f5f43 --- /dev/null +++ b/lib/experimental/block-bindings/block-bindings.php @@ -0,0 +1,69 @@ +register_source( $source_name, $label, $apply ); + } +} + +/** + * 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 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' ) ) { + 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/html-processing.php b/lib/experimental/block-bindings/class-wp-block-bindings.php similarity index 70% rename from lib/experimental/block-bindings/html-processing.php rename to lib/experimental/block-bindings/class-wp-block-bindings.php index 515749d0a8e753..7eb443dd367a6f 100644 --- a/lib/experimental/block-bindings/html-processing.php +++ b/lib/experimental/block-bindings/class-wp-block-bindings.php @@ -1,11 +1,50 @@ 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 +53,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 +146,13 @@ function block_bindings_replace_html( $block_content, $block_name, $block_attr, } return; } + + /** + * Retrieves the list of registered block sources. + * + * @return array The array of registered sources. + */ + public function get_sources() { + return $this->sources; + } } diff --git a/lib/experimental/block-bindings/index.php b/lib/experimental/block-bindings/index.php index cca857e93702f3..dc9a6c9b96957b 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/index.php b/lib/experimental/block-bindings/sources/index.php deleted file mode 100644 index 6d14bc96d86bac..00000000000000 --- a/lib/experimental/block-bindings/sources/index.php +++ /dev/null @@ -1,32 +0,0 @@ - $label, - 'apply' => $apply, - ); - } -} diff --git a/lib/experimental/block-bindings/sources/pattern.php b/lib/experimental/block-bindings/sources/pattern.php index e3456aa468d3eb..16bf46f4f06ab6 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 99b6afc03c0d42..2d53dab6d321ac 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 42663e127870c9..93b65f95fc61ae 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -88,16 +88,8 @@ 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' ) ) { + + if ( ! function_exists( 'gutenberg_process_block_bindings' ) ) { /** * Process the block bindings attribute. * @@ -105,9 +97,19 @@ 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 ) { - // If the block doesn't have the bindings property, return. - if ( ! isset( $block['attrs']['metadata']['bindings'] ) ) { + 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? + $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; } @@ -128,16 +130,13 @@ function process_block_bindings( $block_content, $block, $block_instance ) { // } // } // - global $block_bindings_allowed_blocks; - global $block_bindings_sources; + + $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. @@ -159,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 = 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 ( $block_bindings_allowed_blocks as $block_name => $attributes ) { - add_filter( 'render_block_' . $block_name, 'process_block_bindings', 20, 3 ); - } } + + add_filter( 'render_block', 'gutenberg_process_block_bindings', 20, 3 ); }