Skip to content

Commit fb78023

Browse files
committed
Code Modernization: Use array_any() and array_all() more widely.
This replaces `foreach` loops that return early on the first matching or failing element with `array_any()` and `array_all()`, with no change in behavior. These loops were all added in later features, so they were missed by the conversions in r62550 and r62553. Developed in #14618. Follow-up to r59783, r62550, r62553, r62564, r63453, r64337, r64351, r64388, r64402, r64500, r64588. Props soean. See #65818. git-svn-id: https://develop.svn.wordpress.org/trunk@64690 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 77393df commit fb78023

6 files changed

Lines changed: 28 additions & 48 deletions

File tree

‎src/wp-admin/includes/plugin.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,13 +2205,10 @@ function wp_admin_page_exists(): bool {
22052205
return true;
22062206
}
22072207

2208-
foreach ( (array) $_wp_submenu_nopriv as $nopriv_submenus ) {
2209-
if ( isset( $nopriv_submenus[ $plugin_page ] ) ) {
2210-
return true;
2211-
}
2212-
}
2213-
2214-
return false;
2208+
return array_any(
2209+
(array) $_wp_submenu_nopriv,
2210+
fn( $nopriv_submenus ) => isset( $nopriv_submenus[ $plugin_page ] )
2211+
);
22152212
}
22162213

22172214
/**

‎src/wp-includes/abilities-api/class-wp-ability.php‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,10 @@ private static function is_valid_capability_list( $capabilities ): bool {
641641
return false;
642642
}
643643

644-
foreach ( $capabilities as $capability ) {
645-
if ( ! is_string( $capability ) || '' === trim( $capability ) ) {
646-
return false;
647-
}
648-
}
649-
return true;
644+
return array_all(
645+
$capabilities,
646+
fn( $capability ) => is_string( $capability ) && '' !== trim( $capability )
647+
);
650648
}
651649

652650
/**

‎src/wp-includes/abilities/class-wp-abilities-content.php‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,10 @@ private function parse_list_input( array $input, string $key ): array {
282282
* @return bool True if the requested statuses may be queried.
283283
*/
284284
private function can_query_statuses( array $where, WP_Post_Type $post_type_object ): bool {
285-
foreach ( $this->get_requested_statuses( $where ) as $status ) {
286-
if ( ! $this->can_query_status( $status, $post_type_object ) ) {
287-
return false;
288-
}
289-
}
290-
291-
return true;
285+
return array_all(
286+
$this->get_requested_statuses( $where ),
287+
fn( $status ) => $this->can_query_status( $status, $post_type_object )
288+
);
292289
}
293290

294291
/**
@@ -790,13 +787,10 @@ private function get_query_total( WP_Query $query, array $query_args, int $page
790787
* @return bool True when post meta and term caches should be primed.
791788
*/
792789
private function should_prime_post_caches( array $fields ): bool {
793-
foreach ( self::CACHE_PRIMING_FIELDS as $field ) {
794-
if ( $this->is_field_requested( $field, $fields ) ) {
795-
return true;
796-
}
797-
}
798-
799-
return false;
790+
return array_any(
791+
self::CACHE_PRIMING_FIELDS,
792+
fn( $field ) => $this->is_field_requested( $field, $fields )
793+
);
800794
}
801795

802796
/**

‎src/wp-includes/class-wp-rewrite.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,13 +1559,10 @@ public function random_rewrite_rules(): array {
15591559
$random_base_posts[] = get_page_by_path( $random_slug, OBJECT, 'post' );
15601560
}
15611561

1562-
$random_base_is_taken = false;
1563-
foreach ( $random_base_posts as $random_base_post ) {
1564-
if ( $random_base_post instanceof WP_Post && is_post_publicly_viewable( $random_base_post ) ) {
1565-
$random_base_is_taken = true;
1566-
break;
1567-
}
1568-
}
1562+
$random_base_is_taken = array_any(
1563+
$random_base_posts,
1564+
fn( $random_base_post ) => $random_base_post instanceof WP_Post && is_post_publicly_viewable( $random_base_post )
1565+
);
15691566

15701567
if ( ! $random_base_is_taken ) {
15711568
$rules[ $random_base . '/?$' ] = $this->index . '?random=1';

‎src/wp-includes/media.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7018,12 +7018,9 @@ function wp_is_cross_origin_isolated_request(): bool {
70187018
return true;
70197019
}
70207020

7021-
foreach ( headers_list() as $header ) {
7022-
if ( 0 === stripos( $header, 'Document-Isolation-Policy:' ) ) {
7023-
return true;
7024-
}
7025-
}
7026-
7027-
return false;
7021+
return array_any(
7022+
headers_list(),
7023+
fn( $header ) => 0 === stripos( $header, 'Document-Isolation-Policy:' )
7024+
);
70287025
}
70297026

‎src/wp-includes/secrets/class-wp-secrets-config-key-provider.php‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,10 @@ private function get_site_key() {
261261
* @phpstan-assert-if-true non-empty-string $logged_in_salt
262262
*/
263263
private function are_usable_salt_values( $logged_in_key, $logged_in_salt ): bool {
264-
foreach ( array( $logged_in_key, $logged_in_salt ) as $value ) {
265-
if ( ! is_string( $value ) || '' === $value || self::KNOWN_PLACEHOLDER === $value ) {
266-
return false;
267-
}
268-
}
269-
270-
return true;
264+
return array_all(
265+
array( $logged_in_key, $logged_in_salt ),
266+
fn( $value ) => is_string( $value ) && '' !== $value && self::KNOWN_PLACEHOLDER !== $value
267+
);
271268
}
272269

273270
/**

0 commit comments

Comments
 (0)