| 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 |
// Theme templates and template parts need to be synchronized |
| 14 |
// before the export. |
| 15 |
_gutenberg_synchronize_theme_templates( 'template-part' ); |
| 16 |
_gutenberg_synchronize_theme_templates( 'template' ); |
| 17 |
|
| 18 |
// Create ZIP file and directories. |
| 19 |
$filename = tempnam( get_temp_dir(), 'edit-site-export' ); |
| 20 |
$zip = new ZipArchive(); |
| 21 |
$zip->open( $filename, ZipArchive::OVERWRITE ); |
| 22 |
$zip->addEmptyDir( 'theme' ); |
| 23 |
$zip->addEmptyDir( 'theme/block-templates' ); |
| 24 |
$zip->addEmptyDir( 'theme/block-template-parts' ); |
| 25 |
|
| 26 |
$theme = wp_get_theme()->get_stylesheet(); |
| 27 |
|
| 28 |
// Load templates into the zip file. |
| 29 |
$template_query = new WP_Query( |
| 30 |
array( |
| 31 |
'post_type' => 'wp_template', |
| 32 |
'post_status' => array( 'publish', 'auto-draft' ), |
| 33 |
'meta_key' => 'theme', |
| 34 |
'meta_value' => $theme, |
| 35 |
'posts_per_page' => -1, |
| 36 |
'no_found_rows' => true, |
| 37 |
) |
| 38 |
); |
| 39 |
while ( $template_query->have_posts() ) { |
| 40 |
$template = $template_query->next_post(); |
| 41 |
$zip->addFromString( |
| 42 |
'theme/block-templates/' . $template->post_name . '.html', |
| 43 |
gutenberg_strip_post_ids_from_template_part_blocks( $template->post_content ) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
// Load template parts into the zip file. |
| 48 |
$template_part_query = new WP_Query( |
| 49 |
array( |
| 50 |
'post_type' => 'wp_template_part', |
| 51 |
'post_status' => array( 'publish', 'auto-draft' ), |
| 52 |
'meta_key' => 'theme', |
| 53 |
'meta_value' => $theme, |
| 54 |
'posts_per_page' => -1, |
| 55 |
'no_found_rows' => true, |
| 56 |
) |
| 57 |
); |
| 58 |
while ( $template_part_query->have_posts() ) { |
| 59 |
$template_part = $template_part_query->next_post(); |
| 60 |
$zip->addFromString( |
| 61 |
'theme/block-template-parts/' . $template_part->post_name . '.html', |
| 62 |
gutenberg_strip_post_ids_from_template_part_blocks( $template_part->post_content ) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
// Send back the ZIP file. |
| 67 |
$zip->close(); |
| 68 |
header( 'Content-Type: application/zip' ); |
| 69 |
header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); |
| 70 |
header( 'Content-Length: ' . filesize( $filename ) ); |
| 71 |
flush(); |
| 72 |
echo readfile( $filename ); |
| 73 |
die(); |
| 74 |
} |
| 75 |
|
| 76 |
add_action( |
| 77 |
'rest_api_init', |
| 78 |
function () { |
| 79 |
register_rest_route( |
| 80 |
'__experimental/edit-site/v1', |
| 81 |
'/export', |
| 82 |
array( |
| 83 |
'methods' => 'GET', |
| 84 |
'callback' => 'gutenberg_edit_site_export', |
| 85 |
'permission_callback' => function () { |
| 86 |
return current_user_can( 'edit_theme_options' ); |
| 87 |
}, |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
); |
| 92 |
|
| 93 |
/** |
| 94 |
* Remove post id attributes from template part blocks. |
| 95 |
* |
| 96 |
* This is needed so that Gutenberg loads the HTML file of the template, instead of looking for a template part post. |
| 97 |
* |
| 98 |
* @param string $template_content Template content to modify. |
| 99 |
* |
| 100 |
* @return string Potentially modified template content. |
| 101 |
*/ |
| 102 |
function gutenberg_strip_post_ids_from_template_part_blocks( $template_content ) { |
| 103 |
$blocks = parse_blocks( $template_content ); |
| 104 |
|
| 105 |
array_walk( |
| 106 |
$blocks, |
| 107 |
function( &$block ) { |
| 108 |
if ( 'core/template-part' !== $block['blockName'] ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
unset( $block['attrs']['postId'] ); |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
return serialize_blocks( $blocks ); |
| 117 |
} |
| 118 |
|