Skip to content

Commit 70c3625

Browse files
xavier-lcjorgefilipecostawestonruterxavier-lc
authored
Prevent fatal error when the inline CSS duotone variable is an array (#75283)
Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: xavier-lc <xavilc@git.wordpress.org>
1 parent 605f7a0 commit 70c3625

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎backport-changelog/7.0/10883.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/10883
2+
3+
* https://github.com/WordPress/gutenberg/pull/75283

‎lib/class-wp-duotone-gutenberg.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,14 @@ private static function colord_parse( $input ) {
448448
* var:preset|duotone|blue-orange
449449
* var(--wp--preset--duotone--blue-orange)
450450
*
451-
* @param string $duotone_attr The duotone attribute from a block.
452-
* @return string The slug of the duotone preset or an empty string if no slug is found.
451+
* @param string|string[] $duotone_attr The duotone attribute from a block.
452+
* @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed).
453453
*/
454454
private static function get_slug_from_attribute( $duotone_attr ) {
455+
if ( ! is_string( $duotone_attr ) ) {
456+
return '';
457+
}
458+
455459
// Uses Branch Reset Groups `(?|…)` to return one capture group.
456460
preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches );
457461

@@ -461,10 +465,14 @@ private static function get_slug_from_attribute( $duotone_attr ) {
461465
/**
462466
* Check if we have a valid duotone preset.
463467
*
464-
* @param string $duotone_attr The duotone attribute from a block.
468+
* @param string|string[] $duotone_attr The duotone attribute from a block.
465469
* @return bool True if the duotone preset present and valid.
466470
*/
467471
private static function is_preset( $duotone_attr ) {
472+
if ( ! is_string( $duotone_attr ) ) {
473+
return false;
474+
}
475+
468476
$slug = self::get_slug_from_attribute( $duotone_attr );
469477
$filter_id = self::get_filter_id( $slug );
470478

@@ -809,6 +817,11 @@ public static function set_global_style_block_names() {
809817
continue;
810818
}
811819
// If it has a duotone filter preset, save the block name and the preset slug.
820+
// Only process if it's a string (preset reference), not an array (custom colors).
821+
if ( ! is_string( $duotone_attr ) ) {
822+
continue;
823+
}
824+
812825
$slug = self::get_slug_from_attribute( $duotone_attr );
813826

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

‎phpunit/class-wp-duotone-test.php‎

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

@@ -115,6 +117,8 @@ public function data_is_preset() {
115117
'css-var-invalid-slug-chars' => array( 'var(--wp--preset--duotone--.)', false ),
116118
'css-var-missing-end-parenthesis' => array( 'var(--wp--preset--duotone--blue-orange', false ),
117119
'invalid' => array( 'not a valid attribute', false ),
120+
'array-of-colors' => array( array( '#000000', '#ffffff' ), false ),
121+
'empty-array' => array( array(), false ),
118122
);
119123
}
120124

0 commit comments

Comments
 (0)