Make WordPress Core


Ignore:
Timestamp:
09/25/2023 09:27:51 PM (19 months ago)
Author:
hellofromTonya
Message:

Fonts: Get font-family name from 'fontFamily' field.

Instead of getting the name from the optional 'name' field, the font-family name now comes from the required 'fontFamily' field.

This change fixes a back-compat (BC) break in how the font-family name is pulled from the incoming font data in the WP_Font_Face_Resolver.

Why?

WP Core does not require the 'name' field in theme.json. For themes that do not declare it, that set of font variations is ignored, thus causing a BC break from how the stopgap code worked (see [53282]).

However, WP_Theme_JSON schema does require the fontFamily field in each of the typography.fontFamilies.

Other details:

Includes a parser to extract the first entry when a fontFamily field has a comma-separated list of font-families, e.g. Inter, sans-serif.

References:

Follow-up to [56500], [53282].

Props ironprogrammer, hellofromTonya, mmaattiiaass, pbking.
Fixes #59165.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/fonts/class-wp-font-face-resolver.php

    r56500 r56688  
    5151            foreach ( $font_families as $definition ) {
    5252
    53                 // Skip if font-family "name" is not defined.
    54                 if ( empty( $definition['name'] ) ) {
    55                     continue;
    56                 }
    57 
    5853                // Skip if "fontFace" is not defined, meaning there are no variations.
    5954                if ( empty( $definition['fontFace'] ) ) {
     
    6156                }
    6257
    63                 $font_family = $definition['name'];
     58                // Skip if "fontFamily" is not defined.
     59                if ( empty( $definition['fontFamily'] ) ) {
     60                    continue;
     61                }
     62
     63                $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] );
     64
     65                // Skip if no font family is defined.
     66                if ( empty( $font_family_name ) ) {
     67                    continue;
     68                }
    6469
    6570                // Prepare the fonts array structure for this font-family.
    66                 if ( ! array_key_exists( $font_family, $fonts ) ) {
    67                     $fonts[ $font_family ] = array();
     71                if ( ! array_key_exists( $font_family_name, $fonts ) ) {
     72                    $fonts[ $font_family_name ] = array();
    6873                }
    6974
    70                 $fonts[ $font_family ] = static::convert_font_face_properties( $definition['fontFace'], $font_family );
     75                $fonts[ $font_family_name ] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
    7176            }
    7277        }
    7378
    7479        return $fonts;
     80    }
     81
     82    /**
     83     * Parse font-family name from comma-separated lists.
     84     *
     85     * If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ),
     86     * parse and return the fist font from the list.
     87     *
     88     * @since 6.4.0
     89     *
     90     * @param string $font_family Font family `fontFamily' to parse.
     91     * @return string Font-family name.
     92     */
     93    private static function maybe_parse_name_from_comma_separated_list( $font_family ) {
     94        if ( str_contains( $font_family, ',' ) ) {
     95            $font_family = explode( ',', $font_family )[0];
     96        }
     97
     98        return trim( $font_family, "\"'" );
    7599    }
    76100
Note: See TracChangeset for help on using the changeset viewer.