PluginProbe
Gutenberg / 17.2.1
Gutenberg v17.2.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / experimental / fonts-api / fonts-api.php

fonts-api.php in Gutenberg 17.2.1, at lib/experimental/fonts-api/fonts-api.php

260 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_action( 'admin_print_styles', 'wp_print_fonts', 50 );
30 }
31
32 return $wp_fonts;
33 }
34 }
35
36 if ( ! function_exists( 'wp_register_fonts' ) ) {
37 /**
38 * Registers one or more font-families and each of their variations.
39 *
40 * @since X.X.X
41 *
42 * @param array[] $fonts {
43 * Fonts to be registered, grouped by each font-family.
44 *
45 * @type string $font-family => array[] $variations {
46 * An array of web font variations for this font-family.
47 * Each variation has the following structure.
48 *
49 * @type array $variation {
50 * @type string $provider The provider ID. Default 'local'.
51 * @type string $font-style The font-style property. Default 'normal'.
52 * @type string $font-weight The font-weight property. Default '400'.
53 * @type string $font-display The font-display property. Default 'fallback'.
54 * }
55 * }
56 * }
57 * @return string[] Array of registered font family handles.
58 */
59 function wp_register_fonts( array $fonts ) {
60 $registered = array();
61 $wp_fonts = wp_fonts();
62
63 foreach ( $fonts as $font_family => $variations ) {
64 $font_family_handle = $wp_fonts->add_font_family( $font_family );
65 if ( ! $font_family_handle ) {
66 continue;
67 }
68
69 // Register each of the variations for this font family.
70 foreach ( $variations as $variation_handle => $variation ) {
71 if ( ! WP_Fonts_Utils::is_defined( $variation_handle ) ) {
72 $variation_handle = '';
73 }
74
75 $wp_fonts->add_variation( $font_family_handle, $variation, $variation_handle );
76 }
77
78 $registered[] = $font_family_handle;
79 }
80
81 return $registered;
82 }
83 }
84
85 if ( ! function_exists( 'wp_enqueue_fonts' ) ) {
86 /**
87 * Enqueues one or more font family and all of its variations.
88 *
89 * @since X.X.X
90 *
91 * @param string[] $font_families Font family(ies) to enqueue.
92 */
93 function wp_enqueue_fonts( array $font_families ) {
94 $handles = array_map( array( WP_Fonts_Utils::class, 'convert_font_family_into_handle' ), $font_families );
95
96 wp_fonts()->enqueue( $handles );
97 }
98 }
99
100 if ( ! function_exists( 'wp_enqueue_font_variations' ) ) {
101 /**
102 * Enqueues a specific set of font variations.
103 *
104 * @since X.X.X
105 *
106 * @param string|string[] $variation_handles Variation handle (string) or handles (array of strings).
107 */
108 function wp_enqueue_font_variations( $variation_handles ) {
109 wp_fonts()->enqueue( $variation_handles );
110 }
111 }
112
113 if ( ! function_exists( 'wp_deregister_font_family' ) ) {
114 /**
115 * Deregisters a font family and all of its registered variations.
116 *
117 * @since X.X.X
118 *
119 * @param string $font_family_handle The font family to remove.
120 */
121 function wp_deregister_font_family( $font_family_handle ) {
122 wp_fonts()->remove_font_family( $font_family_handle );
123 }
124 }
125
126 if ( ! function_exists( 'wp_deregister_font_variation' ) ) {
127 /**
128 * Deregisters a font variation.
129 *
130 * @since X.X.X
131 *
132 * @param string $font_family_handle The font family for this variation.
133 * @param string $variation_handle The variation's handle to remove.
134 */
135 function wp_deregister_font_variation( $font_family_handle, $variation_handle ) {
136 wp_fonts()->remove_variation( $font_family_handle, $variation_handle );
137 }
138 }
139
140 if ( ! function_exists( 'wp_register_font_provider' ) ) {
141 /**
142 * Registers a custom font service provider.
143 *
144 * A webfont provider contains the business logic for how to
145 * interact with a remote font service and how to generate
146 * the `@font-face` styles for that remote service.
147 *
148 * How to register a custom font service provider:
149 * 1. Load its class file into memory before registration.
150 * 2. Pass the class' name to this function.
151 *
152 * For example, for a class named `My_Custom_Font_Service_Provider`:
153 * ```
154 * wp_register_font_provider( My_Custom_Font_Service_Provider::class );
155 * ```
156 *
157 * @since 6.0.0
158 *
159 * @param string $name The provider's name.
160 * @param string $classname The provider's class name.
161 * The class should be a child of `WP_Webfonts_Provider`.
162 * See {@see WP_Webfonts_Provider}.
163 *
164 * @return bool True if successfully registered, else false.
165 */
166 function wp_register_font_provider( $name, $classname ) {
167 return wp_fonts()->register_provider( $name, $classname );
168 }
169 }
170
171 if ( ! function_exists( 'wp_print_fonts' ) ) {
172 /**
173 * Invokes each provider to process and print its styles.
174 *
175 * @since X.X.X
176 *
177 * @param string|string[]|bool $handles Optional. Items to be processed: queue (false),
178 * for iframed editor assets (true), single item (string),
179 * or multiple items (array of strings).
180 * Default false.
181 * @return array|string[] Array of font handles that have been processed.
182 * An empty array if none were processed.
183 */
184 function wp_print_fonts( $handles = false ) {
185 $wp_fonts = wp_fonts();
186 $registered = $wp_fonts->get_registered_font_families();
187 $in_iframed_editor = true === $handles;
188
189 // Nothing to print, as no fonts are registered.
190 if ( empty( $registered ) ) {
191 return array();
192 }
193
194 if ( empty( $handles ) ) {
195 // Automatically enqueue all user-selected fonts.
196 WP_Fonts_Resolver::enqueue_user_selected_fonts();
197 $handles = false;
198 } elseif ( $in_iframed_editor ) {
199 // Print all registered fonts for the iframed editor.
200 $queue = $wp_fonts->queue;
201 $done = $wp_fonts->done;
202 $wp_fonts->done = array();
203 $wp_fonts->queue = $registered;
204 $handles = false;
205 }
206
207 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
208
209 $printed = $wp_fonts->do_items( $handles );
210
211 // Reset the API.
212 if ( $in_iframed_editor ) {
213 $wp_fonts->done = $done;
214 $wp_fonts->queue = $queue;
215 }
216
217 return $printed;
218 }
219 }
220
221 /**
222 * Add webfonts mime types.
223 */
224 add_filter(
225 'mime_types',
226 static function ( $mime_types ) {
227 // Webfonts formats.
228 $mime_types['woff2'] = 'font/woff2';
229 $mime_types['woff'] = 'font/woff';
230 $mime_types['ttf'] = 'font/ttf';
231 $mime_types['eot'] = 'application/vnd.ms-fontobject';
232 $mime_types['otf'] = 'application/x-font-opentype';
233
234 return $mime_types;
235 }
236 );
237
238 /*
239 * To make sure blocks are registered before any Theme_JSON operations take place, a priority of 21 is used.
240 *
241 * Why 21?
242 * Blocks are registered via the "init" hook with a priority value of `20`, which is dynamically added
243 * during the build. See: tools/webpack/blocks.js.
244 */
245 add_action( 'init', 'WP_Fonts_Resolver::register_fonts_from_theme_json', 21 );
246
247 add_filter(
248 'block_editor_settings_all',
249 static function ( $settings ) {
250 ob_start();
251 wp_print_fonts( true );
252 $styles = ob_get_clean();
253
254 // Add the font-face styles to iframed editor assets.
255 $settings['__unstableResolvedAssets']['styles'] .= $styles;
256 return $settings;
257 },
258 11
259 );
260