Skip to content

Commit 7a84ab5

Browse files
committed
Add guard against missing post
1 parent 5dbe308 commit 7a84ab5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/parser/block-additions/core-block.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public static function get_inner_blocks( array $inner_blocks, string $block_name
8383
// ContentParser->parse() to avoid calling telemetry and filters again.
8484
$parser = new ContentParser();
8585
$post = get_post( $parsed_block['attrs']['ref'] );
86+
87+
if ( ! $post instanceof \WP_Post ) {
88+
return [];
89+
}
90+
8691
$blocks = parse_blocks( $post->post_content );
8792

8893
return array_map( function ( array $block ) use ( $parser, $context, $post_id ): WP_Block {

tests/parser/test-synced-patterns.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,28 @@ public function test_multiple_synced_patterns() {
165165
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks not equal: %s', wp_json_encode( $blocks['blocks'] ) ) );
166166
}
167167

168+
/* Missing synced pattern */
169+
170+
public function test_missing_synced_pattern() {
171+
$html = sprintf( '<!-- wp:block {"ref":%d} /-->', -1 );
172+
173+
$expected_blocks = [
174+
[
175+
'name' => 'core/block',
176+
'attributes' => [
177+
'ref' => -1,
178+
],
179+
// inner_blocks is omitted when empty for backwards compatibility with earlier release
180+
],
181+
];
182+
183+
$content_parser = new ContentParser( $this->get_block_registry() );
184+
$blocks = $content_parser->parse( $html );
185+
186+
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
187+
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks not equal: %s', wp_json_encode( $blocks['blocks'] ) ) );
188+
}
189+
168190
/* Synced pattern with override */
169191

170192
public function test_synced_pattern_with_override() {

0 commit comments

Comments
 (0)