Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor block binding processing and attribute computation #6059

30 changes: 20 additions & 10 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,13 @@ public function __get( $name ) {
*
* @param string $block_content Block content.
* @param array $block The full block, including name and attributes.
* @return string The modified block content.
* @return array The modified block content.
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
*/
private function process_block_bindings( $block_content ) {
private function process_block_bindings() {
$parsed_block = $this->parsed_block;

$computed_attributes = array();

// 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(
Expand All @@ -251,10 +253,9 @@ private function process_block_bindings( $block_content ) {
empty( $parsed_block['attrs']['metadata']['bindings'] ) ||
! is_array( $parsed_block['attrs']['metadata']['bindings'] )
) {
return $block_content;
return $computed_attributes;
}

$modified_block_content = $block_content;
foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
// If the attribute is not in the allowed list, process next attribute.
if ( ! in_array( $attribute_name, $allowed_blocks[ $this->name ], true ) ) {
Expand All @@ -276,11 +277,11 @@ private function process_block_bindings( $block_content ) {

// If the value is not null, process the HTML based on the block and the attribute.
if ( ! is_null( $source_value ) ) {
$modified_block_content = $this->replace_html( $modified_block_content, $attribute_name, $source_value );
$computed_attributes[ $attribute_name ] = $source_value;
}
}

return $modified_block_content;
return $computed_attributes;
}

/**
Expand Down Expand Up @@ -446,6 +447,19 @@ public function render( $options = array() ) {
}
}

$computed_attributes = $this->process_block_bindings();
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved

// If there are computed attributes, update the attributes with the
// computed ones.
if ( ! empty( $computed_attributes ) ) {
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
// Merge the computed attributes with the original attributes
$this->attributes = array_merge( $this->attributes, $computed_attributes );
}

foreach ( $computed_attributes as $attribute_name => $source_value ) {
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
$block_content = $this->replace_html( $block_content, $attribute_name, $source_value );
}

if ( $is_dynamic ) {
$global_post = $post;
$parent = WP_Block_Supports::$block_to_render;
Expand Down Expand Up @@ -483,10 +497,6 @@ public function render( $options = array() ) {
}
}

// Process the block bindings for this block, if any are registered. This
// will replace the block content with the value from a registered binding source.
$block_content = $this->process_block_bindings( $block_content );

/**
* Filters the content of a single block.
*
Expand Down
Loading