Editor: Fix PHP notice in WP_Block_Supports when no attributes declared.

A check is added to verify if the block's `'attrs'` key exists before accessing it. If no, it defaults to an empty array.

How was this found?
WP 6.1 removed the attributes from the `core/page-list` block. When this block is being processed by `WP_Block_Supports::apply_block_supports()`, the key `'attrs'` does not exist in `self::$block_to_render`.

This commit includes a tiny micro-optimization by moving this line of code after the existing guard clause. Why? If the guard clause is triggered, the method bails out early, meaning the attributes code is not used. By moving it after the guard clause, it saves a tiny bit of memory and processing if this happens.

Follow-up to [49310], [54399].

Props petitphp, spacedmonkey, hellofromTonya.
Fixes #56799.

git-svn-id: https://develop.svn.wordpress.org/trunk@54498 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork 2022-10-11 18:43:52 +00:00
parent a874e5fe5e
commit b7a40f1fc1

View File

@ -94,8 +94,7 @@ class WP_Block_Supports {
* @return string[] Array of HTML attributes.
*/
public function apply_block_supports() {
$block_attributes = self::$block_to_render['attrs'];
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
self::$block_to_render['blockName']
);
@ -104,6 +103,10 @@ class WP_Block_Supports {
return array();
}
$block_attributes = array_key_exists( 'attrs', self::$block_to_render )
? self::$block_to_render['attrs']
: array();
$output = array();
foreach ( $this->block_supports as $block_support_config ) {
if ( ! isset( $block_support_config['apply'] ) ) {