Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
22.22% |
4 / 18 |
|
0.00% |
0 / 2 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_google_fonts_proxy | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_maybe_skip_custom_fonts | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Customizations to the Google Fonts module available in Jetpack. |
| 4 | * |
| 5 | * @package wpcomsh |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The old versions of Gutenberg that don't support the Font Library very well use this constant |
| 10 | * to disable the Font Library. Leave it as it is to keep disabling the Font Library. |
| 11 | */ |
| 12 | if ( ! defined( 'FONT_LIBRARY_DISABLED' ) ) { |
| 13 | define( 'FONT_LIBRARY_DISABLED', true ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Replaces Google Fonts API references in enqueued styles with our caching reverse proxy. |
| 18 | * |
| 19 | * @see pMz3w-g6E-p2#comment-103418 |
| 20 | * |
| 21 | * @param string|array $src The source URL of the enqueued style. |
| 22 | * @return string|array |
| 23 | */ |
| 24 | function wpcomsh_google_fonts_proxy( $src ) { |
| 25 | // If an array, run the function on each item. |
| 26 | if ( is_array( $src ) ) { |
| 27 | return array_map( 'wpcomsh_google_fonts_proxy', $src ); |
| 28 | } |
| 29 | $src = str_replace( 'fonts.googleapis.com', 'fonts-api.wp.com', $src ); |
| 30 | $src = str_replace( 'fonts.gstatic.com', 'fonts.wp.com', $src ); |
| 31 | return $src; |
| 32 | } |
| 33 | add_filter( 'style_loader_src', 'wpcomsh_google_fonts_proxy' ); |
| 34 | add_filter( 'wp_resource_hints', 'wpcomsh_google_fonts_proxy' ); |
| 35 | add_filter( 'jetpack_google_fonts_api_url', 'wpcomsh_google_fonts_proxy' ); |
| 36 | add_filter( 'custom_fonts_google_fonts_api_url', 'wpcomsh_google_fonts_proxy' ); |
| 37 | add_filter( 'jetpack_global_styles_google_fonts_api_url', 'wpcomsh_google_fonts_proxy' ); |
| 38 | |
| 39 | /** |
| 40 | * Skips Custom Fonts output when the active theme has no annotation rules for the chosen fonts. |
| 41 | * |
| 42 | * Fonts saved under a classic theme's Customizer keep loading after switching to a block theme |
| 43 | * (or any unannotated theme) because maybe_render_fonts() only checks that fonts are saved, not |
| 44 | * that any CSS rules exist for them. This guard runs first and removes the action so neither the |
| 45 | * WebFont script nor the empty <style> block appear on the page. |
| 46 | * |
| 47 | * The jetpack_fonts_render_without_rules filter lets sites that reference the chosen font |
| 48 | * in Additional CSS opt back in to downloading it even without annotation rules. |
| 49 | */ |
| 50 | function wpcomsh_maybe_skip_custom_fonts() { |
| 51 | if ( ! class_exists( 'Jetpack_Fonts' ) ) { |
| 52 | return; |
| 53 | } |
| 54 | // Jetpack_Fonts is declared in both the wpcom stubs and vendor/automattic/custom-fonts; the stub lacks get_font_css(). |
| 55 | $instance = Jetpack_Fonts::get_instance(); // @phan-suppress-current-line PhanRedefinedClassReference |
| 56 | // @phan-suppress-next-line PhanRedefinedClassReference, PhanUndeclaredMethod |
| 57 | if ( '' === trim( $instance->get_font_css() ) && ! apply_filters( 'jetpack_fonts_render_without_rules', false ) ) { |
| 58 | remove_action( 'wp_enqueue_scripts', array( $instance, 'maybe_render_fonts' ) ); |
| 59 | } |
| 60 | } |
| 61 | // Priority 5 runs before maybe_render_fonts (priority 10). |
| 62 | add_action( 'wp_enqueue_scripts', 'wpcomsh_maybe_skip_custom_fonts', 5 ); |