Skip to content

Excerpt: Honor the visibility block support when generating excerpts.#12167

Closed
t-hamano wants to merge 5 commits into
WordPress:trunkfrom
t-hamano:65456-visibility-excerpt
Closed

Excerpt: Honor the visibility block support when generating excerpts.#12167
t-hamano wants to merge 5 commits into
WordPress:trunkfrom
t-hamano:65456-visibility-excerpt

Conversation

@t-hamano

@t-hamano t-hamano commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

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 WordPress Project

@t-hamano t-hamano marked this pull request as ready for review June 13, 2026 11:47
@github-actions

github-actions Bot commented Jun 13, 2026

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props wildworks, ramonopoly.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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>
@t-hamano t-hamano force-pushed the 65456-visibility-excerpt branch from 210abfb to c0ae630 Compare June 13, 2026 12:14
t-hamano and others added 2 commits June 13, 2026 21:37
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/wp-includes/blocks.php Outdated
Comment on lines +2241 to +2244
// Skip blocks hidden via the visibility block support.
if ( false === ( $block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

However, the comments were not accurate, so I improved them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explainer!

Aki -1
Copilot - 0

Comment thread src/wp-includes/blocks.php Outdated
Comment on lines +2307 to +2310
// Skip blocks hidden via the visibility block support.
if ( false === ( $inner_block['attrs']['metadata']['blockVisibility'] ?? null ) ) {
continue;
}

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Image

After

Image

…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>
@t-hamano

Copy link
Copy Markdown
Contributor Author

@ramonjd Thanks for the review!

Hidden blocks aren't rendered, whereas viewport block are and are in the HTML content, and therefore should be in the excerpt.

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.

Do you think we'll need a GB patch as well?

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( ']]>', ']]&gt;', $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', ' ' . '[&hellip;]' );
		$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 );

@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 62539
GitHub commit: 18bf527

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants