Make WordPress Core

Changeset 64690


Ignore:
Timestamp:
10/23/2026 05:58:31 PM (5 hours ago)
Author:
westonruter
Message:

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 ​https://github.com/WordPress/wordpress-develop/pull/14618.
Follow-up to r59783, r62550, r62553, r62564, r63453, r64337, r64351, r64388, r64402, r64500, r64588.

Props soean.
See #65818.

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/plugin.php

    r64583 r64690  
    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
  • trunk/src/wp-includes/abilities-api/class-wp-ability.php

    r64685 r64690  
    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
  • trunk/src/wp-includes/abilities/class-wp-abilities-content.php

    r64688 r64690  
    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
    … …  
    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
  • trunk/src/wp-includes/class-wp-rewrite.php

    r64402 r64690  
    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 ) {
  • trunk/src/wp-includes/media.php

    r64588 r64690  
    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;
    7028 }
    7029 
     7021        return array_any(
     7022                headers_list(),
     7023                fn( $header ) => 0 === stripos( $header, 'Document-Isolation-Policy:' )
     7024        );
     7025}
     7026
  • trunk/src/wp-includes/secrets/class-wp-secrets-config-key-provider.php

    r64337 r64690  
    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
Note: See TracChangeset for help on using the changeset viewer.