Excerpt: Honor the visibility block support when generating excerpts.#12167
Excerpt: Honor the visibility block support when generating excerpts.#12167t-hamano wants to merge 5 commits into
Conversation
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Blocks hidden via the `blockVisibility` metadata are stripped from rendered content because `do_blocks()` runs them through the `render_block` filter, where `wp_render_block_visibility_support()` returns an empty string. Excerpt generation bypasses that filter: `wp_trim_excerpt()` unhooks `do_blocks` and instead walks the parsed blocks via `excerpt_remove_blocks()`. Wrapper blocks (group, columns, column) are handed to `_excerpt_render_inner_blocks()`, which renders their inner blocks directly and never evaluates the wrapper's own visibility, so a hidden wrapper's content leaked into the excerpt. Skip blocks whose `blockVisibility` metadata is `false` in both `excerpt_remove_blocks()` and `_excerpt_render_inner_blocks()` so the excerpt matches the rendered content. Co-Authored-By: Claude <noreply@anthropic.com>
210abfb to
c0ae630
Compare
Viewport visibility (e.g. `blockVisibility.viewport.desktop = false`) only toggles the rendered display via CSS and does not fully hide the block. The excerpt strip logic intentionally matches only the strict `blockVisibility === false` case, so viewport-hidden blocks must remain in the excerpt. Add a test to lock in that behavior. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates WordPress’s excerpt generation path to respect the block visibility support when assembling excerpts from parsed blocks, so content hidden via metadata.blockVisibility does not leak into generated excerpts (especially for wrapper blocks where excerpts are built from innerBlocks).
Changes:
- Skip blocks (top-level and nested) marked hidden via visibility support while building excerpt output.
- Add PHPUnit coverage for hidden top-level blocks, hidden wrapper blocks (group/columns/column), hidden inner blocks, and viewport-only hidden configuration.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/wp-includes/blocks.php |
Skips hidden blocks during excerpt rendering for both top-level blocks and recursive innerBlocks traversal. |
tests/phpunit/tests/formatting/excerptRemoveBlocks.php |
Adds tests asserting hidden blocks are removed from excerpts, including wrapper/nested scenarios and viewport-only visibility. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Skip blocks hidden via the visibility block support. | ||
| if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
I guess this is an edge case - the attributes are there, but the block doesn't have support for visibility. What do you think @t-hamano
There was a problem hiding this comment.
It was actually intentional that I didn't check if the block had block support. This is because if visibility support is later disabled for the block, its content could leak.
This logic aligns with the visibility support logic.
- Block Visibility: Keep hide-everywhere working after a block opts outof visibility support #12053
- Block Visibility: Keep hide-everywhere working after a block opts out of visibility support gutenberg#78780
However, the comments were not accurate, so I improved them.
There was a problem hiding this comment.
Thanks for the explainer!
Aki -1
Copilot - 0
| // Skip blocks hidden via the visibility block support. | ||
| if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) { | ||
| continue; | ||
| } |
ramonjd
left a comment
There was a problem hiding this comment.
Works well. Thanks a lot for the patch @t-hamano
I checked the editor as well. A couple of questions came up:
- How should we handle viewport breakpoints? A bit of a rhetorical question really as I think it's fine as it is. Hidden blocks aren't rendered, whereas viewport block are and are in the HTML content, and therefore should be in the excerpt.
- Do you think we'll need a GB patch as well?
Before
After
…ion. The previous comment implied the check inspected the block's visibility support, but the code only inspects the `blockVisibility` metadata. Reword it to explain that the metadata is honored regardless of the block's current visibility support, so blocks that previously had support enabled are not unintentionally exposed after their support is disabled. Co-Authored-By: Claude <noreply@anthropic.com>
|
@ramonjd Thanks for the review!
I agree. Since the content is dynamically controlled by CSS on the front end, the server cannot determine whether to hide the content. It is reasonable to always output the excerpt.
This PR directly updates core functionality, so it may be difficult to apply compatible code to GB. When I tried asking an AI, it indicated that the following extensive code would be necessary 😅 Details<?php
/**
* Honor the visibility block support when generating excerpts.
*
* Backports https://github.com/WordPress/wordpress-develop/pull/12167 (Trac #65456).
*
* Core's excerpt_remove_blocks() / _excerpt_render_inner_blocks() are not
* pluggable and have no per-block extension point, so the only way to inject
* the visibility check is to replace wp_trim_excerpt() on the
* `get_the_excerpt` filter with a Gutenberg copy that calls our excerpt
* functions. Remove this file once the change ships in WordPress Core.
*
* @package gutenberg
*/
/**
* Gutenberg copy of excerpt_remove_blocks() that skips blocks hidden via the
* visibility block support (`metadata.blockVisibility === false`).
*
* @param string $content The content to filter.
* @return string The filtered content.
*/
function gutenberg_excerpt_remove_blocks( $content ) {
if ( ! has_blocks( $content ) ) {
return $content;
}
$allowed_inner_blocks = array(
// Classic blocks have their blockName set to null.
null,
'core/freeform',
'core/heading',
'core/html',
'core/list',
'core/media-text',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
);
$allowed_wrapper_blocks = array(
'core/columns',
'core/column',
'core/group',
);
/** This filter is documented in wp-includes/blocks.php */
$allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );
$allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );
/** This filter is documented in wp-includes/blocks.php */
$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
$blocks = parse_blocks( $content );
$output = '';
foreach ( $blocks as $block ) {
// Hide the block whenever the value is boolean false, regardless of the
// block's current visibility support. This prevents blocks that previously
// supported visibility from unintentionally appearing on the front end
// after their support was disabled.
if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
continue;
}
if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
if ( ! empty( $block['innerBlocks'] ) ) {
if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
$output .= gutenberg_excerpt_render_inner_blocks( $block, $allowed_blocks );
continue;
}
// Skip the block if it has disallowed or nested inner blocks.
foreach ( $block['innerBlocks'] as $inner_block ) {
if (
! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
! empty( $inner_block['innerBlocks'] )
) {
continue 2;
}
}
}
$output .= render_block( $block );
}
}
return $output;
}
/**
* Gutenberg copy of _excerpt_render_inner_blocks() that skips inner blocks
* hidden via the visibility block support.
*
* @param array $parsed_block The parsed block.
* @param array $allowed_blocks The list of allowed inner blocks.
* @return string The rendered inner blocks.
*/
function gutenberg_excerpt_render_inner_blocks( $parsed_block, $allowed_blocks ) {
$output = '';
foreach ( $parsed_block['innerBlocks'] as $inner_block ) {
// Hide the block whenever the value is boolean false, regardless of the
// block's current visibility support. This prevents blocks that previously
// supported visibility from unintentionally appearing on the front end
// after their support was disabled.
if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
continue;
}
if ( ! in_array( $inner_block['blockName'], $allowed_blocks, true ) ) {
continue;
}
if ( empty( $inner_block['innerBlocks'] ) ) {
$output .= render_block( $inner_block );
} else {
$output .= gutenberg_excerpt_render_inner_blocks( $inner_block, $allowed_blocks );
}
}
return $output;
}
/**
* Gutenberg copy of wp_trim_excerpt() that routes block removal through
* gutenberg_excerpt_remove_blocks() so hidden blocks are stripped from
* auto-generated excerpts.
*
* Identical to core wp_trim_excerpt() except for the excerpt_remove_blocks()
* call, which is swapped for gutenberg_excerpt_remove_blocks().
*
* @param string $text The trimmed text.
* @param WP_Post|int $post The post.
* @return string The trimmed excerpt.
*/
function gutenberg_trim_excerpt( $text = '', $post = null ) {
$raw_excerpt = $text;
if ( '' === trim( $text ) ) {
$post = get_post( $post );
$text = get_the_content( '', false, $post );
$text = strip_shortcodes( $text );
$text = gutenberg_excerpt_remove_blocks( $text );
$text = excerpt_remove_footnotes( $text );
/*
* Temporarily unhook wp_filter_content_tags() since any tags
* within the excerpt are stripped out. Modifying the tags here
* is wasteful and can lead to bugs in the image counting logic.
*/
$filter_image_removed = remove_filter( 'the_content', 'wp_filter_content_tags', 12 );
/*
* Temporarily unhook do_blocks() since excerpt_remove_blocks( $text )
* handles block rendering needed for excerpt.
*/
$filter_block_removed = remove_filter( 'the_content', 'do_blocks', 9 );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
// Restore the original filter if removed.
if ( $filter_block_removed ) {
add_filter( 'the_content', 'do_blocks', 9 );
}
/*
* Only restore the filter callback if it was removed above. The logic
* to unhook and restore only applies on the default priority of 10,
* which is generally used for the filter callback in WordPress core.
*/
if ( $filter_image_removed ) {
add_filter( 'the_content', 'wp_filter_content_tags', 12 );
}
/* translators: Maximum number of words used in a post excerpt. */
$excerpt_length = (int) _x( '55', 'excerpt_length' );
/** This filter is documented in wp-includes/formatting.php */
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
/** This filter is documented in wp-includes/formatting.php */
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/** This filter is documented in wp-includes/formatting.php */
return apply_filters( 'wp_trim_excerpt', $text, $raw_excerpt );
}
// Replace core's excerpt trimmer with the visibility-aware version.
remove_filter( 'get_the_excerpt', 'wp_trim_excerpt', 10 );
add_filter( 'get_the_excerpt', 'gutenberg_trim_excerpt', 10, 2 ); |
Trac ticket: https://core.trac.wordpress.org/ticket/65456
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8
Used for: Investigating the excerpt rendering path, implementing the fix, and drafting the tests. All changes were reviewed and edited by me.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.