wp/wp-includes/block-supports/dimensions.php
changeset 19 3d72ae0968f4
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
       
     1 <?php
       
     2 /**
       
     3  * Dimensions block support flag.
       
     4  *
       
     5  * This does not include the `spacing` block support even though that visually
       
     6  * appears under the "Dimensions" panel in the editor. It remains in its
       
     7  * original `spacing.php` file for compatibility with core.
       
     8  *
       
     9  * @package WordPress
       
    10  * @since 5.9.0
       
    11  */
       
    12 
       
    13 /**
       
    14  * Registers the style block attribute for block types that support it.
       
    15  *
       
    16  * @since 5.9.0
       
    17  * @access private
       
    18  *
       
    19  * @param WP_Block_Type $block_type Block Type.
       
    20  */
       
    21 function wp_register_dimensions_support( $block_type ) {
       
    22 	// Setup attributes and styles within that if needed.
       
    23 	if ( ! $block_type->attributes ) {
       
    24 		$block_type->attributes = array();
       
    25 	}
       
    26 
       
    27 	// Check for existing style attribute definition e.g. from block.json.
       
    28 	if ( array_key_exists( 'style', $block_type->attributes ) ) {
       
    29 		return;
       
    30 	}
       
    31 
       
    32 	$has_dimensions_support = block_has_support( $block_type, array( '__experimentalDimensions' ), false );
       
    33 	// Future block supports such as height & width will be added here.
       
    34 
       
    35 	if ( $has_dimensions_support ) {
       
    36 		$block_type->attributes['style'] = array(
       
    37 			'type' => 'object',
       
    38 		);
       
    39 	}
       
    40 }
       
    41 
       
    42 /**
       
    43  * Adds CSS classes for block dimensions to the incoming attributes array.
       
    44  * This will be applied to the block markup in the front-end.
       
    45  *
       
    46  * @since 5.9.0
       
    47  * @access private
       
    48  *
       
    49  * @param WP_Block_Type $block_type       Block Type.
       
    50  * @param array         $block_attributes Block attributes.
       
    51  * @return array Block dimensions CSS classes and inline styles.
       
    52  */
       
    53 function wp_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
       
    54 	if ( wp_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) {
       
    55 		return array();
       
    56 	}
       
    57 
       
    58 	$styles = array();
       
    59 
       
    60 	// Height support to be added in near future.
       
    61 	// Width support to be added in near future.
       
    62 
       
    63 	return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
       
    64 }
       
    65 
       
    66 // Register the block support.
       
    67 WP_Block_Supports::get_instance()->register(
       
    68 	'dimensions',
       
    69 	array(
       
    70 		'register_attribute' => 'wp_register_dimensions_support',
       
    71 		'apply'              => 'wp_apply_dimensions_support',
       
    72 	)
       
    73 );