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


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref