/wp-content/plugins/gutenberg/lib/block-supports/typography.php

https://github.com/livinglab/openlab · PHP · 216 lines · 149 code · 29 blank · 38 comment · 38 complexity · d661e458d2f6695408955bd11241af7c MD5 · raw file

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