Skip to content

Commit

Permalink
Add generic has_blocks() function (#8631)
Browse files Browse the repository at this point in the history
This deprecates `gutenberg_post_has_blocks()`.

See #4418, #8352.
  • Loading branch information
obenland authored and pento committed Aug 16, 2018
1 parent 5f84999 commit 8d9b4b3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 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
6 changes: 3 additions & 3 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function _gutenberg_utf8_split( $str ) {
*/
function gutenberg_disable_editor_settings_wpautop( $settings, $editor_id ) {
$post = get_post();
if ( 'content' === $editor_id && is_object( $post ) && gutenberg_post_has_blocks( $post ) ) {
if ( 'content' === $editor_id && is_object( $post ) && has_blocks( $post ) ) {
$settings['wpautop'] = false;
}

Expand Down Expand Up @@ -107,7 +107,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 All @@ -134,7 +134,7 @@ function gutenberg_check_if_classic_needs_warning_about_blocks() {
return;
}

if ( ! gutenberg_post_has_blocks( $post ) && ! isset( $_REQUEST['cloudflare-error'] ) ) {
if ( ! has_blocks( $post ) && ! isset( $_REQUEST['cloudflare-error'] ) ) {
return;
}

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 @@ -332,6 +332,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.6.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 @@ -341,13 +365,14 @@ function gutenberg_can_edit_post_type( $post_type ) {
* @see gutenberg_parse_blocks()
*
* @since 0.5.0
* @deprecated 3.6.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.6.0', 'has_blocks()' );
return has_blocks( $post );
}

/**
Expand All @@ -356,14 +381,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.6.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.6.0', 'has_blocks()' );
return has_blocks( $content );
}

/**
Expand All @@ -378,7 +406,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 @@ -389,7 +417,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
2 changes: 2 additions & 0 deletions phpunit/class-admin-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function test_gutenberg_can_edit_post() {
* Tests gutenberg_post_has_blocks().
*
* @covers ::gutenberg_post_has_blocks
* @expectedDeprecated gutenberg_post_has_blocks
*/
function test_gutenberg_post_has_blocks() {
$this->assertTrue( gutenberg_post_has_blocks( self::$post_with_blocks ) );
Expand All @@ -113,6 +114,7 @@ function test_gutenberg_post_has_blocks() {
* Tests gutenberg_content_has_blocks().
*
* @covers ::gutenberg_content_has_blocks
* @expectedDeprecated gutenberg_content_has_blocks
*/
function test_gutenberg_content_has_blocks() {
$content_with_blocks = get_post_field( 'post_content', self::$post_with_blocks );
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 8d9b4b3

Please sign in to comment.