bc-layer
2 years ago
class-wp-fonts-provider-local.php
2 years ago
class-wp-fonts-provider.php
2 years ago
class-wp-fonts-utils.php
2 years ago
class-wp-fonts.php
2 years ago
fonts-api.php
2 years ago
register-fonts-from-theme-json.php
2 years ago
fonts-api.php
246 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Fonts API functions. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage Fonts API |
| 7 | * @since X.X.X |
| 8 | */ |
| 9 | |
| 10 | if ( ! function_exists( 'wp_fonts' ) ) { |
| 11 | /** |
| 12 | * Initialize $wp_fonts if it has not been set. |
| 13 | * |
| 14 | * @since X.X.X |
| 15 | * |
| 16 | * @global WP_Fonts $wp_fonts |
| 17 | * |
| 18 | * @return WP_Fonts WP_Fonts instance. |
| 19 | */ |
| 20 | function wp_fonts() { |
| 21 | global $wp_fonts; |
| 22 | |
| 23 | if ( ! ( $wp_fonts instanceof WP_Fonts ) ) { |
| 24 | $wp_fonts = new WP_Fonts(); |
| 25 | |
| 26 | // Initialize. |
| 27 | $wp_fonts->register_provider( 'local', 'WP_Fonts_Provider_Local' ); |
| 28 | add_action( 'wp_head', 'wp_print_fonts', 50 ); |
| 29 | |
| 30 | /* |
| 31 | * For themes without a theme.json, admin printing is initiated by the 'admin_print_styles' hook. |
| 32 | * For themes with theme.json, admin printing is initiated by _wp_get_iframed_editor_assets(). |
| 33 | */ |
| 34 | if ( ! wp_theme_has_theme_json() ) { |
| 35 | add_action( 'admin_print_styles', 'wp_print_fonts', 50 ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return $wp_fonts; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if ( ! function_exists( 'wp_register_fonts' ) ) { |
| 44 | /** |
| 45 | * Registers one or more font-families and each of their variations. |
| 46 | * |
| 47 | * @since X.X.X |
| 48 | * |
| 49 | * @param array[] $fonts { |
| 50 | * Fonts to be registered, grouped by each font-family. |
| 51 | * |
| 52 | * @type string $font-family => array[] $variations { |
| 53 | * An array of web font variations for this font-family. |
| 54 | * Each variation has the following structure. |
| 55 | * |
| 56 | * @type array $variation { |
| 57 | * @type string $provider The provider ID. Default 'local'. |
| 58 | * @type string $font-style The font-style property. Default 'normal'. |
| 59 | * @type string $font-weight The font-weight property. Default '400'. |
| 60 | * @type string $font-display The font-display property. Default 'fallback'. |
| 61 | * } |
| 62 | * } |
| 63 | * } |
| 64 | * @return string[] Array of registered font family handles. |
| 65 | */ |
| 66 | function wp_register_fonts( array $fonts ) { |
| 67 | $registered = array(); |
| 68 | $wp_fonts = wp_fonts(); |
| 69 | |
| 70 | foreach ( $fonts as $font_family => $variations ) { |
| 71 | $font_family_handle = $wp_fonts->add_font_family( $font_family ); |
| 72 | if ( ! $font_family_handle ) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | // Register each of the variations for this font family. |
| 77 | foreach ( $variations as $variation_handle => $variation ) { |
| 78 | if ( ! WP_Fonts_Utils::is_defined( $variation_handle ) ) { |
| 79 | $variation_handle = ''; |
| 80 | } |
| 81 | |
| 82 | $wp_fonts->add_variation( $font_family_handle, $variation, $variation_handle ); |
| 83 | } |
| 84 | |
| 85 | $registered[] = $font_family_handle; |
| 86 | } |
| 87 | |
| 88 | return $registered; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if ( ! function_exists( 'wp_enqueue_fonts' ) ) { |
| 93 | /** |
| 94 | * Enqueues one or more font family and all of its variations. |
| 95 | * |
| 96 | * @since X.X.X |
| 97 | * |
| 98 | * @param string[] $font_families Font family(ies) to enqueue. |
| 99 | */ |
| 100 | function wp_enqueue_fonts( array $font_families ) { |
| 101 | $handles = array_map( array( WP_Fonts_Utils::class, 'convert_font_family_into_handle' ), $font_families ); |
| 102 | |
| 103 | wp_fonts()->enqueue( $handles ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if ( ! function_exists( 'wp_enqueue_font_variations' ) ) { |
| 108 | /** |
| 109 | * Enqueues a specific set of font variations. |
| 110 | * |
| 111 | * @since X.X.X |
| 112 | * |
| 113 | * @param string|string[] $variation_handles Variation handle (string) or handles (array of strings). |
| 114 | */ |
| 115 | function wp_enqueue_font_variations( $variation_handles ) { |
| 116 | wp_fonts()->enqueue( $variation_handles ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if ( ! function_exists( 'wp_deregister_font_family' ) ) { |
| 121 | /** |
| 122 | * Deregisters a font family and all of its registered variations. |
| 123 | * |
| 124 | * @since X.X.X |
| 125 | * |
| 126 | * @param string $font_family_handle The font family to remove. |
| 127 | */ |
| 128 | function wp_deregister_font_family( $font_family_handle ) { |
| 129 | wp_fonts()->remove_font_family( $font_family_handle ); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if ( ! function_exists( 'wp_deregister_font_variation' ) ) { |
| 134 | /** |
| 135 | * Deregisters a font variation. |
| 136 | * |
| 137 | * @since X.X.X |
| 138 | * |
| 139 | * @param string $font_family_handle The font family for this variation. |
| 140 | * @param string $variation_handle The variation's handle to remove. |
| 141 | */ |
| 142 | function wp_deregister_font_variation( $font_family_handle, $variation_handle ) { |
| 143 | wp_fonts()->remove_variation( $font_family_handle, $variation_handle ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( ! function_exists( 'wp_register_font_provider' ) ) { |
| 148 | /** |
| 149 | * Registers a custom font service provider. |
| 150 | * |
| 151 | * A webfont provider contains the business logic for how to |
| 152 | * interact with a remote font service and how to generate |
| 153 | * the `@font-face` styles for that remote service. |
| 154 | * |
| 155 | * How to register a custom font service provider: |
| 156 | * 1. Load its class file into memory before registration. |
| 157 | * 2. Pass the class' name to this function. |
| 158 | * |
| 159 | * For example, for a class named `My_Custom_Font_Service_Provider`: |
| 160 | * ``` |
| 161 | * wp_register_font_provider( My_Custom_Font_Service_Provider::class ); |
| 162 | * ``` |
| 163 | * |
| 164 | * @since 6.0.0 |
| 165 | * |
| 166 | * @param string $name The provider's name. |
| 167 | * @param string $classname The provider's class name. |
| 168 | * The class should be a child of `WP_Webfonts_Provider`. |
| 169 | * See {@see WP_Webfonts_Provider}. |
| 170 | * |
| 171 | * @return bool True if successfully registered, else false. |
| 172 | */ |
| 173 | function wp_register_font_provider( $name, $classname ) { |
| 174 | return wp_fonts()->register_provider( $name, $classname ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if ( ! function_exists( 'wp_print_fonts' ) ) { |
| 179 | /** |
| 180 | * Invokes each provider to process and print its styles. |
| 181 | * |
| 182 | * @since X.X.X |
| 183 | * |
| 184 | * @param string|string[]|bool $handles Optional. Items to be processed: queue (false), |
| 185 | * for iframed editor assets (true), single item (string), |
| 186 | * or multiple items (array of strings). |
| 187 | * Default false. |
| 188 | * @return array|string[] Array of font handles that have been processed. |
| 189 | * An empty array if none were processed. |
| 190 | */ |
| 191 | function wp_print_fonts( $handles = false ) { |
| 192 | $wp_fonts = wp_fonts(); |
| 193 | $registered = $wp_fonts->get_registered_font_families(); |
| 194 | $in_iframed_editor = true === $handles; |
| 195 | |
| 196 | // Nothing to print, as no fonts are registered. |
| 197 | if ( empty( $registered ) ) { |
| 198 | return array(); |
| 199 | } |
| 200 | |
| 201 | // Skip this reassignment decision-making when using the default of `false`. |
| 202 | if ( false !== $handles ) { |
| 203 | // When `true`, print all registered fonts for the iframed editor. |
| 204 | if ( $in_iframed_editor ) { |
| 205 | $queue = $wp_fonts->queue; |
| 206 | $done = $wp_fonts->done; |
| 207 | $wp_fonts->done = array(); |
| 208 | $wp_fonts->queue = $registered; |
| 209 | $handles = false; |
| 210 | } elseif ( empty( $handles ) ) { |
| 211 | // When falsey, assign `false` to print enqueued fonts. |
| 212 | $handles = false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
| 217 | |
| 218 | $printed = $wp_fonts->do_items( $handles ); |
| 219 | |
| 220 | // Reset the API. |
| 221 | if ( $in_iframed_editor ) { |
| 222 | $wp_fonts->done = $done; |
| 223 | $wp_fonts->queue = $queue; |
| 224 | } |
| 225 | |
| 226 | return $printed; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Add webfonts mime types. |
| 232 | */ |
| 233 | add_filter( |
| 234 | 'mime_types', |
| 235 | function( $mime_types ) { |
| 236 | // Webfonts formats. |
| 237 | $mime_types['woff2'] = 'font/woff2'; |
| 238 | $mime_types['woff'] = 'font/woff'; |
| 239 | $mime_types['ttf'] = 'font/ttf'; |
| 240 | $mime_types['eot'] = 'application/vnd.ms-fontobject'; |
| 241 | $mime_types['otf'] = 'application/x-font-opentype'; |
| 242 | |
| 243 | return $mime_types; |
| 244 | } |
| 245 | ); |
| 246 |