<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57574] trunk: Editor: Refactor block binding processing and attribute computation</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57574">57574</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57574","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>gziolo</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-02-09 10:38:38 +0000 (Fri, 09 Feb 2024)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Refactor block binding processing and attribute computation
Refactors the processing of block bindings into steps:
- Gets the value for each "bound" attribute from the respective source.
- Returns the computer attributes with values from the sources.
- The computed attributes get injected into block's content.
- The `render_callback` gets the updated list of attributes and processeded block content.
Fixes <a href="https://core.trac.wordpress.org/ticket/60282">#60282</a>.
Props czapla, gziolo, andraganescu, santosguillamot.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockphp">trunk/src/wp-includes/class-wp-block.php</a></li>
<li><a href="#trunktestsphpunittestsblockbindingsrenderphp">trunk/tests/phpunit/tests/block-bindings/render.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpblockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block.php 2024-02-09 04:15:08 UTC (rev 57573)
+++ trunk/src/wp-includes/class-wp-block.php 2024-02-09 10:38:38 UTC (rev 57574)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -192,7 +192,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Processes the block bindings in block's attributes.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Processes the block bindings and updates the block attributes with the values from the sources.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * A block might contain bindings in its attributes. Bindings are mappings
</span><span class="cx" style="display: block; padding: 0 10px"> * between an attribute of the block and a source. A "source" is a function
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -199,8 +199,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * registered with `register_block_bindings_source()` that defines how to
</span><span class="cx" style="display: block; padding: 0 10px"> * retrieve a value from outside the block, e.g. from post meta.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This function will process those bindings and replace the HTML with the value of the binding.
- * The value is retrieved from the source of the binding.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * This function will process those bindings and update the block's attributes
+ * with the values coming from the bindings.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * ### Example
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,13 +228,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $block_content Block content.
- * @param array $block The full block, including name and attributes.
- * @return string The modified block content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array The computed block attributes for the provided block bindings.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private function process_block_bindings( $block_content ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function process_block_bindings() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $parsed_block = $this->parsed_block;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $computed_attributes = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Allowed blocks that support block bindings.
</span><span class="cx" style="display: block; padding: 0 10px"> // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes?
</span><span class="cx" style="display: block; padding: 0 10px"> $allowed_blocks = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -251,10 +251,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> empty( $parsed_block['attrs']['metadata']['bindings'] ) ||
</span><span class="cx" style="display: block; padding: 0 10px"> ! is_array( $parsed_block['attrs']['metadata']['bindings'] )
</span><span class="cx" style="display: block; padding: 0 10px"> ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $block_content;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $computed_attributes;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $modified_block_content = $block_content;
</del><span class="cx" style="display: block; padding: 0 10px"> foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // If the attribute is not in the allowed list, process next attribute.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! in_array( $attribute_name, $allowed_blocks[ $this->name ], true ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,11 +274,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // If the value is not null, process the HTML based on the block and the attribute.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_null( $source_value ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $modified_block_content = $this->replace_html( $modified_block_content, $attribute_name, $source_value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $computed_attributes[ $attribute_name ] = $source_value;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $modified_block_content;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $computed_attributes;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -391,6 +390,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Generates the render output for the block.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added block bindings processing.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global WP_Post $post Global post object.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -410,6 +410,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Process the block bindings and get attributes updated with the values from the sources.
+ $computed_attributes = $this->process_block_bindings();
+ if ( ! empty( $computed_attributes ) ) {
+ // Merge the computed attributes with the original attributes
+ $this->attributes = array_merge( $this->attributes, $computed_attributes );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
</span><span class="cx" style="display: block; padding: 0 10px"> $block_content = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -445,6 +452,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) {
+ foreach ( $computed_attributes as $attribute_name => $source_value ) {
+ $block_content = $this->replace_html( $block_content, $attribute_name, $source_value );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $is_dynamic ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $global_post = $post;
</span><span class="cx" style="display: block; padding: 0 10px"> $parent = WP_Block_Supports::$block_to_render;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -488,10 +501,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // 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 );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the content of a single block.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunktestsphpunittestsblockbindingsrenderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/block-bindings/render.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-bindings/render.php 2024-02-09 04:15:08 UTC (rev 57573)
+++ trunk/tests/phpunit/tests/block-bindings/render.php 2024-02-09 10:38:38 UTC (rev 57574)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,16 +52,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $block_content = <<<HTML
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source"}}}} -->
+<p>This should not appear</p>
+<!-- /wp:paragraph -->
</ins><span class="cx" style="display: block; padding: 0 10px"> HTML;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px"> $parsed_blocks = parse_blocks( $block_content );
</span><span class="cx" style="display: block; padding: 0 10px"> $block = new WP_Block( $parsed_blocks[0] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = $block->render();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = '<p>test source value</p>';
- $result = $block->render();
-
- $this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame(
+ 'test source value',
+ $block->attributes['content'],
+ "The 'content' attribute should be updated with the value returned by the source."
+ );
+ $this->assertSame(
+ '<p>test source value</p>',
+ trim( $result ),
+ 'The block content should be updated with the value returned by the source.'
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,17 +93,66 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $value = 'test';
</del><span class="cx" style="display: block; padding: 0 10px"> $block_content = <<<HTML
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "$value"}}}}} --><p>This should not appear</p><!-- /wp:paragraph -->
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args": {"key": "test"}}}}} -->
+<p>This should not appear</p>
+<!-- /wp:paragraph -->
</ins><span class="cx" style="display: block; padding: 0 10px"> HTML;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $parsed_blocks = parse_blocks( $block_content );
+ $block = new WP_Block( $parsed_blocks[0] );
+ $result = $block->render();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame(
+ "The attribute name is 'content' and its binding has argument 'key' with value 'test'.",
+ $block->attributes['content'],
+ "The 'content' attribute should be updated with the value returned by the source."
+ );
+ $this->assertSame(
+ "<p>The attribute name is 'content' and its binding has argument 'key' with value 'test'.</p>",
+ trim( $result ),
+ 'The block content should be updated with the value returned by the source.'
+ );
+ }
+
+ /**
+ * Tests if the block content is updated with the value returned by the source
+ * for the Image block in the placeholder state.
+ *
+ * @ticket 60282
+ *
+ * @covers ::register_block_bindings_source
+ */
+ public function test_update_block_with_value_from_source_image_placeholder() {
+ $get_value_callback = function () {
+ return 'https://example.com/image.jpg';
+ };
+
+ register_block_bindings_source(
+ self::SOURCE_NAME,
+ array(
+ 'label' => self::SOURCE_LABEL,
+ 'get_value_callback' => $get_value_callback,
+ )
+ );
+
+ $block_content = <<<HTML
+<!-- wp:image {"metadata":{"bindings":{"url":{"source":"test/source"}}}} -->
+<figure class="wp-block-image"><img alt=""/></figure>
+<!-- /wp:image -->
+HTML;
</ins><span class="cx" style="display: block; padding: 0 10px"> $parsed_blocks = parse_blocks( $block_content );
</span><span class="cx" style="display: block; padding: 0 10px"> $block = new WP_Block( $parsed_blocks[0] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $result = $block->render();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected = "<p>The attribute name is 'content' and its binding has argument 'key' with value '$value'.</p>";
- $result = $block->render();
-
- $this->assertEquals( $expected, $result, 'The block content should be updated with the value returned by the source.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame(
+ 'https://example.com/image.jpg',
+ $block->attributes['url'],
+ "The 'url' attribute should be updated with the value returned by the source."
+ );
+ $this->assertSame(
+ '<figure class="wp-block-image"><img src="https://example.com/image.jpg" alt=""/></figure>',
+ trim( $result ),
+ 'The block content should be updated with the value returned by the source.'
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>