No Description

typography.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Typography block support flag.
  4. *
  5. * @package WordPress
  6. * @since 5.6.0
  7. */
  8. /**
  9. * Registers the style and typography block attributes for block types that support it.
  10. *
  11. * @since 5.6.0
  12. * @access private
  13. *
  14. * @param WP_Block_Type $block_type Block Type.
  15. */
  16. function wp_register_typography_support( $block_type ) {
  17. if ( ! property_exists( $block_type, 'supports' ) ) {
  18. return;
  19. }
  20. $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
  21. if ( ! $typography_supports ) {
  22. return;
  23. }
  24. $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
  25. $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
  26. $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
  27. $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
  28. $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
  29. $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
  30. $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
  31. $has_typography_support = $has_font_family_support
  32. || $has_font_size_support
  33. || $has_font_style_support
  34. || $has_font_weight_support
  35. || $has_line_height_support
  36. || $has_text_decoration_support
  37. || $has_text_transform_support;
  38. if ( ! $block_type->attributes ) {
  39. $block_type->attributes = array();
  40. }
  41. if ( $has_typography_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  42. $block_type->attributes['style'] = array(
  43. 'type' => 'object',
  44. );
  45. }
  46. if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
  47. $block_type->attributes['fontSize'] = array(
  48. 'type' => 'string',
  49. );
  50. }
  51. }
  52. /**
  53. * Add CSS classes and inline styles for typography features such as font sizes
  54. * to the incoming attributes array. This will be applied to the block markup in
  55. * the front-end.
  56. *
  57. * @since 5.6.0
  58. * @access private
  59. *
  60. * @param WP_Block_Type $block_type Block type.
  61. * @param array $block_attributes Block attributes.
  62. *
  63. * @return array Typography CSS classes and inline styles.
  64. */
  65. function wp_apply_typography_support( $block_type, $block_attributes ) {
  66. if ( ! property_exists( $block_type, 'supports' ) ) {
  67. return array();
  68. }
  69. $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
  70. if ( ! $typography_supports ) {
  71. return array();
  72. }
  73. $skip_typography_serialization = _wp_array_get( $typography_supports, array( '__experimentalSkipSerialization' ), false );
  74. if ( $skip_typography_serialization ) {
  75. return array();
  76. }
  77. $attributes = array();
  78. $classes = array();
  79. $styles = array();
  80. $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
  81. $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
  82. $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
  83. $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
  84. $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
  85. $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
  86. $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
  87. if ( $has_font_size_support ) {
  88. $has_named_font_size = array_key_exists( 'fontSize', $block_attributes );
  89. $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
  90. if ( $has_named_font_size ) {
  91. $classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
  92. } elseif ( $has_custom_font_size ) {
  93. $styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] );
  94. }
  95. }
  96. if ( $has_font_family_support ) {
  97. $has_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
  98. if ( $has_font_family ) {
  99. $font_family = $block_attributes['style']['typography']['fontFamily'];
  100. if ( strpos( $font_family, 'var:preset|font-family' ) !== false ) {
  101. // Get the name from the string and add proper styles.
  102. $index_to_splice = strrpos( $font_family, '|' ) + 1;
  103. $font_family_name = substr( $font_family, $index_to_splice );
  104. $styles[] = sprintf( 'font-family: var(--wp--preset--font-family--%s);', $font_family_name );
  105. } else {
  106. $styles[] = sprintf( 'font-family: %s;', $block_attributes['style']['typography']['fontFamily'] );
  107. }
  108. }
  109. }
  110. if ( $has_font_style_support ) {
  111. $font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
  112. if ( $font_style ) {
  113. $styles[] = $font_style;
  114. }
  115. }
  116. if ( $has_font_weight_support ) {
  117. $font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
  118. if ( $font_weight ) {
  119. $styles[] = $font_weight;
  120. }
  121. }
  122. if ( $has_line_height_support ) {
  123. $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
  124. if ( $has_line_height ) {
  125. $styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
  126. }
  127. }
  128. if ( $has_text_decoration_support ) {
  129. $text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
  130. if ( $text_decoration_style ) {
  131. $styles[] = $text_decoration_style;
  132. }
  133. }
  134. if ( $has_text_transform_support ) {
  135. $text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
  136. if ( $text_transform_style ) {
  137. $styles[] = $text_transform_style;
  138. }
  139. }
  140. if ( ! empty( $classes ) ) {
  141. $attributes['class'] = implode( ' ', $classes );
  142. }
  143. if ( ! empty( $styles ) ) {
  144. $attributes['style'] = implode( ' ', $styles );
  145. }
  146. return $attributes;
  147. }
  148. /**
  149. * Generates an inline style for a typography feature e.g. text decoration,
  150. * text transform, and font style.
  151. *
  152. * @since 5.8.0
  153. * @access private
  154. *
  155. * @param array $attributes Block's attributes.
  156. * @param string $feature Key for the feature within the typography styles.
  157. * @param string $css_property Slug for the CSS property the inline style sets.
  158. *
  159. * @return string CSS inline style.
  160. */
  161. function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
  162. // Retrieve current attribute value or skip if not found.
  163. $style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
  164. if ( ! $style_value ) {
  165. return;
  166. }
  167. // If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
  168. if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) {
  169. return sprintf( '%s:%s;', $css_property, $style_value );
  170. }
  171. // We have a preset CSS variable as the style.
  172. // Get the style value from the string and return CSS style.
  173. $index_to_splice = strrpos( $style_value, '|' ) + 1;
  174. $slug = substr( $style_value, $index_to_splice );
  175. // Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
  176. return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
  177. }
  178. // Register the block support.
  179. WP_Block_Supports::get_instance()->register(
  180. 'typography',
  181. array(
  182. 'register_attribute' => 'wp_register_typography_support',
  183. 'apply' => 'wp_apply_typography_support',
  184. )
  185. );