[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/block-supports/ -> spacing.php (source)

   1  <?php
   2  /**
   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.
   7   *
   8   * @package WordPress
   9   * @since 5.8.0
  10   */
  11  
  12  /**
  13   * Registers the style block attribute for block types that support it.
  14   *
  15   * @since 5.8.0
  16   * @access private
  17   *
  18   * @param WP_Block_Type $block_type Block Type.
  19   */
  20  function wp_register_spacing_support( $block_type ) {
  21      $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false );
  22  
  23      // Setup attributes and styles within that if needed.
  24      if ( ! $block_type->attributes ) {
  25          $block_type->attributes = array();
  26      }
  27  
  28      if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  29          $block_type->attributes['style'] = array(
  30              'type' => 'object',
  31          );
  32      }
  33  }
  34  
  35  /**
  36   * Add CSS classes for block spacing to the incoming attributes array.
  37   * This will be applied to the block markup in the front-end.
  38   *
  39   * @since 5.8.0
  40   * @access private
  41   *
  42   * @param WP_Block_Type $block_type       Block Type.
  43   * @param array         $block_attributes Block attributes.
  44   * @return array Block spacing CSS classes and inline styles.
  45   */
  46  function wp_apply_spacing_support( $block_type, $block_attributes ) {
  47      if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
  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' );
  55      $styles              = array();
  56  
  57      if ( $has_padding_support && ! $skip_padding ) {
  58          $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
  59          if ( is_array( $padding_value ) ) {
  60              foreach ( $padding_value as $key => $value ) {
  61                  $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
  62              }
  63          } elseif ( null !== $padding_value ) {
  64              $styles[] = sprintf( 'padding: %s;', $padding_value );
  65          }
  66      }
  67  
  68      if ( $has_margin_support && ! $skip_margin ) {
  69          $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
  70          if ( is_array( $margin_value ) ) {
  71              foreach ( $margin_value as $key => $value ) {
  72                  $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
  73              }
  74          } elseif ( null !== $margin_value ) {
  75              $styles[] = sprintf( 'margin: %s;', $margin_value );
  76          }
  77      }
  78  
  79      return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
  80  }
  81  
  82  // Register the block support.
  83  WP_Block_Supports::get_instance()->register(
  84      'spacing',
  85      array(
  86          'register_attribute' => 'wp_register_spacing_support',
  87          'apply'              => 'wp_apply_spacing_support',
  88      )
  89  );


Generated: Tue Apr 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1