WordPress.org

Make WordPress Core

Ticket #45290: 45290.2.diff

File 45290.2.diff, 3.4 KB (added by pento, 21 months ago)
  • src/wp-includes/blocks.php

    diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
    index 4795631de7..b0eb6ef6ed 100644
    a b function parse_blocks( $content ) { 
    198198 * @return string Updated post content.
    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 );
    203210}
    204211
     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;
     231}
     232
    205233/**
    206234 * Helper function for do_blocks(), to recurse through the block tree.
    207235 *
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index 59b889bf25..a40959bb4b 100644
    a b function wpautop( $pee, $br = true ) { 
    442442        if ( trim($pee) === '' )
    443443                return '';
    444444
    445         // We don't need to autop posts with blocks in them.
    446         if ( has_blocks( $pee ) ) {
    447                 return $pee;
    448         }
    449 
    450445        // Just to make things a little easier, pad the end.
    451446        $pee = $pee . "\n";
    452447
  • tests/phpunit/tests/blocks/render.php

    diff --git a/tests/phpunit/tests/blocks/render.php b/tests/phpunit/tests/blocks/render.php
    index 86aedccba4..436ef22609 100644
    a b class WP_Test_Block_Render extends WP_UnitTestCase { 
    8282                return $content;
    8383        }
    8484
     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' ) );
     114        }
     115
    85116        /**
    86117         * @ticket 45109
    87118         */