wp/wp-includes/block-supports/spacing.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     1 <?php
       
     2 /**
       
     3  * Spacing block support flag.
       
     4  *
       
     5  * @package WordPress
       
     6  * @since 5.8.0
       
     7  */
       
     8 
       
     9 /**
       
    10  * Registers the style block attribute for block types that support it.
       
    11  *
       
    12  * @since 5.8.0
       
    13  * @access private
       
    14  *
       
    15  * @param WP_Block_Type $block_type Block Type.
       
    16  */
       
    17 function wp_register_spacing_support( $block_type ) {
       
    18 	$has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
       
    19 
       
    20 	// Setup attributes and styles within that if needed.
       
    21 	if ( ! $block_type->attributes ) {
       
    22 		$block_type->attributes = array();
       
    23 	}
       
    24 
       
    25 	if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
       
    26 		$block_type->attributes['style'] = array(
       
    27 			'type' => 'object',
       
    28 		);
       
    29 	}
       
    30 }
       
    31 
       
    32 /**
       
    33  * Add CSS classes for block spacing to the incoming attributes array.
       
    34  * This will be applied to the block markup in the front-end.
       
    35  *
       
    36  * @since 5.8.0
       
    37  * @access private
       
    38  *
       
    39  * @param WP_Block_Type $block_type       Block Type.
       
    40  * @param array         $block_attributes Block attributes.
       
    41  *
       
    42  * @return array Block spacing CSS classes and inline styles.
       
    43  */
       
    44 function wp_apply_spacing_support( $block_type, $block_attributes ) {
       
    45 	$has_padding_support = wp_has_spacing_feature_support( $block_type, 'padding' );
       
    46 	$has_margin_support  = wp_has_spacing_feature_support( $block_type, 'margin' );
       
    47 	$styles              = array();
       
    48 
       
    49 	if ( $has_padding_support ) {
       
    50 		$padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
       
    51 		if ( null !== $padding_value ) {
       
    52 			foreach ( $padding_value as $key => $value ) {
       
    53 				$styles[] = sprintf( 'padding-%s: %s;', $key, $value );
       
    54 			}
       
    55 		}
       
    56 	}
       
    57 
       
    58 	if ( $has_margin_support ) {
       
    59 		$margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
       
    60 		if ( null !== $margin_value ) {
       
    61 			foreach ( $margin_value as $key => $value ) {
       
    62 				$styles[] = sprintf( 'margin-%s: %s;', $key, $value );
       
    63 			}
       
    64 		}
       
    65 	}
       
    66 
       
    67 	return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
       
    68 }
       
    69 
       
    70 /**
       
    71  * Checks whether the current block type supports the spacing feature requested.
       
    72  *
       
    73  * @since 5.8.0
       
    74  * @access private
       
    75  *
       
    76  * @param WP_Block_Type $block_type Block type to check for support.
       
    77  * @param string        $feature    Name of the feature to check support for.
       
    78  * @param mixed         $default    Fallback value for feature support, defaults to false.
       
    79  *
       
    80  * @return boolean                  Whether or not the feature is supported.
       
    81  */
       
    82 function wp_has_spacing_feature_support( $block_type, $feature, $default = false ) {
       
    83 	// Check if the specific feature has been opted into individually
       
    84 	// via nested flag under `spacing`.
       
    85 	return block_has_support( $block_type, array( 'spacing', $feature ), $default );
       
    86 }
       
    87 
       
    88 // Register the block support.
       
    89 WP_Block_Supports::get_instance()->register(
       
    90 	'spacing',
       
    91 	array(
       
    92 		'register_attribute' => 'wp_register_spacing_support',
       
    93 		'apply'              => 'wp_apply_spacing_support',
       
    94 	)
       
    95 );