From 897f004a9c3078fac5b0b0483d3a9fb378117e50 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 8 Oct 2020 21:15:13 +0000 Subject: [PATCH] General: Replace older-style PHP type conversion functions with type casts. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This improves performance, readability, and consistency throughout core. * `intval()` → `(int)` * `strval()` → `(string)` * `floatval()` → `(float)` Props ayeshrajans. Fixes #42918. Built from https://develop.svn.wordpress.org/trunk@49108 git-svn-id: http://core.svn.wordpress.org/trunk@48870 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-header.php | 2 +- wp-admin/async-upload.php | 4 +- wp-admin/edit-comments.php | 2 +- wp-admin/includes/ajax-actions.php | 26 ++++----- .../class-walker-nav-menu-checklist.php | 2 +- wp-admin/includes/class-wp-importer.php | 8 +-- .../includes/class-wp-ms-sites-list-table.php | 6 +- .../class-wp-ms-themes-list-table.php | 2 +- .../includes/class-wp-posts-list-table.php | 12 ++-- .../includes/class-wp-users-list-table.php | 2 +- wp-admin/includes/dashboard.php | 4 +- wp-admin/includes/export.php | 24 ++++---- wp-admin/includes/file.php | 2 +- wp-admin/includes/image-edit.php | 8 +-- wp-admin/includes/media.php | 28 +++++----- wp-admin/includes/nav-menu.php | 4 +- wp-admin/includes/plugin.php | 7 ++- wp-admin/includes/post.php | 4 +- wp-admin/includes/template.php | 8 +-- wp-admin/includes/update.php | 2 +- wp-admin/includes/upgrade.php | 6 +- wp-admin/media-upload.php | 4 +- wp-admin/nav-menus.php | 2 +- wp-admin/network/site-info.php | 2 +- wp-admin/network/site-settings.php | 2 +- wp-admin/network/site-themes.php | 2 +- wp-admin/network/site-users.php | 2 +- wp-admin/network/sites.php | 2 +- wp-admin/network/upgrade.php | 2 +- wp-admin/network/users.php | 2 +- wp-admin/users.php | 4 +- wp-comments-post.php | 2 +- .../twentyfourteen/inc/template-tags.php | 2 +- wp-includes/category-template.php | 4 +- wp-includes/class-http.php | 2 +- wp-includes/class-json.php | 2 +- wp-includes/class-wp-comment-query.php | 4 +- wp-includes/class-wp-customize-manager.php | 4 +- wp-includes/class-wp-customize-nav-menus.php | 16 +++--- wp-includes/class-wp-customize-widgets.php | 10 ++-- wp-includes/class-wp-date-query.php | 22 ++++---- wp-includes/class-wp-http-curl.php | 2 +- wp-includes/class-wp-matchesmapregex.php | 2 +- wp-includes/class-wp-network-query.php | 4 +- wp-includes/class-wp-query.php | 8 +-- wp-includes/class-wp-site-query.php | 4 +- wp-includes/class-wp-term-query.php | 6 +- wp-includes/class-wp-user.php | 4 +- wp-includes/class-wp-xmlrpc-server.php | 30 +++++----- wp-includes/comment-template.php | 4 +- wp-includes/comment.php | 2 +- .../class-wp-customize-date-time-control.php | 6 +- ...ass-wp-customize-nav-menu-item-setting.php | 12 ++-- .../class-wp-customize-nav-menu-section.php | 2 +- .../class-wp-customize-nav-menu-setting.php | 8 +-- wp-includes/formatting.php | 10 ++-- wp-includes/functions.php | 2 +- wp-includes/general-template.php | 10 ++-- wp-includes/http.php | 2 +- wp-includes/link-template.php | 18 +++--- wp-includes/media.php | 56 +++++++++---------- wp-includes/meta.php | 8 +-- wp-includes/ms-functions.php | 4 +- wp-includes/nav-menu.php | 4 +- wp-includes/pluggable.php | 2 +- wp-includes/post.php | 10 ++-- wp-includes/rest-api.php | 4 +- .../class-wp-rest-attachments-controller.php | 8 +-- ...ass-wp-rest-block-directory-controller.php | 6 +- ...lass-wp-rest-block-renderer-controller.php | 4 +- wp-includes/revision.php | 6 +- wp-includes/rewrite.php | 6 +- wp-includes/taxonomy.php | 14 ++--- wp-includes/update.php | 4 +- wp-includes/vars.php | 2 +- wp-includes/version.php | 2 +- wp-includes/widgets/class-wp-widget-links.php | 10 ++-- wp-includes/widgets/class-wp-widget-media.php | 2 +- wp-trackback.php | 4 +- 79 files changed, 273 insertions(+), 274 deletions(-) diff --git a/wp-admin/admin-header.php b/wp-admin/admin-header.php index 35b1686b51..8eddb2b892 100644 --- a/wp-admin/admin-header.php +++ b/wp-admin/admin-header.php @@ -170,7 +170,7 @@ if ( $current_screen->taxonomy ) { $admin_body_class .= ' taxonomy-' . $current_screen->taxonomy; } -$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( get_bloginfo( 'version' ) ) ); +$admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', (float) get_bloginfo( 'version' ) ); $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', get_bloginfo( 'version' ) ) ); $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' ); $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) ); diff --git a/wp-admin/async-upload.php b/wp-admin/async-upload.php index 39f28a6237..d1c9b6e1b2 100644 --- a/wp-admin/async-upload.php +++ b/wp-admin/async-upload.php @@ -39,8 +39,8 @@ if ( ! current_user_can( 'upload_files' ) ) { } // Just fetch the detail form for that attachment. -if ( isset( $_REQUEST['attachment_id'] ) && intval( $_REQUEST['attachment_id'] ) && $_REQUEST['fetch'] ) { - $id = intval( $_REQUEST['attachment_id'] ); +if ( isset( $_REQUEST['attachment_id'] ) && (int) $_REQUEST['attachment_id'] && $_REQUEST['fetch'] ) { + $id = (int) $_REQUEST['attachment_id']; $post = get_post( $id ); if ( 'attachment' !== $post->post_type ) { wp_die( __( 'Invalid post type.' ) ); diff --git a/wp-admin/edit-comments.php b/wp-admin/edit-comments.php index 2ced05ab2d..c2a16eef0b 100644 --- a/wp-admin/edit-comments.php +++ b/wp-admin/edit-comments.php @@ -329,7 +329,7 @@ if ( isset( $_REQUEST['approved'] ) || isset( $_REQUEST['deleted'] ) || isset( $ search_box( __( 'Search Comments' ), 'comment' ); ?> - + diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index ea8502ce98..cd43df1436 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -227,7 +227,7 @@ function wp_ajax_wp_compression_test() { * @since 3.1.0 */ function wp_ajax_imgedit_preview() { - $post_id = intval( $_GET['postid'] ); + $post_id = (int) $_GET['postid']; if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { wp_die( -1 ); } @@ -1954,7 +1954,7 @@ function wp_ajax_menu_quick_search() { */ function wp_ajax_get_permalink() { check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); - $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; + $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; wp_die( get_preview_post_link( $post_id ) ); } @@ -1965,7 +1965,7 @@ function wp_ajax_get_permalink() { */ function wp_ajax_sample_permalink() { check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ); - $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; + $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; $title = isset( $_POST['new_title'] ) ? $_POST['new_title'] : ''; $slug = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null; wp_die( get_sample_permalink_html( $post_id, $title, $slug ) ); @@ -2594,7 +2594,7 @@ function wp_ajax_upload_attachment() { * @since 3.1.0 */ function wp_ajax_image_editor() { - $attachment_id = intval( $_POST['postid'] ); + $attachment_id = (int) $_POST['postid']; if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) { wp_die( -1 ); @@ -2651,12 +2651,12 @@ function wp_ajax_image_editor() { function wp_ajax_set_post_thumbnail() { $json = ! empty( $_REQUEST['json'] ); // New-style request. - $post_ID = intval( $_POST['post_id'] ); + $post_ID = (int) $_POST['post_id']; if ( ! current_user_can( 'edit_post', $post_ID ) ) { wp_die( -1 ); } - $thumbnail_id = intval( $_POST['thumbnail_id'] ); + $thumbnail_id = (int) $_POST['thumbnail_id']; if ( $json ) { check_ajax_referer( "update-post_$post_ID" ); @@ -2687,7 +2687,7 @@ function wp_ajax_set_post_thumbnail() { * @since 4.6.0 */ function wp_ajax_get_post_thumbnail_html() { - $post_ID = intval( $_POST['post_id'] ); + $post_ID = (int) $_POST['post_id']; check_ajax_referer( "update-post_$post_ID" ); @@ -2695,7 +2695,7 @@ function wp_ajax_get_post_thumbnail_html() { wp_die( -1 ); } - $thumbnail_id = intval( $_POST['thumbnail_id'] ); + $thumbnail_id = (int) $_POST['thumbnail_id']; // For backward compatibility, -1 refers to no featured image. if ( -1 === $thumbnail_id ) { @@ -3208,7 +3208,7 @@ function wp_ajax_send_attachment_to_editor() { $attachment = wp_unslash( $_POST['attachment'] ); - $id = intval( $attachment['id'] ); + $id = (int) $attachment['id']; $post = get_post( $id ); if ( ! $post ) { @@ -3221,7 +3221,7 @@ function wp_ajax_send_attachment_to_editor() { if ( current_user_can( 'edit_post', $id ) ) { // If this attachment is unattached, attach it. Primarily a back compat thing. - $insert_into_post_id = intval( $_POST['post_id'] ); + $insert_into_post_id = (int) $_POST['post_id']; if ( 0 == $post->post_parent && $insert_into_post_id ) { wp_update_post( @@ -3622,7 +3622,7 @@ function wp_ajax_parse_embed() { wp_send_json_error(); } - $post_id = isset( $_POST['post_ID'] ) ? intval( $_POST['post_ID'] ) : 0; + $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; if ( $post_id > 0 ) { $post = get_post( $post_id ); @@ -3674,9 +3674,9 @@ function wp_ajax_parse_embed() { // Set $content_width so any embeds fit in the destination iframe. if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) { if ( ! isset( $content_width ) ) { - $content_width = intval( $_POST['maxwidth'] ); + $content_width = (int) $_POST['maxwidth']; } else { - $content_width = min( $content_width, intval( $_POST['maxwidth'] ) ); + $content_width = min( $content_width, (int) $_POST['maxwidth'] ); } } diff --git a/wp-admin/includes/class-walker-nav-menu-checklist.php b/wp-admin/includes/class-walker-nav-menu-checklist.php index 11e78dd529..3f4d6c1574 100644 --- a/wp-admin/includes/class-walker-nav-menu-checklist.php +++ b/wp-admin/includes/class-walker-nav-menu-checklist.php @@ -74,7 +74,7 @@ class Walker_Nav_Menu_Checklist extends Walker_Nav_Menu { public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { global $_nav_menu_placeholder, $nav_menu_selected_id; - $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval( $_nav_menu_placeholder ) - 1 : -1; + $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1; $possible_object_id = isset( $item->post_type ) && 'nav_menu_item' === $item->post_type ? $item->object_id : $_nav_menu_placeholder; $possible_db_id = ( ! empty( $item->ID ) ) && ( 0 < $possible_object_id ) ? (int) $item->ID : 0; diff --git a/wp-admin/includes/class-wp-importer.php b/wp-admin/includes/class-wp-importer.php index e677074a94..b60c783c90 100644 --- a/wp-admin/includes/class-wp-importer.php +++ b/wp-admin/includes/class-wp-importer.php @@ -37,7 +37,7 @@ class WP_Importer { if ( ! empty( $results ) ) { foreach ( $results as $r ) { // Set permalinks into array. - $hashtable[ $r->meta_value ] = intval( $r->post_id ); + $hashtable[ $r->meta_value ] = (int) $r->post_id; } } } while ( count( $results ) == $limit ); @@ -69,7 +69,7 @@ class WP_Importer { $result = $wpdb->get_results( $sql ); if ( ! empty( $result ) ) { - $count = intval( $result[0]->cnt ); + $count = (int) $result[0]->cnt; } // Unset to save memory. @@ -106,11 +106,11 @@ class WP_Importer { foreach ( $results as $r ) { // Explode comment_agent key. list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent ); - $source_comment_id = intval( $source_comment_id ); + $source_comment_id = (int) $source_comment_id; // Check if this comment came from this blog. if ( $bid == $ca_bid ) { - $hashtable[ $source_comment_id ] = intval( $r->comment_ID ); + $hashtable[ $source_comment_id ] = (int) $r->comment_ID; } } } diff --git a/wp-admin/includes/class-wp-ms-sites-list-table.php b/wp-admin/includes/class-wp-ms-sites-list-table.php index e1d15d6337..846ad07489 100644 --- a/wp-admin/includes/class-wp-ms-sites-list-table.php +++ b/wp-admin/includes/class-wp-ms-sites-list-table.php @@ -103,8 +103,8 @@ class WP_MS_Sites_List_Table extends WP_List_Table { } $args = array( - 'number' => intval( $per_page ), - 'offset' => intval( ( $pagenum - 1 ) * $per_page ), + 'number' => (int) $per_page, + 'offset' => (int) ( ( $pagenum - 1 ) * $per_page ), 'network_id' => get_current_network_id(), ); @@ -620,7 +620,7 @@ class WP_MS_Sites_List_Table extends WP_List_Table { $site_status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : ''; foreach ( $this->status_list as $status => $col ) { - if ( ( 1 === intval( $_site->{$status} ) ) && ( $site_status !== $status ) ) { + if ( ( 1 === (int) $_site->{$status} ) && ( $site_status !== $status ) ) { $site_states[ $col[0] ] = $col[1]; } } diff --git a/wp-admin/includes/class-wp-ms-themes-list-table.php b/wp-admin/includes/class-wp-ms-themes-list-table.php index de1b741234..113fad5c43 100644 --- a/wp-admin/includes/class-wp-ms-themes-list-table.php +++ b/wp-admin/includes/class-wp-ms-themes-list-table.php @@ -63,7 +63,7 @@ class WP_MS_Themes_List_Table extends WP_List_Table { $this->is_site_themes = ( 'site-themes-network' === $this->screen->id ) ? true : false; if ( $this->is_site_themes ) { - $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; + $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0; } $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'theme' ) && diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index 01d881cb2e..8ebdcda031 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -89,19 +89,17 @@ class WP_Posts_List_Table extends WP_List_Table { 'show_in_admin_all_list' => false, ) ); - $this->user_posts_count = intval( - $wpdb->get_var( - $wpdb->prepare( - " + $this->user_posts_count = (int) $wpdb->get_var( + $wpdb->prepare( + " SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status NOT IN ( '" . implode( "','", $exclude_states ) . "' ) AND post_author = %d ", - $post_type, - get_current_user_id() - ) + $post_type, + get_current_user_id() ) ); diff --git a/wp-admin/includes/class-wp-users-list-table.php b/wp-admin/includes/class-wp-users-list-table.php index 018654bdd2..ae52238844 100644 --- a/wp-admin/includes/class-wp-users-list-table.php +++ b/wp-admin/includes/class-wp-users-list-table.php @@ -54,7 +54,7 @@ class WP_Users_List_Table extends WP_List_Table { $this->is_site_users = 'site-users-network' === $this->screen->id; if ( $this->is_site_users ) { - $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; + $this->site_id = isset( $_REQUEST['id'] ) ? (int) $_REQUEST['id'] : 0; } } diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php index edc30cda2c..c9caa0892f 100644 --- a/wp-admin/includes/dashboard.php +++ b/wp-admin/includes/dashboard.php @@ -606,7 +606,7 @@ function wp_dashboard_recent_drafts( $drafts = false ) { echo '

' . __( 'Your Recent Drafts' ) . "

\n