Block support: Add server-side processing for ariaLabel.

Adds server-side registration for `ariaLabel` block support and its required fields. Fully enabling feature support for dynamic blocks and consumers using `ServerSideRender` component.

Props wildworks, fabiankaegy, joemcgill, poena.
Fixes #62919.
Built from https://develop.svn.wordpress.org/trunk@59925


git-svn-id: http://core.svn.wordpress.org/trunk@59267 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Mamaduka 2025-03-04 13:06:27 +00:00
parent a9ea0e8be5
commit 07ede8e6a2
4 changed files with 73 additions and 2 deletions

View File

@ -0,0 +1,70 @@
<?php
/**
* Aria label block support flag.
*
* @package WordPress
* @since 6.8.0
*/
/**
* Registers the aria-label block attribute for block types that support it.
*
* @since 6.8.0
* @access private
*
* @param WP_Block_Type $block_type Block Type.
*/
function wp_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.
*
* @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',
)
);

View File

@ -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 ] ) ) {

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59924';
$wp_version = '6.8-alpha-59925';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -387,6 +387,7 @@ require ABSPATH . WPINC . '/block-supports/duotone.php';
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';