Skip to content

Commit

Permalink
Avoid changing default wpautop priority (#11722)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonpayton authored and youknowriad committed Nov 29, 2018
1 parent b5d4e15 commit 2a66db0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
29 changes: 29 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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.
Expand Down
17 changes: 0 additions & 17 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 2a66db0

Please sign in to comment.