From cf8a19413c95ca0e6abb221c44519d7d3bde017a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Wed, 21 Oct 2026 09:41:29 +0200 Subject: [PATCH] Code Modernization: Use `array_any()` and `array_all()` in more places. This replaces `foreach` loops that return `true` on the first matching element, or `false` on the first failing one, with `array_any()` and `array_all()`. All of them were added after the first round of conversions in [62550] and [62553]. The negated conditions in the `array_all()` callbacks are written out positively, for example `! is_string( $value ) || '' === $value` becomes `is_string( $value ) && '' !== $value`, so the operands are still evaluated in the same order and short-circuit in the same places. WordPress core includes polyfills for `array_any()` and `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], [62553], [62564], [63453]. See #65818. --- src/wp-admin/includes/plugin.php | 8 +------- .../abilities-api/class-wp-ability.php | 7 +------ .../abilities/class-wp-abilities-content.php | 19 +++++-------------- src/wp-includes/class-wp-rewrite.php | 11 ++++------- src/wp-includes/media.php | 8 +------- .../class-wp-secrets-config-key-provider.php | 11 ++++------- 6 files changed, 16 insertions(+), 48 deletions(-) diff --git a/src/wp-admin/includes/plugin.php b/src/wp-admin/includes/plugin.php index c9830eb1a947e..821fcbb1bfebf 100644 --- a/src/wp-admin/includes/plugin.php +++ b/src/wp-admin/includes/plugin.php @@ -2205,13 +2205,7 @@ function wp_admin_page_exists(): bool { return true; } - foreach ( (array) $_wp_submenu_nopriv as $nopriv_submenus ) { - if ( isset( $nopriv_submenus[ $plugin_page ] ) ) { - return true; - } - } - - return false; + return array_any( (array) $_wp_submenu_nopriv, fn( $nopriv_submenus ) => isset( $nopriv_submenus[ $plugin_page ] ) ); } /** diff --git a/src/wp-includes/abilities-api/class-wp-ability.php b/src/wp-includes/abilities-api/class-wp-ability.php index c7e32ad8efd64..26c8e0b8be61c 100644 --- a/src/wp-includes/abilities-api/class-wp-ability.php +++ b/src/wp-includes/abilities-api/class-wp-ability.php @@ -638,12 +638,7 @@ private static function is_valid_capability_list( $capabilities ): bool { return false; } - foreach ( $capabilities as $capability ) { - if ( ! is_string( $capability ) || '' === trim( $capability ) ) { - return false; - } - } - return true; + return array_all( $capabilities, fn( $capability ) => is_string( $capability ) && '' !== trim( $capability ) ); } /** diff --git a/src/wp-includes/abilities/class-wp-abilities-content.php b/src/wp-includes/abilities/class-wp-abilities-content.php index 725301fd6a0f3..41d04d37d664d 100644 --- a/src/wp-includes/abilities/class-wp-abilities-content.php +++ b/src/wp-includes/abilities/class-wp-abilities-content.php @@ -274,13 +274,10 @@ private function parse_list_input( array $input, string $key ): array { * @return bool True if the requested statuses may be queried. */ private function can_query_statuses( array $where, WP_Post_Type $post_type_object ): bool { - foreach ( $this->get_requested_statuses( $where ) as $status ) { - if ( ! $this->can_query_status( $status, $post_type_object ) ) { - return false; - } - } - - return true; + return array_all( + $this->get_requested_statuses( $where ), + fn( $status ) => $this->can_query_status( $status, $post_type_object ) + ); } /** @@ -718,13 +715,7 @@ private function get_query_total( WP_Query $query, array $query_args, int $page * @return bool True when post meta and term caches should be primed. */ private function should_prime_post_caches( array $fields ): bool { - foreach ( self::CACHE_PRIMING_FIELDS as $field ) { - if ( $this->is_field_requested( $field, $fields ) ) { - return true; - } - } - - return false; + return array_any( self::CACHE_PRIMING_FIELDS, fn( $field ) => $this->is_field_requested( $field, $fields ) ); } /** diff --git a/src/wp-includes/class-wp-rewrite.php b/src/wp-includes/class-wp-rewrite.php index 0c571e4d76000..20967bafa0206 100644 --- a/src/wp-includes/class-wp-rewrite.php +++ b/src/wp-includes/class-wp-rewrite.php @@ -1559,13 +1559,10 @@ public function random_rewrite_rules(): array { $random_base_posts[] = get_page_by_path( $random_slug, OBJECT, 'post' ); } - $random_base_is_taken = false; - foreach ( $random_base_posts as $random_base_post ) { - if ( $random_base_post instanceof WP_Post && is_post_publicly_viewable( $random_base_post ) ) { - $random_base_is_taken = true; - break; - } - } + $random_base_is_taken = array_any( + $random_base_posts, + fn( $random_base_post ) => $random_base_post instanceof WP_Post && is_post_publicly_viewable( $random_base_post ) + ); if ( ! $random_base_is_taken ) { $rules[ $random_base . '/?$' ] = $this->index . '?random=1'; diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 351d4c3f71597..1056037279818 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -7018,12 +7018,6 @@ function wp_is_cross_origin_isolated_request(): bool { return true; } - foreach ( headers_list() as $header ) { - if ( 0 === stripos( $header, 'Document-Isolation-Policy:' ) ) { - return true; - } - } - - return false; + return array_any( headers_list(), fn( $header ) => 0 === stripos( $header, 'Document-Isolation-Policy:' ) ); } diff --git a/src/wp-includes/secrets/class-wp-secrets-config-key-provider.php b/src/wp-includes/secrets/class-wp-secrets-config-key-provider.php index 7fbd208818ea6..c99adc925ecd0 100644 --- a/src/wp-includes/secrets/class-wp-secrets-config-key-provider.php +++ b/src/wp-includes/secrets/class-wp-secrets-config-key-provider.php @@ -261,13 +261,10 @@ private function get_site_key() { * @phpstan-assert-if-true non-empty-string $logged_in_salt */ private function are_usable_salt_values( $logged_in_key, $logged_in_salt ): bool { - foreach ( array( $logged_in_key, $logged_in_salt ) as $value ) { - if ( ! is_string( $value ) || '' === $value || self::KNOWN_PLACEHOLDER === $value ) { - return false; - } - } - - return true; + return array_all( + array( $logged_in_key, $logged_in_salt ), + fn( $value ) => is_string( $value ) && '' !== $value && self::KNOWN_PLACEHOLDER !== $value + ); } /**