Skip to content

Commit a115c04

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents 9f9f3b5 + c94fa58 commit a115c04

5 files changed

Lines changed: 506 additions & 11 deletions

File tree

phpstan.neon.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ parameters:
4040
identifier: function.inner
4141
path: src/wp-includes/canonical.php
4242
count: 1
43+
# Level 2:
44+
# ValueError is PHP 8.0+; core throws it conditionally so the docblocks are correct for WP's 7.4+ range,
45+
# but bleedingEdge's version-aware check treats the class as non-existent against the PHP 7.4 floor.
46+
-
47+
message: '#^PHPDoc tag @throws with type InvalidArgumentException\|ValueError is not subtype of Throwable$#'
48+
path: src/wp-includes/compat.php
49+
reportUnmatched: false

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

src/wp-includes/compat.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,72 @@ function array_last( array $array ) { // phpcs:ignore Universal.NamingConvention
685685
}
686686
}
687687

688+
if ( ! function_exists( 'clamp' ) ) {
689+
/**
690+
* Polyfill for `clamp()` function added in PHP 8.6.
691+
*
692+
* Clamps a value to be within the range of a given minimum and maximum.
693+
*
694+
* If the value is within the bounds, the original value is returned.
695+
* If it is not within the bounds, the closest bound is returned.
696+
*
697+
* @since 7.1.0
698+
*
699+
* @param mixed $value The value to clamp.
700+
* @param mixed $min The minimum bound. Must be less than or equal to `$max`.
701+
* @param mixed $max The maximum bound. Must be greater than or equal to `$min`.
702+
* @return mixed The clamped value. Either `$value`, `$min`, or `$max`.
703+
*
704+
* @throws ValueError If `$min` is greater than `$max`, or if `$min` or `$max` is NAN, and the ValueError class exists (PHP 8.0+ or polyfilled).
705+
* @throws InvalidArgumentException If `$min` is greater than `$max`, or if `$min` or `$max` is NAN, and the ValueError class does not exist (PHP 7.x).
706+
*
707+
* @phpstan-template TValue
708+
* @phpstan-template TMin
709+
* @phpstan-template TMax
710+
* @phpstan-param TValue $value
711+
* @phpstan-param TMin $min
712+
* @phpstan-param TMax $max
713+
* @phpstan-return TValue|TMin|TMax
714+
*/
715+
function clamp( $value, $min, $max ) {
716+
$throw_value_error = static function ( string $message ) {
717+
// The ValueError class was introduced in PHP 8, so in 7.4 throw InvalidArgumentException instead.
718+
if ( ! class_exists( 'ValueError', false ) ) {
719+
throw new InvalidArgumentException( $message );
720+
}
721+
throw new ValueError( $message );
722+
};
723+
724+
if ( is_float( $min ) && is_nan( $min ) ) {
725+
$throw_value_error( 'clamp(): Argument #2 ($min) must not be NAN' );
726+
}
727+
728+
if ( is_float( $max ) && is_nan( $max ) ) {
729+
$throw_value_error( 'clamp(): Argument #3 ($max) must not be NAN' );
730+
}
731+
732+
if ( $max < $min ) {
733+
$throw_value_error( 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)' );
734+
}
735+
736+
/*
737+
* The upper bound is checked before the lower bound to match the order in PHP's
738+
* implementation. Comparison in PHP is not transitive when operands of different
739+
* types are mixed (for example, comparing against a bool coerces both operands to
740+
* bool), so both bounds can compare as exceeded at once, and the first check wins.
741+
*/
742+
if ( $value > $max ) {
743+
return $max;
744+
}
745+
746+
if ( $value < $min ) {
747+
return $min;
748+
}
749+
750+
return $value;
751+
}
752+
}
753+
688754
// IMAGETYPE_AVIF constant is only defined in PHP 8.x or later.
689755
if ( ! defined( 'IMAGETYPE_AVIF' ) ) {
690756
define( 'IMAGETYPE_AVIF', 19 );

src/wp-includes/embed.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ function get_oembed_response_data( $post, $width ) {
594594
)
595595
);
596596

597-
$width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] );
597+
/** @var array{ min: positive-int, max: positive-int } $min_max_width */
598+
$width = clamp( $width, $min_max_width['min'], $min_max_width['max'] );
598599
$height = max( (int) ceil( $width / 16 * 9 ), 200 );
599600

600601
$data = array(

0 commit comments

Comments
 (0)