From 10dc403b88cf8076ae7301e305c999bcd1d9fdef Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Mon, 8 Nov 2021 13:51:57 +0000 Subject: [PATCH] General: Introduce polyfills for `array_key_first()` and `array_key_last()` added in PHP 7.3.0. PHP 7.3.0 introduced two new functions: `array_key_first()` and `array_key_last()`. These two functions return the first and last key of each array respectively, without affecting the internal state of the array. The polyfills make these two functions available for use in Core on PHP versions less than 7.3.0. Ref: * PHP RFC https://wiki.php.net/rfc/array_key_first_last * PHP manual `array_key_first()` https://www.php.net/manual/en/function.array-key-first.php * PHP manual `array_key_last()` https://www.php.net/manual/en/function.array-key-last.php Props desrosj, pbearne, costdev, hellofromTonya, ayeshrajans, manzoorwanijk, audrasjb, sergeybiryukov. Fixes #45055. Built from https://develop.svn.wordpress.org/trunk@52038 git-svn-id: http://core.svn.wordpress.org/trunk@51630 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/compat.php | 42 +++++++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/wp-includes/compat.php b/wp-includes/compat.php index b95275455c..2a8664c065 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -375,6 +375,48 @@ if ( ! function_exists( 'is_iterable' ) ) { } } +if ( ! function_exists( 'array_key_first' ) ) { + /** + * Polyfill for array_key_first() function added in PHP 7.3. + * + * Get the first key of the given array without affecting + * the internal array pointer. + * + * @since 5.9.0 + * + * @param array $arr An array. + * @return string|int|null The first key of array if the array + * is not empty; `null` otherwise. + */ + function array_key_first( array $arr ) { + foreach ( $arr as $key => $value ) { + return $key; + } + } +} + +if ( ! function_exists( 'array_key_last' ) ) { + /** + * Polyfill for `array_key_last()` function added in PHP 7.3. + * + * Get the last key of the given array without affecting the + * internal array pointer. + * + * @since 5.9.0 + * + * @param array $arr An array. + * @return string|int|null The last key of array if the array + *. is not empty; `null` otherwise. + */ + function array_key_last( array $arr ) { + if ( empty( $arr ) ) { + return null; + } + end( $arr ); + return key( $arr ); + } +} + // IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later. if ( ! defined( 'IMAGETYPE_WEBP' ) ) { define( 'IMAGETYPE_WEBP', 18 ); diff --git a/wp-includes/version.php b/wp-includes/version.php index fe68e0585f..540af92d17 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52037'; +$wp_version = '5.9-alpha-52038'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.