@@ -358,8 +358,14 @@ function wp_render_typography_support( $block_content, $block ) {
358358 * }
359359 * @return array|null An array consisting of `'value'` and `'unit'` properties on success.
360360 * `null` on failure.
361+ * @phpstan-param array{
362+ * coerce_to?: string,
363+ * root_size_value?: positive-int,
364+ * acceptable_units?: non-empty-array<non-empty-string>,
365+ * } $options
366+ * @phpstan-return array{ value: float, unit: non-empty-string }|null
361367 */
362- function wp_get_typography_value_and_unit ( $ raw_value , $ options = array () ) {
368+ function wp_get_typography_value_and_unit ( $ raw_value , $ options = array () ): ? array {
363369 if ( ! is_string ( $ raw_value ) && ! is_int ( $ raw_value ) && ! is_float ( $ raw_value ) ) {
364370 _doing_it_wrong (
365371 __FUNCTION__ ,
@@ -384,21 +390,27 @@ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
384390 'acceptable_units ' => array ( 'rem ' , 'px ' , 'em ' ),
385391 );
386392
393+ /**
394+ * @var array{
395+ * coerce_to: string,
396+ * root_size_value: positive-int,
397+ * acceptable_units: non-empty-array<non-empty-string>,
398+ * } $options
399+ */
387400 $ options = wp_parse_args ( $ options , $ defaults );
388401
389- $ acceptable_units_group = implode ( '| ' , $ options ['acceptable_units ' ] );
390- $ pattern = '/^(\d*\.?\d+)( ' . $ acceptable_units_group . '){1,1}$/ ' ;
391-
392- preg_match ( $ pattern , $ raw_value , $ matches );
393-
394- // Bails out if not a number value and a px or rem unit.
395- if ( ! isset ( $ matches [1 ] ) || ! isset ( $ matches [2 ] ) ) {
402+ // Bails out if the raw value can't be parsed.
403+ if ( ! preg_match ( '/^(\d*\.?\d+)([a-zA-Z]+|%)$/ ' , $ raw_value , $ matches ) ) {
396404 return null ;
397405 }
398406
399- $ value = $ matches [1 ];
407+ $ value = ( float ) $ matches [1 ];
400408 $ unit = $ matches [2 ];
401409
410+ if ( ! in_array ( $ unit , $ options ['acceptable_units ' ], true ) ) {
411+ return null ;
412+ }
413+
402414 /*
403415 * Default browser font size. Later, possibly could inject some JS to
404416 * compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
@@ -669,7 +681,7 @@ function wp_get_typography_font_size_value( $preset, $settings = array() ) {
669681 * For a - b * log2(), lower values of b will make the curve move towards the minimum faster.
670682 * The scale factor is constrained between min and max values.
671683 */
672- $ minimum_font_size_factor = min ( max ( 1 - 0.075 * log ( $ preferred_font_size_in_px , 2 ), $ default_minimum_font_size_factor_min ) , $ default_minimum_font_size_factor_max );
684+ $ minimum_font_size_factor = clamp ( 1 - 0.075 * log ( $ preferred_font_size_in_px , 2 ), $ default_minimum_font_size_factor_min , $ default_minimum_font_size_factor_max );
673685 $ calculated_minimum_font_size = round ( $ preferred_size ['value ' ] * $ minimum_font_size_factor , 3 );
674686
675687 // Only use calculated min font size if it's > $minimum_font_size_limit value.
0 commit comments