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
This commit is contained in:
Aaron Jorbin
2025-08-26 21:31:28 +00:00
parent d58780fc7d
commit 783dd4b4f4
2 changed files with 43 additions and 1 deletions

View File

@@ -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 );

View File

@@ -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.