-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
class-wp-rest-font-library-controller.php
457 lines (416 loc) · 12.9 KB
/
class-wp-rest-font-library-controller.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
<?php
/**
* Rest Font Library Controller.
*
* This file contains the class for the REST API Font Library Controller.
*
* @package WordPress
* @subpackage Font Library
* @since 6.5.0
*/
if ( class_exists( 'WP_REST_Font_Library_Controller' ) ) {
return;
}
/**
* Font Library Controller class.
*
* @since 6.5.0
*/
class WP_REST_Font_Library_Controller extends WP_REST_Controller {
/**
* Constructor.
*
* @since 6.5.0
*/
public function __construct() {
$this->rest_base = 'fonts';
$this->namespace = 'wp/v2';
}
/**
* Registers the routes for the objects of the controller.
*
* @since 6.5.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'install_fonts' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
'args' => array(
'font_families' => array(
'required' => true,
'type' => 'string',
'validate_callback' => array( $this, 'validate_install_font_families' ),
),
),
),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'uninstall_fonts' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
'args' => $this->uninstall_schema(),
),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/collections',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collections' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/collections' . '/(?P<id>[\/\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_font_collection' ),
'permission_callback' => array( $this, 'update_font_library_permissions_check' ),
),
)
);
}
/**
* Gets a font collection.
*
* @since 6.5.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collection( $request ) {
$id = $request->get_param( 'id' );
$collection = WP_Font_Library::get_font_collection( $id );
// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
return $collection;
}
$collection_with_data = $collection->get_data();
// If there was an error getting the collection data, return the error.
if ( is_wp_error( $collection_with_data ) ) {
$collection_with_data->add_data( array( 'status' => 500 ) );
return $collection_with_data;
}
return new WP_REST_Response( $collection_with_data );
}
/**
* Gets the font collections available.
*
* @since 6.5.0
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collections() {
$collections = array();
foreach ( WP_Font_Library::get_font_collections() as $collection ) {
$collections[] = $collection->get_config();
}
return new WP_REST_Response( $collections, 200 );
}
/**
* Returns validation errors in font families data for installation.
*
* @since 6.5.0
*
* @param array[] $font_families Font families to install.
* @param array $files Files to install.
* @return array $error_messages Array of error messages.
*/
private function get_validation_errors( $font_families, $files ) {
$error_messages = array();
if ( ! is_array( $font_families ) ) {
$error_messages[] = __( 'font_families should be an array of font families.', 'gutenberg' );
return $error_messages;
}
// Checks if there is at least one font family.
if ( count( $font_families ) < 1 ) {
$error_messages[] = __( 'font_families should have at least one font family definition.', 'gutenberg' );
return $error_messages;
}
for ( $family_index = 0; $family_index < count( $font_families ); $family_index++ ) {
$font_family = $font_families[ $family_index ];
if (
! isset( $font_family['slug'] ) ||
! isset( $font_family['name'] ) ||
! isset( $font_family['fontFamily'] )
) {
$error_messages[] = sprintf(
// translators: 1: font family index.
__( 'Font family [%s] should have slug, name and fontFamily properties defined.', 'gutenberg' ),
$family_index
);
}
if ( isset( $font_family['fontFace'] ) ) {
if ( ! is_array( $font_family['fontFace'] ) ) {
$error_messages[] = sprintf(
// translators: 1: font family index.
__( 'Font family [%s] should have fontFace property defined as an array.', 'gutenberg' ),
$family_index
);
continue;
}
if ( count( $font_family['fontFace'] ) < 1 ) {
$error_messages[] = sprintf(
// translators: 1: font family index.
__( 'Font family [%s] should have at least one font face definition.', 'gutenberg' ),
$family_index
);
}
if ( ! empty( $font_family['fontFace'] ) ) {
for ( $face_index = 0; $face_index < count( $font_family['fontFace'] ); $face_index++ ) {
$font_face = $font_family['fontFace'][ $face_index ];
if ( ! isset( $font_face['fontWeight'] ) || ! isset( $font_face['fontStyle'] ) ) {
$error_messages[] = sprintf(
// translators: 1: font family index, 2: font face index.
__( 'Font family [%1$s] Font face [%2$s] should have fontWeight and fontStyle properties defined.', 'gutenberg' ),
$family_index,
$face_index
);
}
if ( isset( $font_face['downloadFromUrl'] ) && isset( $font_face['uploadedFile'] ) ) {
$error_messages[] = sprintf(
// translators: 1: font family index, 2: font face index.
__( 'Font family [%1$s] Font face [%2$s] should have only one of the downloadFromUrl or uploadedFile properties defined and not both.', 'gutenberg' ),
$family_index,
$face_index
);
}
if ( isset( $font_face['uploadedFile'] ) ) {
if ( ! isset( $files[ $font_face['uploadedFile'] ] ) ) {
$error_messages[] = sprintf(
// translators: 1: font family index, 2: font face index.
__( 'Font family [%1$s] Font face [%2$s] file is not defined in the request files.', 'gutenberg' ),
$family_index,
$face_index
);
}
}
}
}
}
}
return $error_messages;
}
/**
* Validate input for the install endpoint.
*
* @since 6.5.0
*
* @param string $param The font families to install.
* @param WP_REST_Request $request The request object.
* @return true|WP_Error True if the parameter is valid, WP_Error otherwise.
*/
public function validate_install_font_families( $param, $request ) {
$font_families = json_decode( $param, true );
$files = $request->get_file_params();
$error_messages = $this->get_validation_errors( $font_families, $files );
if ( empty( $error_messages ) ) {
return true;
}
return new WP_Error( 'rest_invalid_param', implode( ', ', $error_messages ), array( 'status' => 400 ) );
}
/**
* Gets the schema for the uninstall endpoint.
*
* @since 6.5.0
*
* @return array Schema array.
*/
public function uninstall_schema() {
return array(
'font_families' => array(
'type' => 'array',
'description' => __( 'The font families to install.', 'gutenberg' ),
'required' => true,
'minItems' => 1,
'items' => array(
'required' => true,
'type' => 'object',
'properties' => array(
'slug' => array(
'type' => 'string',
'description' => __( 'The font family slug.', 'gutenberg' ),
'required' => true,
),
),
),
),
);
}
/**
* Removes font families from the Font Library and all their assets.
*
* @since 6.5.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function uninstall_fonts( $request ) {
$fonts_to_uninstall = $request->get_param( 'font_families' );
$errors = array();
$successes = array();
if ( empty( $fonts_to_uninstall ) ) {
$errors[] = new WP_Error(
'no_fonts_to_install',
__( 'No fonts to uninstall', 'gutenberg' )
);
$data = array(
'successes' => $successes,
'errors' => $errors,
);
$response = rest_ensure_response( $data );
$response->set_status( 400 );
return $response;
}
foreach ( $fonts_to_uninstall as $font_data ) {
$font = new WP_Font_Family( $font_data );
$result = $font->uninstall();
if ( is_wp_error( $result ) ) {
$errors[] = $result;
} else {
$successes[] = $result;
}
}
$data = array(
'successes' => $successes,
'errors' => $errors,
);
return rest_ensure_response( $data );
}
/**
* Checks whether the user has permissions to update the Font Library.
*
* @since 6.5.0
*
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
*/
public function update_font_library_permissions_check() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
return new WP_Error(
'rest_cannot_update_font_library',
__( 'Sorry, you are not allowed to update the Font Library on this site.', 'gutenberg' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
return true;
}
/**
* Checks whether the user has write permissions to the temp and fonts directories.
*
* @since 6.5.0
*
* @return true|WP_Error True if the user has write permissions, WP_Error object otherwise.
*/
private function has_write_permission() {
// The update endpoints requires write access to the temp and the fonts directories.
$temp_dir = get_temp_dir();
$upload_dir = WP_Font_Library::get_fonts_dir();
if ( ! is_writable( $temp_dir ) || ! wp_is_writable( $upload_dir ) ) {
return false;
}
return true;
}
/**
* Checks whether the request needs write permissions.
*
* @since 6.5.0
*
* @param array[] $font_families Font families to install.
* @return bool Whether the request needs write permissions.
*/
private function needs_write_permission( $font_families ) {
foreach ( $font_families as $font ) {
if ( isset( $font['fontFace'] ) ) {
foreach ( $font['fontFace'] as $face ) {
// If the font is being downloaded from a URL or uploaded, it needs write permissions.
if ( isset( $face['downloadFromUrl'] ) || isset( $face['uploadedFile'] ) ) {
return true;
}
}
}
}
return false;
}
/**
* Installs new fonts.
*
* Takes a request containing new fonts to install, downloads their assets, and adds them
* to the Font Library.
*
* @since 6.5.0
*
* @param WP_REST_Request $request The request object containing the new fonts to install
* in the request parameters.
* @return WP_REST_Response|WP_Error The updated Font Library post content.
*/
public function install_fonts( $request ) {
// Get new fonts to install.
$fonts_param = $request->get_param( 'font_families' );
/*
* As this is receiving form data, the font families are encoded as a string.
* The form data is used because local fonts need to use that format to
* attach the files in the request.
*/
$fonts_to_install = json_decode( $fonts_param, true );
$successes = array();
$errors = array();
$response_status = 200;
if ( empty( $fonts_to_install ) ) {
$errors[] = new WP_Error(
'no_fonts_to_install',
__( 'No fonts to install', 'gutenberg' )
);
$response_status = 400;
}
if ( $this->needs_write_permission( $fonts_to_install ) && ! $this->has_write_permission() ) {
$errors[] = new WP_Error(
'cannot_write_fonts_folder',
__( 'Error: WordPress does not have permission to write the fonts folder on your server.', 'gutenberg' )
);
$response_status = 500;
}
if ( ! empty( $errors ) ) {
$data = array(
'successes' => $successes,
'errors' => $errors,
);
$response = rest_ensure_response( $data );
$response->set_status( $response_status );
return $response;
}
// Get uploaded files (used when installing local fonts).
$files = $request->get_file_params();
foreach ( $fonts_to_install as $font_data ) {
$font = new WP_Font_Family( $font_data );
$result = $font->install( $files );
if ( is_wp_error( $result ) ) {
$errors[] = $result;
} else {
$successes[] = $result;
}
}
$data = array(
'successes' => $successes,
'errors' => $errors,
);
return rest_ensure_response( $data );
}
}