WordPress.org

Make WordPress Core

Changeset 44226


Ignore:
Timestamp:
12/16/2018 02:22:43 AM (19 months ago)
Author:
jeremyfelt
Message:

Formatting: Ensure wpautop() isn't run on content generated from blocks.

As do_blocks() is run before wpautop() in the_content filter, we can remove in a Just In Time fashion, before that filter is run.

After wpautop()s original priority has passed, we can re-add it in a Just Too Late fashion, to ensure it's available if the_content filter is run multiple times on a page load.

Merges [43879] and [43881] from the 5.0 branch to trunk.

Props pento, nerrad.
Fixes #45290.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/blocks.php

    r44127 r44226  
    199199 */
    200200function do_blocks( $content ) {
     201    // If there are blocks in this content, we shouldn't run wpautop() on it later.
     202    $priority = has_filter( 'the_content', 'wpautop' );
     203    if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
     204        remove_filter( 'the_content', 'wpautop', $priority );
     205        add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
     206    }
     207
    201208    $blocks = parse_blocks( $content );
    202209    return _recurse_do_blocks( $blocks, $blocks );
     210}
     211
     212/**
     213 * If do_blocks() needs to remove wp_autop() from the `the_content` filter, this re-adds it afterwards,
     214 * for subsequent `the_content` usage.
     215 *
     216 * @access private
     217 *
     218 * @since 5.0.0
     219 *
     220 * @param string $content The post content running through this filter.
     221 * @return string The unmodified content.
     222 */
     223function _restore_wpautop_hook( $content ) {
     224    global $wp_filter;
     225    $current_priority = $wp_filter['the_content']->current_priority();
     226
     227    add_filter( 'the_content', 'wpautop', $current_priority - 1 );
     228    remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );
     229
     230    return $content;
    203231}
    204232
  • trunk/src/wp-includes/formatting.php

    r44118 r44226  
    457457    if ( trim( $pee ) === '' ) {
    458458        return '';
    459     }
    460 
    461     // We don't need to autop posts with blocks in them.
    462     if ( has_blocks( $pee ) ) {
    463         return $pee;
    464459    }
    465460
  • trunk/tests/phpunit/tests/blocks/render.php

    r44118 r44226  
    7676        $block_filtered_content = preg_replace( "/\n{2,}/", "\n", $block_filtered_content );
    7777
    78         $this->assertEquals( $classic_filtered_content, $block_filtered_content );
     78        $this->assertEquals( trim( $classic_filtered_content ), trim( $block_filtered_content ) );
    7979    }
    8080
    8181    function handle_shortcode( $atts, $content ) {
    8282        return $content;
     83    }
     84
     85    /**
     86     * @ticket 45290
     87     */
     88    public function test_blocks_arent_autopeed() {
     89        $expected_content = 'test';
     90        $test_content     = "<!-- wp:fake/block -->\n$expected_content\n<!-- /wp:fake/block -->";
     91
     92        $current_priority = has_action( 'the_content', 'wpautop' );
     93
     94        $filtered_content = trim( apply_filters( 'the_content', $test_content ) );
     95
     96        $this->assertEquals( $expected_content, $filtered_content );
     97
     98        // Check that wpautop() is still defined in the same place.
     99        $this->assertSame( $current_priority, has_action( 'the_content', 'wpautop' ) );
     100        // ... and that the restore function has removed itself.
     101        $this->assertFalse( has_action( 'the_content', '_restore_wpautop_hook' ) );
     102
     103        $test_content     = 'test';
     104        $expected_content = "<p>$test_content</p>";
     105
     106        $current_priority = has_action( 'the_content', 'wpautop' );
     107
     108        $filtered_content = trim( apply_filters( 'the_content', $test_content ) );
     109
     110        $this->assertEquals( $expected_content, $filtered_content );
     111
     112        $this->assertSame( $current_priority, has_action( 'the_content', 'wpautop' ) );
     113        $this->assertFalse( has_action( 'the_content', '_restore_wpautop_hook' ) );
    83114    }
    84115
Note: See TracChangeset for help on using the changeset viewer.