Skip to content

Commit

Permalink
Theme Export: Use the theme name for the zip file (WordPress#39471)
Browse files Browse the repository at this point in the history
* Theme Export: Use the theme name for the zip file

* reformat

* don't use the real path

* fix linting

* update test
  • Loading branch information
scruffian authored and jostnes committed Mar 23, 2022
1 parent c9a650f commit a322642
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
22 changes: 10 additions & 12 deletions lib/compat/wordpress-6.0/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,17 @@ 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 );
$filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip';
$obscura = wp_generate_password( 12, false, false );
$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() );
Expand All @@ -69,11 +67,11 @@ 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 ) ) {
$zip->addFile( $file_path, $theme_name . '/' . $relative_path );
$zip->addFile( $file_path, $relative_path );
}
}
}
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand All @@ -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 )
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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-' )
);
Expand All @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ 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 {
Expand Down

0 comments on commit a322642

Please sign in to comment.