From 0c229791bdea499b5c31578c3283fead54784c6a Mon Sep 17 00:00:00 2001 From: epiqueras Date: Thu, 4 Jun 2020 16:28:22 -0700 Subject: [PATCH 1/2] Edit Site: Add theme exporter. --- lib/edit-site-export.php | 64 +++++++++++++++++++++++++ lib/load.php | 1 + lib/template-loader.php | 2 +- package-lock.json | 6 +++ packages/edit-site/package.json | 1 + packages/edit-site/src/plugins/index.js | 29 +++++++++++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 lib/edit-site-export.php diff --git a/lib/edit-site-export.php b/lib/edit-site-export.php new file mode 100644 index 0000000000000..99e14dcbc5b30 --- /dev/null +++ b/lib/edit-site-export.php @@ -0,0 +1,64 @@ +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( + 'edit-site/v1', + '/export', + array( + 'methods' => 'GET', + 'callback' => 'edit_site_export', + 'permission_callback' => function () { + return current_user_can( 'edit_theme_options' ); + }, + ) + ); + } +); diff --git a/lib/load.php b/lib/load.php index 52899966e73b7..a495575668868 100644 --- a/lib/load.php +++ b/lib/load.php @@ -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'; diff --git a/lib/template-loader.php b/lib/template-loader.php index 12ac078ad9231..44536901cc284 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -321,7 +321,7 @@ function gutenberg_find_template_post_and_parts( $template_type, $template_hiera if ( $current_template_post ) { $template_part_ids = array(); - if ( is_admin() ) { + if ( is_admin() || defined( 'REST_REQUEST' ) ) { foreach ( parse_blocks( $current_template_post->post_content ) as $block ) { $template_part_ids = array_merge( $template_part_ids, create_auto_draft_for_template_part_block( $block ) ); } diff --git a/package-lock.json b/package-lock.json index 9c41fdb588a59..780851f2631fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10649,6 +10649,7 @@ "@wordpress/plugins": "file:packages/plugins", "@wordpress/primitives": "file:packages/primitives", "@wordpress/url": "file:packages/url", + "downloadjs": "^1.4.7", "file-saver": "^2.0.2", "jszip": "^3.2.2", "lodash": "^4.17.15", @@ -17546,6 +17547,11 @@ "dotenv-defaults": "^1.0.2" } }, + "downloadjs": { + "version": "1.4.7", + "resolved": "https://registry.npmjs.org/downloadjs/-/downloadjs-1.4.7.tgz", + "integrity": "sha1-9p+W+UDg0FU9rCkROYZaPNAQHjw=" + }, "downshift": { "version": "4.0.7", "resolved": "https://registry.npmjs.org/downshift/-/downshift-4.0.7.tgz", diff --git a/packages/edit-site/package.json b/packages/edit-site/package.json index d9252cf2ba894..764d0e0a8aaca 100644 --- a/packages/edit-site/package.json +++ b/packages/edit-site/package.json @@ -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", diff --git a/packages/edit-site/src/plugins/index.js b/packages/edit-site/src/plugins/index.js index f021f1595880a..14570a7f5f47f 100644 --- a/packages/edit-site/src/plugins/index.js +++ b/packages/edit-site/src/plugins/index.js @@ -1,3 +1,8 @@ +/** + * External dependencies + */ +import downloadjs from 'downloadjs'; + /** * WordPress dependencies */ @@ -5,6 +10,7 @@ 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 @@ -16,6 +22,29 @@ registerPlugin( 'edit-site', { return ( <> + + apiFetch( { + path: '/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' ) } + Date: Mon, 8 Jun 2020 21:23:53 -0700 Subject: [PATCH 2/2] Lib: Make new API experimental and prefix functions. --- lib/edit-site-export.php | 6 +++--- packages/edit-site/src/plugins/index.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/edit-site-export.php b/lib/edit-site-export.php index 99e14dcbc5b30..c62fb42c2c930 100644 --- a/lib/edit-site-export.php +++ b/lib/edit-site-export.php @@ -9,7 +9,7 @@ * Output a ZIP file with an export of the current templates * and template parts from the site editor, and close the connection. */ -function edit_site_export() { +function gutenberg_edit_site_export() { // Create ZIP file and directories. $filename = tempnam( get_temp_dir(), 'edit-site-export' ); $zip = new ZipArchive(); @@ -50,11 +50,11 @@ function edit_site_export() { 'rest_api_init', function () { register_rest_route( - 'edit-site/v1', + '__experimental/edit-site/v1', '/export', array( 'methods' => 'GET', - 'callback' => 'edit_site_export', + 'callback' => 'gutenberg_edit_site_export', 'permission_callback' => function () { return current_user_can( 'edit_theme_options' ); }, diff --git a/packages/edit-site/src/plugins/index.js b/packages/edit-site/src/plugins/index.js index 14570a7f5f47f..436907cfdc697 100644 --- a/packages/edit-site/src/plugins/index.js +++ b/packages/edit-site/src/plugins/index.js @@ -27,7 +27,7 @@ registerPlugin( 'edit-site', { icon="download" onClick={ () => apiFetch( { - path: '/edit-site/v1/export', + path: '/__experimental/edit-site/v1/export', parse: false, } ) .then( ( res ) => res.blob() )