Skip to content

Commit edd6885

Browse files
Block Supports: Prevent fatal error in WP_Duotone when the duotone attribute is an array.
Adds type checks to `get_slug_from_attribute()`, `is_preset()`, and `get_all_global_style_block_names()` to handle cases where the duotone attribute is an array of custom colors instead of a preset reference string. This prevents an error when `preg_match()` receives an array instead of a string. Props jorgefilipecosta, westonruter, xavilc. Fixes #64612. git-svn-id: https://develop.svn.wordpress.org/trunk@61603 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 55227eb commit edd6885

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/wp-includes/class-wp-duotone.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,14 @@ private static function colord_parse( $input ) {
546546
*
547547
* @since 6.3.0
548548
*
549-
* @param string $duotone_attr The duotone attribute from a block.
550-
* @return string The slug of the duotone preset or an empty string if no slug is found.
549+
* @param string|string[] $duotone_attr The duotone attribute from a block.
550+
* @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed).
551551
*/
552552
private static function get_slug_from_attribute( $duotone_attr ) {
553+
if ( ! is_string( $duotone_attr ) ) {
554+
return '';
555+
}
556+
553557
// Uses Branch Reset Groups `(?|…)` to return one capture group.
554558
preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches );
555559

@@ -566,9 +570,13 @@ private static function get_slug_from_attribute( $duotone_attr ) {
566570
* @since 6.3.0
567571
*
568572
* @param string $duotone_attr The duotone attribute from a block.
569-
* @return bool True if the duotone preset present and valid.
573+
* @param string|string[] $duotone_attr The duotone attribute from a block.
570574
*/
571575
private static function is_preset( $duotone_attr ) {
576+
if ( ! is_string( $duotone_attr ) ) {
577+
return false;
578+
}
579+
572580
$slug = self::get_slug_from_attribute( $duotone_attr );
573581
$filter_id = self::get_filter_id( $slug );
574582

@@ -1050,6 +1058,11 @@ private static function get_all_global_style_block_names() {
10501058
continue;
10511059
}
10521060
// If it has a duotone filter preset, save the block name and the preset slug.
1061+
// Only process if it's a string (preset reference), not an array (custom colors).
1062+
if ( ! is_string( $duotone_attr ) ) {
1063+
continue;
1064+
}
1065+
10531066
$slug = self::get_slug_from_attribute( $duotone_attr );
10541067

10551068
if ( $slug && $slug !== $duotone_attr ) {

tests/phpunit/tests/block-supports/duotone.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public function data_get_slug_from_attribute() {
9393
'pipe-slug-no-value' => array( 'var:preset|duotone|', '' ),
9494
'css-var-spaces' => array( 'var(--wp--preset--duotone-- ', '' ),
9595
'pipe-slug-spaces' => array( 'var:preset|duotone| ', '' ),
96+
'array-of-colors' => array( array( '#000000', '#ffffff' ), '' ),
97+
'empty-array' => array( array(), '' ),
9698
);
9799
}
98100

@@ -164,6 +166,8 @@ public function data_is_preset() {
164166
'css-var-invalid-slug-chars' => array( 'var(--wp--preset--duotone--.)', false ),
165167
'css-var-missing-end-parenthesis' => array( 'var(--wp--preset--duotone--blue-orange', false ),
166168
'invalid' => array( 'not a valid attribute', false ),
169+
'array-of-colors' => array( array( '#000000', '#ffffff' ), false ),
170+
'empty-array' => array( array(), false ),
167171
);
168172
}
169173

0 commit comments

Comments
 (0)