From 0454811ff4f804699577a59a12f04366de177379 Mon Sep 17 00:00:00 2001 From: Aki Hamano Date: Fri, 7 Feb 2025 23:18:56 +0900 Subject: [PATCH 1/3] Block support: Add server-side processing for ariaLabel --- src/wp-includes/block-supports/aria-label.php | 70 +++++++++++++++ src/wp-includes/class-wp-block-supports.php | 2 +- src/wp-settings.php | 1 + .../tests/block-supports/aria-label.php | 87 +++++++++++++++++++ .../phpunit/tests/blocks/supportedStyles.php | 43 +++++++++ 5 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 src/wp-includes/block-supports/aria-label.php create mode 100644 tests/phpunit/tests/block-supports/aria-label.php diff --git a/src/wp-includes/block-supports/aria-label.php b/src/wp-includes/block-supports/aria-label.php new file mode 100644 index 0000000000000..15a25cb27164d --- /dev/null +++ b/src/wp-includes/block-supports/aria-label.php @@ -0,0 +1,70 @@ +attributes ) { + $block_type->attributes = array(); + } + + if ( ! array_key_exists( 'ariaLabel', $block_type->attributes ) ) { + $block_type->attributes['ariaLabel'] = array( + 'type' => 'string', + ); + } +} + +/** + * Add the aria-label to the output. + * + * @since 6.8.0 + * @access private + * + * @param WP_Block_Type $block_type Block Type. + * @param array $block_attributes Block attributes. + * + * @return array Block aria-label. + */ +function wp_apply_aria_label_support( $block_type, $block_attributes ) { + if ( ! $block_attributes ) { + return array(); + } + + $has_aria_label_support = block_has_support( $block_type, array( 'ariaLabel' ), false ); + if ( ! $has_aria_label_support ) { + return array(); + } + + $has_aria_label = array_key_exists( 'ariaLabel', $block_attributes ); + if ( ! $has_aria_label ) { + return array(); + } + return array( 'aria-label' => $block_attributes['ariaLabel'] ); +} + +// Register the block support. +WP_Block_Supports::get_instance()->register( + 'aria-label', + array( + 'register_attribute' => 'wp_register_aria_label_support', + 'apply' => 'wp_apply_aria_label_support', + ) +); diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 71d6b4969145c..ec5bc9c8d6846 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -181,7 +181,7 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { // This is hardcoded on purpose. // We only support a fixed list of attributes. - $attributes_to_merge = array( 'style', 'class', 'id' ); + $attributes_to_merge = array( 'style', 'class', 'id', 'aria-label' ); $attributes = array(); foreach ( $attributes_to_merge as $attribute_name ) { if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) { diff --git a/src/wp-settings.php b/src/wp-settings.php index ec08ff11498f3..25924588793cc 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -386,6 +386,7 @@ require ABSPATH . WPINC . '/block-supports/shadow.php'; require ABSPATH . WPINC . '/block-supports/background.php'; require ABSPATH . WPINC . '/block-supports/block-style-variations.php'; +require ABSPATH . WPINC . '/block-supports/aria-label.php'; require ABSPATH . WPINC . '/style-engine.php'; require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php'; require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php'; diff --git a/tests/phpunit/tests/block-supports/aria-label.php b/tests/phpunit/tests/block-supports/aria-label.php new file mode 100644 index 0000000000000..d8ef4a57daf2e --- /dev/null +++ b/tests/phpunit/tests/block-supports/aria-label.php @@ -0,0 +1,87 @@ +test_block_name = null; + } + + public function tear_down() { + unregister_block_type( $this->test_block_name ); + $this->test_block_name = null; + parent::tear_down(); + } + + /** + * Registers a new block for testing aria-label support. + * + * @param string $block_name Name for the test block. + * @param array $supports Array defining block support configuration. + * + * @return WP_Block_Type The block type for the newly registered test block. + */ + private function register_aria_label_block_with_support( $block_name, $supports = array() ) { + $this->test_block_name = $block_name; + register_block_type( + $this->test_block_name, + array( + 'api_version' => 3, + 'supports' => $supports, + ) + ); + $registry = WP_Block_Type_Registry::get_instance(); + + return $registry->get_registered( $this->test_block_name ); + } + + /** + * Tests that position block support works as expected. + * + * @ticket 62919 + * + * @dataProvider data_aria_label_block_support + * + * @param boolean|array $support Aria label block support configuration. + * @param string $value Aria label value for attribute object. + * @param array $expected Expected aria label block support styles. + */ + public function test_wp_apply_aria_label_support( $support, $value, $expected ) { + $block_type = self::register_aria_label_block_with_support( + 'test/aria-label-block', + array( 'ariaLabel' => $support ) + ); + $block_attrs = array( 'ariaLabel' => $value ); + $actual = wp_apply_aria_label_support( $block_type, $block_attrs ); + + $this->assertSame( $expected, $actual ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_aria_label_block_support() { + return array( + 'aria-label attribute is applied' => array( + 'support' => true, + 'value' => 'Label', + 'expected' => array( 'aria-label' => 'Label' ), + ), + 'aria-label attribute is not applied if block does not support it' => array( + 'support' => false, + 'value' => 'Label', + 'expected' => array(), + ), + ); + } +} diff --git a/tests/phpunit/tests/blocks/supportedStyles.php b/tests/phpunit/tests/blocks/supportedStyles.php index dcd029ad274b9..c733b7218d8c5 100644 --- a/tests/phpunit/tests/blocks/supportedStyles.php +++ b/tests/phpunit/tests/blocks/supportedStyles.php @@ -151,6 +151,24 @@ private function assert_content_and_styles_and_classes_match( $block, $expected_ ); } + /** + * Runs assertions that the rendered output has expected content and aria-label attr. + * + * @param array $block Block to render. + * @param string $expected_aria_label Expected output aria-label attr string. + */ + private function assert_content_and_aria_label_match( $block, $expected_aria_label ) { + $styled_block = $this->render_example_block( $block ); + $content = $this->get_content_from_block( $styled_block ); + + $this->assertSame( self::BLOCK_CONTENT, $content, 'Block content does not match expected content' ); + $this->assertSame( + $expected_aria_label, + $this->get_attribute_from_block( 'aria-label', $styled_block ), + 'Aria-label does not match expected aria-label' + ); + } + /** * Tests color support for named color support for named colors. */ @@ -685,6 +703,31 @@ public function test_generated_classnames_support_opt_out() { $this->assert_content_and_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests aria-label server-side block support. + */ + public function test_aria_label_support() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + 'ariaLabel' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'ariaLabel' => 'Label', + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $this->assert_content_and_aria_label_match( $block, 'Label' ); + } + /** * Ensures libxml_internal_errors is being used instead of @ warning suppression */ From d45bdd841cf006aab302911441cf77df0e42fce5 Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Tue, 4 Mar 2025 17:46:06 +0900 Subject: [PATCH 2/3] Fix comment indentation Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/block-supports/aria-label.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/aria-label.php b/src/wp-includes/block-supports/aria-label.php index 15a25cb27164d..aa559bcacd967 100644 --- a/src/wp-includes/block-supports/aria-label.php +++ b/src/wp-includes/block-supports/aria-label.php @@ -38,7 +38,7 @@ function wp_register_aria_label_support( $block_type ) { * @since 6.8.0 * @access private * - * @param WP_Block_Type $block_type Block Type. + * @param WP_Block_Type $block_type Block Type. * @param array $block_attributes Block attributes. * * @return array Block aria-label. From 1743783c9ad21b7d5a2d70577dd1cee844b5110d Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Tue, 4 Mar 2025 17:46:13 +0900 Subject: [PATCH 3/3] Fix comment indentation Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- tests/phpunit/tests/block-supports/aria-label.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/tests/block-supports/aria-label.php b/tests/phpunit/tests/block-supports/aria-label.php index d8ef4a57daf2e..087f4843cebb0 100644 --- a/tests/phpunit/tests/block-supports/aria-label.php +++ b/tests/phpunit/tests/block-supports/aria-label.php @@ -50,9 +50,9 @@ private function register_aria_label_block_with_support( $block_name, $supports * * @dataProvider data_aria_label_block_support * - * @param boolean|array $support Aria label block support configuration. - * @param string $value Aria label value for attribute object. - * @param array $expected Expected aria label block support styles. + * @param boolean|array $support Aria label block support configuration. + * @param string $value Aria label value for attribute object. + * @param array $expected Expected aria label block support styles. */ public function test_wp_apply_aria_label_support( $support, $value, $expected ) { $block_type = self::register_aria_label_block_with_support(