border.php 6.1 KB

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