Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/parser/content-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ public function parse( $post_content, $post_id = null, $filter_options = [] ) {

$this->warnings = [];

$has_blocks = has_blocks( $post_content );
// An empty `post_content` fails `has_blocks` check, but is still valid.
$content_is_empty = empty( trim( $post_content ) );

if ( ! $has_blocks ) {
$has_content = ! empty( trim( $post_content ) );

if ( $has_content && ! has_blocks( $post_content ) ) {
$error_message = join(' ', [
sprintf( 'Error parsing post ID %d: This post does not appear to contain block content.', $post_id ),
'The VIP Block Data API is designed to parse Gutenberg blocks and can not read classic editor content.',
Expand Down
34 changes: 34 additions & 0 deletions tests/parser/test-content-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WPCOMVIP\BlockDataApi;

use WP_Error;

/**
* Content parser tests that are not source-specific.
*/
Expand Down Expand Up @@ -215,4 +217,36 @@ public function test_parse_whitespace_block_removal() {
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks do not match: %s', wp_json_encode( $blocks ) ) );
}

/* Classic editor content */

public function test_parse_classic_editor_content() {
$html = 'Hello, world!';

$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertInstanceOf( WP_Error::class, $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );

$errors = $blocks->get_error_messages();
$this->assertEquals( 1, count( $errors ), 'Expected error messages but found none.' );
$this->assertStringContainsString(
'This post does not appear to contain block content.',
$errors[0],
sprintf( 'Unexpected error messages: %s', wp_json_encode( $errors ) )
);
}

/* Empty content */

public function test_parse_empty_content() {
$html = '';
$expected_blocks = [];

$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertEquals( $expected_blocks, $blocks['blocks'], sprintf( 'Blocks do not match: %s', wp_json_encode( $blocks ) ) );
}
}