From 4563b97bb8b8b0f450600417c559ecc95f4be030 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Tue, 15 Mar 2022 16:08:14 +0000 Subject: [PATCH 1/5] Theme Export: Use the theme name for the zip file --- .../wordpress-6.0/block-template-utils.php | 18 ++++++++---------- ...enberg-rest-edit-site-export-controller.php | 3 ++- .../components/header/more-menu/site-export.js | 5 ++++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 96c819d5aec72a..abd782cbd8a5b9 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -42,18 +42,16 @@ function gutenberg_generate_block_templates_export_file() { } $obscura = wp_generate_password( 12, false, false ); - $filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip'; + $theme_name = wp_get_theme()->get( 'TextDomain' ); + $filename = get_temp_dir() . $theme_name . $obscura . '.zip'; $zip = new ZipArchive(); if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.', 'gutenberg' ) ); } - $theme_name = wp_get_theme()->get( 'TextDomain' ); - - $zip->addEmptyDir( $theme_name ); - $zip->addEmptyDir( $theme_name . '/templates' ); - $zip->addEmptyDir( $theme_name . '/parts' ); + $zip->addEmptyDir( 'templates' ); + $zip->addEmptyDir( 'parts' ); // Get path of the theme. $theme_path = wp_normalize_path( get_stylesheet_directory() ); @@ -73,7 +71,7 @@ function gutenberg_generate_block_templates_export_file() { $relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); if ( ! gutenberg_is_theme_directory_ignored( $relative_path ) ) { - $zip->addFile( $file_path, $theme_name . '/' . $relative_path ); + $zip->addFile( $file_path, $relative_path ); } } } @@ -84,7 +82,7 @@ function gutenberg_generate_block_templates_export_file() { $template->content = _remove_theme_attribute_in_block_template_content( $template->content ); $zip->addFromString( - $theme_name . '/templates/' . $template->slug . '.html', + 'templates/' . $template->slug . '.html', $template->content ); } @@ -93,7 +91,7 @@ function gutenberg_generate_block_templates_export_file() { $template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); foreach ( $template_parts as $template_part ) { $zip->addFromString( - $theme_name . '/parts/' . $template_part->slug . '.html', + 'parts/' . $template_part->slug . '.html', $template_part->content ); } @@ -103,7 +101,7 @@ function gutenberg_generate_block_templates_export_file() { $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); $zip->addFromString( - $theme_name . '/theme.json', + 'theme.json', wp_json_encode( $tree->get_data(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); diff --git a/lib/compat/wordpress-6.0/class-gutenberg-rest-edit-site-export-controller.php b/lib/compat/wordpress-6.0/class-gutenberg-rest-edit-site-export-controller.php index 80d73f001b37a5..f7cedb42980a74 100644 --- a/lib/compat/wordpress-6.0/class-gutenberg-rest-edit-site-export-controller.php +++ b/lib/compat/wordpress-6.0/class-gutenberg-rest-edit-site-export-controller.php @@ -76,8 +76,9 @@ public function export() { return $filename; } + $theme_name = wp_get_theme()->get( 'TextDomain' ); header( 'Content-Type: application/zip' ); - header( 'Content-Disposition: attachment; filename=edit-site-export.zip' ); + header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' ); header( 'Content-Length: ' . filesize( $filename ) ); flush(); readfile( $filename ); diff --git a/packages/edit-site/src/components/header/more-menu/site-export.js b/packages/edit-site/src/components/header/more-menu/site-export.js index 1b8d75c228f396..0de953ca60a43c 100644 --- a/packages/edit-site/src/components/header/more-menu/site-export.js +++ b/packages/edit-site/src/components/header/more-menu/site-export.js @@ -23,8 +23,11 @@ export default function SiteExport() { parse: false, } ); const blob = await response.blob(); + const contentDisposition = response.headers.get( 'content-disposition' ); + const contentDispositionMatches = contentDisposition.match( /=(.+)\.zip/ ); + const fileName = contentDispositionMatches[ 1 ] ? contentDispositionMatches[ 1 ] : 'edit-site-export'; - downloadjs( blob, 'edit-site-export.zip', 'application/zip' ); + downloadjs( blob, fileName + '.zip', 'application/zip' ); } catch ( errorResponse ) { let error = {}; try { From 613789e6d4d4aedff0dc5dd5e1deff5f3691eaca Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Wed, 16 Mar 2022 09:36:26 +0000 Subject: [PATCH 2/5] reformat --- .../src/components/header/more-menu/site-export.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/edit-site/src/components/header/more-menu/site-export.js b/packages/edit-site/src/components/header/more-menu/site-export.js index 0de953ca60a43c..234c493f65291b 100644 --- a/packages/edit-site/src/components/header/more-menu/site-export.js +++ b/packages/edit-site/src/components/header/more-menu/site-export.js @@ -23,9 +23,15 @@ export default function SiteExport() { parse: false, } ); const blob = await response.blob(); - const contentDisposition = response.headers.get( 'content-disposition' ); - const contentDispositionMatches = contentDisposition.match( /=(.+)\.zip/ ); - const fileName = contentDispositionMatches[ 1 ] ? contentDispositionMatches[ 1 ] : 'edit-site-export'; + const contentDisposition = response.headers.get( + 'content-disposition' + ); + const contentDispositionMatches = contentDisposition.match( + /=(.+)\.zip/ + ); + const fileName = contentDispositionMatches[ 1 ] + ? contentDispositionMatches[ 1 ] + : 'edit-site-export'; downloadjs( blob, fileName + '.zip', 'application/zip' ); } catch ( errorResponse ) { From 97cd8424035689a619caad55467923f408a08879 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Thu, 17 Mar 2022 11:18:49 +0000 Subject: [PATCH 3/5] don't use the real path --- lib/compat/wordpress-6.0/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index abd782cbd8a5b9..878684a60bd513 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -67,7 +67,7 @@ function gutenberg_generate_block_templates_export_file() { // Skip directories as they are added automatically. if ( ! $file->isDir() ) { // Get real and relative path for current file. - $file_path = wp_normalize_path( $file->getRealPath() ); + $file_path = wp_normalize_path( $file ); $relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); if ( ! gutenberg_is_theme_directory_ignored( $relative_path ) ) { From 2cd9c7b16f0ae132eedbea617b53af51f996a762 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 18 Mar 2022 10:04:57 +0000 Subject: [PATCH 4/5] fix linting --- lib/compat/wordpress-6.0/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.0/block-template-utils.php b/lib/compat/wordpress-6.0/block-template-utils.php index 878684a60bd513..8d17c9c3b138f3 100644 --- a/lib/compat/wordpress-6.0/block-template-utils.php +++ b/lib/compat/wordpress-6.0/block-template-utils.php @@ -41,9 +41,9 @@ function gutenberg_generate_block_templates_export_file() { return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.', 'gutenberg' ) ); } - $obscura = wp_generate_password( 12, false, false ); + $obscura = wp_generate_password( 12, false, false ); $theme_name = wp_get_theme()->get( 'TextDomain' ); - $filename = get_temp_dir() . $theme_name . $obscura . '.zip'; + $filename = get_temp_dir() . $theme_name . $obscura . '.zip'; $zip = new ZipArchive(); if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { From f8e58ac2a4458fcf9b548fd05cafb9e558b46b2c Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Mon, 21 Mar 2022 15:56:08 +0000 Subject: [PATCH 5/5] update test --- .../e2e-tests/specs/site-editor/site-editor-export.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/e2e-tests/specs/site-editor/site-editor-export.test.js b/packages/e2e-tests/specs/site-editor/site-editor-export.test.js index 82072bdf309af4..cb457fffcfc0b6 100644 --- a/packages/e2e-tests/specs/site-editor/site-editor-export.test.js +++ b/packages/e2e-tests/specs/site-editor/site-editor-export.test.js @@ -43,7 +43,7 @@ describe( 'Site Editor Templates Export', () => { await visitSiteEditor(); } ); - it( 'clicking export should download edit-site-export.zip file', async () => { + it( 'clicking export should download emptytheme.zip file', async () => { const directory = fs.mkdtempSync( path.join( os.tmpdir(), 'test-edit-site-export-' ) ); @@ -53,7 +53,7 @@ describe( 'Site Editor Templates Export', () => { } ); await clickOnMoreMenuItem( 'Export', 'site-editor' ); - const filePath = path.join( directory, 'edit-site-export.zip' ); + const filePath = path.join( directory, 'emptytheme.zip' ); await waitForFileExists( filePath ); expect( fs.existsSync( filePath ) ).toBe( true ); fs.unlinkSync( filePath );