From 417cff4bdc7296047c5c6a377f6988e9f19ab0d3 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 29 Oct 2026 13:37:06 +0100 Subject: [PATCH 01/23] Block Bindings: Support `innerBlocks` bindings on the Group and Column blocks. A block bindings source can now supply the inner blocks of a Group or Column block through `metadata.bindings.innerBlocks`. The value is serialized block markup, which replaces the block's own inner blocks before they render. An empty string leaves the block without inner blocks, and any other value keeps its own. The markup is sanitized with `wp_kses_post()`, as bound rich text already is, and its custom CSS is removed. Supplied blocks can't bind their own inner blocks, so that sources can't expand each other forever. The editor isn't offered `innerBlocks` yet, as it doesn't render these bindings. Backports WordPress/gutenberg#85568. Fixes #66415. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/block-bindings.php | 3 + src/wp-includes/block-editor.php | 3 +- src/wp-includes/class-wp-block.php | 153 +++++++++ .../tests/block-bindings/innerBlocks.php | 312 ++++++++++++++++++ 4 files changed, 470 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/block-bindings/innerBlocks.php diff --git a/src/wp-includes/block-bindings.php b/src/wp-includes/block-bindings.php index 1250f92e99848..7809e05cd4185 100644 --- a/src/wp-includes/block-bindings.php +++ b/src/wp-includes/block-bindings.php @@ -138,6 +138,7 @@ function get_block_bindings_source( string $source_name ) { * @since 6.9.0 * @since 7.1.0 Added support for the List Item block. * @since 7.2.0 Added support for the Icon block. + * @since 7.3.0 Added support for the inner blocks of the Group and Column blocks. * * @param string $block_type The block type whose supported attributes are being retrieved. * @return array The list of block attributes that are supported by block bindings. @@ -153,6 +154,8 @@ function get_block_bindings_supported_attributes( $block_type ) { 'core/navigation-link' => array( 'url' ), 'core/navigation-submenu' => array( 'url' ), 'core/icon' => array( 'icon' ), + 'core/group' => array( 'innerBlocks' ), + 'core/column' => array( 'innerBlocks' ), ); $supported_block_attributes = diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index 24bdd2f6db5db..a3d62db114e68 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -502,7 +502,8 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $editor_settings['__experimentalBlockBindingsSupportedAttributes'] = array(); foreach ( array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ) as $block_type ) { - $supported_block_attributes = get_block_bindings_supported_attributes( $block_type ); + // The editor doesn't render inner blocks bindings yet. + $supported_block_attributes = array_values( array_diff( get_block_bindings_supported_attributes( $block_type ), array( 'innerBlocks' ) ) ); if ( ! empty( $supported_block_attributes ) ) { $editor_settings['__experimentalBlockBindingsSupportedAttributes'][ $block_type ] = $supported_block_attributes; } diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 8baf0f99bacde..6d82a1c13e947 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -273,6 +273,7 @@ public function __get( $name ) { * @since 6.5.0 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. * @since 6.7.0 Return any updated bindings metadata in the computed attributes. + * @since 7.3.0 Replace the inner blocks of the block for an `innerBlocks` binding. * * @return array The computed block attributes for the provided block bindings. */ @@ -310,6 +311,10 @@ private function process_block_bindings() { * resulting array. */ foreach ( $supported_block_attributes as $attribute_name ) { + // Inner blocks are only bound explicitly. + if ( 'innerBlocks' === $attribute_name && ! isset( $bindings['innerBlocks'] ) ) { + continue; + } // Retain any non-pattern override bindings that might be present. $updated_bindings[ $attribute_name ] = $bindings[ $attribute_name ] ?? array( 'source' => 'core/pattern-overrides' ); } @@ -351,6 +356,12 @@ private function process_block_bindings() { $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); + // Inner blocks are blocks, not an attribute. + if ( 'innerBlocks' === $attribute_name ) { + $this->replace_inner_blocks( $source_value ); + continue; + } + // If the value is not null, process the HTML based on the block and the attribute. if ( ! is_null( $source_value ) ) { $computed_attributes[ $attribute_name ] = $source_value; @@ -360,6 +371,148 @@ private function process_block_bindings() { return $computed_attributes; } + /** + * Replaces the inner blocks of the block with the ones a block bindings + * source supplies, before they render. + * + * The value is serialized block markup. An empty string leaves the block + * without inner blocks. Any other value, like the plain text of a field, + * leaves the block with its own inner blocks. + * + * @since 7.3.0 + * + * @global WP_Embed $wp_embed WordPress Embed object. + * + * @param mixed $value The value of the source. + */ + private function replace_inner_blocks( $value ) { + global $wp_embed; + + if ( '' !== $value && ! ( is_string( $value ) && '

Default

' ) { + $bindings = $bindings ?? array( 'innerBlocks' => array( 'source' => self::SOURCE ) ); + $markup = ''; + $markup .= '
' . $inner_html . '
'; + $markup .= ''; + $block = new WP_Block( parse_blocks( $markup )[0] ); + return $block->render(); + } + + /** + * Returns the attributes the supplied blocks render with. + * + * @return array[] The attributes of each rendered block, in render order. + */ + private function render_group_and_get_supplied_attributes() { + $attributes = array(); + $capture = static function ( $parsed_block, $source_block, $parent_block ) use ( &$attributes ) { + if ( $parent_block ) { + $attributes[] = $parsed_block['attrs']; + } + return $parsed_block; + }; + add_filter( 'render_block_data', $capture, 10, 3 ); + $this->render_group(); + remove_filter( 'render_block_data', $capture ); + return $attributes; + } + + /** + * @ticket 66415 + */ + public function test_value_replaces_inner_blocks() { + $this->value = '

One

Two

'; + $output = $this->render_group(); + $this->assertMatchesRegularExpression( '#>One

]*>Two

$#', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + + /** + * @ticket 66415 + */ + public function test_value_may_start_with_whitespace() { + $this->value = "\n\t

One

"; + $this->assertStringContainsString( '>One

', $this->render_group() ); + } + + /** + * @ticket 66415 + */ + public function test_empty_value_removes_inner_blocks() { + $this->value = ''; + $output = $this->render_group(); + $this->assertStringNotContainsString( 'assertStringContainsString( 'wp-block-group', $output ); + } + + /** + * @ticket 66415 + */ + public function test_without_value_keeps_own_inner_blocks() { + $this->assertStringContainsString( '>Default

', $this->render_group() ); + } + + /** + * @ticket 66415 + */ + public function test_plain_text_value_keeps_own_inner_blocks() { + $this->value = 'Plain text'; + $output = $this->render_group(); + $this->assertStringContainsString( '>Default

', $output ); + $this->assertStringNotContainsString( 'Plain text', $output ); + } + + /** + * @ticket 66415 + */ + public function test_value_does_not_become_an_attribute() { + $this->value = '

One

'; + $block = new WP_Block( parse_blocks( '
' )[0] ); + $block->render(); + $this->assertArrayNotHasKey( 'innerBlocks', $block->attributes ); + } + + /** + * @ticket 66415 + */ + public function test_default_pattern_overrides_binding_does_not_bind_inner_blocks() { + $requested = array(); + $record = static function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) use ( &$requested ) { + $requested[] = $attribute_name; + return $value; + }; + add_filter( 'block_bindings_source_value', $record, 10, 5 ); + $output = $this->render_group( array( '__default' => array( 'source' => 'core/pattern-overrides' ) ) ); + remove_filter( 'block_bindings_source_value', $record ); + + $this->assertNotContains( 'innerBlocks', $requested ); + $this->assertStringContainsString( '>Default

', $output ); + } + + /** + * @ticket 66415 + */ + public function test_value_is_sanitized() { + $this->value = '

Safe

'; + $output = $this->render_group(); + $this->assertStringNotContainsString( '

'; + $excerpt = excerpt_remove_blocks( $this->bound_group_markup( array(), 'Default' ) ); + $this->assertStringContainsString( 'One', $excerpt ); + $this->assertStringNotContainsString( 'value = '

One

Code
'; + $excerpt = excerpt_remove_blocks( $this->bound_group_markup( array(), 'Default' ) ); + $this->assertStringContainsString( '>One

', $excerpt ); + $this->assertStringNotContainsString( 'Code', $excerpt ); + } + + /** + * @ticket 66415 + * + * @covers ::excerpt_remove_blocks + */ + public function test_excerpt_leaves_out_the_blocks_that_excerpts_do_not_allow_from_the_value_of_a_group_without_own_inner_blocks() { + $this->value = '

One

Code
'; + $excerpt = excerpt_remove_blocks( '
' ); + $this->assertStringContainsString( '>One

', $excerpt ); + $this->assertStringNotContainsString( 'Code', $excerpt ); + } + + /** + * @ticket 66415 + * + * @covers ::excerpt_remove_blocks + */ + public function test_excerpt_has_the_text_of_the_value_of_a_group_inside_another_group() { + $this->value = '

One

'; + $excerpt = excerpt_remove_blocks( '
' . $this->bound_group_markup( array(), 'Default' ) . '
' ); + $this->assertStringContainsString( '>One

', $excerpt ); + $this->assertStringNotContainsString( 'Default', $excerpt ); + } + + /** + * @ticket 66415 + * + * @covers ::excerpt_remove_blocks + */ + public function test_excerpt_ignores_bindings_to_the_same_source_inside_the_value() { + $this->value = $this->bound_group_markup(); + $excerpt = excerpt_remove_blocks( $this->bound_group_markup( array(), 'Default' ) ); + $this->assertStringContainsString( '>Nested own

', $excerpt ); + $this->assertStringNotContainsString( 'Default', $excerpt ); + $this->assertSame( 1, $this->inner_blocks_calls ); + } } From ea65b90418022648e678657e99bf02273a494b7e Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Thu, 26 Nov 2026 11:19:57 +0100 Subject: [PATCH 17/23] Block Bindings: Test that a rejected value spends no inner blocks budget. A value the allowed blocks check rejects doesn't count against `block_bindings_inner_blocks_budget`, so a later allowed value at the same budget still renders. Co-Authored-By: Claude Opus 5.5 --- .../tests/block-bindings/innerBlocks.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index 29b3c5adb9e68..d1248ac92f989 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -435,6 +435,34 @@ public function test_budget_starts_again_for_the_next_outermost_bound_block() { $this->assertSame( 11, substr_count( $output, '>Supplied

' ) ); } + /** + * @ticket 66415 + */ + public function test_value_the_bound_block_rejects_spends_no_budget() { + $this->value_callback = function ( $source_args ) { + $variant = $source_args['variant'] ?? null; + if ( 'rejected' === $variant ) { + return '

Rejected

'; + } + if ( 'allowed' === $variant ) { + return '

Allowed

'; + } + return '

Rejected own

' + . $this->bound_group_markup( array( 'variant' => 'allowed' ) ); + }; + $budget = static function () { + return 1; + }; + add_filter( 'block_bindings_inner_blocks_budget', $budget ); + $output = $this->render_group(); + remove_filter( 'block_bindings_inner_blocks_budget', $budget ); + + $this->assertStringContainsString( '>Rejected own

', $output ); + $this->assertStringNotContainsString( 'Rejected', $output ); + $this->assertStringContainsString( '>Allowed

', $output ); + $this->assertStringNotContainsString( 'Nested own', $output ); + } + /** * @ticket 66415 */ From afbe803e94a1ee4f698c3535bd2e61d0b73bad80 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Fri, 27 Nov 2026 16:34:12 +0100 Subject: [PATCH 18/23] Block Bindings: Count HTML outside blocks as a Classic block in the allowed blocks check. The allowed blocks check skipped every supplied chunk without a block name, so a bound block with `allowedBlocks` rendered loose HTML it would reject as a block. HTML outside any block now counts as `core/freeform`, unless it is only whitespace. Co-Authored-By: Claude Fable 5.2 --- src/wp-includes/class-wp-block.php | 11 ++++--- .../tests/block-bindings/innerBlocks.php | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index e9b145437f3a6..350b3ef228269 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -638,7 +638,9 @@ private static function prepare_bound_inner_blocks( array $blocks ): array { * * A block fails when the `allowedBlocks` of this block's type or attributes * leaves it out, or when its own `parent` or `ancestor` leaves out this - * block. Inner blocks of the supplied blocks are not checked. + * block. HTML outside any block counts as a Classic block + * (`core/freeform`), unless it is only whitespace. Inner blocks of the + * supplied blocks are not checked. * * @since 7.3.0 * @@ -651,15 +653,16 @@ private function are_inner_blocks_allowed( array $blocks ): bool { $this->attributes['allowedBlocks'] ?? null, ); foreach ( $blocks as $block ) { - if ( null === $block['blockName'] ) { + $block_name = $block['blockName'] ?? 'core/freeform'; + if ( null === $block['blockName'] && '' === trim( $block['innerHTML'] ) ) { continue; } foreach ( $allowed_lists as $allowed_blocks ) { - if ( is_array( $allowed_blocks ) && ! in_array( $block['blockName'], $allowed_blocks, true ) ) { + if ( is_array( $allowed_blocks ) && ! in_array( $block_name, $allowed_blocks, true ) ) { return false; } } - $block_type = $this->registry->get_registered( $block['blockName'] ); + $block_type = $this->registry->get_registered( $block_name ); if ( ! $block_type ) { continue; } diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index d1248ac92f989..ebe38a694d034 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -753,6 +753,38 @@ public function test_supplied_block_missing_from_the_allowed_blocks_attribute_ke $this->assertStringNotContainsString( 'Two', $output ); } + /** + * @ticket 66415 + */ + public function test_supplied_html_outside_blocks_keeps_own_inner_blocks_when_the_classic_block_is_not_allowed() { + $this->value = '

One

Two

'; + $output = do_blocks( '

Default

' ); + $this->assertStringContainsString( '>Default

', $output ); + $this->assertStringNotContainsString( 'One', $output ); + $this->assertStringNotContainsString( 'Two', $output ); + } + + /** + * @ticket 66415 + */ + public function test_supplied_html_outside_blocks_renders_when_the_classic_block_is_allowed() { + $this->value = '

One

Two

'; + $output = do_blocks( '

Default

' ); + $this->assertStringContainsString( '>One

', $output ); + $this->assertStringContainsString( '

Two

', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + + /** + * @ticket 66415 + */ + public function test_supplied_html_outside_blocks_renders_when_the_bound_block_allows_any_block() { + $this->value = '

One

Two

'; + $output = $this->render_group(); + $this->assertStringContainsString( '

Two

', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + /** * @ticket 66415 */ From 09eff416134eb6ecda02cc4b8408a20cea1c2097 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sat, 28 Nov 2026 19:38:07 +0100 Subject: [PATCH 19/23] Block Bindings: Key inner blocks requests by block name too. A source can return other inner blocks for another block type, so two block types with the same binding and attributes are no longer the same request. Also tests that a `block_bindings_source_value` filter with the default priority can change the supplied inner blocks. Co-Authored-By: Claude Fable 5.2 --- src/wp-includes/class-wp-block.php | 4 +- .../tests/block-bindings/innerBlocks.php | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 350b3ef228269..d89bb2c777863 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -477,7 +477,7 @@ private function replace_inner_blocks( WP_Block_Bindings_Source $source, array $ /** * Returns what the block asks its block bindings source for `innerBlocks`: * the source, the arguments of the binding, the context the source uses, - * and the attributes of the block. + * and the name and attributes of the block. * * Two blocks with the same request get the same inner blocks from the * source, whatever the order of its keys. @@ -495,7 +495,7 @@ private function get_inner_blocks_request( WP_Block_Bindings_Source $source, arr $context[ $context_name ] = $this->context[ $context_name ]; } } - $request = array( $source->name, $source_args, $context, $this->attributes ); + $request = array( $source->name, $source_args, $context, $this->name, $this->attributes ); wp_recursive_ksort( $request ); return $request; } diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index ebe38a694d034..2c1d07079358e 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -855,6 +855,49 @@ public function test_nested_supplied_blocks_are_not_checked() { $this->assertStringNotContainsString( 'Default', $output ); } + /** + * @ticket 66415 + */ + public function test_binding_of_another_block_type_with_the_same_request_inside_the_value_resolves() { + register_block_type( 'test/host-a' ); + register_block_type( 'test/host-b' ); + $add_inner_blocks = static function ( $supported_attributes, $block_type ) { + return in_array( $block_type, array( 'test/host-a', 'test/host-b' ), true ) ? array( 'innerBlocks' ) : $supported_attributes; + }; + add_filter( 'block_bindings_supported_attributes', $add_inner_blocks, 10, 2 ); + $this->value_callback = static function ( $source_args, $block_instance ) { + if ( 'test/host-b' === $block_instance->name ) { + return '

From B

'; + } + return '

Nested own

'; + }; + $output = do_blocks( '

Default

' ); + remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); + unregister_block_type( 'test/host-a' ); + unregister_block_type( 'test/host-b' ); + + $this->assertStringContainsString( '>From B

', $output ); + $this->assertStringNotContainsString( 'Nested own', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + + /** + * @ticket 66415 + */ + public function test_source_value_filter_with_the_default_priority_replaces_the_supplied_inner_blocks() { + $this->value = '

One

'; + $replace = static function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) { + return 'innerBlocks' === $attribute_name ? '

Filtered

' : $value; + }; + add_filter( 'block_bindings_source_value', $replace, 10, 5 ); + $output = $this->render_group(); + remove_filter( 'block_bindings_source_value', $replace ); + + $this->assertStringContainsString( '>Filtered

', $output ); + $this->assertStringNotContainsString( 'One', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + /** * @ticket 66415 */ From 51631a4666ec1fcdd42e7fd74bbb82e7aa93be82 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sun, 29 Nov 2026 15:59:25 +0100 Subject: [PATCH 20/23] Block Bindings: Move inner blocks bindings out of WP_Block into filters. The code now lives in wp-includes/block-bindings/inner-blocks.php, the same functions as Gutenberg with Core names. It replaces the inner blocks on `block_bindings_source_value`, after the other callbacks, and stops tracking them on `render_block`. `WP_Block` is the same as on trunk. A source call that is already rendering is still asked, and its value is ignored. The tests now match Gutenberg's, plus the excerpt tests. See #66415. Co-Authored-By: Claude Opus 5.5 --- .../block-bindings/inner-blocks.php | 529 ++++++++++++++ src/wp-includes/class-wp-block.php | 691 +++--------------- src/wp-settings.php | 1 + .../tests/block-bindings/innerBlocks.php | 235 ++++-- 4 files changed, 811 insertions(+), 645 deletions(-) create mode 100644 src/wp-includes/block-bindings/inner-blocks.php diff --git a/src/wp-includes/block-bindings/inner-blocks.php b/src/wp-includes/block-bindings/inner-blocks.php new file mode 100644 index 0000000000000..96fe331e8989b --- /dev/null +++ b/src/wp-includes/block-bindings/inner-blocks.php @@ -0,0 +1,529 @@ +next_token() ) { + if ( $processor->is_html() && ! $processor->is_non_whitespace_html() ) { + continue; + } + return ! $processor->is_html() && $processor->opens_block(); + } + return false; +} + +/** + * Returns the block bindings source call of a bound block: what it asks its + * block bindings source for `innerBlocks`. That is the source, the arguments of + * the binding, the context the source uses, and the name and attributes of the + * block. + * + * Two bound blocks with the same block bindings source call get the same inner + * blocks from the source, whatever the order of its keys. + * + * @since 7.3.0 + * @access private + * + * @param string $source_name The name of the source. + * @param array $source_args The arguments of the binding. + * @param WP_Block $block_instance The block instance. + * @return array The block bindings source call. + */ +function _block_bindings_get_inner_blocks_source_call( $source_name, $source_args, $block_instance ) { + $source = get_block_bindings_source( $source_name ); + $context = array(); + foreach ( $source ? (array) $source->uses_context : array() as $context_name ) { + if ( array_key_exists( $context_name, $block_instance->context ) ) { + $context[ $context_name ] = $block_instance->context[ $context_name ]; + } + } + $source_call = array( $source_name, $source_args, $context, $block_instance->name, $block_instance->attributes ); + wp_recursive_ksort( $source_call ); + return $source_call; +} + +/** + * Returns whether the inner blocks a source supplied for the given block + * bindings source call are rendering. + * + * @since 7.3.0 + * @access private + * + * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. + * + * @param array $source_call The block bindings source call, as `_block_bindings_get_inner_blocks_source_call()` returns it. + * @return bool Whether supplied inner blocks for the block bindings source call are rendering. + */ +function _block_bindings_is_rendering_inner_blocks_source_call( $source_call ) { + global $wp_block_bindings_rendering_inner_blocks; + foreach ( (array) $wp_block_bindings_rendering_inner_blocks as $rendering ) { + if ( $rendering['source_call'] === $source_call ) { + return true; + } + } + return false; +} + +/** + * Stops tracking a block whose supplied inner blocks were rendering, once the + * block itself has rendered. + * + * The blocks still rendering inside it, if any, stopped with it, so they are + * forgotten along with it. + * + * A block that throws while it renders never reaches this filter, so it stays + * tracked. If something catches the exception and goes on rendering, every + * bound block after it counts as nested for the rest of the page load: the + * same source call keeps its own inner blocks, and once the budget is spent, so + * does every other bound block on the page. Nothing loops or renders + * unsanitized because of it. No hook runs after a block throws. + * + * @since 7.3.0 + * @access private + * + * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. + * + * @param string $block_content The rendered block. + * @param array $parsed_block The parsed block. + * @param WP_Block $instance The block instance. + * @return string The rendered block. + */ +function _block_bindings_forget_rendered_inner_blocks( $block_content, $parsed_block, $instance ) { + global $wp_block_bindings_rendering_inner_blocks; + foreach ( (array) $wp_block_bindings_rendering_inner_blocks as $index => $rendering ) { + if ( $rendering['bound_block'] === $instance ) { + $wp_block_bindings_rendering_inner_blocks = array_slice( $wp_block_bindings_rendering_inner_blocks, 0, $index ); + break; + } + } + return $block_content; +} +add_filter( 'render_block', '_block_bindings_forget_rendered_inner_blocks', 10, 3 ); + +/** + * Parses the inner blocks a block bindings source supplies. + * + * The markup never went through the filters that sanitize post content on + * save, and the user who supplied it may not be allowed unfiltered HTML, so by + * default it is sanitized as on save, attributes of its blocks included. Its + * embeds are expanded as the embeds of the content were, before its blocks + * render. + * + * @since 7.3.0 + * @access private + * + * @global WP_Embed $wp_embed WordPress Embed object. + * + * @param string $markup The serialized blocks. + * @param string $source_name The name of the source. + * @param array $source_args The arguments of the binding. + * @param WP_Block $block_instance The block instance. + * @return array[] The parsed blocks. + */ +function _block_bindings_parse_bound_inner_blocks( $markup, $source_name, $source_args, $block_instance ) { + global $wp_embed; + + /** + * Filters whether the inner blocks a block bindings source supplies are + * sanitized before they render. + * + * By default the markup is sanitized as the post content of a user without + * the `unfiltered_html` capability, since the user who supplied it is + * unknown: `wp_kses_post()` removes the HTML that user may not publish, in + * the attributes of the blocks included. A source whose markup comes from + * trusted code, not from users, may keep it as supplied. Custom CSS is + * removed from the blocks either way. + * + * Sanitizing keeps the block delimiters that `serialize_blocks()` writes, + * which escapes the characters `wp_kses_post()` rewrites inside comments. + * + * @since 7.3.0 + * + * @param bool $sanitize Whether to sanitize the markup. Default true. + * @param string $source_name The name of the source. + * @param array $source_args The arguments of the binding. + * @param WP_Block $block_instance The block whose inner blocks the source supplies. + */ + if ( apply_filters( 'block_bindings_sanitize_inner_blocks', true, $source_name, $source_args, $block_instance ) ) { + $markup = wp_kses_post( $markup ); + } + if ( $wp_embed instanceof WP_Embed ) { + $markup = $wp_embed->run_shortcode( $markup ); + $markup = $wp_embed->autoembed( $markup ); + } + + return _block_bindings_prepare_bound_inner_blocks( parse_blocks( $markup ) ); +} + +/** + * Prepares the blocks a source supplies, and their inner blocks, to render. + * + * Removes their custom CSS, which only users who can edit CSS may add: + * whoever supplied the value is unknown. + * + * @since 7.3.0 + * @access private + * + * @param array[] $blocks The parsed blocks. + * @return array[] The parsed blocks. + */ +function _block_bindings_prepare_bound_inner_blocks( $blocks ) { + foreach ( $blocks as &$block ) { + if ( isset( $block['attrs']['style'] ) ) { + $block['attrs']['style'] = wp_strip_custom_css_from_style_array( $block['attrs']['style'] ); + if ( empty( $block['attrs']['style'] ) ) { + unset( $block['attrs']['style'] ); + } + } + + $block['innerBlocks'] = _block_bindings_prepare_bound_inner_blocks( $block['innerBlocks'] ); + } + return $blocks; +} + +/** + * Determines whether the bound block accepts the top-level blocks a source + * supplies. + * + * A block fails when the `allowedBlocks` of the bound block's type or + * attributes leaves it out, or when its own `parent` or `ancestor` leaves out + * the bound block. HTML outside any block counts as a Classic block + * (`core/freeform`), unless it is only whitespace. Inner blocks of the + * supplied blocks are not checked. + * + * @since 7.3.0 + * @access private + * + * @param array[] $blocks The parsed blocks. + * @param WP_Block $block_instance The bound block. + * @return bool Whether every block is allowed. + */ +function _block_bindings_are_inner_blocks_allowed( $blocks, $block_instance ) { + $allowed_lists = array( + $block_instance->block_type->allowed_blocks ?? null, + $block_instance->attributes['allowedBlocks'] ?? null, + ); + foreach ( $blocks as $block ) { + $block_name = $block['blockName'] ?? 'core/freeform'; + if ( null === $block['blockName'] && '' === trim( $block['innerHTML'] ) ) { + continue; + } + foreach ( $allowed_lists as $allowed_blocks ) { + if ( is_array( $allowed_blocks ) && ! in_array( $block_name, $allowed_blocks, true ) ) { + return false; + } + } + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); + if ( ! $block_type ) { + continue; + } + foreach ( array( $block_type->parent, $block_type->ancestor ) as $allowed_parents ) { + if ( is_array( $allowed_parents ) && ! in_array( $block_instance->name, $allowed_parents, true ) ) { + return false; + } + } + } + return true; +} + +/** + * Returns the inner content of a block with slots for the given number of + * inner blocks. + * + * The slots replace the block's own inner blocks and the HTML between them. A + * block without inner blocks gets them before the closing tag of the element + * that wraps its content. + * + * @since 7.3.0 + * @access private + * + * @param array $inner_content The inner content of the block. + * @param int $count The number of inner blocks. + * @return array|null The inner content, or null if there's no element to put + * the inner blocks in. + */ +function _block_bindings_get_inner_content_with_slots( $inner_content, $count ) { + if ( ! is_array( $inner_content ) ) { + return null; + } + + $slots = array_fill( 0, $count, null ); + $own_slots = array_keys( $inner_content, null, true ); + if ( $own_slots ) { + $before_first_slot = array_slice( $inner_content, 0, $own_slots[0] ); + $after_last_slot = array_slice( $inner_content, end( $own_slots ) + 1 ); + return array_merge( $before_first_slot, $slots, $after_last_slot ); + } + + $wrapper_parts = _block_bindings_split_at_wrapper_closer( implode( '', $inner_content ) ); + if ( null === $wrapper_parts ) { + return null; + } + return array_merge( array( $wrapper_parts[0] ), $slots, array( $wrapper_parts[1] ) ); +} + +/** + * Splits the HTML of the element that wraps a block's content before the + * element's closing tag. + * + * The wrapper is the last element of the HTML. When all it holds is the inner + * container of a Group block saved before WordPress 5.8, the split happens + * before the closing tag of the container, where the block's own inner blocks + * would be. + * + * @since 7.3.0 + * @access private + * + * @param string $html The HTML of the wrapper element. + * @return string[]|null The HTML before the closing tag and the HTML from it, + * or null if the HTML has no wrapper element to split. + */ +function _block_bindings_split_at_wrapper_closer( $html ) { + $processor = WP_HTML_Processor::create_fragment( $html ); + if ( null === $processor ) { + return null; + } + + $tokens = array(); + while ( $processor->next_token() ) { + $is_opener = '#tag' === $processor->get_token_type() && ! $processor->is_tag_closer(); + $tokens[] = array( + 'html' => $processor->serialize_token(), + 'depth' => $processor->get_current_depth(), + 'is_opener' => $is_opener, + 'is_closer' => '#tag' === $processor->get_token_type() && $processor->is_tag_closer(), + 'is_inner_container' => $is_opener && $processor->has_class( 'wp-block-group__inner-container' ), + 'is_whitespace' => '#text' === $processor->get_token_type() && '' === trim( $processor->get_modifiable_text() ), + ); + } + if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() ) { + return null; + } + + $closer_index = _block_bindings_find_wrapper_closer( $tokens ); + if ( null === $closer_index ) { + return null; + } + $before = implode( '', array_column( array_slice( $tokens, 0, $closer_index ), 'html' ) ); + $after = implode( '', array_column( array_slice( $tokens, $closer_index ), 'html' ) ); + return array( $before, $after ); +} + +/** + * Finds the closing tag of the wrapper element among the tokens of a block's + * content: the last element of the content, or the legacy inner container of + * a Group block when it is all that element holds. + * + * @since 7.3.0 + * @access private + * + * @param array[] $tokens The tokens, each with its HTML, its depth, and whether it opens an element, closes one, opens a legacy inner container, or is whitespace. + * @return int|null The index of the closing tag, or null if no element wraps the content. + */ +function _block_bindings_find_wrapper_closer( $tokens ) { + $wrapper_depth = null; + foreach ( $tokens as $token ) { + if ( $token['is_opener'] ) { + $wrapper_depth = $token['depth']; + break; + } + } + if ( null === $wrapper_depth ) { + return null; + } + + $closer_index = null; + foreach ( $tokens as $index => $token ) { + if ( $token['is_closer'] && $token['depth'] === $wrapper_depth - 1 ) { + $closer_index = $index; + } + } + if ( null === $closer_index ) { + return null; + } + + $opener_index = $closer_index; + while ( ! $tokens[ $opener_index ]['is_opener'] || $tokens[ $opener_index ]['depth'] !== $wrapper_depth ) { + --$opener_index; + } + + $inner_container_closer_index = _block_bindings_find_sole_inner_container_closer( $tokens, $opener_index, $closer_index, $wrapper_depth ); + return $inner_container_closer_index ?? $closer_index; +} + +/** + * Finds the closing tag of the legacy inner container of a Group block, when + * the container is all that the wrapper element holds, whitespace aside. + * + * @since 7.3.0 + * @access private + * + * @param array[] $tokens The tokens of the block's content. + * @param int $opener_index The index of the wrapper's opening tag. + * @param int $closer_index The index of the wrapper's closing tag. + * @param int $wrapper_depth The depth of the wrapper's opening tag. + * @return int|null The index of the container's closing tag, or null if the wrapper holds anything else. + */ +function _block_bindings_find_sole_inner_container_closer( $tokens, $opener_index, $closer_index, $wrapper_depth ) { + $index = $opener_index + 1; + while ( $index < $closer_index && $tokens[ $index ]['is_whitespace'] ) { + ++$index; + } + if ( $index >= $closer_index || ! $tokens[ $index ]['is_inner_container'] ) { + return null; + } + + while ( $index < $closer_index && ! ( $tokens[ $index ]['is_closer'] && $tokens[ $index ]['depth'] === $wrapper_depth ) ) { + ++$index; + } + $inner_container_closer_index = $index; + for ( ++$index; $index < $closer_index; ++$index ) { + if ( ! $tokens[ $index ]['is_whitespace'] ) { + return null; + } + } + return $inner_container_closer_index < $closer_index ? $inner_container_closer_index : null; +} + +/** + * Replaces the inner blocks of a block with the ones its source supplies, + * before the block renders them. + * + * Runs on the value that `WP_Block::process_block_bindings()` resolves for + * `innerBlocks`, after the other filters of the value, and returns null so + * that it never becomes an attribute. Only a binding in the block's own + * metadata replaces them, not the one the `__default` binding expands to. + * + * Not while the inner blocks the source supplied for the same block bindings + * source call render: the block then keeps its own inner blocks. This way + * sources whose values bind each other's, directly or through content that + * loads as it renders, like the menu of a Navigation block, can't render + * forever. Bindings that ask a source something else still resolve, inside + * supplied inner blocks too. + * + * Nor when the block doesn't accept one of the top-level supplied blocks: it + * then keeps its own inner blocks. + * + * Only within a budget, though: whoever writes a value writes the arguments of + * the bindings inside it, and a value whose bindings ask for it again with + * other arguments would otherwise render once for every order they can nest + * in. The budget counts every bound block that gets inner blocks inside the + * ones supplied to the outermost bound block, at any depth. Once it is spent, + * a bound block inside supplied inner blocks keeps its own. + * + * @since 7.3.0 + * @access private + * + * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. + * @global int $wp_block_bindings_inner_blocks_budget_spent How many bound blocks got inner blocks from a source inside the ones supplied to the outermost bound block rendering. + * + * @param mixed $value The value of the source. + * @param string $source_name The name of the source. + * @param array $source_args The arguments of the binding. + * @param WP_Block $block_instance The block instance. + * @param string $attribute_name The name of the bound attribute. + * @return mixed The value, or null for `innerBlocks`. + */ +function _block_bindings_replace_inner_blocks( $value, $source_name, $source_args, $block_instance, $attribute_name ) { + global $wp_block_bindings_rendering_inner_blocks, $wp_block_bindings_inner_blocks_budget_spent; + + if ( 'innerBlocks' !== $attribute_name ) { + return $value; + } + + $own_binding_source = $block_instance->parsed_block['attrs']['metadata']['bindings']['innerBlocks']['source'] ?? null; + if ( $own_binding_source !== $source_name || ! _block_bindings_is_inner_blocks_value( $value ) ) { + return null; + } + + $source_call = _block_bindings_get_inner_blocks_source_call( $source_name, $source_args, $block_instance ); + if ( _block_bindings_is_rendering_inner_blocks_source_call( $source_call ) ) { + return null; + } + + $is_nested = ! empty( $wp_block_bindings_rendering_inner_blocks ); + if ( $is_nested ) { + /** + * Filters the inner blocks bindings budget: how many bound blocks may + * get inner blocks from a source inside the inner blocks a source + * supplied to one bound block. + * + * The budget counts bound blocks at every depth below that block, and + * siblings alike, rather than levels of nesting. It keeps a value from + * rendering without bound through bindings that ask for it again with + * other arguments or context. + * + * @since 7.3.0 + * + * @param int $budget The number of bound blocks. Default 100. + */ + $budget = (int) apply_filters( 'block_bindings_inner_blocks_budget', 100 ); + if ( (int) $wp_block_bindings_inner_blocks_budget_spent >= $budget ) { + return null; + } + } + + $blocks = _block_bindings_parse_bound_inner_blocks( $value, $source_name, $source_args, $block_instance ); + if ( ! _block_bindings_are_inner_blocks_allowed( $blocks, $block_instance ) ) { + return null; + } + $inner_content = _block_bindings_get_inner_content_with_slots( $block_instance->parsed_block['innerContent'] ?? null, count( $blocks ) ); + if ( null === $inner_content ) { + return null; + } + + $block_instance->parsed_block['innerBlocks'] = $blocks; + $block_instance->parsed_block['innerContent'] = $inner_content; + $block_instance->parsed_block['innerHTML'] = implode( '', array_filter( $inner_content, 'is_string' ) ); + $block_instance->inner_blocks = new WP_Block_List( array() ); + $block_instance->inner_content = $inner_content; + $block_instance->inner_html = $block_instance->parsed_block['innerHTML']; + $block_instance->refresh_parsed_block_dependents(); + + $wp_block_bindings_inner_blocks_budget_spent = $is_nested ? (int) $wp_block_bindings_inner_blocks_budget_spent + 1 : 0; + $wp_block_bindings_rendering_inner_blocks[] = array( + 'bound_block' => $block_instance, + 'source_call' => $source_call, + ); + + return null; +} +add_filter( 'block_bindings_source_value', '_block_bindings_replace_inner_blocks', PHP_INT_MAX, 5 ); diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index d89bb2c777863..8baf0f99bacde 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -65,24 +65,6 @@ class WP_Block { */ protected $registry; - /** - * The blocks whose inner blocks a block bindings source supplied are - * rendering, outermost first, each with the request it made to the source. - * - * @since 7.3.0 - * @var array[] - */ - private static $rendering_inner_blocks = array(); - - /** - * How many blocks got inner blocks from a block bindings source inside the - * ones supplied to the outermost block rendering them. - * - * @since 7.3.0 - * @var int - */ - private static $inner_blocks_budget_spent = 0; - /** * List of inner blocks (of this same class) * @@ -291,8 +273,6 @@ public function __get( $name ) { * @since 6.5.0 * @since 6.6.0 Handle the `__default` attribute for pattern overrides. * @since 6.7.0 Return any updated bindings metadata in the computed attributes. - * @since 7.3.0 Replace the inner blocks of the block for an `innerBlocks` binding, - * which the `__default` binding doesn't add. * * @return array The computed block attributes for the provided block bindings. */ @@ -330,9 +310,6 @@ private function process_block_bindings() { * resulting array. */ foreach ( $supported_block_attributes as $attribute_name ) { - if ( 'innerBlocks' === $attribute_name && ! isset( $bindings['innerBlocks'] ) ) { - continue; - } // Retain any non-pattern override bindings that might be present. $updated_bindings[ $attribute_name ] = $bindings[ $attribute_name ] ?? array( 'source' => 'core/pattern-overrides' ); } @@ -371,13 +348,7 @@ private function process_block_bindings() { } } - $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); - - if ( 'innerBlocks' === $attribute_name ) { - $this->replace_inner_blocks( $block_binding_source, $source_args ); - continue; - } - + $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); $source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name ); // If the value is not null, process the HTML based on the block and the attribute. @@ -389,448 +360,6 @@ private function process_block_bindings() { return $computed_attributes; } - /** - * Replaces the inner blocks of the block with the ones a block bindings - * source supplies, before they render. - * - * Not while the inner blocks the source supplied for the same request - * render: the block then keeps its own inner blocks, and the source isn't - * asked. This way sources whose values bind each other's, directly or - * through content that loads as it renders, like the menu of a Navigation - * block, can't render forever. Bindings that ask a source something else - * still resolve, inside supplied inner blocks too. - * - * Nor when the block doesn't accept one of the top-level supplied blocks: it - * then keeps its own inner blocks. - * - * Only within a budget, though: whoever writes a value writes the arguments - * of the bindings inside it, and a value whose bindings ask for it again - * with other arguments would otherwise render once for every order they can - * nest in. The budget counts every block that gets inner blocks inside the - * ones supplied to the outermost block, at any depth. Once it is spent, a - * block inside supplied inner blocks keeps its own. - * - * @since 7.3.0 - * - * @param WP_Block_Bindings_Source $source The source of the binding. - * @param array $source_args The arguments of the binding. - */ - private function replace_inner_blocks( WP_Block_Bindings_Source $source, array $source_args ) { - $request = $this->get_inner_blocks_request( $source, $source_args ); - foreach ( self::$rendering_inner_blocks as $rendering ) { - if ( $rendering['request'] === $request ) { - return; - } - } - - $is_nested = ! empty( self::$rendering_inner_blocks ); - if ( $is_nested ) { - /** - * Filters the inner blocks bindings budget: how many blocks may get - * inner blocks from a block bindings source inside the inner blocks - * a source supplied to one block. - * - * The budget counts blocks at every depth below that block, and - * siblings alike, rather than levels of nesting. It keeps a value - * from rendering without bound through bindings that ask for it - * again with other arguments or context. - * - * @since 7.3.0 - * - * @param int $budget The number of blocks. Default 100. - */ - $budget = (int) apply_filters( 'block_bindings_inner_blocks_budget', 100 ); - if ( self::$inner_blocks_budget_spent >= $budget ) { - return; - } - } - - $value = $source->get_value( $source_args, $this, 'innerBlocks' ); - if ( ! self::is_inner_blocks_value( $value ) ) { - return; - } - - $blocks = $this->parse_bound_inner_blocks( $value, $source, $source_args ); - if ( ! $this->are_inner_blocks_allowed( $blocks ) ) { - return; - } - $inner_content = self::get_inner_content_with_slots( $this->parsed_block['innerContent'] ?? null, count( $blocks ) ); - if ( null === $inner_content ) { - return; - } - - $this->parsed_block['innerBlocks'] = $blocks; - $this->parsed_block['innerContent'] = $inner_content; - $this->parsed_block['innerHTML'] = implode( '', array_filter( $inner_content, 'is_string' ) ); - $this->inner_blocks = new WP_Block_List( array() ); - $this->inner_content = $inner_content; - $this->inner_html = $this->parsed_block['innerHTML']; - $this->refresh_parsed_block_dependents(); - - self::$inner_blocks_budget_spent = $is_nested ? self::$inner_blocks_budget_spent + 1 : 0; - self::$rendering_inner_blocks[] = array( - 'block' => $this, - 'request' => $request, - ); - } - - /** - * Returns what the block asks its block bindings source for `innerBlocks`: - * the source, the arguments of the binding, the context the source uses, - * and the name and attributes of the block. - * - * Two blocks with the same request get the same inner blocks from the - * source, whatever the order of its keys. - * - * @since 7.3.0 - * - * @param WP_Block_Bindings_Source $source The source of the binding. - * @param array $source_args The arguments of the binding. - * @return array The request. - */ - private function get_inner_blocks_request( WP_Block_Bindings_Source $source, array $source_args ): array { - $context = array(); - foreach ( (array) $source->uses_context as $context_name ) { - if ( array_key_exists( $context_name, $this->context ) ) { - $context[ $context_name ] = $this->context[ $context_name ]; - } - } - $request = array( $source->name, $source_args, $context, $this->name, $this->attributes ); - wp_recursive_ksort( $request ); - return $request; - } - - /** - * Stops tracking the block, once it has rendered, if the inner blocks a - * source supplied for it were rendering. - * - * The blocks still rendering inside it, if any, stopped with it, so they - * are forgotten along with it. A block that stops rendering with an - * exception is forgotten too, so that it doesn't count as rendering for - * the rest of the request when something catches the exception. - * - * @since 7.3.0 - */ - private function forget_rendered_inner_blocks() { - foreach ( self::$rendering_inner_blocks as $index => $rendering ) { - if ( $rendering['block'] === $this ) { - self::$rendering_inner_blocks = array_slice( self::$rendering_inner_blocks, 0, $index ); - return; - } - } - } - - /** - * Determines whether the value of a block bindings source supplies inner - * blocks. - * - * Serialized block markup supplies them, and an empty string supplies none. - * Markup supplies them when the first block delimiter the block parser - * finds, after any whitespace, opens a block. Any other value, like the - * plain text of a field, leaves the block with its own inner blocks. - * - * @since 7.3.0 - * - * @param mixed $value The value of the source. - * @return bool Whether the value supplies inner blocks. - */ - private static function is_inner_blocks_value( $value ): bool { - if ( '' === $value ) { - return true; - } - if ( ! is_string( $value ) ) { - return false; - } - - $processor = new WP_Block_Processor( $value ); - while ( $processor->next_token() ) { - if ( $processor->is_html() && ! $processor->is_non_whitespace_html() ) { - continue; - } - return ! $processor->is_html() && $processor->opens_block(); - } - return false; - } - - /** - * Parses the inner blocks a block bindings source supplies. - * - * The markup never went through the filters that sanitize post content on - * save, and the user who supplied it may not be allowed unfiltered HTML, so - * by default it is sanitized as on save, attributes of its blocks included. - * Its embeds are expanded as the embeds of the content were, before its - * blocks render. - * - * @since 7.3.0 - * - * @global WP_Embed $wp_embed WordPress Embed object. - * - * @param string $markup The serialized blocks. - * @param WP_Block_Bindings_Source $source The source of the binding. - * @param array $source_args The arguments of the binding. - * @return array[] The parsed blocks. - */ - private function parse_bound_inner_blocks( string $markup, WP_Block_Bindings_Source $source, array $source_args ): array { - global $wp_embed; - - /** - * Filters whether the inner blocks a block bindings source supplies are - * sanitized before they render. - * - * By default the markup is sanitized as the post content of a user - * without the `unfiltered_html` capability, since the user who supplied - * it is unknown: `wp_kses_post()` removes the HTML that user may not - * publish, in the attributes of the blocks included. A source whose - * markup comes from trusted code, not from users, may keep it as - * supplied. Custom CSS is removed from the blocks either way. - * - * Sanitizing keeps the block delimiters that `serialize_blocks()` - * writes, which escapes the characters `wp_kses_post()` rewrites - * inside comments. - * - * @since 7.3.0 - * - * @param bool $sanitize Whether to sanitize the markup. Default true. - * @param string $source_name The name of the source. - * @param array $source_args The arguments of the binding. - * @param WP_Block $block_instance The block whose inner blocks the source supplies. - */ - if ( apply_filters( 'block_bindings_sanitize_inner_blocks', true, $source->name, $source_args, $this ) ) { - $markup = wp_kses_post( $markup ); - } - if ( $wp_embed instanceof WP_Embed ) { - $markup = $wp_embed->run_shortcode( $markup ); - $markup = $wp_embed->autoembed( $markup ); - } - - return self::prepare_bound_inner_blocks( parse_blocks( $markup ) ); - } - - /** - * Prepares the blocks a block bindings source supplies, and their inner - * blocks, to render. - * - * Removes their custom CSS, which only users who can edit CSS may add: - * whoever supplied the value is unknown. - * - * @since 7.3.0 - * - * @param array[] $blocks The parsed blocks. - * @return array[] The parsed blocks. - */ - private static function prepare_bound_inner_blocks( array $blocks ): array { - foreach ( $blocks as &$block ) { - if ( isset( $block['attrs']['style'] ) ) { - $block['attrs']['style'] = wp_strip_custom_css_from_style_array( $block['attrs']['style'] ); - if ( empty( $block['attrs']['style'] ) ) { - unset( $block['attrs']['style'] ); - } - } - - $block['innerBlocks'] = self::prepare_bound_inner_blocks( $block['innerBlocks'] ); - } - return $blocks; - } - - /** - * Determines whether the block accepts the top-level blocks a block - * bindings source supplies. - * - * A block fails when the `allowedBlocks` of this block's type or attributes - * leaves it out, or when its own `parent` or `ancestor` leaves out this - * block. HTML outside any block counts as a Classic block - * (`core/freeform`), unless it is only whitespace. Inner blocks of the - * supplied blocks are not checked. - * - * @since 7.3.0 - * - * @param array[] $blocks The parsed blocks. - * @return bool Whether every block is allowed. - */ - private function are_inner_blocks_allowed( array $blocks ): bool { - $allowed_lists = array( - $this->block_type->allowed_blocks ?? null, - $this->attributes['allowedBlocks'] ?? null, - ); - foreach ( $blocks as $block ) { - $block_name = $block['blockName'] ?? 'core/freeform'; - if ( null === $block['blockName'] && '' === trim( $block['innerHTML'] ) ) { - continue; - } - foreach ( $allowed_lists as $allowed_blocks ) { - if ( is_array( $allowed_blocks ) && ! in_array( $block_name, $allowed_blocks, true ) ) { - return false; - } - } - $block_type = $this->registry->get_registered( $block_name ); - if ( ! $block_type ) { - continue; - } - foreach ( array( $block_type->parent, $block_type->ancestor ) as $allowed_parents ) { - if ( is_array( $allowed_parents ) && ! in_array( $this->name, $allowed_parents, true ) ) { - return false; - } - } - } - return true; - } - - /** - * Returns the inner content of a block with slots for the given number of - * inner blocks. - * - * The slots replace the block's own inner blocks and the HTML between them. - * A block without inner blocks gets them before the closing tag of the - * element that wraps its content. - * - * @since 7.3.0 - * - * @param array|null $inner_content The inner content of the block. - * @param int $count The number of inner blocks. - * @return array|null The inner content, or null if there's no element to put - * the inner blocks in. - */ - private static function get_inner_content_with_slots( $inner_content, int $count ): ?array { - if ( ! is_array( $inner_content ) ) { - return null; - } - - $slots = array_fill( 0, $count, null ); - $own_slots = array_keys( $inner_content, null, true ); - if ( $own_slots ) { - $before_first_slot = array_slice( $inner_content, 0, $own_slots[0] ); - $after_last_slot = array_slice( $inner_content, end( $own_slots ) + 1 ); - return array_merge( $before_first_slot, $slots, $after_last_slot ); - } - - $wrapper_parts = self::split_at_wrapper_closer( implode( '', $inner_content ) ); - if ( null === $wrapper_parts ) { - return null; - } - return array_merge( array( $wrapper_parts[0] ), $slots, array( $wrapper_parts[1] ) ); - } - - /** - * Splits the HTML of the element that wraps a block's content before the - * element's closing tag. - * - * The wrapper is the last element of the HTML. When all it holds is the - * inner container of a Group block saved before WordPress 5.8, the split - * happens before the closing tag of the container, where the block's own - * inner blocks would be. - * - * @since 7.3.0 - * - * @param string $html The HTML of the wrapper element. - * @return string[]|null The HTML before the closing tag and the HTML from it, - * or null if the HTML has no wrapper element to split. - */ - private static function split_at_wrapper_closer( string $html ): ?array { - $processor = WP_HTML_Processor::create_fragment( $html ); - if ( null === $processor ) { - return null; - } - - $tokens = array(); - while ( $processor->next_token() ) { - $is_opener = '#tag' === $processor->get_token_type() && ! $processor->is_tag_closer(); - $tokens[] = array( - 'html' => $processor->serialize_token(), - 'depth' => $processor->get_current_depth(), - 'is_opener' => $is_opener, - 'is_closer' => '#tag' === $processor->get_token_type() && $processor->is_tag_closer(), - 'is_inner_container' => $is_opener && $processor->has_class( 'wp-block-group__inner-container' ), - 'is_whitespace' => '#text' === $processor->get_token_type() && '' === trim( $processor->get_modifiable_text() ), - ); - } - if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() ) { - return null; - } - - $closer_index = self::find_wrapper_closer( $tokens ); - if ( null === $closer_index ) { - return null; - } - $before = implode( '', array_column( array_slice( $tokens, 0, $closer_index ), 'html' ) ); - $after = implode( '', array_column( array_slice( $tokens, $closer_index ), 'html' ) ); - return array( $before, $after ); - } - - /** - * Finds the closing tag of the wrapper element among the tokens of a block's - * content: the last element of the content, or the legacy inner container - * of a Group block when it is all that element holds. - * - * @since 7.3.0 - * - * @param array[] $tokens The tokens, each with its HTML, its depth, and whether it opens an element, - * closes one, opens a legacy inner container, or is whitespace. - * @return int|null The index of the closing tag, or null if no element wraps the content. - */ - private static function find_wrapper_closer( array $tokens ): ?int { - $wrapper_depth = null; - foreach ( $tokens as $token ) { - if ( $token['is_opener'] ) { - $wrapper_depth = $token['depth']; - break; - } - } - if ( null === $wrapper_depth ) { - return null; - } - - $closer_index = null; - foreach ( $tokens as $index => $token ) { - if ( $token['is_closer'] && $token['depth'] === $wrapper_depth - 1 ) { - $closer_index = $index; - } - } - if ( null === $closer_index ) { - return null; - } - - $opener_index = $closer_index; - while ( ! $tokens[ $opener_index ]['is_opener'] || $tokens[ $opener_index ]['depth'] !== $wrapper_depth ) { - --$opener_index; - } - - $inner_container_closer_index = self::find_sole_inner_container_closer( $tokens, $opener_index, $closer_index, $wrapper_depth ); - return $inner_container_closer_index ?? $closer_index; - } - - /** - * Finds the closing tag of the legacy inner container of a Group block, when - * the container is all that the wrapper element holds, whitespace aside. - * - * @since 7.3.0 - * - * @param array[] $tokens The tokens of the block's content. - * @param int $opener_index The index of the wrapper's opening tag. - * @param int $closer_index The index of the wrapper's closing tag. - * @param int $wrapper_depth The depth of the wrapper's opening tag. - * @return int|null The index of the container's closing tag, or null if the wrapper holds anything else. - */ - private static function find_sole_inner_container_closer( array $tokens, int $opener_index, int $closer_index, int $wrapper_depth ): ?int { - $index = $opener_index + 1; - while ( $index < $closer_index && $tokens[ $index ]['is_whitespace'] ) { - ++$index; - } - if ( $index >= $closer_index || ! $tokens[ $index ]['is_inner_container'] ) { - return null; - } - - while ( $index < $closer_index && ! ( $tokens[ $index ]['is_closer'] && $tokens[ $index ]['depth'] === $wrapper_depth ) ) { - ++$index; - } - $inner_container_closer_index = $index; - for ( ++$index; $index < $closer_index; ++$index ) { - if ( ! $tokens[ $index ]['is_whitespace'] ) { - return null; - } - } - return $inner_container_closer_index < $closer_index ? $inner_container_closer_index : null; - } - /** * Depending on the block attribute name, replace its value in the HTML based on the value provided. * @@ -987,8 +516,6 @@ public function replace_rich_text( $rich_text, $inner_block_offsets = array() ) * @since 5.5.0 * @since 6.5.0 Added block bindings processing. * @since 7.1.0 Preserve inner blocks when binding a rich text attribute. - * @since 7.3.0 Stop tracking the inner blocks a block bindings source supplied once - * the block has rendered, whether or not it finished. * * @global WP_Post $post Global post object. * @@ -1038,147 +565,143 @@ public function render( $options = array() ) { ) ); - try { - // 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 ); - } + // 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 ); + } - $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); - $block_content = ''; + $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic(); + $block_content = ''; - /* - * Byte offsets in $block_content where each inner block's rendered output - * begins. Block bindings rich-text replacement uses these to stop at the - * first inner block inside a selector, so it replaces only the block's own - * rich text and never the markup produced by nested inner blocks. - * - * They are only collected when the block has bound attributes to resolve; - * otherwise they are never read, so recording them would add work to every - * block render for no benefit. - */ - $inner_block_offsets = array(); - $collect_offsets = ! empty( $computed_attributes ); + /* + * Byte offsets in $block_content where each inner block's rendered output + * begins. Block bindings rich-text replacement uses these to stop at the + * first inner block inside a selector, so it replaces only the block's own + * rich text and never the markup produced by nested inner blocks. + * + * They are only collected when the block has bound attributes to resolve; + * otherwise they are never read, so recording them would add work to every + * block render for no benefit. + */ + $inner_block_offsets = array(); + $collect_offsets = ! empty( $computed_attributes ); + + if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { + $index = 0; + + foreach ( $this->inner_content as $chunk ) { + if ( is_string( $chunk ) ) { + $block_content .= $chunk; + } else { + if ( $collect_offsets ) { + $inner_block_offsets[] = strlen( $block_content ); + } + $inner_block = $this->inner_blocks[ $index ]; + $parent_block = $this; - if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { - $index = 0; + /** This filter is documented in wp-includes/blocks.php */ + $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); - foreach ( $this->inner_content as $chunk ) { - if ( is_string( $chunk ) ) { - $block_content .= $chunk; + if ( ! is_null( $pre_render ) ) { + $block_content .= $pre_render; } else { - if ( $collect_offsets ) { - $inner_block_offsets[] = strlen( $block_content ); - } - $inner_block = $this->inner_blocks[ $index ]; - $parent_block = $this; + $source_block = $inner_block->parsed_block; + $inner_block_context = $inner_block->context; + + /** This filter is documented in wp-includes/blocks.php */ + $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); /** This filter is documented in wp-includes/blocks.php */ - $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); - - if ( ! is_null( $pre_render ) ) { - $block_content .= $pre_render; - } else { - $source_block = $inner_block->parsed_block; - $inner_block_context = $inner_block->context; - - /** This filter is documented in wp-includes/blocks.php */ - $inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block ); - - /** This filter is documented in wp-includes/blocks.php */ - $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); - - /* - * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`. - * Therefore the second condition is irrelevant if the first one is satisfied. - */ - if ( $inner_block->context !== $inner_block_context ) { - $inner_block->refresh_context_dependents(); - } elseif ( $inner_block->parsed_block !== $source_block ) { - $inner_block->refresh_parsed_block_dependents(); - } - - $block_content .= $inner_block->render(); + $inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block ); + + /* + * The `refresh_context_dependents()` method already calls `refresh_parsed_block_dependents()`. + * Therefore the second condition is irrelevant if the first one is satisfied. + */ + if ( $inner_block->context !== $inner_block_context ) { + $inner_block->refresh_context_dependents(); + } elseif ( $inner_block->parsed_block !== $source_block ) { + $inner_block->refresh_parsed_block_dependents(); } - ++$index; + $block_content .= $inner_block->render(); } + + ++$index; } } + } - if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { - foreach ( $computed_attributes as $attribute_name => $source_value ) { - $updated_block_content = $this->replace_html( $block_content, $attribute_name, $source_value, $inner_block_offsets ); - - /* - * The offsets describe $block_content as it was assembled. A - * replacement that modifies the markup shifts byte positions, so - * once the content changes the remaining attributes fall back to - * offset-free replacement rather than clamp at a stale position. - * Attributes that leave the markup untouched keep the offsets - * valid: the computed `metadata` attribute produced by a pattern - * overrides `__default` binding has no HTML source, so it must - * not invalidate the offsets for the rich text that follows it. - */ - if ( $updated_block_content !== $block_content ) { - $inner_block_offsets = array(); - } + if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) { + foreach ( $computed_attributes as $attribute_name => $source_value ) { + $updated_block_content = $this->replace_html( $block_content, $attribute_name, $source_value, $inner_block_offsets ); - $block_content = $updated_block_content; + /* + * The offsets describe $block_content as it was assembled. A + * replacement that modifies the markup shifts byte positions, so + * once the content changes the remaining attributes fall back to + * offset-free replacement rather than clamp at a stale position. + * Attributes that leave the markup untouched keep the offsets + * valid: the computed `metadata` attribute produced by a pattern + * overrides `__default` binding has no HTML source, so it must + * not invalidate the offsets for the rich text that follows it. + */ + if ( $updated_block_content !== $block_content ) { + $inner_block_offsets = array(); } + + $block_content = $updated_block_content; } + } - if ( $is_dynamic ) { - $global_post = $post; - $parent = WP_Block_Supports::$block_to_render; + if ( $is_dynamic ) { + $global_post = $post; + $parent = WP_Block_Supports::$block_to_render; - WP_Block_Supports::$block_to_render = $this->parsed_block; + WP_Block_Supports::$block_to_render = $this->parsed_block; - $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); + $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); - WP_Block_Supports::$block_to_render = $parent; + WP_Block_Supports::$block_to_render = $parent; - $post = $global_post; - } + $post = $global_post; + } - if ( ( ! empty( $this->block_type->script_handles ) ) ) { - foreach ( $this->block_type->script_handles as $script_handle ) { - wp_enqueue_script( $script_handle ); - } + if ( ( ! empty( $this->block_type->script_handles ) ) ) { + foreach ( $this->block_type->script_handles as $script_handle ) { + wp_enqueue_script( $script_handle ); } + } - if ( ! empty( $this->block_type->view_script_handles ) ) { - foreach ( $this->block_type->view_script_handles as $view_script_handle ) { - wp_enqueue_script( $view_script_handle ); - } + if ( ! empty( $this->block_type->view_script_handles ) ) { + foreach ( $this->block_type->view_script_handles as $view_script_handle ) { + wp_enqueue_script( $view_script_handle ); } + } - if ( ! empty( $this->block_type->view_script_module_ids ) ) { - foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { - wp_enqueue_script_module( $view_script_module_id ); - } + if ( ! empty( $this->block_type->view_script_module_ids ) ) { + foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) { + wp_enqueue_script_module( $view_script_module_id ); } + } - /* - * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns - * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on - * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued. - */ - if ( ( ! empty( $this->block_type->style_handles ) ) ) { - foreach ( $this->block_type->style_handles as $style_handle ) { - wp_enqueue_style( $style_handle ); - } + /* + * For Core blocks, these styles are only enqueued if `wp_should_load_separate_core_block_assets()` returns + * true. Otherwise these `wp_enqueue_style()` calls will not have any effect, as the Core blocks are relying on + * the combined 'wp-block-library' stylesheet instead, which is unconditionally enqueued. + */ + if ( ( ! empty( $this->block_type->style_handles ) ) ) { + foreach ( $this->block_type->style_handles as $style_handle ) { + wp_enqueue_style( $style_handle ); } + } - if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { - foreach ( $this->block_type->view_style_handles as $view_style_handle ) { - wp_enqueue_style( $view_style_handle ); - } + if ( ( ! empty( $this->block_type->view_style_handles ) ) ) { + foreach ( $this->block_type->view_style_handles as $view_style_handle ) { + wp_enqueue_style( $view_style_handle ); } - } finally { - $this->forget_rendered_inner_blocks(); } /** diff --git a/src/wp-settings.php b/src/wp-settings.php index 92f9456aa02ca..c2f3341291a31 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -432,6 +432,7 @@ require ABSPATH . WPINC . '/class-wp-classic-to-block-menu-converter.php'; require ABSPATH . WPINC . '/class-wp-navigation-fallback.php'; require ABSPATH . WPINC . '/block-bindings.php'; +require ABSPATH . WPINC . '/block-bindings/inner-blocks.php'; require ABSPATH . WPINC . '/block-bindings/pattern-overrides.php'; require ABSPATH . WPINC . '/block-bindings/post-data.php'; require ABSPATH . WPINC . '/block-bindings/post-meta.php'; diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index 2c1d07079358e..dab43cdbd61e5 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -9,8 +9,7 @@ * @group blocks * @group block-bindings * - * @covers WP_Block::process_block_bindings - * @covers WP_Block::replace_inner_blocks + * @covers ::_block_bindings_replace_inner_blocks */ class Tests_Block_Bindings_InnerBlocks extends WP_UnitTestCase { @@ -63,7 +62,17 @@ public function set_up() { } public function tear_down() { - unregister_block_bindings_source( self::SOURCE ); + unset( $GLOBALS['wp_block_bindings_rendering_inner_blocks'], $GLOBALS['wp_block_bindings_inner_blocks_budget_spent'] ); + foreach ( array_keys( get_all_registered_block_bindings_sources() ) as $source_name ) { + if ( str_starts_with( $source_name, 'test/' ) ) { + unregister_block_bindings_source( $source_name ); + } + } + foreach ( array_keys( WP_Block_Type_Registry::get_instance()->get_all_registered() ) as $block_name ) { + if ( str_starts_with( $block_name, 'test/' ) ) { + unregister_block_type( $block_name ); + } + } parent::tear_down(); } @@ -209,6 +218,24 @@ public function test_plain_text_value_keeps_own_inner_blocks() { $this->assertStringNotContainsString( 'Plain text', $output ); } + /** + * @ticket 66415 + * + * @dataProvider data_values_that_are_not_strings + */ + public function test_value_that_is_not_a_string_keeps_own_inner_blocks( $value ) { + $this->value = $value; + $this->assertStringContainsString( '>Default

', $this->render_group() ); + } + + public function data_values_that_are_not_strings() { + return array( + 'true' => array( true ), + 'false' => array( false ), + 'array' => array( array( '

One

' ) ), + ); + } + /** * @ticket 66415 */ @@ -223,17 +250,20 @@ public function test_value_does_not_become_an_attribute() { * @ticket 66415 */ public function test_default_pattern_overrides_binding_does_not_bind_inner_blocks() { - $requested = array(); - $record = static function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) use ( &$requested ) { - $requested[] = $attribute_name; + $markup = '

Default

'; + $context = array( 'pattern/overrides' => array( 'Free' => array( 'innerBlocks' => '

Override

' ) ) ); + $values = array(); + $capture = static function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) use ( &$values ) { + $values[ $attribute_name ] = $value; return $value; }; - add_filter( 'block_bindings_source_value', $record, 10, 5 ); - $output = $this->render_group( array( '__default' => array( 'source' => 'core/pattern-overrides' ) ) ); - remove_filter( 'block_bindings_source_value', $record ); + add_filter( 'block_bindings_source_value', $capture, 5, 5 ); + $output = ( new WP_Block( parse_blocks( $markup )[0], $context ) )->render(); + remove_filter( 'block_bindings_source_value', $capture, 5 ); - $this->assertNotContains( 'innerBlocks', $requested ); - $this->assertSame( $this->render_group( array() ), $output ); + $this->assertSame( $context['pattern/overrides']['Free']['innerBlocks'], $values['innerBlocks'] ?? null ); + $this->assertStringContainsString( '>Default

', $output ); + $this->assertStringNotContainsString( 'Override', $output ); } /** @@ -256,6 +286,17 @@ public function test_block_attributes_in_the_value_are_sanitized() { $this->assertStringContainsString( '>Safe

', $output ); } + /** + * @ticket 66415 + */ + public function test_block_attributes_with_escaped_html_in_the_value_are_sanitized() { + $this->value = '

Safe

'; + $attributes = $this->render_group_and_get_supplied_attributes(); + $this->assertCount( 1, $attributes ); + $this->assertArrayHasKey( 'className', $attributes[0] ); + $this->assertStringNotContainsString( 'onerror', $attributes[0]['className'] ); + } + /** * @ticket 66415 */ @@ -302,7 +343,6 @@ public function test_bindings_to_the_same_source_inside_the_value_are_ignored() $output = $this->render_group(); $this->assertStringContainsString( '>Nested own

', $output ); $this->assertSame( 2, substr_count( $output, '
' ) ); - $this->assertSame( 1, $this->inner_blocks_calls ); } /** @@ -320,7 +360,6 @@ public function test_sources_binding_each_other_render_once() { ); $this->value = '

From test

'; $output = $this->render_group(); - unregister_block_bindings_source( 'test/other-inner-blocks' ); $this->assertStringContainsString( '>From other

', $output ); $this->assertStringNotContainsString( 'From test', $output ); $this->assertStringNotContainsString( 'Default', $output ); @@ -351,7 +390,6 @@ public function test_bindings_with_the_same_arguments_in_another_order_inside_th ); $this->assertStringContainsString( '>Nested own

', $output ); $this->assertSame( 2, substr_count( $output, '
' ) ); - $this->assertSame( 1, $this->inner_blocks_calls ); } /** @@ -369,7 +407,6 @@ public function test_bindings_to_another_source_inside_the_value_resolve() { ); $this->value = '

Nested own

'; $output = $this->render_group(); - unregister_block_bindings_source( 'test/other-inner-blocks' ); $this->assertStringContainsString( '>From other

', $output ); $this->assertStringNotContainsString( 'Nested own', $output ); } @@ -413,28 +450,6 @@ public function test_bindings_asking_for_the_same_value_with_other_arguments_sto $this->assertStringContainsString( '>Nested own

', $output ); } - /** - * @ticket 66415 - */ - public function test_budget_starts_again_for_the_next_outermost_bound_block() { - $this->value_callback = function () { - $value = '

Supplied

'; - foreach ( range( 1, 6 ) as $variant ) { - $value .= $this->bound_group_markup( array( 'variant' => $variant ) ); - } - return $value; - }; - $budget = static function () { - return 10; - }; - add_filter( 'block_bindings_inner_blocks_budget', $budget ); - $this->render_group(); - $output = $this->render_group(); - remove_filter( 'block_bindings_inner_blocks_budget', $budget ); - - $this->assertSame( 11, substr_count( $output, '>Supplied

' ) ); - } - /** * @ticket 66415 */ @@ -447,7 +462,7 @@ public function test_value_the_bound_block_rejects_spends_no_budget() { if ( 'allowed' === $variant ) { return '

Allowed

'; } - return '

Rejected own

' + return '

Rejected own

' . $this->bound_group_markup( array( 'variant' => 'allowed' ) ); }; $budget = static function () { @@ -495,8 +510,6 @@ public function test_bindings_with_other_context_inside_the_value_resolve() { ) ); $output = do_blocks( '

Default

' ); - unregister_block_bindings_source( 'test/post-inner-blocks' ); - unregister_block_type( 'test/provides-post' ); $this->assertStringContainsString( '>Post 2

', $output ); $this->assertStringNotContainsString( 'Nested own', $output ); @@ -517,9 +530,7 @@ public function test_bindings_inside_content_loaded_by_the_value_are_ignored() { ); $this->value = ''; $output = $this->render_group(); - unregister_block_type( 'test/loads-content' ); - $this->assertSame( 1, $this->inner_blocks_calls ); $this->assertSame( 1, substr_count( $output, 'Loaded own' ) ); $this->assertStringNotContainsString( 'Default', $output ); } @@ -549,7 +560,7 @@ public function test_bindings_resolve_again_after_supplied_inner_blocks_render() /** * @ticket 66415 */ - public function test_blocks_that_stop_rendering_inside_a_host_are_forgotten_with_it() { + public function test_blocks_that_stop_rendering_inside_a_bound_block_are_forgotten_with_it() { register_block_type( 'test/throws', array( @@ -586,16 +597,17 @@ public function test_blocks_that_stop_rendering_inside_a_host_are_forgotten_with ), ) ); - unregister_block_type( 'test/throws' ); - unregister_block_type( 'test/catches' ); - $this->assertStringContainsString( '>One

', $output ); } /** * @ticket 66415 + * + * Accepted until the feature lands in Core, where WP_Block::render() can stop + * tracking the block in a `finally` block. If this starts failing because the + * block is forgotten, assert that the second render supplies its inner blocks. */ - public function test_a_block_that_stops_rendering_with_an_exception_is_forgotten() { + public function test_a_bound_block_that_stops_rendering_with_an_exception_keeps_its_own_inner_blocks_afterwards() { register_block_type( 'test/throws', array( @@ -611,11 +623,12 @@ public function test_a_block_that_stops_rendering_with_an_exception_is_forgotten } catch ( Exception $exception ) { $stopped = $exception->getMessage(); } - unregister_block_type( 'test/throws' ); $this->assertSame( 'Stopped', $stopped ); $this->value = '

One

'; - $this->assertStringContainsString( '>One

', $this->render_group() ); + $output = $this->render_group(); + $this->assertStringContainsString( '>Default

', $output ); + $this->assertStringNotContainsString( '>One

', $output ); } /** @@ -674,6 +687,16 @@ public function test_value_goes_inside_the_inner_container_of_an_empty_legacy_gr $this->assertStringContainsString( '>One

', $output ); } + /** + * @ticket 66415 + */ + public function test_value_goes_after_the_text_that_follows_the_inner_container_of_an_empty_legacy_group() { + $this->value = '

One

'; + $output = $this->render_group( null, '
Tail' ); + $this->assertStringContainsString( '
TailassertStringEndsWith( '>One

', $output ); + } + /** * @ticket 66415 */ @@ -723,6 +746,58 @@ public function test_value_is_skipped_without_markup_to_place_it_in() { $this->assertSame( '', $output ); } + /** + * @ticket 66415 + */ + public function test_value_is_skipped_without_an_element_to_place_it_in() { + $this->value = '

One

'; + $output = render_block( + array( + 'blockName' => 'core/group', + 'attrs' => array( 'metadata' => array( 'bindings' => array( 'innerBlocks' => array( 'source' => self::SOURCE ) ) ) ), + 'innerBlocks' => array(), + 'innerHTML' => 'Only text', + 'innerContent' => array( 'Only text' ), + ) + ); + $this->assertSame( 'Only text', $output ); + } + + /** + * @ticket 66415 + */ + public function test_value_is_skipped_when_the_markup_ends_inside_a_comment() { + $this->value = '

One

'; + $output = render_block( + array( + 'blockName' => 'core/group', + 'attrs' => array( 'metadata' => array( 'bindings' => array( 'innerBlocks' => array( 'source' => self::SOURCE ) ) ) ), + 'innerBlocks' => array(), + 'innerHTML' => '

One

'; + $output = do_blocks( '

Default

' ); + remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); + + $this->assertStringContainsString( '>One

', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + /** * @ticket 66415 */ @@ -736,8 +811,6 @@ public function test_supplied_blocks_the_bound_block_allows_render() { ); $this->value = "

One

\n"; $output = do_blocks( '

Default

' ); - unregister_block_type( 'test/child' ); - $this->assertStringContainsString( '>One

', $output ); $this->assertStringNotContainsString( 'Default', $output ); } @@ -797,7 +870,6 @@ public function test_supplied_block_missing_from_the_allowed_blocks_of_the_block $this->value = '

One

'; $output = do_blocks( '

Default

' ); remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); - unregister_block_type( 'test/host' ); $this->assertStringContainsString( '>Default

', $output ); $this->assertStringNotContainsString( 'One', $output ); @@ -818,8 +890,6 @@ public function test_supplied_block_whose_parent_is_another_block_keeps_own_inne ); $this->value = ''; $output = $this->render_group(); - unregister_block_type( 'test/child' ); - $this->assertStringContainsString( '>Default

', $output ); $this->assertStringNotContainsString( 'Child', $output ); } @@ -839,8 +909,6 @@ public function test_supplied_block_whose_ancestor_is_another_block_keeps_own_in ); $this->value = ''; $output = $this->render_group(); - unregister_block_type( 'test/child' ); - $this->assertStringContainsString( '>Default

', $output ); $this->assertStringNotContainsString( 'Child', $output ); } @@ -858,7 +926,7 @@ public function test_nested_supplied_blocks_are_not_checked() { /** * @ticket 66415 */ - public function test_binding_of_another_block_type_with_the_same_request_inside_the_value_resolves() { + public function test_binding_of_another_block_type_with_the_same_source_call_inside_the_value_resolves() { register_block_type( 'test/host-a' ); register_block_type( 'test/host-b' ); $add_inner_blocks = static function ( $supported_attributes, $block_type ) { @@ -873,8 +941,6 @@ public function test_binding_of_another_block_type_with_the_same_request_inside_ }; $output = do_blocks( '

Default

' ); remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); - unregister_block_type( 'test/host-a' ); - unregister_block_type( 'test/host-b' ); $this->assertStringContainsString( '>From B

', $output ); $this->assertStringNotContainsString( 'Nested own', $output ); @@ -929,7 +995,6 @@ public function test_editor_offers_inner_blocks_bindings_that_other_blocks_add() add_filter( 'block_bindings_supported_attributes', $add_inner_blocks, 10, 2 ); $settings = get_block_editor_settings( array(), new WP_Block_Editor_Context() ); remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); - unregister_block_type( 'test/host' ); $this->assertSame( array( 'innerBlocks' ), $settings['__experimentalBlockBindingsSupportedAttributes']['test/host'] ); } @@ -950,6 +1015,54 @@ public function test_embeds_in_the_value_are_rendered() { $this->assertStringContainsString( '', $output ); } + /** + * @ticket 66415 + */ + public function test_budget_starts_again_for_the_next_outermost_bound_block() { + $this->value_callback = function () { + $value = '

Supplied

'; + foreach ( range( 1, 6 ) as $variant ) { + $value .= $this->bound_group_markup( array( 'variant' => $variant ) ); + } + return $value; + }; + $budget = static function () { + return 10; + }; + add_filter( 'block_bindings_inner_blocks_budget', $budget ); + $this->render_group(); + $output = $this->render_group(); + remove_filter( 'block_bindings_inner_blocks_budget', $budget ); + + $this->assertSame( 11, substr_count( $output, '>Supplied

' ) ); + } + + /** + * @ticket 66415 + */ + public function test_binding_of_another_block_type_with_the_same_request_inside_the_value_resolves() { + register_block_type( 'test/host-a' ); + register_block_type( 'test/host-b' ); + $add_inner_blocks = static function ( $supported_attributes, $block_type ) { + return in_array( $block_type, array( 'test/host-a', 'test/host-b' ), true ) ? array( 'innerBlocks' ) : $supported_attributes; + }; + add_filter( 'block_bindings_supported_attributes', $add_inner_blocks, 10, 2 ); + $this->value_callback = static function ( $source_args, $block_instance ) { + if ( 'test/host-b' === $block_instance->name ) { + return '

From B

'; + } + return '

Nested own

'; + }; + $output = do_blocks( '

Default

' ); + remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); + unregister_block_type( 'test/host-a' ); + unregister_block_type( 'test/host-b' ); + + $this->assertStringContainsString( '>From B

', $output ); + $this->assertStringNotContainsString( 'Nested own', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + /** * @ticket 66415 * @@ -1033,6 +1146,6 @@ public function test_excerpt_ignores_bindings_to_the_same_source_inside_the_valu $excerpt = excerpt_remove_blocks( $this->bound_group_markup( array(), 'Default' ) ); $this->assertStringContainsString( '>Nested own

', $excerpt ); $this->assertStringNotContainsString( 'Default', $excerpt ); - $this->assertSame( 1, $this->inner_blocks_calls ); + $this->assertSame( 2, substr_count( $excerpt, '
' ) ); } } From 904d4139fdc735485e29f01b40447f1dc2d770f4 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sun, 29 Nov 2026 16:15:36 +0100 Subject: [PATCH 21/23] Block Bindings: Inline the inner blocks helpers used only once. The source call and the check against the source calls rendering move into _block_bindings_replace_inner_blocks(), and finding the wrapper closer moves into _block_bindings_split_at_wrapper_closer(). Same change as in Gutenberg. See #66415. Co-Authored-By: Claude Opus 5.5 --- .../block-bindings/inner-blocks.php | 155 +++++------------- 1 file changed, 38 insertions(+), 117 deletions(-) diff --git a/src/wp-includes/block-bindings/inner-blocks.php b/src/wp-includes/block-bindings/inner-blocks.php index 96fe331e8989b..1096a39136493 100644 --- a/src/wp-includes/block-bindings/inner-blocks.php +++ b/src/wp-includes/block-bindings/inner-blocks.php @@ -50,58 +50,6 @@ function _block_bindings_is_inner_blocks_value( $value ) { return false; } -/** - * Returns the block bindings source call of a bound block: what it asks its - * block bindings source for `innerBlocks`. That is the source, the arguments of - * the binding, the context the source uses, and the name and attributes of the - * block. - * - * Two bound blocks with the same block bindings source call get the same inner - * blocks from the source, whatever the order of its keys. - * - * @since 7.3.0 - * @access private - * - * @param string $source_name The name of the source. - * @param array $source_args The arguments of the binding. - * @param WP_Block $block_instance The block instance. - * @return array The block bindings source call. - */ -function _block_bindings_get_inner_blocks_source_call( $source_name, $source_args, $block_instance ) { - $source = get_block_bindings_source( $source_name ); - $context = array(); - foreach ( $source ? (array) $source->uses_context : array() as $context_name ) { - if ( array_key_exists( $context_name, $block_instance->context ) ) { - $context[ $context_name ] = $block_instance->context[ $context_name ]; - } - } - $source_call = array( $source_name, $source_args, $context, $block_instance->name, $block_instance->attributes ); - wp_recursive_ksort( $source_call ); - return $source_call; -} - -/** - * Returns whether the inner blocks a source supplied for the given block - * bindings source call are rendering. - * - * @since 7.3.0 - * @access private - * - * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. - * - * @param array $source_call The block bindings source call, as `_block_bindings_get_inner_blocks_source_call()` returns it. - * @return bool Whether supplied inner blocks for the block bindings source call are rendering. - */ -function _block_bindings_is_rendering_inner_blocks_source_call( $source_call ) { - global $wp_block_bindings_rendering_inner_blocks; - foreach ( (array) $wp_block_bindings_rendering_inner_blocks as $rendering ) { - if ( $rendering['source_call'] === $source_call ) { - return true; - } - } - return false; -} - /** * Stops tracking a block whose supplied inner blocks were rendering, once the * block itself has rendered. @@ -338,27 +286,6 @@ function _block_bindings_split_at_wrapper_closer( $html ) { return null; } - $closer_index = _block_bindings_find_wrapper_closer( $tokens ); - if ( null === $closer_index ) { - return null; - } - $before = implode( '', array_column( array_slice( $tokens, 0, $closer_index ), 'html' ) ); - $after = implode( '', array_column( array_slice( $tokens, $closer_index ), 'html' ) ); - return array( $before, $after ); -} - -/** - * Finds the closing tag of the wrapper element among the tokens of a block's - * content: the last element of the content, or the legacy inner container of - * a Group block when it is all that element holds. - * - * @since 7.3.0 - * @access private - * - * @param array[] $tokens The tokens, each with its HTML, its depth, and whether it opens an element, closes one, opens a legacy inner container, or is whitespace. - * @return int|null The index of the closing tag, or null if no element wraps the content. - */ -function _block_bindings_find_wrapper_closer( $tokens ) { $wrapper_depth = null; foreach ( $tokens as $token ) { if ( $token['is_opener'] ) { @@ -380,47 +307,28 @@ function _block_bindings_find_wrapper_closer( $tokens ) { return null; } - $opener_index = $closer_index; - while ( ! $tokens[ $opener_index ]['is_opener'] || $tokens[ $opener_index ]['depth'] !== $wrapper_depth ) { - --$opener_index; + $index = $closer_index; + while ( ! $tokens[ $index ]['is_opener'] || $tokens[ $index ]['depth'] !== $wrapper_depth ) { + --$index; } - - $inner_container_closer_index = _block_bindings_find_sole_inner_container_closer( $tokens, $opener_index, $closer_index, $wrapper_depth ); - return $inner_container_closer_index ?? $closer_index; -} - -/** - * Finds the closing tag of the legacy inner container of a Group block, when - * the container is all that the wrapper element holds, whitespace aside. - * - * @since 7.3.0 - * @access private - * - * @param array[] $tokens The tokens of the block's content. - * @param int $opener_index The index of the wrapper's opening tag. - * @param int $closer_index The index of the wrapper's closing tag. - * @param int $wrapper_depth The depth of the wrapper's opening tag. - * @return int|null The index of the container's closing tag, or null if the wrapper holds anything else. - */ -function _block_bindings_find_sole_inner_container_closer( $tokens, $opener_index, $closer_index, $wrapper_depth ) { - $index = $opener_index + 1; - while ( $index < $closer_index && $tokens[ $index ]['is_whitespace'] ) { + do { ++$index; - } - if ( $index >= $closer_index || ! $tokens[ $index ]['is_inner_container'] ) { - return null; - } + } while ( $index < $closer_index && $tokens[ $index ]['is_whitespace'] ); - while ( $index < $closer_index && ! ( $tokens[ $index ]['is_closer'] && $tokens[ $index ]['depth'] === $wrapper_depth ) ) { - ++$index; - } - $inner_container_closer_index = $index; - for ( ++$index; $index < $closer_index; ++$index ) { - if ( ! $tokens[ $index ]['is_whitespace'] ) { - return null; + $split_index = $closer_index; + if ( $index < $closer_index && $tokens[ $index ]['is_inner_container'] ) { + while ( $index < $closer_index && ! ( $tokens[ $index ]['is_closer'] && $tokens[ $index ]['depth'] === $wrapper_depth ) ) { + ++$index; + } + $after_container = array_slice( $tokens, $index + 1, max( 0, $closer_index - $index - 1 ) ); + if ( $index < $closer_index && ! in_array( false, array_column( $after_container, 'is_whitespace' ), true ) ) { + $split_index = $index; } } - return $inner_container_closer_index < $closer_index ? $inner_container_closer_index : null; + + $before = implode( '', array_column( array_slice( $tokens, 0, $split_index ), 'html' ) ); + $after = implode( '', array_column( array_slice( $tokens, $split_index ), 'html' ) ); + return array( $before, $after ); } /** @@ -433,11 +341,14 @@ function _block_bindings_find_sole_inner_container_closer( $tokens, $opener_inde * metadata replaces them, not the one the `__default` binding expands to. * * Not while the inner blocks the source supplied for the same block bindings - * source call render: the block then keeps its own inner blocks. This way - * sources whose values bind each other's, directly or through content that - * loads as it renders, like the menu of a Navigation block, can't render - * forever. Bindings that ask a source something else still resolve, inside - * supplied inner blocks too. + * source call render: the block then keeps its own inner blocks. A block + * bindings source call is the source, the arguments of the binding, the + * context the source uses, and the name and attributes of the block. Its keys + * are sorted, so two calls that differ only in the order of their keys are the + * same call. This way sources whose values bind each other's, directly or + * through content that loads as it renders, like the menu of a Navigation + * block, can't render forever. Bindings that ask a source something else + * still resolve, inside supplied inner blocks too. * * Nor when the block doesn't accept one of the top-level supplied blocks: it * then keeps its own inner blocks. @@ -474,9 +385,19 @@ function _block_bindings_replace_inner_blocks( $value, $source_name, $source_arg return null; } - $source_call = _block_bindings_get_inner_blocks_source_call( $source_name, $source_args, $block_instance ); - if ( _block_bindings_is_rendering_inner_blocks_source_call( $source_call ) ) { - return null; + $source = get_block_bindings_source( $source_name ); + $context = array(); + foreach ( $source ? (array) $source->uses_context : array() as $context_name ) { + if ( array_key_exists( $context_name, $block_instance->context ) ) { + $context[ $context_name ] = $block_instance->context[ $context_name ]; + } + } + $source_call = array( $source_name, $source_args, $context, $block_instance->name, $block_instance->attributes ); + wp_recursive_ksort( $source_call ); + foreach ( (array) $wp_block_bindings_rendering_inner_blocks as $rendering ) { + if ( $rendering['source_call'] === $source_call ) { + return null; + } } $is_nested = ! empty( $wp_block_bindings_rendering_inner_blocks ); From 94ae302732ea6e39df7083c9bba2f5aa4e49b0a0 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sun, 29 Nov 2026 16:30:25 +0100 Subject: [PATCH 22/23] Block Bindings: Make inner blocks bindings smaller. Drop the allowedBlocks, parent and ancestor check (saved post content isn't checked either), the block_bindings_inner_blocks_budget filter (the budget stays at 100), and the special case for empty legacy Groups. Same change as in Gutenberg. See #66415. Co-Authored-By: Claude Opus 5.5 --- .../block-bindings/inner-blocks.php | 297 ++++-------------- .../tests/block-bindings/innerBlocks.php | 297 ++---------------- 2 files changed, 96 insertions(+), 498 deletions(-) diff --git a/src/wp-includes/block-bindings/inner-blocks.php b/src/wp-includes/block-bindings/inner-blocks.php index 1096a39136493..43a25d63585ee 100644 --- a/src/wp-includes/block-bindings/inner-blocks.php +++ b/src/wp-includes/block-bindings/inner-blocks.php @@ -3,14 +3,11 @@ * Inner blocks bindings: a block bindings source supplies the inner blocks of a * block, through `metadata.bindings.innerBlocks`. * - * The value is serialized block markup, which replaces the inner blocks of the - * block. An empty string leaves it without inner blocks. Without a value, the - * block keeps its own inner blocks. - * - * The Group and Column blocks support it, through - * `get_block_bindings_supported_attributes()`. Any other block gets the same - * when it adds `innerBlocks` through the `block_bindings_supported_attributes` - * filter. + * Block markup replaces the fallback inner blocks, the ones saved inside the + * block. An empty string leaves the block empty. Any other value renders the + * fallback inner blocks. The Group and Column blocks support it, through + * `get_block_bindings_supported_attributes()`, and other blocks opt in through + * the `block_bindings_supported_attributes` filter. * * @since 7.3.0 * @package WordPress @@ -18,13 +15,8 @@ */ /** - * Determines whether the value of a block bindings source supplies inner - * blocks. - * - * Serialized block markup supplies them, and an empty string supplies none. - * Markup supplies them when the first block delimiter the block parser finds, - * after any whitespace, opens a block. Any other value, like the plain text of - * a field, leaves the block with its own inner blocks. + * Determines whether a source value supplies inner blocks: an empty string, or + * markup whose first block delimiter, after any whitespace, opens a block. * * @since 7.3.0 * @access private @@ -51,23 +43,17 @@ function _block_bindings_is_inner_blocks_value( $value ) { } /** - * Stops tracking a block whose supplied inner blocks were rendering, once the - * block itself has rendered. + * Stops tracking a bound block, and the ones rendering inside it, once it has + * rendered. * - * The blocks still rendering inside it, if any, stopped with it, so they are - * forgotten along with it. - * - * A block that throws while it renders never reaches this filter, so it stays - * tracked. If something catches the exception and goes on rendering, every - * bound block after it counts as nested for the rest of the page load: the - * same source call keeps its own inner blocks, and once the budget is spent, so - * does every other bound block on the page. Nothing loops or renders - * unsanitized because of it. No hook runs after a block throws. + * A block that throws never reaches this filter and stays tracked. If something + * catches the exception, later bound blocks count as nested for the rest of the + * page load. Nothing loops or renders unsanitized because of it. * * @since 7.3.0 * @access private * - * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. + * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their source calls. * * @param string $block_content The rendered block. * @param array $parsed_block The parsed block. @@ -87,13 +73,10 @@ function _block_bindings_forget_rendered_inner_blocks( $block_content, $parsed_b add_filter( 'render_block', '_block_bindings_forget_rendered_inner_blocks', 10, 3 ); /** - * Parses the inner blocks a block bindings source supplies. + * Parses the inner blocks a source supplies. * - * The markup never went through the filters that sanitize post content on - * save, and the user who supplied it may not be allowed unfiltered HTML, so by - * default it is sanitized as on save, attributes of its blocks included. Its - * embeds are expanded as the embeds of the content were, before its blocks - * render. + * The markup never went through the sanitizing on save, so it is sanitized with + * `wp_kses_post()`. Embeds expand as in post content. * * @since 7.3.0 * @access private @@ -110,18 +93,9 @@ function _block_bindings_parse_bound_inner_blocks( $markup, $source_name, $sourc global $wp_embed; /** - * Filters whether the inner blocks a block bindings source supplies are - * sanitized before they render. - * - * By default the markup is sanitized as the post content of a user without - * the `unfiltered_html` capability, since the user who supplied it is - * unknown: `wp_kses_post()` removes the HTML that user may not publish, in - * the attributes of the blocks included. A source whose markup comes from - * trusted code, not from users, may keep it as supplied. Custom CSS is - * removed from the blocks either way. - * - * Sanitizing keeps the block delimiters that `serialize_blocks()` writes, - * which escapes the characters `wp_kses_post()` rewrites inside comments. + * Filters whether the inner blocks a source supplies are sanitized with + * `wp_kses_post()`. A source whose markup comes from code, not from users, + * may return false. Custom CSS is removed either way. * * @since 7.3.0 * @@ -138,14 +112,12 @@ function _block_bindings_parse_bound_inner_blocks( $markup, $source_name, $sourc $markup = $wp_embed->autoembed( $markup ); } - return _block_bindings_prepare_bound_inner_blocks( parse_blocks( $markup ) ); + return _block_bindings_strip_custom_css( parse_blocks( $markup ) ); } /** - * Prepares the blocks a source supplies, and their inner blocks, to render. - * - * Removes their custom CSS, which only users who can edit CSS may add: - * whoever supplied the value is unknown. + * Removes the custom CSS of the supplied blocks and their inner blocks. Only + * users who can edit CSS may add it, and the author of the value is unknown. * * @since 7.3.0 * @access private @@ -153,7 +125,7 @@ function _block_bindings_parse_bound_inner_blocks( $markup, $source_name, $sourc * @param array[] $blocks The parsed blocks. * @return array[] The parsed blocks. */ -function _block_bindings_prepare_bound_inner_blocks( $blocks ) { +function _block_bindings_strip_custom_css( $blocks ) { foreach ( $blocks as &$block ) { if ( isset( $block['attrs']['style'] ) ) { $block['attrs']['style'] = wp_strip_custom_css_from_style_array( $block['attrs']['style'] ); @@ -161,71 +133,25 @@ function _block_bindings_prepare_bound_inner_blocks( $blocks ) { unset( $block['attrs']['style'] ); } } - - $block['innerBlocks'] = _block_bindings_prepare_bound_inner_blocks( $block['innerBlocks'] ); + $block['innerBlocks'] = _block_bindings_strip_custom_css( $block['innerBlocks'] ); } return $blocks; } /** - * Determines whether the bound block accepts the top-level blocks a source - * supplies. - * - * A block fails when the `allowedBlocks` of the bound block's type or - * attributes leaves it out, or when its own `parent` or `ancestor` leaves out - * the bound block. HTML outside any block counts as a Classic block - * (`core/freeform`), unless it is only whitespace. Inner blocks of the - * supplied blocks are not checked. - * - * @since 7.3.0 - * @access private - * - * @param array[] $blocks The parsed blocks. - * @param WP_Block $block_instance The bound block. - * @return bool Whether every block is allowed. - */ -function _block_bindings_are_inner_blocks_allowed( $blocks, $block_instance ) { - $allowed_lists = array( - $block_instance->block_type->allowed_blocks ?? null, - $block_instance->attributes['allowedBlocks'] ?? null, - ); - foreach ( $blocks as $block ) { - $block_name = $block['blockName'] ?? 'core/freeform'; - if ( null === $block['blockName'] && '' === trim( $block['innerHTML'] ) ) { - continue; - } - foreach ( $allowed_lists as $allowed_blocks ) { - if ( is_array( $allowed_blocks ) && ! in_array( $block_name, $allowed_blocks, true ) ) { - return false; - } - } - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); - if ( ! $block_type ) { - continue; - } - foreach ( array( $block_type->parent, $block_type->ancestor ) as $allowed_parents ) { - if ( is_array( $allowed_parents ) && ! in_array( $block_instance->name, $allowed_parents, true ) ) { - return false; - } - } - } - return true; -} - -/** - * Returns the inner content of a block with slots for the given number of - * inner blocks. + * Returns the inner content of a block with slots for the given number of inner + * blocks. * - * The slots replace the block's own inner blocks and the HTML between them. A - * block without inner blocks gets them before the closing tag of the element - * that wraps its content. + * The slots replace the fallback inner blocks and the HTML between them. A block + * without any gets them before the closing tag of its wrapper, the last + * top-level element of its markup. * * @since 7.3.0 * @access private * * @param array $inner_content The inner content of the block. * @param int $count The number of inner blocks. - * @return array|null The inner content, or null if there's no element to put + * @return array|null The inner content, or null if there's no wrapper to put * the inner blocks in. */ function _block_bindings_get_inner_content_with_slots( $inner_content, $count ) { @@ -236,135 +162,56 @@ function _block_bindings_get_inner_content_with_slots( $inner_content, $count ) $slots = array_fill( 0, $count, null ); $own_slots = array_keys( $inner_content, null, true ); if ( $own_slots ) { - $before_first_slot = array_slice( $inner_content, 0, $own_slots[0] ); - $after_last_slot = array_slice( $inner_content, end( $own_slots ) + 1 ); - return array_merge( $before_first_slot, $slots, $after_last_slot ); - } - - $wrapper_parts = _block_bindings_split_at_wrapper_closer( implode( '', $inner_content ) ); - if ( null === $wrapper_parts ) { - return null; + return array_merge( array_slice( $inner_content, 0, $own_slots[0] ), $slots, array_slice( $inner_content, end( $own_slots ) + 1 ) ); } - return array_merge( array( $wrapper_parts[0] ), $slots, array( $wrapper_parts[1] ) ); -} -/** - * Splits the HTML of the element that wraps a block's content before the - * element's closing tag. - * - * The wrapper is the last element of the HTML. When all it holds is the inner - * container of a Group block saved before WordPress 5.8, the split happens - * before the closing tag of the container, where the block's own inner blocks - * would be. - * - * @since 7.3.0 - * @access private - * - * @param string $html The HTML of the wrapper element. - * @return string[]|null The HTML before the closing tag and the HTML from it, - * or null if the HTML has no wrapper element to split. - */ -function _block_bindings_split_at_wrapper_closer( $html ) { - $processor = WP_HTML_Processor::create_fragment( $html ); + $processor = WP_HTML_Processor::create_fragment( implode( '', $inner_content ) ); if ( null === $processor ) { return null; } - - $tokens = array(); - while ( $processor->next_token() ) { - $is_opener = '#tag' === $processor->get_token_type() && ! $processor->is_tag_closer(); - $tokens[] = array( - 'html' => $processor->serialize_token(), - 'depth' => $processor->get_current_depth(), - 'is_opener' => $is_opener, - 'is_closer' => '#tag' === $processor->get_token_type() && $processor->is_tag_closer(), - 'is_inner_container' => $is_opener && $processor->has_class( 'wp-block-group__inner-container' ), - 'is_whitespace' => '#text' === $processor->get_token_type() && '' === trim( $processor->get_modifiable_text() ), - ); - } - if ( null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() ) { - return null; - } - + $tokens = array(); $wrapper_depth = null; - foreach ( $tokens as $token ) { - if ( $token['is_opener'] ) { - $wrapper_depth = $token['depth']; - break; + $closer_index = null; + while ( $processor->next_token() ) { + $tokens[] = $processor->serialize_token(); + if ( '#tag' !== $processor->get_token_type() ) { + continue; } - } - if ( null === $wrapper_depth ) { - return null; - } - - $closer_index = null; - foreach ( $tokens as $index => $token ) { - if ( $token['is_closer'] && $token['depth'] === $wrapper_depth - 1 ) { - $closer_index = $index; + if ( null === $wrapper_depth && ! $processor->is_tag_closer() ) { + $wrapper_depth = $processor->get_current_depth(); + } elseif ( $processor->is_tag_closer() && $processor->get_current_depth() === $wrapper_depth - 1 ) { + $closer_index = count( $tokens ) - 1; } } - if ( null === $closer_index ) { + if ( null === $closer_index || null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() ) { return null; } - $index = $closer_index; - while ( ! $tokens[ $index ]['is_opener'] || $tokens[ $index ]['depth'] !== $wrapper_depth ) { - --$index; - } - do { - ++$index; - } while ( $index < $closer_index && $tokens[ $index ]['is_whitespace'] ); - - $split_index = $closer_index; - if ( $index < $closer_index && $tokens[ $index ]['is_inner_container'] ) { - while ( $index < $closer_index && ! ( $tokens[ $index ]['is_closer'] && $tokens[ $index ]['depth'] === $wrapper_depth ) ) { - ++$index; - } - $after_container = array_slice( $tokens, $index + 1, max( 0, $closer_index - $index - 1 ) ); - if ( $index < $closer_index && ! in_array( false, array_column( $after_container, 'is_whitespace' ), true ) ) { - $split_index = $index; - } - } - - $before = implode( '', array_column( array_slice( $tokens, 0, $split_index ), 'html' ) ); - $after = implode( '', array_column( array_slice( $tokens, $split_index ), 'html' ) ); - return array( $before, $after ); + $before = implode( '', array_slice( $tokens, 0, $closer_index ) ); + $after = implode( '', array_slice( $tokens, $closer_index ) ); + return array_merge( array( $before ), $slots, array( $after ) ); } /** - * Replaces the inner blocks of a block with the ones its source supplies, - * before the block renders them. - * - * Runs on the value that `WP_Block::process_block_bindings()` resolves for - * `innerBlocks`, after the other filters of the value, and returns null so - * that it never becomes an attribute. Only a binding in the block's own - * metadata replaces them, not the one the `__default` binding expands to. + * Replaces the inner blocks of a bound block with the ones its source supplies. * - * Not while the inner blocks the source supplied for the same block bindings - * source call render: the block then keeps its own inner blocks. A block - * bindings source call is the source, the arguments of the binding, the - * context the source uses, and the name and attributes of the block. Its keys - * are sorted, so two calls that differ only in the order of their keys are the - * same call. This way sources whose values bind each other's, directly or - * through content that loads as it renders, like the menu of a Navigation - * block, can't render forever. Bindings that ask a source something else - * still resolve, inside supplied inner blocks too. + * Runs last on the value `WP_Block::process_block_bindings()` resolves for + * `innerBlocks`, and returns null so it never becomes an attribute. Only the + * block's own binding counts, not the one `__default` expands to. * - * Nor when the block doesn't accept one of the top-level supplied blocks: it - * then keeps its own inner blocks. - * - * Only within a budget, though: whoever writes a value writes the arguments of - * the bindings inside it, and a value whose bindings ask for it again with - * other arguments would otherwise render once for every order they can nest - * in. The budget counts every bound block that gets inner blocks inside the - * ones supplied to the outermost bound block, at any depth. Once it is spent, - * a bound block inside supplied inner blocks keeps its own. + * The block renders its fallback inner blocks when the inner blocks supplied + * for the same source call are rendering, so sources that supply each other's + * markup can't loop forever. A source call is the source, the arguments of the + * binding, the context the source uses, and the name and attributes of the + * block, with its keys sorted. Other nested bindings resolve, up to 100 bound + * blocks inside the outermost one, since a value could otherwise ask for itself + * again with other arguments in every order. * * @since 7.3.0 * @access private * - * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their block bindings source calls. - * @global int $wp_block_bindings_inner_blocks_budget_spent How many bound blocks got inner blocks from a source inside the ones supplied to the outermost bound block rendering. + * @global array[] $wp_block_bindings_rendering_inner_blocks The bound blocks whose supplied inner blocks are rendering, with their source calls. + * @global int $wp_block_bindings_inner_blocks_budget_spent How many bound blocks got inner blocks inside the outermost one. * * @param mixed $value The value of the source. * @param string $source_name The name of the source. @@ -401,31 +248,11 @@ function _block_bindings_replace_inner_blocks( $value, $source_name, $source_arg } $is_nested = ! empty( $wp_block_bindings_rendering_inner_blocks ); - if ( $is_nested ) { - /** - * Filters the inner blocks bindings budget: how many bound blocks may - * get inner blocks from a source inside the inner blocks a source - * supplied to one bound block. - * - * The budget counts bound blocks at every depth below that block, and - * siblings alike, rather than levels of nesting. It keeps a value from - * rendering without bound through bindings that ask for it again with - * other arguments or context. - * - * @since 7.3.0 - * - * @param int $budget The number of bound blocks. Default 100. - */ - $budget = (int) apply_filters( 'block_bindings_inner_blocks_budget', 100 ); - if ( (int) $wp_block_bindings_inner_blocks_budget_spent >= $budget ) { - return null; - } - } - - $blocks = _block_bindings_parse_bound_inner_blocks( $value, $source_name, $source_args, $block_instance ); - if ( ! _block_bindings_are_inner_blocks_allowed( $blocks, $block_instance ) ) { + if ( $is_nested && (int) $wp_block_bindings_inner_blocks_budget_spent >= 100 ) { return null; } + + $blocks = _block_bindings_parse_bound_inner_blocks( $value, $source_name, $source_args, $block_instance ); $inner_content = _block_bindings_get_inner_content_with_slots( $block_instance->parsed_block['innerContent'] ?? null, count( $blocks ) ); if ( null === $inner_content ) { return null; diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index dab43cdbd61e5..1efa75fe36583 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -139,50 +139,53 @@ public function test_value_replaces_inner_blocks() { $this->assertStringNotContainsString( 'Default', $output ); } - /** - * @ticket 66415 - */ - public function test_value_may_start_with_whitespace() { - $this->value = "\n\t

One

"; - $this->assertStringContainsString( '>One

', $this->render_group() ); - } - /** * @ticket 66415 * - * @dataProvider data_whitespace_after_the_delimiter_opener + * @dataProvider data_values_with_whitespace * - * @param string $whitespace The whitespace between `

One

"; + public function test_value_with_whitespace_replaces_inner_blocks( $value ) { + $this->value = $value; $output = $this->render_group(); $this->assertStringContainsString( '>One

', $output ); $this->assertStringNotContainsString( 'Default', $output ); } - public function data_whitespace_after_the_delimiter_opener() { + public function data_values_with_whitespace() { return array( - 'space' => array( ' ' ), - 'tab' => array( "\t" ), - 'newline' => array( "\n" ), + 'before the value' => array( "\n\t

One

" ), + 'space after the delimiter opener' => array( '

One

' ), + 'tab after the delimiter opener' => array( "

One

" ), + 'newline after the delimiter opener' => array( "

One

" ), ); } /** * @ticket 66415 + * + * @dataProvider data_values_without_inner_blocks + * + * @param mixed $value The value of the source. */ - public function test_whitespace_value_keeps_own_inner_blocks() { - $this->value = " \n\t"; - $this->assertStringContainsString( '>Default

', $this->render_group() ); + public function test_value_without_inner_blocks_renders_the_fallback_inner_blocks( $value ) { + $this->value = $value; + $output = $this->render_group(); + $this->assertStringContainsString( '>Default

', $output ); + $this->assertStringNotContainsString( 'One', $output ); } - /** - * @ticket 66415 - */ - public function test_value_starting_with_a_closer_keeps_own_inner_blocks() { - $this->value = '

One

'; - $this->assertStringContainsString( '>Default

', $this->render_group() ); + public function data_values_without_inner_blocks() { + return array( + 'no value' => array( null ), + 'whitespace' => array( " \n\t" ), + 'plain text' => array( 'One' ), + 'markup opening a closer' => array( '

One

' ), + 'true' => array( true ), + 'false' => array( false ), + 'array' => array( array( '

One

' ) ), + ); } /** @@ -204,38 +207,6 @@ public function test_empty_value_removes_inner_blocks_without_wrapper_markup() { $this->assertSame( '', $output ); } - public function test_without_value_keeps_own_inner_blocks() { - $this->assertStringContainsString( '>Default

', $this->render_group() ); - } - - /** - * @ticket 66415 - */ - public function test_plain_text_value_keeps_own_inner_blocks() { - $this->value = 'Plain text'; - $output = $this->render_group(); - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'Plain text', $output ); - } - - /** - * @ticket 66415 - * - * @dataProvider data_values_that_are_not_strings - */ - public function test_value_that_is_not_a_string_keeps_own_inner_blocks( $value ) { - $this->value = $value; - $this->assertStringContainsString( '>Default

', $this->render_group() ); - } - - public function data_values_that_are_not_strings() { - return array( - 'true' => array( true ), - 'false' => array( false ), - 'array' => array( array( '

One

' ) ), - ); - } - /** * @ticket 66415 */ @@ -439,45 +410,12 @@ public function test_bindings_asking_for_the_same_value_with_other_arguments_sto } return $value; }; - $budget = static function () { - return 10; - }; - add_filter( 'block_bindings_inner_blocks_budget', $budget ); - $output = $this->render_group(); - remove_filter( 'block_bindings_inner_blocks_budget', $budget ); + $output = $this->render_group(); - $this->assertSame( 11, substr_count( $output, '>Supplied

' ) ); + $this->assertSame( 101, substr_count( $output, '>Supplied

' ) ); $this->assertStringContainsString( '>Nested own

', $output ); } - /** - * @ticket 66415 - */ - public function test_value_the_bound_block_rejects_spends_no_budget() { - $this->value_callback = function ( $source_args ) { - $variant = $source_args['variant'] ?? null; - if ( 'rejected' === $variant ) { - return '

Rejected

'; - } - if ( 'allowed' === $variant ) { - return '

Allowed

'; - } - return '

Rejected own

' - . $this->bound_group_markup( array( 'variant' => 'allowed' ) ); - }; - $budget = static function () { - return 1; - }; - add_filter( 'block_bindings_inner_blocks_budget', $budget ); - $output = $this->render_group(); - remove_filter( 'block_bindings_inner_blocks_budget', $budget ); - - $this->assertStringContainsString( '>Rejected own

', $output ); - $this->assertStringNotContainsString( 'Rejected', $output ); - $this->assertStringContainsString( '>Allowed

', $output ); - $this->assertStringNotContainsString( 'Nested own', $output ); - } - /** * @ticket 66415 */ @@ -603,11 +541,9 @@ public function test_blocks_that_stop_rendering_inside_a_bound_block_are_forgott /** * @ticket 66415 * - * Accepted until the feature lands in Core, where WP_Block::render() can stop - * tracking the block in a `finally` block. If this starts failing because the - * block is forgotten, assert that the second render supplies its inner blocks. + * Accepted, since no hook runs after a block throws. */ - public function test_a_bound_block_that_stops_rendering_with_an_exception_keeps_its_own_inner_blocks_afterwards() { + public function test_a_bound_block_that_stops_rendering_with_an_exception_renders_the_fallback_inner_blocks_afterwards() { register_block_type( 'test/throws', array( @@ -678,25 +614,6 @@ public function test_value_goes_inside_the_inner_container_of_a_legacy_group() { $this->assertStringEndsWith( '>One

', $output ); } - /** - * @ticket 66415 - */ - public function test_value_goes_inside_the_inner_container_of_an_empty_legacy_group() { - $this->value = '

One

'; - $output = $this->render_group( null, '
' ); - $this->assertStringContainsString( '>One

', $output ); - } - - /** - * @ticket 66415 - */ - public function test_value_goes_after_the_text_that_follows_the_inner_container_of_an_empty_legacy_group() { - $this->value = '

One

'; - $output = $this->render_group( null, '
Tail' ); - $this->assertStringContainsString( '
TailassertStringEndsWith( '>One

', $output ); - } - /** * @ticket 66415 */ @@ -801,128 +718,13 @@ public function test_value_replaces_the_inner_blocks_of_a_block_that_adds_inner_ /** * @ticket 66415 */ - public function test_supplied_blocks_the_bound_block_allows_render() { - register_block_type( - 'test/child', - array( - 'parent' => array( 'core/group' ), - 'ancestor' => array( 'core/group' ), - ) - ); - $this->value = "

One

\n"; - $output = do_blocks( '

Default

' ); - $this->assertStringContainsString( '>One

', $output ); - $this->assertStringNotContainsString( 'Default', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_block_missing_from_the_allowed_blocks_attribute_keeps_own_inner_blocks() { - $this->value = '

One

Two

'; - $output = do_blocks( '

Default

' ); - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'One', $output ); - $this->assertStringNotContainsString( 'Two', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_html_outside_blocks_keeps_own_inner_blocks_when_the_classic_block_is_not_allowed() { - $this->value = '

One

Two

'; - $output = do_blocks( '

Default

' ); - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'One', $output ); - $this->assertStringNotContainsString( 'Two', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_html_outside_blocks_renders_when_the_classic_block_is_allowed() { - $this->value = '

One

Two

'; - $output = do_blocks( '

Default

' ); - $this->assertStringContainsString( '>One

', $output ); - $this->assertStringContainsString( '

Two

', $output ); - $this->assertStringNotContainsString( 'Default', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_html_outside_blocks_renders_when_the_bound_block_allows_any_block() { + public function test_html_outside_blocks_in_the_value_renders() { $this->value = '

One

Two

'; $output = $this->render_group(); $this->assertStringContainsString( '

Two

', $output ); $this->assertStringNotContainsString( 'Default', $output ); } - /** - * @ticket 66415 - */ - public function test_supplied_block_missing_from_the_allowed_blocks_of_the_block_type_keeps_own_inner_blocks() { - register_block_type( 'test/host', array( 'allowed_blocks' => array( 'core/paragraph' ) ) ); - $add_inner_blocks = static function ( $supported_attributes, $block_type ) { - return 'test/host' === $block_type ? array( 'innerBlocks' ) : $supported_attributes; - }; - add_filter( 'block_bindings_supported_attributes', $add_inner_blocks, 10, 2 ); - $this->value = '

One

'; - $output = do_blocks( '

Default

' ); - remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); - - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'One', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_block_whose_parent_is_another_block_keeps_own_inner_blocks() { - register_block_type( - 'test/child', - array( - 'parent' => array( 'core/column' ), - 'render_callback' => static function () { - return '

Child

'; - }, - ) - ); - $this->value = ''; - $output = $this->render_group(); - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'Child', $output ); - } - - /** - * @ticket 66415 - */ - public function test_supplied_block_whose_ancestor_is_another_block_keeps_own_inner_blocks() { - register_block_type( - 'test/child', - array( - 'ancestor' => array( 'core/columns' ), - 'render_callback' => static function () { - return '

Child

'; - }, - ) - ); - $this->value = ''; - $output = $this->render_group(); - $this->assertStringContainsString( '>Default

', $output ); - $this->assertStringNotContainsString( 'Child', $output ); - } - - /** - * @ticket 66415 - */ - public function test_nested_supplied_blocks_are_not_checked() { - $this->value = '

One

'; - $output = do_blocks( '

Default

' ); - $this->assertStringContainsString( '>One

', $output ); - $this->assertStringNotContainsString( 'Default', $output ); - } - /** * @ticket 66415 */ @@ -1026,41 +828,10 @@ public function test_budget_starts_again_for_the_next_outermost_bound_block() { } return $value; }; - $budget = static function () { - return 10; - }; - add_filter( 'block_bindings_inner_blocks_budget', $budget ); $this->render_group(); $output = $this->render_group(); - remove_filter( 'block_bindings_inner_blocks_budget', $budget ); - - $this->assertSame( 11, substr_count( $output, '>Supplied

' ) ); - } - /** - * @ticket 66415 - */ - public function test_binding_of_another_block_type_with_the_same_request_inside_the_value_resolves() { - register_block_type( 'test/host-a' ); - register_block_type( 'test/host-b' ); - $add_inner_blocks = static function ( $supported_attributes, $block_type ) { - return in_array( $block_type, array( 'test/host-a', 'test/host-b' ), true ) ? array( 'innerBlocks' ) : $supported_attributes; - }; - add_filter( 'block_bindings_supported_attributes', $add_inner_blocks, 10, 2 ); - $this->value_callback = static function ( $source_args, $block_instance ) { - if ( 'test/host-b' === $block_instance->name ) { - return '

From B

'; - } - return '

Nested own

'; - }; - $output = do_blocks( '

Default

' ); - remove_filter( 'block_bindings_supported_attributes', $add_inner_blocks ); - unregister_block_type( 'test/host-a' ); - unregister_block_type( 'test/host-b' ); - - $this->assertStringContainsString( '>From B

', $output ); - $this->assertStringNotContainsString( 'Nested own', $output ); - $this->assertStringNotContainsString( 'Default', $output ); + $this->assertSame( 101, substr_count( $output, '>Supplied

' ) ); } /** From ecc82ff13fbd997766b373c9e3ab7f07e27a6f76 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Sun, 29 Nov 2026 22:54:48 +0100 Subject: [PATCH 23/23] Block Bindings: Test inner blocks placement on markup the HTML API does not support. Also tests that excerpts leave out hidden blocks the source supplies, and removes two checks that never change the result: the null check after `WP_HTML_Processor::create_fragment()`, and the `blockVisibility` check in the excerpt filter, since `render_block()` already renders hidden blocks as nothing. See #66415. Co-Authored-By: Claude Opus 5.5 --- .../block-bindings/inner-blocks.php | 5 +-- src/wp-includes/blocks.php | 8 +---- .../tests/block-bindings/innerBlocks.php | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/block-bindings/inner-blocks.php b/src/wp-includes/block-bindings/inner-blocks.php index 43a25d63585ee..5a6f8ede57e8d 100644 --- a/src/wp-includes/block-bindings/inner-blocks.php +++ b/src/wp-includes/block-bindings/inner-blocks.php @@ -165,10 +165,7 @@ function _block_bindings_get_inner_content_with_slots( $inner_content, $count ) return array_merge( array_slice( $inner_content, 0, $own_slots[0] ), $slots, array_slice( $inner_content, end( $own_slots ) + 1 ) ); } - $processor = WP_HTML_Processor::create_fragment( implode( '', $inner_content ) ); - if ( null === $processor ) { - return null; - } + $processor = WP_HTML_Processor::create_fragment( implode( '', $inner_content ) ); $tokens = array(); $wrapper_depth = null; $closer_index = null; diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3ee75683042bd..7d8aed5cdc706 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2438,13 +2438,7 @@ function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { */ function _excerpt_render_bound_inner_blocks( $parsed_block, $allowed_blocks ) { $skip_disallowed_blocks = static function ( $pre_render, $block ) use ( $allowed_blocks ) { - if ( - false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) || - ! in_array( $block['blockName'], $allowed_blocks, true ) - ) { - return ''; - } - return $pre_render; + return in_array( $block['blockName'], $allowed_blocks, true ) ? $pre_render : ''; }; add_filter( 'pre_render_block', $skip_disallowed_blocks, 10, 2 ); diff --git a/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php index 1efa75fe36583..1806ebc5a19fa 100644 --- a/tests/phpunit/tests/block-bindings/innerBlocks.php +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -698,6 +698,25 @@ public function test_value_is_skipped_when_the_markup_ends_inside_a_comment() { $this->assertStringNotContainsString( 'One', $output ); } + /** + * @ticket 66415 + */ + public function test_value_is_skipped_when_the_markup_is_not_supported_by_the_html_api() { + $this->value = '

One

'; + $markup = '
Tail
'; + $output = render_block( + array( + 'blockName' => 'core/group', + 'attrs' => array( 'metadata' => array( 'bindings' => array( 'innerBlocks' => array( 'source' => self::SOURCE ) ) ) ), + 'innerBlocks' => array(), + 'innerHTML' => $markup, + 'innerContent' => array( $markup ), + ) + ); + $this->assertStringContainsString( '
Tail
', $output ); + $this->assertStringNotContainsString( 'One', $output ); + } + /** * @ticket 66415 */ @@ -883,6 +902,18 @@ public function test_excerpt_leaves_out_the_blocks_of_the_value_that_excerpts_do $this->assertStringNotContainsString( 'Code', $excerpt ); } + /** + * @ticket 66415 + * + * @covers ::excerpt_remove_blocks + */ + public function test_excerpt_leaves_out_the_hidden_blocks_of_the_value() { + $this->value = '

One

Hidden

'; + $excerpt = excerpt_remove_blocks( $this->bound_group_markup( array(), 'Default' ) ); + $this->assertStringContainsString( '>One

', $excerpt ); + $this->assertStringNotContainsString( 'Hidden', $excerpt ); + } + /** * @ticket 66415 *