Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/wp-includes/class-wp-duotone.php

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. I can see that $filter_id is now assigned in all branches of the conditional.

Before this change, PhpStorm also flags an error:

Image

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking this in PhpStorm as well! With the early type guard, unsupported attribute values return unchanged and every remaining branch assigns $filter_id.

Original file line number Diff line number Diff line change
Expand Up @@ -1116,9 +1116,12 @@ public static function render_duotone_support( $block_content, $block, $wp_block
*/

$duotone_attr = $block['attrs']['style']['color']['duotone'];
$is_preset = is_string( $duotone_attr ) && self::is_preset( $duotone_attr );
$is_css = is_string( $duotone_attr ) && ! $is_preset;
$is_custom = is_array( $duotone_attr );
if ( ! is_string( $duotone_attr ) && ! is_array( $duotone_attr ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! I think we'd need to sync Gutenberg's copy as well lib/class-wp-duotone-gutenberg.php

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the Gutenberg copy! I have opened WordPress/gutenberg#84801 to sync the early type guard and exhaustive branches, with coverage for the four unsupported attribute values. The existing isset( $filter_id ) guard in Gutenberg is preserved.

Validation: all 27 duotone tests (27 assertions), Gutenberg PHPCS for both changed PHP files, and the production build pass. PHP syntax checks also pass on PHP 7.4 and 8.3. The Gutenberg PR includes a backport-changelog entry linking it to this Core PR.

return $block_content;
}

$is_preset = is_string( $duotone_attr ) && self::is_preset( $duotone_attr );
$is_css = is_string( $duotone_attr ) && ! $is_preset;

if ( $is_preset ) {

Expand All @@ -1136,7 +1139,7 @@ public static function render_duotone_support( $block_content, $block, $wp_block

// Just block CSS.
self::enqueue_block_css( $filter_id, $duotone_selector, $filter_value );
} elseif ( $is_custom ) {
} else {
$slug = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); // e.g. '000000-ffffff-2'.
$filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-000000-ffffff-2'.
$filter_value = self::get_filter_url( $filter_id ); // e.g. 'url(#wp-duotone-filter-000000-ffffff-2)'.
Expand All @@ -1148,7 +1151,7 @@ public static function render_duotone_support( $block_content, $block, $wp_block
// SVG filter and block CSS.
self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data );
}
} elseif ( $has_global_styles_duotone ) {
} else {
$slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
$filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
$filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
Expand Down
5 changes: 0 additions & 5 deletions tests/phpstan/baselines/variable.undefined.neon
Original file line number Diff line number Diff line change
Expand Up @@ -703,11 +703,6 @@ parameters:
identifier: variable.undefined
count: 5
path: ../../../src/wp-includes/class-wp-block-processor.php
-
message: '#^Variable \$filter_id might not be defined\.$#'
identifier: variable.undefined
count: 1
path: ../../../src/wp-includes/class-wp-duotone.php
-
message: '#^Variable \$processed_response might not be defined\.$#'
identifier: variable.undefined
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@
*/

class Tests_Block_Supports_Duotone extends WP_UnitTestCase {
/**
* Tests that unsupported duotone attribute types leave the block unchanged.
*
* @ticket 65817
* @dataProvider data_render_duotone_support_invalid_attribute
* @covers ::render_duotone_support
*
* @param mixed $duotone Invalid duotone attribute value.
*/
public function test_render_duotone_support_invalid_attribute( $duotone ) {
$block = array(
'blockName' => 'core/image',
'attrs' => array( 'style' => array( 'color' => array( 'duotone' => $duotone ) ) ),
);
$wp_block = new WP_Block( $block );
$block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';

$this->assertSame( $block_content, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
}

/**
* @return array
*/
public function data_render_duotone_support_invalid_attribute() {
return array(
'false' => array( false ),
'true' => array( true ),
'integer' => array( 1 ),
'float' => array( 1.5 ),
);
}

/**
* Tests whether the duotone preset class is added to the block.
*
Expand Down
Loading