From 756471a1e85c6129adee78d545cba087925d2160 Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Fri, 3 Jan 2025 17:40:28 +0200 Subject: [PATCH] feat: add support for resolving PostContent blocks (#326) Co-authored-by: Ta5r --- .changeset/itchy-mugs-sniff.md | 5 ++ includes/Data/ContentBlocksResolver.php | 31 +++++++++- tests/unit/ContentBlocksResolverTest.php | 77 ++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 .changeset/itchy-mugs-sniff.md diff --git a/.changeset/itchy-mugs-sniff.md b/.changeset/itchy-mugs-sniff.md new file mode 100644 index 00000000..4a457962 --- /dev/null +++ b/.changeset/itchy-mugs-sniff.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": minor +--- + +feat: add support for resolving PostContent blocks diff --git a/includes/Data/ContentBlocksResolver.php b/includes/Data/ContentBlocksResolver.php index dcf6c43c..e4c589ad 100644 --- a/includes/Data/ContentBlocksResolver.php +++ b/includes/Data/ContentBlocksResolver.php @@ -149,8 +149,8 @@ private static function handle_do_block( array $block ): ?array { // @todo apply more hydrations. $block = self::populate_template_part_inner_blocks( $block ); + $block = self::populate_post_content_inner_blocks( $block ); $block = self::populate_reusable_blocks( $block ); - $block = self::populate_pattern_inner_blocks( $block ); // Prepare innerBlocks. @@ -213,6 +213,35 @@ private static function populate_template_part_inner_blocks( array $block ): arr return $block; } + /** + * Populates the innerBlocks of a core/post-content block with the blocks from the post content. + * + * @param array $block The block to populate. + * + * @return array The populated block. + */ + private static function populate_post_content_inner_blocks( array $block ): array { + if ( 'core/post-content' !== $block['blockName'] ) { + return $block; + } + + $post = get_post(); + + if ( ! $post ) { + return $block; + } + + $parsed_blocks = ! empty( $post->post_content ) ? self::parse_blocks( $post->post_content ) : null; + + if ( empty( $parsed_blocks ) ) { + return $block; + } + + $block['innerBlocks'] = $parsed_blocks; + + return $block; + } + /** * Populates reusable blocks with the blocks from the reusable ref ID. * diff --git a/tests/unit/ContentBlocksResolverTest.php b/tests/unit/ContentBlocksResolverTest.php index dea2faae..0bf575c4 100644 --- a/tests/unit/ContentBlocksResolverTest.php +++ b/tests/unit/ContentBlocksResolverTest.php @@ -4,6 +4,7 @@ use WPGraphQL\ContentBlocks\Data\ContentBlocksResolver; use WPGraphQL\Model\Post; +use WPGraphQL\Model\Term; final class ContentBlocksResolverTest extends PluginTestCase { public $instance; @@ -350,6 +351,82 @@ protected function assertEqualBlocks( $expected, $actual, $message = '' ) { } } + /** + * Tests that post content inner blocks are resolved correctly. + */ + public function test_resolve_content_blocks_resolves_post_content_inner_blocks() { + // The post content holds what will be the inner blocks. + $post_content = ' + +
+ +
+ +

Column 1 Paragraph

+ +
+ + +
+ +

Column 2 Heading

+ +
+ +
+ + '; + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $post_content, + ] + ); + + // The term holds the PostContent block. + $term_id = $this->factory()->term->create( + [ + 'taxonomy' => 'category', + 'name' => 'Post Content Term', + 'description' => '', + ] + ); + $model_with_blocks = new Term( get_term( $term_id ) ); + + add_filter( 'wpgraphql_content_blocks_resolver_content' , function( $content, $node ) { + if ( $node instanceof Term ) { + // The PostContent Block resolves based on the global. + global $post; + $post = get_post( $this->post_id ); + + return $node->description; + } + + return $content; + }, 10, 2 ); + + // Resolve blocks as nested. + $resolved_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => false ] ); + + // Assertions for nested blocks. + $this->assertCount( 1, $resolved_blocks, 'There should be only one top-level block (post-content).' ); + $this->assertEquals( 'core/post-content', $resolved_blocks[0]['blockName'] ); + // $this->assertCount( 1, $resolved_blocks[0]['innerBlocks'], 'There should be one top-level block in post content.' ); + $this->assertEquals( 'core/columns', $resolved_blocks[0]['innerBlocks'][0]['blockName'] ); + + // Resolve blocks as flat + $resolved_flat_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => true ] ); + + // Assertions for flat blocks + $this->assertCount( 6, $resolved_flat_blocks, 'There should be five blocks when flattened.' ); + $this->assertEquals( 'core/post-content', $resolved_flat_blocks[0]['blockName'] ); + $this->assertEquals( 'core/columns', $resolved_flat_blocks[1]['blockName'] ); + $this->assertEquals( 'core/column', $resolved_flat_blocks[2]['blockName'] ); + $this->assertEquals( 'core/paragraph', $resolved_flat_blocks[3]['blockName'] ); + $this->assertEquals( 'core/column', $resolved_flat_blocks[4]['blockName'] ); + $this->assertEquals( 'core/heading', $resolved_flat_blocks[5]['blockName'] ); + } + /** * Tests that pattern inner blocks are resolved correctly. */