| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST endpoint for exporting the contents of the Edit Site Page editor. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Output a ZIP file with an export of the current templates |
| 10 |
* and template parts from the site editor, and close the connection. |
| 11 |
*/ |
| 12 |
function gutenberg_edit_site_export() { |
| 13 |
// Create ZIP file and directories. |
| 14 |
$filename = tempnam( get_temp_dir(), 'edit-site-export' ); |
| 15 |
$zip = new ZipArchive(); |
| 16 |
$zip->open( $filename, ZipArchive::OVERWRITE ); |
| 17 |
$zip->addEmptyDir( 'theme' ); |
| 18 |
$zip->addEmptyDir( 'theme/block-templates' ); |
| 19 |
$zip->addEmptyDir( 'theme/block-template-parts' ); |
| 20 |
|
| 21 |
// Load files into ZIP file. |
| 22 |
foreach ( get_template_types() as $template_type ) { |
| 23 |
// Skip 'embed' for now because it is not a regular template type. |
| 24 |
// Skip 'index' because it's a fallback that we handle differently. |
| 25 |
if ( in_array( $template_type, array( 'embed', 'index' ), true ) ) { |
| 26 |
continue; |
| 27 |
} |
| 28 |
|
| 29 |
$current_template = gutenberg_find_template_post_and_parts( $template_type ); |
| 30 |
if ( isset( $current_template ) ) { |
| 31 |
$zip->addFromString( 'theme/block-templates/' . $current_template['template_post']->post_name . '.html', $current_template['template_post']->post_content ); |
| 32 |
|
| 33 |
foreach ( $current_template['template_part_ids'] as $template_part_id ) { |
| 34 |
$template_part = get_post( $template_part_id ); |
| 35 |
$zip->addFromString( 'theme/block-template-parts/' . $template_part->post_name . '.html', $template_part->post_content ); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Send back the ZIP file. |
| 41 |
$zip->close(); |
| 42 |
header( 'Content-Type: application/zip' ); |
| 43 |
header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); |
| 44 |
header( 'Content-Length: ' . filesize( $filename ) ); |
| 45 |
flush(); |
| 46 |
echo readfile( $filename ); |
| 47 |
die(); |
| 48 |
} |
| 49 |
add_action( |
| 50 |
'rest_api_init', |
| 51 |
function () { |
| 52 |
register_rest_route( |
| 53 |
'__experimental/edit-site/v1', |
| 54 |
'/export', |
| 55 |
array( |
| 56 |
'methods' => 'GET', |
| 57 |
'callback' => 'gutenberg_edit_site_export', |
| 58 |
'permission_callback' => function () { |
| 59 |
return current_user_can( 'edit_theme_options' ); |
| 60 |
}, |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
); |
| 65 |
|