From d6f7432f681df821d6700931c80ad9d391a3688a Mon Sep 17 00:00:00 2001 From: Vicente Canales <1157901+vcanales@users.noreply.github.com> Date: Tue, 4 Jun 2024 08:38:26 -0500 Subject: [PATCH] Check if theme fonts present before removing (#660) * Check if theme fonts present before removing Fixes: https://github.com/WordPress/create-block-theme/issues/659 * make sure remove_deactivated_font_assets is static * use correct condition for asset removal ie. bail on null * remove unused variable * add comments and reword some of the existing ones --- includes/create-theme/theme-fonts.php | 73 +++++++++++++++++++-------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/includes/create-theme/theme-fonts.php b/includes/create-theme/theme-fonts.php index 0e403b69..04922fd0 100644 --- a/includes/create-theme/theme-fonts.php +++ b/includes/create-theme/theme-fonts.php @@ -164,28 +164,32 @@ public static function copy_activated_fonts_to_theme() { } - public static function remove_deactivated_fonts_from_theme() { - - $user_settings = CBT_Theme_JSON_Resolver::get_user_data()->get_settings(); - $theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents(); - - // If there are no deactivated theme fonts, bounce out - if ( ! isset( $user_settings['typography']['fontFamilies']['theme'] ) ) { + /** + * Remove font face assets from the theme that are not in the user configuration. + * + * @param array $font_families_to_not_remove + * @param array $theme_font_families + */ + private static function remove_deactivated_font_assets( $font_families_to_not_remove, $theme_font_families ) { + /* Bail if there are no theme font families, which can happen + * if the theme.json file, missing, or if the theme is a child theme, in + * which case the font families are inherited from the parent theme. + */ + if ( null === $theme_font_families ) { return; } - $font_families_to_not_remove = $user_settings['typography']['fontFamilies']['theme']; - - // Remove font assets from theme + // Remove font face assets from the theme that are not in the user configuration. $theme_font_asset_location = get_stylesheet_directory() . '/assets/fonts/'; $font_families_to_remove = array_values( array_filter( - $theme_json['settings']['typography']['fontFamilies'], + $theme_font_families, function( $theme_font_family ) use ( $font_families_to_not_remove ) { return ! in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); } ) ); + foreach ( $font_families_to_remove as $font_family ) { if ( isset( $font_family['fontFace'] ) ) { foreach ( $font_family['fontFace'] as $font_face ) { @@ -203,19 +207,45 @@ function( $theme_font_family ) use ( $font_families_to_not_remove ) { } } } + } + + /** + * Remove any deactivated fonts from the theme configuration. + * This includes removing the font face assets from the theme, + * but does not remove the font face assets from the user configuration. + * + * This is because the user may have deactivated a font, but still want to use it in the future. + */ + public static function remove_deactivated_fonts_from_theme() { + + $user_settings = CBT_Theme_JSON_Resolver::get_user_data()->get_settings(); + $theme_json = CBT_Theme_JSON_Resolver::get_theme_file_contents(); + + // If there are no deactivated theme fonts, bounce out + if ( ! isset( $user_settings['typography']['fontFamilies']['theme'] ) ) { + return; + } + + $font_families_to_not_remove = $user_settings['typography']['fontFamilies']['theme']; + + $theme_font_families = isset( $theme_json['settings']['typography']['fontFamilies'] ) ? $theme_json['settings']['typography']['fontFamilies'] : null; + static::remove_deactivated_font_assets( $font_families_to_not_remove, $theme_font_families ); + + // If there are font families in the theme, remove the deactivated ones + if ( null !== $theme_font_families ) { + $theme_json['settings']['typography']['fontFamilies'] = array_values( + array_filter( + $theme_font_families, + function( $theme_font_family ) use ( $font_families_to_not_remove ) { + return in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); + } + ) + ); + } - // Remove user fonts from theme - $theme_json['settings']['typography']['fontFamilies'] = array_values( - array_filter( - $theme_json['settings']['typography']['fontFamilies'], - function( $theme_font_family ) use ( $font_families_to_not_remove ) { - return in_array( $theme_font_family['slug'], array_column( $font_families_to_not_remove, 'slug' ), true ); - } - ) - ); CBT_Theme_JSON_Resolver::write_theme_file_contents( $theme_json ); - // Remove user preferences for theme font activation + // Remove deactivated fonts from user settings unset( $user_settings['typography']['fontFamilies']['theme'] ); if ( empty( $user_settings['typography']['fontFamilies'] ) ) { unset( $user_settings['typography']['fontFamilies'] ); @@ -226,5 +256,4 @@ function( $theme_font_family ) use ( $font_families_to_not_remove ) { CBT_Theme_JSON_Resolver::write_user_settings( $user_settings ); } - }