Skip to content

Commit

Permalink
Check if theme fonts present before removing (#660)
Browse files Browse the repository at this point in the history
* Check if theme fonts present before removing

Fixes: #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
  • Loading branch information
vcanales authored Jun 4, 2024
1 parent e862dd6 commit d6f7432
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions includes/create-theme/theme-fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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'] );
Expand All @@ -226,5 +256,4 @@ function( $theme_font_family ) use ( $font_families_to_not_remove ) {

CBT_Theme_JSON_Resolver::write_user_settings( $user_settings );
}

}

0 comments on commit d6f7432

Please sign in to comment.