PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | modules/google-fonts/current/class-jetpack-google-font-face.php +143 -55 13.5.2 → 16.3-beta View file →
@@ -5,11 +5,11 @@
5 5 * @package automattic/jetpack
6 6 */
7 7
8 8 /**
9 - * Jetpack Google Font Face disables Font Face hooks in Core that prints **ALL** font faces.
10 - * Instead, it collects fonts that are used in global styles or block-level settings and
11 - * print those fonts in use.
9 + * Prints used legacy catalogue faces while native font printers handle theme and user fonts.
10 + *
11 + * @phan-constructor-used-for-side-effects
12 12 */
13 13 class Jetpack_Google_Font_Face {
14 14 /**
15 15 * The fonts that are used in global styles or block-level settings.
@@ -21,23 +21,27 @@
21 21 /**
22 22 * The constructor.
23 23 */
24 24 public function __construct() {
25 - // Turns off hooks to print fonts
25 + // Retire the legacy printer while keeping native font-face printing.
26 26 add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
27 27 add_action( 'current_screen', array( $this, 'current_screen' ), 10 );
28 28
29 29 // Collect and print fonts in use
30 - add_action( 'wp_head', array( $this, 'print_font_faces' ), 50 );
30 + if ( wp_is_block_theme() ) {
31 + add_action( 'wp_head', array( $this, 'print_font_faces' ), 50 );
32 + } else {
33 + // In classic themes wp_head runs before the blocks are processed to collect the block fonts.
34 + add_action( 'wp_footer', array( $this, 'print_font_faces' ), 50 );
35 + }
31 36 add_filter( 'pre_render_block', array( $this, 'collect_block_fonts' ), 10, 2 );
32 37 }
33 38
34 39 /**
35 - * Turn off hooks to print fonts on frontend
40 + * Turn off the legacy webfonts printer on the frontend.
36 41 */
37 42 public function wp_loaded() {
38 43 remove_action( 'wp_head', 'wp_print_fonts', 50 );
39 - remove_action( 'wp_head', 'wp_print_font_faces', 50 );
40 44 }
41 45
42 46 /**
43 47 * Turn off hooks to print fonts on wp-admin, except for GB editor pages.
@@ -53,42 +57,149 @@
53 57 /**
54 58 * Print fonts that are used in global styles or block-level settings.
55 59 */
56 60 public function print_font_faces() {
57 - // @todo Remove suppression when we drop support for core versions without the Font library.
58 - // @phan-suppress-next-line PhanUndeclaredClassMethod -- This file is only loaded (via modules/google-fonts/load.php) when core's new Font library exists. @phan-suppress-current-line UnusedPluginSuppression
59 - $fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
60 - $font_slug_aliases = $this->get_font_slug_aliases();
61 - $fonts_to_print = array();
61 + if ( jetpack_google_fonts_load_font_faces() ) {
62 + return;
63 + }
62 64
63 65 $this->collect_global_styles_fonts();
64 - $fonts_in_use = array_values( array_unique( $this->fonts_in_use, SORT_STRING ) );
65 - $fonts_in_use = array_map(
66 - function ( $font_slug ) use ( $font_slug_aliases ) {
67 - return $font_slug_aliases[ $font_slug ] ?? $font_slug;
68 - },
69 - $this->fonts_in_use
66 + $fonts_in_use = array_fill_keys( $this->fonts_in_use, true );
67 + if ( empty( $fonts_in_use ) ) {
68 + return;
69 + }
70 +
71 + $settings = function_exists( 'gutenberg_get_global_settings' )
72 + ? gutenberg_get_global_settings()
73 + : wp_get_global_settings();
74 + $native_slugs = array();
75 + $native_faces = array();
76 + foreach ( $settings['typography']['fontFamilies'] ?? array() as $origin => $families ) {
77 + foreach ( $families as $family ) {
78 + if ( 'default' === $origin && empty( $family['fontFace'] ) ) {
79 + continue;
80 + }
81 + if ( empty( $family['fontFamily'] ) ) {
82 + continue;
83 + }
84 + $key = $this->format_font( self::get_font_family_name( $family ) );
85 + if ( ! empty( $family['slug'] ) ) {
86 + $native_slugs[ $this->format_font( $family['slug'] ) ] = $key;
87 + }
88 + if ( ! empty( $family['fontFace'] ) ) {
89 + foreach ( $family['fontFace'] as $face ) {
90 + $native_faces[ $key ][] = $face;
91 + }
92 + }
93 + }
94 + }
95 +
96 + // Presets without their own faces can still refer to a legacy catalogue family.
97 + $fonts_in_use = array_fill_keys(
98 + array_map(
99 + static function ( $font ) use ( $native_slugs ) {
100 + return $native_slugs[ $font ] ?? $font;
101 + },
102 + array_keys( $fonts_in_use )
103 + ),
104 + true
70 105 );
71 106
72 - foreach ( $fonts as $font_faces ) {
73 - $font_family = $font_faces[0]['font-family'] ?? '';
74 - if ( in_array( $this->format_font( $font_family ), $fonts_in_use, true ) ) {
75 - $fonts_to_print[] = $font_faces;
107 + $data = jetpack_get_google_fonts_data();
108 + if ( empty( $data['fontFamilies'] ) ) {
109 + return;
110 + }
111 + $available_fonts = jetpack_get_available_google_fonts_map( $data );
112 + $fonts_to_print = array();
113 + foreach ( $data['fontFamilies'] as $family ) {
114 + if ( empty( $available_fonts[ $family['name'] ] ) || empty( $family['fontFace'] ) ) {
115 + continue;
76 116 }
117 +
118 + $name = self::get_font_family_name( $family );
119 + $key = $this->format_font( $name );
120 + $slug = $this->format_font( $family['slug'] );
121 + // A preset that reuses this slug for another family replaces it.
122 + if ( isset( $native_slugs[ $slug ] ) && $native_slugs[ $slug ] !== $key ) {
123 + continue;
124 + }
125 + if ( ! isset( $fonts_in_use[ $slug ] ) && ! isset( $fonts_in_use[ $key ] ) ) {
126 + continue;
127 + }
128 +
129 + $catalogue_faces = $family['fontFace'];
130 + if ( isset( $native_faces[ $key ] ) ) {
131 + // Native files win; the catalogue only fills styles and weights they lack.
132 + $catalogue_faces = array_filter(
133 + $catalogue_faces,
134 + static function ( $face ) use ( $native_faces, $key ) {
135 + return ! self::is_face_provided( $face, $native_faces[ $key ] );
136 + }
137 + );
138 + if ( empty( $catalogue_faces ) ) {
139 + continue;
140 + }
141 + }
142 +
143 + $faces = array();
144 + foreach ( $catalogue_faces as $face ) {
145 + $face['font-family'] = $name;
146 + $converted = array();
147 + foreach ( $face as $property => $value ) {
148 + $converted[ _wp_to_kebab_case( $property ) ] = $value;
149 + }
150 + $faces[] = $converted;
151 + }
152 + $fonts_to_print[] = $faces;
77 153 }
78 154
79 155 if ( ! empty( $fonts_to_print ) ) {
80 - // @todo Remove suppression when we drop support for core versions without the Font library.
81 - // @phan-suppress-next-line PhanUndeclaredFunction -- This file is only loaded (via modules/google-fonts/load.php) when core's new Font library exists. @phan-suppress-current-line UnusedPluginSuppression
82 156 wp_print_font_faces( $fonts_to_print );
83 157 }
84 158 }
85 159
86 160 /**
161 + * Whether a native face already covers a catalogue face's style and weight.
162 + *
163 + * @param array $face Catalogue face.
164 + * @param array[] $native_faces Native faces for the same family.
165 + * @return bool
166 + */
167 + private static function is_face_provided( $face, $native_faces ) {
168 + $style = $face['fontStyle'] ?? 'normal';
169 + list( $min, $max ) = self::get_weight_range( $face['fontWeight'] ?? '400' );
170 + foreach ( $native_faces as $native ) {
171 + if ( ( $native['fontStyle'] ?? 'normal' ) !== $style ) {
172 + continue;
173 + }
174 + list( $native_min, $native_max ) = self::get_weight_range( $native['fontWeight'] ?? '400' );
175 + // Overlap, not containment: a later catalogue rule would win at a shared weight.
176 + if ( $native_min <= $max && $min <= $native_max ) {
177 + return true;
178 + }
179 + }
180 + return false;
181 + }
182 +
183 + /**
184 + * Parses a font-weight descriptor such as "400", "bold" or "100 900".
185 + *
186 + * @param string|int $weight Font weight.
187 + * @return int[] Minimum and maximum weight.
188 + */
189 + private static function get_weight_range( $weight ) {
190 + $weight = str_replace( array( 'normal', 'bold' ), array( '400', '700' ), (string) $weight );
191 + $parts = preg_split( '/\s+/', trim( $weight ) );
192 + return array( (int) $parts[0], (int) ( $parts[1] ?? $parts[0] ) );
193 + }
194 +
195 + /**
87 196 * Collect fonts used for global styles settings.
88 197 */
89 198 public function collect_global_styles_fonts() {
90 - $global_styles = wp_get_global_styles();
199 + $global_styles = function_exists( 'gutenberg_get_global_styles' )
200 + ? gutenberg_get_global_styles()
201 + : wp_get_global_styles();
91 202
92 203 $global_styles_font_slug = $this->get_font_slug_from_setting( $global_styles );
93 204 if ( $global_styles_font_slug ) {
94 205 $this->add_font( $global_styles_font_slug );
@@ -137,9 +248,11 @@
137 248 *
138 249 * @param string $font_slug The font slug.
139 250 */
140 251 public function add_font( $font_slug ) {
141 - $this->fonts_in_use[] = $this->format_font( $font_slug );
252 + if ( is_string( $font_slug ) ) {
253 + $this->fonts_in_use[] = $this->format_font( $font_slug );
254 + }
142 255 }
143 256
144 257 /**
145 258 * Format the given font slug.
@@ -153,32 +266,8 @@
153 266 return _wp_to_kebab_case( strtolower( $font_slug ) );
154 267 }
155 268
156 269 /**
157 - * Get the font slug aliases that maps the font slug to the font family if they are different.
158 - *
159 - * The font definition may define an alias slug name, so we have to add the map from the slug name to the font family.
160 - * See https://github.com/WordPress/twentytwentyfour/blob/df92472089ede6fae5924c124a93c843b84e8cbd/theme.json#L215.
161 - */
162 - public function get_font_slug_aliases() {
163 - $font_slug_aliases = array();
164 -
165 - $theme_json = WP_Theme_JSON_Resolver::get_theme_data();
166 - $raw_data = $theme_json->get_data();
167 - if ( ! empty( $raw_data['settings']['typography']['fontFamilies'] ) ) {
168 - foreach ( $raw_data['settings']['typography']['fontFamilies'] as $font ) {
169 - $font_family_name = $this->format_font( $this->get_font_family_name( $font ) );
170 - $font_slug = $font['slug'] ?? '';
171 - if ( $font_slug && $font_slug !== $font_family_name && ! array_key_exists( $font_slug, $font_slug_aliases ) ) {
172 - $font_slug_aliases[ $font_slug ] = $font_family_name;
173 - }
174 - }
175 - }
176 -
177 - return $font_slug_aliases;
178 - }
179 -
180 - /**
181 270 * Get the font family name from a font.
182 271 *
183 272 * @param array $font The font definition object.
184 273 */
@@ -211,11 +300,10 @@
211 300 if ( ! is_string( $font_family ) ) {
212 301 return null;
213 302 }
214 303
215 - // Full string: var(--wp--preset--font-family--slug).
216 - // We do not care about the origin of the font, only its slug.
217 - preg_match( '/font-family--(?P<slug>.+)\)$/', $font_family, $matches );
304 + // A preset reference can include a var() fallback and a trailing font stack.
305 + preg_match( '/^\s*var\(\s*--wp--preset--font-family--(?P<slug>[^\s,()]+)\s*[,)]/', $font_family, $matches );
218 306
219 307 if ( isset( $matches['slug'] ) ) {
220 308 return $matches['slug'];
221 309 }
@@ -221,15 +309,15 @@
221 309 }
222 310
223 311 // Full string: var:preset|font-family|slug
224 312 // We do not care about the origin of the font, only its slug.
225 - preg_match( '/font-family\|(?P<slug>.+)$/', $font_family, $matches );
313 + preg_match( '/^\s*var:preset\|font-family\|(?P<slug>[^\s,]+)/', $font_family, $matches );
226 314
227 315 if ( isset( $matches['slug'] ) ) {
228 316 return $matches['slug'];
229 317 }
230 318
231 - return $font_family;
319 + return self::get_font_family_name( $setting['typography'] );
232 320 }
233 321
234 322 /**
235 323 * Check if the current screen is the block editor.