[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Border block support flag.
   4   *
   5   * @package WordPress
   6   * @since 5.8.0
   7   */
   8  
   9  /**
  10   * Registers the style attribute used by the border feature if needed for block
  11   * types that support borders.
  12   *
  13   * @since 5.8.0
  14   * @since 6.1.0 Improved conditional blocks optimization.
  15   * @access private
  16   *
  17   * @param WP_Block_Type $block_type Block Type.
  18   */
  19  function wp_register_border_support( $block_type ) {
  20      // Setup attributes and styles within that if needed.
  21      if ( ! $block_type->attributes ) {
  22          $block_type->attributes = array();
  23      }
  24  
  25      if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
  26          $block_type->attributes['style'] = array(
  27              'type' => 'object',
  28          );
  29      }
  30  
  31      if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
  32          $block_type->attributes['borderColor'] = array(
  33              'type' => 'string',
  34          );
  35      }
  36  }
  37  
  38  /**
  39   * Adds CSS classes and inline styles for border styles to the incoming
  40   * attributes array. This will be applied to the block markup in the front-end.
  41   *
  42   * @since 5.8.0
  43   * @since 6.1.0 Implemented the style engine to generate CSS and classnames.
  44   * @access private
  45   *
  46   * @param WP_Block_Type $block_type       Block type.
  47   * @param array         $block_attributes Block attributes.
  48   * @return array Border CSS classes and inline styles.
  49   */
  50  function wp_apply_border_support( $block_type, $block_attributes ) {
  51      if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
  52          return array();
  53      }
  54  
  55      $border_block_styles      = array();
  56      $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
  57      $has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
  58  
  59      // Border radius.
  60      if (
  61          wp_has_border_feature_support( $block_type, 'radius' ) &&
  62          isset( $block_attributes['style']['border']['radius'] ) &&
  63          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
  64      ) {
  65          $border_radius = $block_attributes['style']['border']['radius'];
  66  
  67          if ( is_numeric( $border_radius ) ) {
  68              $border_radius .= 'px';
  69          }
  70  
  71          $border_block_styles['radius'] = $border_radius;
  72      }
  73  
  74      // Border style.
  75      if (
  76          wp_has_border_feature_support( $block_type, 'style' ) &&
  77          isset( $block_attributes['style']['border']['style'] ) &&
  78          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
  79      ) {
  80          $border_block_styles['style'] = $block_attributes['style']['border']['style'];
  81      }
  82  
  83      // Border width.
  84      if (
  85          $has_border_width_support &&
  86          isset( $block_attributes['style']['border']['width'] ) &&
  87          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
  88      ) {
  89          $border_width = $block_attributes['style']['border']['width'];
  90  
  91          // This check handles original unitless implementation.
  92          if ( is_numeric( $border_width ) ) {
  93              $border_width .= 'px';
  94          }
  95  
  96          $border_block_styles['width'] = $border_width;
  97      }
  98  
  99      // Border color.
 100      if (
 101          $has_border_color_support &&
 102          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
 103      ) {
 104          $preset_border_color          = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
 105          $custom_border_color          = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null;
 106          $border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
 107      }
 108  
 109      // Generates styles for individual border sides.
 110      if ( $has_border_color_support || $has_border_width_support ) {
 111          foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
 112              $border                       = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null;
 113              $border_side_values           = array(
 114                  'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
 115                  'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
 116                  'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
 117              );
 118              $border_block_styles[ $side ] = $border_side_values;
 119          }
 120      }
 121  
 122      // Collect classes and styles.
 123      $attributes = array();
 124      $styles     = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
 125  
 126      if ( ! empty( $styles['classnames'] ) ) {
 127          $attributes['class'] = $styles['classnames'];
 128      }
 129  
 130      if ( ! empty( $styles['css'] ) ) {
 131          $attributes['style'] = $styles['css'];
 132      }
 133  
 134      return $attributes;
 135  }
 136  
 137  /**
 138   * Checks whether the current block type supports the border feature requested.
 139   *
 140   * If the `__experimentalBorder` support flag is a boolean `true` all border
 141   * support features are available. Otherwise, the specific feature's support
 142   * flag nested under `experimentalBorder` must be enabled for the feature
 143   * to be opted into.
 144   *
 145   * @since 5.8.0
 146   * @access private
 147   *
 148   * @param WP_Block_Type $block_type    Block type to check for support.
 149   * @param string        $feature       Name of the feature to check support for.
 150   * @param mixed         $default_value Fallback value for feature support, defaults to false.
 151   * @return bool Whether the feature is supported.
 152   */
 153  function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
 154      // Check if all border support features have been opted into via `"__experimentalBorder": true`.
 155      if ( $block_type instanceof WP_Block_Type ) {
 156          $block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] )
 157              ? $block_type->supports['__experimentalBorder']
 158              : $default_value;
 159          if ( true === $block_type_supports_border ) {
 160              return true;
 161          }
 162      }
 163  
 164      // Check if the specific feature has been opted into individually
 165      // via nested flag under `__experimentalBorder`.
 166      return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
 167  }
 168  
 169  // Register the block support.
 170  WP_Block_Supports::get_instance()->register(
 171      'border',
 172      array(
 173          'register_attribute' => 'wp_register_border_support',
 174          'apply'              => 'wp_apply_border_support',
 175      )
 176  );


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref