PluginProbe
Gutenberg / 23.2.0
Gutenberg v23.2.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / block-template-utils.php
lib
block-supports 4 months ago compat 3 months ago experimental 3 months ago media 3 months ago README.md 10.0 KB 7 months ago block-editor-settings.php 5.4 KB 7 months ago block-template-utils.php 3.5 KB 1 year ago blocks.php 11.8 KB 7 months ago class-wp-duotone-gutenberg.php 37.6 KB 5 months ago class-wp-icons-registry-gutenberg.php 7.1 KB 4 months ago class-wp-rest-edit-site-export-controller-gutenberg.php 1.2 KB 2 years ago class-wp-rest-global-styles-controller-gutenberg.php 24.5 KB 7 months ago class-wp-rest-icons-controller-gutenberg.php 481 B 4 months ago class-wp-theme-json-data-gutenberg.php 1.7 KB 2 years ago class-wp-theme-json-gutenberg.php 196.1 KB 3 months ago class-wp-theme-json-resolver-gutenberg.php 34.7 KB 6 months ago class-wp-theme-json-schema-gutenberg.php 7.4 KB 7 months ago client-assets.php 16.4 KB 4 months ago demo.php 1.6 KB 1 year ago global-styles-and-settings.php 14.2 KB 7 months ago init.php 945 B 4 months ago interactivity-api.php 1.2 KB 7 months ago load.php 10.9 KB 3 months ago mathml-kses.php 6.3 KB 10 months ago overlay-patterns.php 12.2 KB 6 months ago remove-core-enqueue-scripts.php 779 B 5 months ago rest-api.php 1.6 KB 4 months ago script-loader.php 5.9 KB 5 months ago theme-i18n.json 1.8 KB 7 months ago theme.json 9.2 KB 5 months ago upgrade.php 2.6 KB 5 months ago

block-template-utils.php in Gutenberg 23.2.0, at lib/block-template-utils.php

115 lines 3.5 KB Raw Download Zip
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