Make WordPress Core


Ignore:
Timestamp:
10/11/2022 06:42:20 PM (2 years ago)
Author:
davidbaumwald
Message:

Editor: Merge latest fluid typography bugfixes for 6.1 Release Candidate 1.

Merges the following:

Follow-up to [54280].

Props andrewserong, isabel_brison, ramonopoly.
See #56467.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/typography.php

    r54387 r54497  
    120120            ? $block_attributes['style']['typography']['fontSize']
    121121            : null;
    122         $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : $custom_font_size;
     122            $typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(
     123                array(
     124                    'size' => $custom_font_size,
     125                )
     126            );
    123127    }
    124128
     
    246250
    247251/**
     252 * Renders typography styles/content to the block wrapper.
     253 *
     254 * @since 6.1.0
     255 *
     256 * @param string $block_content Rendered block content.
     257 * @param array  $block         Block object.
     258 * @return string Filtered block content.
     259 */
     260function wp_render_typography_support( $block_content, $block ) {
     261    if ( ! isset( $block['attrs']['style']['typography']['fontSize'] ) ) {
     262        return $block_content;
     263    }
     264
     265    $custom_font_size = $block['attrs']['style']['typography']['fontSize'];
     266    $fluid_font_size  = wp_get_typography_font_size_value( array( 'size' => $custom_font_size ) );
     267
     268    /*
     269     * Checks that $fluid_font_size does not match $custom_font_size,
     270     * which means it's been mutated by the fluid font size functions.
     271     */
     272    if ( ! empty( $fluid_font_size ) && $fluid_font_size !== $custom_font_size ) {
     273        // Replaces the first instance of `font-size:$custom_font_size` with `font-size:$fluid_font_size`.
     274        return preg_replace( '/font-size\s*:\s*' . preg_quote( $custom_font_size, '/' ) . '\s*;?/', 'font-size:' . esc_attr( $fluid_font_size ) . ';', $block_content, 1 );
     275    }
     276
     277    return $block_content;
     278}
     279
     280/**
    248281 * Checks a string for a unit and value and returns an array
    249  * consisting of `'value'` and `'unit'`, e.g., [ '42', 'rem' ].
     282 * consisting of `'value'` and `'unit'`, e.g. array( '42', 'rem' ).
    250283 *
    251284 * @since 6.1.0
    252  * @access private
    253  *
    254  * @param string $raw_value Raw size value from theme.json.
    255  * @param array  $options   {
     285 *
     286 * @param string|int|float $raw_value Raw size value from theme.json.
     287 * @param array            $options   {
    256288 *     Optional. An associative array of options. Default is empty array.
    257289 *
    258290 *     @type string   $coerce_to        Coerce the value to rem or px. Default `'rem'`.
    259291 *     @type int      $root_size_value  Value of root font size for rem|em <-> px conversion. Default `16`.
    260  *     @type string[] $acceptable_units An array of font size units. Default `[ 'rem', 'px', 'em' ]`;
     292 *     @type string[] $acceptable_units An array of font size units. Default `array( 'rem', 'px', 'em' )`;
    261293 * }
    262294 * @return array|null An array consisting of `'value'` and `'unit'` properties on success.
     
    264296 */
    265297function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
     298    if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) {
     299        _doing_it_wrong(
     300            __FUNCTION__,
     301            __( 'Raw size value must be a string, integer, or float.' ),
     302            '6.1.0'
     303        );
     304        return null;
     305    }
     306
    266307    if ( empty( $raw_value ) ) {
    267308        return null;
     309    }
     310
     311    // Converts numbers to pixel values by default.
     312    if ( is_numeric( $raw_value ) ) {
     313        $raw_value = $raw_value . 'px';
    268314    }
    269315
     
    289335    $unit  = $matches[2];
    290336
    291     // Default browser font size. Later, possibly could inject some JS to
    292     // compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
     337    /*
     338     * Default browser font size. Later, possibly could inject some JS to
     339     * compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
     340     */
    293341    if ( 'px' === $options['coerce_to'] && ( 'em' === $unit || 'rem' === $unit ) ) {
    294342        $value = $value * $options['root_size_value'];
     
    324372 *     @type int    $scale_factor           A scale factor to determine how fast a font scales within boundaries.
    325373 * }
    326  * @return string|null A font-size value using clamp() on success. Else, null.
     374 * @return string|null A font-size value using clamp() on success, otherwise null.
    327375 */
    328376function wp_get_computed_fluid_typography_value( $args = array() ) {
     
    396444 *     Required. fontSizes preset value as seen in theme.json.
    397445 *
    398  *     @type string $name Name of the font size preset.
    399  *     @type string $slug Kebab-case unique identifier for the font size preset.
    400  *     @type string $size CSS font-size value, including units where applicable.
     446 *     @type string           $name Name of the font size preset.
     447 *     @type string           $slug Kebab-case, unique identifier for the font size preset.
     448 *     @type string|int|float $size CSS font-size value, including units if applicable.
    401449 * }
    402450 * @param bool  $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing.
    403  *                                           Default is `false`.
    404  * @return string Font-size value.
     451 *                                           Default is false.
     452 * @return string|null Font-size value or null if a size is not passed in $preset.
    405453 */
    406454function wp_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
     455    if ( ! isset( $preset['size'] ) ) {
     456        return null;
     457    }
     458
     459    /*
     460     * Catches empty values and 0/'0'.
     461     * Fluid calculations cannot be performed on 0.
     462     */
     463    if ( empty( $preset['size'] ) ) {
     464        return $preset['size'];
     465    }
     466
    407467    // Checks if fluid font sizes are activated.
    408468    $typography_settings         = wp_get_global_settings( array( 'typography' ) );
Note: See TracChangeset for help on using the changeset viewer.