Skip to content

Commit 3fccf83

Browse files
Code Modernization: Use array_all() where appropriate.
This commit replaces several `foreach` loops that iterate an array, return `false` as soon as an element fails a condition, and otherwise fall through to `true`. That is exactly what PHP 8.4's `array_all()` expresses in a single, more readable call. WordPress core includes a polyfill for `array_all()` on PHP < 8.4 as of WordPress 6.8, so the change works on every supported PHP version without raising the minimum requirement. Follow-up to [59783], [62550]. Props Soean, mukesh27, westonruter, SergeyBiryukov. See #65519. git-svn-id: https://develop.svn.wordpress.org/trunk@62553 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 434c770 commit 3fccf83

3 files changed

Lines changed: 6 additions & 21 deletions

File tree

src/wp-includes/class-wp-user.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -827,13 +827,7 @@ public function has_cap( $cap, ...$args ) {
827827
unset( $capabilities['do_not_allow'] );
828828

829829
// Must have ALL requested caps.
830-
foreach ( (array) $caps as $cap ) {
831-
if ( empty( $capabilities[ $cap ] ) ) {
832-
return false;
833-
}
834-
}
835-
836-
return true;
830+
return array_all( (array) $caps, fn( $cap, $key ) => ! empty( $capabilities[ $cap ] ) );
837831
}
838832

839833
/**

src/wp-includes/functions.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5318,13 +5318,7 @@ function wp_is_numeric_array( $data ): bool {
53185318
return false;
53195319
}
53205320

5321-
foreach ( $data as $key => $value ) {
5322-
if ( is_string( $key ) ) {
5323-
return false;
5324-
}
5325-
}
5326-
5327-
return true;
5321+
return array_all( $data, fn( $value, $key ) => ! is_string( $key ) );
53285322
}
53295323

53305324
/**

src/wp-includes/rest-api.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,13 +2095,10 @@ function rest_are_values_equal( $value1, $value2 ) {
20952095
return false;
20962096
}
20972097

2098-
foreach ( $value1 as $index => $value ) {
2099-
if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) {
2100-
return false;
2101-
}
2102-
}
2103-
2104-
return true;
2098+
return array_all(
2099+
$value1,
2100+
fn( $value, $index ) => array_key_exists( $index, $value2 ) && rest_are_values_equal( $value, $value2[ $index ] )
2101+
);
21052102
}
21062103

21072104
if ( is_int( $value1 ) && is_float( $value2 )

0 commit comments

Comments
 (0)