Skip to content

Validate each arg before setting WP_Block_Type properties #48039

@hellofromtonya

Description

@hellofromtonya

Description

Part of #41236.

Found in backport of adding support for editing block style variations in the global styles. See https://core.trac.wordpress.org/ticket/57583#comment:19.

It is possible to register a block type with its properties set to the wrong data type(s). For example, the WP_Block_Type::$style is expected to be an array, but can be registered as any other data type including a string.

The code interacting with the WP_Block_Type expects each property to be the correct data type. If any are not, then

  • PHP Warning can happen in PHP 5.6 to 8.x.
  • Unexpected behaviors could happen.
  • Can make debugging harder to find the root cause.

As a result, data type checks are being introduced within the code that interacts with WP_Block_Type. These data type checks could be removed if the properties of WP_Block_Type were always in the expected and documented data type.

Step-by-step reproduction instructions

  1. Turn on WP_DEBUG and WP_DEBUG_DISPLAY in wp-config.php:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
  1. Add a mu-plugins test file and copy/paste this gist into it https://gist.github.com/hellofromtonya/8c4176c4ea87e02424cf3bfd7b954ac0.
  2. Refresh the web page.
  3. Notice the PHP Warning.

You can also var_dump() the block type returned register_block_type() to notice the properties are not validated.

Screenshots, screen recording, code snippet

https://3v4l.org/9YloM

Running the provided testing steps throws the following warning:

Warning: Invalid argument supplied for foreach() in /var/www/src/wp-includes/class-wp-theme-json.php on line 866

Note: This warning will soon be resolved with a guard being added into Core at the error line noted above. This guard can be removed if validation is added at the point of setting the property to avoid masking the problem.

Environment info

  • WordPress trunk

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs DevReady for, and needs developer efforts[Feature] Block APIAPI that allows to express the block paradigm.[Type] BugAn existing feature does not function as intended[Type] Code QualityIssues or PRs that relate to code quality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions