-
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?
Fix and improve logic in WP_Fonts_Resolver::get_settings() #56489
Conversation
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/experimental/fonts-api/class-wp-fonts-resolver.php |
! isset( $settings['typography']['fontFamilies']['theme'] ) || | ||
! is_array( $settings['typography']['fontFamilies']['theme'] ) | ||
) { | ||
$settings['typography']['fontFamilies']['theme'] = array(); |
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 the typography.fontFamilies.theme
structure in the given array and it works well without this check now
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.
@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:
if (
! isset( $settings['typography']['fontFamilies']['theme'] ) ||
! is_array( $settings['typography']['fontFamilies']['theme'] )
) {
$settings['typography']['fontFamilies']['theme'] = array();
}
from get_settings()
to set_tyopgraphy_settings_array_structure()
, and not just for themes
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!
} | ||
|
||
// Make sure there are no duplicate values with different names (array keys). | ||
$settings['typography']['fontFamilies']['theme'] = array_unique( $settings['typography']['fontFamilies']['theme'], SORT_REGULAR ); |
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.
The typography.fontFamilies.theme
might not be set if there is no variation or all of them don't have the font definition.
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 agree. If $settings['typography']['fontFamilies']['theme']
is not initialliy set and there are no $variations
, this could become an issue.
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.
Right, good catch, thanks. Fixing.
! isset( $settings['typography']['fontFamilies']['theme'] ) || | ||
! is_array( $settings['typography']['fontFamilies']['theme'] ) | ||
) { | ||
$settings['typography']['fontFamilies']['theme'] = array(); |
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.
@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.
|
||
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Variations overwrite the globals.
I like the effort to explain what array_unique()
does here. 👍
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.
Hehe, yea may not be obvious to all. A simple inline comment may save somebody an hour or two in some cases :)
} | ||
|
||
// Make sure there are no duplicate values with different names (array keys). | ||
$settings['typography']['fontFamilies']['theme'] = array_unique( $settings['typography']['fontFamilies']['theme'], SORT_REGULAR ); |
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 agree. If $settings['typography']['fontFamilies']['theme']
is not initialliy set and there are no $variations
, this could become an issue.
…y before checking for duplicates
Flaky tests detected in 654ae65. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6984746001
|
What?
Follow-up to #56067.
Changes:
$variation['settings']['typography']['fontFamilies']['theme']
are set at the top of the loop. It doesn't make sense to only check for$variation['settings']['typography']['fontFamilies']
.$settings['typography']['fontFamilies']['theme']
to a empty array before that array is used. It doesn't make sense for that check to be after the array is needed/used.array_unique()
after the loop. It doesn't make sense to be inside the loop/to run on every iteration.$settings['typography']['fontFamilies']['theme']
array for uniqueness as we were just modifying it in the loop. It doesn't make sense to check$settings['typography']['fontFamilies']
for uniqueness as that array was never modified in this function. This erroneous check was also the cause of the fatal errors as far as I see.Why?
Attempts to completely fix the errors in
WP_Fonts_Resolver::get_settings()
that merges the default settings with the variations (if any).Testing Instructions
See #56067 for instructions.