Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme Export: Export the whole theme #39202

Merged
merged 6 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 deletions lib/compat/wordpress-6.0/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
* @package gutenberg
*/

/**
* Filters theme directories that should be ignored during export.
*
* @since 6.0.0
*
* @param string $path The path of the file in the theme.
* @return Bool Whether this file is in an ignored directory.
*/
function gutenberg_is_theme_directory_ignored( $path ) {
$directories_to_ignore = array( '.git', 'node_modules', 'vendor' );
foreach ( $directories_to_ignore as $directory ) {
if ( strpos( $path, $directory ) === 0 ) {
return true;
}
}

return false;
}

/**
* Creates an export of the current templates and
* template parts from the site editor at the
Expand All @@ -26,21 +45,46 @@ function gutenberg_generate_block_templates_export_file() {
$filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the zip still be named this, or can it be the $theme_name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I second @jffng we should name the zip $theme_name IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in a followup


$zip = new ZipArchive();
if ( true !== $zip->open( $filename, ZipArchive::CREATE ) ) {
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' ) );
}

$zip->addEmptyDir( 'theme' );
$zip->addEmptyDir( 'theme/templates' );
$zip->addEmptyDir( 'theme/parts' );
$theme_name = wp_get_theme()->get( 'TextDomain' );

$zip->addEmptyDir( $theme_name );
$zip->addEmptyDir( $theme_name . '/templates' );
$zip->addEmptyDir( $theme_name . '/parts' );

// Get path of the theme.
$theme_path = wp_normalize_path( get_stylesheet_directory() );

// Create recursive directory iterator.
$theme_files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $theme_path ),
RecursiveIteratorIterator::LEAVES_ONLY
);

// Make a copy of the current theme.
foreach ( $theme_files as $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() );
$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 );
}
}
}

// Load templates into the zip file.
$templates = gutenberg_get_block_templates();
foreach ( $templates as $template ) {
$template->content = _remove_theme_attribute_in_block_template_content( $template->content );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template parts can also reference other template parts, is there a way to remove the theme attribute from those also?


$zip->addFromString(
'theme/templates/' . $template->slug . '.html',
$theme_name . '/templates/' . $template->slug . '.html',
$template->content
);
}
Expand All @@ -49,29 +93,20 @@ 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/parts/' . $template_part->slug . '.html',
$theme_name . '/parts/' . $template_part->slug . '.html',
$template_part->content
);
}

// Load theme.json into the zip file.
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data();
$tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );

$zip->addFromString(
'theme/theme.json',
$theme_name . '/theme.json',
wp_json_encode( $tree->get_data(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
);

$zip->addFile(
wp_normalize_path( get_stylesheet_directory() ) . '/index.php',
'theme/index.php'
);

$zip->addFile(
wp_normalize_path( get_stylesheet_directory() ) . '/style.css',
'theme/style.css'
);

// Save changes to the zip file.
$zip->close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export default function SiteExport() {
role="menuitem"
icon={ download }
onClick={ handleExport }
info={ __( 'Download your templates and styles as a theme.' ) }
info={ __(
'Download your theme with updated templates and styles.'
) }
>
{ _x( 'Export', 'site exporter menu item' ) }
</MenuItem>
Expand Down