PluginProbe
Gutenberg / 17.0.2
Gutenberg v17.0.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 +39 -107 23.7.0 → 17.0.2 View file →
@@ -25,13 +25,11 @@
25 25 $has_font_style_support = $typography_supports['__experimentalFontStyle'] ?? false;
26 26 $has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
27 27 $has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
28 28 $has_line_height_support = $typography_supports['lineHeight'] ?? false;
29 - $has_text_align_support = $typography_supports['textAlign'] ?? false;
30 29 $has_text_columns_support = $typography_supports['textColumns'] ?? false;
31 30 $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
32 31 $has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
33 - $has_text_indent_support = $typography_supports['textIndent'] ?? false;
34 32 $has_writing_mode_support = $typography_supports['__experimentalWritingMode'] ?? false;
35 33
36 34 $has_typography_support = $has_font_family_support
37 35 || $has_font_size_support
@@ -38,13 +36,11 @@
38 36 || $has_font_style_support
39 37 || $has_font_weight_support
40 38 || $has_letter_spacing_support
41 39 || $has_line_height_support
42 - || $has_text_align_support
43 40 || $has_text_columns_support
44 41 || $has_text_decoration_support
45 42 || $has_text_transform_support
46 - || $has_text_indent_support
47 43 || $has_writing_mode_support;
48 44
49 45 if ( ! $block_type->attributes ) {
50 46 $block_type->attributes = array();
@@ -98,13 +94,11 @@
98 94 $has_font_style_support = $typography_supports['__experimentalFontStyle'] ?? false;
99 95 $has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
100 96 $has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
101 97 $has_line_height_support = $typography_supports['lineHeight'] ?? false;
102 - $has_text_align_support = $typography_supports['textAlign'] ?? false;
103 98 $has_text_columns_support = $typography_supports['textColumns'] ?? false;
104 99 $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
105 100 $has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
106 - $has_text_indent_support = $typography_supports['textIndent'] ?? false;
107 101 $has_writing_mode_support = $typography_supports['__experimentalWritingMode'] ?? false;
108 102
109 103 // Whether to skip individual block support features.
110 104 $should_skip_font_size = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
@@ -111,20 +105,18 @@
111 105 $should_skip_font_family = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' );
112 106 $should_skip_font_style = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
113 107 $should_skip_font_weight = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
114 108 $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 109 $should_skip_text_columns = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textColumns' );
117 110 $should_skip_text_decoration = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
118 111 $should_skip_text_transform = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
119 112 $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 113 $should_skip_writing_mode = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'writingMode' );
122 114
123 115 $typography_block_styles = array();
124 116 if ( $has_font_size_support && ! $should_skip_font_size ) {
125 117 $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;
118 + $custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null;
127 119 $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : gutenberg_get_typography_font_size_value(
128 120 array(
129 121 'size' => $custom_font_size,
130 122 )
@@ -150,12 +142,8 @@
150 142 if ( $has_line_height_support && ! $should_skip_line_height ) {
151 143 $typography_block_styles['lineHeight'] = $block_attributes['style']['typography']['lineHeight'] ?? null;
152 144 }
153 145
154 - if ( $has_text_align_support && ! $should_skip_text_align ) {
155 - $typography_block_styles['textAlign'] = $block_attributes['style']['typography']['textAlign'] ?? null;
156 - }
157 -
158 146 if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
159 147 $typography_block_styles['textColumns'] = $block_attributes['style']['typography']['textColumns'] ?? null;
160 148 }
161 149
@@ -177,14 +165,9 @@
177 165 if ( $has_writing_mode_support && ! $should_skip_writing_mode && isset( $block_attributes['style']['typography']['writingMode'] ) ) {
178 166 $typography_block_styles['writingMode'] = $block_attributes['style']['typography']['writingMode'] ?? null;
179 167 }
180 168
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 169 $attributes = array();
186 - $classnames = array();
187 170 $styles = gutenberg_style_engine_get_styles(
188 171 array( 'typography' => $typography_block_styles ),
189 172 array( 'convert_vars_to_classnames' => true )
190 173 );
@@ -189,19 +172,11 @@
189 172 array( 'convert_vars_to_classnames' => true )
190 173 );
191 174
192 175 if ( ! empty( $styles['classnames'] ) ) {
193 - $classnames[] = $styles['classnames'];
176 + $attributes['class'] = $styles['classnames'];
194 177 }
195 178
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 179 if ( ! empty( $styles['css'] ) ) {
205 180 $attributes['style'] = $styles['css'];
206 181 }
207 182
@@ -251,28 +226,8 @@
251 226 * @param array $block Block object.
252 227 * @return string Filtered block content.
253 228 */
254 229 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 230 if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
276 231 return $block_content;
277 232 }
278 233
@@ -389,13 +344,13 @@
389 344 * }
390 345 * @return string|null A font-size value using clamp().
391 346 */
392 347 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;
348 + $maximum_viewport_width_raw = isset( $args['maximum_viewport_width'] ) ? $args['maximum_viewport_width'] : null;
349 + $minimum_viewport_width_raw = isset( $args['minimum_viewport_width'] ) ? $args['minimum_viewport_width'] : null;
350 + $maximum_font_size_raw = isset( $args['maximum_font_size'] ) ? $args['maximum_font_size'] : null;
351 + $minimum_font_size_raw = isset( $args['minimum_font_size'] ) ? $args['minimum_font_size'] : null;
352 + $scale_factor = isset( $args['scale_factor'] ) ? $args['scale_factor'] : null;
398 353
399 354 // Normalizes the minimum font size in order to use the value for calculations.
400 355 $minimum_font_size = gutenberg_get_typography_value_and_unit( $minimum_font_size_raw );
401 356
@@ -402,9 +357,9 @@
402 357 /*
403 358 * We get a 'preferred' unit to keep units consistent when calculating,
404 359 * otherwise the result will not be accurate.
405 360 */
406 - $font_size_unit = $minimum_font_size['unit'] ?? 'rem';
361 + $font_size_unit = isset( $minimum_font_size['unit'] ) ? $minimum_font_size['unit'] : 'rem';
407 362
408 363 // Grabs the maximum font size and normalize it in order to use the value for calculations.
409 364 $maximum_font_size = gutenberg_get_typography_value_and_unit(
410 365 $maximum_font_size_raw,
@@ -445,20 +400,12 @@
445 400 if ( ! $minimum_viewport_width || ! $maximum_viewport_width ) {
446 401 return null;
447 402 }
448 403
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 - */
404 + // Build CSS rule.
405 + // Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
459 406 $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 ) );
407 + $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) );
461 408 $linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
462 409 $linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
463 410 $fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";
464 411
@@ -464,9 +411,8 @@
464 411
465 412 return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)";
466 413 }
467 414
468 -
469 415 /**
470 416 * Returns a font-size value based on a given font-size preset.
471 417 * Takes into account fluid typography parameters and attempts to return a CSS
472 418 * formula depending on available, valid values.
@@ -475,12 +421,10 @@
475 421 * @since 6.1.1 Adjusted rules for min and max font sizes.
476 422 * @since 6.2.0 Added 'settings.typography.fluid.minFontSize' support.
477 423 * @since 6.3.0 Using layout.wideSize as max viewport width, and logarithmic scale factor to calculate minimum font scale.
478 424 * @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 425 *
482 - * @param array $preset {
426 + * @param array $preset {
483 427 * Required. fontSizes preset value as seen in theme.json.
484 428 *
485 429 * @type string $name Name of the font size preset.
486 430 * @type string $slug Kebab-case unique identifier for the font size preset.
@@ -485,61 +429,41 @@
485 429 * @type string $name Name of the font size preset.
486 430 * @type string $slug Kebab-case unique identifier for the font size preset.
487 431 * @type string|int|float $size CSS font-size value, including units where applicable.
488 432 * }
489 - * @param bool|array $settings Optional Theme JSON settings array that overrides any global theme settings.
490 - * Default is `array()`.
433 + * @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing. Default is `false`.
491 434 *
492 435 * @return string|null Font-size value or `null` if a size is not passed in $preset.
493 436 */
494 -function gutenberg_get_typography_font_size_value( $preset, $settings = array() ) {
437 +function gutenberg_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
495 438 if ( ! isset( $preset['size'] ) ) {
496 439 return null;
497 440 }
498 441
499 442 /*
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`.
443 + * Catches empty values and 0/'0'.
444 + * Fluid calculations cannot be performed on 0.
502 445 */
503 - $fluid_font_size_settings = $preset['fluid'] ?? null;
504 - if ( false === $fluid_font_size_settings || empty( $preset['size'] ) ) {
446 + if ( empty( $preset['size'] ) ) {
505 447 return $preset['size'];
506 448 }
507 449
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 - }
450 + // Checks if fluid font sizes are activated.
451 + $global_settings = gutenberg_get_global_settings();
452 + $typography_settings = isset( $global_settings['typography'] ) ? $global_settings['typography'] : array();
453 + $layout_settings = isset( $global_settings['layout'] ) ? $global_settings['layout'] : array();
520 454
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();
455 + $should_use_fluid_typography
456 + = isset( $typography_settings['fluid'] ) &&
457 + ( true === $typography_settings['fluid'] || is_array( $typography_settings['fluid'] ) ) ?
458 + true :
459 + $should_use_fluid_typography;
528 460
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 ) ) {
461 + if ( ! $should_use_fluid_typography ) {
537 462 return $preset['size'];
538 463 }
539 464
540 - $fluid_settings = $typography_settings['fluid'] ?? array();
541 - $layout_settings = $settings['layout'] ?? array();
465 + $fluid_settings = isset( $typography_settings['fluid'] ) && is_array( $typography_settings['fluid'] ) ? $typography_settings['fluid'] : array();
542 466
543 467 // Defaults.
544 468 $default_maximum_viewport_width = '1600px';
545 469 $default_minimum_viewport_width = '320px';
@@ -548,9 +472,9 @@
548 472 $default_scale_factor = 1;
549 473 $default_minimum_font_size_limit = '14px';
550 474
551 475 // Defaults overrides.
552 - $minimum_viewport_width = $fluid_settings['minViewportWidth'] ?? $default_minimum_viewport_width;
476 + $minimum_viewport_width = isset( $fluid_settings['minViewportWidth'] ) ? $fluid_settings['minViewportWidth'] : $default_minimum_viewport_width;
553 477 $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 478 if ( isset( $fluid_settings['maxViewportWidth'] ) ) {
555 479 $maximum_viewport_width = $fluid_settings['maxViewportWidth'];
556 480 }
@@ -556,11 +480,19 @@
556 480 }
557 481 $has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
558 482 $minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit;
559 483
484 + // Font sizes.
485 + $fluid_font_size_settings = isset( $preset['fluid'] ) ? $preset['fluid'] : null;
486 +
487 + // A font size has explicitly bypassed fluid calculations.
488 + if ( false === $fluid_font_size_settings ) {
489 + return $preset['size'];
490 + }
491 +
560 492 // 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;
493 + $minimum_font_size_raw = isset( $fluid_font_size_settings['min'] ) ? $fluid_font_size_settings['min'] : null;
494 + $maximum_font_size_raw = isset( $fluid_font_size_settings['max'] ) ? $fluid_font_size_settings['max'] : null;
563 495
564 496 // Font sizes.
565 497 $preferred_size = gutenberg_get_typography_value_and_unit( $preset['size'] );
566 498