| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utilities used to fetch and create templates and template parts. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Creates an export of the current templates and |
| 10 |
* template parts from the site editor at the |
| 11 |
* specified path in a ZIP file. |
| 12 |
* |
| 13 |
* @since 5.9.0 |
| 14 |
* @since 6.0.0 Adds the whole theme to the export archive. |
| 15 |
* |
| 16 |
* @global string $wp_version The WordPress version string. |
| 17 |
* |
| 18 |
* @return WP_Error|string Path of the ZIP file or error on failure. |
| 19 |
*/ |
| 20 |
function gutenberg_generate_block_templates_export_file() { |
| 21 |
global $wp_version; |
| 22 |
|
| 23 |
if ( ! class_exists( 'ZipArchive' ) ) { |
| 24 |
return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.', 'gutenberg' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
$obscura = wp_generate_password( 12, false, false ); |
| 28 |
$theme_name = basename( get_stylesheet() ); |
| 29 |
$filename = get_temp_dir() . $theme_name . $obscura . '.zip'; |
| 30 |
|
| 31 |
$zip = new ZipArchive(); |
| 32 |
if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { |
| 33 |
return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.', 'gutenberg' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
$zip->addEmptyDir( 'templates' ); |
| 37 |
$zip->addEmptyDir( 'parts' ); |
| 38 |
|
| 39 |
// Get path of the theme. |
| 40 |
$theme_path = wp_normalize_path( get_stylesheet_directory() ); |
| 41 |
|
| 42 |
// Create recursive directory iterator. |
| 43 |
$theme_files = new RecursiveIteratorIterator( |
| 44 |
new RecursiveDirectoryIterator( $theme_path ), |
| 45 |
RecursiveIteratorIterator::LEAVES_ONLY |
| 46 |
); |
| 47 |
|
| 48 |
// Make a copy of the current theme. |
| 49 |
foreach ( $theme_files as $file ) { |
| 50 |
// Skip directories as they are added automatically. |
| 51 |
if ( ! $file->isDir() ) { |
| 52 |
// Get real and relative path for current file. |
| 53 |
$file_path = wp_normalize_path( $file ); |
| 54 |
$relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); |
| 55 |
|
| 56 |
if ( ! wp_is_theme_directory_ignored( $relative_path ) ) { |
| 57 |
$zip->addFile( $file_path, $relative_path ); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Load templates into the zip file. |
| 63 |
$templates = get_block_templates(); |
| 64 |
foreach ( $templates as $template ) { |
| 65 |
$template->content = traverse_and_serialize_blocks( |
| 66 |
parse_blocks( $template->content ), |
| 67 |
'_remove_theme_attribute_from_template_part_block' |
| 68 |
); |
| 69 |
|
| 70 |
$zip->addFromString( |
| 71 |
'templates/' . $template->slug . '.html', |
| 72 |
$template->content |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
// Load template parts into the zip file. |
| 77 |
$template_parts = get_block_templates( array(), 'wp_template_part' ); |
| 78 |
foreach ( $template_parts as $template_part ) { |
| 79 |
$zip->addFromString( |
| 80 |
'parts/' . $template_part->slug . '.html', |
| 81 |
$template_part->content |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
// Load theme.json into the zip file. |
| 86 |
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array(), array( 'with_supports' => false ) ); |
| 87 |
// Merge with user data. |
| 88 |
$tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() ); |
| 89 |
|
| 90 |
$theme_json_raw = $tree->get_data(); |
| 91 |
// If a version is defined, add a schema. |
| 92 |
if ( $theme_json_raw['version'] ) { |
| 93 |
$theme_json_version = 'wp/' . substr( $wp_version, 0, 3 ); |
| 94 |
$schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' ); |
| 95 |
$theme_json_raw = array_merge( $schema, $theme_json_raw ); |
| 96 |
} |
| 97 |
|
| 98 |
// Convert to a string. |
| 99 |
$theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); |
| 100 |
|
| 101 |
// Replace 4 spaces with a tab. |
| 102 |
$theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded ); |
| 103 |
|
| 104 |
// Add the theme.json file to the zip. |
| 105 |
$zip->addFromString( |
| 106 |
'theme.json', |
| 107 |
$theme_json_tabbed |
| 108 |
); |
| 109 |
|
| 110 |
// Save changes to the zip file. |
| 111 |
$zip->close(); |
| 112 |
|
| 113 |
return $filename; |
| 114 |
} |
| 115 |
|