<!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>[59080] trunk: Block Bindings: Adds context needed by sources during its processing</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/59080">59080</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/59080","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-09-23 12:33:14 +0000 (Mon, 23 Sep 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'>Block Bindings: Adds context needed by sources during its processing
Extends block context during block bindings processing. This implies that the context is extended ONLY for the blocks where bindings are defined and only when rendered on the page.
Props santosguillamot, gziolo, artemiosans, cbravobernal.
Fixes <a href="https://core.trac.wordpress.org/ticket/61642">#61642</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpblockbindingsregistryphp">trunk/src/wp-includes/class-wp-block-bindings-registry.php</a></li>
<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>
<li><a href="#trunktestsphpunittestsblockbindingswpBlockBindingsRegistryphp">trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpblockbindingsregistryphp"></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-bindings-registry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-bindings-registry.php 2024-09-23 06:50:27 UTC (rev 59079)
+++ trunk/src/wp-includes/class-wp-block-bindings-registry.php 2024-09-23 12:33:14 UTC (rev 59080)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -189,20 +189,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->sources[ $source_name ] = $source;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Adds `uses_context` defined by block bindings sources.
- add_filter(
- 'get_block_type_uses_context',
- function ( $uses_context, $block_type ) use ( $source ) {
- if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) {
- return $uses_context;
- }
- // Use array_values to reset the array keys.
- return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) );
- },
- 10,
- 2
- );
-
</del><span class="cx" style="display: block; padding: 0 10px"> return $source;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<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-09-23 06:50:27 UTC (rev 59079)
+++ trunk/src/wp-includes/class-wp-block.php 2024-09-23 12:33:14 UTC (rev 59080)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,6 +301,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</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">+ // Adds the necessary context defined by the source.
+ if ( ! empty( $block_binding_source->uses_context ) ) {
+ foreach ( $block_binding_source->uses_context as $context_name ) {
+ if ( array_key_exists( $context_name, $this->available_context ) ) {
+ $this->context[ $context_name ] = $this->available_context[ $context_name ];
+ }
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array();
</span><span class="cx" style="display: block; padding: 0 10px"> $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name );
</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-09-23 06:50:27 UTC (rev 59079)
+++ trunk/tests/phpunit/tests/block-bindings/render.php 2024-09-23 12:33:14 UTC (rev 59080)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,6 +158,68 @@
</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">+ * Tests that blocks can only access the context from the specific source.
+ *
+ * @ticket 61642
+ *
+ * @covers ::register_block_bindings_source
+ */
+ public function test_blocks_can_just_access_the_specific_uses_context() {
+ register_block_bindings_source(
+ 'test/source-one',
+ array(
+ 'label' => 'Test Source One',
+ 'get_value_callback' => function () {
+ return;
+ },
+ 'uses_context' => array( 'contextOne' ),
+ )
+ );
+
+ register_block_bindings_source(
+ 'test/source-two',
+ array(
+ 'label' => 'Test Source Two',
+ 'get_value_callback' => function ( $source_args, $block_instance, $attribute_name ) {
+ $value = $block_instance->context['contextTwo'];
+ // Try to use the context from source one, which shouldn't be available.
+ if ( ! empty( $block_instance->context['contextOne'] ) ) {
+ $value = $block_instance->context['contextOne'];
+ }
+ return "Value: $value";
+ },
+ 'uses_context' => array( 'contextTwo' ),
+ )
+ );
+
+ $block_content = <<<HTML
+<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source-two", "args": {"key": "test"}}}}} -->
+<p>Default content</p>
+<!-- /wp:paragraph -->
+HTML;
+ $parsed_blocks = parse_blocks( $block_content );
+ $block = new WP_Block(
+ $parsed_blocks[0],
+ array(
+ 'contextOne' => 'source one context value',
+ 'contextTwo' => 'source two context value',
+ )
+ );
+ $result = $block->render();
+
+ $this->assertSame(
+ 'Value: source two context value',
+ $block->attributes['content'],
+ "The 'content' should be updated with the value of the second source context value."
+ );
+ $this->assertSame(
+ '<p>Value: source two context value</p>',
+ trim( $result ),
+ 'The block content should be updated with the value of the source context.'
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Tests if the block content is updated with the value returned by the source
</span><span class="cx" style="display: block; padding: 0 10px"> * for the Image block in the placeholder state.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre></div>
<a id="trunktestsphpunittestsblockbindingswpBlockBindingsRegistryphp"></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/wpBlockBindingsRegistry.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php 2024-09-23 06:50:27 UTC (rev 59079)
+++ trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php 2024-09-23 12:33:14 UTC (rev 59080)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -344,51 +344,4 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $result = $this->registry->is_registered( self::$test_source_name );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- /**
- * Tests merging `uses_context` from multiple sources.
- *
- * @ticket 60525
- *
- * @covers ::register_block_bindings_source
- * @covers WP_Block_Type::get_uses_context
- */
- public function test_merging_uses_context_from_multiple_sources() {
- $get_value_callback = function () {
- return 'Anything';
- };
-
- $block_registry = WP_Block_Type_Registry::get_instance();
- $original_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
-
- register_block_bindings_source(
- 'test/source-one',
- array(
- 'label' => 'Test Source One',
- 'get_value_callback' => $get_value_callback,
- 'uses_context' => array( 'commonContext', 'sourceOneContext' ),
- )
- );
-
- register_block_bindings_source(
- 'test/source-two',
- array(
- 'label' => 'Test Source Two',
- 'get_value_callback' => $get_value_callback,
- 'uses_context' => array( 'commonContext', 'sourceTwoContext' ),
- )
- );
-
- $new_uses_context = $block_registry->get_registered( 'core/paragraph' )->uses_context;
- unregister_block_bindings_source( 'test/source-one' );
- unregister_block_bindings_source( 'test/source-two' );
- // Checks that the resulting `uses_context` contains the values from both sources.
- $this->assertContains( 'commonContext', $new_uses_context );
- $this->assertContains( 'sourceOneContext', $new_uses_context );
- $this->assertContains( 'sourceTwoContext', $new_uses_context );
- // Checks that the resulting `uses_context` added 3 unique items.
- $this->assertSame( count( $original_uses_context ) + 3, count( $new_uses_context ) );
- // Checks that the array isn't sparse to prevent issues in the editor.
- $this->assertSame( array_key_last( $new_uses_context ), count( $new_uses_context ) - 1 );
- }
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>