Skip to content

Commit

Permalink
Block support: Add server-side processing for ariaLabel (WordPress#69096
Browse files Browse the repository at this point in the history
)

* Block support: Add server-side processing for ariaLabel

* Add backport changelog

* Add unit test

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: joemcgill <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
  • Loading branch information
7 people authored Feb 25, 2025
1 parent 77da6c2 commit 3e6ddf9
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8274.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8274

* https://github.com/WordPress/gutenberg/pull/69096
63 changes: 63 additions & 0 deletions lib/block-supports/aria-label.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Aria label block support flag.
*
* @package gutenberg
*/

/**
* Registers the aria-label block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_aria_label_support( $block_type ) {
$has_aria_label_support = block_has_support( $block_type, array( 'ariaLabel' ), false );

if ( ! $has_aria_label_support ) {
return;
}

if ( ! $block_type->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.
*
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block aria-label.
*/
function gutenberg_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' => 'gutenberg_register_aria_label_support',
'apply' => 'gutenberg_apply_aria_label_support',
)
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/shadow.php';
require __DIR__ . '/block-supports/background.php';
require __DIR__ . '/block-supports/block-style-variations.php';
require __DIR__ . '/block-supports/aria-label.php';

// Data views.
require_once __DIR__ . '/experimental/data-views.php';
Expand Down
85 changes: 85 additions & 0 deletions phpunit/block-supports/aria-label-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Test the aria-label block support.
*
* @package Gutenberg
*/
class WP_Block_Supports_Aria_Label_Test extends WP_UnitTestCase {
/**
* @var string|null
*/
private $test_block_name;

public function set_up() {
parent::set_up();
$this->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.
*
* @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_gutenberg_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 = gutenberg_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(),
),
);
}
}

0 comments on commit 3e6ddf9

Please sign in to comment.