Skip to content

Commit a1314bb

Browse files
Code Modernization: Use array_any() where appropriate.
This commit replaces several `foreach` loops that iterate an array, return `true` as soon as an element matches a condition, and otherwise fall through to `false`. That is exactly what PHP 8.4's `array_any()` expresses in a single, more readable call. WordPress core includes a polyfill for `array_any()` 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]. Props Soean, mukesh27, SergeyBiryukov. See #65519. git-svn-id: https://develop.svn.wordpress.org/trunk@62550 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 859d7fe commit a1314bb

7 files changed

Lines changed: 8 additions & 47 deletions

File tree

src/wp-admin/includes/class-wp-site-health.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,13 +3834,7 @@ public function should_suggest_persistent_object_cache() {
38343834
'users_count' => $wpdb->users,
38353835
);
38363836

3837-
foreach ( $threshold_map as $threshold => $table ) {
3838-
if ( $thresholds[ $threshold ] <= $results[ $table ]->rows ) {
3839-
return true;
3840-
}
3841-
}
3842-
3843-
return false;
3837+
return array_any( $threshold_map, fn( $table, $threshold ) => $thresholds[ $threshold ] <= $results[ $table ]->rows );
38443838
}
38453839

38463840
/**

src/wp-admin/includes/post.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,13 +1980,8 @@ function wp_create_post_autosave( $post_data ) {
19801980
$post = get_post( $post_id );
19811981

19821982
// If the new autosave has the same content as the post, delete the autosave.
1983-
$autosave_is_different = false;
1984-
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
1985-
if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
1986-
$autosave_is_different = true;
1987-
break;
1988-
}
1989-
}
1983+
$fields = array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) );
1984+
$autosave_is_different = array_any( $fields, fn( $field ) => normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) );
19901985

19911986
if ( ! $autosave_is_different ) {
19921987
wp_delete_post_revision( $old_autosave->ID );

src/wp-includes/block-template-utils.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,13 +1461,7 @@ function block_footer_area() {
14611461
function wp_is_theme_directory_ignored( $path ) {
14621462
$directories_to_ignore = array( '.DS_Store', '.svn', '.git', '.hg', '.bzr', 'node_modules', 'vendor' );
14631463

1464-
foreach ( $directories_to_ignore as $directory ) {
1465-
if ( str_starts_with( $path, $directory ) ) {
1466-
return true;
1467-
}
1468-
}
1469-
1470-
return false;
1464+
return array_any( $directories_to_ignore, fn( $directory ) => str_starts_with( $path, $directory ) );
14711465
}
14721466

14731467
/**

src/wp-includes/class-wp-block-processor.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,13 +1598,7 @@ public function opens_block( string ...$block_type ): bool {
15981598
return true;
15991599
}
16001600

1601-
foreach ( $block_type as $block ) {
1602-
if ( $this->is_block_type( $block ) ) {
1603-
return true;
1604-
}
1605-
}
1606-
1607-
return false;
1601+
return array_any( $block_type, fn( $block ) => $this->is_block_type( $block ) );
16081602
}
16091603

16101604
/**

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,7 @@ public function in_default_dir( $src ) {
850850
return false;
851851
}
852852

853-
foreach ( (array) $this->default_dirs as $test ) {
854-
if ( str_starts_with( $src, $test ) ) {
855-
return true;
856-
}
857-
}
858-
return false;
853+
return array_any( (array) $this->default_dirs, fn( $test ) => str_starts_with( $src, $test ) );
859854
}
860855

861856
/**

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,7 @@ public function in_default_dir( $src ) {
459459
return true;
460460
}
461461

462-
foreach ( (array) $this->default_dirs as $test ) {
463-
if ( str_starts_with( $src, $test ) ) {
464-
return true;
465-
}
466-
}
467-
return false;
462+
return array_any( (array) $this->default_dirs, fn( $test ) => str_starts_with( $src, $test ) );
468463
}
469464

470465
/**

src/wp-includes/kses.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,13 +2056,7 @@ function wp_kses_bad_protocol_once2( $scheme, $allowed_protocols ) {
20562056
$scheme = wp_kses_no_null( $scheme );
20572057
$scheme = strtolower( $scheme );
20582058

2059-
$allowed = false;
2060-
foreach ( (array) $allowed_protocols as $one_protocol ) {
2061-
if ( strtolower( $one_protocol ) === $scheme ) {
2062-
$allowed = true;
2063-
break;
2064-
}
2065-
}
2059+
$allowed = array_any( (array) $allowed_protocols, fn( $protocol ) => strtolower( $protocol ) === $scheme );
20662060

20672061
if ( $allowed ) {
20682062
return "$scheme:";

0 commit comments

Comments
 (0)