Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 65 additions & 0 deletions src/wp-includes/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ignore?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @ignore PHPDoc tag tells documentation generators to exclude the function from the generated documentation.

It's only used by clamp(). External code (plugins/themes) should never need to call it — they can throw their own exceptions directly.

* @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)' );
}
Comment on lines +559 to +589

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 );
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading