← All changes
|
modules/google-fonts/current/class-jetpack-google-font-face.php
+128
-50
16.3-a.5
→
16.3-beta
View file →
| @@ -5,11 +5,9 @@ | ||
| 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. | |
| 12 | 10 | * |
| 13 | 11 | * @phan-constructor-used-for-side-effects |
| 14 | 12 | */ |
| 15 | 13 | class Jetpack_Google_Font_Face { |
| @@ -23,9 +21,9 @@ | ||
| 23 | 21 | /** |
| 24 | 22 | * The constructor. |
| 25 | 23 | */ |
| 26 | 24 | public function __construct() { |
| 27 | - // Turns off hooks to print fonts | |
| 25 | + // Retire the legacy printer while keeping native font-face printing. | |
| 28 | 26 | add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
| 29 | 27 | add_action( 'current_screen', array( $this, 'current_screen' ), 10 ); |
| 30 | 28 | |
| 31 | 29 | // Collect and print fonts in use |
| @@ -38,16 +36,12 @@ | ||
| 38 | 36 | add_filter( 'pre_render_block', array( $this, 'collect_block_fonts' ), 10, 2 ); |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | 39 | /** |
| 42 | - * Turn off hooks to print fonts on frontend | |
| 40 | + * Turn off the legacy webfonts printer on the frontend. | |
| 43 | 41 | */ |
| 44 | 42 | public function wp_loaded() { |
| 45 | 43 | remove_action( 'wp_head', 'wp_print_fonts', 50 ); |
| 46 | - remove_action( 'wp_head', 'wp_print_font_faces', 50 ); | |
| 47 | - // Gutenberg 22.4+ overrides Core's font printing with its own function | |
| 48 | - // for classic theme support. Remove it so we can print only fonts in use. | |
| 49 | - remove_action( 'wp_head', 'gutenberg_print_font_faces', 50 ); | |
| 50 | 44 | } |
| 51 | 45 | |
| 52 | 46 | /** |
| 53 | 47 | * Turn off hooks to print fonts on wp-admin, except for GB editor pages. |
| @@ -63,26 +57,100 @@ | ||
| 63 | 57 | /** |
| 64 | 58 | * Print fonts that are used in global styles or block-level settings. |
| 65 | 59 | */ |
| 66 | 60 | public function print_font_faces() { |
| 67 | - $fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json(); | |
| 68 | - $font_slug_aliases = $this->get_font_slug_aliases(); | |
| 69 | - $fonts_to_print = array(); | |
| 61 | + if ( jetpack_google_fonts_load_font_faces() ) { | |
| 62 | + return; | |
| 63 | + } | |
| 70 | 64 | |
| 71 | 65 | $this->collect_global_styles_fonts(); |
| 72 | - $fonts_in_use = array_values( array_unique( $this->fonts_in_use, SORT_STRING ) ); | |
| 73 | - $fonts_in_use = array_map( | |
| 74 | - function ( $font_slug ) use ( $font_slug_aliases ) { | |
| 75 | - return $font_slug_aliases[ $font_slug ] ?? $font_slug; | |
| 76 | - }, | |
| 77 | - $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 | |
| 78 | 105 | ); |
| 79 | 106 | |
| 80 | - foreach ( $fonts as $font_faces ) { | |
| 81 | - $font_family = $font_faces[0]['font-family'] ?? ''; | |
| 82 | - if ( in_array( $this->format_font( $font_family ), $fonts_in_use, true ) ) { | |
| 83 | - $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; | |
| 84 | 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; | |
| 85 | 153 | } |
| 86 | 154 | |
| 87 | 155 | if ( ! empty( $fonts_to_print ) ) { |
| 88 | 156 | wp_print_font_faces( $fonts_to_print ); |
| @@ -89,12 +157,49 @@ | ||
| 89 | 157 | } |
| 90 | 158 | } |
| 91 | 159 | |
| 92 | 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 | + /** | |
| 93 | 196 | * Collect fonts used for global styles settings. |
| 94 | 197 | */ |
| 95 | 198 | public function collect_global_styles_fonts() { |
| 96 | - $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(); | |
| 97 | 202 | |
| 98 | 203 | $global_styles_font_slug = $this->get_font_slug_from_setting( $global_styles ); |
| 99 | 204 | if ( $global_styles_font_slug ) { |
| 100 | 205 | $this->add_font( $global_styles_font_slug ); |
| @@ -158,35 +263,8 @@ | ||
| 158 | 263 | * @return string The formatted font slug. |
| 159 | 264 | */ |
| 160 | 265 | public function format_font( $font_slug ) { |
| 161 | 266 | return _wp_to_kebab_case( strtolower( $font_slug ) ); |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * Get the font slug aliases that maps the font slug to the font family if they are different. | |
| 166 | - * | |
| 167 | - * The font definition may define an alias slug name, so we have to add the map from the slug name to the font family. | |
| 168 | - * See https://github.com/WordPress/twentytwentyfour/blob/df92472089ede6fae5924c124a93c843b84e8cbd/theme.json#L215. | |
| 169 | - */ | |
| 170 | - public function get_font_slug_aliases() { | |
| 171 | - $font_slug_aliases = array(); | |
| 172 | - | |
| 173 | - $theme_json = WP_Theme_JSON_Resolver::get_theme_data(); | |
| 174 | - $raw_data = $theme_json->get_data(); | |
| 175 | - if ( ! empty( $raw_data['settings']['typography']['fontFamilies'] ) ) { | |
| 176 | - foreach ( $raw_data['settings']['typography']['fontFamilies'] as $font ) { | |
| 177 | - if ( ! isset( $font['fontFamily'] ) ) { | |
| 178 | - continue; | |
| 179 | - } | |
| 180 | - $font_family_name = $this->format_font( $this->get_font_family_name( $font ) ); | |
| 181 | - $font_slug = $font['slug'] ?? ''; | |
| 182 | - if ( $font_slug && $font_slug !== $font_family_name && ! array_key_exists( $font_slug, $font_slug_aliases ) ) { | |
| 183 | - $font_slug_aliases[ $font_slug ] = $font_family_name; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - } | |
| 187 | - | |
| 188 | - return $font_slug_aliases; | |
| 189 | 267 | } |
| 190 | 268 | |
| 191 | 269 | /** |
| 192 | 270 | * Get the font family name from a font. |