From 893ec34c85e7c341aae920fcf47c08d16c19ac08 Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 10 Oct 2023 13:04:52 +0100 Subject: [PATCH 1/6] Template Parts: Fix theme slug injection for patterns --- src/wp-includes/class-wp-block-patterns-registry.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index d1ef2ad422a59..9143a5ac97b36 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -164,12 +164,16 @@ public function unregister( $pattern_name ) { */ private function prepare_content( $pattern, $hooked_blocks ) { $content = $pattern['content']; + + $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; + $after_block_visitor = null; + $hooked_blocks = get_hooked_blocks(); if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - $blocks = parse_blocks( $content ); - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern ); - $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); } + $blocks = parse_blocks( $content ); + $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); return $content; } From 9824ee2c30802b7816e493805d03c7cf3fa2fb3e Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:37:05 +0200 Subject: [PATCH 2/6] Fix failing unit tests. --- src/wp-includes/class-wp-block-patterns-registry.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 9143a5ac97b36..8f81ecf3b6f43 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -169,8 +169,8 @@ private function prepare_content( $pattern, $hooked_blocks ) { $after_block_visitor = null; $hooked_blocks = get_hooked_blocks(); if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template ); + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern ); } $blocks = parse_blocks( $content ); $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); From 3cb17f8a1191924175ab8dc12c3d8e8394db91a4 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 10 Oct 2023 14:48:28 +0200 Subject: [PATCH 3/6] Add test coverage for theme attr insertion into patterns --- .../tests/blocks/wpBlockPatternsRegistry.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php index 8eb4fce78b251..b6c85c58c2e05 100644 --- a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php +++ b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php @@ -368,6 +368,29 @@ public function test_get_all_registered_includes_hooked_blocks() { $this->assertSame( $expected, $registered ); } + /** + * Should insert a theme attribute into Template Part blocks in registered patterns. + * + * @ticket XXXXX + * + * @covers WP_Block_Patterns_Registry::register + * @covers WP_Block_Patterns_Registry::get_registered + */ + public function test_get_registered_includes_theme_attribute() { + $test_pattern = array( + 'title' => 'Test Pattern', + 'content' => '', + ); + $this->registry->register( 'test/pattern', $test_pattern ); + + $expected = sprintf( + '', + get_stylesheet() + ); + $pattern = $this->registry->get_registered( 'test/pattern' ); + $this->assertSame( $expected, $pattern['content'] ); + } + /** * Should insert hooked blocks into registered patterns. * From 3c849c0e670d5b9313446ffdc396c6604b415ecf Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:51:11 +0200 Subject: [PATCH 4/6] Remove unnecessary line. --- src/wp-includes/class-wp-block-patterns-registry.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 8f81ecf3b6f43..e516277e42dde 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -167,7 +167,6 @@ private function prepare_content( $pattern, $hooked_blocks ) { $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; $after_block_visitor = null; - $hooked_blocks = get_hooked_blocks(); if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern ); From d077949adcb90db0d787e7136caa79765a6464f6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 10 Oct 2023 14:57:43 +0200 Subject: [PATCH 5/6] Add test coverage for theme attr injection in get_all_registered --- .../tests/blocks/wpBlockPatternsRegistry.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php index b6c85c58c2e05..e6913c39f4d26 100644 --- a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php +++ b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php @@ -316,6 +316,29 @@ public function test_get_registered() { $this->assertSame( $pattern_two, $pattern ); } + /** + * Should insert a theme attribute into Template Part blocks in registered patterns. + * + * @ticket XXXXX + * + * @covers WP_Block_Patterns_Registry::register + * @covers WP_Block_Patterns_Registry::get_all_registered + */ + public function test_get_all_registered_includes_theme_attribute() { + $test_pattern = array( + 'title' => 'Test Pattern', + 'content' => '', + ); + $this->registry->register( 'test/pattern', $test_pattern ); + + $expected = sprintf( + '', + get_stylesheet() + ); + $patterns = $this->registry->get_all_registered(); + $this->assertSame( $expected, $patterns[0]['content'] ); + } + /** * Should insert hooked blocks into registered patterns. * From 15a8998e6eb6fbb4bba4d397720756597bd81980 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 10 Oct 2023 15:03:12 +0200 Subject: [PATCH 6/6] Update ticket references in tests --- tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php index e6913c39f4d26..61298d1b164af 100644 --- a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php +++ b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php @@ -319,7 +319,7 @@ public function test_get_registered() { /** * Should insert a theme attribute into Template Part blocks in registered patterns. * - * @ticket XXXXX + * @ticket 59583 * * @covers WP_Block_Patterns_Registry::register * @covers WP_Block_Patterns_Registry::get_all_registered @@ -394,7 +394,7 @@ public function test_get_all_registered_includes_hooked_blocks() { /** * Should insert a theme attribute into Template Part blocks in registered patterns. * - * @ticket XXXXX + * @ticket 59583 * * @covers WP_Block_Patterns_Registry::register * @covers WP_Block_Patterns_Registry::get_registered