Sin descripción

border.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * @access private
  14. *
  15. * @param WP_Block_Type $block_type Block Type.
  16. */
  17. function wp_register_border_support( $block_type ) {
  18. // Determine if any border related features are supported.
  19. $has_border_support = block_has_support( $block_type, array( '__experimentalBorder' ) );
  20. $has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
  21. // Setup attributes and styles within that if needed.
  22. if ( ! $block_type->attributes ) {
  23. $block_type->attributes = array();
  24. }
  25. if ( $has_border_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  26. $block_type->attributes['style'] = array(
  27. 'type' => 'object',
  28. );
  29. }
  30. if ( $has_border_color_support && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
  31. $block_type->attributes['borderColor'] = array(
  32. 'type' => 'string',
  33. );
  34. }
  35. }
  36. /**
  37. * Adds CSS classes and inline styles for border styles to the incoming
  38. * attributes array. This will be applied to the block markup in the front-end.
  39. *
  40. * @since 5.8.0
  41. * @access private
  42. *
  43. * @param WP_Block_Type $block_type Block type.
  44. * @param array $block_attributes Block attributes.
  45. *
  46. * @return array Border CSS classes and inline styles.
  47. */
  48. function wp_apply_border_support( $block_type, $block_attributes ) {
  49. if ( wp_skip_border_serialization( $block_type ) ) {
  50. return array();
  51. }
  52. $classes = array();
  53. $styles = array();
  54. // Border radius.
  55. if (
  56. wp_has_border_feature_support( $block_type, 'radius' ) &&
  57. isset( $block_attributes['style']['border']['radius'] )
  58. ) {
  59. $border_radius = (int) $block_attributes['style']['border']['radius'];
  60. $styles[] = sprintf( 'border-radius: %dpx;', $border_radius );
  61. }
  62. // Border style.
  63. if (
  64. wp_has_border_feature_support( $block_type, 'style' ) &&
  65. isset( $block_attributes['style']['border']['style'] )
  66. ) {
  67. $border_style = $block_attributes['style']['border']['style'];
  68. $styles[] = sprintf( 'border-style: %s;', $border_style );
  69. }
  70. // Border width.
  71. if (
  72. wp_has_border_feature_support( $block_type, 'width' ) &&
  73. isset( $block_attributes['style']['border']['width'] )
  74. ) {
  75. $border_width = intval( $block_attributes['style']['border']['width'] );
  76. $styles[] = sprintf( 'border-width: %dpx;', $border_width );
  77. }
  78. // Border color.
  79. if ( wp_has_border_feature_support( $block_type, 'color' ) ) {
  80. $has_named_border_color = array_key_exists( 'borderColor', $block_attributes );
  81. $has_custom_border_color = isset( $block_attributes['style']['border']['color'] );
  82. if ( $has_named_border_color || $has_custom_border_color ) {
  83. $classes[] = 'has-border-color';
  84. }
  85. if ( $has_named_border_color ) {
  86. $classes[] = sprintf( 'has-%s-border-color', $block_attributes['borderColor'] );
  87. } elseif ( $has_custom_border_color ) {
  88. $border_color = $block_attributes['style']['border']['color'];
  89. $styles[] = sprintf( 'border-color: %s;', $border_color );
  90. }
  91. }
  92. // Collect classes and styles.
  93. $attributes = array();
  94. if ( ! empty( $classes ) ) {
  95. $attributes['class'] = implode( ' ', $classes );
  96. }
  97. if ( ! empty( $styles ) ) {
  98. $attributes['style'] = implode( ' ', $styles );
  99. }
  100. return $attributes;
  101. }
  102. /**
  103. * Checks whether serialization of the current block's border properties should
  104. * occur.
  105. *
  106. * @since 5.8.0
  107. * @access private
  108. *
  109. * @param WP_Block_Type $block_type Block type.
  110. *
  111. * @return boolean
  112. */
  113. function wp_skip_border_serialization( $block_type ) {
  114. $border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false );
  115. return is_array( $border_support ) &&
  116. array_key_exists( '__experimentalSkipSerialization', $border_support ) &&
  117. $border_support['__experimentalSkipSerialization'];
  118. }
  119. /**
  120. * Checks whether the current block type supports the border feature requested.
  121. *
  122. * If the `__experimentalBorder` support flag is a boolean `true` all border
  123. * support features are available. Otherwise, the specific feature's support
  124. * flag nested under `experimentalBorder` must be enabled for the feature
  125. * to be opted into.
  126. *
  127. * @since 5.8.0
  128. * @access private
  129. *
  130. * @param WP_Block_Type $block_type Block type to check for support.
  131. * @param string $feature Name of the feature to check support for.
  132. * @param mixed $default Fallback value for feature support, defaults to false.
  133. *
  134. * @return boolean Whether or not the feature is supported.
  135. */
  136. function wp_has_border_feature_support( $block_type, $feature, $default = false ) {
  137. // Check if all border support features have been opted into via `"__experimentalBorder": true`.
  138. if (
  139. property_exists( $block_type, 'supports' ) &&
  140. ( true === _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default ) )
  141. ) {
  142. return true;
  143. }
  144. // Check if the specific feature has been opted into individually
  145. // via nested flag under `__experimentalBorder`.
  146. return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default );
  147. }
  148. // Register the block support.
  149. WP_Block_Supports::get_instance()->register(
  150. 'border',
  151. array(
  152. 'register_attribute' => 'wp_register_border_support',
  153. 'apply' => 'wp_apply_border_support',
  154. )
  155. );