From 7e5efa2bcfa02c970a9bf0955e04a34a532157e7 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Mon, 6 Aug 2018 15:27:14 -0700 Subject: [PATCH] Add generic has_blocks function 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. --- lib/blocks.php | 2 +- lib/compat.php | 2 +- lib/plugin-compat.php | 2 +- lib/register.php | 40 ++++++++++++++++++++++++----- phpunit/class-registration-test.php | 34 ++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 9 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 e91a52b7036c9..1582c77df8b45 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -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; } 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 058cdd831f262..94d815957d756 100644 --- a/lib/register.php +++ b/lib/register.php @@ -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, '