From 0325db4c30d80bc17e353f38ec34dfb927625f7e Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 16 Apr 2024 12:43:37 -0400 Subject: [PATCH 1/2] Include activated Fonts on theme zip export functions --- admin/class-create-theme.php | 10 ++--- admin/create-theme/theme-fonts.php | 17 ++++--- admin/create-theme/theme-zip.php | 55 ++++++++++++++++++++++- includes/class-create-block-theme-api.php | 29 ++++++++---- 4 files changed, 90 insertions(+), 21 deletions(-) diff --git a/admin/class-create-theme.php b/admin/class-create-theme.php index 0b98697a..7462f637 100644 --- a/admin/class-create-theme.php +++ b/admin/class-create-theme.php @@ -109,7 +109,7 @@ function export_child_theme( $theme ) { $zip = Theme_Zip::copy_theme_to_zip( $zip, null, null ); $zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'current' ) ); $zip->close(); @@ -144,7 +144,7 @@ function create_sibling_theme( $theme, $screenshot ) { $zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] ); $zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'current' ) ); // Add readme.txt. $zip->addFromStringToTheme( @@ -213,7 +213,7 @@ function clone_theme( $theme, $screenshot ) { $zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] ); $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'all' ) ); // Add readme.txt. $zip->addFromStringToTheme( @@ -277,7 +277,7 @@ function create_child_theme( $theme, $screenshot ) { $zip = Theme_Zip::create_zip( $filename ); $zip = Theme_Zip::add_templates_to_zip( $zip, 'user', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'user' ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'user' ) ); // Add readme.txt. $zip->addFromStringToTheme( @@ -321,7 +321,7 @@ function export_theme( $theme ) { $zip = Theme_Zip::copy_theme_to_zip( $zip, null, null ); $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, MY_Theme_JSON_Resolver::export_theme_data( 'all' ) ); $zip->close(); diff --git a/admin/create-theme/theme-fonts.php b/admin/create-theme/theme-fonts.php index 2f46059f..c2d6ab4b 100644 --- a/admin/create-theme/theme-fonts.php +++ b/admin/create-theme/theme-fonts.php @@ -12,17 +12,24 @@ public static function persist_font_settings() { static::copy_activated_fonts_to_theme(); } - public static function copy_activated_fonts_to_theme() { - + public static function get_user_activated_fonts() { $user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); - $theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); + if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) { + return null; + } + + return $user_settings['typography']['fontFamilies']['custom']; + } + + public static function copy_activated_fonts_to_theme() { + $font_families_to_copy = static::get_user_activated_fonts(); // If there are no custom fonts, bounce out - if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) { + if ( is_null( $font_families_to_copy ) ) { return; } - $font_families_to_copy = $user_settings['typography']['fontFamilies']['custom']; + $theme_json = MY_Theme_JSON_Resolver::get_theme_file_contents(); // copy font face assets to theme and change the src to the new location require_once ABSPATH . 'wp-admin/includes/file.php'; diff --git a/admin/create-theme/theme-zip.php b/admin/create-theme/theme-zip.php index 34087371..8742433d 100644 --- a/admin/create-theme/theme-zip.php +++ b/admin/create-theme/theme-zip.php @@ -21,14 +21,65 @@ public static function create_zip( $filename, $theme_slug = null ) { return $zip; } - public static function add_theme_json_to_zip( $zip, $export_type ) { + public static function add_theme_json_to_zip( $zip, $theme_json ) { $zip->addFromStringToTheme( 'theme.json', - MY_Theme_JSON_Resolver::export_theme_data( $export_type ) + $theme_json ); return $zip; } + public static function add_activated_fonts_to_zip( $zip, $theme_json_string ) { + + $theme_json = json_decode( $theme_json_string, true ); + + $font_families_to_copy = Theme_Fonts::get_user_activated_fonts(); + $theme_font_asset_location = '/assets/fonts/'; + $font_slugs_to_remove = array(); + + foreach ( $font_families_to_copy as &$font_family ) { + if ( ! isset( $font_family['fontFace'] ) ) { + continue; + } + $font_slugs_to_remove[] = $font_family['slug']; + foreach ( $font_family['fontFace'] as &$font_face ) { + $font_filename = basename( $font_face['src'] ); + $font_dir = wp_get_font_dir(); + if ( str_contains( $font_face['src'], $font_dir['url'] ) ) { + $zip->addFileToTheme( $font_dir['path'] . '/' . $font_filename, $theme_font_asset_location . '/' . $font_filename ); + } else { + // otherwise download it from wherever it is hosted + $tmp_file = download_url( $font_face['src'] ); + $zip->addFileToTheme( $tmp_file, $theme_font_asset_location . '/' . $font_filename ); + unlink( $tmp_file ); + } + + $font_face['src'] = 'file:./assets/fonts/' . $font_filename; + } + } + + if ( ! isset( $theme_json['settings']['typography']['fontFamilies'] ) ) { + $theme_json['settings']['typography']['fontFamilies'] = array(); + } + + // Remove user fonts that have already been added to the theme_json + // otherwise they will be duplicated when we add them next + foreach ( $theme_json['settings']['typography']['fontFamilies'] as $key => $theme_font_family ) { + if ( in_array( $theme_font_family['slug'], $font_slugs_to_remove, true ) ) { + unset( $theme_json['settings']['typography']['fontFamilies'][ $key ] ); + } + } + + // Copy user fonts to theme + $theme_json['settings']['typography']['fontFamilies'] = array_merge( + $theme_json['settings']['typography']['fontFamilies'], + $font_families_to_copy + ); + + return wp_json_encode( $theme_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + + } + public static function copy_theme_to_zip( $zip, $new_slug, $new_name ) { // Get real path for our folder diff --git a/includes/class-create-block-theme-api.php b/includes/class-create-block-theme-api.php index 361281a5..af7f1d51 100644 --- a/includes/class-create-block-theme-api.php +++ b/includes/class-create-block-theme-api.php @@ -269,10 +269,14 @@ function rest_export_cloned_theme( $request ) { // Create ZIP file in the temporary directory. $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = Theme_Zip::create_zip( $filename, $theme['slug'] ); + $zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] ); + $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] ); - $zip = Theme_Zip::copy_theme_to_zip( $zip, $theme['slug'], $theme['name'] ); - $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' ); + //TODO: Should the font persistent be optional? + // If so then the Font Library fonts will need to be removed from the theme.json settings. + $theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'all' ); + $theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json ); // Add readme.txt. $zip->addFromStringToTheme( @@ -316,8 +320,11 @@ function rest_export_child_cloned_theme( $request ) { $filename = tempnam( get_temp_dir(), $theme['slug'] ); $zip = Theme_Zip::create_zip( $filename, $theme['slug'] ); - $zip = Theme_Zip::add_templates_to_zip( $zip, 'user', $theme['slug'] ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'variation' ); + //TODO: Should the font persistent be optional? + // If so then the Font Library fonts will need to be removed from the theme.json settings. + $theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'variation' ); + $theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json ); + $zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json ); // Add readme.txt. $zip->addFromStringToTheme( @@ -369,13 +376,17 @@ function rest_export_theme( $request ) { $zip = Theme_Zip::copy_theme_to_zip( $zip, null, null ); if ( is_child_theme() ) { - $zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme_slug ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'current' ); + $zip = Theme_Zip::add_templates_to_zip( $zip, 'current', $theme_slug ); + $theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'current' ); } else { - $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null ); - $zip = Theme_Zip::add_theme_json_to_zip( $zip, 'all' ); + $zip = Theme_Zip::add_templates_to_zip( $zip, 'all', null ); + $theme_json = MY_Theme_JSON_Resolver::export_theme_data( 'all' ); } + $theme_json = Theme_Zip::add_activated_fonts_to_zip( $zip, $theme_json ); + + $zip = Theme_Zip::add_theme_json_to_zip( $zip, $theme_json ); + $zip->close(); header( 'Content-Type: application/zip' ); From d27beb862d95ca67325027a3d67719e53b64945b Mon Sep 17 00:00:00 2001 From: Jason Crist Date: Tue, 16 Apr 2024 12:56:02 -0400 Subject: [PATCH 2/2] Fixed font saving unit test/bug --- admin/create-theme/theme-fonts.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/admin/create-theme/theme-fonts.php b/admin/create-theme/theme-fonts.php index c2d6ab4b..20fb2496 100644 --- a/admin/create-theme/theme-fonts.php +++ b/admin/create-theme/theme-fonts.php @@ -22,7 +22,12 @@ public static function get_user_activated_fonts() { } public static function copy_activated_fonts_to_theme() { - $font_families_to_copy = static::get_user_activated_fonts(); + $user_settings = MY_Theme_JSON_Resolver::get_user_data()->get_settings(); + if ( ! isset( $user_settings['typography']['fontFamilies']['custom'] ) ) { + return null; + } + + $font_families_to_copy = $user_settings['typography']['fontFamilies']['custom']; // If there are no custom fonts, bounce out if ( is_null( $font_families_to_copy ) ) {