wp/wp-includes/block-supports/spacing.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Spacing block support flag.
     3  * Spacing block support flag.
       
     4 
       
     5  * For backwards compatibility, this remains separate to the dimensions.php
       
     6  * block support despite both belonging under a single panel in the editor.
     4  *
     7  *
     5  * @package WordPress
     8  * @package WordPress
     6  * @since 5.8.0
     9  * @since 5.8.0
     7  */
    10  */
     8 
    11 
    36  * @since 5.8.0
    39  * @since 5.8.0
    37  * @access private
    40  * @access private
    38  *
    41  *
    39  * @param WP_Block_Type $block_type       Block Type.
    42  * @param WP_Block_Type $block_type       Block Type.
    40  * @param array         $block_attributes Block attributes.
    43  * @param array         $block_attributes Block attributes.
    41  *
       
    42  * @return array Block spacing CSS classes and inline styles.
    44  * @return array Block spacing CSS classes and inline styles.
    43  */
    45  */
    44 function wp_apply_spacing_support( $block_type, $block_attributes ) {
    46 function wp_apply_spacing_support( $block_type, $block_attributes ) {
    45 	$has_padding_support = wp_has_spacing_feature_support( $block_type, 'padding' );
    47 	if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
    46 	$has_margin_support  = wp_has_spacing_feature_support( $block_type, 'margin' );
    48 		return array();
       
    49 	}
       
    50 
       
    51 	$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
       
    52 	$has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
       
    53 	$skip_padding        = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
       
    54 	$skip_margin         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
    47 	$styles              = array();
    55 	$styles              = array();
    48 
    56 
    49 	if ( $has_padding_support ) {
    57 	if ( $has_padding_support && ! $skip_padding ) {
    50 		$padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
    58 		$padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
    51 		if ( null !== $padding_value ) {
    59 		if ( is_array( $padding_value ) ) {
    52 			foreach ( $padding_value as $key => $value ) {
    60 			foreach ( $padding_value as $key => $value ) {
    53 				$styles[] = sprintf( 'padding-%s: %s;', $key, $value );
    61 				$styles[] = sprintf( 'padding-%s: %s;', $key, $value );
    54 			}
    62 			}
       
    63 		} elseif ( null !== $padding_value ) {
       
    64 			$styles[] = sprintf( 'padding: %s;', $padding_value );
    55 		}
    65 		}
    56 	}
    66 	}
    57 
    67 
    58 	if ( $has_margin_support ) {
    68 	if ( $has_margin_support && ! $skip_margin ) {
    59 		$margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
    69 		$margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
    60 		if ( null !== $margin_value ) {
    70 		if ( is_array( $margin_value ) ) {
    61 			foreach ( $margin_value as $key => $value ) {
    71 			foreach ( $margin_value as $key => $value ) {
    62 				$styles[] = sprintf( 'margin-%s: %s;', $key, $value );
    72 				$styles[] = sprintf( 'margin-%s: %s;', $key, $value );
    63 			}
    73 			}
       
    74 		} elseif ( null !== $margin_value ) {
       
    75 			$styles[] = sprintf( 'margin: %s;', $margin_value );
    64 		}
    76 		}
    65 	}
    77 	}
    66 
    78 
    67 	return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
    79 	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 }
    80 }
    87 
    81 
    88 // Register the block support.
    82 // Register the block support.
    89 WP_Block_Supports::get_instance()->register(
    83 WP_Block_Supports::get_instance()->register(
    90 	'spacing',
    84 	'spacing',