WP_Font_Face_Resolver
- Source
wp-includes/fonts/class-wp-font-face-resolver.php:19
The Font Face Resolver abstracts the processing of different data sources (such as theme.json) for processing within the Font Face.
Description
This class is for internal core usage and is not supposed to be used by extenders (plugins and/or themes).
Compatibility
- WordPress
- core
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Methods · 7
- get_fonts_from_theme_json()Gets fonts defined in theme.json.
- get_fonts_from_style_variations()Gets fonts defined in style variations.
- parse_settings()Parse theme.json settings to extract font definitions with variations grouped by font-family.
- maybe_parse_name_from_comma_separated_list()Parse font-family name from comma-separated lists.
- convert_font_face_properties()Converts font-face properties from theme.json format.
- to_theme_file_uri()Converts each 'file:./' placeholder into a URI to the font file in the theme.
- to_kebab_case()Converts all first dimension keys into kebab-case.
Source code
class WP_Font_Face_Resolver { /** * Gets fonts defined in theme.json. * * @since 6.4.0 * * @return array Returns the font-families, each with their font-face variations. */ public static function get_fonts_from_theme_json() { $settings = wp_get_global_settings(); // Bail out early if there are no font settings. if ( empty( $settings['typography']['fontFamilies'] ) ) { return array(); } return static::parse_settings( $settings ); } /** * Gets fonts defined in style variations. * * @since 6.7.0 * * @return array Returns an array of font-families. */ public static function get_fonts_from_style_variations() { $variations = WP_Theme_JSON_Resolver::get_style_variations(); $fonts = array(); if ( empty( $variations ) ) { return $fonts; } foreach ( $variations as $variation ) { if ( ! empty( $variation['settings']['typography']['fontFamilies']['theme'] ) ) { $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] ); } } $settings = array( 'typography' => array( 'fontFamilies' => array( 'theme' => $fonts, ), ), ); return static::parse_settings( $settings ); } /** * Parse theme.json settings to extract font definitions with variations grouped by font-family. * * @since 6.4.0 * * @param array $settings Font settings to parse. * @return array Returns an array of fonts, grouped by font-family. */ private static function parse_settings( array $settings ) { $fonts = array(); foreach ( $settings['typography']['fontFamilies'] as $font_families ) { foreach ( $font_families as $definition ) { // Skip if "fontFace" is not defined, meaning there are no variations. if ( empty( $definition['fontFace'] ) ) { continue; } // Skip if "fontFamily" is not defined. if ( empty( $definition['fontFamily'] ) ) { continue; } $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] ); // Skip if no font family is defined. if ( empty( $font_family_name ) ) {Changelog
Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/fonts/class-wp-font-face-resolver.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.