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..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..3ee75683042bd 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,39 @@ 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 ) { + if ( + false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) || + ! in_array( $block['blockName'], $allowed_blocks, true ) + ) { + return ''; + } + return $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-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 8baf0f99bacde..e9b145437f3a6 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -65,6 +65,24 @@ 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) * @@ -273,6 +291,8 @@ 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. */ @@ -310,6 +330,9 @@ 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' ); } @@ -348,7 +371,13 @@ private function process_block_bindings() { } } - $source_args = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array(); + $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_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. @@ -360,6 +389,445 @@ 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 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->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. 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 ) { + if ( null === $block['blockName'] ) { + continue; + } + foreach ( $allowed_lists as $allowed_blocks ) { + if ( is_array( $allowed_blocks ) && ! in_array( $block['blockName'], $allowed_blocks, true ) ) { + return false; + } + } + $block_type = $this->registry->get_registered( $block['blockName'] ); + 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. * @@ -516,6 +984,8 @@ 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. * @@ -565,143 +1035,147 @@ public function render( $options = array() ) { ) ); - // 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 ); - } + 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 ); + } - $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 ); - - 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; + /* + * 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 ); - /** This filter is documented in wp-includes/blocks.php */ - $pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block ); + if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { + $index = 0; - if ( ! is_null( $pre_render ) ) { - $block_content .= $pre_render; + foreach ( $this->inner_content as $chunk ) { + if ( is_string( $chunk ) ) { + $block_content .= $chunk; } 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 ); + if ( $collect_offsets ) { + $inner_block_offsets[] = strlen( $block_content ); + } + $inner_block = $this->inner_blocks[ $index ]; + $parent_block = $this; /** 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(); + $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(); } - $block_content .= $inner_block->render(); + ++$index; } - - ++$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 ); + 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(); + } - /* - * 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; } - - $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/tests/phpunit/tests/block-bindings/innerBlocks.php b/tests/phpunit/tests/block-bindings/innerBlocks.php new file mode 100644 index 0000000000000..d1248ac92f989 --- /dev/null +++ b/tests/phpunit/tests/block-bindings/innerBlocks.php @@ -0,0 +1,963 @@ +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() { + unregister_block_bindings_source( self::SOURCE ); + 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 .= '
' . $inner_html . '
'; + $markup .= ''; + $block = new WP_Block( parse_blocks( $markup )[0] ); + return $block->render(); + } + + /** + * Returns the markup of a Group block bound to the test source, with a + * paragraph of its own. + * + * @param array $args The arguments of the binding. + * @param string $own_paragraph The text of the Group's own paragraph. + * @return string The markup. + */ + private function bound_group_markup( $args = array(), $own_paragraph = 'Nested own' ) { + $binding = array( 'source' => self::SOURCE ); + if ( $args ) { + $binding['args'] = $args; + } + return '

' . $own_paragraph . '

'; + } + + /** + * 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->assertStringContainsString( '>One

assertStringContainsString( '>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 + * + * @dataProvider data_whitespace_after_the_delimiter_opener + * + * @param string $whitespace The whitespace between `

One

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

', $output ); + $this->assertStringNotContainsString( 'Default', $output ); + } + + public function data_whitespace_after_the_delimiter_opener() { + return array( + 'space' => array( ' ' ), + 'tab' => array( "\t" ), + 'newline' => array( "\n" ), + ); + } + + /** + * @ticket 66415 + */ + public function test_whitespace_value_keeps_own_inner_blocks() { + $this->value = " \n\t"; + $this->assertStringContainsString( '>Default

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

One

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

', $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_empty_value_removes_inner_blocks_without_wrapper_markup() { + $this->value = ''; + $output = render_block( parse_blocks( '

Default

' )[0] ); + $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 + */ + 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->assertSame( $this->render_group( array() ), $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 ); + } +}