Skip to content

Commit

Permalink
Add remaining PHP changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Oct 11, 2022
1 parent 805565d commit 05f8898
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/wp-includes/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ function wp_render_typography_support( $block_content, $block ) {
* @since 6.1.0
* @access private
*
* @param string $raw_value Raw size value from theme.json.
* @param array $options {
* @param string|int|float $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'`.
Expand All @@ -294,10 +294,24 @@ function wp_render_typography_support( $block_content, $block ) {
* `null` on failure.
*/
function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) {
_doing_it_wrong(
__FUNCTION__,
__( 'Raw size value must be a string, integer or a float.' ),
'6.1.0'
);
return null;
}

if ( empty( $raw_value ) ) {
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,
Expand Down Expand Up @@ -426,19 +440,27 @@ 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|int $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|float $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`.
* @return string|null Font-size value or `null` if a size is not passed in $preset.
*/
function wp_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
if ( ! isset( $preset['size'] ) || empty( $preset['size'] ) ) {
if ( ! isset( $preset['size'] ) ) {
return null;
}

/*
* Catches empty values and 0/'0'.
* Fluid calculations cannot be performed on 0.
*/
if ( empty( $preset['size'] ) ) {
return $preset['size'];
}

// Checks if fluid font sizes are activated.
$typography_settings = wp_get_global_settings( array( 'typography' ) );
$should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography;
Expand Down
165 changes: 165 additions & 0 deletions tests/phpunit/tests/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,30 @@ public function data_generate_font_size_preset_fixtures() {
'expected_output' => '28px',
),

'size: int 0' => array(
'font_size_preset' => array(
'size' => 0,
),
'should_use_fluid_typography' => true,
'expected_output' => 0,
),

'size: string 0' => array(
'font_size_preset' => array(
'size' => '0',
),
'should_use_fluid_typography' => true,
'expected_output' => '0',
),

'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',
Expand Down Expand Up @@ -363,6 +387,30 @@ 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_floats_with_units' => array(
'font_size_preset' => array(
'size' => '100.175px',
),
'should_use_fluid_typography' => true,
'expected_output' => 'clamp(75.13125px, 4.695703125rem + ((1vw - 7.68px) * 9.03), 150.2625px)',
),

'return_fluid_value_with_integer_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_fluid_value_with_float_coerced_to_px' => array(
'font_size_preset' => array(
'size' => 100.23,
),
'should_use_fluid_typography' => true,
'expected_output' => 'clamp(75.1725px, 4.69828125rem + ((1vw - 7.68px) * 9.035), 150.345px)',
),

'return_default_fluid_values_with_empty_fluid_array' => array(
'font_size_preset' => array(
'size' => '28px',
Expand Down Expand Up @@ -583,4 +631,121 @@ public function data_generate_replace_inline_font_styles_with_fluid_values_fixtu
),
);
}

/**
* Tests that valid font size values are parsed.
*
* @ticket 56467
*
* @covers ::wp_get_typography_value_and_unit
*
* @dataProvider data_valid_size_wp_get_typography_value_and_unit
*
* @param mixed $raw_value Raw size value to test.
* @param mixed $expected An expected return value.
*/
public function test_valid_size_wp_get_typography_value_and_unit( $raw_value, $expected ) {
$this->assertEquals( $expected, wp_get_typography_value_and_unit( $raw_value ) );
}

/**
* Data provider.
*
* @return array
*/
public function data_valid_size_wp_get_typography_value_and_unit() {
return array(
'size: 10vh with default units do not match' => array(
'raw_value' => '10vh',
'expected' => null,
),
'size: calc() values do not match' => array(
'raw_value' => 'calc(2 * 10px)',
'expected' => null,
),
'size: clamp() values do not match' => array(
'raw_value' => 'clamp(15px, 0.9375rem + ((1vw - 7.68px) * 5.409), 60px)',
'expected' => null,
),
'size: `"10"`' => array(
'raw_value' => '10',
'expected' => array(
'value' => 10,
'unit' => 'px',
),
),
'size: `11`' => array(
'raw_value' => 11,
'expected' => array(
'value' => 11,
'unit' => 'px',
),
),
'size: `11.234`' => array(
'raw_value' => '11.234',
'expected' => array(
'value' => 11.234,
'unit' => 'px',
),
),
'size: `"12rem"`' => array(
'raw_value' => '12rem',
'expected' => array(
'value' => 12,
'unit' => 'rem',
),
),
'size: `"12px"`' => array(
'raw_value' => '12px',
'expected' => array(
'value' => 12,
'unit' => 'px',
),
),
'size: `"12em"`' => array(
'raw_value' => '12em',
'expected' => array(
'value' => 12,
'unit' => 'em',
),
),
'size: `"12.74em"`' => array(
'raw_value' => '12.74em',
'expected' => array(
'value' => 12.74,
'unit' => 'em',
),
),
);
}

/**
* Tests that invalid font size values are not parsed and trigger incorrect usage.
*
* @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.
*/
public 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: array' => array( array( '10' ) ),
);
}
}

0 comments on commit 05f8898

Please sign in to comment.