Make WordPress Core

Opened 3 years ago

Last modified 9 days ago

#52592 new defect (bug)

PHP warning when the label property is missing from register_block_style

Reported by: poena's profile poena Owned by:
Milestone: 6.7 Priority: normal
Severity: normal Version: 5.3
Component: Editor Keywords: has-patch needs-testing needs-unit-tests
Focuses: Cc:

Description

If I register a block style using the PHP function
register_block_style, and I leave out the label property, there is a PHP notice:

Notice:  Undefined index: label in /var/www/src/wp-includes/script-loader.php on line 2312

The label is required, but the class does not check if a label exists or not, see:
https://core.trac.wordpress.org/browser/tags/5.6.1/src/wp-includes/class-wp-block-styles-registry.php#L43

In comparison, if I leave out the name property, a _doing_it_wrong message is shown.
I think a _doing_it_wrong should be shown for the label too.

Steps to reproduce the issue:

Register an incomplete block style in a theme or plugin file using register_block_style.

register_block_style(
    'core/quote',
    array(
        'inline_style' => '.wp-block-quote.is-style-blue-quote { color: blue; }',
    )
);

Confirm that the doing it wrong message for the name is showing.

Add the name property:

register_block_style(
    'core/quote',
    array(
	'name'  => 'test',
        'inline_style' => '.wp-block-quote.is-style-blue-quote { color: blue; }',
    )
);

Open the block editor. Confirm that a PHP notice for the label is showing.

Change History (7)

#1 @poena
11 days ago

I tested again today on 6.6 RC3 using PHP 8.1.23, and what was a notice is now a warning:

Warning:  Undefined array key "label" in wp-includes/script-loader.php on line 2716


#2 @poena
11 days ago

  • Summary changed from PHP notice when the label property is missing from register_block_style to PHP warning when the label property is missing from register_block_style

This ticket was mentioned in PR #7008 on WordPress/wordpress-develop by @poena.


11 days ago
#3

  • Keywords has-patch added

This PR updates the register method inside WP_Block_Styles_Registry:
Adds a check for the required label style property.
If the label is not a string or is not set, display a doing_it_wrong notice, and return early.

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

#4 @poena
11 days ago

  • Keywords needs-testing added

Testing instructions for PR7008:

Enabling displaying or logging of PHP errors/warnings/notices.
Add the following code inside twentytwentyfour_block_styles() in functions.php of the theme Twenty Twenty-Four:

register_block_style(
    'core/quote',
    array(
	'name'  => 'test',
        'inline_style' => '.wp-block-quote.is-style-blue-quote { color: blue; }',
    )
);

The notice added in the PR should be present on the front and the admin area, but you may need to refresh the page first.

Notice: Function WP_Block_Styles_Registry::register was called incorrectly. Block style label must be a string. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/src/wp-includes/functions.php on line 6085
Last edited 11 days ago by poena (previous) (diff)

#5 @poena
11 days ago

  • Milestone changed from Awaiting Review to 6.7

#6 @poena
11 days ago

  • Keywords needs-unit-tests added

#7 @poena
9 days ago

I tried but was not able to figure out how to create a new test to check that if the label is missing, the style is not registered and the doing_it_wrong message is triggered.

Note: See TracTickets for help on using tickets.