-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Font Library: ensure merged fontFace data is enconded as an array instead of an object #54435
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/font-library/class-wp-font-family-utils.php ❔ phpunit/tests/fonts/font-library/wpFontFamilyUtils/mergeFontsData.php |
Flaky tests detected in 6d894c4. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6175255285
|
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.
Makes sense to me, I ran into a similar bug working on the duplicate variants issue, which was fixed by using array_values
. Thanks!
What?
Why?
To fix a bug consisting of fontFace data being JSON encoded as an object ( {...} ) instead of an array ( [...] ).
When we remove duplicates, we could end up with missing sequential array keys in the fontFace list.
Because of the nature of PHP arrays and how they are encoded into JSON. In PHP, arrays can behave like indexed arrays or associative arrays (hashmaps). When they are encoded into JSON, indexed arrays become JSON arrays (e.g., [...]), while associative arrays become JSON objects (e.g., {...}).
The reason fontFace is being encoded as an object in your JSON output is due to the non-sequential keys.
How?
To ensure that fontFace is always encoded as a JSON array, I reset the keys of the
$unique_face
s array usingarray_values()
before assigning it to$merged_font['fontFace']
. This will reindex the keys and make sure they are sequential.Testing Instructions
Run PHP unit tests with and without calling
array_values()
on this line:The added test case should fail when
array_values()
is not used.