Skip to content

Blocks: Preserve pre-rendered content when generating preset styles - #14615

Open
salvatorecorsi wants to merge 1 commit into
WordPress:trunkfrom
salvatorecorsi:fix/59551-preserve-pre-rendered-content
Open

salvatorecorsi wants to merge 1 commit into
WordPress:trunkfrom
salvatorecorsi:fix/59551-preserve-pre-rendered-content

Conversation

@salvatorecorsi

Copy link
Copy Markdown

When an earlier pre_render_block filter returns cached HTML, '', or '0', _wp_add_block_level_preset_styles() replaces that value with null. WordPress then renders the block instead of honoring the earlier short-circuit.

Return the incoming $pre_render from all three exits and document the string|null return type. Preset CSS generation and enqueueing still run when the block supports settings and has presets, including when its markup was provided by an earlier filter. A null input continues through normal rendering.

Trac ticket: https://core.trac.wordpress.org/ticket/59551

Companion Gutenberg change: WordPress/gutenberg#85260

This follows the ticket reported by danieliser and the pass-through approach discussed by jeremyfelt and pbiron. The remaining change concerns the preset-styles callback. Element support already uses render_block_data, following its migration in WordPress 6.6.

Reproduction

  1. Create a post containing a Paragraph block.
  2. Add an earlier filter:
add_filter(
    'pre_render_block',
    static function ( $pre_render, $parsed_block ) {
        if ( 'core/paragraph' === $parsed_block['blockName'] ) {
            return '';
        }
        return $pre_render;
    },
    9,
    2
);
  1. View the post. The Paragraph should be omitted; before this change, the preset-styles filter clears the earlier result and the Paragraph renders.
  2. Repeat with '<p>Cached paragraph</p>' and '0'. The exact replacement should be returned.
  3. Remove the filter and confirm normal rendering.

Validation

The regression tests use a registered dynamic block, the real serialize_block()/do_blocks() filter chain, and renderer/filter callback counts. Twelve cases cover unsupported blocks, supported blocks without settings, and blocks with real palette presets, each with empty string, string zero, cached HTML, and null. The enqueued stylesheet callbacks verify that preset variables and classes are still emitted. The tests restore their owned filter, style-hook snapshots, and block registration.

  • Frozen-base RED on e46ac23b14711601e6cb17aafe63af6673c03591: 12 tests, 35 assertions, nine expected failures and no errors. All three null controls passed, including actual preset CSS emission.
  • Fixed-runtime focused PHPUnit: 12 tests and 68 assertions passed with the same test file.
  • Complete block-supports PHPUnit group: 511 tests and 741 assertions passed.
  • PHP syntax, WordPress coding standards and PHP compatibility checks passed for both changed files. Scoped PHPStan for the production file reported zero errors.
  • Full npm run build passed. The built settings.php matches the reviewed source byte-for-byte.
  • A separate Codex agent independently reviewed the implementation, tests and validation evidence without actionable findings.

Validation used Windows, PHP 8.3.28, PHPUnit 9.6.38 and an isolated MariaDB test database. PHPUnit was executed through a local runner that serializes access to this disposable database. The checkout-local commands were:

php vendor/bin/phpunit -c phpunit.xml.dist --filter Tests_Block_Supports_Settings
php vendor/bin/phpunit -c phpunit.xml.dist --group block-supports
php -l src/wp-includes/block-supports/settings.php
php -l tests/phpunit/tests/block-supports/settings.php
php vendor/squizlabs/php_codesniffer/bin/phpcs --report=summary,source --parallel=1 src/wp-includes/block-supports/settings.php tests/phpunit/tests/block-supports/settings.php
php vendor/squizlabs/php_codesniffer/bin/phpcs --standard=phpcompat.xml.dist --report=summary,source --parallel=1 src/wp-includes/block-supports/settings.php tests/phpunit/tests/block-supports/settings.php
php vendor/bin/phpstan analyse --memory-limit=2G --no-progress --error-format=json src/wp-includes/block-supports/settings.php
npm run build

PHPUnit reported a deprecated XML configuration schema warning. The successful build emitted Node deprecation and webpack asset-size/performance warnings. These warnings remain visible; no configuration, baseline or suppression was changed to hide them. The full PHPUnit suite, multisite and Docker-based tests were not run. Gutenberg's results are recorded separately in the companion PR.

Use of AI Tools

AI assistance: Yes
Tool(s): OpenAI Codex
Used for: Issue investigation, implementation, regression tests, validation, and this PR description. A separate Codex agent independently reviewed the implementation and tests and read the validation evidence. No human review by the account owner is claimed; contributor and maintainer review is requested.


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

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 salvatorecorsi.

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.

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.

1 participant