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-bindings/inner-blocks.php b/src/wp-includes/block-bindings/inner-blocks.php new file mode 100644 index 0000000000000..5a6f8ede57e8d --- /dev/null +++ b/src/wp-includes/block-bindings/inner-blocks.php @@ -0,0 +1,274 @@ +next_token() ) { + if ( $processor->is_html() && ! $processor->is_non_whitespace_html() ) { + continue; + } + return ! $processor->is_html() && $processor->opens_block(); + } + return false; +} + +/** + * Stops tracking a bound block, and the ones rendering inside it, once it has + * rendered. + * + * 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 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 source supplies. + * + * 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 + * + * @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 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 + * + * @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_strip_custom_css( parse_blocks( $markup ) ); +} + +/** + * 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 + * + * @param array[] $blocks The parsed blocks. + * @return array[] The parsed 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'] ); + if ( empty( $block['attrs']['style'] ) ) { + unset( $block['attrs']['style'] ); + } + } + $block['innerBlocks'] = _block_bindings_strip_custom_css( $block['innerBlocks'] ); + } + return $blocks; +} + +/** + * Returns the inner content of a block with slots for the given number of inner + * blocks. + * + * 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 wrapper 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 ) { + 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 ) ); + $tokens = array(); + $wrapper_depth = null; + $closer_index = null; + while ( $processor->next_token() ) { + $tokens[] = $processor->serialize_token(); + if ( '#tag' !== $processor->get_token_type() ) { + continue; + } + 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 || null !== $processor->get_last_error() || $processor->paused_at_incomplete_token() ) { + return null; + } + + $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 bound block with the ones its source supplies. + * + * 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. + * + * 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 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. + * @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 = 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 ); + 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; + } + + $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/block-editor.php b/src/wp-includes/block-editor.php index 24bdd2f6db5db..d6b5a456a1751 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -485,6 +485,8 @@ function wp_get_post_content_block_attributes() { * Returns the contextualized block editor settings for a selected editor context. * * @since 5.8.0 + * @since 7.3.0 Leaves `innerBlocks` out of the attributes that block bindings support + * on the Group and Column blocks, as the editor doesn't render its bindings yet. * * @param array $custom_settings Custom settings to use with the given editor type. * @param WP_Block_Editor_Context $block_editor_context The current block editor context. @@ -503,6 +505,9 @@ 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 ); + if ( in_array( $block_type, array( 'core/group', 'core/column' ), true ) ) { + $supported_block_attributes = array_values( array_diff( $supported_block_attributes, array( 'innerBlocks' ) ) ); + } if ( ! empty( $supported_block_attributes ) ) { $editor_settings['__experimentalBlockBindingsSupportedAttributes'][ $block_type ] = $supported_block_attributes; } diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index ba63ddc331636..7d8aed5cdc706 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -2262,6 +2262,7 @@ function filter_block_core_template_part_attributes( $attribute_value, $attribut * this function renders the blocks that are most likely to contain such text. * * @since 5.0.0 + * @since 7.3.0 Renders the inner blocks that block bindings supply. * * @param string $content The content to parse. * @return string The parsed and filtered content. @@ -2331,6 +2332,11 @@ function excerpt_remove_blocks( $content ) { } if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { + if ( isset( $block['attrs']['metadata']['bindings']['innerBlocks'] ) && in_array( 'innerBlocks', get_block_bindings_supported_attributes( $block['blockName'] ), true ) ) { + $output .= _excerpt_render_bound_inner_blocks( $block, $allowed_blocks ); + continue; + } + if ( ! empty( $block['innerBlocks'] ) ) { if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) { $output .= _excerpt_render_inner_blocks( $block, $allowed_blocks ); @@ -2381,6 +2387,7 @@ function excerpt_remove_footnotes( $content ) { * for generating an excerpt. * * @since 5.8.0 + * @since 7.3.0 Renders the inner blocks that block bindings supply. * @access private * * @param array $parsed_block The parsed block. @@ -2403,7 +2410,9 @@ function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { continue; } - if ( empty( $inner_block['innerBlocks'] ) ) { + if ( isset( $inner_block['attrs']['metadata']['bindings']['innerBlocks'] ) && in_array( 'innerBlocks', get_block_bindings_supported_attributes( $inner_block['blockName'] ), true ) ) { + $output .= _excerpt_render_bound_inner_blocks( $inner_block, $allowed_blocks ); + } elseif ( empty( $inner_block['innerBlocks'] ) ) { $output .= render_block( $inner_block ); } else { $output .= _excerpt_render_inner_blocks( $inner_block, $allowed_blocks ); @@ -2413,6 +2422,33 @@ function _excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) { return $output; } +/** + * Renders a block whose inner blocks a block bindings source supplies, for + * generating an excerpt. + * + * The block renders as on the front end, so its binding resolves with the same + * rules, and the blocks the excerpt doesn't allow render nothing. + * + * @since 7.3.0 + * @access private + * + * @param array $parsed_block The parsed block. + * @param array $allowed_blocks The list of allowed blocks. + * @return string The rendered block. + */ +function _excerpt_render_bound_inner_blocks( $parsed_block, $allowed_blocks ) { + $skip_disallowed_blocks = static function ( $pre_render, $block ) use ( $allowed_blocks ) { + return in_array( $block['blockName'], $allowed_blocks, true ) ? $pre_render : ''; + }; + + add_filter( 'pre_render_block', $skip_disallowed_blocks, 10, 2 ); + try { + return render_block( $parsed_block ); + } finally { + remove_filter( 'pre_render_block', $skip_disallowed_blocks ); + } +} + /** * Renders a single block into a HTML string. * 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 new file mode 100644 index 0000000000000..1806ebc5a19fa --- /dev/null +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -0,0 +1,953 @@ +value = null; + $this->value_callback = null; + $this->inner_blocks_calls = 0; + register_block_bindings_source( + self::SOURCE, + array( + 'label' => 'Test', + 'get_value_callback' => function ( $source_args, $block_instance, $attribute_name ) { + if ( 'innerBlocks' !== $attribute_name ) { + return 'Bound text'; + } + ++$this->inner_blocks_calls; + if ( $this->value_callback ) { + return call_user_func( $this->value_callback, $source_args, $block_instance ); + } + return $this->value; + }, + ) + ); + } + + public function tear_down() { + 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(); + } + + /** + * Renders a Group block with the given bindings and, by default, a + * `Default` paragraph as its own inner blocks. + * + * @param array|null $bindings The bindings of the Group block. + * @param string $inner_html The markup inside the Group's wrapper. + * @return string The rendered block. + */ + private function render_group( $bindings = null, $inner_html = '
Default
' ) { + $bindings = $bindings ?? array( 'innerBlocks' => array( 'source' => self::SOURCE ) ); + $markup = ''; + $markup .= '' . $own_paragraph . '
One
Two
'; + $output = $this->render_group(); + $this->assertStringContainsString( '>OneassertStringContainsString( '>Two
', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + + /** + * @ticket 66415 + * + * @dataProvider data_values_with_whitespace + * + * @param string $value The value of the source. + */ + 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_values_with_whitespace() { + return array( + 'before the value' => array( "\n\tOne
" ), + '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_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 ); + } + + 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
' ) ), + ); + } + + /** + * @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_empty_value_removes_inner_blocks_without_wrapper_markup() { + $this->value = ''; + $output = render_block( parse_blocks( '
Default
' )[0] ); + $this->assertSame( '', $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() { + $markup = 'Default
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', $capture, 5, 5 ); + $output = ( new WP_Block( parse_blocks( $markup )[0], $context ) )->render(); + remove_filter( 'block_bindings_source_value', $capture, 5 ); + + $this->assertSame( $context['pattern/overrides']['Free']['innerBlocks'], $values['innerBlocks'] ?? null ); + $this->assertStringContainsString( '>Default', $output ); + $this->assertStringNotContainsString( 'Override', $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( '