diff --git a/lib/experimental/interactivity-api/class-wp-directive-processor.php b/lib/experimental/interactivity-api/class-wp-directive-processor.php index 5f14c030fef853..4f5a3e50c76e99 100644 --- a/lib/experimental/interactivity-api/class-wp-directive-processor.php +++ b/lib/experimental/interactivity-api/class-wp-directive-processor.php @@ -166,6 +166,11 @@ public function process_rendered_html( $tags, $prefix, $directives ) { while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) { $tag_name = $tags->get_tag(); + if ( str_contains( $tag_name, 'WP-INNER-BLOCKS' ) ) { + // Should we process the inner blocks here or just shut. + return $tags; + } + // Is this a tag that closes the latest opening tag? if ( $tags->is_tag_closer() ) { if ( 0 === count( $tag_stack ) ) { diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index 8bed9a46409b38..51046b0f5ab28c 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -172,38 +172,50 @@ function gutenberg_interactivity_evaluate_reference( $path, array $context = arr * * @return string The processed HTML. */ -function gutenberg_process_interactive_block( $interactive_block ) { - $block_index = 0; - $content = ''; - $directives = array( +function gutenberg_process_interactive_block( $interactive_block, $previous_html = null ) { + $block_index = 0; + $content = ''; + $directives = array( 'data-wp-bind' => 'gutenberg_interactivity_process_wp_bind', 'data-wp-context' => 'gutenberg_interactivity_process_wp_context', 'data-wp-class' => 'gutenberg_interactivity_process_wp_class', 'data-wp-style' => 'gutenberg_interactivity_process_wp_style', 'data-wp-text' => 'gutenberg_interactivity_process_wp_text', ); - $inner_blocks = array(); + $interactive_inner_blocks = array(); foreach ( $interactive_block['innerContent'] as $inner_content ) { if ( is_string( $inner_content ) ) { $content .= $inner_content; } else { // This is an inner block. It may be an interactive block or a - // non-interactive block. - $content .= ''; - $inner_blocks[] = $interactive_block['innerBlocks'][ $block_index ]; - $block_index += 1; + // non-interactive block. Not sure if autoclosed custom tags are supported in HTML_Tag_Processor. + $content .= ''; + $interactive_inner_blocks[] = $interactive_block['innerBlocks'][ $block_index ]; + $block_index += 1; } } - $tags = new WP_Directive_Processor( $content ); - $tags->process_rendered_html( $tags, 'data-wp-', $directives ); - $content = $tags->get_updated_html(); - if ( ! empty( $inner_blocks ) ) { - $content = str_replace( '', gutenberg_process_interactive_block( $inner_blocks[0] ), $content ); - array_shift( $inner_blocks ); + // If we are processing an interactive inner block, we need to update the instance with the new HTML. + // If we create a new instance, we lose the previous context. So the next lines are wrong. + if ( isset( $previous_html ) ) { + $complete_content = str_replace( '', $content, $previous_html ); + $inner_tags = new WP_Directive_Processor( $complete_content ); + $inner_tags = $inner_tags->process_rendered_html( $inner_tags, 'data-wp-', $directives ); + return $inner_tags->get_updated_html(); + } else { + $tags = new WP_Directive_Processor( $content ); } - return $content; + // Process inner blocks. + if ( ! empty( $interactive_inner_blocks ) ) { + foreach ( $interactive_inner_blocks as $inner_block ) { + $inner_block_content = gutenberg_process_interactive_block( $inner_block, $tags->get_updated_html() ); + } + array_shift( $interactive_inner_blocks ); + return $inner_block_content; + } + $tags = $tags->process_rendered_html( $tags, 'data-wp-', $directives ); + return $tags->get_updated_html(); } /** @@ -216,7 +228,7 @@ function gutenberg_process_interactive_block( $interactive_block ) { * * @return string The processed HTML. */ -function gutenberg_process_non_interactive_block( $non_interactive_block ) { +function gutenberg_process_non_interactive_block( $non_interactive_block, $tags = null ) { $block_index = 0; $content = ''; foreach ( $non_interactive_block['innerContent'] as $inner_content ) { @@ -231,9 +243,9 @@ function gutenberg_process_non_interactive_block( $non_interactive_block ) { $block_index += 1; if ( 'core/interactivity-wrapper' === $inner_block['blockName'] ) { - $content .= gutenberg_process_interactive_block( $inner_block ); + $content .= gutenberg_process_interactive_block( $inner_block, $tags ); } elseif ( 'core/non-interactivity-wrapper' === $inner_block['blockName'] ) { - $content .= gutenberg_process_non_interactive_block( $inner_block ); + $content .= gutenberg_process_non_interactive_block( $inner_block, $tags ); } } }