Make WordPress Core

Opened 8 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#66176 closed enhancement (duplicate)

Block Parsing improvements in WordPress 7.2

Reported by: dmsnell Owned by:
Priority: normal Milestone:
Component: General Version: trunk
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

This tracking issue collects scattered improvements to the Block Parsing APIs in Core during the WordPress 7.2 release cycle.

Change History (15)

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


8 weeks ago
#1

  • Keywords has-patch added

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.


8 weeks ago
#2

Trac ticket: Core-66176

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

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.

#3 follow-up: @jonsurrell
6 weeks ago

Shall we merge this into #66138 (as duplicate) and widen that to block parsing instead of just the block processor?

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


3 weeks ago

#5 in reply to: ↑ 3 @audrasjb
3 weeks ago

Replying to jonsurrell:

Shall we merge this into #66138 (as duplicate) and widen that to block parsing instead of just the block processor?

@dmsnell what do you think? We're at the beta 2 stage now, so I think we should address this as soon as possible, given this is an enhancement therefore it will be punted before RC1.

#6 @dmsnell
3 weeks ago

  • Milestone 7.2
  • Resolution → duplicate
  • Status new → closed

Duplicate of #66138.

Sounds good @audrasjb — I hope to try and merge the open work ASAP on this as well so it makes it into 7.2

​@jonsurrell commented on ​PR #13695:


3 weeks ago
#7

Merged in r64870.

​@dmsnell commented on ​PR #13695:


3 weeks ago
#8

@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
#9

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
#10

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
#11

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
#12

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
#13

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
#14

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 #13720:


3 weeks ago
#15

Merged in r64996
1021d4d381869524e42ca770356fd38dc18d4a98

Note: See TracTickets for help on using tickets.