Make WordPress Core

Changeset 62539


Ignore:
Timestamp:
06/22/2026 09:37:59 AM (7 weeks ago)
Author:
wildworks
Message:

Excerpt: Honor the block visibility metadata in generated excerpts.

When an excerpt is auto-generated, the metadata.blockVisibility attribute was ignored, so blocks marked as hidden still leaked their text into the excerpt even though they are not rendered on the front end.

Skip any block whose metadata.blockVisibility attribute is boolean false, for both top-level blocks and inner blocks.

Props n8finch, ramonopoly, wildworks.
Fixes #65456.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r62359 r62539  
    22392239
    22402240        foreach ( $blocks as $block ) {
     2241                // Hide the block whenever the value is boolean false, regardless of the
     2242                // block's current visibility support. This prevents blocks that previously
     2243                // supported visibility from unintentionally appearing on the front end
     2244                // after their support was disabled.
     2245                if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
     2246                        continue;
     2247                }
     2248
    22412249                if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
    22422250                        if ( ! empty( $block['innerBlocks'] ) ) {
     
    23002308
    23012309        foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
     2310                // Hide the block whenever the value is boolean false, regardless of the
     2311                // block's current visibility support. This prevents blocks that previously
     2312                // supported visibility from unintentionally appearing on the front end
     2313                // after their support was disabled.
     2314                if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
     2315                        continue;
     2316                }
     2317
    23022318                if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
    23032319                        continue;
  • trunk/tests/phpunit/tests/formatting/excerptRemoveBlocks.php

    r61605 r62539  
    130130                $this->assertEmpty( do_blocks( '<!-- wp:core/fake /-->' ) );
    131131        }
     132
     133        /**
     134         * Tests that a top-level block hidden via the visibility block support
     135         * is removed from the excerpt.
     136         *
     137         * @ticket 65456
     138         */
     139        public function test_excerpt_remove_blocks_skips_hidden_block() {
     140                $content = '<!-- wp:paragraph {"metadata":{"blockVisibility":false}} -->
     141<p>hidden</p>
     142<!-- /wp:paragraph -->
     143<!-- wp:paragraph --><p>visible</p><!-- /wp:paragraph -->';
     144
     145                $output = excerpt_remove_blocks( $content );
     146
     147                $this->assertStringNotContainsString( 'hidden', $output );
     148                $this->assertStringContainsString( 'visible', $output );
     149        }
     150
     151        /**
     152         * Tests that a hidden wrapper block (group/columns/column) is removed
     153         * from the excerpt, including its inner blocks.
     154         *
     155         * @ticket 65456
     156         *
     157         * @covers ::_excerpt_render_inner_blocks
     158         */
     159        public function test_excerpt_remove_blocks_skips_hidden_wrapper_block() {
     160                $content = '<!-- wp:group {"metadata":{"blockVisibility":false}} -->
     161<div class="wp-block-group">
     162<!-- wp:paragraph --><p>hidden inside group</p><!-- /wp:paragraph -->
     163</div>
     164<!-- /wp:group -->
     165<!-- wp:paragraph --><p>visible</p><!-- /wp:paragraph -->';
     166
     167                $output = excerpt_remove_blocks( $content );
     168
     169                $this->assertStringNotContainsString( 'hidden inside group', $output );
     170                $this->assertStringContainsString( 'visible', $output );
     171        }
     172
     173        /**
     174         * Tests that a hidden block nested inside a visible wrapper is removed.
     175         *
     176         * @ticket 65456
     177         *
     178         * @covers ::_excerpt_render_inner_blocks
     179         */
     180        public function test_excerpt_remove_blocks_skips_hidden_inner_block() {
     181                $content = '<!-- wp:group -->
     182<div class="wp-block-group">
     183<!-- wp:paragraph {"metadata":{"blockVisibility":false}} --><p>hidden inner</p><!-- /wp:paragraph -->
     184<!-- wp:paragraph --><p>visible inner</p><!-- /wp:paragraph -->
     185</div>
     186<!-- /wp:group -->';
     187
     188                $output = excerpt_remove_blocks( $content );
     189
     190                $this->assertStringNotContainsString( 'hidden inner', $output );
     191                $this->assertStringContainsString( 'visible inner', $output );
     192        }
     193
     194        /**
     195         * Tests that a block hidden only on a specific viewport is kept in the
     196         * excerpt. Viewport visibility only affects the rendered display via CSS,
     197         * so it must not strip the block's text from the excerpt.
     198         *
     199         * @ticket 65456
     200         */
     201        public function test_excerpt_remove_blocks_keeps_viewport_hidden_block() {
     202                $content = '<!-- wp:paragraph {"metadata":{"blockVisibility":{"viewport":{"desktop":false}}}} -->
     203<p>Hello World</p>
     204<!-- /wp:paragraph -->';
     205
     206                $output = excerpt_remove_blocks( $content );
     207
     208                $this->assertStringContainsString( 'Hello World', $output );
     209        }
    132210}
Note: See TracChangeset for help on using the changeset viewer.