Skip to content

Commit

Permalink
Merge pull request #53 from 10up/feature/detect-classic-editor-posts
Browse files Browse the repository at this point in the history
Feature/detect classic editor posts
  • Loading branch information
jeffpaul authored May 13, 2024
2 parents 377f657 + 4230cec commit ee4125b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
40 changes: 40 additions & 0 deletions includes/classes/CatalogBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ public function to_block_list( $blocks ) {
$output = [];

foreach ( $blocks as $block ) {
// change null blocks to classic editor blocks if matched
if ( $this->is_classic_editor_block( $block ) ) {
$block['blockName'] = 'core/classic';
}

// ignore empty blocks
if ( empty( $block['blockName'] ) ) {
continue;
Expand Down Expand Up @@ -517,4 +522,39 @@ public function get_variation_parent_term( $name ) {

return intval( $result->term_id );
}

/**
* Checks if block is a classic editor block
*
* @param array $block The block data
* @return boolean
*/
public function is_classic_editor_block( $block ) {
if ( empty( $block ) ) {
return false;
}

// if block has a name, it's not a classic editor block
if ( ! is_null( $block['blockName'] ) ) {
return false;
}

$allowed_tags = array_keys( wp_kses_allowed_html( 'post' ) );

$inner_html = $block['innerHTML'] ?? '';
$inner_html = trim( $inner_html );

if ( empty( $inner_html ) ) {
return false;
}

// if inner_html has any of the allowed tags, it's a classic editor block
foreach ( $allowed_tags as $tag ) {
if ( strpos( $inner_html, "<{$tag}" ) !== false ) {
return true;
}
}

return false;
}
}
86 changes: 86 additions & 0 deletions tests/classes/CatalogBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,90 @@ function test_it_can_delete_block_catalog_index() {
$this->assertEmpty( $actual );
}

function test_it_knows_core_paragraph_is_not_classic_editor() {
$block = [
'blockName' => 'core/paragraph',
];

$this->assertFalse( $this->builder->is_classic_editor_block( $block ) );
}

function test_it_knows_untitled_html_is_classic_editor() {
$block = [
'blockName' => null,
'innerHTML' => '<p>Some HTML</p>',
];

$this->assertTrue( $this->builder->is_classic_editor_block( $block ) );
}

function test_it_does_not_have_classic_editor_in_terms_if_only_gutenberg() {
$post_content = <<<HTML
<!-- wp:paragraph -->
<p>Paragraph</p>
<!-- /wp:paragraph -->
<!-- wp:image -->
<figure class="wp-block-image"><img src="https://example.com/image.jpg" alt="Image" /></figure>
<!-- /wp:image -->
HTML;

$post_id = $this->factory->post->create( [ 'post_content' => $post_content ] );
$post_terms = [
'core/paragraph' => 'Paragraph',
'core/image' => 'Image',
];

$actual = $this->builder->get_post_block_terms( $post_id );

$this->assertEquals( $post_terms, $actual['terms'] );
}

function test_it_has_classic_editor_in_terms_with_variation_1() {
$post_content = <<<HTML
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
Ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
</p>
<hr />
<!-- wp:image {"sizeSlug":"large","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<img
src="https://example.com/image.jpg"
alt=""
/>
</figure>
<!-- /wp:image -->
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
Ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
</p>
<!-- wp:image {"sizeSlug":"large","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default">
<img
src="https://example.com/image-2.jpg"
alt=""
/>
</figure>
<!-- /wp:image -->
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
Ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
</p>
HTML;
$post_id = $this->factory->post->create( [ 'post_content' => $post_content ] );
$actual = $this->builder->get_post_block_terms( $post_id );

$this->assertEquals( 'Classic', $actual['terms']['core/classic'] );
}


}

0 comments on commit ee4125b

Please sign in to comment.