Make WordPress Core


Ignore:
Timestamp:
06/23/2026 01:53:35 PM (3 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

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

    r62353 r62550  
    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 ) {
Note: See TracChangeset for help on using the changeset viewer.