Skip to content

Commit

Permalink
feat: add support for resolving PostContent blocks
Browse files Browse the repository at this point in the history
Co-authored-by: Ta5r <[email protected]>
  • Loading branch information
justlevine and Ta5r committed Dec 18, 2024
1 parent ffb6c83 commit 0980c00
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-mugs-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": minor
---

feat: add support for resolving PostContent blocks
31 changes: 30 additions & 1 deletion includes/Data/ContentBlocksResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string,mixed> $block The block to populate.
*
* @return array<string,mixed> 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.
*
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/ContentBlocksResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WPGraphQL\ContentBlocks\Data\ContentBlocksResolver;
use WPGraphQL\Model\Post;
use WPGraphQL\Model\Term;

final class ContentBlocksResolverTest extends PluginTestCase {
public $instance;
Expand Down Expand Up @@ -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 = '
<!-- wp:columns -->
<div class="wp-block-columns">
<!-- wp:column -->
<div class="wp-block-column">
<!-- wp:paragraph -->
<p>Column 1 Paragraph</p>
<!-- /wp:paragraph -->
</div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column">
<!-- wp:heading -->
<h2>Column 2 Heading</h2>
<!-- /wp:heading -->
</div>
<!-- /wp:column -->
</div>
<!-- /wp:columns -->
';
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' => '<!-- wp:post-content /-->',
]
);
$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.
*/
Expand Down

0 comments on commit 0980c00

Please sign in to comment.