Make WordPress Core

Opened 2 months ago

Last modified 7 days ago

#66138 assigned task (blessed)

Block processor fixes and improvements for 7.2

Reported by: jonsurrell Owned by:
Priority: normal Milestone: 7.2
Component: General Version: 6.9
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description (last modified by jonsurrell)

This ticket tracks fixes and improvements to the block parsing class for the 7.2 release.

Change History (52)

This ticket was mentioned in ​PR #13604 on ​WordPress/wordpress-develop by ​@jonsurrell.


2 months ago
#1

  • Keywords has-patch has-unit-tests added

Stop WP_Block_Processor from reporting an extra block delimiter when HTML content ends in <, <!, or <!-.

For example, text< makes next_delimiter() report core/a twice. With no previous delimiter, text< reports a delimiter with an empty block name.

The processor returns the final HTML span but then treats the trailing bytes as a delimiter, reusing the previous delimiter's state. Set INCOMPLETE_INPUT when returning that span so the next call stops parsing. The example then produces one core/a delimiter and an HTML span; next_token() returns false on the following call.

Ten test cases cover the three partial comment prefixes, preceding opening, void and closing delimiters, and two complete-input controls. Eight cases fail before the fix. Local validation passed: 232 block-processor tests and 633 blocks tests.

Use of AI Tools

AI assistance: Yes
Tools: Claude Code, Codex
Models: Claude Opus, GPT-6
Used for: Finding the defect, drafting the fix and tests, and editing this description.

#2 @jonsurrell
2 months ago

  • Keywords commit added

#3 @jonsurrell
2 months ago

  • Description modified (diff)
  • Summary Block processor may produce non-existent blocks → Block processor fixes and improvements for 7.2

#4 @jonsurrell
2 months ago

  • Type defect (bug) → task (blessed)

#5 @jonsurrell
2 months ago

  • Keywords commit removed

#6 @jonsurrell
8 weeks ago

In 63888:

Block Processor: Stop extra block delimiter on trailing comments.

WP_Block_Processor::next_delimiter() reported an extra delimiter when HTML content ended in <, <!, or <!-. Set INCOMPLETE_INPUT when returning that span to prevent incorrect parsing.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13604

Props jonsurrell, westonruter, dmsnell.
See #66138.

​@jonsurrell commented on ​PR #13604:


8 weeks ago
#7

Merged in r63888.

This ticket was mentioned in ​PR #13679 on ​WordPress/wordpress-develop by ​@jonsurrell.


8 weeks ago
#8

find_html_comment_end() skips a comment the delimiter scan has rejected. After a -- that did not close the comment, it advanced one byte and found the same run of dashes again, so a run of n dashes was measured n times. A rejected comment containing 100 kilobytes of dashes took 1.1 seconds to skip; 200 kilobytes took 4.5 seconds. After the change both are immeasurable.

Resume the search where the closer would have been, after the whole run. Every -- inside the run leads to the same closer position, so nothing that could end the comment is skipped.

Six cases are added to the rejected-comment test: a dash run, a run followed by !, ! followed by dashes, the ---!> and --!--> endings, which must still close the comment, and a run of 50,000 dashes.

Use of AI Tools

AI assistance: Yes
Tools: Claude Code
Models: Claude Fable 5.1
Used for: Finding the defect, drafting the fix, tests and this description; reviewed and edited by me.

This ticket was mentioned in ​PR #13680 on ​WordPress/wordpress-develop by ​@jonsurrell.


8 weeks ago
#9

Keep each block delimiter within a single HTML comment. WP_Block_Processor now finds the comment end before checking the attributes, using find_html_comment_end() to handle both --> and --!>.

Comments ending in --!> remain invalid block delimiters. The processor now skips them without searching later comments for a -->. A closed comment at the end of the document is no longer reported as incomplete input.

When the attributes do not match the delimiter grammar, scanning resumes after the comment. A , and the abruptly closed <!--> form.

Adds 23 regression cases covering comment endings, delimiter recognition after rejected comments, dashes and ! in attributes, and the incomplete-input result.

Depends on ​https://github.com/WordPress/wordpress-develop/pull/13679; its commit is included.

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

Use of AI Tools

AI assistance: Yes
Tools: Claude Code, Codex
Models: Claude Fable 5.1, GPT-6
Used for: Claude Code assisted with investigation, implementation and tests, reviewed and edited by me. Codex edited this description.

​@dmsnell commented on ​PR #13680:


8 weeks ago
#10

@sirreal I approved this but added a couple commits, therefore I did not merge it. you are welcome to make the judgement call yourself on it, whether to revert some or all of my changes.

#11 @jonsurrell
8 weeks ago

In 63911:

Block Processor: Improve comment end search behavior.

Prevent WP_Block_Processor::find_html_comment_end() from repeatedly scanning some spans that cannot contain a comment end.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13679

Props jonsurrell, dmsnell.
See #66138.

​@jonsurrell commented on ​PR #13679:


8 weeks ago
#12

Merged in r63911.

#13 @jonsurrell
8 weeks ago

In 63913:

Block Processor: Ensure block delimiters align with single HTML comments.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13680

Props jonsurrell, dmsnell.
See #66138.

​@jonsurrell commented on ​PR #13680:


8 weeks ago
#14

Merged in r63913.

This ticket was mentioned in ​PR #14104 on ​WordPress/wordpress-develop by ​@jonsurrell.


6 weeks ago
#15

WP_Block_Processor::extract_full_block_and_advance() skips the token after a void block. In this loop, whether the first block has a closer decides whether b is found:

$processor = new WP_Block_Processor( $html );
while ( $processor->next_block() ) {
        echo $processor->extract_full_block_and_advance()['blockName'], "\n";
}
$html Output on trunk
core/a, core/b
core/a

Extraction stops on the closer of a block with inner content, so next_block() advances to b. After a void block it stops on the next token, b, so next_block() advances past it. The method's own loop does the same after an inner void block: extracts g without w.

Extraction now stops on the extracted block's last token in every case: the closer, the void delimiter, or the freeform HTML span. This removes the HTML span check after an inner extraction, which handled the old void case and, with incomplete input, copied the inner block's trailing HTML into the parent. The docblocks stated the void behavior as the rule for every block; they now state the new rule, and the splice examples take the block's end from the last token's span.

Seven new data_various_block_posts() inputs compare extraction with parse_blocks(), and a new test asserts the stop token for each kind of block. 12 cases fail on trunk.

Open question: a void block is now extracted without advancing, which the method name does not suggest. The alternative, stopping on the following token for every block as the old docblock said, changes the stop position for every block with a closer, including in Gutenberg's Table of Contents block.

Use of AI Tools

AI assistance: Yes
Tools: Claude Code
Models: Claude Opus 5.5
Used for: Investigation, implementation, tests, and this description.

​@jonsurrell commented on ​PR #14104:


6 weeks ago
#16

Good feedback, thank you. The behavior on void blocks could probably use an example, but I agree that we don't want to change behavior or contracts.

I've reworked this to be a minimal change only fixing the defect and not changing any public behaviors.

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


3 weeks ago

#18 @dmsnell
3 weeks ago

#66176 was marked as a duplicate.

This ticket was mentioned in ​PR #13695 on ​WordPress/wordpress-develop by ​@dmsnell.


3 weeks ago
#19

Trac ticket: Core-66138
Trac ticket: Core-66176

This patch unifies parsing between WP_Block_Parser and WP_Block_Processor, which brings the additional benefit of the optimizations in WP_Block_Processor.

This ticket was mentioned in ​PR #13720 on ​WordPress/wordpress-develop by ​@dmsnell.


3 weeks ago
#20

Trac ticket: Core-66138
Trac ticket: Core-66176

See wordpress/gutenberg#74427.
Dependent on #13695.

Status

Found a problem with this and the input:

PHP Fatal error:  Uncaught Error: Cannot use object of type WP_Block_Parser_Block as array in /Users/dmsnell/code/WordPress-develop/src/wp-includes/blocks.php:2163
Stack trace:
#0 /Users/dmsnell/code/WordPress-develop/src/wp-includes/blocks.php(2128): filter_block_kses(Object(WP_Block_Parser_Block), 'post', Array)
#1 /Users/dmsnell/code/WordPress-develop/src/wp-includes/formatting.php(5378): filter_block_content('<!-- wp:unsafe ...', 'post', Array)
#2 /Users/dmsnell/code/WordPress-develop/src/wp-includes/class-wp-hook.php(369): wp_pre_kses_block_attributes('<!-- wp:unsafe ...', 'post', Array)
#3 /Users/dmsnell/code/WordPress-develop/src/wp-includes/plugin.php(207): WP_Hook->apply_filters('<!-- wp:unsafe ...', Array)
#4 /Users/dmsnell/code/WordPress-develop/src/wp-includes/kses.php(2312): apply_filters('pre_kses', '<!-- wp:unsafe ...', 'post', Array)
#5 /Users/dmsnell/code/WordPress-develop/src/wp-includes/kses.php(1005): wp_kses_hook('<!-- wp:unsafe ...', 'post', Array)
#6 Command line code(1): wp_kses('<!-- wp:unsafe ...', 'post')
#7 {main}
  thrown in /Users/dmsnell/code/WordPress-develop/src/wp-includes/blocks.php on line 2163

Desscription

When reaching the end of a document during block parsing, and blocks
remain open, existing open blocks should implicitly close. Previously,
there was a mistake in the closing code which duplicated content in the
implicitly-closed blocks.

In this patch, the semantic actions of closing blocks is extracted for
easier identification of the logic, and the defect is resolved.

#21 @dmsnell
3 weeks ago

In 64870:

Blocks: Rely on WP_Block_Processor inside WP_Block_Parser

When WP_Block_Processor was introduced, it added a second pathway for parsing block structure. This patch unifies that parsing by replacing the internals of the WP_Block_Parser class with the newer WP_Block_Processor.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13695
Discussed in: https://core.trac.wordpress.org/ticket/66138

Props dmsnell, jonsurrell.
See #66138.

​@jonsurrell commented on ​PR #13695:


3 weeks ago
#22

Merged in r64870.

#23 @jonsurrell
3 weeks ago

  • Description modified (diff)

​@dmsnell commented on ​PR #13695:


3 weeks ago
#24

@westonruter thanks for the review. Updating the WP_Block_Parser_Block constructor’s annotation for $attrs argument seems fitting. For the other claims I feel like this is a hard problem based on a mismatch of the code’s design and PHPStan’s type system. I’m very reluctant to start adding a bunch of runtime checks (as those can mislead a dev who might start asking, “wait, why might this be null”, and as they add more noise overall) particularly without evidence of any defects.

these might catch regressions if the code for get_span() or get_block_type() or something else changes, but that’s putting the burden on the end-customer to detect and circumventing the early detections we would have with tests covering those cases. I think those are all good candidates to ensure are covered in tests, either unit tests with specific scenarios, or some fuzzing test that spends time trying to find if there are ever situations where these invariants break.

​@westonruter commented on ​PR #13695:


3 weeks ago
#25

I have an old unpushed branch for improving the type of $attrs and WP_Block_Parser_Block. Let me see if that would help here. I'll open a PR.

​@westonruter commented on ​PR #13720:


3 weeks ago
#26

Just noting that PHPStan is already flagging the error in filter_block_content():

​https://github.com/WordPress/wordpress-develop/blob/288dc0f5886e35f6d51582271107e3a37ec82064/src/wp-includes/blocks.php#L2127-L2129

The error was baselined:

​https://github.com/WordPress/wordpress-develop/blob/288dc0f5886e35f6d51582271107e3a37ec82064/tests/phpstan/baselines/argument.type.neon#L656-L660

With this change:

  • src/wp-includes/blocks.php

    diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
    index 626e7b7f14..6d96390b75 100644
    a b function _filter_block_content_callback( $matches ) {  
    21522152 *
    21532153 * @since 5.3.1
    21542154 *
    2155  * @param WP_Block_Parser_Block $block             The parsed block object.
     2155 * @param array                $block             The parsed block object.
    21562156 * @param array[]|string        $allowed_html      An array of allowed HTML elements and attributes,
    21572157 *                                                 or a context name such as 'post'. See wp_kses_allowed_html()
    21582158 *                                                 for the list of accepted context names.

The following baselined errors can be removed:

  • tests/phpstan/baselines/argument.type.neon

    diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon
    index a8beb0366a..b0d0b3208b 100644
    a b parameters:  
    653653                        identifier: argument.type
    654654                        count: 1
    655655                        path: ../../../src/wp-includes/author-template.php
    656                 -
    657                         message: '#^Parameter \#1 \$block of function filter_block_kses expects WP_Block_Parser_Block, array given\.$#'
    658                         identifier: argument.type
    659                         count: 1
    660                         path: ../../../src/wp-includes/blocks.php
    661                 -
    662                         message: '#^Parameter \#4 \$block_context of function filter_block_kses_value expects array\|null, WP_Block_Parser_Block given\.$#'
    663                         identifier: argument.type
    664                         count: 1
    665                         path: ../../../src/wp-includes/blocks.php
    666656                -
    667657                        message: '#^Parameter \#2 \$callback of function preg_replace_callback expects callable\(array\<string\>\)\: string, ''lowercase_octets'' given\.$#'
    668658                        identifier: argument.type
  • tests/phpstan/baselines/return.type.neon

    diff --git a/tests/phpstan/baselines/return.type.neon b/tests/phpstan/baselines/return.type.neon
    index 8122cecd33..a7ae371e90 100644
    a b parameters:  
    2828                        identifier: return.type
    2929                        count: 1
    3030                        path: ../../../src/wp-content/themes/twentytwenty/functions.php
    31                 -
    32                         message: '#^Function filter_block_kses\(\) should return array but returns ArrayAccess&WP_Block_Parser_Block\.$#'
    33                         identifier: return.type
    34                         count: 1
    35                         path: ../../../src/wp-includes/blocks.php
    3631                -
    3732                        message: '#^Method WP_Block_Processor\:\:extract_full_block_and_advance\(\) should return array\<array\>\|null but returns array\<string, array\|string\|null\>\.$#'
    3833                        identifier: return.type

​@dmsnell commented on ​PR #13720:


3 weeks ago
#27

already flagging the error in filter_block_content():

to confirm: this is an already-present error in filter_block_contents() type annotation from 2019 and unrelated to this change, yeah?

​@westonruter commented on ​PR #13720:


3 weeks ago
#28

to confirm: this is an already-present error in filter_block_contents() type annotation from 2019 and unrelated to this change, yeah?

I think so. The error was baselined in b00336ac (r63024) as part of the original bump to rule level 5. So it was an existing issue.

​@westonruter commented on ​PR #13720:


3 weeks ago
#29

I can apply the fix to the other typing PR I'm working on if you don't want to fix it here.

​@dmsnell commented on ​PR #13720:


3 weeks ago
#30

I can apply the fix to the other typing PR I'm working on if you don't want to fix it here.

if you are fixing the type in filter_block_content() then another PR would seem more appropriate. I think it was accidental that we discovered it on this one, but that’s why I was trying to confirm: that invalid type annotation is unrelated to and unaffected by the changes here.

​@dmsnell commented on ​PR #14104:


3 weeks ago
#31

@sirreal I pushed 93aada3 to this branch after spending some time trying to understand the fix, and I think I finally have a way to express what the expectation should be for this function.

I flipped the flag so that we’re making a positive check rather than negating one. initially I renamed $should_advance to $skip_past_closer, inverted the while condition, and then struggled to understand why we should only skip past the closer if we’re _not_ on a void delimiter.

Then I realized I had the condition backwards in my head. We’re not speaking about the closer so much as we’re asking if we are already where we need to be for the next loop iteration, and short-circuiting next_token(). This is of course logically the same as saying ! $should_advance || $this->next_token() but I find this other construction more natural $skip_next_advance || $this->next_token() (either we stay put, or we advance).

Maybe we could still rename this $stay_put

Then I didn’t like the split assignments throughout the loop so I lifted it to the first line. Now it only needs a single assignment and is hopefully more difficult to miss in any follow-up work.

​@dmsnell commented on ​PR #14104:


3 weeks ago
#32

oops: I found another failing case by trying to use this for WP_Block_Parser::parse() — will post an update soon. I think it’s the same problem, but we only applied the fix for inner blocks and it should apply to top-level voids as well

​@dmsnell commented on ​PR #14104:


3 weeks ago
#33

@sirreal I returned to ​my previous comment and updated the tests you added, which brings the behaviors back to something we discussed, but was awash in the evolution of this patch

the way it stands now, the class is designed to support this exact flow

public function parse( $document ) {
                $this->document = $document;
                $this->offset   = 0;
                $this->output   = array();
                $this->stack    = array();

                $this->block_processor = new WP_Block_Processor( $document );

                while ( $this->block_processor->next_block( '*' ) ) {
                        $this->output[] = $this->block_processor->extract_full_block_and_advance();
                }

                return $this->output;
        }

and I have this passing all tests locally when I replace the WP_Block_Parser code with that. I think this is right, and the original goal of the method, as evidenced by the test code in test_extracts_equivalent_parses_as_parse_blocks()

#34 @dmsnell
3 weeks ago

In 64994:

Blocks: Fix advancement inside extract_full_block_and_advance().

When the Block Processor was introduced, the extract_full_block_and_advance() method was provided in order to support backwards compatibility with parse_blocks(), specifically when wanting to extract the structural representation of a subset of blocks within a large post.

Unfortunately, when extracting void blocks, it was advancing to the next token, which, if called from within a loop that was already advancing, would lead to a skipped block delimiter.

In this patch the behavior is normalized such that, after a call to extract_full_block_and_advance(), the calling code should call next_token() to continue processing the next block or top-level freeform HTML in the post.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14104
Discussed in: https://core.trac.wordpress.org/ticket/66138

Follow-up to r60939.

Props dmsnell, jonsurrell, westonruter.
See #66138.

​@dmsnell commented on ​PR #14104:


3 weeks ago
#35

Merged in r64994
30b32ec224f968bba5443468e0b44930bafcf240

#36 @dmsnell
3 weeks ago

In 64996:

Blocks: Fix implicit block closing in default block parser.

When reaching the end of a document during block parsing, and blocks remain open, existing open blocks should implicitly close. Previously, there was a mistake in the closing code which duplicated content in the implicitly-closed blocks.

In this patch, that implicit closing of open blocks is moved into a new helper method where the logic can be more clearly isolated and analyzed, and the defect is resolved.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13720
Discussed in: https://core.trac.wordpress.org/ticket/66138

Props dmsnell, jonsurrell, westonruter.
See #66138.

​@dmsnell commented on ​PR #13720:


3 weeks ago
#37

Merged in r64996
1021d4d381869524e42ca770356fd38dc18d4a98

This ticket was mentioned in ​PR #15197 on ​WordPress/wordpress-develop by ​@chihsuan.


3 weeks ago
#38

Fix the default block parser repeating earlier content when a document ends with an unclosed block.

When the last open block has no HTML right before it, [64996] treats its leading freeform span as starting at offset 0, so everything before that block is added again as a freeform block. For example, A parses into the paragraph, a freeform block holding the paragraph's markup again, then the latest posts block. The span now starts at the block's own token, so it is empty and no freeform block is added.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Bisecting the regression across parser versions and drafting the fix and tests. I reviewed the change and ran the blocks test group locally.

---

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.

This ticket was mentioned in ​PR #15199 on ​WordPress/wordpress-develop by ​@chihsuan.


3 weeks ago
#39

Fix a fatal error when a document ends with nested blocks that are both left open.

When [64996] implicitly closes nested blocks, it adds each inner block to its parent as a WP_Block_Parser_Block object instead of an array. Every other path adds inner blocks as arrays, so code that reads them fails: for <div><p>B</p>, serialize_blocks( parse_blocks() ) throws "Cannot use object of type WP_Block_Parser_Block as array", and do_blocks() ends in a TypeError. The inner block is now cast to an array.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Finding and reproducing the error and drafting the fix and test. I reviewed the change and ran the blocks test group locally.

---

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.

#40 @chihsuan
3 weeks ago

Hi @dmsnell, thanks for all the parser work! We just ran into two regressions from [64996] in Jetpack's tests against trunk:

Both are one-line fixes with tests. Could you take a look? Thanks!

#41 @dmsnell
3 weeks ago

In 65062:

Blocks: Ensure array type when implicitly closing nested blocks.

When a fix was recently applied to the block parser for implicitly closing open blocks that remain open at the end of a document, the fix appended the structured WP_Block_Parser_Block class instance to the list of output blocks. This led to issues with code assuming array data types, accessing properties with array access rather than object access.

This patch casts the object to an array to ensure compatibility.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15199
Discussed in: https://core.trac.wordpress.org/ticket/66138

Follow-up to r64996.

Props chihsuan.
See #66138.

​@dmsnell commented on ​PR #15199:


3 weeks ago
#42

Merged in r65062
71c7321ea542b52d7775c7d7da12341e290fc99a

#43 @dmsnell
3 weeks ago

In 65090:

Blocks: Fix duplicated content when implicitly closing a final open block

When a fix was recently applied to the block parser for implicitly closing open blocks that remain open at the end of a document, a mistake was made when collecting HTML spans preceding the still-open blocks. This led to duplicated content in documents missing expected closing delimiters.

This patch adjusts the implict closing algorithm to properly detect those HTML spans.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15197
Discussed in: https://core.trac.wordpress.org/ticket/66138

Follow-up to r64996.

Props chihsuan, dmsnell.
See #66138.

​@dmsnell commented on ​PR #15197:


3 weeks ago
#44

Thanks for another nice catch, @chihsuan, and for the fix.

Merged in r65090.
fc4beffbd8e7aa11fa2133ad8261c4d19b14dc14

This ticket was mentioned in ​PR #15340 on ​WordPress/wordpress-develop by ​@rejaulalomkhan.


2 weeks ago
#45

Description

Since r64870, WP_Block_Parser::parse() passes its document to the WP_Block_Processor constructor, which accepts only a string. A null document now causes a fatal TypeError in parse_blocks(), do_blocks(), filter_block_content(), and apply_filters( 'the_content', null ).

The 7.1 parser returns an empty list of blocks for the same input, with two "Passing null" deprecation notices, so this is a regression for any plugin or theme that passes null. One way to reach it in core is a REST request that omits a Block widget's content, for example PUT /wp/v2/widgets/block-2 with form_data=widget-block[2][_x]=1. WP_Widget_Block::update() stores content as null, the request fails with the TypeError, and dynamic_sidebar() keeps failing until the value is repaired.

This treats a null document as an empty one in parse() and leaves the strict type on WP_Block_Processor. Other non-string input behaves as before.

Not changed here: WP_Widget_Block::update() still reads content without a guard, and other the_content callbacks can still raise their own deprecation notices for null.

Testing Instructions

  1. Run the new test, which fails on trunk with the TypeError above:
    vendor/bin/phpunit --filter test_parses_null_document_as_empty
    
  1. Run the related groups on single site:
    vendor/bin/phpunit --group blocks
       vendor/bin/phpunit --group block-processor
       vendor/bin/phpunit --group widgets
    
  1. Run the blocks group on multisite:
    vendor/bin/phpunit -c tests/phpunit/multisite.xml --group blocks
    
  1. Run the static checks:
    vendor/bin/phpcs src/wp-includes/class-wp-block-parser.php tests/phpunit/tests/blocks/wpBlockParser.php
       composer phpstan
    

Results on PHP 8.3: blocks 719 tests and 3461 assertions, block-processor 279 and 822, widgets 505 and 2016, multisite blocks 718 and 3451, all passing. PHPCS is clean. PHPStan reports one error, $authors_dropdown in class-wp-posts-list-table.php, which is also present on trunk without this change.

---
This Pull Request is for code review only. Please keep all other discussion to the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: root-cause analysis, drafting the fix and regression test, running the checks listed above, and drafting this description. I reviewed every line and take responsibility for the change.

#46 @rejaulalomkhan
2 weeks ago

Regression from [64870]: parse_blocks( null ) now causes a fatal TypeError, because WP_Block_Parser::parse() passes the document to WP_Block_Processor::__construct( string $source_text ).

Reproduced on trunk at [65184], PHP 8.3, single site: parse_blocks( null ), do_blocks( null ), filter_block_content( null ) and apply_filters( 'the_content', null ) all throw it. The 7.1 parser, run on its own, returns an empty array for null with two deprecation notices.

Core can reach it through a REST request that omits a Block widget's content. With an administrator, PUT /wp/v2/widgets/block-2 and form_data=widget-block[2][_x]=1 stores content as null and throws, and dynamic_sidebar() then throws until the value is repaired. I did not find a path through the standard editor UI, so plugins, themes and custom REST clients are where this would show up.

PR with a failing test and fix: ​https://github.com/WordPress/wordpress-develop/pull/15340

The fix treats null as an empty document in parse() and keeps the strict type on WP_Block_Processor. If you would rather reject null and treat this as a caller bug, this should at least be a deliberate back-compat change.

AI assistance: Claude Code (Claude Opus 5.5) reproduced this and drafted the patch and this note; I checked the results and the points above myself.

#47 @mmtr86
10 days ago

I also spotted the same fatal error that @rejaulalomkhan reported above on a sample of WordPress.com sites running the latest 7.2 beta. So far, they came from a couple of plugins:

  • Divi 5: ET\Builder\FrontEnd\Assets\DynamicAssetsUtils::get_active_block_widgets() calls parse_blocks( null ) on the wp action, for Block widgets with no content.
  • The Notice FAQ plugin, which calls parse_blocks( null ) on wp_head.

#48 @jonsurrell
10 days ago

Thanks @rejaulalomkhan and @mmtr86 for those reports. ​The PR looks like the right direction and a good fix, it does have some feedback to address before landing.

#49 @manfcarlo
9 days ago

​Left a comment:

Just letting you know I am getting 500 errors on 7.2-beta4 because the new class has not been included through the main parser file (see ​WordPress/gutenberg#51670 for how it was done for the other classes)

This happens because the block parser classes were historically all included in the one file, before they were moved into separate files in ​WordPress/gutenberg#48693. Therefore, if someone wanted to load the parser without loading any other core classes, the classes being moved into separate files would have caused an error, which is why 51670 linked above re-included the classes just in case. The same should probably be done for the new classes added in the 7.2 cycle.

Perhaps a unit test could also be added to ensure that including the main parser class without other core classes works without errors, in the case that more changes are made in the future.

#50 @rejaulalomkhan
9 days ago

Thanks, @jonsurrell and @mmtr86. I've updated ​PR 15340 following @dmsnell's review. parse() keeps its string type, and a document that is not a string now returns an empty array with a _doing_it_wrong() notice instead of a fatal error.

For the plugins @mmtr86 found, parse_blocks( null ) would then return an empty array, so Divi 5 and Notice FAQ would see no blocks and the page would load. I checked parse_blocks(), do_blocks() and the_content with null, but not the plugins themselves.

One change to note: on trunk, parse_blocks( 123 ) returns a freeform block containing 123. With the PR, it returns an empty array with the notice, like other non-strings.

AI assistance: Claude Code (Claude Opus 5.5) helped draft this comment and the change. I reviewed both.

#51 @dmsnell
7 days ago

In 65405:

Blocks: Non-string documents should parse as empty arrays.

Following the change to rely on WP_Block_Processor for parsing inside of WP_Block_Parser, a small behavioral change affected non-string inputs. Previously they coerced to strings, but the updated code throws a TypeError.

In this patch, non-string and Stringable inputs call _doing_it_wrong(), Stringables coerce to string, and non-string inputs return an empty array.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15340
Discussed in: https://core.trac.wordpress.org/ticket/66138

Follow-up to r64870.

Props dmsnell, rejaulalomkhan.
See #66138.

Note: See TracTickets for help on using tickets.