Skip to content

Commit

Permalink
Edit Site: Add theme exporter. (#22922)
Browse files Browse the repository at this point in the history
* Edit Site: Add theme exporter.

* Lib: Make new API experimental and prefix functions.
  • Loading branch information
epiqueras authored Jun 9, 2020
1 parent c53d36b commit 848b493
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
64 changes: 64 additions & 0 deletions lib/edit-site-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* REST endpoint for exporting the contents of the Edit Site Page editor.
*
* @package gutenberg
*/

/**
* Output a ZIP file with an export of the current templates
* and template parts from the site editor, and close the connection.
*/
function gutenberg_edit_site_export() {
// Create ZIP file and directories.
$filename = tempnam( get_temp_dir(), 'edit-site-export' );
$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::OVERWRITE );
$zip->addEmptyDir( 'theme' );
$zip->addEmptyDir( 'theme/block-templates' );
$zip->addEmptyDir( 'theme/block-template-parts' );

// Load files into ZIP file.
foreach ( get_template_types() as $template_type ) {
// Skip 'embed' for now because it is not a regular template type.
// Skip 'index' because it's a fallback that we handle differently.
if ( in_array( $template_type, array( 'embed', 'index' ), true ) ) {
continue;
}

$current_template = gutenberg_find_template_post_and_parts( $template_type );
if ( isset( $current_template ) ) {
$zip->addFromString( 'theme/block-templates/' . $current_template['template_post']->post_name . '.html', $current_template['template_post']->post_content );

foreach ( $current_template['template_part_ids'] as $template_part_id ) {
$template_part = get_post( $template_part_id );
$zip->addFromString( 'theme/block-template-parts/' . $template_part->post_name . '.html', $template_part->post_content );
}
}
}

// Send back the ZIP file.
$zip->close();
header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename=edit-site-export.zip' );
header( 'Content-Length: ' . filesize( $filename ) );
flush();
echo readfile( $filename );
die();
}
add_action(
'rest_api_init',
function () {
register_rest_route(
'__experimental/edit-site/v1',
'/export',
array(
'methods' => 'GET',
'callback' => 'gutenberg_edit_site_export',
'permission_callback' => function () {
return current_user_can( 'edit_theme_options' );
},
)
);
}
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require dirname( __FILE__ ) . '/experiments-page.php';
require dirname( __FILE__ ) . '/customizer.php';
require dirname( __FILE__ ) . '/edit-site-page.php';
require dirname( __FILE__ ) . '/edit-site-export.php';
require dirname( __FILE__ ) . '/editor-features.php';
require dirname( __FILE__ ) . '/global-styles.php';
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@wordpress/plugins": "file:../plugins",
"@wordpress/primitives": "file:../primitives",
"@wordpress/url": "file:../url",
"downloadjs": "^1.4.7",
"file-saver": "^2.0.2",
"jszip": "^3.2.2",
"lodash": "^4.17.15",
Expand Down
29 changes: 29 additions & 0 deletions packages/edit-site/src/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/**
* External dependencies
*/
import downloadjs from 'downloadjs';

/**
* WordPress dependencies
*/
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';

/**
* Internal dependencies
Expand All @@ -16,6 +22,29 @@ registerPlugin( 'edit-site', {
return (
<>
<ToolsMoreMenuGroup>
<MenuItem
role="menuitem"
icon="download"
onClick={ () =>
apiFetch( {
path: '/__experimental/edit-site/v1/export',
parse: false,
} )
.then( ( res ) => res.blob() )
.then( ( blob ) =>
downloadjs(
blob,
'edit-site-export.zip',
'application/zip'
)
)
}
info={ __(
'Download your templates and template parts.'
) }
>
{ __( 'Export' ) }
</MenuItem>
<MenuItem
role="menuitem"
href={ addQueryArgs( 'edit.php', {
Expand Down

0 comments on commit 848b493

Please sign in to comment.