diff --git a/lib/blocks.php b/lib/blocks.php index a8083466a04053..0b11041d0576de 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -174,6 +174,13 @@ function gutenberg_render_block( $block ) { * @return string Updated post content. */ function do_blocks( $content ) { + // If there are blocks in this content, we shouldn't run wpautop() on it later. + $priority = has_filter( 'the_content', 'wpautop' ); + if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) { + remove_filter( 'the_content', 'wpautop', $priority ); + add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 ); + } + $blocks = gutenberg_parse_blocks( $content ); $output = ''; @@ -187,6 +194,28 @@ function do_blocks( $content ) { add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed. } +if ( ! function_exists( '_restore_wpautop_hook' ) ) { + /** + * If do_blocks() needs to remove wpautop() from the `the_content` filter, + * this re-adds it afterwards, for subsequent `the_content` usage. + * + * @access private + * + * @since 4.6.0 + * + * @param string $content The post content running through this filter. + * @return string The unmodified content. + */ + function _restore_wpautop_hook( $content ) { + $current_priority = has_filter( 'the_content', '_restore_wpautop_hook' ); + + add_filter( 'the_content', 'wpautop', $current_priority - 1 ); + remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority ); + + return $content; + } +} + if ( ! function_exists( 'strip_dynamic_blocks' ) ) { /** * Remove all dynamic blocks from the given content. diff --git a/lib/compat.php b/lib/compat.php index 88b417491a89e9..03af8d9302af14 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -99,23 +99,6 @@ function gutenberg_add_rest_nonce_to_heartbeat_response_headers( $response ) { } add_filter( 'wp_refresh_nonces', 'gutenberg_add_rest_nonce_to_heartbeat_response_headers' ); -/** - * As a substitute for the default content `wpautop` filter, applies autop - * behavior only for posts where content does not contain blocks. - * - * @param string $content Post content. - * @return string Paragraph-converted text if non-block content. - */ -function gutenberg_wpautop( $content ) { - if ( has_blocks( $content ) ) { - return $content; - } - - return wpautop( $content ); -} -remove_filter( 'the_content', 'wpautop' ); -add_filter( 'the_content', 'gutenberg_wpautop', 6 ); - /** * Check if we need to load the block warning in the Classic Editor. *