load-google-fonts.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Load the google fonts by the new Font Library. See pNEWy-hhx-p2. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Gets the Google Fonts data |
| 10 | * |
| 11 | * @return object[] The collection data of the Google Fonts. |
| 12 | */ |
| 13 | function jetpack_get_google_fonts_data() { |
| 14 | $default_google_fonts_api_url = 'https://fonts.gstatic.com'; |
| 15 | $jetpack_google_fonts_collection_url = 'https://s0.wp.com/i/font-collections/jetpack-google-fonts.json'; |
| 16 | $cache_key = 'jetpack_google_fonts_' . md5( $jetpack_google_fonts_collection_url ); |
| 17 | $data = get_transient( $cache_key ); |
| 18 | if ( $data === false ) { |
| 19 | $response = wp_remote_get( $jetpack_google_fonts_collection_url ); |
| 20 | if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 25 | if ( $data === null ) { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | set_transient( $cache_key, $data, DAY_IN_SECONDS ); |
| 30 | } |
| 31 | |
| 32 | // Replace the google fonts api url if the custom one is provided. |
| 33 | $custom_google_fonts_api_url = \esc_url( |
| 34 | /** This filter is documented in projects/packages/google-fonts-provider/src/class-google-fonts-provider.php */ |
| 35 | apply_filters( 'jetpack_google_fonts_api_url', $default_google_fonts_api_url ) |
| 36 | ); |
| 37 | if ( $custom_google_fonts_api_url !== $default_google_fonts_api_url ) { |
| 38 | foreach ( $data['fontFamilies'] as &$font_family ) { |
| 39 | foreach ( $font_family['fontFace'] as &$font_face ) { |
| 40 | $font_face['src'] = str_replace( |
| 41 | $default_google_fonts_api_url, |
| 42 | $custom_google_fonts_api_url, |
| 43 | $font_face['src'] |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return $data; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets the map of the available Google Fonts |
| 54 | * |
| 55 | * @param object[] $google_fonts_data The collection data of the Google Fonts. |
| 56 | * @return object[] The map of the the available Google Fonts. |
| 57 | */ |
| 58 | function jetpack_get_available_google_fonts_map( $google_fonts_data ) { |
| 59 | $jetpack_google_fonts_list = array_map( |
| 60 | function ( $font_family ) { |
| 61 | return $font_family['name']; |
| 62 | }, |
| 63 | $google_fonts_data['fontFamilies'] |
| 64 | ); |
| 65 | |
| 66 | /** This filter is documented in modules/google-fonts/wordpress-6.3/load-google-fonts.php */ |
| 67 | $google_font_list = apply_filters( 'jetpack_google_fonts_list', $jetpack_google_fonts_list ); |
| 68 | $available_google_fonts_map = array(); |
| 69 | |
| 70 | foreach ( $google_font_list as $google_font ) { |
| 71 | $available_google_fonts_map[ $google_font ] = true; |
| 72 | } |
| 73 | |
| 74 | return $available_google_fonts_map; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the font families of the active theme |
| 79 | * |
| 80 | * @return object[] The font families of the active theme. |
| 81 | */ |
| 82 | function jetpack_get_theme_fonts_map() { |
| 83 | if ( ! class_exists( 'WP_Theme_JSON_Resolver' ) ) { |
| 84 | return array(); |
| 85 | } |
| 86 | |
| 87 | $theme_json = WP_Theme_JSON_Resolver::get_theme_data(); |
| 88 | $raw_data = $theme_json->get_data(); |
| 89 | if ( empty( $raw_data['settings']['typography']['fontFamilies'] ) ) { |
| 90 | return array(); |
| 91 | } |
| 92 | |
| 93 | $theme_fonts_map = array(); |
| 94 | foreach ( $raw_data['settings']['typography']['fontFamilies'] as $font_family ) { |
| 95 | if ( isset( $font_family['name'] ) ) { |
| 96 | $theme_fonts_map[ $font_family['name'] ] = true; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $theme_fonts_map; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Register google fonts to the theme json data |
| 105 | * |
| 106 | * @param WP_Theme_JSON_Data $theme_json The theme json data of core. |
| 107 | * @return WP_Theme_JSON_Data The theme json data with registered google fonts. |
| 108 | */ |
| 109 | function jetpack_register_google_fonts_to_theme_json( $theme_json ) { |
| 110 | $google_fonts_data = jetpack_get_google_fonts_data(); |
| 111 | if ( ! $google_fonts_data ) { |
| 112 | return $theme_json; |
| 113 | } |
| 114 | |
| 115 | $available_google_fonts_map = jetpack_get_available_google_fonts_map( $google_fonts_data ); |
| 116 | $theme_fonts_map = jetpack_get_theme_fonts_map(); |
| 117 | $google_fonts_families = array_values( |
| 118 | array_filter( |
| 119 | $google_fonts_data['fontFamilies'], |
| 120 | function ( $google_fonts_family ) use ( $available_google_fonts_map, $theme_fonts_map ) { |
| 121 | $name = $google_fonts_family['name']; |
| 122 | |
| 123 | // Filter out the fonts that are provided by the active theme. |
| 124 | if ( isset( $theme_fonts_map[ $name ] ) && $theme_fonts_map[ $name ] ) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return isset( $available_google_fonts_map[ $name ] ) |
| 129 | ? $available_google_fonts_map[ $name ] |
| 130 | : false; |
| 131 | } |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | $raw_data = $theme_json->get_data(); |
| 136 | $origin = 'default'; |
| 137 | if ( empty( $raw_data['settings']['typography']['fontFamilies'][ $origin ] ) ) { |
| 138 | $raw_data['settings']['typography']['fontFamilies'][ $origin ] = array(); |
| 139 | } |
| 140 | |
| 141 | foreach ( $google_fonts_families as $font_family ) { |
| 142 | $raw_data['settings']['typography']['fontFamilies'][ $origin ][] = $font_family; |
| 143 | } |
| 144 | |
| 145 | $theme_json_class = get_class( $theme_json ); |
| 146 | return new $theme_json_class( $raw_data, $origin ); |
| 147 | } |
| 148 | |
| 149 | add_filter( 'wp_theme_json_data_default', 'jetpack_register_google_fonts_to_theme_json' ); |
| 150 |