-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bfc4119
Theme Export: Export the whole theme with updated templates and styles
scruffian 70ac83f
Ensure paths are normalized
scruffian 1b02408
ignore utility directories
scruffian 9085b8d
rename function
scruffian 3f25bf8
fixing linter errors
scruffian be291f3
fix lint error
scruffian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -26,21 +45,46 @@ function gutenberg_generate_block_templates_export_file() { | |
$filename = get_temp_dir() . 'edit-site-export-' . $obscura . '.zip'; | ||
|
||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
); | ||
} | ||
|
@@ -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(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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