From bce851dcf25439a74c3f7ce42bb784c84ca8f3cc Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 19 Mar 2015 03:56:27 +0000 Subject: [PATCH] Replace `array_shift()` with `reset()` where appropriate for performance. Props SergeyBiryukov. Fixes #31259. Built from https://develop.svn.wordpress.org/trunk@31829 git-svn-id: http://core.svn.wordpress.org/trunk@31811 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 4 ++-- wp-admin/includes/class-wp-upgrader.php | 2 +- wp-admin/includes/media.php | 6 +++--- wp-admin/includes/menu.php | 4 ++-- wp-admin/includes/ms.php | 4 ++-- wp-admin/includes/plugin-install.php | 4 ++-- wp-admin/user-edit.php | 2 +- wp-admin/widgets.php | 2 +- wp-includes/pomo/streams.php | 2 +- wp-includes/post-formats.php | 2 +- wp-includes/post.php | 2 +- wp-includes/query.php | 2 +- wp-includes/version.php | 2 +- 13 files changed, 19 insertions(+), 19 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index f61c305caa..9c97317a92 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1091,7 +1091,7 @@ function wp_ajax_add_menu_item() { } $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); - $_menu_item = array_shift( $_menu_items ); + $_menu_item = reset( $_menu_items ); // Restore the missing menu item properties $menu_item_data['menu-item-description'] = $_menu_item->description; @@ -1793,7 +1793,7 @@ function wp_ajax_save_widget() { if ( !$multi_number ) wp_die( $error ); - $_POST['widget-' . $id_base] = array( $multi_number => array_shift($settings) ); + $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) ); $widget_id = $id_base . '-' . $multi_number; $sidebar[] = $widget_id; } diff --git a/wp-admin/includes/class-wp-upgrader.php b/wp-admin/includes/class-wp-upgrader.php index 155fe8f016..8f46852d46 100644 --- a/wp-admin/includes/class-wp-upgrader.php +++ b/wp-admin/includes/class-wp-upgrader.php @@ -1904,7 +1904,7 @@ class Language_Pack_Upgrader extends WP_Upgrader { break; case 'plugin': $plugin_data = get_plugins( '/' . $update->slug ); - $plugin_data = array_shift( $plugin_data ); + $plugin_data = reset( $plugin_data ); if ( $plugin_data ) return $plugin_data['Name']; break; diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 0d15ae2c5a..e569040138 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -621,8 +621,8 @@ function media_upload_form_handler() { $errors = null; if ( isset($_POST['send']) ) { - $keys = array_keys($_POST['send']); - $send_id = (int) array_shift($keys); + $keys = array_keys( $_POST['send'] ); + $send_id = (int) reset( $keys ); } if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { @@ -1357,7 +1357,7 @@ function get_media_item( $attachment_id, $args = null ) { $post_mime_types = get_post_mime_types(); $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) ); - $type = array_shift( $keys ); + $type = reset( $keys ); $type_html = ""; $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] ); diff --git a/wp-admin/includes/menu.php b/wp-admin/includes/menu.php index cb6111a7d8..ce90c1ba3c 100644 --- a/wp-admin/includes/menu.php +++ b/wp-admin/includes/menu.php @@ -94,7 +94,7 @@ foreach ( $menu as $id => $data ) { if ( empty($submenu[$data[2]]) ) continue; $subs = $submenu[$data[2]]; - $first_sub = array_shift($subs); + $first_sub = reset( $subs ); $old_parent = $data[2]; $new_parent = $first_sub[2]; /* @@ -163,7 +163,7 @@ foreach ( $menu as $id => $data ) { */ if ( ! empty( $submenu[$data[2]] ) && 1 == count ( $submenu[$data[2]] ) ) { $subs = $submenu[$data[2]]; - $first_sub = array_shift($subs); + $first_sub = reset( $subs ); if ( $data[2] == $first_sub[2] ) unset( $submenu[$data[2]] ); } diff --git a/wp-admin/includes/ms.php b/wp-admin/includes/ms.php index 8ef291a458..ecc9f51f0a 100644 --- a/wp-admin/includes/ms.php +++ b/wp-admin/includes/ms.php @@ -804,11 +804,11 @@ function choose_primary_blog() { userblog_id ); } } elseif ( count( $all_blogs ) == 1 ) { - $blog = array_shift( $all_blogs ); + $blog = reset( $all_blogs ); echo $blog->domain; if ( $primary_blog != $blog->userblog_id ) // Set the primary blog again if it's out of sync with blog list. update_user_meta( get_current_user_id(), 'primary_blog', $blog->userblog_id ); diff --git a/wp-admin/includes/plugin-install.php b/wp-admin/includes/plugin-install.php index 8d3ee90ec0..7ac3b0f019 100644 --- a/wp-admin/includes/plugin-install.php +++ b/wp-admin/includes/plugin-install.php @@ -304,7 +304,7 @@ function install_plugin_install_status($api, $loop = false) { $url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug); } else { $key = array_keys( $installed_plugin ); - $key = array_shift( $key ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers + $key = reset( $key ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers $update_file = $api->slug . '/' . $key; if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ){ $status = 'latest_installed'; @@ -391,7 +391,7 @@ function install_plugin_information() { $section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description'; // Default to the Description tab, Do not translate, API returns English. if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) { $section_titles = array_keys( (array) $api->sections ); - $section = array_shift( $section_titles ); + $section = reset( $section_titles ); } iframe_header( __( 'Plugin Install' ) ); diff --git a/wp-admin/user-edit.php b/wp-admin/user-edit.php index dbc10b3e5d..0f0afaf04f 100644 --- a/wp-admin/user-edit.php +++ b/wp-admin/user-edit.php @@ -315,7 +315,7 @@ do_action( 'personal_options', $profileuser ); roles ), array_keys( get_editable_roles() ) ); -$user_role = array_shift( $user_roles ); +$user_role = reset( $user_roles ); // print the full list of roles with the primary one selected. wp_dropdown_roles($user_role); diff --git a/wp-admin/widgets.php b/wp-admin/widgets.php index 604a369dc2..42ebc1b7e1 100644 --- a/wp-admin/widgets.php +++ b/wp-admin/widgets.php @@ -189,7 +189,7 @@ if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) { if ( isset($_GET['addnew']) ) { // Default to the first sidebar $keys = array_keys( $wp_registered_sidebars ); - $sidebar = array_shift( $keys ); + $sidebar = reset( $keys ); 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/pomo/streams.php b/wp-includes/pomo/streams.php index 8215e2ddaf..8e50ab4eac 100644 --- a/wp-includes/pomo/streams.php +++ b/wp-includes/pomo/streams.php @@ -40,7 +40,7 @@ class POMO_Reader { return false; $endian_letter = ('big' == $this->endian)? 'N' : 'V'; $int = unpack($endian_letter, $bytes); - return array_shift($int); + return reset( $int ); } /** diff --git a/wp-includes/post-formats.php b/wp-includes/post-formats.php index eb2c95ed5c..94a38581c5 100644 --- a/wp-includes/post-formats.php +++ b/wp-includes/post-formats.php @@ -26,7 +26,7 @@ function get_post_format( $post = null ) { if ( empty( $_format ) ) return false; - $format = array_shift( $_format ); + $format = reset( $_format ); return str_replace('post-format-', '', $format->slug ); } diff --git a/wp-includes/post.php b/wp-includes/post.php index 3c174d3544..917515afc2 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4599,7 +4599,7 @@ function get_pages( $args = array() ) { } if ( 1 == count( $post_status ) ) { - $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], array_shift( $post_status ) ); + $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $r['post_type'], reset( $post_status ) ); } else { $post_status = implode( "', '", $post_status ); $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); diff --git a/wp-includes/query.php b/wp-includes/query.php index f076533231..5c1c97cd31 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -4673,7 +4673,7 @@ function wp_old_slug_redirect() { if ( is_array( $post_type ) ) { if ( count( $post_type ) > 1 ) return; - $post_type = array_shift( $post_type ); + $post_type = reset( $post_type ); } // Do not attempt redirect for hierarchical post types diff --git a/wp-includes/version.php b/wp-includes/version.php index 44e10e94e3..e2e5fec167 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-beta1-31828'; +$wp_version = '4.2-beta1-31829'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.