Skip to content

Commit 756471a

Browse files
justlevineTa5r
andauthored
feat: add support for resolving PostContent blocks (#326)
Co-authored-by: Ta5r <[email protected]>
1 parent 19f6e27 commit 756471a

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

.changeset/itchy-mugs-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wpengine/wp-graphql-content-blocks": minor
3+
---
4+
5+
feat: add support for resolving PostContent blocks

includes/Data/ContentBlocksResolver.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ private static function handle_do_block( array $block ): ?array {
149149

150150
// @todo apply more hydrations.
151151
$block = self::populate_template_part_inner_blocks( $block );
152+
$block = self::populate_post_content_inner_blocks( $block );
152153
$block = self::populate_reusable_blocks( $block );
153-
154154
$block = self::populate_pattern_inner_blocks( $block );
155155

156156
// Prepare innerBlocks.
@@ -213,6 +213,35 @@ private static function populate_template_part_inner_blocks( array $block ): arr
213213
return $block;
214214
}
215215

216+
/**
217+
* Populates the innerBlocks of a core/post-content block with the blocks from the post content.
218+
*
219+
* @param array<string,mixed> $block The block to populate.
220+
*
221+
* @return array<string,mixed> The populated block.
222+
*/
223+
private static function populate_post_content_inner_blocks( array $block ): array {
224+
if ( 'core/post-content' !== $block['blockName'] ) {
225+
return $block;
226+
}
227+
228+
$post = get_post();
229+
230+
if ( ! $post ) {
231+
return $block;
232+
}
233+
234+
$parsed_blocks = ! empty( $post->post_content ) ? self::parse_blocks( $post->post_content ) : null;
235+
236+
if ( empty( $parsed_blocks ) ) {
237+
return $block;
238+
}
239+
240+
$block['innerBlocks'] = $parsed_blocks;
241+
242+
return $block;
243+
}
244+
216245
/**
217246
* Populates reusable blocks with the blocks from the reusable ref ID.
218247
*

tests/unit/ContentBlocksResolverTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WPGraphQL\ContentBlocks\Data\ContentBlocksResolver;
66
use WPGraphQL\Model\Post;
7+
use WPGraphQL\Model\Term;
78

89
final class ContentBlocksResolverTest extends PluginTestCase {
910
public $instance;
@@ -350,6 +351,82 @@ protected function assertEqualBlocks( $expected, $actual, $message = '' ) {
350351
}
351352
}
352353

354+
/**
355+
* Tests that post content inner blocks are resolved correctly.
356+
*/
357+
public function test_resolve_content_blocks_resolves_post_content_inner_blocks() {
358+
// The post content holds what will be the inner blocks.
359+
$post_content = '
360+
<!-- wp:columns -->
361+
<div class="wp-block-columns">
362+
<!-- wp:column -->
363+
<div class="wp-block-column">
364+
<!-- wp:paragraph -->
365+
<p>Column 1 Paragraph</p>
366+
<!-- /wp:paragraph -->
367+
</div>
368+
<!-- /wp:column -->
369+
<!-- wp:column -->
370+
<div class="wp-block-column">
371+
<!-- wp:heading -->
372+
<h2>Column 2 Heading</h2>
373+
<!-- /wp:heading -->
374+
</div>
375+
<!-- /wp:column -->
376+
</div>
377+
<!-- /wp:columns -->
378+
';
379+
wp_update_post(
380+
[
381+
'ID' => $this->post_id,
382+
'post_content' => $post_content,
383+
]
384+
);
385+
386+
// The term holds the PostContent block.
387+
$term_id = $this->factory()->term->create(
388+
[
389+
'taxonomy' => 'category',
390+
'name' => 'Post Content Term',
391+
'description' => '<!-- wp:post-content /-->',
392+
]
393+
);
394+
$model_with_blocks = new Term( get_term( $term_id ) );
395+
396+
add_filter( 'wpgraphql_content_blocks_resolver_content' , function( $content, $node ) {
397+
if ( $node instanceof Term ) {
398+
// The PostContent Block resolves based on the global.
399+
global $post;
400+
$post = get_post( $this->post_id );
401+
402+
return $node->description;
403+
}
404+
405+
return $content;
406+
}, 10, 2 );
407+
408+
// Resolve blocks as nested.
409+
$resolved_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => false ] );
410+
411+
// Assertions for nested blocks.
412+
$this->assertCount( 1, $resolved_blocks, 'There should be only one top-level block (post-content).' );
413+
$this->assertEquals( 'core/post-content', $resolved_blocks[0]['blockName'] );
414+
// $this->assertCount( 1, $resolved_blocks[0]['innerBlocks'], 'There should be one top-level block in post content.' );
415+
$this->assertEquals( 'core/columns', $resolved_blocks[0]['innerBlocks'][0]['blockName'] );
416+
417+
// Resolve blocks as flat
418+
$resolved_flat_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => true ] );
419+
420+
// Assertions for flat blocks
421+
$this->assertCount( 6, $resolved_flat_blocks, 'There should be five blocks when flattened.' );
422+
$this->assertEquals( 'core/post-content', $resolved_flat_blocks[0]['blockName'] );
423+
$this->assertEquals( 'core/columns', $resolved_flat_blocks[1]['blockName'] );
424+
$this->assertEquals( 'core/column', $resolved_flat_blocks[2]['blockName'] );
425+
$this->assertEquals( 'core/paragraph', $resolved_flat_blocks[3]['blockName'] );
426+
$this->assertEquals( 'core/column', $resolved_flat_blocks[4]['blockName'] );
427+
$this->assertEquals( 'core/heading', $resolved_flat_blocks[5]['blockName'] );
428+
}
429+
353430
/**
354431
* Tests that pattern inner blocks are resolved correctly.
355432
*/

0 commit comments

Comments
 (0)