|
| 1 | +<?php |
| 2 | + |
| 3 | +function gutenberg_get_family_indexes_from_theme_json( $font_families ) { |
| 4 | + $font_families_by_index = array(); |
| 5 | + |
| 6 | + foreach ( $font_families as $index => $font_family ) { |
| 7 | + $font_families_by_index[ WP_Webfonts::get_font_slug( $font_family ) ] = $index; |
| 8 | + } |
| 9 | + |
| 10 | + return $font_families_by_index; |
| 11 | +} |
| 12 | + |
| 13 | +function gutenberg_transform_font_face_to_camel_case( $font_face ) { |
| 14 | + $camel_cased = array(); |
| 15 | + |
| 16 | + foreach ( $font_face as $key => $value ) { |
| 17 | + $camel_cased[ lcfirst( str_replace( '-', '', ucwords( $key, '-' ) ) ) ] = $value; |
| 18 | + } |
| 19 | + |
| 20 | + return $camel_cased; |
| 21 | +} |
| 22 | + |
| 23 | +function gutenberg_transform_font_face_to_kebab_case( $font_face ) { |
| 24 | + $new_face = array(); |
| 25 | + |
| 26 | + foreach ( $font_face as $key => $value ) { |
| 27 | + $new_face[ _wp_to_kebab_case( $key ) ] = $value; |
| 28 | + } |
| 29 | + |
| 30 | + return $new_face; |
| 31 | +} |
| 32 | + |
| 33 | +function gutenberg_transform_font_faces_to_theme_json_format( $font_faces ) { |
| 34 | + $transformed_font_faces = array(); |
| 35 | + |
| 36 | + foreach ( $font_faces as $font_face ) { |
| 37 | + $transformed_font_faces[] = array_merge( |
| 38 | + array( |
| 39 | + 'origin' => 'gutenberg_wp_webfonts_api', |
| 40 | + 'dynamicallyIncludedIntoThemeJSON' => true, |
| 41 | + ), |
| 42 | + gutenberg_transform_font_face_to_camel_case( $font_face ) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return $transformed_font_faces; |
| 47 | +} |
| 48 | + |
| 49 | +function gutenberg_transform_font_family_to_theme_json_format( $slug, $font_faces ) { |
| 50 | + $family_name = $font_faces[0]['font-family']; |
| 51 | + |
| 52 | + $providers_for_family = array(); |
| 53 | + |
| 54 | + foreach ( $font_faces as $font_face ) { |
| 55 | + $providers_for_family[] = $font_face['provider']; |
| 56 | + } |
| 57 | + |
| 58 | + $providers_for_family = array_unique( $providers_for_family ); |
| 59 | + |
| 60 | + if ( 1 === count( $providers_for_family ) ) { |
| 61 | + foreach ( $font_faces as $index => $font_face ) { |
| 62 | + unset( $font_faces[ $index ]['provider'] ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $font_family_in_theme_json_format = array( |
| 67 | + 'origin' => 'gutenberg_wp_webfonts_api', |
| 68 | + 'dynamicallyIncludedIntoThemeJSON' => true, |
| 69 | + 'fontFamily' => str_contains( $family_name, ' ' ) ? "'{$family_name}'" : $family_name, |
| 70 | + 'name' => $family_name, |
| 71 | + 'slug' => $slug, |
| 72 | + 'fontFaces' => gutenberg_transform_font_faces_to_theme_json_format( $font_faces ), |
| 73 | + ); |
| 74 | + |
| 75 | + if ( 1 === count( $providers_for_family ) ) { |
| 76 | + $font_family_in_theme_json_format['provider'] = $providers_for_family[0]; |
| 77 | + } |
| 78 | + |
| 79 | + return $font_family_in_theme_json_format; |
| 80 | +} |
| 81 | + |
| 82 | +function gutenberg_is_webfont_equal( $a, $b ) { |
| 83 | + $equality_attrs = array( |
| 84 | + 'font-family', |
| 85 | + 'font-style', |
| 86 | + 'font-weight', |
| 87 | + ); |
| 88 | + |
| 89 | + foreach ( $equality_attrs as $attr ) { |
| 90 | + if ( $a[ $attr ] !== $b[ $attr ] ) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +function gutenberg_find_webfont( $webfonts, $webfont_to_find ) { |
| 99 | + foreach ( $webfonts as $index => $webfont ) { |
| 100 | + if ( gutenberg_is_webfont_equal( $webfont, $webfont_to_find ) ) { |
| 101 | + return $index; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return false; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Add missing fonts data to the global styles. |
| 110 | + * |
| 111 | + * @param array $data The global styles. |
| 112 | + * @return array The global styles with missing fonts data. |
| 113 | + */ |
| 114 | +function gutenberg_add_programmatically_registered_webfonts_to_theme_json( $data ) { |
| 115 | + $programmatically_registered_font_families = wp_webfonts()->get_registered_webfonts(); |
| 116 | + |
| 117 | + // Make sure the path to settings.typography.fontFamilies.theme exists |
| 118 | + // before adding missing fonts. |
| 119 | + if ( empty( $data['settings'] ) ) { |
| 120 | + $data['settings'] = array(); |
| 121 | + } |
| 122 | + if ( empty( $data['settings']['typography'] ) ) { |
| 123 | + $data['settings']['typography'] = array(); |
| 124 | + } |
| 125 | + if ( empty( $data['settings']['typography']['fontFamilies'] ) ) { |
| 126 | + $data['settings']['typography']['fontFamilies'] = array(); |
| 127 | + } |
| 128 | + |
| 129 | + $font_family_indexes_in_theme_json = gutenberg_get_family_indexes_from_theme_json( $data['settings']['typography']['fontFamilies'] ); |
| 130 | + |
| 131 | + foreach ( $programmatically_registered_font_families as $slug => $programmatically_registered_font_faces ) { |
| 132 | + // This programmatically registered font family does not exist in theme.json, so let's add it, specifying its origin. |
| 133 | + if ( ! isset( $font_family_indexes_in_theme_json[ $slug ] ) ) { |
| 134 | + $data['settings']['typography']['fontFamilies'][] = gutenberg_transform_font_family_to_theme_json_format( |
| 135 | + $slug, |
| 136 | + $programmatically_registered_font_faces |
| 137 | + ); |
| 138 | + |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + // We know that the programmatically registered font family exists in theme.json at this point. |
| 143 | + |
| 144 | + $font_family_index_in_theme_json = $font_family_indexes_in_theme_json[ $slug ]; |
| 145 | + $font_family_in_theme_json = $data['settings']['typography']['fontFamilies'][ $font_family_index_in_theme_json ]; |
| 146 | + |
| 147 | + /** |
| 148 | + * The theme.json entry is specifying a provider at the top level, so that means it'll try to register the whole family later. |
| 149 | + * Let's make theme.json take precedence over the API as the source of truth, |
| 150 | + * and unregister the programmatically registered font family. |
| 151 | + */ |
| 152 | + if ( isset( $font_family_in_theme_json['provider'] ) ) { |
| 153 | + wp_webfonts()->unregister_font_family_by_slug( $slug ); |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * The entry in `theme.json` is not registering any font faces, so let's add them so them shows up in the editor. |
| 159 | + */ |
| 160 | + if ( ! isset( $font_family_in_theme_json['fontFaces'] ) ) { |
| 161 | + $data['settings']['typography']['fontFamilies'][ $font_family_index_in_theme_json ]['fontFaces'] = gutenberg_transform_font_faces_to_theme_json_format( $programmatically_registered_font_faces ); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * There are font faces being registered, so let's add only the ones that are missing |
| 167 | + */ |
| 168 | + |
| 169 | + $font_faces_to_add_to_theme_json = $programmatically_registered_font_faces; |
| 170 | + |
| 171 | + /** |
| 172 | + * Let's un-register from the API the font faces that theme.json is going to register. |
| 173 | + * Remember: theme.json is the source of truth here. |
| 174 | + */ |
| 175 | + foreach ( $font_family_in_theme_json['fontFaces'] as $index => $font_face_in_theme_json ) { |
| 176 | + $font_face_to_register_index = gutenberg_find_webfont( $font_faces_to_add_to_theme_json, gutenberg_transform_font_face_to_kebab_case( $font_face_in_theme_json ) ); |
| 177 | + |
| 178 | + if ( isset( $font_face_in_theme_json['provider'] ) ) { |
| 179 | + // It'll register the font face in theme.json, and we don't want to re-register it, |
| 180 | + // so lets remove it from the API registry. |
| 181 | + if ( false !== $font_face_to_register_index ) { |
| 182 | + wp_webfonts()->unregister_font_face_by_index( $slug, $font_face_to_register_index ); |
| 183 | + unset( $font_faces_to_add_to_theme_json[ $font_face_to_register_index ] ); |
| 184 | + } |
| 185 | + |
| 186 | + continue; |
| 187 | + } |
| 188 | + |
| 189 | + // If listed in theme.json, but found in programmatically registered font faces, let's signal it. |
| 190 | + if ( false !== $font_face_to_register_index ) { |
| 191 | + $data['settings']['typography']['fontFamilies'][ $font_family_index_in_theme_json ]['fontFaces'][ $index ] = $font_faces_to_add_to_theme_json[ $font_face_to_register_index ]; |
| 192 | + $data['settings']['typography']['fontFamilies'][ $font_family_index_in_theme_json ]['fontFaces'][ $index ]['origin'] = 'gutenberg_wp_webfonts_api'; |
| 193 | + // And remove from the list of font faces to add to the family in theme.json. |
| 194 | + unset( $font_faces_to_add_to_theme_json[ $font_face_to_register_index ] ); |
| 195 | + continue; |
| 196 | + } |
| 197 | + |
| 198 | + trigger_error( |
| 199 | + sprintf( |
| 200 | + 'The %s:%s:%s font face specified in theme.json is not registered programmatically, nor through theme.json.', |
| 201 | + $font_face_in_theme_json['fontFamily'], |
| 202 | + $font_face_in_theme_json['fontStyle'], |
| 203 | + $font_face_in_theme_json['fontWeight'], |
| 204 | + ) |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Finally, let's add the remaining programmatically registered font faces to the respective font family entry. |
| 210 | + */ |
| 211 | + |
| 212 | + foreach ( $font_faces_to_add_to_theme_json as $font_face_to_add_to_theme_json ) { |
| 213 | + $data['settings']['typography']['fontFamilies'][ $font_family_index_in_theme_json ]['fontFaces'][] = array_merge( |
| 214 | + array( |
| 215 | + 'origin' => 'gutenberg_wp_webfonts_api', |
| 216 | + 'dynamicallyIncludedIntoThemeJSON' => true, |
| 217 | + ), |
| 218 | + gutenberg_transform_font_face_to_camel_case( $font_face_to_add_to_theme_json ) |
| 219 | + ); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return $data; |
| 224 | +} |
0 commit comments