Skip to content

Commit

Permalink
Merge branch 'trunk' into fix/nested-html-translation-string-conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Oct 23, 2024
2 parents e3497d3 + 33c5ff9 commit d6e9041
Show file tree
Hide file tree
Showing 30 changed files with 1,616 additions and 1,289 deletions.
64 changes: 34 additions & 30 deletions assets/boilerplate/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
<!-- wp:template-part {"slug":"header","tagName":"header"} /-->

<!-- wp:query {"tagName":"main","layout":{"inherit":true}} -->
<main class="wp-block-query">
<!-- wp:post-template -->
<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:post-title {"isLink":true} /-->
<!-- wp:post-featured-image {"isLink":true} /-->
<!-- wp:post-content /-->
<!-- wp:group {"layout":{"type":"flex"}, "style":{"typography":{"fontSize":"14px"}}} -->
<div class="wp-block-group" style="font-size:14px">
<!-- wp:post-author {"showAvatar":false,"showBio":false} /-->
<!-- wp:post-date {"isLink":true} /-->
<!-- wp:post-terms {"term":"category"} /-->
<!-- wp:post-terms {"term": "post_tag"} /-->
<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group">
<!-- wp:query {"layout":{"inherit":true}} -->
<div class="wp-block-query">
<!-- wp:post-template -->
<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:post-title {"isLink":true} /-->
<!-- wp:post-featured-image {"isLink":true} /-->
<!-- wp:post-content /-->
<!-- wp:group {"layout":{"type":"flex"}, "style":{"typography":{"fontSize":"14px"}}} -->
<div class="wp-block-group" style="font-size:14px">
<!-- wp:post-author {"showAvatar":false,"showBio":false} /-->
<!-- wp:post-date {"isLink":true} /-->
<!-- wp:post-terms {"term":"category"} /-->
<!-- wp:post-terms {"term": "post_tag"} /-->
</div>
<!-- /wp:group -->
<!-- wp:spacer {"height":40} -->
<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
</div>
<!-- /wp:group -->
<!-- wp:spacer {"height":40} -->
<div style="height:40px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->
</div>
<!-- /wp:group -->
<!-- /wp:post-template -->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:query-pagination -->
<!-- wp:query-pagination-previous /-->
<!-- /wp:post-template -->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group">
<!-- wp:query-pagination -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers /-->
<!-- wp:query-pagination-numbers /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->
<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->
</div>
<!-- /wp:group -->
</div>
<!-- /wp:group -->
</main>
<!-- /wp:query -->
<!-- /wp:query -->
</main>
<!-- /wp:group -->

<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->
2 changes: 1 addition & 1 deletion create-block-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plugin Name: Create Block Theme
* Plugin URI: https://wordpress.org/plugins/create-block-theme
* Description: Generates a block theme
* Version: 2.4.0
* Version: 2.5.0
* Author: WordPress.org
* Author URI: https://wordpress.org/
* License: GNU General Public License v2 or later
Expand Down
18 changes: 9 additions & 9 deletions includes/class-create-block-theme-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,15 @@ function rest_create_child_theme( $request ) {
}

function rest_create_variation( $request ) {
$options = $request->get_params();

$save_fonts = isset( $options['saveFonts'] ) && true === $options['saveFonts'];

$response = CBT_Theme_JSON::add_theme_json_variation_to_local( 'variation', $this->sanitize_theme_data( $request->get_params() ) );
$response = CBT_Theme_JSON::add_theme_json_variation_to_local(
'variation',
$this->sanitize_theme_data( $options ),
$save_fonts
);

wp_cache_flush();

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

// Relocate the theme to a new folder
$response = CBT_Theme_Utils::relocate_theme( $theme['subfolder'] );

if ( is_wp_error( $response ) ) {
return $response;
}

wp_cache_flush();

return new WP_REST_Response(
Expand Down Expand Up @@ -463,9 +463,9 @@ private function sanitize_theme_data( $theme ) {
$sanitized_theme['author'] = sanitize_text_field( $theme['author'] ?? '' );
$sanitized_theme['author_uri'] = sanitize_text_field( $theme['author_uri'] ?? '' );
$sanitized_theme['tags_custom'] = sanitize_text_field( $theme['tags_custom'] ?? '' );
$sanitized_theme['subfolder'] = sanitize_text_field( $theme['subfolder'] ?? '' );
$sanitized_theme['version'] = sanitize_text_field( $theme['version'] ?? '' );
$sanitized_theme['screenshot'] = sanitize_text_field( $theme['screenshot'] ?? '' );
$sanitized_theme['requires_wp'] = sanitize_text_field( $theme['requires_wp'] ?? '' );
$sanitized_theme['recommended_plugins'] = sanitize_textarea_field( $theme['recommended_plugins'] ?? '' );
$sanitized_theme['font_credits'] = sanitize_textarea_field( $theme['font_credits'] ?? '' );
$sanitized_theme['image_credits'] = sanitize_textarea_field( $theme['image_credits'] ?? '' );
Expand Down
7 changes: 7 additions & 0 deletions includes/class-create-block-theme-editor-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ function create_block_theme_sidebar_enqueue() {
'create-block-theme-slot-fill',
);

global $wp_version;
wp_add_inline_script(
'create-block-theme-slot-fill',
'const WP_VERSION = "' . $wp_version . '";',
'before'
);

// Enable localization in the plugin sidebar.
wp_set_script_translations( 'create-block-theme-slot-fill', 'create-block-theme' );
}
Expand Down
1 change: 0 additions & 1 deletion includes/create-theme/cbt-zip-archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// This Class extends the ZipArchive class to add the theme slug as a base folder for all the files
class CBT_Zip_Archive extends ZipArchive {

private string $theme_slug;
private string $theme_folder;

function __construct( $theme_slug ) {
Expand Down
35 changes: 28 additions & 7 deletions includes/create-theme/resolver_additions.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
}

// Merge the User Data
if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
$theme->merge( WP_Theme_JSON_Resolver_Gutenberg::get_user_data() );
} else {
$theme->merge( static::get_user_data() );
}
$theme->merge( static::get_user_data() );

// Merge the extra theme data received as a parameter
if ( ! empty( $extra_theme_data ) ) {
Expand All @@ -93,8 +89,33 @@ public static function export_theme_data( $content, $extra_theme_data = null ) {
$schema = 'https://schemas.wp.org/' . $theme_json_version . '/theme.json';
}
$data['$schema'] = $schema;
$theme_json = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $theme_json );
return static::stringify( $data );
}

/**
* Get the user data.
*
* This is a copy of the parent function with the addition of the Gutenberg resolver.
*
* @return array
*/
public static function get_user_data() {
// Determine the correct method to retrieve user data
return class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' )
? WP_Theme_JSON_Resolver_Gutenberg::get_user_data()
: parent::get_user_data();
}

/**
* Stringify the array data.
*
* $data is an array of data to be converted to a JSON string.
* @return string JSON string.
*/
public static function stringify( $data ) {
$data = wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
// Convert spaces to tabs
return preg_replace( '~(?:^|\G)\h{4}~m', "\t", $data );
}

public static function get_theme_file_contents() {
Expand Down
28 changes: 4 additions & 24 deletions includes/create-theme/theme-create.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public static function clone_current_theme( $theme ) {
// Create theme directory.
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];

if ( $theme['subfolder'] ) {
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
}

if ( file_exists( $new_theme_path ) ) {
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
}
Expand All @@ -40,22 +36,14 @@ public static function clone_current_theme( $theme ) {
file_put_contents( path_join( $new_theme_path, 'readme.txt' ), CBT_Theme_Readme::create( $theme ) );
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 ) );

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
switch_theme( $theme['slug'] );
}

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

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

if ( $theme['subfolder'] ) {
$new_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
}

if ( file_exists( $new_theme_path ) ) {
return new WP_Error( 'theme_already_exists', __( 'Theme already exists.', 'create-block-theme' ) );
}
Expand Down Expand Up @@ -91,18 +79,14 @@ public static function create_child_theme( $theme, $screenshot ) {
copy( $source, $new_theme_path . DIRECTORY_SEPARATOR . 'screenshot.png' );
}

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
switch_theme( $theme['slug'] );
}

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

// Create theme directory.
$source = plugin_dir_path( __DIR__ ) . '../assets/boilerplate';
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['subfolder'] . DIRECTORY_SEPARATOR . $theme['slug'];
$blank_theme_path = get_theme_root() . DIRECTORY_SEPARATOR . $theme['slug'];

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

if ( $theme['subfolder'] ) {
switch_theme( $theme['subfolder'] . '/' . $theme['slug'] );
} else {
switch_theme( $theme['slug'] );
}
switch_theme( $theme['slug'] );
}

private static function is_valid_screenshot( $file ) {
Expand Down
36 changes: 26 additions & 10 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,18 @@ public static function make_filename_from_fontface( $font_face, $src, $src_index
return $font_filename;
}

public static function copy_activated_fonts_to_theme() {
$font_families_to_copy = self::get_user_activated_fonts();

if ( is_null( $font_families_to_copy ) ) {
return;
}

$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
/*
* Copy the font assets to the theme.
*
* @param array $font_families The font families to copy.
* @return array $font_families The font families with the font face src updated to the theme font asset location.
*/
public static function copy_font_assets_to_theme( $font_families ) {
$theme_font_asset_location = path_join( get_stylesheet_directory(), 'assets/fonts/' );
// Create the font asset directory if it does not exist.
wp_mkdir_p( $theme_font_asset_location );

foreach ( $font_families_to_copy as &$font_family ) {
foreach ( $font_families as &$font_family ) {
if ( ! isset( $font_family['fontFace'] ) ) {
continue;
}
Expand All @@ -139,6 +138,10 @@ public static function copy_activated_fonts_to_theme() {
// if it is a string, cast it to an array
$font_face['src'] = (array) $font_face['src'];
foreach ( $font_face['src'] as $font_src_index => &$font_src ) {
if ( str_starts_with( $font_src, 'file:' ) ) {
// If the font source starts with 'file:' then it's already a theme asset.
continue;
}
$font_filename = basename( $font_src );
$font_pretty_filename = self::make_filename_from_fontface( $font_face, $font_src, $font_src_index );
$font_face_path = path_join( $font_family_dir_path, $font_pretty_filename );
Expand All @@ -158,9 +161,22 @@ public static function copy_activated_fonts_to_theme() {
}
}

return $font_families;
}

public static function copy_activated_fonts_to_theme() {
$font_families_to_copy = self::get_user_activated_fonts();

if ( is_null( $font_families_to_copy ) ) {
return;
}

$theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents();
$copied_font_families = self::copy_font_assets_to_theme( $font_families_to_copy );

$theme_json['settings']['typography']['fontFamilies'] = array_merge(
$theme_json['settings']['typography']['fontFamilies'] ?? array(),
$font_families_to_copy
$copied_font_families
);

$user_settings = CBT_Theme_JSON_Resolver::get_user_data()->get_settings();
Expand Down
30 changes: 20 additions & 10 deletions includes/create-theme/theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static function add_theme_json_to_local( $export_type ) {
);
}

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

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

$_POST['theme']['variation_slug'] = $theme['slug'];

$extra_theme_data = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'title' => $theme['name'],
);

$variation_theme_json = CBT_Theme_JSON_Resolver::export_theme_data( $export_type, $extra_theme_data );
$theme_json = class_exists( 'WP_Theme_JSON_Gutenberg' ) ? new WP_Theme_JSON_Gutenberg() : new WP_Theme_JSON();
$user_data = CBT_Theme_JSON_Resolver::get_user_data();
$theme_json->merge( $user_data );
$variation = $theme_json->get_data();
$variation['title'] = $theme['name'];

if (
$save_fonts &&
isset( $variation['settings']['typography']['fontFamilies'] )
) {
$font_families = $variation['settings']['typography']['fontFamilies'];
// Copy the font assets to the theme assets folder.
$copied_font_families = CBT_Theme_Fonts::copy_font_assets_to_theme( $font_families );
// Update the the variation theme json with the font families with the new paths.
$variation['settings']['typography']['fontFamilies'] = $copied_font_families;
}

file_put_contents(
$variation_path . $theme['slug'] . '.json',
$variation_theme_json
CBT_Theme_JSON_Resolver::stringify( $variation )
);

return $variation;
}
}
5 changes: 4 additions & 1 deletion includes/create-theme/theme-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public static function get_media_absolute_urls_from_template( $template ) {
public static function make_relative_media_url( $absolute_url ) {
if ( ! empty( $absolute_url ) && CBT_Theme_Utils::is_absolute_url( $absolute_url ) ) {
$folder_path = self::get_media_folder_path_from_url( $absolute_url );
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
if ( is_child_theme() ) {
return '<?php echo esc_url( get_stylesheet_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
}
return '<?php echo esc_url( get_template_directory_uri() ); ?>' . $folder_path . basename( $absolute_url );
}
return $absolute_url;
}
Expand Down
1 change: 0 additions & 1 deletion includes/create-theme/theme-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public static function pattern_from_template( $template, $new_slug = null ) {
/**
* Title: {$template->slug}
* Slug: {$pattern_slug}
* Categories: hidden
* Inserter: no
*/
?>
Expand Down
Loading

0 comments on commit d6e9041

Please sign in to comment.