lue ) { if ( $callback( $value, $key ) ) { return $value; } } return null; } } if ( ! function_exists( 'array_find_key' ) ) { /** * Polyfill for `array_find_key()` function added in PHP 8.4. * * Searches an array for the first key that passes a given callback. * * @since 6.8.0 * * @param array $array The array to search. * @param callable $callback The callback to run for each element. * @return int|string|null The first key in the array that passes the `$callback`, otherwise null. */ function array_find_key( array $array, callable $callback ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound foreach ( $array as $key => $value ) { if ( $callback( $value, $key ) ) { return $key; } } return null; } } if ( ! function_exists( 'array_any' ) ) { /** * Polyfill for `array_any()` function added in PHP 8.4. * * Checks if any element of an array passes a given callback. * * @since 6.8.0 * * @param array $array The array to check. * @param callable $callback The callback to run for each element. * @return bool True if any element in the array passes the `$callback`, otherwise false. */ function array_any( array $array, callable $callback ): bool { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound foreach ( $array as $key => $value ) { if ( $callback( $value, $key ) ) { return true; } } return false; } } if ( ! function_exists( 'array_all' ) ) { /** * Polyfill for `array_all()` function added in PHP 8.4. * * Checks if all elements of an array pass a given callback. * * @since 6.8.0 * * @param array $array The array to check. * @param callable $callback The callback to run for each element. * @return bool True if all elements in the array pass the `$callback`, otherwise false. */ function array_all( array $array, callable $callback ): bool { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound foreach ( $array as $key => $value ) { if ( ! $callback( $value, $key ) ) { return false; } } return true; } } if ( ! function_exists( 'array_first' ) ) { /** * Polyfill for `array_first()` function added in PHP 8.5. * * Returns the first element of an array. * * @since 6.9.0 * * @param array $array The array to get the first element from. * @return mixed|null The first element of the array, or null if the array is empty. */ function array_first( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound if ( empty( $array ) ) { return null; } foreach ( $array as $value ) { return $value; } } } if ( ! function_exists( 'array_last' ) ) { /** * Polyfill for `array_last()` function added in PHP 8.5. * * Returns the last element of an array. * * @since 6.9.0 * * @param array $array The array to get the last element from. * @return mixed|null The last element of the array, or null if the array is empty. */ function array_last( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound if ( empty( $array ) ) { return null; } return $array[ array_key_last( $array ) ]; } } 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. Either `$value`, `$min`, or `$max`. * * @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). * @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). * * @phpstan-template TValue * @phpstan-template TMin * @phpstan-template TMax * @phpstan-param TValue $value * @phpstan-param TMin $min * @phpstan-param TMax $max * @phpstan-return TValue|TMin|TMax */ function clamp( $value, $min, $max ) { $throw_value_error = static function ( string $message ) { // The ValueError class was introduced in PHP 8, so in 7.4 throw InvalidArgumentException instead. if ( ! class_exists( 'ValueError', false ) ) { throw new InvalidArgumentException( $message ); } throw new ValueError( $message ); }; if ( is_float( $min ) && is_nan( $min ) ) { $throw_value_error( 'clamp(): Argument #2 ($min) must not be NAN' ); } if ( is_float( $max ) && is_nan( $max ) ) { $throw_value_error( 'clamp(): Argument #3 ($max) must not be NAN' ); } if ( $max < $min ) { $throw_value_error( 'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)' ); } /* * The upper bound is checked before the lower bound to match the order in PHP's * implementation. Comparison in PHP is not transitive when operands of different * types are mixed (for example, comparing against a bool coerces both operands to * bool), so both bounds can compare as exceeded at once, and the first check wins. */ if ( $value > $max ) { return $max; } if ( $value < $min ) { return $min; } return $value; } } // IMAGETYPE_AVIF constant is only defined in PHP 8.x or later. if ( ! defined( 'IMAGETYPE_AVIF' ) ) { define( 'IMAGETYPE_AVIF', 19 ); } // IMG_AVIF constant is only defined in PHP 8.x or later. if ( ! defined( 'IMG_AVIF' ) ) { define( 'IMG_AVIF', IMAGETYPE_AVIF ); } // IMAGETYPE_HEIF constant is only defined in PHP 8.5 or later. if ( ! defined( 'IMAGETYPE_HEIF' ) ) { define( 'IMAGETYPE_HEIF', 20 ); }