wp_apply_spacing_support() – Add CSS classes for block spacing to the incoming attributes array.

You appear to be a bot. Output may be restricted

Description

Add CSS classes for block spacing to the incoming attributes array.

This will be applied to the block markup in the front-end.

Usage

$array = wp_apply_spacing_support( $block_type, $block_attributes );

Parameters

$block_type
( WP_Block_Type ) required – Block Type.
$block_attributes
( array ) required – Block attributes.

Returns

array Block spacing CSS classes and inline styles.

Source

File name: wordpress/wp-includes/block-supports/spacing.php


Lines:

1 to 36 of 36
function wp_apply_spacing_support( $block_type, $block_attributes ) {
  if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
    return array();
  }

  $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
  $has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
  $skip_padding        = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
  $skip_margin         = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
  $styles              = array();

  if ( $has_padding_support && ! $skip_padding ) {
    $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
    if ( is_array( $padding_value ) ) {
      foreach ( $padding_value as $key => $value ) {
        $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
      }
    } elseif ( null !== $padding_value ) {
      $styles[] = sprintf( 'padding: %s;', $padding_value );
    }
  }

  if ( $has_margin_support && ! $skip_margin ) {
    $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
    if ( is_array( $margin_value ) ) {
      foreach ( $margin_value as $key => $value ) {
        $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
      }
    } elseif ( null !== $margin_value ) {
      $styles[] = sprintf( 'margin: %s;', $margin_value );
    }
  }

  return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
}
 

 View on GitHub View on Trac