From ba644b94df35d58eceb7a89489d4d0b625f44fe8 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Sun, 18 Feb 2024 14:57:23 +0000 Subject: [PATCH] Editor: Prevent PHP warning when parsing duotone hue values. Props jacobcassidy, rahmohn, mukesh27. Fixes #59496. git-svn-id: https://develop.svn.wordpress.org/trunk@57652 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-duotone.php | 5 +--- .../phpunit/tests/block-supports/duotone.php | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-duotone.php b/src/wp-includes/class-wp-duotone.php index 194b649843..40e9f7548b 100644 --- a/src/wp-includes/class-wp-duotone.php +++ b/src/wp-includes/class-wp-duotone.php @@ -209,10 +209,7 @@ class WP_Duotone { 'rad' => 360 / ( M_PI * 2 ), ); - $factor = $angle_units[ $unit ]; - if ( ! $factor ) { - $factor = 1; - } + $factor = isset( $angle_units[ $unit ] ) ? $angle_units[ $unit ] : 1; return (float) $value * $factor; } diff --git a/tests/phpunit/tests/block-supports/duotone.php b/tests/phpunit/tests/block-supports/duotone.php index faeeaa2b40..5a31dc9362 100644 --- a/tests/phpunit/tests/block-supports/duotone.php +++ b/tests/phpunit/tests/block-supports/duotone.php @@ -5,7 +5,7 @@ * * @group block-supports * - * @package WordPress + * @coversDefaultClass WP_Duotone */ class Tests_Block_Supports_Duotone extends WP_UnitTestCase { @@ -166,4 +166,32 @@ class Tests_Block_Supports_Duotone extends WP_UnitTestCase { 'invalid' => array( 'not a valid attribute', false ), ); } + + /** + * @dataProvider data_colord_parse_hue + * @ticket 59496 + */ + public function test_colord_parse_hue( $value, $unit, $expected ) { + $reflection = new ReflectionMethod( 'WP_Duotone', 'colord_parse_hue' ); + $reflection->setAccessible( true ); + + $this->assertSame( $expected, $reflection->invoke( null, $value, $unit ) ); + } + + /** + * Data provider. + * + * @return array[]. + */ + public function data_colord_parse_hue() { + return array( + 'deg-angle-unit' => array( 120, 'deg', 120.0 ), + 'grad-angle-unit' => array( 120, 'grad', 108.0 ), + 'turn-angle-unit' => array( 120, 'turn', 43200.0 ), + 'rad-angle-unit' => array( 120, 'rad', 6875.493541569878 ), + 'empty-angle-unit' => array( 120, '', 120.0 ), + 'invalid-angle-unit' => array( 120, 'invalid', 120.0 ), + 'negative-value-deg-angle-unit' => array( -120, 'deg', -120.0 ), + ); + } }