Skip to content

Commit d6e9041

Browse files
Merge branch 'trunk' into fix/nested-html-translation-string-conversion
2 parents e3497d3 + 33c5ff9 commit d6e9041

30 files changed

+1616
-1289
lines changed
Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
<!-- wp:template-part {"slug":"header","tagName":"header"} /-->
22

3-
<!-- wp:query {"tagName":"main","layout":{"inherit":true}} -->
4-
<main class="wp-block-query">
5-
<!-- wp:post-template -->
6-
<!-- wp:group -->
7-
<div class="wp-block-group">
8-
<!-- wp:post-title {"isLink":true} /-->
9-
<!-- wp:post-featured-image {"isLink":true} /-->
10-
<!-- wp:post-content /-->
11-
<!-- wp:group {"layout":{"type":"flex"}, "style":{"typography":{"fontSize":"14px"}}} -->
12-
<div class="wp-block-group" style="font-size:14px">
13-
<!-- wp:post-author {"showAvatar":false,"showBio":false} /-->
14-
<!-- wp:post-date {"isLink":true} /-->
15-
<!-- wp:post-terms {"term":"category"} /-->
16-
<!-- wp:post-terms {"term": "post_tag"} /-->
3+
<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
4+
<main class="wp-block-group">
5+
<!-- wp:query {"layout":{"inherit":true}} -->
6+
<div class="wp-block-query">
7+
<!-- wp:post-template -->
8+
<!-- wp:group -->
9+
<div class="wp-block-group">
10+
<!-- wp:post-title {"isLink":true} /-->
11+
<!-- wp:post-featured-image {"isLink":true} /-->
12+
<!-- wp:post-content /-->
13+
<!-- wp:group {"layout":{"type":"flex"}, "style":{"typography":{"fontSize":"14px"}}} -->
14+
<div class="wp-block-group" style="font-size:14px">
15+
<!-- wp:post-author {"showAvatar":false,"showBio":false} /-->
16+
<!-- wp:post-date {"isLink":true} /-->
17+
<!-- wp:post-terms {"term":"category"} /-->
18+
<!-- wp:post-terms {"term": "post_tag"} /-->
19+
</div>
20+
<!-- /wp:group -->
21+
<!-- wp:spacer {"height":40} -->
22+
<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>
23+
<!-- /wp:spacer -->
1724
</div>
1825
<!-- /wp:group -->
19-
<!-- wp:spacer {"height":40} -->
20-
<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>
21-
<!-- /wp:spacer -->
22-
</div>
23-
<!-- /wp:group -->
24-
<!-- /wp:post-template -->
25-
<!-- wp:group {"layout":{"inherit":true}} -->
26-
<div class="wp-block-group">
27-
<!-- wp:query-pagination -->
28-
<!-- wp:query-pagination-previous /-->
26+
<!-- /wp:post-template -->
27+
<!-- wp:group {"layout":{"inherit":true}} -->
28+
<div class="wp-block-group">
29+
<!-- wp:query-pagination -->
30+
<!-- wp:query-pagination-previous /-->
2931

30-
<!-- wp:query-pagination-numbers /-->
32+
<!-- wp:query-pagination-numbers /-->
3133

32-
<!-- wp:query-pagination-next /-->
33-
<!-- /wp:query-pagination -->
34+
<!-- wp:query-pagination-next /-->
35+
<!-- /wp:query-pagination -->
36+
</div>
37+
<!-- /wp:group -->
3438
</div>
35-
<!-- /wp:group -->
36-
</main>
37-
<!-- /wp:query -->
39+
<!-- /wp:query -->
40+
</main>
41+
<!-- /wp:group -->
3842

3943
<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

create-block-theme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Plugin Name: Create Block Theme
66
* Plugin URI: https://wordpress.org/plugins/create-block-theme
77
* Description: Generates a block theme
8-
* Version: 2.4.0
8+
* Version: 2.5.0
99
* Author: WordPress.org
1010
* Author URI: https://wordpress.org/
1111
* License: GNU General Public License v2 or later

includes/class-create-block-theme-api.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,15 @@ function rest_create_child_theme( $request ) {
239239
}
240240

241241
function rest_create_variation( $request ) {
242+
$options = $request->get_params();
243+
244+
$save_fonts = isset( $options['saveFonts'] ) && true === $options['saveFonts'];
242245

243-
$response = CBT_Theme_JSON::add_theme_json_variation_to_local( 'variation', $this->sanitize_theme_data( $request->get_params() ) );
246+
$response = CBT_Theme_JSON::add_theme_json_variation_to_local(
247+
'variation',
248+
$this->sanitize_theme_data( $options ),
249+
$save_fonts
250+
);
244251

245252
wp_cache_flush();
246253

@@ -343,13 +350,6 @@ function rest_update_theme( $request ) {
343350
CBT_Theme_Utils::replace_screenshot( $theme['screenshot'] );
344351
}
345352

346-
// Relocate the theme to a new folder
347-
$response = CBT_Theme_Utils::relocate_theme( $theme['subfolder'] );
348-
349-
if ( is_wp_error( $response ) ) {
350-
return $response;
351-
}
352-
353353
wp_cache_flush();
354354

355355
return new WP_REST_Response(
@@ -463,9 +463,9 @@ private function sanitize_theme_data( $theme ) {
463463
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] ?? '' );
464464
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ?? '' );
465465
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] ?? '' );
466-
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ?? '' );
467466
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
468467
$sanitized_theme['screenshot'] = sanitize_text_field( $theme['screenshot'] ?? '' );
468+
$sanitized_theme['requires_wp'] = sanitize_text_field( $theme['requires_wp'] ?? '' );
469469
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
470470
$sanitized_theme['font_credits'] = sanitize_textarea_field( $theme['font_credits'] ?? '' );
471471
$sanitized_theme['image_credits'] = sanitize_textarea_field( $theme['image_credits'] ?? '' );

includes/class-create-block-theme-editor-tools.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ function create_block_theme_sidebar_enqueue() {
3838
'create-block-theme-slot-fill',
3939
);
4040

41+
global $wp_version;
42+
wp_add_inline_script(
43+
'create-block-theme-slot-fill',
44+
'const WP_VERSION = "' . $wp_version . '";',
45+
'before'
46+
);
47+
4148
// Enable localization in the plugin sidebar.
4249
wp_set_script_translations( 'create-block-theme-slot-fill', 'create-block-theme' );
4350
}

includes/create-theme/cbt-zip-archive.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// This Class extends the ZipArchive class to add the theme slug as a base folder for all the files
66
class CBT_Zip_Archive extends ZipArchive {
77

8-
private string $theme_slug;
98
private string $theme_folder;
109

1110
function __construct( $theme_slug ) {

includes/create-theme/resolver_additions.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
6565
}
6666

6767
// Merge the User Data
68-
if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
69-
$theme->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
70-
} else {
71-
$theme->merge( static::get_user_data() );
72-
}
68+
$theme->merge( static::get_user_data() );
7369

7470
// Merge the extra theme data received as a parameter
7571
if ( ! empty( $extra_theme_data ) ) {
@@ -93,8 +89,33 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
9389
$schema = 'https://schemas.wp.org/' . $theme_json_version . '/theme.json';
9490
}
9591
$data['$schema'] = $schema;
96-
$theme_json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
97-
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json );
92+
return static::stringify( $data );
93+
}
94+
95+
/**
96+
* Get the user data.
97+
*
98+
* This is a copy of the parent function with the addition of the Gutenberg resolver.
99+
*
100+
* @return array
101+
*/
102+
public static function get_user_data() {
103+
// Determine the correct method to retrieve user data
104+
return class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' )
105+
? WP_Theme_JSON_Resolver_Gutenberg::get_user_data()
106+
: parent::get_user_data();
107+
}
108+
109+
/**
110+
* Stringify the array data.
111+
*
112+
* $data is an array of data to be converted to a JSON string.
113+
* @return string JSON string.
114+
*/
115+
public static function stringify( $data ) {
116+
$data = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
117+
// Convert spaces to tabs
118+
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $data );
98119
}
99120

100121
public static function get_theme_file_contents() {

includes/create-theme/theme-create.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ public static function clone_current_theme( $theme ) {
1515
// Create theme directory.
1616
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];
1717

18-
if ( $theme['subfolder'] ) {
19-
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
20-
}
21-
2218
if ( file_exists( $new_theme_path ) ) {
2319
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
2420
}
@@ -40,22 +36,14 @@ public static function clone_current_theme( $theme ) {
4036
file_put_contents( path_join( $new_theme_path, 'readme.txt' ), CBT_Theme_Readme::create( $theme ) );
4137
file_put_contents( path_join( $new_theme_path, 'style.css' ), CBT_Theme_Styles::update_style_css( file_get_contents( path_join( $new_theme_path, 'style.css' ) ), $theme ) );
4238

43-
if ( $theme['subfolder'] ) {
44-
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
45-
} else {
46-
switch_theme( $theme['slug'] );
47-
}
39+
switch_theme( $theme['slug'] );
4840
}
4941

5042
public static function create_child_theme( $theme, $screenshot ) {
5143

5244
// Create theme directory.
5345
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];
5446

55-
if ( $theme['subfolder'] ) {
56-
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
57-
}
58-
5947
if ( file_exists( $new_theme_path ) ) {
6048
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
6149
}
@@ -91,18 +79,14 @@ public static function create_child_theme( $theme, $screenshot ) {
9179
copy( $source, $new_theme_path . DIRECTORY_SEPARATOR . 'screenshot.png' );
9280
}
9381

94-
if ( $theme['subfolder'] ) {
95-
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
96-
} else {
97-
switch_theme( $theme['slug'] );
98-
}
82+
switch_theme( $theme['slug'] );
9983
}
10084

10185
public static function create_blank_theme( $theme, $screenshot ) {
10286

10387
// Create theme directory.
10488
$source = plugin_dir_path( __DIR__ ) . '../assets/boilerplate';
105-
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
89+
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];
10690

10791
if ( file_exists( $blank_theme_path ) ) {
10892
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
@@ -158,11 +142,7 @@ public static function create_blank_theme( $theme, $screenshot ) {
158142
file_put_contents( $theme_json_path, $theme_json_string );
159143
}
160144

161-
if ( $theme['subfolder'] ) {
162-
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
163-
} else {
164-
switch_theme( $theme['slug'] );
165-
}
145+
switch_theme( $theme['slug'] );
166146
}
167147

168148
private static function is_valid_screenshot( $file ) {

includes/create-theme/theme-fonts.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,18 @@ public static function make_filename_from_fontface( $font_face, $src, $src_index
112112
return $font_filename;
113113
}
114114

115-
public static function copy_activated_fonts_to_theme() {
116-
$font_families_to_copy = self::get_user_activated_fonts();
117-
118-
if ( is_null( $font_families_to_copy ) ) {
119-
return;
120-
}
121-
122-
$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
115+
/*
116+
* Copy the font assets to the theme.
117+
*
118+
* @param array $font_families The font families to copy.
119+
* @return array $font_families The font families with the font face src updated to the theme font asset location.
120+
*/
121+
public static function copy_font_assets_to_theme( $font_families ) {
123122
$theme_font_asset_location = path_join( get_stylesheet_directory(), 'assets/fonts/' );
124123
// Create the font asset directory if it does not exist.
125124
wp_mkdir_p( $theme_font_asset_location );
126125

127-
foreach ( $font_families_to_copy as &$font_family ) {
126+
foreach ( $font_families as &$font_family ) {
128127
if ( ! isset( $font_family['fontFace'] ) ) {
129128
continue;
130129
}
@@ -139,6 +138,10 @@ public static function copy_activated_fonts_to_theme() {
139138
// if it is a string, cast it to an array
140139
$font_face['src'] = (array) $font_face['src'];
141140
foreach ( $font_face['src'] as $font_src_index => &$font_src ) {
141+
if ( str_starts_with( $font_src, 'file:' ) ) {
142+
// If the font source starts with 'file:' then it's already a theme asset.
143+
continue;
144+
}
142145
$font_filename = basename( $font_src );
143146
$font_pretty_filename = self::make_filename_from_fontface( $font_face, $font_src, $font_src_index );
144147
$font_face_path = path_join( $font_family_dir_path, $font_pretty_filename );
@@ -158,9 +161,22 @@ public static function copy_activated_fonts_to_theme() {
158161
}
159162
}
160163

164+
return $font_families;
165+
}
166+
167+
public static function copy_activated_fonts_to_theme() {
168+
$font_families_to_copy = self::get_user_activated_fonts();
169+
170+
if ( is_null( $font_families_to_copy ) ) {
171+
return;
172+
}
173+
174+
$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
175+
$copied_font_families = self::copy_font_assets_to_theme( $font_families_to_copy );
176+
161177
$theme_json['settings']['typography']['fontFamilies'] = array_merge(
162178
$theme_json['settings']['typography']['fontFamilies'] ?? array(),
163-
$font_families_to_copy
179+
$copied_font_families
164180
);
165181

166182
$user_settings = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();

includes/create-theme/theme-json.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static function add_theme_json_to_local( $export_type ) {
99
);
1010
}
1111

12-
public static function add_theme_json_variation_to_local( $export_type, $theme ) {
12+
public static function add_theme_json_variation_to_local( $export_type, $theme, $save_fonts = false ) {
1313
$variation_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR;
1414

1515
if ( ! file_exists( $variation_path ) ) {
@@ -20,18 +20,28 @@ public static function add_theme_json_variation_to_local( $export_type, $theme )
2020
return new WP_Error( 'variation_already_exists', __( 'Variation already exists.', 'create-block-theme' ) );
2121
}
2222

23-
$_POST['theme']['variation_slug'] = $theme['slug'];
24-
25-
$extra_theme_data = array(
26-
'version' => WP_Theme_JSON::LATEST_SCHEMA,
27-
'title' => $theme['name'],
28-
);
29-
30-
$variation_theme_json = CBT_Theme_JSON_Resolver::export_theme_data( $export_type, $extra_theme_data );
23+
$theme_json = class_exists( 'WP_Theme_JSON_Gutenberg' ) ? new WP_Theme_JSON_Gutenberg() : new WP_Theme_JSON();
24+
$user_data = CBT_Theme_JSON_Resolver::get_user_data();
25+
$theme_json->merge( $user_data );
26+
$variation = $theme_json->get_data();
27+
$variation['title'] = $theme['name'];
28+
29+
if (
30+
$save_fonts &&
31+
isset( $variation['settings']['typography']['fontFamilies'] )
32+
) {
33+
$font_families = $variation['settings']['typography']['fontFamilies'];
34+
// Copy the font assets to the theme assets folder.
35+
$copied_font_families = CBT_Theme_Fonts::copy_font_assets_to_theme( $font_families );
36+
// Update the the variation theme json with the font families with the new paths.
37+
$variation['settings']['typography']['fontFamilies'] = $copied_font_families;
38+
}
3139

3240
file_put_contents(
3341
$variation_path . $theme['slug'] . '.json',
34-
$variation_theme_json
42+
CBT_Theme_JSON_Resolver::stringify( $variation )
3543
);
44+
45+
return $variation;
3646
}
3747
}

includes/create-theme/theme-media.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ public static function get_media_absolute_urls_from_template( $template ) {
9494
public static function make_relative_media_url( $absolute_url ) {
9595
if ( ! empty( $absolute_url ) && CBT_Theme_Utils::is_absolute_url( $absolute_url ) ) {
9696
$folder_path = self::get_media_folder_path_from_url( $absolute_url );
97-
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
97+
if ( is_child_theme() ) {
98+
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
99+
}
100+
return '<?php echo esc_url( get_template_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
98101
}
99102
return $absolute_url;
100103
}

includes/create-theme/theme-patterns.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public static function pattern_from_template( $template, $new_slug = null ) {
99
/**
1010
* Title: {$template->slug}
1111
* Slug: {$pattern_slug}
12-
* Categories: hidden
1312
* Inserter: no
1413
*/
1514
?>

0 commit comments

Comments
 (0)