diff --git a/wp-includes/compat.php b/wp-includes/compat.php index 6a393f7c98..01b9996c75 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -549,6 +549,98 @@ if ( ! function_exists( 'str_ends_with' ) ) { } } +if ( ! function_exists( 'array_find' ) ) { + /** + * Polyfill for `array_find()` function added in PHP 8.4. + * + * Searches an array for the first element 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 mixed|null The first element in the array that passes the `$callback`, otherwise null. + */ + function array_find( array $array, callable $callback ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound + foreach ( $array as $key => $value ) { + 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; + } +} + // IMAGETYPE_AVIF constant is only defined in PHP 8.x or later. if ( ! defined( 'IMAGETYPE_AVIF' ) ) { define( 'IMAGETYPE_AVIF', 19 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 8d6bdcacf8..18b22b8542 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-alpha-59777'; +$wp_version = '6.8-alpha-59783'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.