-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into tabs/decimal-point-indicator
- Loading branch information
Showing
33 changed files
with
323 additions
and
14 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
/** | ||
* Utilities used to fetch and create templates and template parts. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Creates an export of the current templates and | ||
* template parts from the site editor at the | ||
* specified path in a ZIP file. | ||
* | ||
* @since 5.9.0 | ||
* @since 6.0.0 Adds the whole theme to the export archive. | ||
* | ||
* @global string $wp_version The WordPress version string. | ||
* | ||
* @return WP_Error|string Path of the ZIP file or error on failure. | ||
*/ | ||
function gutenberg_generate_block_templates_export_file() { | ||
global $wp_version; | ||
|
||
if ( ! class_exists( 'ZipArchive' ) ) { | ||
return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.', 'gutenberg' ) ); | ||
} | ||
|
||
$obscura = wp_generate_password( 12, false, false ); | ||
$theme_name = basename( get_stylesheet() ); | ||
$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' ) ); | ||
} | ||
|
||
$zip->addEmptyDir( 'templates' ); | ||
$zip->addEmptyDir( '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 ); | ||
$relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); | ||
|
||
if ( ! wp_is_theme_directory_ignored( $relative_path ) ) { | ||
$zip->addFile( $file_path, $relative_path ); | ||
} | ||
} | ||
} | ||
|
||
// Load templates into the zip file. | ||
$templates = gutenberg_get_block_templates(); | ||
foreach ( $templates as $template ) { | ||
$template->content = traverse_and_serialize_blocks( | ||
parse_blocks( $template->content ), | ||
'_remove_theme_attribute_from_template_part_block' | ||
); | ||
|
||
$zip->addFromString( | ||
'templates/' . $template->slug . '.html', | ||
$template->content | ||
); | ||
} | ||
|
||
// Load template parts into the zip file. | ||
$template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' ); | ||
foreach ( $template_parts as $template_part ) { | ||
$zip->addFromString( | ||
'parts/' . $template_part->slug . '.html', | ||
$template_part->content | ||
); | ||
} | ||
|
||
// Load theme.json into the zip file. | ||
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array(), array( 'with_supports' => false ) ); | ||
// Merge with user data. | ||
$tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); | ||
|
||
$theme_json_raw = $tree->get_data(); | ||
// If a version is defined, add a schema. | ||
if ( $theme_json_raw['version'] ) { | ||
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 ); | ||
$schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' ); | ||
$theme_json_raw = array_merge( $schema, $theme_json_raw ); | ||
} | ||
|
||
// Convert to a string. | ||
$theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); | ||
|
||
// Replace 4 spaces with a tab. | ||
$theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); | ||
|
||
// Add the theme.json file to the zip. | ||
$zip->addFromString( | ||
'theme.json', | ||
$theme_json_tabbed | ||
); | ||
|
||
// Save changes to the zip file. | ||
$zip->close(); | ||
|
||
return $filename; | ||
} |
46 changes: 46 additions & 0 deletions
46
lib/class-wp-rest-edit-site-export-controller-gutenberg.php
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Controller which provides REST endpoint for exporting current templates | ||
* and template parts. | ||
* | ||
* This class extension exists so that theme exporting takes into account any updates/changes to | ||
* WP_Theme_JSON_Gutenberg, WP_Theme_JSON_Resolver_Gutenberg or related classes. | ||
* | ||
* @package gutenberg | ||
* @subpackage REST_API | ||
* @since 5.9.0 | ||
*/ | ||
|
||
if ( class_exists( 'WP_REST_Edit_Site_Export_Controller_Gutenberg' ) ) { | ||
return; | ||
} | ||
|
||
class WP_REST_Edit_Site_Export_Controller_Gutenberg extends WP_REST_Edit_Site_Export_Controller { | ||
/** | ||
* Output a ZIP file with an export of the current templates | ||
* and template parts from the site editor, and close the connection. | ||
* | ||
* @since 5.9.0 | ||
* | ||
* @return WP_Error|void | ||
*/ | ||
public function export() { | ||
// Generate the export file. | ||
$filename = gutenberg_generate_block_templates_export_file(); | ||
|
||
if ( is_wp_error( $filename ) ) { | ||
$filename->add_data( array( 'status' => 500 ) ); | ||
|
||
return $filename; | ||
} | ||
|
||
$theme_name = basename( get_stylesheet() ); | ||
header( 'Content-Type: application/zip' ); | ||
header( 'Content-Disposition: attachment; filename=' . $theme_name . '.zip' ); | ||
header( 'Content-Length: ' . filesize( $filename ) ); | ||
flush(); | ||
readfile( $filename ); | ||
unlink( $filename ); | ||
exit; | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.