From 41448bdb8c1ac541e1a5f7c3967c5bfc1fbb0109 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 10 Oct 2022 13:59:48 -0500 Subject: [PATCH 1/3] Backport of GB PR 44807 PHP code --- src/wp-includes/block-supports/typography.php | 5 +++++ tests/phpunit/tests/block-supports/typography.php | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/wp-includes/block-supports/typography.php b/src/wp-includes/block-supports/typography.php index 9d947088e2d2c..6fa3e9ddb4ef1 100644 --- a/src/wp-includes/block-supports/typography.php +++ b/src/wp-includes/block-supports/typography.php @@ -267,6 +267,11 @@ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) { return null; } + // Converts numbers to pixel values by default. + if ( is_numeric( $raw_value ) ) { + $raw_value = $raw_value . 'px'; + } + $defaults = array( 'coerce_to' => '', 'root_size_value' => 16, diff --git a/tests/phpunit/tests/block-supports/typography.php b/tests/phpunit/tests/block-supports/typography.php index a7e6a225678fa..34eee73006c76 100644 --- a/tests/phpunit/tests/block-supports/typography.php +++ b/tests/phpunit/tests/block-supports/typography.php @@ -289,6 +289,14 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '28px', ), + 'default_return_value_when_size_is_undefined' => array( + 'font_size_preset' => array( + 'size' => null, + ), + 'should_use_fluid_typography' => false, + 'expected_output' => null, + ), + 'default_return_value_when_fluid_is_false' => array( 'font_size_preset' => array( 'size' => '28px', From a7e1f21f889742f41027310d7ffe0435f86c76f9 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 10 Oct 2022 14:29:25 -0500 Subject: [PATCH 2/3] Add invalid data type checks --- src/wp-includes/block-supports/typography.php | 19 +++++--- .../tests/block-supports/typography.php | 45 +++++++++++++++++-- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/block-supports/typography.php b/src/wp-includes/block-supports/typography.php index 6fa3e9ddb4ef1..57a7b56807065 100644 --- a/src/wp-includes/block-supports/typography.php +++ b/src/wp-includes/block-supports/typography.php @@ -251,8 +251,8 @@ function wp_typography_get_preset_inline_style_value( $style_value, $css_propert * @since 6.1.0 * @access private * - * @param string $raw_value Raw size value from theme.json. - * @param array $options { + * @param string|int $raw_value Raw size value from theme.json. + * @param array $options { * Optional. An associative array of options. Default is empty array. * * @type string $coerce_to Coerce the value to rem or px. Default `'rem'`. @@ -263,6 +263,15 @@ function wp_typography_get_preset_inline_style_value( $style_value, $css_propert * `null` on failure. */ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) { + if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) ) { + _doing_it_wrong( + __FUNCTION__, + __( 'Raw size value must be a string or integer.' ), + '6.1.0' + ); + return null; + } + if ( empty( $raw_value ) ) { return null; } @@ -400,9 +409,9 @@ function wp_get_computed_fluid_typography_value( $args = array() ) { * @param array $preset { * Required. fontSizes preset value as seen in theme.json. * - * @type string $name Name of the font size preset. - * @type string $slug Kebab-case unique identifier for the font size preset. - * @type string $size CSS font-size value, including units where applicable. + * @type string $name Name of the font size preset. + * @type string $slug Kebab-case unique identifier for the font size preset. + * @type string|int $size CSS font-size value, including units where applicable. * } * @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing. * Default is `false`. diff --git a/tests/phpunit/tests/block-supports/typography.php b/tests/phpunit/tests/block-supports/typography.php index 34eee73006c76..960792a67a6a2 100644 --- a/tests/phpunit/tests/block-supports/typography.php +++ b/tests/phpunit/tests/block-supports/typography.php @@ -289,12 +289,20 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '28px', ), - 'default_return_value_when_size_is_undefined' => array( + 'size: int 0' => array( 'font_size_preset' => array( - 'size' => null, + 'size' => 0, ), 'should_use_fluid_typography' => false, - 'expected_output' => null, + 'expected_output' => 0, + ), + + 'size: string 0' => array( + 'font_size_preset' => array( + 'size' => '0', + ), + 'should_use_fluid_typography' => false, + 'expected_output' => '0', ), 'default_return_value_when_fluid_is_false' => array( @@ -379,4 +387,35 @@ public function data_generate_font_size_preset_fixtures() { ), ); } + + /** + * Tests generating font size values, including fluid formulae, from fontSizes preset. + * + * @ticket 56467 + * + * @covers ::wp_get_typography_value_and_unit + * + * @dataProvider data_invalid_size_wp_get_typography_value_and_unit + * @expectedIncorrectUsage wp_get_typography_value_and_unit + * + * @param mixed $raw_value Raw size value to test. + */ + function test_invalid_size_wp_get_typography_value_and_unit( $raw_value ) { + $this->assertNull( wp_get_typography_value_and_unit( $raw_value ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_invalid_size_wp_get_typography_value_and_unit() { + return array( + 'size: null' => array( null ), + 'size: false' => array( false ), + 'size: true' => array( true ), + 'size: float' => array( 10.1234 ), + 'size: array' => array( array( '10' ) ), + ); + } } From 77175b18ade47f683667ab7d1dedd7d6ae4c31e8 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Mon, 10 Oct 2022 14:33:52 -0500 Subject: [PATCH 3/3] Add missing dataset --- .../tests/block-supports/typography.php | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/tests/block-supports/typography.php b/tests/phpunit/tests/block-supports/typography.php index 960792a67a6a2..a8d1f34642424 100644 --- a/tests/phpunit/tests/block-supports/typography.php +++ b/tests/phpunit/tests/block-supports/typography.php @@ -281,7 +281,7 @@ function test_wp_get_typography_font_size_value( $font_size_preset, $should_use_ */ public function data_generate_font_size_preset_fixtures() { return array( - 'default_return_value' => array( + 'default_return_value' => array( 'font_size_preset' => array( 'size' => '28px', ), @@ -289,7 +289,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '28px', ), - 'size: int 0' => array( + 'size: int 0' => array( 'font_size_preset' => array( 'size' => 0, ), @@ -297,7 +297,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => 0, ), - 'size: string 0' => array( + 'size: string 0' => array( 'font_size_preset' => array( 'size' => '0', ), @@ -305,7 +305,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '0', ), - 'default_return_value_when_fluid_is_false' => array( + 'default_return_value_when_fluid_is_false' => array( 'font_size_preset' => array( 'size' => '28px', 'fluid' => false, @@ -314,7 +314,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '28px', ), - 'return_fluid_value' => array( + 'return_fluid_value' => array( 'font_size_preset' => array( 'size' => '1.75rem', ), @@ -322,6 +322,14 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => 'clamp(1.3125rem, 1.3125rem + ((1vw - 0.48rem) * 2.524), 2.625rem)', ), + 'return_fluid_value_with_number_coerced_to_px' => array( + 'font_size_preset' => array( + 'size' => 33, + ), + 'should_use_fluid_typography' => true, + 'expected_output' => 'clamp(24.75px, 1.546875rem + ((1vw - 7.68px) * 2.975), 49.5px)', + ), + 'return_default_fluid_values_with_empty_fluid_array' => array( 'font_size_preset' => array( 'size' => '28px', @@ -331,7 +339,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)', ), - 'return_default_fluid_values_with_null_value' => array( + 'return_default_fluid_values_with_null_value' => array( 'font_size_preset' => array( 'size' => '28px', 'fluid' => null, @@ -340,7 +348,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => 'clamp(21px, 1.3125rem + ((1vw - 7.68px) * 2.524), 42px)', ), - 'return_size_with_invalid_fluid_units' => array( + 'return_size_with_invalid_fluid_units' => array( 'font_size_preset' => array( 'size' => '10em', 'fluid' => array( @@ -352,7 +360,7 @@ public function data_generate_font_size_preset_fixtures() { 'expected_output' => '10em', ), - 'return_fluid_clamp_value' => array( + 'return_fluid_clamp_value' => array( 'font_size_preset' => array( 'size' => '28px', 'fluid' => array(