From 065a43f68d207842636b9bb1f7fa63dbbc76c8be Mon Sep 17 00:00:00 2001 From: desrosj Date: Fri, 7 Feb 2025 18:54:22 +0000 Subject: [PATCH] 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. - `array_find()`: https://www.php.net/manual/en/function.array-find.php - `array_find_key()`: https://www.php.net/manual/en/function.array-find-key.php - `array_all()`: https://www.php.net/manual/en/function.array-all.php - `array_any()`: https://www.php.net/manual/en/function.array-any.php 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. Built from https://develop.svn.wordpress.org/trunk@59783 git-svn-id: http://core.svn.wordpress.org/trunk@59125 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/compat.php | 92 +++++++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 93 insertions(+), 1 deletion(-) 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.