From c542affc11a6b18564e59e8a0985865b2060df53 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 1/9] Test that we can identify the real root blocks --- .../directive-processing.php | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index 41223c08158869..4d508e60a51a2b 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -7,6 +7,9 @@ * @subpackage Interactivity API */ +global $do_block_calls; +$do_block_calls = 0; + /** * Process directives in each block. * @@ -16,23 +19,31 @@ * @return string Filtered block content. */ function gutenberg_interactivity_process_directives_in_root_blocks( $block_content, $block ) { - // Don't process inner blocks or root blocks that don't contain directives. - if ( ! WP_Directive_Processor::is_root_block( $block ) || strpos( $block_content, 'data-wp-' ) === false ) { - return $block_content; - } + global $do_block_calls; - // TODO: Add some directive/components registration mechanism. - $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', - ); + if ( WP_Directive_Processor::is_root_block( $block ) ) { + + if ( $do_block_calls === 1 ) { + $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', + ); + + $tags = new WP_Directive_Processor( $block_content ); + $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); + + $do_block_calls -= 1; - $tags = new WP_Directive_Processor( $block_content ); - $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); - return $tags->get_updated_html(); + return $tags->get_updated_html(); + } else { + $do_block_calls -= 1; + } + } + + return $block_content; } add_filter( 'render_block', 'gutenberg_interactivity_process_directives_in_root_blocks', 10, 2 ); @@ -47,9 +58,13 @@ function gutenberg_interactivity_process_directives_in_root_blocks( $block_conte * @return array The parsed block. */ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block, $parent_block ) { + global $do_block_calls; + if ( ! isset( $parent_block ) ) { WP_Directive_Processor::add_root_block( $parsed_block ); + $do_block_calls += 1; } + return $parsed_block; } add_filter( 'render_block_data', 'gutenberg_interactivity_mark_inner_blocks', 10, 3 ); From 398d58c152ff7142c431ebf42cc2d2ef6ca2f101 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 2/9] Add some tests, fix yoda condition and linting issues --- .../directive-processing.php | 2 +- .../directive-processing-test.php | 123 ++++++++++++++++-- 2 files changed, 110 insertions(+), 15 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index 4d508e60a51a2b..c04a4fa73213dd 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -23,7 +23,7 @@ function gutenberg_interactivity_process_directives_in_root_blocks( $block_conte if ( WP_Directive_Processor::is_root_block( $block ) ) { - if ( $do_block_calls === 1 ) { + if ( 1 === $do_block_calls ) { $directives = array( 'data-wp-bind' => 'gutenberg_interactivity_process_wp_bind', 'data-wp-context' => 'gutenberg_interactivity_process_wp_context', diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index 44838b4ce68d3e..dfba23ec07ccdb 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -40,19 +40,19 @@ public function test_correctly_call_attribute_directive_processor_on_closing_tag $test_helper = $this->createMock( Helper_Class::class ); $test_helper->expects( $this->exactly( 2 ) ) - ->method( 'process_foo_test' ) - ->with( - $this->callback( - function ( $p ) { - return 'DIV' === $p->get_tag() && ( - // Either this is a closing tag... - $p->is_tag_closer() || - // ...or it is an open tag, and has the directive attribute set. - ( ! $p->is_tag_closer() && 'abc' === $p->get_attribute( 'foo-test' ) ) - ); - } - ) - ); + ->method( 'process_foo_test' ) + ->with( + $this->callback( + function ( $p ) { + return 'DIV' === $p->get_tag() && ( + // Either this is a closing tag... + $p->is_tag_closer() || + // ...or it is an open tag, and has the directive attribute set. + ( ! $p->is_tag_closer() && 'abc' === $p->get_attribute( 'foo-test' ) ) + ); + } + ) + ); $directives = array( 'foo-test' => array( $test_helper, 'process_foo_test' ), @@ -66,7 +66,7 @@ function ( $p ) { public function test_directives_with_double_hyphen_processed_correctly() { $test_helper = $this->createMock( Helper_Class::class ); $test_helper->expects( $this->atLeastOnce() ) - ->method( 'process_foo_test' ); + ->method( 'process_foo_test' ); $directives = array( 'foo-test' => array( $test_helper, 'process_foo_test' ), @@ -76,8 +76,103 @@ public function test_directives_with_double_hyphen_processed_correctly() { $tags = new WP_HTML_Tag_Processor( $markup ); gutenberg_interactivity_process_directives( $tags, 'foo-', $directives ); } + + public function test_interactivity_process_directives_in_root_blocks() { + // Reset root blocks counter. + WP_Directive_Processor::$root_blocks = array(); + $provider = $this->data_only_root_blocks_are_processed(); + foreach ( $provider as $provider ) { + $parsed_blocks = parse_blocks( $provider['page_content'] ); + foreach ( $parsed_blocks as $parsed_block ) { + render_block( $parsed_block ); + } + $this->assertSame( $provider['root_blocks'], count( WP_Directive_Processor::$root_blocks ) ); + // Reset root blocks counter. + WP_Directive_Processor::$root_blocks = array(); + } + } + + /** + * Data provider . + * + * @return array + **/ + public function data_only_root_blocks_are_processed() { + + return array( + array( + 'root_blocks' => 2, + 'page_content' => '' . + '
' . + '

The XYZ Doohickey Company was founded in 1971, and has been providing' . + 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . + 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . + '
' . + '' . + '' . + '
' . + '

The XYZ Doohickey Company was founded in 1971, and has been providing' . + 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . + 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . + '
' . + '', + ), + array( + 'root_blocks' => 1, + 'page_content' => '' . + '' . + '
' . + '
' . + '' . + '' . + '

As a new WordPress user, you should go to your dashboardto delete this page and' . + 'create new pages for your content. Have fun!

' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '
' . + '', + ), + array( + 'root_blocks' => 6, + 'page_content' => '' . + '
' . + '

Deeply Nested

' . + '
' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '' . + '' . + '
' . + '

Deeply Nested

' . + '
' . + '', + ), + ); + } } + /** * Tests for the gutenberg_interactivity_evaluate_reference function. * From dae40fbdad68bf0ca546c06be07541fedf65b087 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 3/9] Refactor to static function filter --- .../directive-processing.php | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index c04a4fa73213dd..36e5938ab1bcfa 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -7,46 +7,6 @@ * @subpackage Interactivity API */ -global $do_block_calls; -$do_block_calls = 0; - -/** - * Process directives in each block. - * - * @param string $block_content The block content. - * @param array $block The full block. - * - * @return string Filtered block content. - */ -function gutenberg_interactivity_process_directives_in_root_blocks( $block_content, $block ) { - global $do_block_calls; - - if ( WP_Directive_Processor::is_root_block( $block ) ) { - - if ( 1 === $do_block_calls ) { - $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', - ); - - $tags = new WP_Directive_Processor( $block_content ); - $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); - - $do_block_calls -= 1; - - return $tags->get_updated_html(); - } else { - $do_block_calls -= 1; - } - } - - return $block_content; -} -add_filter( 'render_block', 'gutenberg_interactivity_process_directives_in_root_blocks', 10, 2 ); - /** * Mark the inner blocks with a temporary property so we can discard them later, * and process only the root blocks. @@ -58,17 +18,56 @@ function gutenberg_interactivity_process_directives_in_root_blocks( $block_conte * @return array The parsed block. */ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block, $parent_block ) { - global $do_block_calls; + static $render_block_data = 0; + static $process_directives_in_root_blocks = null; + + if ( ! isset( $process_directives_in_root_blocks ) ) { + /** + * Process directives in each root block. + * + * @param string $block_content The block content. + * @param array $block The full block. + * + * @return string Filtered block content. + */ + $process_directives_in_root_blocks = static function ( $block_content, $block ) use ( &$render_block_data ) { + + if ( WP_Directive_Processor::is_root_block( $block ) ) { + + if ( 1 === $render_block_data ) { + $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', + ); + + $tags = new WP_Directive_Processor( $block_content ); + + $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); + $render_block_data -= 1; + return $tags->get_updated_html(); + } else { + $render_block_data -= 1; + } + } + + return $block_content; + }; + add_filter( 'render_block', $process_directives_in_root_blocks, 10, 2 ); + } if ( ! isset( $parent_block ) ) { WP_Directive_Processor::add_root_block( $parsed_block ); - $do_block_calls += 1; + $render_block_data += 1; } return $parsed_block; } add_filter( 'render_block_data', 'gutenberg_interactivity_mark_inner_blocks', 10, 3 ); + /** * Process directives. * From bfd5645c13188005c97494daf8f366ac398fc689 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 4/9] Small nitpicks --- .../directive-processing.php | 3 +-- .../directive-processing-test.php | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index 36e5938ab1bcfa..aa3c170021a57c 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -43,8 +43,7 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block 'data-wp-text' => 'gutenberg_interactivity_process_wp_text', ); - $tags = new WP_Directive_Processor( $block_content ); - + $tags = new WP_Directive_Processor( $block_content ); $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); $render_block_data -= 1; return $tags->get_updated_html(); diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index dfba23ec07ccdb..f09db8c27fafdc 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -80,12 +80,10 @@ public function test_directives_with_double_hyphen_processed_correctly() { public function test_interactivity_process_directives_in_root_blocks() { // Reset root blocks counter. WP_Directive_Processor::$root_blocks = array(); - $provider = $this->data_only_root_blocks_are_processed(); - foreach ( $provider as $provider ) { - $parsed_blocks = parse_blocks( $provider['page_content'] ); - foreach ( $parsed_blocks as $parsed_block ) { - render_block( $parsed_block ); - } + + $providers = $this->data_only_root_blocks_are_processed(); + foreach ( $providers as $provider ) { + do_blocks( $provider['page_content'] ); $this->assertSame( $provider['root_blocks'], count( WP_Directive_Processor::$root_blocks ) ); // Reset root blocks counter. WP_Directive_Processor::$root_blocks = array(); @@ -168,6 +166,20 @@ public function data_only_root_blocks_are_processed() { '' . '', ), + array( + 'root_blocks' => 3, + 'page_content' => '' . + '

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

' . + '' . + '' . + '' . + '
' . + '' . + '

Test

' . + '
' . + '', + ), ); } } From a857f7c8cdfc74717b805aabb51a650f89ab3044 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 5/9] Update test with a pattern --- .../directive-processing-test.php | 122 +++++++----------- 1 file changed, 47 insertions(+), 75 deletions(-) diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index f09db8c27fafdc..7d4fd4f8636960 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -78,11 +78,26 @@ public function test_directives_with_double_hyphen_processed_correctly() { } public function test_interactivity_process_directives_in_root_blocks() { + $pattern_content = '' . + '

Pattern Content Block 1

' . + '' . + '' . + '

Pattern Content Block 2

' . + ''; + register_block_pattern( + 'core/interactivity-pattern', + array( + 'title' => 'Interactivity Pattern', + 'content' => $pattern_content, + ) + ); + // Reset root blocks counter. WP_Directive_Processor::$root_blocks = array(); $providers = $this->data_only_root_blocks_are_processed(); foreach ( $providers as $provider ) { + do_blocks( $provider['page_content'] ); $this->assertSame( $provider['root_blocks'], count( WP_Directive_Processor::$root_blocks ) ); // Reset root blocks counter. @@ -99,85 +114,42 @@ public function data_only_root_blocks_are_processed() { return array( array( - 'root_blocks' => 2, - 'page_content' => '' . - '
' . - '

The XYZ Doohickey Company was founded in 1971, and has been providing' . - 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . - 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . - '
' . - '' . - '' . - '
' . - '

The XYZ Doohickey Company was founded in 1971, and has been providing' . - 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . - 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . - '
' . - '', + 'root_blocks' => 2, + 'is_root_block_calls' => 4, + 'page_content' => + '' . + '
+ ' . + '

The XYZ Doohickey Company was founded in 1971, and has been providing' . + 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . + 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . + ' +
' . + '' . + '' . + '
+ ' . + '

The XYZ Doohickey Company was founded in 1971, and has been providing' . + 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . + 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . + ' +
' . + '', ), array( - 'root_blocks' => 1, - 'page_content' => '' . - '' . - '
' . - '
' . - '' . - '' . - '

As a new WordPress user, you should go to your dashboardto delete this page and' . - 'create new pages for your content. Have fun!

' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '
' . - '', - ), - array( - 'root_blocks' => 6, - 'page_content' => '' . - '
' . - '

Deeply Nested

' . - '
' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '' . - '' . - '
' . - '

Deeply Nested

' . - '
' . - '', - ), - array( - 'root_blocks' => 3, - 'page_content' => '' . - '

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

' . + 'root_blocks' => 3, + 'is_root_block_calls' => 6, + 'page_content' => + '' . + '

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

' . '' . - '' . + '' . '' . - '
' . - '' . - '

Test

' . - '
' . + '
' . + '' . + '

Test

' . + '
' . '', ), ); From fbcfa47f891618c179e827c8fe6d0bba726c4452 Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 6/9] Refactor and use test for counting root blocks --- .../directive-processing.php | 37 +++++++++---------- .../directive-processing-test.php | 24 ++++++------ 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index aa3c170021a57c..316ebbff300876 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -18,7 +18,7 @@ * @return array The parsed block. */ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block, $parent_block ) { - static $render_block_data = 0; + static $is_inside_root_block = false; static $process_directives_in_root_blocks = null; if ( ! isset( $process_directives_in_root_blocks ) ) { @@ -30,26 +30,23 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block * * @return string Filtered block content. */ - $process_directives_in_root_blocks = static function ( $block_content, $block ) use ( &$render_block_data ) { + $process_directives_in_root_blocks = static function ( $block_content, $block ) use ( &$is_inside_root_block ) { if ( WP_Directive_Processor::is_root_block( $block ) ) { - if ( 1 === $render_block_data ) { - $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', - ); - - $tags = new WP_Directive_Processor( $block_content ); - $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); - $render_block_data -= 1; - return $tags->get_updated_html(); - } else { - $render_block_data -= 1; - } + $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', + ); + + $tags = new WP_Directive_Processor( $block_content ); + $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); + $is_inside_root_block = false; + return $tags->get_updated_html(); + } return $block_content; @@ -57,9 +54,9 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block add_filter( 'render_block', $process_directives_in_root_blocks, 10, 2 ); } - if ( ! isset( $parent_block ) ) { + if ( ! isset( $parent_block ) && ! $is_inside_root_block ) { WP_Directive_Processor::add_root_block( $parsed_block ); - $render_block_data += 1; + $is_inside_root_block = true; } return $parsed_block; diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index 7d4fd4f8636960..5e082071fb21b8 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -78,11 +78,12 @@ public function test_directives_with_double_hyphen_processed_correctly() { } public function test_interactivity_process_directives_in_root_blocks() { - $pattern_content = '' . - '

Pattern Content Block 1

' . + $pattern_content = + '' . + '

Pattern Content Block 1

' . '' . '' . - '

Pattern Content Block 2

' . + '

Pattern Content Block 2

' . ''; register_block_pattern( 'core/interactivity-pattern', @@ -97,7 +98,6 @@ public function test_interactivity_process_directives_in_root_blocks() { $providers = $this->data_only_root_blocks_are_processed(); foreach ( $providers as $provider ) { - do_blocks( $provider['page_content'] ); $this->assertSame( $provider['root_blocks'], count( WP_Directive_Processor::$root_blocks ) ); // Reset root blocks counter. @@ -114,9 +114,8 @@ public function data_only_root_blocks_are_processed() { return array( array( - 'root_blocks' => 2, - 'is_root_block_calls' => 4, - 'page_content' => + 'root_blocks' => 2, + 'page_content' => '' . '
' . @@ -129,17 +128,16 @@ public function data_only_root_blocks_are_processed() { '' . '
' . - '

The XYZ Doohickey Company was founded in 1971, and has been providing' . - 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . - 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . + '

The XYZ Doohickey Company was founded in 1971, and has been providing' . + 'quality doohickeys to the public ever since. Located in Gotham City, XYZ employs' . + 'over 2,000 people and does all kinds of awesome things for the Gotham community.

' . '
' . '', ), array( - 'root_blocks' => 3, - 'is_root_block_calls' => 6, - 'page_content' => + 'root_blocks' => 3, + 'page_content' => '' . '

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

' . '' . From 51d20e01d13d1ae002a57d422efbccbcfbe9f31e Mon Sep 17 00:00:00 2001 From: Carlos Bravo Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 7/9] Simplify tests --- .../directive-processing-test.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index 5e082071fb21b8..49bb183f4b8074 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -93,15 +93,11 @@ public function test_interactivity_process_directives_in_root_blocks() { ) ); - // Reset root blocks counter. - WP_Directive_Processor::$root_blocks = array(); - $providers = $this->data_only_root_blocks_are_processed(); foreach ( $providers as $provider ) { do_blocks( $provider['page_content'] ); $this->assertSame( $provider['root_blocks'], count( WP_Directive_Processor::$root_blocks ) ); - // Reset root blocks counter. - WP_Directive_Processor::$root_blocks = array(); + } } @@ -136,19 +132,12 @@ public function data_only_root_blocks_are_processed() { '', ), array( - 'root_blocks' => 3, + 'root_blocks' => 2, 'page_content' => '' . '

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

' . '' . - '' . - '' . - '
' . - '' . - '

Test

' . - '
' . - '', + '', ), ); } From 448c3913d299741f7bb39f21118d8445bf46b9a5 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Wed, 8 Nov 2023 10:17:29 +0100 Subject: [PATCH 8/9] Fix e2e tests --- .../interactivity-api/directive-processing.php | 16 +++++++++------- .../e2e-tests/plugins/interactive-blocks.php | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/experimental/interactivity-api/directive-processing.php b/lib/experimental/interactivity-api/directive-processing.php index 316ebbff300876..eae731e2438913 100644 --- a/lib/experimental/interactivity-api/directive-processing.php +++ b/lib/experimental/interactivity-api/directive-processing.php @@ -8,8 +8,9 @@ */ /** - * Mark the inner blocks with a temporary property so we can discard them later, - * and process only the root blocks. + * Process the Interactivity API directives using the root blocks of the + * outermost rendering, ignoring the root blocks of inner blocks like Patterns, + * Template Parts or Content. * * @param array $parsed_block The parsed block. * @param array $source_block The source block. @@ -17,7 +18,7 @@ * * @return array The parsed block. */ -function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block, $parent_block ) { +function gutenberg_interactivity_process_directives( $parsed_block, $source_block, $parent_block ) { static $is_inside_root_block = false; static $process_directives_in_root_blocks = null; @@ -43,7 +44,7 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block ); $tags = new WP_Directive_Processor( $block_content ); - $tags = gutenberg_interactivity_process_directives( $tags, 'data-wp-', $directives ); + $tags = gutenberg_interactivity_process_rendered_html( $tags, 'data-wp-', $directives ); $is_inside_root_block = false; return $tags->get_updated_html(); @@ -61,11 +62,12 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block return $parsed_block; } -add_filter( 'render_block_data', 'gutenberg_interactivity_mark_inner_blocks', 10, 3 ); +add_filter( 'render_block_data', 'gutenberg_interactivity_process_directives', 10, 3 ); /** - * Process directives. + * Traverses the HTML searching for Interactivity API directives and processing + * them. * * @param WP_Directive_Processor $tags An instance of the WP_Directive_Processor. * @param string $prefix Attribute prefix. @@ -74,7 +76,7 @@ function gutenberg_interactivity_mark_inner_blocks( $parsed_block, $source_block * @return WP_Directive_Processor The modified instance of the * WP_Directive_Processor. */ -function gutenberg_interactivity_process_directives( $tags, $prefix, $directives ) { +function gutenberg_interactivity_process_rendered_html( $tags, $prefix, $directives ) { $context = new WP_Directive_Context(); $tag_stack = array(); diff --git a/packages/e2e-tests/plugins/interactive-blocks.php b/packages/e2e-tests/plugins/interactive-blocks.php index a6bd468493840d..956508a11361e4 100644 --- a/packages/e2e-tests/plugins/interactive-blocks.php +++ b/packages/e2e-tests/plugins/interactive-blocks.php @@ -39,8 +39,8 @@ function () { // HTML is not correct or malformed. if ( 'true' === $_GET['disable_directives_ssr'] ) { remove_filter( - 'render_block', - 'gutenberg_interactivity_process_directives_in_root_blocks' + 'render_block_data', + 'gutenberg_interactivity_process_directives' ); } } From 72686e2a5acff117e6a12927d0002299881c6d8f Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Wed, 8 Nov 2023 10:49:11 +0100 Subject: [PATCH 9/9] Fix unit tests --- .../interactivity-api/directive-processing-test.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpunit/experimental/interactivity-api/directive-processing-test.php b/phpunit/experimental/interactivity-api/directive-processing-test.php index 49bb183f4b8074..97dddba3c263f7 100644 --- a/phpunit/experimental/interactivity-api/directive-processing-test.php +++ b/phpunit/experimental/interactivity-api/directive-processing-test.php @@ -28,10 +28,10 @@ function gutenberg_test_process_directives_helper_increment( $store ) { } /** - * Tests for the gutenberg_interactivity_process_directives function. + * Tests for the gutenberg_interactivity_process_rendered_html function. * * @group interactivity-api - * @covers gutenberg_interactivity_process_directives + * @covers gutenberg_interactivity_process_rendered_html */ class Tests_Process_Directives extends WP_UnitTestCase { public function test_correctly_call_attribute_directive_processor_on_closing_tag() { @@ -60,7 +60,7 @@ function ( $p ) { $markup = '
Example:
This is a test>
Here is a nested div
'; $tags = new WP_HTML_Tag_Processor( $markup ); - gutenberg_interactivity_process_directives( $tags, 'foo-', $directives ); + gutenberg_interactivity_process_rendered_html( $tags, 'foo-', $directives ); } public function test_directives_with_double_hyphen_processed_correctly() { @@ -74,7 +74,7 @@ public function test_directives_with_double_hyphen_processed_correctly() { $markup = '
'; $tags = new WP_HTML_Tag_Processor( $markup ); - gutenberg_interactivity_process_directives( $tags, 'foo-', $directives ); + gutenberg_interactivity_process_rendered_html( $tags, 'foo-', $directives ); } public function test_interactivity_process_directives_in_root_blocks() {