PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.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 / compat / wordpress-6.0 / block-template-utils.php

block-template-utils.php in Gutenberg 14.7.0, at lib/compat/wordpress-6.0/block-template-utils.php

133 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Temporary compatibility shims for features present in Gutenberg.
4 * This file should be removed when WordPress 6.0.0 becomes the lowest
5 * supported version by this plugin.
6 *
7 * @package gutenberg
8 */
9
10 /**
11 * Filters theme files and folders that should be ignored during export.
12 *
13 * @since 6.0.0
14 *
15 * @param string $path The path of the file in the theme.
16 * @return Bool Whether this file or directory is ignored.
17 */
18 function gutenberg_is_theme_directory_ignored( $path ) {
19 $ignore_list = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
20 foreach ( $ignore_list as $ignored ) {
21 if ( str_starts_with( $path, $ignored ) ) {
22 return true;
23 }
24 }
25
26 return false;
27 }
28
29 /**
30 * Creates an export of the current templates and
31 * template parts from the site editor at the
32 * specified path in a ZIP file.
33 *
34 * @since 5.9.0
35 * @since 6.0.0 Adds theme.json to the export archive.
36 *
37 * @return WP_Error|string Path of the ZIP file or error on failure.
38 */
39 function gutenberg_generate_block_templates_export_file() {
40 if ( ! class_exists( 'ZipArchive' ) ) {
41 return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.', 'gutenberg' ) );
42 }
43
44 $obscura = wp_generate_password( 12, false, false );
45 $theme_name = wp_get_theme()->get( 'TextDomain' );
46 $filename = get_temp_dir() . $theme_name . $obscura . '.zip';
47
48 $zip = new ZipArchive();
49 if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
50 return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.', 'gutenberg' ) );
51 }
52
53 $zip->addEmptyDir( 'templates' );
54 $zip->addEmptyDir( 'parts' );
55
56 // Get path of the theme.
57 $theme_path = wp_normalize_path( get_stylesheet_directory() );
58
59 // Create recursive directory iterator.
60 $theme_files = new RecursiveIteratorIterator(
61 new RecursiveDirectoryIterator( $theme_path ),
62 RecursiveIteratorIterator::LEAVES_ONLY
63 );
64
65 // Make a copy of the current theme.
66 foreach ( $theme_files as $file ) {
67 // Skip directories as they are added automatically.
68 if ( ! $file->isDir() ) {
69 // Get real and relative path for current file.
70 $file_path = wp_normalize_path( $file );
71 $relative_path = substr( $file_path, strlen( $theme_path ) + 1 );
72
73 if ( ! gutenberg_is_theme_directory_ignored( $relative_path ) ) {
74 $zip->addFile( $file_path, $relative_path );
75 }
76 }
77 }
78
79 // Load templates into the zip file.
80 $templates = gutenberg_get_block_templates();
81 foreach ( $templates as $template ) {
82 $template->content = _remove_theme_attribute_in_block_template_content( $template->content );
83
84 $zip->addFromString(
85 'templates/' . $template->slug . '.html',
86 $template->content
87 );
88 }
89
90 // Load template parts into the zip file.
91 $template_parts = gutenberg_get_block_templates( array(), 'wp_template_part' );
92 foreach ( $template_parts as $template_part ) {
93 $zip->addFromString(
94 'parts/' . $template_part->slug . '.html',
95 $template_part->content
96 );
97 }
98
99 // Load theme.json into the zip file.
100 $tree = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data( array(), array( 'with_supports' => false ) );
101 // Merge with user data.
102 $tree->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
103
104 $theme_json_raw = $tree->get_data();
105 // If a version is defined, add a schema.
106 if ( $theme_json_raw['version'] ) {
107 global $wp_version;
108 $theme_json_version = 'wp/' . substr( $wp_version, 0, 3 );
109 if ( defined( 'IS_GUTENBERG_PLUGIN' ) ) {
110 $theme_json_version = 'trunk';
111 }
112 $schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' );
113 $theme_json_raw = array_merge( $schema, $theme_json_raw );
114 }
115
116 // Convert to a string.
117 $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
118
119 // Replace 4 spaces with a tab.
120 $theme_json_tabbed = preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json_encoded );
121
122 // Add the theme.json file to the zip.
123 $zip->addFromString(
124 'theme.json',
125 $theme_json_tabbed
126 );
127
128 // Save changes to the zip file.
129 $zip->close();
130
131 return $filename;
132 }
133