Make WordPress Core

Changeset 63567 for trunk


Ignore:
Timestamp:
09/09/2026 07:49:38 PM (2 days ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use array_key_first() to read the first key of an array.

Reading the first key of an array through array_keys() allocates a full array of every key just to keep one entry and discard the rest. array_key_first() reads the first bucket directly.

Follow-up to r63297 / #65773, which is scoped to the two nested current( array_keys( $array ) ) call sites. This commit covers a second shape of the same idea, which that ticket does not include: the key array is assigned to a variable first, then read on the next line.

$keys    = array_keys( $wp_registered_sidebars );
$sidebar = reset( $keys );
$sidebar = array_key_first( $wp_registered_sidebars );

13 occurrences across 11 files, in two shapes — reset( $keys ) and $keys[0]. In every case the intermediate variable existed only to carry the key array to the next line and is never read again afterwards.

One change that goes further

In spawn_cron() and _wp_cron() the surrounding check is dropped too:

$keys = array_keys( $crons );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
if ( array_key_first( $crons ) > $gmt_time ) {

Both functions return early a few lines above when $crons is empty, so isset( $keys[0] ) can never be false there.

Behavior notes

  • On an empty array reset() returns false while array_key_first() returns null. None of the touched call sites compares the result with ===, and most sit behind an empty() guard.
  • Where the old code used $keys[0], the new code is strictly safer: $keys[0] emitted a notice on an empty array, array_key_first() returns null quietly.

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

Follow-up to r63297.

Props Soean, mukesh27.
See #65773.

Location:
trunk/src
Files:
11 edited

Legend:

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

    r59868 r63567  
    119119                                $plugins = get_plugins( '/' . $plugin_slug );
    120120                                if ( ! empty( $plugins ) ) {
    121                                         $keys        = array_keys( $plugins );
    122                                         $plugin_file = $plugin_slug . '/' . $keys[0];
     121                                        $plugin_file = $plugin_slug . '/' . array_key_first( $plugins );
    123122                                        $url         = wp_nonce_url(
    124123                                                add_query_arg(
  • trunk/src/wp-admin/includes/class-plugin-upgrader.php

    r61455 r63567  
    543543
    544544                // Assume the requested plugin is the first in the list.
    545                 $plugin_files = array_keys( $plugin );
    546 
    547                 return $this->result['destination_name'] . '/' . $plugin_files[0];
     545                return $this->result['destination_name'] . '/' . array_key_first( $plugin );
    548546        }
    549547
  • trunk/src/wp-admin/includes/media.php

    r63497 r63567  
    797797
    798798        if ( isset( $_POST['send'] ) ) {
    799                 $keys    = array_keys( $_POST['send'] );
    800                 $send_id = (int) reset( $keys );
     799                $send_id = (int) array_key_first( $_POST['send'] );
    801800        }
    802801
     
    16861685
    16871686        $post_mime_types = get_post_mime_types();
    1688         $keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
    1689         $type            = reset( $keys );
     1687        $matched_types   = wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type );
     1688        $type            = array_key_first( $matched_types ) ?? '';
    16901689        $type_html       = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
    16911690
  • trunk/src/wp-admin/includes/plugin-install.php

    r63494 r63567  
    594594        $section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description';
    595595        if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
    596                 $section_titles = array_keys( (array) $api->sections );
    597                 $section        = reset( $section_titles );
     596                $section = array_key_first( (array) $api->sections );
    598597        }
    599598
  • trunk/src/wp-admin/includes/revision.php

    r62273 r63567  
    326326                $found = array_search( $selected_revision_id, array_keys( $revisions ), true );
    327327                if ( $found ) {
    328                         $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
    329                         $from = reset( $from );
     328                        $from = array_key_first( array_slice( $revisions, $found - 1, 1, true ) );
    330329                } else {
    331330                        $from = 0;
  • trunk/src/wp-admin/plugin-editor.php

    r62167 r63567  
    7272                }
    7373        } else {
    74                 $plugin = array_keys( $plugins );
    75                 $plugin = $plugin[0];
     74                $plugin = array_key_first( $plugins );
    7675        }
    7776}
  • trunk/src/wp-admin/widgets-form.php

    r62649 r63567  
    230230        if ( isset( $_GET['addnew'] ) ) {
    231231                // Default to the first sidebar.
    232                 $keys    = array_keys( $wp_registered_sidebars );
    233                 $sidebar = reset( $keys );
     232                $sidebar = array_key_first( $wp_registered_sidebars );
    234233
    235234                if ( isset( $_GET['base'] ) && isset( $_GET['num'] ) ) { // Multi-widget.
  • trunk/src/wp-includes/class-wp-query.php

    r63358 r63567  
    40294029                                // For other tax queries, grab the first term from the first clause.
    40304030                                if ( ! empty( $this->tax_query->queried_terms ) ) {
    4031                                         $queried_taxonomies = array_keys( $this->tax_query->queried_terms );
    4032                                         $matched_taxonomy   = reset( $queried_taxonomies );
    4033                                         $query              = $this->tax_query->queried_terms[ $matched_taxonomy ];
     4031                                        $matched_taxonomy = array_key_first( $this->tax_query->queried_terms );
     4032                                        $query            = $this->tax_query->queried_terms[ $matched_taxonomy ];
    40344033
    40354034                                        if ( ! empty( $query['terms'] ) ) {
  • trunk/src/wp-includes/cron.php

    r63491 r63567  
    930930        }
    931931
    932         $keys = array_keys( $crons );
    933         if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
     932        if ( array_key_first( $crons ) > $gmt_time ) {
    934933                return false;
    935934        }
     
    10601059
    10611060        $gmt_time = microtime( true );
    1062         $keys     = array_keys( $crons );
    1063         if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
     1061        if ( array_key_first( $crons ) > $gmt_time ) {
    10641062                return 0;
    10651063        }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php

    r61429 r63567  
    201201                }
    202202
    203                 $plugin_files = array_keys( $plugin_files );
    204 
    205                 return $slug . '/' . reset( $plugin_files );
     203                return $slug . '/' . array_key_first( $plugin_files );
    206204        }
    207205
  • trunk/src/wp-includes/widgets/class-wp-widget-tag-cloud.php

    r51967 r63567  
    170170                        // Just a single tag cloud supporting taxonomy found, no need to display a select.
    171171                        case 1:
    172                                 $keys     = array_keys( $taxonomies );
    173                                 $taxonomy = reset( $keys );
     172                                $taxonomy = array_key_first( $taxonomies );
    174173                                ?>
    175174                                <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="<?php echo esc_attr( $taxonomy ); ?>" />
Note: See TracChangeset for help on using the changeset viewer.