[ 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   * @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. Default false.
  79   * @return bool Whether the feature is supported.
  80   */
  81  function wp_has_spacing_feature_support( $block_type, $feature, $default = false ) {
  82      // Check if the specific feature has been opted into individually
  83      // via nested flag under `spacing`.
  84      return block_has_support( $block_type, array( 'spacing', $feature ), $default );
  85  }
  86  
  87  // Register the block support.
  88  WP_Block_Supports::get_instance()->register(
  89      'spacing',
  90      array(
  91          'register_attribute' => 'wp_register_spacing_support',
  92          'apply'              => 'wp_apply_spacing_support',
  93      )
  94  );


Generated: Mon Sep 20 01:00:04 2021 Cross-referenced by PHPXref 0.7.1