Opened 5 weeks ago
Closed 4 weeks ago
#64612 closed defect (bug) (fixed)
WP_Duotone: Fatal error when duotone attribute is an array instead of a string
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | trunk |
| Component: | Editor | Keywords: | has-patch has-unit-tests |
| Focuses: | Cc: |
Description
A fatal error occurs in WP_Duotone::get_slug_from_attribute() when the $duotone_attr parameter is an array (custom colors) instead of a string (preset reference).
Description
When a theme's theme.json defines duotone with an array of custom colors rather than a preset reference string, a fatal error is thrown:
Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in wp-includes/class-wp-duotone.php
The duotone attribute can be:
- A preset reference string:
var:preset|duotone|blue-orangeorvar(--wp--preset--duotone--blue-orange) - An array of custom colors:
['#000000', '#ffffff'] - A CSS string:
unset
The current code assumes the attribute is always a string when checking for presets, which causes a fatal error when an array of custom colors is passed.
Steps to Reproduce
- Create a theme with a
theme.jsonthat uses an array value for duotone:{ "styles": { "blocks": { "core/image": { "filter": { "duotone": ["#000000", "#ffffff"] } } } } } - Activate the theme
- Load any page - fatal error occurs
Expected Results
No fatal error. Custom duotone colors should be processed correctly.
Actual Results
Fatal error is thrown due to preg_match() receiving an array instead of a string.
Solution
Add type checks to ensure $duotone_attr is a string before attempting to parse it:
get_slug_from_attribute()- Returns empty string for non-string inputis_preset()- Returns false for non-string inputget_all_global_style_block_names()- Skips non-string duotone attributes
Related
- Gutenberg PR: https://github.com/WordPress/gutenberg/pull/75283
- Props: xavier-lc
## Trac ticket
https://core.trac.wordpress.org/ticket/64612
## Description
This PR backports the fix from Gutenberg PR #75283 to prevent a fatal error when the duotone attribute in
theme.jsonis an array (custom colors) instead of a string (preset reference).## Problem
A fatal error occurs in
WP_Duotone::get_slug_from_attribute()when$duotone_attris an array:This happens when a theme's
theme.jsondefines duotone with an array of colors (custom colors) rather than a preset reference string.## Solution
Add type checks to ensure
$duotone_attris a string before attempting to parse it:get_slug_from_attribute()- Returns empty string for non-string inputis_preset()- Returns false for non-string inputget_all_global_style_block_names()- Skips non-string duotone attributes## Testing
theme.jsonthat has an array value for duotone (custom colors)phpunit tests/phpunit/tests/block-supports/duotone.phpProps xavier-lc for the Gutenberg PR.