← All changes
|
modules/google-fonts/current/load-google-fonts.php
+52
-4
16.3-a.7
→
16.3-beta
View file →
| @@ -121,8 +121,53 @@ | ||
| 121 | 121 | return $available_google_fonts_map; |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** |
| 125 | + * Whether the request URL is a REST route. | |
| 126 | + * | |
| 127 | + * Theme JSON can resolve before REST_REQUEST is defined, and the Accept header also matches front-end requests. | |
| 128 | + * | |
| 129 | + * @since 16.3 | |
| 130 | + * | |
| 131 | + * @return bool | |
| 132 | + */ | |
| 133 | +function jetpack_google_fonts_is_rest_url() { | |
| 134 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only used to classify the request. | |
| 135 | + if ( isset( $_GET['rest_route'] ) ) { | |
| 136 | + return true; | |
| 137 | + } | |
| 138 | + $path = (string) wp_parse_url( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '', PHP_URL_PATH ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Parsed for path comparison only. | |
| 139 | + return str_contains( $path, '/' . rest_get_url_prefix() . '/' ); | |
| 140 | +} | |
| 141 | + | |
| 142 | +/** | |
| 143 | + * Whether catalogue faces are needed in theme JSON for an editor or API request. | |
| 144 | + * | |
| 145 | + * @since 16.3 | |
| 146 | + * | |
| 147 | + * @return bool | |
| 148 | + */ | |
| 149 | +function jetpack_google_fonts_load_font_faces() { | |
| 150 | + $load_font_faces = is_admin() | |
| 151 | + || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) | |
| 152 | + || jetpack_google_fonts_is_rest_url() | |
| 153 | + || ( defined( 'WP_CLI' ) && WP_CLI ); | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * Filters whether catalogue font faces are loaded into theme JSON. | |
| 157 | + * | |
| 158 | + * When true, the catalogue's files reach theme JSON and the native font printer prints all of them. | |
| 159 | + * | |
| 160 | + * @module google-fonts | |
| 161 | + * | |
| 162 | + * @since 16.3 | |
| 163 | + * | |
| 164 | + * @param bool $load_font_faces Whether to load catalogue font faces. Default true for editor, API and CLI requests. | |
| 165 | + */ | |
| 166 | + return (bool) apply_filters( 'jetpack_google_fonts_load_font_faces', $load_font_faces ); | |
| 167 | +} | |
| 168 | + | |
| 169 | +/** | |
| 125 | 170 | * Register google fonts to the theme json data |
| 126 | 171 | * |
| 127 | 172 | * @param WP_Theme_JSON_Data $theme_json The theme json data of core. |
| 128 | 173 | * @return WP_Theme_JSON_Data The theme json data with registered google fonts. |
| @@ -149,9 +194,13 @@ | ||
| 149 | 194 | if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) { |
| 150 | 195 | $raw_data['settings']['typography']['fontFamilies'][ $origin ] = array(); |
| 151 | 196 | } |
| 152 | 197 | |
| 198 | + $load_font_faces = jetpack_google_fonts_load_font_faces(); | |
| 153 | 199 | foreach ( $google_fonts_families as $font_family ) { |
| 200 | + if ( ! $load_font_faces ) { | |
| 201 | + unset( $font_family['fontFace'] ); | |
| 202 | + } | |
| 154 | 203 | $raw_data['settings']['typography']['fontFamilies'][ $origin ][] = $font_family; |
| 155 | 204 | } |
| 156 | 205 | |
| 157 | 206 | $theme_json_class = get_class( $theme_json ); |
| @@ -162,10 +211,10 @@ | ||
| 162 | 211 | |
| 163 | 212 | /** |
| 164 | 213 | * Filter out the deprecated font families that are from the jetpack-google-fonts provider. |
| 165 | 214 | * |
| 166 | - * @param object[] $font_families The font families. | |
| 167 | - * @return object[] The filtered font families. | |
| 215 | + * @param array[] $font_families The font families. | |
| 216 | + * @return array[] The filtered font families. | |
| 168 | 217 | */ |
| 169 | 218 | function jetpack_google_fonts_filter_out_deprecated_font_data( $font_families ) { |
| 170 | 219 | return array_values( |
| 171 | 220 | array_filter( |
| @@ -246,7 +295,6 @@ | ||
| 246 | 295 | wp_update_post( wp_slash( (array) $changes ), true ); |
| 247 | 296 | } |
| 248 | 297 | add_action( 'jetpack_deactivate_module_google-fonts', 'jetpack_unregister_google_fonts' ); |
| 249 | 298 | |
| 250 | -// Initialize Jetpack Google Font Face to avoid printing **ALL** google fonts provided by this module. | |
| 251 | -// See p1700040028362329-slack-C4GAQ900P and p7DVsv-jib-p2 | |
| 299 | +// Print legacy catalogue faces separately from native theme and user fonts. | |
| 252 | 300 | new Jetpack_Google_Font_Face(); |