From b7a40f1fc115cb71d628c3ea0369cb0417a97dd1 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 11 Oct 2022 18:43:52 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-block-supports.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index a8e2ecb36b..d6ef909af5 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -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'] ) ) {