diff --git a/src/wp-includes/block-supports/typography.php b/src/wp-includes/block-supports/typography.php index 4f8e32571af73..ffa76d31a329c 100644 --- a/src/wp-includes/block-supports/typography.php +++ b/src/wp-includes/block-supports/typography.php @@ -669,7 +669,7 @@ function wp_get_typography_font_size_value( $preset, $settings = array() ) { * For a - b * log2(), lower values of b will make the curve move towards the minimum faster. * The scale factor is constrained between min and max values. */ - $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 ); + $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 ); $calculated_minimum_font_size = round( $preferred_size['value'] * $minimum_font_size_factor, 3 ); // Only use calculated min font size if it's > $minimum_font_size_limit value. diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php index 3ac1372fdca1e..f192bc3ab1a91 100644 --- a/src/wp-includes/compat.php +++ b/src/wp-includes/compat.php @@ -535,6 +535,71 @@ function array_last( array $array ) { // phpcs:ignore Universal.NamingConvention } } +/** + * Throws a ValueError (PHP 8.0+) or an InvalidArgumentException (PHP 7.x) with the given message. + * + * Helper for polyfills that need to throw ValueError but must also run on PHP 7.4. + * + * @ignore + * @since 7.1.0 + * @access private + * + * @param string $message The error message. + * @throws ValueError On PHP 8.0 and later. + * @throws InvalidArgumentException On PHP 7.x as a fallback. + */ +function _wp_throw_value_error( $message ) { + if ( ! class_exists( 'ValueError' ) ) { + throw new InvalidArgumentException( $message ); + } + + throw new ValueError( $message ); +} + +if ( ! function_exists( 'clamp' ) ) { + /** + * Polyfill for `clamp()` function added in PHP 8.6. + * + * Clamps a value to be within the range of a given minimum and maximum. + * + * If the value is within the bounds, the original value is returned. + * If it is not within the bounds, the closest bound is returned. + * + * @since 7.1.0 + * + * @param mixed $value The value to clamp. + * @param mixed $min The minimum bound. Must be less than or equal to `$max`. + * @param mixed $max The maximum bound. Must be greater than or equal to `$min`. + * @return mixed The clamped value. + * + * @throws ValueError On PHP 8.0+: if `$min` is greater than `$max`, or if `$min` or `$max` is NAN. + * @throws InvalidArgumentException On PHP 7.x: if `$min` is greater than `$max`, or if `$min` or `$max` is NAN. + */ + function clamp( $value, $min, $max ) { + if ( is_float( $min ) && is_nan( $min ) ) { + _wp_throw_value_error( 'clamp(): Argument #2 ($min) cannot be NAN' ); + } + + if ( is_float( $max ) && is_nan( $max ) ) { + _wp_throw_value_error( 'clamp(): Argument #3 ($max) cannot be NAN' ); + } + + if ( $max < $min ) { + _wp_throw_value_error( 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)' ); + } + + if ( $value < $min ) { + return $min; + } + + if ( $value > $max ) { + return $max; + } + + return $value; + } +} + // IMAGETYPE_AVIF constant is only defined in PHP 8.x or later. if ( ! defined( 'IMAGETYPE_AVIF' ) ) { define( 'IMAGETYPE_AVIF', 19 ); diff --git a/src/wp-includes/embed.php b/src/wp-includes/embed.php index 3fb8968c7c62c..d944ae1f9962f 100644 --- a/src/wp-includes/embed.php +++ b/src/wp-includes/embed.php @@ -594,7 +594,7 @@ function get_oembed_response_data( $post, $width ) { ) ); - $width = min( max( $min_max_width['min'], $width ), $min_max_width['max'] ); + $width = clamp( $width, $min_max_width['min'], $min_max_width['max'] ); $height = max( (int) ceil( $width / 16 * 9 ), 200 ); $data = array(