Skip to content

Commit 05f8898

Browse files
committed
Add remaining PHP changes
1 parent 805565d commit 05f8898

2 files changed

Lines changed: 193 additions & 6 deletions

File tree

src/wp-includes/block-supports/typography.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ function wp_render_typography_support( $block_content, $block ) {
282282
* @since 6.1.0
283283
* @access private
284284
*
285-
* @param string $raw_value Raw size value from theme.json.
286-
* @param array $options {
285+
* @param string|int|float $raw_value Raw size value from theme.json.
286+
* @param array $options {
287287
* Optional. An associative array of options. Default is empty array.
288288
*
289289
* @type string $coerce_to Coerce the value to rem or px. Default `'rem'`.
@@ -294,10 +294,24 @@ function wp_render_typography_support( $block_content, $block ) {
294294
* `null` on failure.
295295
*/
296296
function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
297+
if ( ! is_string( $raw_value ) && ! is_int( $raw_value ) && ! is_float( $raw_value ) ) {
298+
_doing_it_wrong(
299+
__FUNCTION__,
300+
__( 'Raw size value must be a string, integer or a float.' ),
301+
'6.1.0'
302+
);
303+
return null;
304+
}
305+
297306
if ( empty( $raw_value ) ) {
298307
return null;
299308
}
300309

310+
// Converts numbers to pixel values by default.
311+
if ( is_numeric( $raw_value ) ) {
312+
$raw_value = $raw_value . 'px';
313+
}
314+
301315
$defaults = array(
302316
'coerce_to' => '',
303317
'root_size_value' => 16,
@@ -426,19 +440,27 @@ function wp_get_computed_fluid_typography_value( $args = array() ) {
426440
* @param array $preset {
427441
* Required. fontSizes preset value as seen in theme.json.
428442
*
429-
* @type string $name Name of the font size preset.
430-
* @type string $slug Kebab-case unique identifier for the font size preset.
431-
* @type string|int $size CSS font-size value, including units where applicable.
443+
* @type string $name Name of the font size preset.
444+
* @type string $slug Kebab-case unique identifier for the font size preset.
445+
* @type string|int|float $size CSS font-size value, including units where applicable.
432446
* }
433447
* @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing.
434448
* Default is `false`.
435449
* @return string|null Font-size value or `null` if a size is not passed in $preset.
436450
*/
437451
function wp_get_typography_font_size_value( $preset, $should_use_fluid_typography = false ) {
438-
if ( ! isset( $preset['size'] ) || empty( $preset['size'] ) ) {
452+
if ( ! isset( $preset['size'] ) ) {
439453
return null;
440454
}
441455

456+
/*
457+
* Catches empty values and 0/'0'.
458+
* Fluid calculations cannot be performed on 0.
459+
*/
460+
if ( empty( $preset['size'] ) ) {
461+
return $preset['size'];
462+
}
463+
442464
// Checks if fluid font sizes are activated.
443465
$typography_settings = wp_get_global_settings( array( 'typography' ) );
444466
$should_use_fluid_typography = isset( $typography_settings['fluid'] ) && true === $typography_settings['fluid'] ? true : $should_use_fluid_typography;

tests/phpunit/tests/block-supports/typography.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,30 @@ public function data_generate_font_size_preset_fixtures() {
328328
'expected_output' => '28px',
329329
),
330330

331+
'size: int 0' => array(
332+
'font_size_preset' => array(
333+
'size' => 0,
334+
),
335+
'should_use_fluid_typography' => true,
336+
'expected_output' => 0,
337+
),
338+
339+
'size: string 0' => array(
340+
'font_size_preset' => array(
341+
'size' => '0',
342+
),
343+
'should_use_fluid_typography' => true,
344+
'expected_output' => '0',
345+
),
346+
347+
'default_return_value_when_size_is_undefined' => array(
348+
'font_size_preset' => array(
349+
'size' => null,
350+
),
351+
'should_use_fluid_typography' => false,
352+
'expected_output' => null,
353+
),
354+
331355
'default_return_value_when_fluid_is_false' => array(
332356
'font_size_preset' => array(
333357
'size' => '28px',
@@ -363,6 +387,30 @@ public function data_generate_font_size_preset_fixtures() {
363387
'expected_output' => 'clamp(1.3125rem, 1.3125rem + ((1vw - 0.48rem) * 2.524), 2.625rem)',
364388
),
365389

390+
'return_fluid_value_with_floats_with_units' => array(
391+
'font_size_preset' => array(
392+
'size' => '100.175px',
393+
),
394+
'should_use_fluid_typography' => true,
395+
'expected_output' => 'clamp(75.13125px, 4.695703125rem + ((1vw - 7.68px) * 9.03), 150.2625px)',
396+
),
397+
398+
'return_fluid_value_with_integer_coerced_to_px' => array(
399+
'font_size_preset' => array(
400+
'size' => 33,
401+
),
402+
'should_use_fluid_typography' => true,
403+
'expected_output' => 'clamp(24.75px, 1.546875rem + ((1vw - 7.68px) * 2.975), 49.5px)',
404+
),
405+
406+
'return_fluid_value_with_float_coerced_to_px' => array(
407+
'font_size_preset' => array(
408+
'size' => 100.23,
409+
),
410+
'should_use_fluid_typography' => true,
411+
'expected_output' => 'clamp(75.1725px, 4.69828125rem + ((1vw - 7.68px) * 9.035), 150.345px)',
412+
),
413+
366414
'return_default_fluid_values_with_empty_fluid_array' => array(
367415
'font_size_preset' => array(
368416
'size' => '28px',
@@ -583,4 +631,121 @@ public function data_generate_replace_inline_font_styles_with_fluid_values_fixtu
583631
),
584632
);
585633
}
634+
635+
/**
636+
* Tests that valid font size values are parsed.
637+
*
638+
* @ticket 56467
639+
*
640+
* @covers ::wp_get_typography_value_and_unit
641+
*
642+
* @dataProvider data_valid_size_wp_get_typography_value_and_unit
643+
*
644+
* @param mixed $raw_value Raw size value to test.
645+
* @param mixed $expected An expected return value.
646+
*/
647+
public function test_valid_size_wp_get_typography_value_and_unit( $raw_value, $expected ) {
648+
$this->assertEquals( $expected, wp_get_typography_value_and_unit( $raw_value ) );
649+
}
650+
651+
/**
652+
* Data provider.
653+
*
654+
* @return array
655+
*/
656+
public function data_valid_size_wp_get_typography_value_and_unit() {
657+
return array(
658+
'size: 10vh with default units do not match' => array(
659+
'raw_value' => '10vh',
660+
'expected' => null,
661+
),
662+
'size: calc() values do not match' => array(
663+
'raw_value' => 'calc(2 * 10px)',
664+
'expected' => null,
665+
),
666+
'size: clamp() values do not match' => array(
667+
'raw_value' => 'clamp(15px, 0.9375rem + ((1vw - 7.68px) * 5.409), 60px)',
668+
'expected' => null,
669+
),
670+
'size: `"10"`' => array(
671+
'raw_value' => '10',
672+
'expected' => array(
673+
'value' => 10,
674+
'unit' => 'px',
675+
),
676+
),
677+
'size: `11`' => array(
678+
'raw_value' => 11,
679+
'expected' => array(
680+
'value' => 11,
681+
'unit' => 'px',
682+
),
683+
),
684+
'size: `11.234`' => array(
685+
'raw_value' => '11.234',
686+
'expected' => array(
687+
'value' => 11.234,
688+
'unit' => 'px',
689+
),
690+
),
691+
'size: `"12rem"`' => array(
692+
'raw_value' => '12rem',
693+
'expected' => array(
694+
'value' => 12,
695+
'unit' => 'rem',
696+
),
697+
),
698+
'size: `"12px"`' => array(
699+
'raw_value' => '12px',
700+
'expected' => array(
701+
'value' => 12,
702+
'unit' => 'px',
703+
),
704+
),
705+
'size: `"12em"`' => array(
706+
'raw_value' => '12em',
707+
'expected' => array(
708+
'value' => 12,
709+
'unit' => 'em',
710+
),
711+
),
712+
'size: `"12.74em"`' => array(
713+
'raw_value' => '12.74em',
714+
'expected' => array(
715+
'value' => 12.74,
716+
'unit' => 'em',
717+
),
718+
),
719+
);
720+
}
721+
722+
/**
723+
* Tests that invalid font size values are not parsed and trigger incorrect usage.
724+
*
725+
* @ticket 56467
726+
*
727+
* @covers ::wp_get_typography_value_and_unit
728+
*
729+
* @dataProvider data_invalid_size_wp_get_typography_value_and_unit
730+
* @expectedIncorrectUsage wp_get_typography_value_and_unit
731+
*
732+
* @param mixed $raw_value Raw size value to test.
733+
*/
734+
public function test_invalid_size_wp_get_typography_value_and_unit( $raw_value ) {
735+
$this->assertNull( wp_get_typography_value_and_unit( $raw_value ) );
736+
}
737+
738+
/**
739+
* Data provider.
740+
*
741+
* @return array
742+
*/
743+
public function data_invalid_size_wp_get_typography_value_and_unit() {
744+
return array(
745+
'size: null' => array( null ),
746+
'size: false' => array( false ),
747+
'size: true' => array( true ),
748+
'size: array' => array( array( '10' ) ),
749+
);
750+
}
586751
}

0 commit comments

Comments
 (0)