| 1 |
<?php |
| 2 |
/** |
| 3 |
* Webfonts API functions. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
* @subpackage WebFonts |
| 7 |
* @since 6.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! function_exists( 'wp_webfonts' ) ) { |
| 11 |
/** |
| 12 |
* Instantiates the webfonts controller, if not already set, and returns it. |
| 13 |
* |
| 14 |
* @since 6.0.0 |
| 15 |
* |
| 16 |
* @return WP_Webfonts Instance of the controller. |
| 17 |
*/ |
| 18 |
function wp_webfonts() { |
| 19 |
global $wp_webfonts; |
| 20 |
|
| 21 |
if ( ! $wp_webfonts instanceof WP_Webfonts ) { |
| 22 |
$wp_webfonts = new WP_Webfonts(); |
| 23 |
$wp_webfonts->init(); |
| 24 |
} |
| 25 |
|
| 26 |
return $wp_webfonts; |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! function_exists( 'wp_register_webfonts' ) ) { |
| 31 |
/** |
| 32 |
* Registers a collection of webfonts. |
| 33 |
* |
| 34 |
* Example of how to register Source Serif Pro font with font-weight range of 200-900 |
| 35 |
* and font-style of normal and italic: |
| 36 |
* |
| 37 |
* If the font files are contained within the theme: |
| 38 |
* <code> |
| 39 |
* wp_register_webfonts( |
| 40 |
* array( |
| 41 |
* array( |
| 42 |
* 'provider' => 'local', |
| 43 |
* 'font-family' => 'Source Serif Pro', |
| 44 |
* 'font-weight' => '200 900', |
| 45 |
* 'font-style' => 'normal', |
| 46 |
* 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), |
| 47 |
* ), |
| 48 |
* array( |
| 49 |
* 'provider' => 'local', |
| 50 |
* 'font-family' => 'Source Serif Pro', |
| 51 |
* 'font-weight' => '200 900', |
| 52 |
* 'font-style' => 'italic', |
| 53 |
* 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ), |
| 54 |
* ), |
| 55 |
* ) |
| 56 |
* ); |
| 57 |
* </code> |
| 58 |
* |
| 59 |
* Webfonts should be registered in the `after_setup_theme` hook. |
| 60 |
* |
| 61 |
* @since 6.0.0 |
| 62 |
* |
| 63 |
* @param array[] $webfonts Webfonts to be registered. |
| 64 |
* This contains an array of webfonts to be registered. |
| 65 |
* Each webfont is an array. |
| 66 |
* @return string[] The font family slug of the registered webfonts. |
| 67 |
*/ |
| 68 |
function wp_register_webfonts( array $webfonts ) { |
| 69 |
$registered_webfont_slugs = array(); |
| 70 |
|
| 71 |
foreach ( $webfonts as $webfont ) { |
| 72 |
$slug = wp_register_webfont( $webfont ); |
| 73 |
|
| 74 |
if ( is_string( $slug ) ) { |
| 75 |
$registered_webfont_slugs[ $slug ] = true; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return array_keys( $registered_webfont_slugs ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! function_exists( 'wp_register_webfont' ) ) { |
| 84 |
/** |
| 85 |
* Registers a single webfont. |
| 86 |
* |
| 87 |
* Example of how to register Source Serif Pro font with font-weight range of 200-900: |
| 88 |
* |
| 89 |
* If the font file is contained within the theme: |
| 90 |
* |
| 91 |
* <code> |
| 92 |
* wp_register_webfont( |
| 93 |
* array( |
| 94 |
* 'provider' => 'local', |
| 95 |
* 'font-family' => 'Source Serif Pro', |
| 96 |
* 'font-weight' => '200 900', |
| 97 |
* 'font-style' => 'normal', |
| 98 |
* 'src' => get_theme_file_uri( 'assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ), |
| 99 |
* ) |
| 100 |
* ); |
| 101 |
* </code> |
| 102 |
* |
| 103 |
* @since 6.0.0 |
| 104 |
* |
| 105 |
* @param array $webfont Webfont to be registered. |
| 106 |
* @return string|false The font family slug if successfully registered, else false. |
| 107 |
*/ |
| 108 |
function wp_register_webfont( array $webfont ) { |
| 109 |
return wp_webfonts()->register_webfont( $webfont ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! function_exists( 'wp_enqueue_webfonts' ) ) { |
| 114 |
/** |
| 115 |
* Enqueues a collection of font families. |
| 116 |
* |
| 117 |
* Example of how to enqueue Source Serif Pro and Roboto font families, both registered beforehand. |
| 118 |
* |
| 119 |
* <code> |
| 120 |
* wp_enqueue_webfonts( |
| 121 |
* 'Roboto', |
| 122 |
* 'Sans Serif Pro' |
| 123 |
* ); |
| 124 |
* </code> |
| 125 |
* |
| 126 |
* Font families should be enqueued from the `init` hook or later. |
| 127 |
* |
| 128 |
* @since 6.0.0 |
| 129 |
* |
| 130 |
* @param string[] $webfonts Font families to be enqueued. |
| 131 |
*/ |
| 132 |
function wp_enqueue_webfonts( array $webfonts ) { |
| 133 |
foreach ( $webfonts as $webfont ) { |
| 134 |
wp_enqueue_webfont( $webfont ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! function_exists( 'wp_enqueue_webfont' ) ) { |
| 140 |
/** |
| 141 |
* Enqueue a single font family that has been registered beforehand. |
| 142 |
* |
| 143 |
* Example of how to enqueue Source Serif Pro font: |
| 144 |
* |
| 145 |
* <code> |
| 146 |
* wp_enqueue_webfont( 'Source Serif Pro' ); |
| 147 |
* </code> |
| 148 |
* |
| 149 |
* Font families should be enqueued from the `init` hook or later. |
| 150 |
* |
| 151 |
* @since 6.0.0 |
| 152 |
* |
| 153 |
* @param string $font_family_name The font family name to be enqueued. |
| 154 |
* @return bool True if successfully enqueued, else false. |
| 155 |
*/ |
| 156 |
function wp_enqueue_webfont( $font_family_name ) { |
| 157 |
return wp_webfonts()->enqueue_webfont( $font_family_name ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ( ! function_exists( 'wp_register_webfont_provider' ) ) { |
| 162 |
/** |
| 163 |
* Registers a custom font service provider. |
| 164 |
* |
| 165 |
* A webfont provider contains the business logic for how to |
| 166 |
* interact with a remote font service and how to generate |
| 167 |
* the `@font-face` styles for that remote service. |
| 168 |
* |
| 169 |
* How to register a custom font service provider: |
| 170 |
* 1. Load its class file into memory before registration. |
| 171 |
* 2. Pass the class' name to this function. |
| 172 |
* |
| 173 |
* For example, for a class named `My_Custom_Font_Service_Provider`: |
| 174 |
* ``` |
| 175 |
* wp_register_webfont_provider( My_Custom_Font_Service_Provider::class ); |
| 176 |
* ``` |
| 177 |
* |
| 178 |
* @since 6.0.0 |
| 179 |
* |
| 180 |
* @param string $name The provider's name. |
| 181 |
* @param string $classname The provider's class name. |
| 182 |
* The class should be a child of `WP_Webfonts_Provider`. |
| 183 |
* See {@see WP_Webfonts_Provider}. |
| 184 |
* |
| 185 |
* @return bool True if successfully registered, else false. |
| 186 |
*/ |
| 187 |
function wp_register_webfont_provider( $name, $classname ) { |
| 188 |
return wp_webfonts()->register_provider( $name, $classname ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if ( ! function_exists( 'wp_get_webfont_providers' ) ) { |
| 193 |
/** |
| 194 |
* Gets all registered providers. |
| 195 |
* |
| 196 |
* Return an array of providers, each keyed by their unique |
| 197 |
* ID (i.e. the `$id` property in the provider's object) with |
| 198 |
* an instance of the provider (object): |
| 199 |
* ID => provider instance |
| 200 |
* |
| 201 |
* Each provider contains the business logic for how to |
| 202 |
* process its specific font service (i.e. local or remote) |
| 203 |
* and how to generate the `@font-face` styles for its service. |
| 204 |
* |
| 205 |
* @since 6.0.0 |
| 206 |
* |
| 207 |
* @return WP_Webfonts_Provider[] All registered providers, each keyed by their unique ID. |
| 208 |
*/ |
| 209 |
function wp_get_webfont_providers() { |
| 210 |
return wp_webfonts()->get_providers(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Add webfonts mime types. |
| 216 |
*/ |
| 217 |
add_filter( |
| 218 |
'mime_types', |
| 219 |
function( $mime_types ) { |
| 220 |
// Webfonts formats. |
| 221 |
$mime_types['woff2'] = 'font/woff2'; |
| 222 |
$mime_types['woff'] = 'font/woff'; |
| 223 |
$mime_types['ttf'] = 'font/ttf'; |
| 224 |
$mime_types['eot'] = 'application/vnd.ms-fontobject'; |
| 225 |
$mime_types['otf'] = 'application/x-font-opentype'; |
| 226 |
|
| 227 |
return $mime_types; |
| 228 |
} |
| 229 |
); |
| 230 |
|