Skip to content

Commit 4c548c3

Browse files
justlevineTa5r
andauthored
feat: resolve block patterns (#323)
Co-authored-by: Ta5r <[email protected]>
1 parent cab682e commit 4c548c3

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

.changeset/tiny-news-warn.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 Block Patterns

includes/Data/ContentBlocksResolver.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ private static function handle_do_block( array $block ): ?array {
151151

152152
$block = self::populate_reusable_blocks( $block );
153153

154+
$block = self::populate_pattern_inner_blocks( $block );
155+
154156
// Prepare innerBlocks.
155157
if ( ! empty( $block['innerBlocks'] ) ) {
156158
$block['innerBlocks'] = self::handle_do_blocks( $block['innerBlocks'] );
@@ -208,6 +210,32 @@ private static function populate_reusable_blocks( array $block ): array {
208210
return array_merge( ...$parsed_blocks );
209211
}
210212

213+
/**
214+
* Populates the pattern innerBlocks with the blocks from the pattern.
215+
*
216+
* @param array<string,mixed> $block The block to populate.
217+
* @return array<string,mixed> The populated block.
218+
*/
219+
private static function populate_pattern_inner_blocks( array $block ): array {
220+
// Bail if not WP 6.6 or later.
221+
if ( ! function_exists( 'resolve_pattern_blocks' ) ) {
222+
return $block;
223+
}
224+
225+
if ( 'core/pattern' !== $block['blockName'] || ! isset( $block['attrs']['slug'] ) ) {
226+
return $block;
227+
}
228+
229+
$resolved_patterns = resolve_pattern_blocks( [ $block ] );
230+
231+
if ( empty( $resolved_patterns ) ) {
232+
return $block;
233+
}
234+
235+
$block['innerBlocks'] = $resolved_patterns;
236+
return $block;
237+
}
238+
211239
/**
212240
* Flattens a list blocks into a single array
213241
*

tests/unit/ContentBlocksResolverTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,68 @@ protected function assertEqualBlocks( $expected, $actual, $message = '' ) {
349349
$this->assertEqualBlocks( $expected_inner_block, $actual_inner_blocks[ $index ], $message );
350350
}
351351
}
352+
353+
/**
354+
* Tests that pattern inner blocks are resolved correctly.
355+
*/
356+
public function test_resolve_content_blocks_resolves_pattern_inner_blocks() {
357+
// Skip if pattern resolution functionality is not supported.
358+
if ( ! function_exists( 'resolve_pattern_blocks' ) ) {
359+
$this->markTestSkipped( 'Pattern block resolution not supported in this WordPress version.' );
360+
}
361+
362+
// Create a pattern
363+
$pattern_name = 'test/pattern-blocks';
364+
$pattern_content = '
365+
<!-- wp:paragraph -->
366+
<p>Pattern Paragraph</p>
367+
<!-- /wp:paragraph -->
368+
<!-- wp:heading -->
369+
<h2>Pattern Heading</h2>
370+
<!-- /wp:heading -->
371+
';
372+
373+
// Register the pattern.
374+
register_block_pattern(
375+
$pattern_name,
376+
[
377+
'title' => 'Test Pattern',
378+
'content' => $pattern_content,
379+
]
380+
);
381+
382+
// Update post content to include pattern block.
383+
$post_content = '
384+
<!-- wp:pattern {"slug":"test/pattern-blocks"} /-->
385+
';
386+
387+
wp_update_post(
388+
[
389+
'ID' => $this->post_id,
390+
'post_content' => $post_content,
391+
]
392+
);
393+
394+
$post_model = new Post( get_post( $this->post_id ) );
395+
396+
// Resolve blocks as nested.
397+
$resolved_blocks = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => false ] );
398+
399+
$this->assertCount( 1, $resolved_blocks, 'There should be only one top-level block (pattern).' );
400+
$this->assertEquals( 'core/pattern', $resolved_blocks[0]['blockName'] );
401+
$this->assertCount( 2, $resolved_blocks[0]['innerBlocks'], 'There should be two inner blocks in the pattern.' );
402+
$this->assertEquals( 'core/paragraph', $resolved_blocks[0]['innerBlocks'][0]['blockName'] );
403+
$this->assertEquals( 'core/heading', $resolved_blocks[0]['innerBlocks'][1]['blockName'] );
404+
405+
// Resolve blocks as flat.
406+
$resolved_flat_blocks = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ] );
407+
408+
$this->assertCount( 3, $resolved_flat_blocks, 'There should be three blocks when flattened.' );
409+
$this->assertEquals( 'core/pattern', $resolved_flat_blocks[0]['blockName'] );
410+
$this->assertEquals( 'core/paragraph', $resolved_flat_blocks[1]['blockName'] );
411+
$this->assertEquals( 'core/heading', $resolved_flat_blocks[2]['blockName'] );
412+
413+
// Cleanup: Unregistering the pattern.
414+
unregister_block_pattern( $pattern_name );
415+
}
352416
}

0 commit comments

Comments
 (0)