[ 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      $has_border_color_support  = wp_has_border_feature_support( $block_type, 'color' );
  56      $has_border_radius_support = wp_has_border_feature_support( $block_type, 'radius' );
  57      $has_border_style_support  = wp_has_border_feature_support( $block_type, 'style' );
  58      $has_border_width_support  = wp_has_border_feature_support( $block_type, 'width' );
  59      $skip_color                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' );
  60      $skip_radius               = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' );
  61      $skip_style                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' );
  62      $skip_width                = wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' );
  63  
  64      // The unset() calls below fail on a non-array style or border.
  65      if ( ! isset( $block_attributes['style']['border'] ) || ! is_array( $block_attributes['style']['border'] ) ) {
  66          unset( $block_attributes['style'] );
  67      }
  68  
  69      if ( ! $has_border_radius_support || $skip_radius ) {
  70          unset( $block_attributes['style']['border']['radius'] );
  71      }
  72  
  73      if ( ! $has_border_style_support || $skip_style ) {
  74          unset( $block_attributes['style']['border']['style'] );
  75      }
  76  
  77      if ( ! $has_border_width_support || $skip_width ) {
  78          unset( $block_attributes['style']['border']['width'] );
  79      }
  80  
  81      if ( ! $has_border_color_support || $skip_color ) {
  82          unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] );
  83      }
  84  
  85      // Side values follow skipped serialization only, as on trunk.
  86      foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
  87          $has_side = isset( $block_attributes['style']['border'][ $side ] ) && is_array( $block_attributes['style']['border'][ $side ] );
  88  
  89          if ( ! $has_side || ( ! $has_border_color_support && ! $has_border_width_support ) ) {
  90              unset( $block_attributes['style']['border'][ $side ] );
  91              continue;
  92          }
  93  
  94          if ( $skip_width ) {
  95              unset( $block_attributes['style']['border'][ $side ]['width'] );
  96          }
  97  
  98          if ( $skip_color ) {
  99              unset( $block_attributes['style']['border'][ $side ]['color'] );
 100          }
 101  
 102          if ( $skip_style ) {
 103              unset( $block_attributes['style']['border'][ $side ]['style'] );
 104          }
 105      }
 106  
 107      return wp_get_border_classes_and_styles( $block_attributes );
 108  }
 109  
 110  /**
 111   * Returns border classes and inline styles for block attributes, like the JS
 112   * `getBorderClassesAndStyles()`. Does not check block support or skipped
 113   * serialization.
 114   *
 115   * @since 7.3.0
 116   *
 117   * @param array<string, mixed> $block_attributes Block attributes.
 118   * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
 119   *
 120   * @phpstan-param array{ style?: array<string, mixed>, ...<string, mixed> } $block_attributes
 121   * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
 122   */
 123  function wp_get_border_classes_and_styles( $block_attributes ) {
 124      if ( ! is_array( $block_attributes ) ) {
 125          return array();
 126      }
 127  
 128      $border        = isset( $block_attributes['style']['border'] ) && is_array( $block_attributes['style']['border'] )
 129          ? $block_attributes['style']['border']
 130          : array();
 131      $border_styles = array();
 132  
 133      // Unitless radius and width are from the original implementation.
 134      if ( isset( $border['radius'] ) ) {
 135          $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
 136      }
 137  
 138      if ( isset( $border['style'] ) ) {
 139          $border_styles['style'] = $border['style'];
 140      }
 141  
 142      if ( isset( $border['width'] ) ) {
 143          $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
 144      }
 145  
 146      // Preset slugs are strings in theme.json and the editor.
 147      $preset_slug            = $block_attributes['borderColor'] ?? null;
 148      $border_styles['color'] = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|color|{$preset_slug}" : ( $border['color'] ?? null );
 149  
 150      foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
 151          if ( ! isset( $border[ $side ] ) || ! is_array( $border[ $side ] ) ) {
 152              continue;
 153          }
 154          $border_styles[ $side ] = array(
 155              'width' => $border[ $side ]['width'] ?? null,
 156              'color' => $border[ $side ]['color'] ?? null,
 157              'style' => $border[ $side ]['style'] ?? null,
 158          );
 159      }
 160  
 161      $attributes = array();
 162      $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
 163  
 164      if ( ! empty( $styles['classnames'] ) ) {
 165          $attributes['class'] = $styles['classnames'];
 166      }
 167  
 168      if ( ! empty( $styles['css'] ) ) {
 169          $attributes['style'] = $styles['css'];
 170      }
 171  
 172      return $attributes;
 173  }
 174  
 175  /**
 176   * Checks whether the current block type supports the border feature requested.
 177   *
 178   * If the `__experimentalBorder` support flag is a boolean `true` all border
 179   * support features are available. Otherwise, the specific feature's support
 180   * flag nested under `experimentalBorder` must be enabled for the feature
 181   * to be opted into.
 182   *
 183   * @since 5.8.0
 184   * @access private
 185   *
 186   * @param WP_Block_Type $block_type    Block type to check for support.
 187   * @param string        $feature       Name of the feature to check support for.
 188   * @param mixed         $default_value Fallback value for feature support, defaults to false.
 189   * @return bool Whether the feature is supported.
 190   */
 191  function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
 192      // Check if all border support features have been opted into via `"__experimentalBorder": true`.
 193      if ( $block_type instanceof WP_Block_Type ) {
 194          $block_type_supports_border = $block_type->supports['__experimentalBorder'] ?? $default_value;
 195          if ( true === $block_type_supports_border ) {
 196              return true;
 197          }
 198      }
 199  
 200      // Check if the specific feature has been opted into individually
 201      // via nested flag under `__experimentalBorder`.
 202      return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
 203  }
 204  
 205  // Register the block support.
 206  WP_Block_Supports::get_instance()->register(
 207      'border',
 208      array(
 209          'register_attribute' => 'wp_register_border_support',
 210          'apply'              => 'wp_apply_border_support',
 211      )
 212  );


Generated : Mon Nov 30 08:20:01 2026 Cross-referenced by PHPXref