-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
class-wp-font-family.php
563 lines (493 loc) · 14.5 KB
/
class-wp-font-family.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
/**
* Font Family class.
*
* This file contains the Font Family class definition.
*
* @package WordPress
* @subpackage Fonts Library
* @since 6.4.0
*/
if ( class_exists( 'WP_Font_Family' ) ) {
return;
}
/**
* Fonts Library class.
*
* @since 6.4.0
*/
class WP_Font_Family {
/**
* Font family data.
*
* @since 6.4.0
*
* @var array
*/
private $data;
/**
* WP_Font_Family constructor.
*
* @since 6.4.0
*
* @param array $font_data Font family data.
* @throws Exception If the font family data is missing the slug.
*/
public function __construct( $font_data = array() ) {
if ( empty( $font_data['slug'] ) ) {
throw new Exception( 'Font family data is missing the slug.' );
}
$this->data = $font_data;
}
/**
* Gets the font family data.
*
* @since 6.4.0
*
* @return array An array in fontFamily theme.json format.
*/
public function get_data() {
return $this->data;
}
/**
* Gets the font family data.
*
* @since 6.4.0
*
* @return string fontFamily in theme.json format as stringified JSON.
*/
public function get_data_as_json() {
return wp_json_encode( $this->get_data() );
}
/**
* Checks whether the font family has font faces defined.
*
* @since 6.4.0
*
* @return bool True if the font family has font faces defined, false otherwise.
*/
public function has_font_faces() {
return ! empty( $this->data['fontFace'] ) && is_array( $this->data['fontFace'] );
}
/**
* Removes font family assets.
*
* @since 6.4.0
*
* @return bool True if assets were removed, false otherwise.
*/
private function remove_font_family_assets() {
if ( $this->has_font_faces() ) {
foreach ( $this->data['fontFace'] as $font_face ) {
$were_assets_removed = $this->delete_font_face_assets( $font_face );
if ( false === $were_assets_removed ) {
return false;
}
}
}
return true;
}
/**
* Removes a font family from the database and deletes its assets.
*
* @since 6.4.0
*
* @return bool|WP_Error True if the font family was uninstalled, WP_Error otherwise.
*/
public function uninstall() {
$post = $this->get_data_from_post();
if ( null === $post ) {
return new WP_Error(
'font_family_not_found',
__( 'The font family could not be found.', 'gutenberg' )
);
}
if (
! $this->remove_font_family_assets() ||
! wp_delete_post( $post->ID, true )
) {
return new WP_Error(
'font_family_not_deleted',
__( 'The font family could not be deleted.', 'gutenberg' )
);
}
return true;
}
/**
* Deletes a specified font asset file from the fonts directory.
*
* @since 6.4.0
*
* @param string $src The path of the font asset file to delete.
* @return bool Whether the file was deleted.
*/
private static function delete_asset( $src ) {
$filename = basename( $src );
$file_path = path_join( WP_Fonts_Library::get_fonts_dir(), $filename );
wp_delete_file( $file_path );
return ! file_exists( $file_path );
}
/**
* Deletes all font face asset files associated with a given font face.
*
* @since 6.4.0
*
* @param array $font_face The font face array containing the 'src' attribute
* with the file path(s) to be deleted.
* @return bool True if delete was successful, otherwise false.
*/
private static function delete_font_face_assets( $font_face ) {
$sources = (array) $font_face['src'];
foreach ( $sources as $src ) {
$was_asset_removed = self::delete_asset( $src );
if ( ! $was_asset_removed ) {
// Bail if any of the assets could not be removed.
return false;
}
}
return true;
}
/**
* Gets the overrides for the 'wp_handle_upload' function.
*
* @since 6.4.0
*
* @param string $filename The filename to be used for the uploaded file.
* @return array The overrides for the 'wp_handle_upload' function.
*/
private function get_upload_overrides( $filename ) {
return array(
// Arbitrary string to avoid the is_uploaded_file() check applied
// when using 'wp_handle_upload'.
'action' => 'wp_handle_font_upload',
// Not testing a form submission.
'test_form' => false,
// Seems mime type for files that are not images cannot be tested.
// See wp_check_filetype_and_ext().
'test_type' => false,
'unique_filename_callback' => static function() use ( $filename ) {
// Keep the original filename.
return $filename;
},
);
}
/**
* Downloads a font asset from a specified source URL and saves it to
* the font directory.
*
* @since 6.4.0
*
* @param string $url The source URL of the font asset to be downloaded.
* @param string $filename The filename to save the downloaded font asset as.
* @return string|bool The relative path to the downloaded font asset.
* False if the download failed.
*/
private function download_asset( $url, $filename ) {
// Checks if the file to be downloaded has a font mime type.
if ( ! WP_Font_Family_Utils::has_font_mime_type( $filename ) ) {
return false;
}
// Include file with download_url() if function doesn't exist.
if ( ! function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
// Downloads the font asset or returns false.
$temp_file = download_url( $url );
if ( is_wp_error( $temp_file ) ) {
return false;
}
$overrides = $this->get_upload_overrides( $filename );
$file = array(
'tmp_name' => $temp_file,
'name' => $filename,
);
$handled_file = wp_handle_upload( $file, $overrides );
// Cleans the temp file.
@unlink( $temp_file );
if ( ! isset( $handled_file['url'] ) ) {
return false;
}
// Returns the relative path to the downloaded font asset to be used as
// font face src.
return $handled_file['url'];
}
/**
* Moves an uploaded font face asset from temp folder to the fonts directory.
*
* This is used when uploading local fonts.
*
* @since 6.4.0
*
* @param array $font_face Font face to download.
* @param array $file Uploaded file.
* @return array New font face with all assets downloaded and referenced in
* the font face definition.
*/
private function move_font_face_asset( $font_face, $file ) {
$new_font_face = $font_face;
$filename = WP_Font_Family_Utils::get_filename_from_font_face(
$this->data['slug'],
$font_face,
$file['name']
);
// Remove the uploaded font asset reference from the font face definition
// because it is no longer needed.
unset( $new_font_face['uploaded_file'] );
// If the filename has no font mime type, don't move the file and
// return the font face definition without src to be ignored later.
if ( ! WP_Font_Family_Utils::has_font_mime_type( $filename ) ) {
return $new_font_face;
}
// Move the uploaded font asset from the temp folder to the fonts directory.
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$overrides = $this->get_upload_overrides( $filename );
$handled_file = wp_handle_upload( $file, $overrides );
if ( isset( $handled_file['url'] ) ) {
// If the file was successfully moved, update the font face definition
// to reference the new file location.
$new_font_face['src'] = $handled_file['url'];
}
return $new_font_face;
}
/**
* Sanitizes the font family data using WP_Theme_JSON.
*
* @since 6.4.0
*
* @return array A sanitized font family definition.
*/
private function sanitize() {
// Creates the structure of theme.json array with the new fonts.
$fonts_json = array(
'version' => '2',
'settings' => array(
'typography' => array(
'fontFamilies' => array( $this->data ),
),
),
);
// Creates a new WP_Theme_JSON object with the new fonts to
// leverage sanitization and validation.
$theme_json = new WP_Theme_JSON_Gutenberg( $fonts_json );
$theme_data = $theme_json->get_data();
$sanitized_font = ! empty( $theme_data['settings']['typography']['fontFamilies'] )
? $theme_data['settings']['typography']['fontFamilies'][0]
: array();
$this->data = $sanitized_font;
return $this->data;
}
/**
* Downloads font face assets.
*
* Downloads the font face asset(s) associated with a font face. It works with
* both single source URLs and arrays of multiple source URLs.
*
* @since 6.4.0
*
* @param array $font_face The font face array containing the 'src' attribute
* with the source URL(s) of the assets.
* @return array The modified font face array with the new source URL(s) to
* the downloaded assets.
*/
private function download_font_face_assets( $font_face ) {
$new_font_face = $font_face;
$sources = (array) $font_face['download_from_url'];
$new_font_face['src'] = array();
$index = 0;
foreach ( $sources as $src ) {
$suffix = $index++ > 0 ? $index : '';
$filename = WP_Font_Family_Utils::get_filename_from_font_face(
$this->data['slug'],
$font_face,
$src,
$suffix
);
$new_src = $this->download_asset( $src, $filename );
if ( $new_src ) {
$new_font_face['src'][] = $new_src;
}
}
if ( count( $new_font_face['src'] ) === 1 ) {
$new_font_face['src'] = $new_font_face['src'][0];
}
// Remove the download url reference from the font face definition
// because it is no longer needed.
unset( $new_font_face['download_from_url'] );
return $new_font_face;
}
/**
* Downloads font face assets if the font family is a Google font,
* or moves them if it is a local font.
*
* @since 6.4.0
*
* @param array $files An array of files to be installed.
* @return bool True if the font faces were downloaded or moved successfully, false otherwise.
*/
private function download_or_move_font_faces( $files ) {
if ( ! $this->has_font_faces() ) {
return true;
}
$new_font_faces = array();
foreach ( $this->data['fontFace'] as $font_face ) {
// If the fonts are not meant to be dowloaded or uploaded
// (for example to install fonts that use a remote url).
$new_font_face = $font_face;
// If installing google fonts, download the font face assets.
if ( ! empty( $font_face['download_from_url'] ) ) {
$new_font_face = $this->download_font_face_assets( $new_font_face );
}
// If installing local fonts, move the font face assets from
// the temp folder to the wp fonts directory.
if ( ! empty( $font_face['uploaded_file'] ) && ! empty( $files ) ) {
$new_font_face = $this->move_font_face_asset(
$new_font_face,
$files[ $new_font_face['uploaded_file'] ]
);
}
/*
* If the font face assets were successfully downloaded, add the font face
* to the new font. Font faces with failed downloads are not added to the
* new font.
*/
if ( ! empty( $new_font_face['src'] ) ) {
$new_font_faces[] = $new_font_face;
}
}
if ( ! empty( $new_font_faces ) ) {
$this->data['fontFace'] = $new_font_faces;
return true;
}
return false;
}
/**
* Gets the post for a font family.
*
* @since 6.4.0
*
* @return WP_Post|null The post for this font family object or
* null if the post does not exist.
*/
public function get_font_post() {
$args = array(
'post_type' => 'wp_font_family',
'post_name' => $this->data['slug'],
'name' => $this->data['slug'],
'posts_per_page' => 1,
);
$posts_query = new WP_Query( $args );
if ( $posts_query->have_posts() ) {
return $posts_query->posts[0];
}
return null;
}
/**
* Gets the data for this object from the database and
* sets it to the data property.
*
* @since 6.4.0
*
* @return WP_Post|null The post for this font family object or
* null if the post does not exist.
*/
private function get_data_from_post() {
$post = $this->get_font_post();
if ( $post ) {
$this->data = json_decode( $post->post_content, true );
return $post;
}
return null;
}
/**
* Creates a post for a font family.
*
* @since 6.4.0
*
* @return int|WP_Error Post ID if the post was created, WP_Error otherwise.
*/
private function create_font_post() {
$post = array(
'post_title' => $this->data['name'],
'post_name' => $this->data['slug'],
'post_type' => 'wp_font_family',
'post_content' => $this->get_data_as_json(),
'post_status' => 'publish',
);
$post_id = wp_insert_post( $post );
if ( 0 === $post_id || is_wp_error( $post_id ) ) {
return new WP_Error(
'font_post_creation_failed',
__( 'Font post creation failed', 'gutenberg' )
);
}
return $post_id;
}
/**
* Updates a post for a font family.
*
* @since 6.4.0
*
* @param WP_Post $post The post to update.
* @return int|WP_Error Post ID if the update was successful, WP_Error otherwise.
*/
private function update_font_post( $post ) {
$post_font_data = json_decode( $post->post_content, true );
$new_data = WP_Font_Family_Utils::merge_fonts_data( $post_font_data, $this->data );
$this->data = $new_data;
$post = array(
'ID' => $post->ID,
'post_content' => $this->get_data_as_json(),
);
$post_id = wp_update_post( $post );
if ( 0 === $post_id || is_wp_error( $post_id ) ) {
return new WP_Error(
'font_post_update_failed',
__( 'Font post update failed', 'gutenberg' )
);
}
return $post_id;
}
/**
* Creates a post for a font in the fonts library if it doesn't exist,
* or updates it if it does.
*
* @since 6.4.0
*
* @return int|WP_Error Post id if the post was created or updated successfully,
* WP_Error otherwise.
*/
private function create_or_update_font_post() {
$this->sanitize();
$post = $this->get_font_post();
if ( $post ) {
return $this->update_font_post( $post );
}
return $this->create_font_post();
}
/**
* Installs the font family into the library.
*
* @since 6.4.0
*
* @param array $files Optional. An array of files to be installed. Default null.
* @return array|WP_Error An array of font family data on success, WP_Error otherwise.
*/
public function install( $files = null ) {
add_filter( 'upload_dir', array( 'WP_Fonts_Library', 'set_upload_dir' ) );
$were_assets_written = $this->download_or_move_font_faces( $files );
remove_filter( 'upload_dir', array( 'WP_Fonts_Library', 'set_upload_dir' ) );
if ( ! $were_assets_written ) {
return new WP_Error(
'font_face_download_failed',
__( 'The font face assets could not be written.', 'gutenberg' )
);
}
$post_id = $this->create_or_update_font_post();
if ( is_wp_error( $post_id ) ) {
return $post_id;
}
return $this->get_data();
}
}