wp/wp-includes/block-supports/aria-label.php
changeset 22 8c2e4d02f4ef
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
       
     1 <?php
       
     2 /**
       
     3  * Aria label block support flag.
       
     4  *
       
     5  * @package WordPress
       
     6  * @since 6.8.0
       
     7  */
       
     8 
       
     9 /**
       
    10  * Registers the aria-label block attribute for block types that support it.
       
    11  *
       
    12  * @since 6.8.0
       
    13  * @access private
       
    14  *
       
    15  * @param WP_Block_Type $block_type Block Type.
       
    16  */
       
    17 function wp_register_aria_label_support( $block_type ) {
       
    18 	$has_aria_label_support = block_has_support( $block_type, array( 'ariaLabel' ), false );
       
    19 
       
    20 	if ( ! $has_aria_label_support ) {
       
    21 		return;
       
    22 	}
       
    23 
       
    24 	if ( ! $block_type->attributes ) {
       
    25 		$block_type->attributes = array();
       
    26 	}
       
    27 
       
    28 	if ( ! array_key_exists( 'ariaLabel', $block_type->attributes ) ) {
       
    29 		$block_type->attributes['ariaLabel'] = array(
       
    30 			'type' => 'string',
       
    31 		);
       
    32 	}
       
    33 }
       
    34 
       
    35 /**
       
    36  * Add the aria-label to the output.
       
    37  *
       
    38  * @since 6.8.0
       
    39  * @access private
       
    40  *
       
    41  * @param WP_Block_Type $block_type       Block Type.
       
    42  * @param array         $block_attributes Block attributes.
       
    43  *
       
    44  * @return array Block aria-label.
       
    45  */
       
    46 function wp_apply_aria_label_support( $block_type, $block_attributes ) {
       
    47 	if ( ! $block_attributes ) {
       
    48 		return array();
       
    49 	}
       
    50 
       
    51 	$has_aria_label_support = block_has_support( $block_type, array( 'ariaLabel' ), false );
       
    52 	if ( ! $has_aria_label_support ) {
       
    53 		return array();
       
    54 	}
       
    55 
       
    56 	$has_aria_label = array_key_exists( 'ariaLabel', $block_attributes );
       
    57 	if ( ! $has_aria_label ) {
       
    58 		return array();
       
    59 	}
       
    60 	return array( 'aria-label' => $block_attributes['ariaLabel'] );
       
    61 }
       
    62 
       
    63 // Register the block support.
       
    64 WP_Block_Supports::get_instance()->register(
       
    65 	'aria-label',
       
    66 	array(
       
    67 		'register_attribute' => 'wp_register_aria_label_support',
       
    68 		'apply'              => 'wp_apply_aria_label_support',
       
    69 	)
       
    70 );