[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WP_Font_Face_Resolver class. 4 * 5 * @package WordPress 6 * @subpackage Fonts 7 * @since 6.4.0 8 */ 9 10 /** 11 * The Font Face Resolver abstracts the processing of different data sources 12 * (such as theme.json) for processing within the Font Face. 13 * 14 * This class is for internal core usage and is not supposed to be used by 15 * extenders (plugins and/or themes). 16 * 17 * @access private 18 */ 19 class WP_Font_Face_Resolver { 20 21 /** 22 * Gets fonts defined in theme.json. 23 * 24 * @since 6.4.0 25 * 26 * @return array Returns the font-families, each with their font-face variations. 27 */ 28 public static function get_fonts_from_theme_json() { 29 $settings = wp_get_global_settings(); 30 31 // Bail out early if there are no font settings. 32 if ( empty( $settings['typography']['fontFamilies'] ) ) { 33 return array(); 34 } 35 36 return static::parse_settings( $settings ); 37 } 38 39 /** 40 * Gets fonts defined in style variations. 41 * 42 * @since 6.7.0 43 * 44 * @return array Returns an array of font-families. 45 */ 46 public static function get_fonts_from_style_variations() { 47 $variations = WP_Theme_JSON_Resolver::get_style_variations(); 48 $fonts = array(); 49 50 if ( empty( $variations ) ) { 51 return $fonts; 52 } 53 54 foreach ( $variations as $variation ) { 55 if ( ! empty( $variation['settings']['typography']['fontFamilies']['theme'] ) ) { 56 $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] ); 57 } 58 } 59 60 $settings = array( 61 'typography' => array( 62 'fontFamilies' => array( 63 'theme' => $fonts, 64 ), 65 ), 66 ); 67 68 return static::parse_settings( $settings ); 69 } 70 71 /** 72 * Parse theme.json settings to extract font definitions with variations grouped by font-family. 73 * 74 * @since 6.4.0 75 * 76 * @param array $settings Font settings to parse. 77 * @return array Returns an array of fonts, grouped by font-family. 78 */ 79 private static function parse_settings( array $settings ) { 80 $fonts = array(); 81 82 foreach ( $settings['typography']['fontFamilies'] as $font_families ) { 83 foreach ( $font_families as $definition ) { 84 85 // Skip if "fontFace" is not defined, meaning there are no variations. 86 if ( empty( $definition['fontFace'] ) ) { 87 continue; 88 } 89 90 // Skip if "fontFamily" is not defined. 91 if ( empty( $definition['fontFamily'] ) ) { 92 continue; 93 } 94 95 $font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] ); 96 97 // Skip if no font family is defined. 98 if ( empty( $font_family_name ) ) { 99 continue; 100 } 101 102 $fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name ); 103 } 104 } 105 106 return $fonts; 107 } 108 109 /** 110 * Parse font-family name from comma-separated lists. 111 * 112 * If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ), 113 * parse and return the fist font from the list. 114 * 115 * @since 6.4.0 116 * 117 * @param string $font_family Font family `fontFamily' to parse. 118 * @return string Font-family name. 119 */ 120 private static function maybe_parse_name_from_comma_separated_list( $font_family ) { 121 if ( str_contains( $font_family, ',' ) ) { 122 $font_family = explode( ',', $font_family )[0]; 123 } 124 125 return trim( $font_family, "\"'" ); 126 } 127 128 /** 129 * Converts font-face properties from theme.json format. 130 * 131 * @since 6.4.0 132 * 133 * @param array $font_face_definition The font-face definitions to convert. 134 * @param string $font_family_property The value to store in the font-face font-family property. 135 * @return array Converted font-face properties. 136 */ 137 private static function convert_font_face_properties( array $font_face_definition, $font_family_property ) { 138 $converted_font_faces = array(); 139 140 foreach ( $font_face_definition as $font_face ) { 141 // Add the font-family property to the font-face. 142 $font_face['font-family'] = $font_family_property; 143 144 // Converts the "file:./" src placeholder into a theme font file URI. 145 if ( ! empty( $font_face['src'] ) ) { 146 $font_face['src'] = static::to_theme_file_uri( (array) $font_face['src'] ); 147 } 148 149 // Convert camelCase properties into kebab-case. 150 $font_face = static::to_kebab_case( $font_face ); 151 152 $converted_font_faces[] = $font_face; 153 } 154 155 return $converted_font_faces; 156 } 157 158 /** 159 * Converts each 'file:./' placeholder into a URI to the font file in the theme. 160 * 161 * The 'file:./' is specified in the theme's `theme.json` as a placeholder to be 162 * replaced with the URI to the font file's location in the theme. When a "src" 163 * beings with this placeholder, it is replaced, converting the src into a URI. 164 * 165 * @since 6.4.0 166 * 167 * @param array $src An array of font file sources to process. 168 * @return array An array of font file src URI(s). 169 */ 170 private static function to_theme_file_uri( array $src ) { 171 $placeholder = 'file:./'; 172 173 foreach ( $src as $src_key => $src_url ) { 174 // Skip if the src doesn't start with the placeholder, as there's nothing to replace. 175 if ( ! str_starts_with( $src_url, $placeholder ) ) { 176 continue; 177 } 178 179 $src_file = str_replace( $placeholder, '', $src_url ); 180 $src[ $src_key ] = get_theme_file_uri( $src_file ); 181 } 182 183 return $src; 184 } 185 186 /** 187 * Converts all first dimension keys into kebab-case. 188 * 189 * @since 6.4.0 190 * 191 * @param array $data The array to process. 192 * @return array Data with first dimension keys converted into kebab-case. 193 */ 194 private static function to_kebab_case( array $data ) { 195 foreach ( $data as $key => $value ) { 196 $kebab_case = _wp_to_kebab_case( $key ); 197 $data[ $kebab_case ] = $value; 198 if ( $kebab_case !== $key ) { 199 unset( $data[ $key ] ); 200 } 201 } 202 203 return $data; 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Wed Dec 25 08:20:01 2024 | Cross-referenced by PHPXref |