Commit c486d4e665 for wordpress.org
commit c486d4e66520b09a9b01d9e0fa634a9758e87e74
Author: SergeyBiryukov <sergeybiryukov@git.wordpress.org>
Date: Wed Sep 9 19:50:46 2026 +0000
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.
Built from https://develop.svn.wordpress.org/trunk@63567
git-svn-id: http://core.svn.wordpress.org/trunk@62743 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-admin/import.php b/wp-admin/import.php
index ce17a70f20..98ecd9bb38 100644
--- a/wp-admin/import.php
+++ b/wp-admin/import.php
@@ -118,8 +118,7 @@ if ( empty( $importers ) ) {
// Looks like an importer is installed, but not active.
$plugins = get_plugins( '/' . $plugin_slug );
if ( ! empty( $plugins ) ) {
- $keys = array_keys( $plugins );
- $plugin_file = $plugin_slug . '/' . $keys[0];
+ $plugin_file = $plugin_slug . '/' . array_key_first( $plugins );
$url = wp_nonce_url(
add_query_arg(
array(
diff --git a/wp-admin/includes/class-plugin-upgrader.php b/wp-admin/includes/class-plugin-upgrader.php
index f5290ff525..5361c63821 100644
--- a/wp-admin/includes/class-plugin-upgrader.php
+++ b/wp-admin/includes/class-plugin-upgrader.php
@@ -542,9 +542,7 @@ class Plugin_Upgrader extends WP_Upgrader {
}
// Assume the requested plugin is the first in the list.
- $plugin_files = array_keys( $plugin );
-
- return $this->result['destination_name'] . '/' . $plugin_files[0];
+ return $this->result['destination_name'] . '/' . array_key_first( $plugin );
}
/**
diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php
index be10d2f7bf..beaf88101e 100644
--- a/wp-admin/includes/media.php
+++ b/wp-admin/includes/media.php
@@ -796,8 +796,7 @@ function media_upload_form_handler() {
$errors = null;
if ( isset( $_POST['send'] ) ) {
- $keys = array_keys( $_POST['send'] );
- $send_id = (int) reset( $keys );
+ $send_id = (int) array_key_first( $_POST['send'] );
}
if ( ! empty( $_POST['attachments'] ) ) {
@@ -1685,8 +1684,8 @@ function get_media_item( $attachment_id, $args = null ) {
$title = esc_attr( $post->post_title );
$post_mime_types = get_post_mime_types();
- $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
- $type = reset( $keys );
+ $matched_types = wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type );
+ $type = array_key_first( $matched_types ) ?? '';
$type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
$form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] );
diff --git a/wp-admin/includes/plugin-install.php b/wp-admin/includes/plugin-install.php
index 62e93a36ee..b37b956d34 100644
--- a/wp-admin/includes/plugin-install.php
+++ b/wp-admin/includes/plugin-install.php
@@ -593,8 +593,7 @@ function install_plugin_information() {
// Default to the Description tab, Do not translate, API returns English.
$section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description';
if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
- $section_titles = array_keys( (array) $api->sections );
- $section = reset( $section_titles );
+ $section = array_key_first( (array) $api->sections );
}
iframe_header( __( 'Plugin Installation' ) );
diff --git a/wp-admin/includes/revision.php b/wp-admin/includes/revision.php
index 2f15f1c9e2..ec58011f7a 100644
--- a/wp-admin/includes/revision.php
+++ b/wp-admin/includes/revision.php
@@ -325,8 +325,7 @@ function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null
if ( ! $compare_two_mode ) {
$found = array_search( $selected_revision_id, array_keys( $revisions ), true );
if ( $found ) {
- $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
- $from = reset( $from );
+ $from = array_key_first( array_slice( $revisions, $found - 1, 1, true ) );
} else {
$from = 0;
}
diff --git a/wp-admin/plugin-editor.php b/wp-admin/plugin-editor.php
index 4beee7b840..b129a7652b 100644
--- a/wp-admin/plugin-editor.php
+++ b/wp-admin/plugin-editor.php
@@ -71,8 +71,7 @@ if ( empty( $plugin ) ) {
$plugin = $file;
}
} else {
- $plugin = array_keys( $plugins );
- $plugin = $plugin[0];
+ $plugin = array_key_first( $plugins );
}
}
diff --git a/wp-admin/widgets-form.php b/wp-admin/widgets-form.php
index e47905c5d0..25ee8215b9 100644
--- a/wp-admin/widgets-form.php
+++ b/wp-admin/widgets-form.php
@@ -229,8 +229,7 @@ if ( isset( $_GET['editwidget'] ) && $_GET['editwidget'] ) {
if ( isset( $_GET['addnew'] ) ) {
// Default to the first sidebar.
- $keys = array_keys( $wp_registered_sidebars );
- $sidebar = reset( $keys );
+ $sidebar = array_key_first( $wp_registered_sidebars );
if ( isset( $_GET['base'] ) && isset( $_GET['num'] ) ) { // Multi-widget.
// Copy minimal info from an existing instance of this widget to a new instance.
diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php
index 9385ae832f..1d6b652c5a 100644
--- a/wp-includes/class-wp-query.php
+++ b/wp-includes/class-wp-query.php
@@ -4028,9 +4028,8 @@ class WP_Query {
} else {
// For other tax queries, grab the first term from the first clause.
if ( ! empty( $this->tax_query->queried_terms ) ) {
- $queried_taxonomies = array_keys( $this->tax_query->queried_terms );
- $matched_taxonomy = reset( $queried_taxonomies );
- $query = $this->tax_query->queried_terms[ $matched_taxonomy ];
+ $matched_taxonomy = array_key_first( $this->tax_query->queried_terms );
+ $query = $this->tax_query->queried_terms[ $matched_taxonomy ];
if ( ! empty( $query['terms'] ) ) {
if ( 'term_id' === $query['field'] ) {
diff --git a/wp-includes/cron.php b/wp-includes/cron.php
index 82889d7cc7..1070ae4680 100644
--- a/wp-includes/cron.php
+++ b/wp-includes/cron.php
@@ -929,8 +929,7 @@ function spawn_cron( $gmt_time = 0 ) {
return false;
}
- $keys = array_keys( $crons );
- if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
+ if ( array_key_first( $crons ) > $gmt_time ) {
return false;
}
@@ -1059,8 +1058,7 @@ function _wp_cron() {
}
$gmt_time = microtime( true );
- $keys = array_keys( $crons );
- if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
+ if ( array_key_first( $crons ) > $gmt_time ) {
return 0;
}
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php
index c2c92efbbc..31267f9600 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php
@@ -200,9 +200,7 @@ class WP_REST_Block_Directory_Controller extends WP_REST_Controller {
return '';
}
- $plugin_files = array_keys( $plugin_files );
-
- return $slug . '/' . reset( $plugin_files );
+ return $slug . '/' . array_key_first( $plugin_files );
}
/**
diff --git a/wp-includes/version.php b/wp-includes/version.php
index bce20f106f..59603dcb8e 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-63566';
+$wp_version = '7.2-alpha-63567';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
diff --git a/wp-includes/widgets/class-wp-widget-tag-cloud.php b/wp-includes/widgets/class-wp-widget-tag-cloud.php
index c56a774b11..aeb3c09ce8 100644
--- a/wp-includes/widgets/class-wp-widget-tag-cloud.php
+++ b/wp-includes/widgets/class-wp-widget-tag-cloud.php
@@ -169,8 +169,7 @@ class WP_Widget_Tag_Cloud extends WP_Widget {
// Just a single tag cloud supporting taxonomy found, no need to display a select.
case 1:
- $keys = array_keys( $taxonomies );
- $taxonomy = reset( $keys );
+ $taxonomy = array_key_first( $taxonomies );
?>
<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 ); ?>" />
<?php