From 8d9b4b380ea385b723c91889b4b4674504646073 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Wed, 15 Aug 2018 21:15:52 -0700 Subject: [PATCH] Add generic has_blocks() function (#8631) This deprecates `gutenberg_post_has_blocks()`. See #4418, #8352. --- lib/blocks.php | 2 +- lib/compat.php | 6 ++--- lib/plugin-compat.php | 2 +- lib/register.php | 40 ++++++++++++++++++++++++----- phpunit/class-admin-test.php | 2 ++ phpunit/class-registration-test.php | 34 ++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 11 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index f00a11dd93b48..e735ab0ccae78 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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(), diff --git a/lib/compat.php b/lib/compat.php index 54347c4281098..e58966eb25f39 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -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; } @@ -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; } @@ -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; } diff --git a/lib/plugin-compat.php b/lib/plugin-compat.php index 980d4b8236f79..963535624de4c 100644 --- a/lib/plugin-compat.php +++ b/lib/plugin-compat.php @@ -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; diff --git a/lib/register.php b/lib/register.php index 24f7dc4d3479d..5e8cdbd00d238 100644 --- a/lib/register.php +++ b/lib/register.php @@ -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, '