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 ); } - }