Skip to content

Commit

Permalink
Add generic has_blocks function
Browse files Browse the repository at this point in the history
Merges `gutenberg_post_has_blocks()` and `gutenberg_content_has_blocks()`, resulting in a template function that can be used context-independently.
It accepts a content string, a post object, or post ID, and default to the current post if none of those are given. Make this part of the API behave close to existing WordPress template functions.

It's my first Gutenberg PR, so I'd love to get feedback on this, especially around a better phpunit setup potentially?

See #4418.
  • Loading branch information
obenland committed Aug 6, 2018
1 parent 4fd5c20 commit 7e5efa2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function gutenberg_parse_blocks( $content ) {
* If there are no blocks in the content, return a single block, rather
* than wasting time trying to parse the string.
*/
if ( ! gutenberg_content_has_blocks( $content ) ) {
if ( ! has_blocks( $content ) ) {
return array(
array(
'attrs' => array(),
Expand Down
2 changes: 1 addition & 1 deletion lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function gutenberg_add_rest_nonce_to_heartbeat_response_headers( $response ) {
* @return string Paragraph-converted text if non-block content.
*/
function gutenberg_wpautop( $content ) {
if ( gutenberg_content_has_blocks( $content ) ) {
if ( has_blocks( $content ) ) {
return $content;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/plugin-compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @return array $post Post object.
*/
function gutenberg_remove_wpcom_markdown_support( $post ) {
if ( class_exists( 'WPCom_Markdown' ) && gutenberg_content_has_blocks( $post['post_content'] ) ) {
if ( class_exists( 'WPCom_Markdown' ) && has_blocks( $post['post_content'] ) ) {
WPCom_Markdown::get_instance()->unload_markdown_for_posts();
}
return $post;
Expand Down
40 changes: 34 additions & 6 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,30 @@ function gutenberg_can_edit_post_type( $post_type ) {
return apply_filters( 'gutenberg_can_edit_post_type', $can_edit, $post_type );
}

/**
* Determine whether a post or content string has blocks.
*
* This test optimizes for performance rather than strict accuracy, detecting
* the pattern of a block but not validating its structure. For strict accuracy
* you should use the block parser on post content.
*
* @since 3.4.0
* @see gutenberg_parse_blocks()
*
* @param int|string|WP_Post|null $post Optional. Post content, post ID, or post object. Defaults to global $post.
* @return bool Whether the post has blocks.
*/
function has_blocks( $post = null ) {
if ( ! is_string( $post ) ) {
$wp_post = get_post( $post );
if ( $wp_post instanceof WP_Post ) {
$post = $wp_post->post_content;
}
}

return false !== strpos( (string) $post, '<!-- wp:' );
}

/**
* Determine whether a post has blocks. This test optimizes for performance
* rather than strict accuracy, detecting the pattern of a block but not
Expand All @@ -327,13 +351,14 @@ function gutenberg_can_edit_post_type( $post_type ) {
* @see gutenberg_parse_blocks()
*
* @since 0.5.0
* @deprecated 3.4.0 Use has_block()
*
* @param object $post Post.
* @return bool Whether the post has blocks.
*/
function gutenberg_post_has_blocks( $post ) {
$post = get_post( $post );
return $post && gutenberg_content_has_blocks( $post->post_content );
_deprecated_function( __FUNCTION__, '3.4.0', 'has_blocks()' );
return has_blocks( $post );
}

/**
Expand All @@ -342,14 +367,17 @@ function gutenberg_post_has_blocks( $post ) {
* but not validating its structure. For strict accuracy, you should use the
* block parser on post content.
*
* @since 1.6.0
* @see gutenberg_parse_blocks()
*
* @since 1.6.0
* @deprecated 3.4.0 Use has_block()
*
* @param string $content Content to test.
* @return bool Whether the content contains blocks.
*/
function gutenberg_content_has_blocks( $content ) {
return false !== strpos( $content, '<!-- wp:' );
_deprecated_function( __FUNCTION__, '3.4.0', 'has_blocks()' );
return has_blocks( $content );
}

/**
Expand All @@ -364,7 +392,7 @@ function gutenberg_content_has_blocks( $content ) {
* @return int The block format version.
*/
function gutenberg_content_block_version( $content ) {
return gutenberg_content_has_blocks( $content ) ? 1 : 0;
return has_blocks( $content ) ? 1 : 0;
}

/**
Expand All @@ -375,7 +403,7 @@ function gutenberg_content_block_version( $content ) {
* @return array A filtered array of post display states.
*/
function gutenberg_add_gutenberg_post_state( $post_states, $post ) {
if ( gutenberg_post_has_blocks( $post ) ) {
if ( has_blocks( $post ) ) {
$post_states[] = 'Gutenberg';
}

Expand Down
34 changes: 34 additions & 0 deletions phpunit/class-registration-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
*/
class Registration_Test extends WP_UnitTestCase {

protected static $post_id;

public static function wpSetUpBeforeClass( $factory ) {
self::$post_id = $factory->post->create( array(
'post_content' => file_get_contents( dirname( __FILE__ ) . '/fixtures/do-blocks-original.html' ),
) );
}

public static function wpTearDownAfterClass() {
// Also deletes revisions.
wp_delete_post( self::$post_id, true );
}

function render_stub() {}

function tearDown() {
Expand Down Expand Up @@ -60,4 +73,25 @@ function test_get_dynamic_block_names() {
$this->assertContains( 'core/dynamic', $dynamic_block_names );
$this->assertNotContains( 'core/dummy', $dynamic_block_names );
}

function test_has_blocks() {
// Test with passing post ID.
$this->assertTrue( has_blocks( self::$post_id ) );

// Test with passing WP_Post object.
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );

// Test with passing content string.
$this->assertTrue( has_blocks( get_post( self::$post_id ) ) );

// Test default.
$this->assertFalse( has_blocks() );
$query = new WP_Query( array( 'post__in' => array( self::$post_id ) ) );
$query->the_post();
$this->assertTrue( has_blocks() );

// Test string (without blocks).
$content = file_get_contents( dirname( __FILE__ ) . '/fixtures/do-blocks-expected.html' );
$this->assertFalse( has_blocks( $content ) );
}
}

0 comments on commit 7e5efa2

Please sign in to comment.