Make WordPress Core

Changeset 59783


Ignore:
Timestamp:
02/07/2025 06:52:54 PM (4 hours ago)
Author:
desrosj
Message:

General: Introduce polyfills for new array related functions in PHP 8.4.

PHP 8.4 introduced four new functions to provide a common way to more easily perform common operations on arrays.

These functions are now polyfilled making them available on all supported versions of PHP (currently 7.2+).

Props Soean, swissspidy, TobiasBg, ayeshrajans, mukesh27, joemcgill.
Fixes #62558.

Location:
trunk
Files:
4 added
1 edited

Legend:

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

    r59453 r59783  
    550550}
    551551
     552if ( ! function_exists( 'array_find' ) ) {
     553    /**
     554     * Polyfill for `array_find()` function added in PHP 8.4.
     555     *
     556     * Searches an array for the first element that passes a given callback.
     557     *
     558     * @since 6.8.0
     559     *
     560     * @param array    $array    The array to search.
     561     * @param callable $callback The callback to run for each element.
     562     * @return mixed|null The first element in the array that passes the `$callback`, otherwise null.
     563     */
     564    function array_find( array $array, callable $callback ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
     565        foreach ( $array as $key => $value ) {
     566            if ( $callback( $value, $key ) ) {
     567                return $value;
     568            }
     569        }
     570
     571        return null;
     572    }
     573}
     574
     575if ( ! function_exists( 'array_find_key' ) ) {
     576    /**
     577     * Polyfill for `array_find_key()` function added in PHP 8.4.
     578     *
     579     * Searches an array for the first key that passes a given callback.
     580     *
     581     * @since 6.8.0
     582     *
     583     * @param array    $array    The array to search.
     584     * @param callable $callback The callback to run for each element.
     585     * @return int|string|null The first key in the array that passes the `$callback`, otherwise null.
     586     */
     587    function array_find_key( array $array, callable $callback ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
     588        foreach ( $array as $key => $value ) {
     589            if ( $callback( $value, $key ) ) {
     590                return $key;
     591            }
     592        }
     593
     594        return null;
     595    }
     596}
     597
     598if ( ! function_exists( 'array_any' ) ) {
     599    /**
     600     * Polyfill for `array_any()` function added in PHP 8.4.
     601     *
     602     * Checks if any element of an array passes a given callback.
     603     *
     604     * @since 6.8.0
     605     *
     606     * @param array    $array    The array to check.
     607     * @param callable $callback The callback to run for each element.
     608     * @return bool True if any element in the array passes the `$callback`, otherwise false.
     609     */
     610    function array_any( array $array, callable $callback ): bool { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
     611        foreach ( $array as $key => $value ) {
     612            if ( $callback( $value, $key ) ) {
     613                return true;
     614            }
     615        }
     616
     617        return false;
     618    }
     619}
     620
     621if ( ! function_exists( 'array_all' ) ) {
     622    /**
     623     * Polyfill for `array_all()` function added in PHP 8.4.
     624     *
     625     * Checks if all elements of an array pass a given callback.
     626     *
     627     * @since 6.8.0
     628     *
     629     * @param array    $array    The array to check.
     630     * @param callable $callback The callback to run for each element.
     631     * @return bool True if all elements in the array pass the `$callback`, otherwise false.
     632     */
     633    function array_all( array $array, callable $callback ): bool { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
     634        foreach ( $array as $key => $value ) {
     635            if ( ! $callback( $value, $key ) ) {
     636                return false;
     637            }
     638        }
     639
     640        return true;
     641    }
     642}
     643
    552644// IMAGETYPE_AVIF constant is only defined in PHP 8.x or later.
    553645if ( ! defined( 'IMAGETYPE_AVIF' ) ) {
Note: See TracChangeset for help on using the changeset viewer.