|
1 <?php |
|
2 /** |
|
3 * Fonts functions. |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Fonts |
|
7 * @since 6.4.0 |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Generates and prints font-face styles for given fonts or theme.json fonts. |
|
12 * |
|
13 * @since 6.4.0 |
|
14 * |
|
15 * @param array[][] $fonts { |
|
16 * Optional. The font-families and their font faces. Default empty array. |
|
17 * |
|
18 * @type array ...$0 { |
|
19 * An indexed or associative (keyed by font-family) array of font variations for this font-family. |
|
20 * Each font face has the following structure. |
|
21 * |
|
22 * @type array ...$0 { |
|
23 * The font face properties. |
|
24 * |
|
25 * @type string $font-family The font-family property. |
|
26 * @type string|string[] $src The URL(s) to each resource containing the font data. |
|
27 * @type string $font-style Optional. The font-style property. Default 'normal'. |
|
28 * @type string $font-weight Optional. The font-weight property. Default '400'. |
|
29 * @type string $font-display Optional. The font-display property. Default 'fallback'. |
|
30 * @type string $ascent-override Optional. The ascent-override property. |
|
31 * @type string $descent-override Optional. The descent-override property. |
|
32 * @type string $font-stretch Optional. The font-stretch property. |
|
33 * @type string $font-variant Optional. The font-variant property. |
|
34 * @type string $font-feature-settings Optional. The font-feature-settings property. |
|
35 * @type string $font-variation-settings Optional. The font-variation-settings property. |
|
36 * @type string $line-gap-override Optional. The line-gap-override property. |
|
37 * @type string $size-adjust Optional. The size-adjust property. |
|
38 * @type string $unicode-range Optional. The unicode-range property. |
|
39 * } |
|
40 * } |
|
41 * } |
|
42 */ |
|
43 function wp_print_font_faces( $fonts = array() ) { |
|
44 |
|
45 if ( empty( $fonts ) ) { |
|
46 $fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json(); |
|
47 } |
|
48 |
|
49 if ( empty( $fonts ) ) { |
|
50 return; |
|
51 } |
|
52 |
|
53 $wp_font_face = new WP_Font_Face(); |
|
54 $wp_font_face->generate_and_print( $fonts ); |
|
55 } |
|
56 |
|
57 /** |
|
58 * Registers a new font collection in the font library. |
|
59 * |
|
60 * See {@link https://schemas.wp.org/trunk/font-collection.json} for the schema |
|
61 * the font collection data must adhere to. |
|
62 * |
|
63 * @since 6.5.0 |
|
64 * |
|
65 * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes, |
|
66 * and underscores. See sanitize_title(). |
|
67 * @param array $args { |
|
68 * Font collection data. |
|
69 * |
|
70 * @type string $name Required. Name of the font collection shown in the Font Library. |
|
71 * @type string $description Optional. A short descriptive summary of the font collection. Default empty. |
|
72 * @type array|string $font_families Required. Array of font family definitions that are in the collection, |
|
73 * or a string containing the path or URL to a JSON file containing the font collection. |
|
74 * @type array $categories Optional. Array of categories, each with a name and slug, that are used by the |
|
75 * fonts in the collection. Default empty. |
|
76 * } |
|
77 * @return WP_Font_Collection|WP_Error A font collection if it was registered |
|
78 * successfully, or WP_Error object on failure. |
|
79 */ |
|
80 function wp_register_font_collection( string $slug, array $args ) { |
|
81 return WP_Font_Library::get_instance()->register_font_collection( $slug, $args ); |
|
82 } |
|
83 |
|
84 /** |
|
85 * Unregisters a font collection from the Font Library. |
|
86 * |
|
87 * @since 6.5.0 |
|
88 * |
|
89 * @param string $slug Font collection slug. |
|
90 * @return bool True if the font collection was unregistered successfully, else false. |
|
91 */ |
|
92 function wp_unregister_font_collection( string $slug ) { |
|
93 return WP_Font_Library::get_instance()->unregister_font_collection( $slug ); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Retrieves font uploads directory information. |
|
98 * |
|
99 * Same as wp_font_dir() but "light weight" as it doesn't attempt to create the font uploads directory. |
|
100 * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases |
|
101 * when not uploading files. |
|
102 * |
|
103 * @since 6.5.0 |
|
104 * |
|
105 * @see wp_font_dir() |
|
106 * |
|
107 * @return array See wp_font_dir() for description. |
|
108 */ |
|
109 function wp_get_font_dir() { |
|
110 return wp_font_dir( false ); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Returns an array containing the current fonts upload directory's path and URL. |
|
115 * |
|
116 * @since 6.5.0 |
|
117 * |
|
118 * @param bool $create_dir Optional. Whether to check and create the font uploads directory. Default true. |
|
119 * @return array { |
|
120 * Array of information about the font upload directory. |
|
121 * |
|
122 * @type string $path Base directory and subdirectory or full path to the fonts upload directory. |
|
123 * @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory. |
|
124 * @type string $subdir Subdirectory |
|
125 * @type string $basedir Path without subdir. |
|
126 * @type string $baseurl URL path without subdir. |
|
127 * @type string|false $error False or error message. |
|
128 * } |
|
129 */ |
|
130 function wp_font_dir( $create_dir = true ) { |
|
131 /* |
|
132 * Allow extenders to manipulate the font directory consistently. |
|
133 * |
|
134 * Ensures the upload_dir filter is fired both when calling this function |
|
135 * directly and when the upload directory is filtered in the Font Face |
|
136 * REST API endpoint. |
|
137 */ |
|
138 add_filter( 'upload_dir', '_wp_filter_font_directory' ); |
|
139 $font_dir = wp_upload_dir( null, $create_dir, false ); |
|
140 remove_filter( 'upload_dir', '_wp_filter_font_directory' ); |
|
141 return $font_dir; |
|
142 } |
|
143 |
|
144 /** |
|
145 * A callback function for use in the {@see 'upload_dir'} filter. |
|
146 * |
|
147 * This function is intended for internal use only and should not be used by plugins and themes. |
|
148 * Use wp_get_font_dir() instead. |
|
149 * |
|
150 * @since 6.5.0 |
|
151 * @access private |
|
152 * |
|
153 * @param string $font_dir The font directory. |
|
154 * @return string The modified font directory. |
|
155 */ |
|
156 function _wp_filter_font_directory( $font_dir ) { |
|
157 if ( doing_filter( 'font_dir' ) ) { |
|
158 // Avoid an infinite loop. |
|
159 return $font_dir; |
|
160 } |
|
161 |
|
162 $font_dir = array( |
|
163 'path' => untrailingslashit( $font_dir['basedir'] ) . '/fonts', |
|
164 'url' => untrailingslashit( $font_dir['baseurl'] ) . '/fonts', |
|
165 'subdir' => '', |
|
166 'basedir' => untrailingslashit( $font_dir['basedir'] ) . '/fonts', |
|
167 'baseurl' => untrailingslashit( $font_dir['baseurl'] ) . '/fonts', |
|
168 'error' => false, |
|
169 ); |
|
170 |
|
171 /** |
|
172 * Filters the fonts directory data. |
|
173 * |
|
174 * This filter allows developers to modify the fonts directory data. |
|
175 * |
|
176 * @since 6.5.0 |
|
177 * |
|
178 * @param array $font_dir { |
|
179 * Array of information about the font upload directory. |
|
180 * |
|
181 * @type string $path Base directory and subdirectory or full path to the fonts upload directory. |
|
182 * @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory. |
|
183 * @type string $subdir Subdirectory |
|
184 * @type string $basedir Path without subdir. |
|
185 * @type string $baseurl URL path without subdir. |
|
186 * @type string|false $error False or error message. |
|
187 * } |
|
188 */ |
|
189 return apply_filters( 'font_dir', $font_dir ); |
|
190 } |
|
191 |
|
192 /** |
|
193 * Deletes child font faces when a font family is deleted. |
|
194 * |
|
195 * @access private |
|
196 * @since 6.5.0 |
|
197 * |
|
198 * @param int $post_id Post ID. |
|
199 * @param WP_Post $post Post object. |
|
200 */ |
|
201 function _wp_after_delete_font_family( $post_id, $post ) { |
|
202 if ( 'wp_font_family' !== $post->post_type ) { |
|
203 return; |
|
204 } |
|
205 |
|
206 $font_faces = get_children( |
|
207 array( |
|
208 'post_parent' => $post_id, |
|
209 'post_type' => 'wp_font_face', |
|
210 ) |
|
211 ); |
|
212 |
|
213 foreach ( $font_faces as $font_face ) { |
|
214 wp_delete_post( $font_face->ID, true ); |
|
215 } |
|
216 } |
|
217 |
|
218 /** |
|
219 * Deletes associated font files when a font face is deleted. |
|
220 * |
|
221 * @access private |
|
222 * @since 6.5.0 |
|
223 * |
|
224 * @param int $post_id Post ID. |
|
225 * @param WP_Post $post Post object. |
|
226 */ |
|
227 function _wp_before_delete_font_face( $post_id, $post ) { |
|
228 if ( 'wp_font_face' !== $post->post_type ) { |
|
229 return; |
|
230 } |
|
231 |
|
232 $font_files = get_post_meta( $post_id, '_wp_font_face_file', false ); |
|
233 $font_dir = untrailingslashit( wp_get_font_dir()['basedir'] ); |
|
234 |
|
235 foreach ( $font_files as $font_file ) { |
|
236 wp_delete_file( $font_dir . '/' . $font_file ); |
|
237 } |
|
238 } |
|
239 |
|
240 /** |
|
241 * Register the default font collections. |
|
242 * |
|
243 * @access private |
|
244 * @since 6.5.0 |
|
245 */ |
|
246 function _wp_register_default_font_collections() { |
|
247 wp_register_font_collection( |
|
248 'google-fonts', |
|
249 array( |
|
250 'name' => _x( 'Google Fonts', 'font collection name' ), |
|
251 'description' => __( 'Install from Google Fonts. Fonts are copied to and served from your site.' ), |
|
252 'font_families' => 'https://s.w.org/images/fonts/wp-6.5/collections/google-fonts-with-preview.json', |
|
253 'categories' => array( |
|
254 array( |
|
255 'name' => _x( 'Sans Serif', 'font category' ), |
|
256 'slug' => 'sans-serif', |
|
257 ), |
|
258 array( |
|
259 'name' => _x( 'Display', 'font category' ), |
|
260 'slug' => 'display', |
|
261 ), |
|
262 array( |
|
263 'name' => _x( 'Serif', 'font category' ), |
|
264 'slug' => 'serif', |
|
265 ), |
|
266 array( |
|
267 'name' => _x( 'Handwriting', 'font category' ), |
|
268 'slug' => 'handwriting', |
|
269 ), |
|
270 array( |
|
271 'name' => _x( 'Monospace', 'font category' ), |
|
272 'slug' => 'monospace', |
|
273 ), |
|
274 ), |
|
275 ) |
|
276 ); |
|
277 } |