Make WordPress Core


Ignore:
Timestamp:
07/14/2026 04:35:26 AM (3 months ago)
Author:
westonruter
Message:

Code Modernization: Add a polyfill for PHP 8.6's clamp().

Introduce a clamp() polyfill in compat.php so that core, plugins, and themes can call the function on every supported PHP version without a version check. It returns the value when it lies within the bounds and the nearest bound otherwise, and throws a ValueError when $min is greater than $max or when either bound is NAN. On PHP 7.x, an InvalidArgumentException is thrown instead, since the ValueError class does not yet exist.

Adopt the function at the two places in core that were clamping via a nested min( max( … ) ): the oEmbed response width in get_oembed_response_data(), and the minimum font size factor in wp_get_typography_font_size_value().

Additionally, fix PHPStan errors in code that touches the newly-introduced clamp() usage. Add types to wp_get_typography_value_and_unit() and refactor the parsing to allow static analysis to identify the shape of $matches and avoid issues with passing special regex characters in units.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11669.
Follow-up to r35436, r54260, r55947.

Props soean, mukesh27, westonruter.
See #64898.
Fixes #65143.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/embed.php

    r62521 r62719  
    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
Note: See TracChangeset for help on using the changeset viewer.