Make WordPress Core


Ignore:
Timestamp:
08/11/2026 01:16:00 PM (7 weeks ago)
Author:
SergeyBiryukov
Message:

Code Quality: Use isset() instead of in_array() over array_keys() for key lookups.

This updates several places in core that test whether an array has a given key:

  • in_array( $key, array_keys( $array ), true ) allocates a complete copy of every key and walks it.
  • isset( $array[ $key ] ), on the other hand, does a single hash lookup in constant time with no allocation.

Note: For arrays that main contain null values, ​array_key_exists() should be used instead.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12929.

Props mukesh27, siliconforks.
Fixes #65842.

File:
1 edited

Legend:

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

    r63001 r63177  
    569569                }
    570570        } else {
    571                 $post    = get_default_post_to_edit( 'post', true );
    572                 $user_id = get_current_user_id();
     571                $post            = get_default_post_to_edit( 'post', true );
     572                $user_id         = get_current_user_id();
     573                $user_blogs      = get_blogs_of_user( $user_id );
     574                $current_blog_id = get_current_blog_id();
    573575
    574576                // Don't create an option if this is a super admin who does not belong to this site.
    575                 if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) {
     577                if ( isset( $user_blogs[ $current_blog_id ] ) ) {
    576578                        update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID.
    577579                }
Note: See TracChangeset for help on using the changeset viewer.