[ Index ]

PHP Cross Reference of WordPress

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   * @access private
  15   *
  16   * @param WP_Block_Type $block_type Block Type.
  17   */
  18  function wp_register_border_support( $block_type ) {
  19      // Determine if any border related features are supported.
  20      $has_border_support       = block_has_support( $block_type, array( '__experimentalBorder' ) );
  21      $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
  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_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  29          $block_type->attributes['style'] = array(
  30              'type' => 'object',
  31          );
  32      }
  33  
  34      if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
  35          $block_type->attributes['borderColor'] = array(
  36              'type' => 'string',
  37          );
  38      }
  39  }
  40  
  41  /**
  42   * Adds CSS classes and inline styles for border styles to the incoming
  43   * attributes array. This will be applied to the block markup in the front-end.
  44   *
  45   * @since 5.8.0
  46   * @access private
  47   *
  48   * @param WP_Block_Type $block_type       Block type.
  49   * @param array         $block_attributes Block attributes.
  50   * @return array Border CSS classes and inline styles.
  51   */
  52  function wp_apply_border_support( $block_type, $block_attributes ) {
  53      if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
  54          return array();
  55      }
  56  
  57      $classes = array();
  58      $styles  = array();
  59  
  60      // Border radius.
  61      if (
  62          wp_has_border_feature_support( $block_type, 'radius' ) &&
  63          isset( $block_attributes['style']['border']['radius'] ) &&
  64          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
  65      ) {
  66          $border_radius = $block_attributes['style']['border']['radius'];
  67  
  68          if ( is_array( $border_radius ) ) {
  69              // We have individual border radius corner values.
  70              foreach ( $border_radius as $key => $radius ) {
  71                  // Convert CamelCase corner name to kebab-case.
  72                  $corner   = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
  73                  $styles[] = sprintf( 'border-%s-radius: %s;', $corner, $radius );
  74              }
  75          } else {
  76              // This check handles original unitless implementation.
  77              if ( is_numeric( $border_radius ) ) {
  78                  $border_radius .= 'px';
  79              }
  80  
  81              $styles[] = sprintf( 'border-radius: %s;', $border_radius );
  82          }
  83      }
  84  
  85      // Border style.
  86      if (
  87          wp_has_border_feature_support( $block_type, 'style' ) &&
  88          isset( $block_attributes['style']['border']['style'] ) &&
  89          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
  90      ) {
  91          $border_style = $block_attributes['style']['border']['style'];
  92          $styles[]     = sprintf( 'border-style: %s;', $border_style );
  93      }
  94  
  95      // Border width.
  96      if (
  97          wp_has_border_feature_support( $block_type, 'width' ) &&
  98          isset( $block_attributes['style']['border']['width'] ) &&
  99          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
 100      ) {
 101          $border_width = $block_attributes['style']['border']['width'];
 102  
 103          // This check handles original unitless implementation.
 104          if ( is_numeric( $border_width ) ) {
 105              $border_width .= 'px';
 106          }
 107  
 108          $styles[] = sprintf( 'border-width: %s;', $border_width );
 109      }
 110  
 111      // Border color.
 112      if (
 113          wp_has_border_feature_support( $block_type, 'color' ) &&
 114          ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
 115      ) {
 116          $has_named_border_color  = array_key_exists( 'borderColor', $block_attributes );
 117          $has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
 118  
 119          if ( $has_named_border_color || $has_custom_border_color ) {
 120              $classes[] = 'has-border-color';
 121          }
 122  
 123          if ( $has_named_border_color ) {
 124              $classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] );
 125          } elseif ( $has_custom_border_color ) {
 126              $border_color = $block_attributes['style']['border']['color'];
 127              $styles[]     = sprintf( 'border-color: %s;', $border_color );
 128          }
 129      }
 130  
 131      // Collect classes and styles.
 132      $attributes = array();
 133  
 134      if ( ! empty( $classes ) ) {
 135          $attributes['class'] = implode( ' ', $classes );
 136      }
 137  
 138      if ( ! empty( $styles ) ) {
 139          $attributes['style'] = implode( ' ', $styles );
 140      }
 141  
 142      return $attributes;
 143  }
 144  
 145  /**
 146   * Checks whether the current block type supports the border feature requested.
 147   *
 148   * If the `__experimentalBorder` support flag is a boolean `true` all border
 149   * support features are available. Otherwise, the specific feature's support
 150   * flag nested under `experimentalBorder` must be enabled for the feature
 151   * to be opted into.
 152   *
 153   * @since 5.8.0
 154   * @access private
 155   *
 156   * @param WP_Block_Type $block_type    Block type to check for support.
 157   * @param string        $feature       Name of the feature to check support for.
 158   * @param mixed         $default_value Fallback value for feature support, defaults to false.
 159   * @return bool Whether the feature is supported.
 160   */
 161  function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
 162      // Check if all border support features have been opted into via `"__experimentalBorder": true`.
 163      if (
 164          property_exists( $block_type, 'supports' ) &&
 165          ( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default_value ) )
 166      ) {
 167          return true;
 168      }
 169  
 170      // Check if the specific feature has been opted into individually
 171      // via nested flag under `__experimentalBorder`.
 172      return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
 173  }
 174  
 175  // Register the block support.
 176  WP_Block_Supports::get_instance()->register(
 177      'border',
 178      array(
 179          'register_attribute' => 'wp_register_border_support',
 180          'apply'              => 'wp_apply_border_support',
 181      )
 182  );


Generated: Fri Apr 26 01:00:03 2024 Cross-referenced by PHPXref 0.7.1