PluginProbe
Gutenberg / 14.5.2
Gutenberg v14.5.2
24.0.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 All 403 releases
← All changes | lib/block-supports/typography.php +148 -237 23.5.3 → 14.5.2 View file →
@@ -10,29 +10,25 @@
10 10 *
11 11 * @param WP_Block_Type $block_type Block Type.
12 12 */
13 13 function gutenberg_register_typography_support( $block_type ) {
14 - if ( ! $block_type instanceof WP_Block_Type ) {
14 + if ( ! property_exists( $block_type, 'supports' ) ) {
15 15 return;
16 16 }
17 17
18 - $typography_supports = $block_type->supports['typography'] ?? false;
18 + $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
19 19 if ( ! $typography_supports ) {
20 20 return;
21 21 }
22 22
23 - $has_font_family_support = $typography_supports['__experimentalFontFamily'] ?? false;
24 - $has_font_size_support = $typography_supports['fontSize'] ?? false;
25 - $has_font_style_support = $typography_supports['__experimentalFontStyle'] ?? false;
26 - $has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
27 - $has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
28 - $has_line_height_support = $typography_supports['lineHeight'] ?? false;
29 - $has_text_align_support = $typography_supports['textAlign'] ?? false;
30 - $has_text_columns_support = $typography_supports['textColumns'] ?? false;
31 - $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
32 - $has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
33 - $has_text_indent_support = $typography_supports['textIndent'] ?? false;
34 - $has_writing_mode_support = $typography_supports['__experimentalWritingMode'] ?? false;
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 );
35 31
36 32 $has_typography_support = $has_font_family_support
37 33 || $has_font_size_support
38 34 || $has_font_style_support
@@ -38,14 +34,10 @@
38 34 || $has_font_style_support
39 35 || $has_font_weight_support
40 36 || $has_letter_spacing_support
41 37 || $has_line_height_support
42 - || $has_text_align_support
43 - || $has_text_columns_support
44 38 || $has_text_decoration_support
45 - || $has_text_transform_support
46 - || $has_text_indent_support
47 - || $has_writing_mode_support;
39 + || $has_text_transform_support;
48 40
49 41 if ( ! $block_type->attributes ) {
50 42 $block_type->attributes = array();
51 43 }
@@ -79,52 +71,44 @@
79 71 *
80 72 * @return array Typography CSS classes and inline styles.
81 73 */
82 74 function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
83 - if ( ! $block_type instanceof WP_Block_Type ) {
75 + if ( ! property_exists( $block_type, 'supports' ) ) {
84 76 return array();
85 77 }
86 78
87 - $typography_supports = $block_type->supports['typography'] ?? false;
79 + $typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
88 80 if ( ! $typography_supports ) {
89 81 return array();
90 82 }
91 83
92 - if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
84 + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
93 85 return array();
94 86 }
95 87
96 - $has_font_family_support = $typography_supports['__experimentalFontFamily'] ?? false;
97 - $has_font_size_support = $typography_supports['fontSize'] ?? false;
98 - $has_font_style_support = $typography_supports['__experimentalFontStyle'] ?? false;
99 - $has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
100 - $has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
101 - $has_line_height_support = $typography_supports['lineHeight'] ?? false;
102 - $has_text_align_support = $typography_supports['textAlign'] ?? false;
103 - $has_text_columns_support = $typography_supports['textColumns'] ?? false;
104 - $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
105 - $has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
106 - $has_text_indent_support = $typography_supports['textIndent'] ?? false;
107 - $has_writing_mode_support = $typography_supports['__experimentalWritingMode'] ?? false;
88 + $has_font_family_support = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
89 + $has_font_size_support = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
90 + $has_font_style_support = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
91 + $has_font_weight_support = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
92 + $has_letter_spacing_support = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false );
93 + $has_line_height_support = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
94 + $has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
95 + $has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
108 96
109 97 // Whether to skip individual block support features.
110 - $should_skip_font_size = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
111 - $should_skip_font_family = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' );
112 - $should_skip_font_style = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
113 - $should_skip_font_weight = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
114 - $should_skip_line_height = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' );
115 - $should_skip_text_align = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textAlign' );
116 - $should_skip_text_columns = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textColumns' );
117 - $should_skip_text_decoration = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
118 - $should_skip_text_transform = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
119 - $should_skip_letter_spacing = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' );
120 - $should_skip_text_indent = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textIndent' );
121 - $should_skip_writing_mode = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'writingMode' );
98 + $should_skip_font_size = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
99 + $should_skip_font_family = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' );
100 + $should_skip_font_style = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
101 + $should_skip_font_weight = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
102 + $should_skip_line_height = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' );
103 + $should_skip_text_decoration = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
104 + $should_skip_text_transform = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
105 + $should_skip_letter_spacing = gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' );
122 106
123 107 $typography_block_styles = array();
124 108 if ( $has_font_size_support && ! $should_skip_font_size ) {
125 109 $preset_font_size = array_key_exists( 'fontSize', $block_attributes ) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null;
126 - $custom_font_size = $block_attributes['style']['typography']['fontSize'] ?? null;
110 + $custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null;
127 111 $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : gutenberg_get_typography_font_size_value(
128 112 array(
129 113 'size' => $custom_font_size,
130 114 )
@@ -147,19 +131,11 @@
147 131 gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontWeight'], 'font-weight' );
148 132 }
149 133
150 134 if ( $has_line_height_support && ! $should_skip_line_height ) {
151 - $typography_block_styles['lineHeight'] = $block_attributes['style']['typography']['lineHeight'] ?? null;
135 + $typography_block_styles['lineHeight'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'lineHeight' ), null );
152 136 }
153 137
154 - if ( $has_text_align_support && ! $should_skip_text_align ) {
155 - $typography_block_styles['textAlign'] = $block_attributes['style']['typography']['textAlign'] ?? null;
156 - }
157 -
158 - if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
159 - $typography_block_styles['textColumns'] = $block_attributes['style']['typography']['textColumns'] ?? null;
160 - }
161 -
162 138 if ( $has_text_decoration_support && ! $should_skip_text_decoration && isset( $block_attributes['style']['typography']['textDecoration'] ) ) {
163 139 $typography_block_styles['textDecoration'] =
164 140 gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['textDecoration'], 'text-decoration' );
165 141 }
@@ -173,18 +149,9 @@
173 149 $typography_block_styles['letterSpacing'] =
174 150 gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['letterSpacing'], 'letter-spacing' );
175 151 }
176 152
177 - if ( $has_writing_mode_support && ! $should_skip_writing_mode && isset( $block_attributes['style']['typography']['writingMode'] ) ) {
178 - $typography_block_styles['writingMode'] = $block_attributes['style']['typography']['writingMode'] ?? null;
179 - }
180 -
181 - if ( $has_text_indent_support && ! $should_skip_text_indent && isset( $block_attributes['style']['typography']['textIndent'] ) ) {
182 - $typography_block_styles['textIndent'] = $block_attributes['style']['typography']['textIndent'] ?? null;
183 - }
184 -
185 153 $attributes = array();
186 - $classnames = array();
187 154 $styles = gutenberg_style_engine_get_styles(
188 155 array( 'typography' => $typography_block_styles ),
189 156 array( 'convert_vars_to_classnames' => true )
190 157 );
@@ -189,19 +156,11 @@
189 156 array( 'convert_vars_to_classnames' => true )
190 157 );
191 158
192 159 if ( ! empty( $styles['classnames'] ) ) {
193 - $classnames[] = $styles['classnames'];
160 + $attributes['class'] = $styles['classnames'];
194 161 }
195 162
196 - if ( $has_text_align_support && ! $should_skip_text_align && isset( $block_attributes['style']['typography']['textAlign'] ) ) {
197 - $classnames[] = 'has-text-align-' . $block_attributes['style']['typography']['textAlign'];
198 - }
199 -
200 - if ( ! empty( $classnames ) ) {
201 - $attributes['class'] = implode( ' ', $classnames );
202 - }
203 -
204 163 if ( ! empty( $styles['css'] ) ) {
205 164 $attributes['style'] = $styles['css'];
206 165 }
207 166
@@ -208,23 +167,18 @@
208 167 return $attributes;
209 168 }
210 169
211 170 /**
171 + * Note: this method is for backwards compatibility.
172 + * It mostly replaces `gutenberg_typography_get_css_variable_inline_style()`.
173 + *
212 174 * Generates an inline style value for a typography feature e.g. text decoration,
213 175 * text transform, and font style.
214 176 *
215 - * Note: This function is for backwards compatibility.
216 - * * It is necessary to parse older blocks whose typography styles contain presets.
217 - * * It mostly replaces the deprecated `wp_typography_get_css_variable_inline_style()`,
218 - * but skips compiling a CSS declaration as the style engine takes over this role.
177 + * @param string $style_value A raw style value for a single typography feature from a block's style attribute.
178 + * @param string $css_property Slug for the CSS property the inline style sets.
219 179 *
220 - * @link https://github.com/wordpress/gutenberg/pull/27555
221 - *
222 - * @since 6.1.0
223 - *
224 - * @param string $style_value A raw style value for a single typography feature from a block's style attribute.
225 - * @param string $css_property Slug for the CSS property the inline style sets.
226 - * @return string A CSS inline style value.
180 + * @return string? A CSS inline style value.
227 181 */
228 182 function gutenberg_typography_get_preset_inline_style_value( $style_value, $css_property ) {
229 183 // If the style value is not a preset CSS variable go no further.
230 184 if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
@@ -230,14 +184,12 @@
230 184 if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
231 185 return $style_value;
232 186 }
233 187
234 - /*
235 - * For backwards compatibility.
236 - * Presets were removed in WordPress/gutenberg#27555.
237 - * We have a preset CSS variable as the style.
238 - * Get the style value from the string and return CSS style.
239 - */
188 + // For backwards compatibility.
189 + // Presets were removed in https://github.com/WordPress/gutenberg/pull/27555.
190 + // We have a preset CSS variable as the style.
191 + // Get the style value from the string and return CSS style.
240 192 $index_to_splice = strrpos( $style_value, '|' ) + 1;
241 193 $slug = _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
242 194
243 195 // Return the actual CSS inline style value e.g. `var(--wp--preset--text-decoration--underline);`.
@@ -244,8 +196,44 @@
244 196 return sprintf( 'var(--wp--preset--%s--%s);', $css_property, $slug );
245 197 }
246 198
247 199 /**
200 + * Deprecated.
201 + * This method is no longer used and will have to be deprecated in Core.
202 + *
203 + * It can be deleted once migrated to the next WordPress version.
204 + *
205 + * Generates an inline style for a typography feature e.g. text decoration,
206 + * text transform, and font style.
207 + *
208 + * @param array $attributes Block's attributes.
209 + * @param string $feature Key for the feature within the typography styles.
210 + * @param string $css_property Slug for the CSS property the inline style sets.
211 + *
212 + * @return string CSS inline style.
213 + */
214 +function gutenberg_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
215 + // Retrieve current attribute value or skip if not found.
216 + $style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
217 + if ( ! $style_value ) {
218 + return;
219 + }
220 +
221 + // If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
222 + if ( ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
223 + return sprintf( '%s:%s;', $css_property, $style_value );
224 + }
225 +
226 + // We have a preset CSS variable as the style.
227 + // Get the style value from the string and return CSS style.
228 + $index_to_splice = strrpos( $style_value, '|' ) + 1;
229 + $slug = substr( $style_value, $index_to_splice );
230 +
231 + // Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
232 + return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
233 +}
234 +
235 +/**
248 236 * Renders typography styles/content to the block wrapper.
249 237 *
250 238 * @param string $block_content Rendered block content.
251 239 * @param array $block Block object.
@@ -251,28 +239,8 @@
251 239 * @param array $block Block object.
252 240 * @return string Filtered block content.
253 241 */
254 242 function gutenberg_render_typography_support( $block_content, $block ) {
255 - if ( ! empty( $block['attrs']['fitText'] ) && $block['attrs']['fitText'] && ! is_admin() ) {
256 - wp_enqueue_script_module( '@wordpress/block-editor/utils/fit-text-frontend' );
257 -
258 - // Add Interactivity API directives for fit text to work with client-side navigation.
259 - if ( ! empty( $block_content ) ) {
260 - $processor = new WP_HTML_Tag_Processor( $block_content );
261 - if ( $processor->next_tag() ) {
262 - if ( ! $processor->get_attribute( 'data-wp-interactive' ) ) {
263 - $processor->set_attribute( 'data-wp-interactive', true );
264 - }
265 - $processor->set_attribute( 'data-wp-context---core-fit-text', 'core/fit-text::{"fontSize":""}' );
266 - $processor->set_attribute( 'data-wp-init---core-fit-text', 'core/fit-text::callbacks.init' );
267 - $processor->set_attribute( 'data-wp-style--font-size', 'core/fit-text::context.fontSize' );
268 - $block_content = $processor->get_updated_html();
269 - }
270 - }
271 - // fitText supersedes any other typography features
272 - return $block_content;
273 - }
274 -
275 243 if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
276 244 return $block_content;
277 245 }
278 246
@@ -288,8 +256,9 @@
288 256 return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 );
289 257 }
290 258
291 259 return $block_content;
260 +
292 261 }
293 262
294 263 /**
295 264 * Internal method that checks a string for a unit and value and returns an array consisting of `'value'` and `'unit'`, e.g., [ '42', 'rem' ].
@@ -316,13 +285,14 @@
316 285 );
317 286 return null;
318 287 }
319 288
289 + // Converts numeric values to pixel values by default.
320 290 if ( empty( $raw_value ) ) {
321 291 return null;
322 292 }
323 293
324 - // Converts numeric values to pixel values by default.
294 + // Converts numbers to pixel values by default.
325 295 if ( is_numeric( $raw_value ) ) {
326 296 $raw_value = $raw_value . 'px';
327 297 }
328 298
@@ -389,13 +359,13 @@
389 359 * }
390 360 * @return string|null A font-size value using clamp().
391 361 */
392 362 function gutenberg_get_computed_fluid_typography_value( $args = array() ) {
393 - $maximum_viewport_width_raw = $args['maximum_viewport_width'] ?? null;
394 - $minimum_viewport_width_raw = $args['minimum_viewport_width'] ?? null;
395 - $maximum_font_size_raw = $args['maximum_font_size'] ?? null;
396 - $minimum_font_size_raw = $args['minimum_font_size'] ?? null;
397 - $scale_factor = $args['scale_factor'] ?? null;
363 + $maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null;
364 + $minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null;
365 + $maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null;
366 + $minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null;
367 + $scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null;
398 368
399 369 // Normalizes the minimum font size in order to use the value for calculations.
400 370 $minimum_font_size = gutenberg_get_typography_value_and_unit( $minimum_font_size_raw );
401 371
@@ -402,9 +372,9 @@
402 372 /*
403 373 * We get a 'preferred' unit to keep units consistent when calculating,
404 374 * otherwise the result will not be accurate.
405 375 */
406 - $font_size_unit = $minimum_font_size['unit'] ?? 'rem';
376 + $font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem';
407 377
408 378 // Grabs the maximum font size and normalize it in order to use the value for calculations.
409 379 $maximum_font_size = gutenberg_get_typography_value_and_unit(
410 380 $maximum_font_size_raw,
@@ -432,9 +402,8 @@
432 402 array(
433 403 'coerce_to' => $font_size_unit,
434 404 )
435 405 );
436 -
437 406 $minimum_viewport_width = gutenberg_get_typography_value_and_unit(
438 407 $minimum_viewport_width_raw,
439 408 array(
440 409 'coerce_to' => $font_size_unit,
@@ -440,25 +409,12 @@
440 409 'coerce_to' => $font_size_unit,
441 410 )
442 411 );
443 412
444 - // Protects against unsupported units in min and max viewport widths.
445 - if ( ! $minimum_viewport_width || ! $maximum_viewport_width ) {
446 - return null;
447 - }
448 -
449 - // Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.
450 - $linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];
451 - if ( empty( $linear_factor_denominator ) ) {
452 - return null;
453 - }
454 -
455 - /*
456 - * Build CSS rule.
457 - * Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
458 - */
413 + // Build CSS rule.
414 + // Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
459 415 $view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;
460 - $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );
416 + $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) );
461 417 $linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
462 418 $linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
463 419 $fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";
464 420
@@ -464,23 +420,13 @@
464 420
465 421 return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)";
466 422 }
467 423
468 -
469 424 /**
470 425 * Returns a font-size value based on a given font-size preset.
471 - * Takes into account fluid typography parameters and attempts to return a CSS
472 - * formula depending on available, valid values.
426 + * Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values.
473 427 *
474 - * @since 6.1.0
475 - * @since 6.1.1 Adjusted rules for min and max font sizes.
476 - * @since 6.2.0 Added 'settings.typography.fluid.minFontSize' support.
477 - * @since 6.3.0 Using layout.wideSize as max viewport width, and logarithmic scale factor to calculate minimum font scale.
478 - * @since 6.4.0 Added configurable min and max viewport width values to the typography.fluid theme.json schema.
479 - * @since 6.6.0 Deprecated bool argument $should_use_fluid_typography.
480 - * @since 6.7.0 Font size presets can enable fluid typography individually, even if it’s disabled globally.
481 - *
482 - * @param array $preset {
428 + * @param array $preset {
483 429 * Required. fontSizes preset value as seen in theme.json.
484 430 *
485 431 * @type string $name Name of the font size preset.
486 432 * @type string $slug Kebab-case unique identifier for the font size preset.
@@ -485,144 +431,109 @@
485 431 * @type string $name Name of the font size preset.
486 432 * @type string $slug Kebab-case unique identifier for the font size preset.
487 433 * @type string|int|float $size CSS font-size value, including units where applicable.
488 434 * }
489 - * @param bool|array $settings Optional Theme JSON settings array that overrides any global theme settings.
490 - * Default is `array()`.
435 + * @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing. Default is `false`.
491 436 *
492 437 * @return string|null Font-size value or `null` if a size is not passed in $preset.
493 438 */
494 -function gutenberg_get_typography_font_size_value( $preset, $settings = array() ) {
439 +function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
495 440 if ( ! isset( $preset['size'] ) ) {
496 441 return null;
497 442 }
498 443
499 444 /*
500 - * Catch falsy values and 0/'0'. Fluid calculations cannot be performed on `0`.
501 - * Also return early when a preset font size explicitly disables fluid typography with `false`.
445 + * Catches empty values and 0/'0'.
446 + * Fluid calculations cannot be performed on 0.
502 447 */
503 - $fluid_font_size_settings = $preset['fluid'] ?? null;
504 - if ( false === $fluid_font_size_settings || empty( $preset['size'] ) ) {
448 + if ( empty( $preset['size'] ) ) {
505 449 return $preset['size'];
506 450 }
507 451
508 - /*
509 - * Backwards compatibility since 6.5.
510 - * As a bool (deprecated since 6.5), $settings acts as an override to switch fluid typography "on" (`true`) or "off" (`false`).
511 - */
512 - if ( is_bool( $settings ) ) {
513 - _deprecated_argument( __FUNCTION__, '6.6.0', __( '`boolean` type for second argument `$settings` is deprecated. Use `array()` instead.', 'gutenberg' ) );
514 - $settings = array(
515 - 'typography' => array(
516 - 'fluid' => $settings,
517 - ),
518 - );
519 - }
452 + // Checks if fluid font sizes are activated.
453 + $typography_settings = gutenberg_get_global_settings( array( 'typography' ) );
454 + $should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography;
520 455
521 - // Fallback to global settings as default.
522 - $global_settings = gutenberg_get_global_settings();
523 - $settings = wp_parse_args(
524 - $settings,
525 - $global_settings
526 - );
527 - $typography_settings = $settings['typography'] ?? array();
528 -
529 - /*
530 - * Return early when fluid typography is disabled in the settings, and there
531 - * are no local settings to enable it for the individual preset.
532 - *
533 - * If this condition isn't met, either the settings or individual preset settings
534 - * have enabled fluid typography.
535 - */
536 - if ( empty( $typography_settings['fluid'] ) && empty( $fluid_font_size_settings ) ) {
456 + if ( ! $should_use_fluid_typography ) {
537 457 return $preset['size'];
538 458 }
539 459
540 - $fluid_settings = $typography_settings['fluid'] ?? array();
541 - $layout_settings = $settings['layout'] ?? array();
460 + // Defaults.
461 + $default_maximum_viewport_width = '1600px';
462 + $default_minimum_viewport_width = '768px';
463 + $default_minimum_font_size_factor = 0.75;
464 + $default_maximum_font_size_factor = 1.5;
465 + $default_scale_factor = 1;
466 + $default_minimum_font_size_limit = '14px';
542 467
543 - // Defaults.
544 - $default_maximum_viewport_width = '1600px';
545 - $default_minimum_viewport_width = '320px';
546 - $default_minimum_font_size_factor_max = 0.75;
547 - $default_minimum_font_size_factor_min = 0.25;
548 - $default_scale_factor = 1;
549 - $default_minimum_font_size_limit = '14px';
468 + // Font sizes.
469 + $fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;
550 470
551 - // Defaults overrides.
552 - $minimum_viewport_width = $fluid_settings['minViewportWidth'] ?? $default_minimum_viewport_width;
553 - $maximum_viewport_width = isset( $layout_settings['wideSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $layout_settings['wideSize'] ) ) ? $layout_settings['wideSize'] : $default_maximum_viewport_width;
554 - if ( isset( $fluid_settings['maxViewportWidth'] ) ) {
555 - $maximum_viewport_width = $fluid_settings['maxViewportWidth'];
471 + // A font size has explicitly bypassed fluid calculations.
472 + if ( false === $fluid_font_size_settings ) {
473 + return $preset['size'];
556 474 }
557 - $has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
558 - $minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit;
559 475
560 476 // Try to grab explicit min and max fluid font sizes.
561 - $minimum_font_size_raw = $fluid_font_size_settings['min'] ?? null;
562 - $maximum_font_size_raw = $fluid_font_size_settings['max'] ?? null;
477 + $minimum_font_size_raw = isset( $fluid_font_size_settings['min'] ) ? $fluid_font_size_settings['min'] : null;
478 + $maximum_font_size_raw = isset( $fluid_font_size_settings['max'] ) ? $fluid_font_size_settings['max'] : null;
563 479
564 480 // Font sizes.
565 481 $preferred_size = gutenberg_get_typography_value_and_unit( $preset['size'] );
566 482
567 - // Protects against unsupported units.
483 + // Protect against unsupported units.
568 484 if ( empty( $preferred_size['unit'] ) ) {
569 485 return $preset['size'];
570 486 }
571 487
488 + // If no fluid max font size is available, create one using max font size factor.
489 + if ( ! $maximum_font_size_raw ) {
490 + $maximum_font_size_raw = round( $preferred_size['value'] * $default_maximum_font_size_factor, 3 ) . $preferred_size['unit'];
491 + }
492 +
493 + // If no fluid min font size is available, create one using min font size factor.
494 + if ( ! $minimum_font_size_raw ) {
495 + $minimum_font_size_raw = round( $preferred_size['value'] * $default_minimum_font_size_factor, 3 ) . $preferred_size['unit'];
496 + }
497 +
572 498 // Parses the minimum font size limit, so we can perform checks using it.
573 499 $minimum_font_size_limit = gutenberg_get_typography_value_and_unit(
574 - $minimum_font_size_limit,
500 + $default_minimum_font_size_limit,
575 501 array(
576 502 'coerce_to' => $preferred_size['unit'],
577 503 )
578 504 );
579 505
580 - // Don't enforce minimum font size if a font size has explicitly set a min and max value.
581 - if ( ! empty( $minimum_font_size_limit ) && ( ! $minimum_font_size_raw && ! $maximum_font_size_raw ) ) {
506 + if ( ! empty( $minimum_font_size_limit ) ) {
582 507 /*
583 508 * If a minimum size was not passed to this function
584 509 * and the user-defined font size is lower than $minimum_font_size_limit,
585 - * do not calculate a fluid value.
510 + * then uses the user-defined font size as the minimum font-size.
586 511 */
587 - if ( $preferred_size['value'] <= $minimum_font_size_limit['value'] ) {
588 - return $preset['size'];
589 - }
590 - }
512 + if ( ! isset( $fluid_font_size_settings['min'] ) && $preferred_size['value'] < $minimum_font_size_limit['value'] ) {
513 + $minimum_font_size_raw = implode( '', $preferred_size );
514 + } else {
515 + $minimum_font_size_parsed = gutenberg_get_typography_value_and_unit(
516 + $minimum_font_size_raw,
517 + array(
518 + 'coerce_to' => $preferred_size['unit'],
519 + )
520 + );
591 521
592 - // If no fluid max font size is available use the incoming value.
593 - if ( ! $maximum_font_size_raw ) {
594 - $maximum_font_size_raw = $preferred_size['value'] . $preferred_size['unit'];
595 - }
596 -
597 - /*
598 - * If no minimumFontSize is provided, create one using
599 - * the given font size multiplied by the min font size scale factor.
600 - */
601 - if ( ! $minimum_font_size_raw ) {
602 - $preferred_font_size_in_px = 'px' === $preferred_size['unit'] ? $preferred_size['value'] : $preferred_size['value'] * 16;
603 -
604 - /*
605 - * The scale factor is a multiplier that affects how quickly the curve will move towards the minimum,
606 - * that is, how quickly the size factor reaches 0 given increasing font size values.
607 - * For a - b * log2(), lower values of b will make the curve move towards the minimum faster.
608 - * The scale factor is constrained between min and max values.
609 - */
610 - $minimum_font_size_factor = min( max( 1 - 0.075 * log( $preferred_font_size_in_px, 2 ), $default_minimum_font_size_factor_min ), $default_minimum_font_size_factor_max );
611 - $calculated_minimum_font_size = round( $preferred_size['value'] * $minimum_font_size_factor, 3 );
612 -
613 - // Only use calculated min font size if it's > $minimum_font_size_limit value.
614 - if ( ! empty( $minimum_font_size_limit ) && $calculated_minimum_font_size <= $minimum_font_size_limit['value'] ) {
615 - $minimum_font_size_raw = $minimum_font_size_limit['value'] . $minimum_font_size_limit['unit'];
616 - } else {
617 - $minimum_font_size_raw = $calculated_minimum_font_size . $preferred_size['unit'];
522 + /*
523 + * If the passed or calculated minimum font size is lower than $minimum_font_size_limit
524 + * use $minimum_font_size_limit instead.
525 + */
526 + if ( ! empty( $minimum_font_size_parsed ) && $minimum_font_size_parsed['value'] < $minimum_font_size_limit['value'] ) {
527 + $minimum_font_size_raw = implode( '', $minimum_font_size_limit );
528 + }
618 529 }
619 530 }
620 531
621 532 $fluid_font_size_value = gutenberg_get_computed_fluid_typography_value(
622 533 array(
623 - 'minimum_viewport_width' => $minimum_viewport_width,
624 - 'maximum_viewport_width' => $maximum_viewport_width,
534 + 'minimum_viewport_width' => $default_minimum_viewport_width,
535 + 'maximum_viewport_width' => $default_maximum_viewport_width,
625 536 'minimum_font_size' => $minimum_font_size_raw,
626 537 'maximum_font_size' => $maximum_font_size_raw,
627 538 'scale_factor' => $default_scale_factor,
628 539 )