From 783dd4b4f49b04a36c861cc5e18be1b6028b4288 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Tue, 26 Aug 2025 21:31:28 +0000 Subject: [PATCH] General: Add polyfills for new PHP 8.5 array functions: `array_first` and `array_last`. This power couple of function is coming in PHP 8.5: array_first and array_last. For more information on these functions, check out the PHP RFC at https://wiki.php.net/rfc/array_first_last. Props tusharbharti, jorbin, peterwilsoncc, mukesh27, johnbillion. Fixes #63853. See #63061. Built from https://develop.svn.wordpress.org/trunk@60672 git-svn-id: http://core.svn.wordpress.org/trunk@60008 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 f0bdf07974..84e611faee 100644 --- a/wp-includes/compat.php +++ b/wp-includes/compat.php @@ -539,6 +539,48 @@ if ( ! function_exists( 'array_all' ) ) { } } +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 ) ]; + } +} + // 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 2f55d7808b..86820be505 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.9-alpha-60671'; +$wp_version = '6.9-alpha-60672'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.