From 255f03f442ba2fa47a51123627e676dbe73e8111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Mon, 22 Jun 2026 10:01:59 +0200 Subject: [PATCH] Code Modernization: Use array_all() in various places. Replace `foreach` loops that return `false` on the first element failing a condition (and `true` otherwise) with the more expressive `array_all()` function. `array_all()` was introduced in PHP 8.4; a polyfill has been available in core since WordPress 6.8.0 (see `wp-includes/compat.php`), so this is safe on all supported PHP versions. --- src/wp-includes/class-wp-user.php | 8 +------- src/wp-includes/functions.php | 9 +-------- src/wp-includes/rest-api.php | 12 ++++-------- 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/class-wp-user.php b/src/wp-includes/class-wp-user.php index 63c3f8d04bf6a..9ae005069bd03 100644 --- a/src/wp-includes/class-wp-user.php +++ b/src/wp-includes/class-wp-user.php @@ -827,13 +827,7 @@ public function has_cap( $cap, ...$args ) { unset( $capabilities['do_not_allow'] ); // Must have ALL requested caps. - foreach ( (array) $caps as $cap ) { - if ( empty( $capabilities[ $cap ] ) ) { - return false; - } - } - - return true; + return array_all( (array) $caps, fn( $cap ) => ! empty( $capabilities[ $cap ] ) ); } /** diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 355d9f8a1ec37..8a86368e6bdda 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -5317,14 +5317,7 @@ function wp_is_numeric_array( $data ): bool { if ( ! is_array( $data ) ) { return false; } - - foreach ( $data as $key => $value ) { - if ( is_string( $key ) ) { - return false; - } - } - - return true; + return array_all( $data, fn( $value, $key ) => ! is_string( $key ) ); } /** diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index a4c22e8f1cca1..1a74059942c54 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -2090,14 +2090,10 @@ function rest_are_values_equal( $value1, $value2 ) { if ( count( $value1 ) !== count( $value2 ) ) { return false; } - - foreach ( $value1 as $index => $value ) { - if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) { - return false; - } - } - - return true; + return array_all( + $value1, + fn( $value, $index ) => array_key_exists( $index, $value2 ) && rest_are_values_equal( $value, $value2[ $index ] ) + ); } if ( is_int( $value1 ) && is_float( $value2 )