PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / block-supports / typography.php

typography.php in Gutenberg 12.1.0, at lib/block-supports/typography.php

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