-
Notifications
You must be signed in to change notification settings - Fork 57
/
class-create-block-theme-api.php
477 lines (416 loc) · 13.8 KB
/
class-create-block-theme-api.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
<?php
require_once __DIR__ . '/create-theme/resolver_additions.php';
require_once __DIR__ . '/create-theme/theme-locale.php';
require_once __DIR__ . '/create-theme/theme-tags.php';
require_once __DIR__ . '/create-theme/theme-zip.php';
require_once __DIR__ . '/create-theme/theme-media.php';
require_once __DIR__ . '/create-theme/theme-patterns.php';
require_once __DIR__ . '/create-theme/theme-templates.php';
require_once __DIR__ . '/create-theme/theme-styles.php';
require_once __DIR__ . '/create-theme/theme-json.php';
require_once __DIR__ . '/create-theme/theme-utils.php';
require_once __DIR__ . '/create-theme/theme-readme.php';
require_once __DIR__ . '/create-theme/theme-fonts.php';
require_once __DIR__ . '/create-theme/theme-create.php';
/**
* The api functionality of the plugin leveraged by the site editor UI.
*
* @package Create_Block_Theme
* @subpackage Create_Block_Theme/admin
* @author WordPress.org
*/
class CBT_Theme_API {
/**
* Initialize the class and set its properties.
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
}
/**
* Register the REST routes.
*/
public function register_rest_routes() {
register_rest_route(
'create-block-theme/v1',
'/export',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_export_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/update',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_update_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/save',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_save_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/clone',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_clone_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/create-variation',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_create_variation' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/create-blank',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_create_blank_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/create-child',
array(
'methods' => 'POST',
'callback' => array( $this, 'rest_create_child_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/get-readme-data',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_readme_data' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
register_rest_route(
'create-block-theme/v1',
'/get-theme-data',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_theme_data' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
),
);
register_rest_route(
'create-block-theme/v1',
'/font-families',
array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_font_families' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
),
);
register_rest_route(
'create-block-theme/v1',
'/reset-theme',
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'rest_reset_theme' ),
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
),
);
}
function rest_get_theme_data( $request ) {
try {
$theme_data = CBT_Theme_JSON_Resolver::get_theme_file_contents();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme data retrieved.', 'create-block-theme' ),
'data' => $theme_data,
),
);
} catch ( Exception $error ) {
return new WP_REST_Response(
array(
'status' => 'FAILURE',
'message' => $error->getMessage(),
)
);
}
}
function rest_get_readme_data( $request ) {
try {
$readme_data = CBT_Theme_Readme::get_sections();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Readme file data retrieved.', 'create-block-theme' ),
'data' => $readme_data,
)
);
} catch ( Exception $error ) {
return new WP_REST_Response(
array(
'status' => 'FAILURE',
'message' => $error->getMessage(),
)
);
}
}
function rest_clone_theme( $request ) {
$response = CBT_Theme_Create::clone_current_theme( $this->sanitize_theme_data( $request->get_params() ) );
wp_cache_flush();
if ( is_wp_error( $response ) ) {
return $response;
}
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Cloned Theme Created.', 'create-block-theme' ),
)
);
}
function rest_create_child_theme( $request ) {
$theme = $this->sanitize_theme_data( $request->get_params() );
$theme['is_child_theme'] = true;
//TODO: Handle screenshots
$screenshot = null;
$response = CBT_Theme_Create::create_child_theme( $theme, $screenshot );
wp_cache_flush();
if ( is_wp_error( $response ) ) {
return $response;
}
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Child Theme Created.', 'create-block-theme' ),
)
);
}
function rest_create_variation( $request ) {
$options = $request->get_params();
$save_fonts = isset( $options['saveFonts'] ) && true === $options['saveFonts'];
$response = CBT_Theme_JSON::add_theme_json_variation_to_local(
'variation',
$this->sanitize_theme_data( $options ),
$save_fonts
);
wp_cache_flush();
if ( is_wp_error( $response ) ) {
return $response;
}
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme Variation Created.', 'create-block-theme' ),
)
);
}
function rest_create_blank_theme( $request ) {
$theme = $this->sanitize_theme_data( $request->get_params() );
//TODO: Handle screenshots
$screenshot = null;
$response = CBT_Theme_Create::create_blank_theme( $theme, $screenshot );
wp_cache_flush();
if ( is_wp_error( $response ) ) {
return $response;
}
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Blank Theme Created.', 'create-block-theme' ),
)
);
}
/**
* Export the theme as a ZIP file.
*/
function rest_export_theme( $request ) {
if ( ! class_exists( 'ZipArchive' ) ) {
return new WP_Error(
'missing_zip_package',
__( 'Unable to create a zip file. ZipArchive not available.', 'create-block-theme' ),
);
}
wp_cache_flush();
$theme_slug = wp_get_theme()->get( 'TextDomain' );
// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), $theme_slug );
$zip = CBT_Theme_Zip::create_zip( $filename, $theme_slug );
$zip = CBT_Theme_Zip::copy_theme_to_zip( $zip, null, null );
if ( is_child_theme() ) {
wp_cache_flush();
$zip = CBT_Theme_Zip::add_templates_to_zip( $zip, 'current', $theme_slug );
$theme_json = CBT_Theme_JSON_Resolver::export_theme_data( 'current' );
} else {
$zip = CBT_Theme_Zip::add_templates_to_zip( $zip, 'all', null );
$theme_json = CBT_Theme_JSON_Resolver::export_theme_data( 'all' );
}
$theme_json = CBT_Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json );
$zip = CBT_Theme_Zip::add_theme_json_to_zip( $zip, $theme_json );
$zip->close();
wp_cache_flush();
header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename=' . $theme_slug . '.zip' );
header( 'Content-Length: ' . filesize( $filename ) );
flush();
echo readfile( $filename );
exit;
}
/**
* Update the theme metadata and relocate the theme.
*/
function rest_update_theme( $request ) {
$theme = $this->sanitize_theme_data( $request->get_params() );
// Update the metadata of the theme in the style.css file
$style_css = file_get_contents( get_stylesheet_directory() . '/style.css' );
$style_css = CBT_Theme_Styles::update_style_css( $style_css, $theme );
file_put_contents( get_stylesheet_directory() . '/style.css', $style_css );
file_put_contents(
CBT_Theme_Readme::file_path(),
CBT_Theme_Readme::update( $theme, CBT_Theme_Readme::get_content() )
);
// Replace Screenshot
if ( wp_get_theme()->get_screenshot() !== $theme['screenshot'] ) {
CBT_Theme_Utils::replace_screenshot( $theme['screenshot'] );
}
wp_cache_flush();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme Updated.', 'create-block-theme' ),
)
);
}
/**
* Save the user changes to the theme and clear user changes.
*/
function rest_save_theme( $request ) {
$options = $request->get_params();
if ( isset( $options['saveFonts'] ) && true === $options['saveFonts'] ) {
CBT_Theme_Fonts::persist_font_settings();
}
if ( isset( $options['saveTemplates'] ) && true === $options['saveTemplates'] ) {
if ( true === $options['processOnlySavedTemplates'] ) {
CBT_Theme_Templates::add_templates_to_local( 'user', null, null, $options );
} else {
if ( is_child_theme() ) {
CBT_Theme_Templates::add_templates_to_local( 'current', null, null, $options );
} else {
CBT_Theme_Templates::add_templates_to_local( 'all', null, null, $options );
}
}
CBT_Theme_Templates::clear_user_templates_customizations();
CBT_Theme_Templates::clear_user_template_parts_customizations();
}
if ( isset( $options['saveStyle'] ) && true === $options['saveStyle'] ) {
if ( is_child_theme() ) {
CBT_Theme_JSON::add_theme_json_to_local( 'current', null, null, $options );
} else {
CBT_Theme_JSON::add_theme_json_to_local( 'all', null, null, $options );
}
CBT_Theme_Styles::clear_user_styles_customizations();
}
if ( isset( $options['savePatterns'] ) && true === $options['savePatterns'] ) {
$response = CBT_Theme_Patterns::add_patterns_to_theme( $options );
if ( is_wp_error( $response ) ) {
return $response;
}
}
wp_get_theme()->cache_delete();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme Saved.', 'create-block-theme' ),
)
);
}
/**
* Get a list of all the font families used in the theme.
*
* It includes the font families from the theme.json data (theme.json file + global styles) and the theme style variations.
* The font families with font faces containing src urls relative to the theme folder are converted to absolute urls.
*/
function rest_get_font_families( $request ) {
$font_families = CBT_Theme_Fonts::get_all_fonts();
return new WP_REST_Response(
array(
'status' => 'SUCCESS',
'message' => __( 'Font Families retrieved.', 'create-block-theme' ),
'data' => $font_families,
)
);
}
/**
* Reset the theme to the default state.
*/
function rest_reset_theme( $request ) {
$options = $request->get_params();
if ( isset( $options['resetStyles'] ) && true === $options['resetStyles'] ) {
CBT_Theme_Styles::clear_user_styles_customizations();
}
if ( isset( $options['resetTemplates'] ) && true === $options['resetTemplates'] ) {
CBT_Theme_Templates::clear_user_templates_customizations();
}
if ( isset( $options['resetTemplateParts'] ) && true === $options['resetTemplateParts'] ) {
CBT_Theme_Templates::clear_user_template_parts_customizations();
}
return rest_ensure_response(
array(
'status' => 'SUCCESS',
'message' => __( 'Theme Reset.', 'create-block-theme' ),
)
);
}
private function sanitize_theme_data( $theme ) {
$sanitized_theme['name'] = sanitize_text_field( $theme['name'] );
$sanitized_theme['description'] = sanitize_text_field( $theme['description'] ?? '' );
$sanitized_theme['uri'] = sanitize_text_field( $theme['uri'] ?? '' );
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] ?? '' );
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ?? '' );
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] ?? '' );
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
$sanitized_theme['screenshot'] = sanitize_text_field( $theme['screenshot'] ?? '' );
$sanitized_theme['requires_wp'] = sanitize_text_field( $theme['requires_wp'] ?? '' );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
$sanitized_theme['font_credits'] = sanitize_textarea_field( $theme['font_credits'] ?? '' );
$sanitized_theme['image_credits'] = sanitize_textarea_field( $theme['image_credits'] ?? '' );
$sanitized_theme['template'] = '';
$sanitized_theme['slug'] = sanitize_title( $theme['name'] );
$sanitized_theme['text_domain'] = $sanitized_theme['slug'];
return $sanitized_theme;
}
}