|
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 * Parse theme.json settings to extract font definitions with variations grouped by font-family. |
|
41 * |
|
42 * @since 6.4.0 |
|
43 * |
|
44 * @param array $settings Font settings to parse. |
|
45 * @return array Returns an array of fonts, grouped by font-family. |
|
46 */ |
|
47 private static function parse_settings( array $settings ) { |
|
48 $fonts = array(); |
|
49 |
|
50 foreach ( $settings['typography']['fontFamilies'] as $font_families ) { |
|
51 foreach ( $font_families as $definition ) { |
|
52 |
|
53 // Skip if "fontFace" is not defined, meaning there are no variations. |
|
54 if ( empty( $definition['fontFace'] ) ) { |
|
55 continue; |
|
56 } |
|
57 |
|
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 } |
|
69 |
|
70 $fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name ); |
|
71 } |
|
72 } |
|
73 |
|
74 return $fonts; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Parse font-family name from comma-separated lists. |
|
79 * |
|
80 * If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ), |
|
81 * parse and return the fist font from the list. |
|
82 * |
|
83 * @since 6.4.0 |
|
84 * |
|
85 * @param string $font_family Font family `fontFamily' to parse. |
|
86 * @return string Font-family name. |
|
87 */ |
|
88 private static function maybe_parse_name_from_comma_separated_list( $font_family ) { |
|
89 if ( str_contains( $font_family, ',' ) ) { |
|
90 $font_family = explode( ',', $font_family )[0]; |
|
91 } |
|
92 |
|
93 return trim( $font_family, "\"'" ); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Converts font-face properties from theme.json format. |
|
98 * |
|
99 * @since 6.4.0 |
|
100 * |
|
101 * @param array $font_face_definition The font-face definitions to convert. |
|
102 * @param string $font_family_property The value to store in the font-face font-family property. |
|
103 * @return array Converted font-face properties. |
|
104 */ |
|
105 private static function convert_font_face_properties( array $font_face_definition, $font_family_property ) { |
|
106 $converted_font_faces = array(); |
|
107 |
|
108 foreach ( $font_face_definition as $font_face ) { |
|
109 // Add the font-family property to the font-face. |
|
110 $font_face['font-family'] = $font_family_property; |
|
111 |
|
112 // Converts the "file:./" src placeholder into a theme font file URI. |
|
113 if ( ! empty( $font_face['src'] ) ) { |
|
114 $font_face['src'] = static::to_theme_file_uri( (array) $font_face['src'] ); |
|
115 } |
|
116 |
|
117 // Convert camelCase properties into kebab-case. |
|
118 $font_face = static::to_kebab_case( $font_face ); |
|
119 |
|
120 $converted_font_faces[] = $font_face; |
|
121 } |
|
122 |
|
123 return $converted_font_faces; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Converts each 'file:./' placeholder into a URI to the font file in the theme. |
|
128 * |
|
129 * The 'file:./' is specified in the theme's `theme.json` as a placeholder to be |
|
130 * replaced with the URI to the font file's location in the theme. When a "src" |
|
131 * beings with this placeholder, it is replaced, converting the src into a URI. |
|
132 * |
|
133 * @since 6.4.0 |
|
134 * |
|
135 * @param array $src An array of font file sources to process. |
|
136 * @return array An array of font file src URI(s). |
|
137 */ |
|
138 private static function to_theme_file_uri( array $src ) { |
|
139 $placeholder = 'file:./'; |
|
140 |
|
141 foreach ( $src as $src_key => $src_url ) { |
|
142 // Skip if the src doesn't start with the placeholder, as there's nothing to replace. |
|
143 if ( ! str_starts_with( $src_url, $placeholder ) ) { |
|
144 continue; |
|
145 } |
|
146 |
|
147 $src_file = str_replace( $placeholder, '', $src_url ); |
|
148 $src[ $src_key ] = get_theme_file_uri( $src_file ); |
|
149 } |
|
150 |
|
151 return $src; |
|
152 } |
|
153 |
|
154 /** |
|
155 * Converts all first dimension keys into kebab-case. |
|
156 * |
|
157 * @since 6.4.0 |
|
158 * |
|
159 * @param array $data The array to process. |
|
160 * @return array Data with first dimension keys converted into kebab-case. |
|
161 */ |
|
162 private static function to_kebab_case( array $data ) { |
|
163 foreach ( $data as $key => $value ) { |
|
164 $kebab_case = _wp_to_kebab_case( $key ); |
|
165 $data[ $kebab_case ] = $value; |
|
166 if ( $kebab_case !== $key ) { |
|
167 unset( $data[ $key ] ); |
|
168 } |
|
169 } |
|
170 |
|
171 return $data; |
|
172 } |
|
173 } |