Skip to content

Commit

Permalink
Check if theme fonts present before removing
Browse files Browse the repository at this point in the history
Fixes: #659
  • Loading branch information
vcanales committed May 31, 2024
1 parent eabd382 commit 07ae606
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,26 @@ 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'] ) ) {
private 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
$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 ) {
Expand All @@ -203,16 +201,39 @@ 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.
*/
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;
$did_remove_assets = static::remove_deactivated_font_assets( $font_families_to_not_remove, $theme_font_families );

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
Expand Down

0 comments on commit 07ae606

Please sign in to comment.