-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix and improve logic in WP_Fonts_Resolver::get_settings() #56489
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,35 +191,41 @@ private static function get_settings() { | |
|
||
foreach ( $variations as $variation ) { | ||
|
||
// Skip if settings.typography.fontFamilies are not defined in the variation. | ||
if ( empty( $variation['settings']['typography']['fontFamilies'] ) ) { | ||
// Skip if settings.typography.fontFamilies.theme is not defined or is not an array. | ||
if ( | ||
! isset( $variation['settings']['typography']['fontFamilies']['theme'] ) || | ||
! is_array( $variation['settings']['typography']['fontFamilies']['theme'] ) | ||
) { | ||
continue; | ||
} | ||
|
||
// One time, set any missing parts of the array structure. | ||
if ( $set_theme_structure ) { | ||
$set_theme_structure = false; | ||
$settings = static::set_tyopgraphy_settings_array_structure( $settings ); | ||
} | ||
|
||
// Initialize the font families from variation if set and is an array, otherwise default to an empty array. | ||
$variation_font_families = ( isset( $variation['settings']['typography']['fontFamilies']['theme'] ) && is_array( $variation['settings']['typography']['fontFamilies']['theme'] ) ) | ||
? $variation['settings']['typography']['fontFamilies']['theme'] | ||
: array(); | ||
// Font families from settings must be an array. | ||
if ( | ||
! isset( $settings['typography']['fontFamilies']['theme'] ) || | ||
! is_array( $settings['typography']['fontFamilies']['theme'] ) | ||
) { | ||
$settings['typography']['fontFamilies']['theme'] = array(); | ||
} | ||
} | ||
|
||
// Merge the variation settings with the global settings. | ||
// Merge the variation settings with the global settings. Variations overwrite the globals. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I like the effort to explain what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hehe, yea may not be obvious to all. A simple inline comment may save somebody an hour or two in some cases :) |
||
$settings['typography']['fontFamilies']['theme'] = array_merge( | ||
$settings['typography']['fontFamilies']['theme'], | ||
$variation_font_families | ||
$variation['settings']['typography']['fontFamilies']['theme'] | ||
); | ||
} | ||
|
||
// Make sure there are no duplicates. | ||
$settings['typography']['fontFamilies'] = array_unique( $settings['typography']['fontFamilies'], SORT_REGULAR ); | ||
|
||
// The font families from settings might become null after running the `array_unique`. | ||
if ( ! isset( $settings['typography']['fontFamilies']['theme'] ) || ! is_array( $settings['typography']['fontFamilies']['theme'] ) ) { | ||
$settings['typography']['fontFamilies']['theme'] = array(); | ||
} | ||
// Make sure there are no duplicate values with different names (array keys). | ||
if ( ! empty( $settings['typography']['fontFamilies']['theme'] ) ) { | ||
$settings['typography']['fontFamilies']['theme'] = array_unique( | ||
$settings['typography']['fontFamilies']['theme'], | ||
SORT_REGULAR | ||
); | ||
} | ||
|
||
return $settings; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need this anymore as the
set_tyopgraphy_settings_array_structure
sets thetypography.fontFamilies.theme
structure in the given array and it works well without this check nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arthur791004 The
set_tyopgraphy_settings_array_structure()
method checks whether the value of$data['typography']['fontFamilies']['theme']
is not empty. However, it does not verify whether the value is actually an array. Therefore, the value could be not empty but not an array. So, in my opinion, this check is still necessary.Alternatively, this check could be moved to the
set_tyopgraphy_settings_array_structure()
method instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, thinking that
set_tyopgraphy_settings_array_structure()
should be ensuring this is an array, not just checking if empty. I.e. move this code:from
get_settings()
toset_tyopgraphy_settings_array_structure()
, and not just forthemes
but for all values that should be arrays.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!