[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/block-supports/ -> typography.php (source)

   1  <?php
   2  /**
   3   * Typography block support flag.
   4   *
   5   * @package WordPress
   6   * @since 5.6.0
   7   */
   8  
   9  /**
  10   * Registers the style and typography block attributes for block types that support it.
  11   *
  12   * @since 5.6.0
  13   * @since 6.3.0 Added support for text-columns.
  14   * @since 7.0.0 Added support for text-indent.
  15   * @access private
  16   *
  17   * @param WP_Block_Type $block_type Block Type.
  18   */
  19  function wp_register_typography_support( $block_type ) {
  20      if ( ! ( $block_type instanceof WP_Block_Type ) ) {
  21          return;
  22      }
  23  
  24      $typography_supports = $block_type->supports['typography'] ?? false;
  25      if ( ! $typography_supports ) {
  26          return;
  27      }
  28  
  29      $has_font_family_support     = $typography_supports['__experimentalFontFamily'] ?? false;
  30      $has_font_size_support       = $typography_supports['fontSize'] ?? false;
  31      $has_font_style_support      = $typography_supports['__experimentalFontStyle'] ?? false;
  32      $has_font_weight_support     = $typography_supports['__experimentalFontWeight'] ?? false;
  33      $has_letter_spacing_support  = $typography_supports['__experimentalLetterSpacing'] ?? false;
  34      $has_line_height_support     = $typography_supports['lineHeight'] ?? false;
  35      $has_text_align_support      = $typography_supports['textAlign'] ?? false;
  36      $has_text_columns_support    = $typography_supports['textColumns'] ?? false;
  37      $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
  38      $has_text_transform_support  = $typography_supports['__experimentalTextTransform'] ?? false;
  39      $has_text_indent_support     = $typography_supports['textIndent'] ?? false;
  40      $has_text_shadow_support     = $typography_supports['textShadow'] ?? false;
  41      $has_writing_mode_support    = $typography_supports['__experimentalWritingMode'] ?? false;
  42  
  43      $has_typography_support = $has_font_family_support
  44          || $has_font_size_support
  45          || $has_font_style_support
  46          || $has_font_weight_support
  47          || $has_letter_spacing_support
  48          || $has_line_height_support
  49          || $has_text_align_support
  50          || $has_text_columns_support
  51          || $has_text_decoration_support
  52          || $has_text_transform_support
  53          || $has_text_indent_support
  54          || $has_text_shadow_support
  55          || $has_writing_mode_support;
  56  
  57      if ( ! $block_type->attributes ) {
  58          $block_type->attributes = array();
  59      }
  60  
  61      if ( $has_typography_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  62          $block_type->attributes['style'] = array(
  63              'type' => 'object',
  64          );
  65      }
  66  
  67      if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
  68          $block_type->attributes['fontSize'] = array(
  69              'type' => 'string',
  70          );
  71      }
  72  
  73      if ( $has_font_family_support && ! array_key_exists( 'fontFamily', $block_type->attributes ) ) {
  74          $block_type->attributes['fontFamily'] = array(
  75              'type' => 'string',
  76          );
  77      }
  78  
  79      if ( $has_text_shadow_support && ! array_key_exists( 'textShadow', $block_type->attributes ) ) {
  80          $block_type->attributes['textShadow'] = array(
  81              'type' => 'string',
  82          );
  83      }
  84  }
  85  
  86  /**
  87   * Adds CSS classes and inline styles for typography features such as font sizes
  88   * to the incoming attributes array. This will be applied to the block markup in
  89   * the front-end.
  90   *
  91   * @since 5.6.0
  92   * @since 6.1.0 Used the style engine to generate CSS and classnames.
  93   * @since 6.3.0 Added support for text-columns.
  94   * @since 7.0.0 Added support for text-indent.
  95   * @access private
  96   *
  97   * @param WP_Block_Type $block_type       Block type.
  98   * @param array         $block_attributes Block attributes.
  99   * @return array Typography CSS classes and inline styles.
 100   */
 101  function wp_apply_typography_support( $block_type, $block_attributes ) {
 102      if ( ! ( $block_type instanceof WP_Block_Type ) ) {
 103          return array();
 104      }
 105  
 106      $typography_supports = $block_type->supports['typography'] ?? false;
 107      if ( ! $typography_supports ) {
 108          return array();
 109      }
 110  
 111      if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
 112          return array();
 113      }
 114  
 115      $has_font_family_support     = $typography_supports['__experimentalFontFamily'] ?? false;
 116      $has_font_size_support       = $typography_supports['fontSize'] ?? false;
 117      $has_font_style_support      = $typography_supports['__experimentalFontStyle'] ?? false;
 118      $has_font_weight_support     = $typography_supports['__experimentalFontWeight'] ?? false;
 119      $has_letter_spacing_support  = $typography_supports['__experimentalLetterSpacing'] ?? false;
 120      $has_line_height_support     = $typography_supports['lineHeight'] ?? false;
 121      $has_text_align_support      = $typography_supports['textAlign'] ?? false;
 122      $has_text_columns_support    = $typography_supports['textColumns'] ?? false;
 123      $has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
 124      $has_text_transform_support  = $typography_supports['__experimentalTextTransform'] ?? false;
 125      $has_text_indent_support     = $typography_supports['textIndent'] ?? false;
 126      $has_text_shadow_support     = $typography_supports['textShadow'] ?? false;
 127      $has_writing_mode_support    = $typography_supports['__experimentalWritingMode'] ?? false;
 128  
 129      // Whether to skip individual block support features.
 130      $should_skip_font_size       = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
 131      $should_skip_font_family     = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' );
 132      $should_skip_font_style      = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
 133      $should_skip_font_weight     = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
 134      $should_skip_line_height     = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' );
 135      $should_skip_text_align      = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textAlign' );
 136      $should_skip_text_columns    = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textColumns' );
 137      $should_skip_text_decoration = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
 138      $should_skip_text_transform  = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
 139      $should_skip_letter_spacing  = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' );
 140      $should_skip_text_indent     = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textIndent' );
 141      $should_skip_text_shadow     = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textShadow' );
 142      $should_skip_writing_mode    = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'writingMode' );
 143  
 144      $typography_block_styles = array();
 145      if ( $has_font_size_support && ! $should_skip_font_size ) {
 146          $preset_font_size                    = array_key_exists( 'fontSize', $block_attributes )
 147              ? "var:preset|font-size|{$block_attributes['fontSize']}"
 148              : null;
 149          $custom_font_size                    = $block_attributes['style']['typography']['fontSize'] ?? null;
 150          $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(
 151              array(
 152                  'size' => $custom_font_size,
 153              )
 154          );
 155      }
 156  
 157      if ( $has_font_family_support && ! $should_skip_font_family ) {
 158          $preset_font_family                    = array_key_exists( 'fontFamily', $block_attributes )
 159              ? "var:preset|font-family|{$block_attributes['fontFamily']}"
 160              : null;
 161          $custom_font_family                    = isset( $block_attributes['style']['typography']['fontFamily'] )
 162              ? wp_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontFamily'], 'font-family' )
 163              : null;
 164          $typography_block_styles['fontFamily'] = $preset_font_family ? $preset_font_family : $custom_font_family;
 165      }
 166  
 167      if (
 168          $has_font_style_support &&
 169          ! $should_skip_font_style &&
 170          isset( $block_attributes['style']['typography']['fontStyle'] )
 171      ) {
 172          $typography_block_styles['fontStyle'] = wp_typography_get_preset_inline_style_value(
 173              $block_attributes['style']['typography']['fontStyle'],
 174              'font-style'
 175          );
 176      }
 177  
 178      if (
 179          $has_font_weight_support &&
 180          ! $should_skip_font_weight &&
 181          isset( $block_attributes['style']['typography']['fontWeight'] )
 182      ) {
 183          $typography_block_styles['fontWeight'] = wp_typography_get_preset_inline_style_value(
 184              $block_attributes['style']['typography']['fontWeight'],
 185              'font-weight'
 186          );
 187      }
 188  
 189      if ( $has_line_height_support && ! $should_skip_line_height ) {
 190          $typography_block_styles['lineHeight'] = $block_attributes['style']['typography']['lineHeight'] ?? null;
 191      }
 192  
 193      if ( $has_text_align_support && ! $should_skip_text_align ) {
 194          $typography_block_styles['textAlign'] = $block_attributes['style']['typography']['textAlign'] ?? null;
 195      }
 196  
 197      if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
 198          $typography_block_styles['textColumns'] = $block_attributes['style']['typography']['textColumns'] ?? null;
 199      }
 200  
 201      if (
 202          $has_text_decoration_support &&
 203          ! $should_skip_text_decoration &&
 204          isset( $block_attributes['style']['typography']['textDecoration'] )
 205      ) {
 206          $typography_block_styles['textDecoration'] = wp_typography_get_preset_inline_style_value(
 207              $block_attributes['style']['typography']['textDecoration'],
 208              'text-decoration'
 209          );
 210      }
 211  
 212      if (
 213          $has_text_transform_support &&
 214          ! $should_skip_text_transform &&
 215          isset( $block_attributes['style']['typography']['textTransform'] )
 216      ) {
 217          $typography_block_styles['textTransform'] = wp_typography_get_preset_inline_style_value(
 218              $block_attributes['style']['typography']['textTransform'],
 219              'text-transform'
 220          );
 221      }
 222  
 223      if (
 224          $has_letter_spacing_support &&
 225          ! $should_skip_letter_spacing &&
 226          isset( $block_attributes['style']['typography']['letterSpacing'] )
 227      ) {
 228          $typography_block_styles['letterSpacing'] = wp_typography_get_preset_inline_style_value(
 229              $block_attributes['style']['typography']['letterSpacing'],
 230              'letter-spacing'
 231          );
 232      }
 233  
 234      if ( $has_writing_mode_support &&
 235          ! $should_skip_writing_mode &&
 236          isset( $block_attributes['style']['typography']['writingMode'] )
 237      ) {
 238          $typography_block_styles['writingMode'] = $block_attributes['style']['typography']['writingMode'] ?? null;
 239      }
 240  
 241      if ( $has_text_indent_support && ! $should_skip_text_indent && isset( $block_attributes['style']['typography']['textIndent'] ) ) {
 242          $typography_block_styles['textIndent'] = $block_attributes['style']['typography']['textIndent'] ?? null;
 243      }
 244  
 245      if ( $has_text_shadow_support && ! $should_skip_text_shadow ) {
 246          $preset_text_shadow                    = array_key_exists( 'textShadow', $block_attributes ) ? "var:preset|text-shadow|{$block_attributes['textShadow']}" : null;
 247          $custom_text_shadow                    = $block_attributes['style']['typography']['textShadow'] ?? null;
 248          $typography_block_styles['textShadow'] = $preset_text_shadow ? $preset_text_shadow : $custom_text_shadow;
 249      }
 250  
 251      $attributes = array();
 252      $classnames = array();
 253      $styles     = wp_style_engine_get_styles(
 254          array( 'typography' => $typography_block_styles ),
 255          array( 'convert_vars_to_classnames' => true )
 256      );
 257  
 258      if ( ! empty( $styles['classnames'] ) ) {
 259          $classnames[] = $styles['classnames'];
 260      }
 261  
 262      if ( $has_text_align_support && ! $should_skip_text_align && isset( $block_attributes['style']['typography']['textAlign'] ) ) {
 263          $classnames[] = 'has-text-align-' . $block_attributes['style']['typography']['textAlign'];
 264      }
 265  
 266      if ( ! empty( $classnames ) ) {
 267          $attributes['class'] = implode( ' ', $classnames );
 268      }
 269  
 270      if ( ! empty( $styles['css'] ) ) {
 271          $attributes['style'] = $styles['css'];
 272      }
 273  
 274      return $attributes;
 275  }
 276  
 277  /**
 278   * Generates an inline style value for a typography feature e.g. text decoration,
 279   * text transform, and font style.
 280   *
 281   * Note: This function is for backwards compatibility.
 282   * * It is necessary to parse older blocks whose typography styles contain presets.
 283   * * It mostly replaces the deprecated `wp_typography_get_css_variable_inline_style()`,
 284   *   but skips compiling a CSS declaration as the style engine takes over this role.
 285   *
 286   * @since 6.1.0
 287   *
 288   * @link https://github.com/wordpress/gutenberg/pull/27555
 289   *
 290   * @param string $style_value  A raw style value for a single typography feature from a block's style attribute.
 291   * @param string $css_property Slug for the CSS property the inline style sets.
 292   * @return string A CSS inline style value.
 293   */
 294  function wp_typography_get_preset_inline_style_value( $style_value, $css_property ) {
 295      // If the style value is not a preset CSS variable go no further.
 296      if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
 297          return $style_value;
 298      }
 299  
 300      /*
 301       * For backwards compatibility.
 302       * Presets were removed in WordPress/gutenberg#27555.
 303       * A preset CSS variable is the style.
 304       * Gets the style value from the string and return CSS style.
 305       */
 306      $index_to_splice = strrpos( $style_value, '|' ) + 1;
 307      $slug            = _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
 308  
 309      // Return the actual CSS inline style value,
 310      // e.g. `var(--wp--preset--text-decoration--underline);`.
 311      return sprintf( 'var(--wp--preset--%s--%s);', $css_property, $slug );
 312  }
 313  
 314  /**
 315   * Renders typography styles/content to the block wrapper.
 316   *
 317   * @since 6.1.0
 318   *
 319   * @param string $block_content Rendered block content.
 320   * @param array  $block         Block object.
 321   * @return string Filtered block content.
 322   */
 323  function wp_render_typography_support( $block_content, $block ) {
 324      if ( ! empty( $block['attrs']['fitText'] ) && ! is_admin() ) {
 325          wp_enqueue_script_module( '@wordpress/block-editor/utils/fit-text-frontend' );
 326  
 327          // Add Interactivity API directives for fit text to work with client-side navigation.
 328          if ( ! empty( $block_content ) ) {
 329              $processor = new WP_HTML_Tag_Processor( $block_content );
 330              if ( $processor->next_tag() ) {
 331                  $processor->add_class( 'has-fit-text' );
 332                  if ( ! $processor->get_attribute( 'data-wp-interactive' ) ) {
 333                      $processor->set_attribute( 'data-wp-interactive', true );
 334                  }
 335                  $processor->set_attribute( 'data-wp-context---core-fit-text', 'core/fit-text::{"fontSize":""}' );
 336                  $processor->set_attribute( 'data-wp-init---core-fit-text', 'core/fit-text::callbacks.init' );
 337                  $processor->set_attribute( 'data-wp-style--font-size', 'core/fit-text::context.fontSize' );
 338                  $block_content = $processor->get_updated_html();
 339              }
 340          }
 341          // fitText supersedes any other typography features
 342          return $block_content;
 343      }
 344      if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
 345          return $block_content;
 346      }
 347  
 348      $custom_font_size = $block['attrs']['style']['typography']['fontSize'];
 349      $fluid_font_size  = wp_get_typography_font_size_value( array( 'size' => $custom_font_size ) );
 350  
 351      /*
 352       * Checks that $fluid_font_size does not match $custom_font_size,
 353       * which means it's been mutated by the fluid font size functions.
 354       */
 355      if ( ! empty( $fluid_font_size ) && $fluid_font_size !== $custom_font_size ) {
 356          // Replaces the first instance of `font-size:$custom_font_size` with `font-size:$fluid_font_size`.
 357          return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 );
 358      }
 359  
 360      return $block_content;
 361  }
 362  
 363  /**
 364   * Checks a string for a unit and value and returns an array
 365   * consisting of `'value'` and `'unit'`, e.g. array( '42', 'rem' ).
 366   *
 367   * @since 6.1.0
 368   *
 369   * @param string|int|float $raw_value Raw size value from theme.json.
 370   * @param array            $options   {
 371   *     Optional. An associative array of options. Default is empty array.
 372   *
 373   *     @type string   $coerce_to        Coerce the value to rem or px. Default `'rem'`.
 374   *     @type int      $root_size_value  Value of root font size for rem|em <-> px conversion. Default `16`.
 375   *     @type string[] $acceptable_units An array of font size units. Default `array( 'rem', 'px', 'em' )`;
 376   * }
 377   * @return array|null An array consisting of `'value'` and `'unit'` properties on success.
 378   *                    `null` on failure.
 379   * @phpstan-param array{
 380   *     coerce_to?: string,
 381   *     root_size_value?: positive-int,
 382   *     acceptable_units?: non-empty-array<non-empty-string>,
 383   * } $options
 384   * @phpstan-return array{ value: float, unit: non-empty-string }|null
 385   */
 386  function wp_get_typography_value_and_unit( $raw_value, $options = array() ): ?array {
 387      if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) {
 388          _doing_it_wrong(
 389              __FUNCTION__,
 390              __( 'Raw size value must be a string, integer, or float.' ),
 391              '6.1.0'
 392          );
 393          return null;
 394      }
 395  
 396      if ( empty( $raw_value ) ) {
 397          return null;
 398      }
 399  
 400      // Converts numbers to pixel values by default.
 401      if ( is_numeric( $raw_value ) ) {
 402          $raw_value = $raw_value . 'px';
 403      }
 404  
 405      $defaults = array(
 406          'coerce_to'        => '',
 407          'root_size_value'  => 16,
 408          'acceptable_units' => array( 'rem', 'px', 'em' ),
 409      );
 410  
 411      /**
 412       * @var array{
 413       *     coerce_to: string,
 414       *     root_size_value: positive-int,
 415       *     acceptable_units: non-empty-array<non-empty-string>,
 416       * } $options
 417       */
 418      $options = wp_parse_args( $options, $defaults );
 419  
 420      // Bails out if the raw value can't be parsed.
 421      if ( ! preg_match( '/^(\d*\.?\d+)([a-zA-Z]+|%)$/', $raw_value, $matches ) ) {
 422          return null;
 423      }
 424  
 425      $value = (float) $matches[1];
 426      $unit  = $matches[2];
 427  
 428      if ( ! in_array( $unit, $options['acceptable_units'], true ) ) {
 429          return null;
 430      }
 431  
 432      /*
 433       * Default browser font size. Later, possibly could inject some JS to
 434       * compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
 435       */
 436      if ( 'px' === $options['coerce_to'] && ( 'em' === $unit || 'rem' === $unit ) ) {
 437          $value = $value * $options['root_size_value'];
 438          $unit  = $options['coerce_to'];
 439      }
 440  
 441      if ( 'px' === $unit && ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) ) {
 442          $value = $value / $options['root_size_value'];
 443          $unit  = $options['coerce_to'];
 444      }
 445  
 446      /*
 447       * No calculation is required if swapping between em and rem yet,
 448       * since we assume a root size value. Later we might like to differentiate between
 449       * :root font size (rem) and parent element font size (em) relativity.
 450       */
 451      if ( ( 'em' === $options['coerce_to'] || 'rem' === $options['coerce_to'] ) && ( 'em' === $unit || 'rem' === $unit ) ) {
 452          $unit = $options['coerce_to'];
 453      }
 454  
 455      return array(
 456          'value' => round( $value, 3 ),
 457          'unit'  => $unit,
 458      );
 459  }
 460  
 461  /**
 462   * Internal implementation of CSS clamp() based on available min/max viewport
 463   * width and min/max font sizes.
 464   *
 465   * @since 6.1.0
 466   * @since 6.3.0 Checks for unsupported min/max viewport values that cause invalid clamp values.
 467   * @since 6.5.0 Returns early when min and max viewport subtraction is zero to avoid division by zero.
 468   * @access private
 469   *
 470   * @param array $args {
 471   *     Optional. An associative array of values to calculate a fluid formula
 472   *     for font size. Default is empty array.
 473   *
 474   *     @type string $maximum_viewport_width Maximum size up to which type will have fluidity.
 475   *     @type string $minimum_viewport_width Minimum viewport size from which type will have fluidity.
 476   *     @type string $maximum_font_size      Maximum font size for any clamp() calculation.
 477   *     @type string $minimum_font_size      Minimum font size for any clamp() calculation.
 478   *     @type int    $scale_factor           A scale factor to determine how fast a font scales within boundaries.
 479   * }
 480   * @return string|null A font-size value using clamp() on success, otherwise null.
 481   */
 482  function wp_get_computed_fluid_typography_value( $args = array() ) {
 483      $maximum_viewport_width_raw = $args['maximum_viewport_width'] ?? null;
 484      $minimum_viewport_width_raw = $args['minimum_viewport_width'] ?? null;
 485      $maximum_font_size_raw      = $args['maximum_font_size'] ?? null;
 486      $minimum_font_size_raw      = $args['minimum_font_size'] ?? null;
 487      $scale_factor               = $args['scale_factor'] ?? null;
 488  
 489      // Normalizes the minimum font size in order to use the value for calculations.
 490      $minimum_font_size = wp_get_typography_value_and_unit( $minimum_font_size_raw );
 491  
 492      /*
 493       * We get a 'preferred' unit to keep units consistent when calculating,
 494       * otherwise the result will not be accurate.
 495       */
 496      $font_size_unit = $minimum_font_size['unit'] ?? 'rem';
 497  
 498      // Normalizes the maximum font size in order to use the value for calculations.
 499      $maximum_font_size = wp_get_typography_value_and_unit(
 500          $maximum_font_size_raw,
 501          array(
 502              'coerce_to' => $font_size_unit,
 503          )
 504      );
 505  
 506      // Checks for mandatory min and max sizes, and protects against unsupported units.
 507      if ( ! $maximum_font_size || ! $minimum_font_size ) {
 508          return null;
 509      }
 510  
 511      // Uses rem for accessible fluid target font scaling.
 512      $minimum_font_size_rem = wp_get_typography_value_and_unit(
 513          $minimum_font_size_raw,
 514          array(
 515              'coerce_to' => 'rem',
 516          )
 517      );
 518  
 519      // Viewport widths defined for fluid typography. Normalize units.
 520      $maximum_viewport_width = wp_get_typography_value_and_unit(
 521          $maximum_viewport_width_raw,
 522          array(
 523              'coerce_to' => $font_size_unit,
 524          )
 525      );
 526      $minimum_viewport_width = wp_get_typography_value_and_unit(
 527          $minimum_viewport_width_raw,
 528          array(
 529              'coerce_to' => $font_size_unit,
 530          )
 531      );
 532  
 533      // Protects against unsupported units in min and max viewport widths.
 534      if ( ! $minimum_viewport_width || ! $maximum_viewport_width ) {
 535          return null;
 536      }
 537  
 538      // Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.
 539      $linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];
 540      if ( empty( $linear_factor_denominator ) ) {
 541          return null;
 542      }
 543  
 544      /*
 545       * Build CSS rule.
 546       * Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
 547       */
 548      $view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;
 549      $linear_factor          = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );
 550      $linear_factor_scaled   = round( $linear_factor * $scale_factor, 3 );
 551      $linear_factor_scaled   = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
 552      $fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";
 553  
 554      return "clamp($minimum_font_size_raw, $fluid_target_font_size, $maximum_font_size_raw)";
 555  }
 556  
 557  /**
 558   * Returns a font-size value based on a given font-size preset.
 559   * Takes into account fluid typography parameters and attempts to return a CSS
 560   * formula depending on available, valid values.
 561   *
 562   * @since 6.1.0
 563   * @since 6.1.1 Adjusted rules for min and max font sizes.
 564   * @since 6.2.0 Added 'settings.typography.fluid.minFontSize' support.
 565   * @since 6.3.0 Using layout.wideSize as max viewport width, and logarithmic scale factor to calculate minimum font scale.
 566   * @since 6.4.0 Added configurable min and max viewport width values to the typography.fluid theme.json schema.
 567   * @since 6.6.0 Deprecated bool argument $should_use_fluid_typography.
 568   * @since 6.7.0 Font size presets can enable fluid typography individually, even if it’s disabled globally.
 569   *
 570   * @param array      $preset   {
 571   *     Required. fontSizes preset value as seen in theme.json.
 572   *
 573   *     @type string           $name Name of the font size preset.
 574   *     @type string           $slug Kebab-case, unique identifier for the font size preset.
 575   *     @type string|int|float $size CSS font-size value, including units if applicable.
 576   * }
 577   * @param bool|array $settings Optional. Theme JSON settings array that overrides any global theme settings.
 578   *                             Passing a boolean is deprecated. Default empty array.
 579   * @return string|null Font-size value or null if a size is not passed in $preset.
 580   *
 581   * @phpstan-param array $settings
 582   */
 583  function wp_get_typography_font_size_value( $preset, $settings = array() ) {
 584      if ( ! isset( $preset['size'] ) ) {
 585          return null;
 586      }
 587  
 588      /*
 589       * Catches falsy values and 0/'0'. Fluid calculations cannot be performed on `0`.
 590       * Also returns early when a preset font size explicitly disables fluid typography with `false`.
 591       */
 592      $fluid_font_size_settings = $preset['fluid'] ?? null;
 593      if ( false === $fluid_font_size_settings || empty( $preset['size'] ) ) {
 594          return $preset['size'];
 595      }
 596  
 597      /*
 598       * As a boolean (deprecated since 6.6), $settings acts as an override to switch fluid typography "on" (`true`) or "off" (`false`).
 599       */
 600      if ( is_bool( $settings ) ) {
 601          _deprecated_argument( __FUNCTION__, '6.6.0', __( '`boolean` type for second argument `$settings` is deprecated. Use `array()` instead.' ) );
 602          $settings = array(
 603              'typography' => array(
 604                  'fluid' => $settings,
 605              ),
 606          );
 607      }
 608  
 609      // Fallback to global settings as default.
 610      $global_settings = wp_get_global_settings();
 611      $settings        = wp_parse_args(
 612          $settings,
 613          $global_settings
 614      );
 615  
 616      $typography_settings = $settings['typography'] ?? array();
 617  
 618      /*
 619       * Return early when fluid typography is disabled in the settings, and there
 620       * are no local settings to enable it for the individual preset.
 621       *
 622       * If this condition isn't met, either the settings or individual preset settings
 623       * have enabled fluid typography.
 624       */
 625      if ( empty( $typography_settings['fluid'] ) && empty( $fluid_font_size_settings ) ) {
 626          return $preset['size'];
 627      }
 628  
 629      $fluid_settings  = $typography_settings['fluid'] ?? array();
 630      $layout_settings = $settings['layout'] ?? array();
 631  
 632      // Defaults.
 633      $default_maximum_viewport_width       = '1600px';
 634      $default_minimum_viewport_width       = '320px';
 635      $default_minimum_font_size_factor_max = 0.75;
 636      $default_minimum_font_size_factor_min = 0.25;
 637      $default_scale_factor                 = 1;
 638      $default_minimum_font_size_limit      = '14px';
 639  
 640      // Defaults overrides.
 641      $minimum_viewport_width = $fluid_settings['minViewportWidth'] ?? $default_minimum_viewport_width;
 642      $maximum_viewport_width = isset( $layout_settings['wideSize'] ) && ! empty( wp_get_typography_value_and_unit( $layout_settings['wideSize'] ) ) ? $layout_settings['wideSize'] : $default_maximum_viewport_width;
 643      if ( isset( $fluid_settings['maxViewportWidth'] ) ) {
 644          $maximum_viewport_width = $fluid_settings['maxViewportWidth'];
 645      }
 646      $has_min_font_size       = isset( $fluid_settings['minFontSize'] ) && ! empty( wp_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
 647      $minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : $default_minimum_font_size_limit;
 648  
 649      // Try to grab explicit min and max fluid font sizes.
 650      $minimum_font_size_raw = $fluid_font_size_settings['min'] ?? null;
 651      $maximum_font_size_raw = $fluid_font_size_settings['max'] ?? null;
 652  
 653      // Font sizes.
 654      $preferred_size = wp_get_typography_value_and_unit( $preset['size'] );
 655  
 656      // Protects against unsupported units.
 657      if ( empty( $preferred_size['unit'] ) ) {
 658          return $preset['size'];
 659      }
 660  
 661      /*
 662       * Normalizes the minimum font size limit according to the incoming unit,
 663       * in order to perform comparative checks.
 664       */
 665      $minimum_font_size_limit = wp_get_typography_value_and_unit(
 666          $minimum_font_size_limit,
 667          array(
 668              'coerce_to' => $preferred_size['unit'],
 669          )
 670      );
 671  
 672      // Don't enforce minimum font size if a font size has explicitly set a min and max value.
 673      if ( ! empty( $minimum_font_size_limit ) && ( ! $minimum_font_size_raw && ! $maximum_font_size_raw ) ) {
 674          /*
 675           * If a minimum size was not passed to this function
 676           * and the user-defined font size is lower than $minimum_font_size_limit,
 677           * do not calculate a fluid value.
 678           */
 679          if ( $preferred_size['value'] <= $minimum_font_size_limit['value'] ) {
 680              return $preset['size'];
 681          }
 682      }
 683  
 684      // If no fluid max font size is available use the incoming value.
 685      if ( ! $maximum_font_size_raw ) {
 686          $maximum_font_size_raw = $preferred_size['value'] . $preferred_size['unit'];
 687      }
 688  
 689      /*
 690       * If no minimumFontSize is provided, create one using
 691       * the given font size multiplied by the min font size scale factor.
 692       */
 693      if ( ! $minimum_font_size_raw ) {
 694          $preferred_font_size_in_px = 'px' === $preferred_size['unit'] ? $preferred_size['value'] : $preferred_size['value'] * 16;
 695  
 696          /*
 697           * The scale factor is a multiplier that affects how quickly the curve will move towards the minimum,
 698           * that is, how quickly the size factor reaches 0 given increasing font size values.
 699           * For a - b * log2(), lower values of b will make the curve move towards the minimum faster.
 700           * The scale factor is constrained between min and max values.
 701           */
 702          $minimum_font_size_factor     = clamp( 1 - 0.075 * log( $preferred_font_size_in_px, 2 ), $default_minimum_font_size_factor_min, $default_minimum_font_size_factor_max );
 703          $calculated_minimum_font_size = round( $preferred_size['value'] * $minimum_font_size_factor, 3 );
 704  
 705          // Only use calculated min font size if it's > $minimum_font_size_limit value.
 706          if ( ! empty( $minimum_font_size_limit ) && $calculated_minimum_font_size <= $minimum_font_size_limit['value'] ) {
 707              $minimum_font_size_raw = $minimum_font_size_limit['value'] . $minimum_font_size_limit['unit'];
 708          } else {
 709              $minimum_font_size_raw = $calculated_minimum_font_size . $preferred_size['unit'];
 710          }
 711      }
 712  
 713      $fluid_font_size_value = wp_get_computed_fluid_typography_value(
 714          array(
 715              'minimum_viewport_width' => $minimum_viewport_width,
 716              'maximum_viewport_width' => $maximum_viewport_width,
 717              'minimum_font_size'      => $minimum_font_size_raw,
 718              'maximum_font_size'      => $maximum_font_size_raw,
 719              'scale_factor'           => $default_scale_factor,
 720          )
 721      );
 722  
 723      if ( ! empty( $fluid_font_size_value ) ) {
 724          return $fluid_font_size_value;
 725      }
 726  
 727      return $preset['size'];
 728  }
 729  
 730  // Register the block support.
 731  WP_Block_Supports::get_instance()->register(
 732      'typography',
 733      array(
 734          'register_attribute' => 'wp_register_typography_support',
 735          'apply'              => 'wp_apply_typography_support',
 736      )
 737  );


Generated : Thu Dec 3 08:20:02 2026 Cross-referenced by PHPXref