diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index 1593ebe0..093de263 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -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(); diff --git a/includes/create-theme/resolver_additions.php b/includes/create-theme/resolver_additions.php index 1a6ad48f..0c684739 100644 --- a/includes/create-theme/resolver_additions.php +++ b/includes/create-theme/resolver_additions.php @@ -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 ) ) { @@ -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() { diff --git a/includes/create-theme/theme-fonts.php b/includes/create-theme/theme-fonts.php index d29a68a6..1802ea43 100644 --- a/includes/create-theme/theme-fonts.php +++ b/includes/create-theme/theme-fonts.php @@ -84,22 +84,22 @@ public static function get_user_activated_fonts() { return $user_settings['typography']['fontFamilies']['custom'] ?? null; } - 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 = get_stylesheet_directory() . '/assets/fonts/'; require_once ABSPATH . 'wp-admin/includes/file.php'; + if ( ! file_exists( $theme_font_asset_location ) ) { mkdir( $theme_font_asset_location, 0777, true ); } - foreach ( $font_families_to_copy as &$font_family ) { + foreach ( $font_families as &$font_family ) { if ( ! isset( $font_family['fontFace'] ) ) { continue; } @@ -124,9 +124,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(); diff --git a/includes/create-theme/theme-json.php b/includes/create-theme/theme-json.php index e8059de7..dfedb885 100644 --- a/includes/create-theme/theme-json.php +++ b/includes/create-theme/theme-json.php @@ -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 ) ) { @@ -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; } }