From 856e1a27b8dfa9208c36b8b474afcb5acc90322b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 9 Apr 2020 15:43:10 +0000 Subject: [PATCH] Coding Standards: Use strict type check for `in_array()` and `array_search()`. This addresses all the remaining `WordPress.PHP.StrictInArray.MissingTrueStrict` issues in core. Includes minor code layout fixes for better readability. Follow-up to [47550]. See #49542. Built from https://develop.svn.wordpress.org/trunk@47557 git-svn-id: http://core.svn.wordpress.org/trunk@47332 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-walker-category-checklist.php | 11 +++-- .../includes/class-wp-filesystem-base.php | 2 +- .../includes/class-wp-ms-users-list-table.php | 2 +- .../includes/class-wp-terms-list-table.php | 2 +- wp-admin/includes/dashboard.php | 2 +- wp-admin/includes/ms.php | 6 ++- wp-admin/includes/nav-menu.php | 14 ++++-- wp-admin/includes/plugin.php | 4 +- wp-admin/includes/post.php | 6 +-- wp-admin/includes/revision.php | 2 +- wp-admin/includes/screen.php | 4 +- wp-admin/includes/template.php | 28 +++++++++-- wp-admin/nav-menus.php | 34 ++++++++----- wp-admin/network/site-info.php | 5 +- wp-admin/users.php | 2 +- wp-includes/class-http.php | 2 +- wp-includes/class-walker-page.php | 6 ++- wp-includes/class-wp-http-cookie.php | 2 +- wp-includes/class-wp-http-curl.php | 4 +- wp-includes/class-wp-query.php | 4 +- wp-includes/class-wp-theme.php | 2 +- .../class-wp-customize-nav-menu-setting.php | 6 ++- wp-includes/general-template.php | 7 ++- wp-includes/link-template.php | 4 +- wp-includes/nav-menu-template.php | 11 +++-- wp-includes/post.php | 48 ++++++++++++++----- wp-includes/rewrite.php | 2 +- wp-includes/taxonomy.php | 27 ++++++++--- wp-includes/version.php | 2 +- 29 files changed, 173 insertions(+), 78 deletions(-) diff --git a/wp-admin/includes/class-walker-category-checklist.php b/wp-admin/includes/class-walker-category-checklist.php index 3647d8770b..f4d3fbed36 100644 --- a/wp-admin/includes/class-walker-category-checklist.php +++ b/wp-admin/includes/class-walker-category-checklist.php @@ -83,7 +83,7 @@ class Walker_Category_Checklist extends Walker { $args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats']; - $class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : ''; + $class = in_array( $category->term_id, $args['popular_cats'], true ) ? ' class="popular-category"' : ''; $args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats']; @@ -91,7 +91,7 @@ class Walker_Category_Checklist extends Walker { $aria_checked = 'false'; $inner_class = 'category'; - if ( in_array( $category->term_id, $args['selected_cats'] ) ) { + if ( in_array( $category->term_id, $args['selected_cats'], true ) ) { $inner_class .= ' selected'; $aria_checked = 'true'; } @@ -102,10 +102,13 @@ class Walker_Category_Checklist extends Walker { /** This filter is documented in wp-includes/category-template.php */ esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . ''; } else { + $is_selected = in_array( $category->term_id, $args['selected_cats'], true ); + $is_disabled = ! empty( $args['disabled'] ); + $output .= "\n
  • " . ''; } diff --git a/wp-admin/includes/class-wp-filesystem-base.php b/wp-admin/includes/class-wp-filesystem-base.php index d9459b31df..fe96929b5c 100644 --- a/wp-admin/includes/class-wp-filesystem-base.php +++ b/wp-admin/includes/class-wp-filesystem-base.php @@ -401,7 +401,7 @@ class WP_Filesystem_Base { $attarray = preg_split( '//', $mode ); for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) { - $key = array_search( $attarray[ $i ], $legal ); + $key = array_search( $attarray[ $i ], $legal, true ); if ( $key ) { $realmode .= $legal[ $key ]; } diff --git a/wp-admin/includes/class-wp-ms-users-list-table.php b/wp-admin/includes/class-wp-ms-users-list-table.php index 4f2932acdc..57968ae935 100644 --- a/wp-admin/includes/class-wp-ms-users-list-table.php +++ b/wp-admin/includes/class-wp-ms-users-list-table.php @@ -505,7 +505,7 @@ class WP_MS_Users_List_Table extends WP_List_Table { $actions['edit'] = '' . __( 'Edit' ) . ''; } - if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) { + if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) { $actions['delete'] = '' . __( 'Delete' ) . ''; } diff --git a/wp-admin/includes/class-wp-terms-list-table.php b/wp-admin/includes/class-wp-terms-list-table.php index 03b997a443..2d9aca5932 100644 --- a/wp-admin/includes/class-wp-terms-list-table.php +++ b/wp-admin/includes/class-wp-terms-list-table.php @@ -300,7 +300,7 @@ class WP_Terms_List_Table extends WP_List_Table { $my_parent = get_term( $p, $taxonomy ); $my_parents[] = $my_parent; $p = $my_parent->parent; - if ( in_array( $p, $parent_ids ) ) { // Prevent parent loops. + if ( in_array( $p, $parent_ids, true ) ) { // Prevent parent loops. break; } $parent_ids[] = $p; diff --git a/wp-admin/includes/dashboard.php b/wp-admin/includes/dashboard.php index 2a31c6c0b3..11fb18268c 100644 --- a/wp-admin/includes/dashboard.php +++ b/wp-admin/includes/dashboard.php @@ -519,7 +519,7 @@ function wp_dashboard_quick_press( $error_msg = false ) { $post = get_default_post_to_edit( 'post', true ); $user_id = get_current_user_id(); // Don't create an option if this is a super admin who does not belong to this site. - if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ) ) ) { + if ( in_array( get_current_blog_id(), array_keys( get_blogs_of_user( $user_id ) ), true ) ) { update_user_option( $user_id, 'dashboard_quick_press_last_post_id', (int) $post->ID ); // Save post_ID. } } diff --git a/wp-admin/includes/ms.php b/wp-admin/includes/ms.php index f1567cb5d4..f683c737ef 100644 --- a/wp-admin/includes/ms.php +++ b/wp-admin/includes/ms.php @@ -922,19 +922,23 @@ function confirm_delete_users( $users ) { 'fields' => array( 'ID', 'user_login' ), ) ); + if ( is_array( $blog_users ) && ! empty( $blog_users ) ) { $user_site = "{$details->blogname}"; $user_dropdown = ''; $user_dropdown .= "\n"; ?> diff --git a/wp-admin/includes/nav-menu.php b/wp-admin/includes/nav-menu.php index e3d1053f77..64dda1db61 100644 --- a/wp-admin/includes/nav-menu.php +++ b/wp-admin/includes/nav-menu.php @@ -1149,8 +1149,10 @@ function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selecte 'post_status' => 'draft,publish', ) ); - $messages = array(); - $menu_items = array(); + + $messages = array(); + $menu_items = array(); + // Index menu items by DB ID. foreach ( $unsorted_menu_items as $_item ) { $menu_items[ $_item->db_id ] = $_item; @@ -1173,6 +1175,7 @@ function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selecte ); wp_defer_term_counting( true ); + // Loop through all the menu items' POST variables. if ( ! empty( $_POST['menu-item-db-id'] ) ) { foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) { @@ -1209,19 +1212,22 @@ function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selecte // Store 'auto-add' pages. $auto_add = ! empty( $_POST['auto-add-pages'] ); $nav_menu_option = (array) get_option( 'nav_menu_options' ); + if ( ! isset( $nav_menu_option['auto_add'] ) ) { $nav_menu_option['auto_add'] = array(); } + if ( $auto_add ) { - if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) { + if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'], true ) ) { $nav_menu_option['auto_add'][] = $nav_menu_selected_id; } } else { - $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ); + $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'], true ); if ( false !== $key ) { unset( $nav_menu_option['auto_add'][ $key ] ); } } + // Remove non-existent/deleted menus. $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) ); update_option( 'nav_menu_options', $nav_menu_option ); diff --git a/wp-admin/includes/plugin.php b/wp-admin/includes/plugin.php index 67f3d9bd40..9a8851afeb 100644 --- a/wp-admin/includes/plugin.php +++ b/wp-admin/includes/plugin.php @@ -2170,7 +2170,7 @@ function add_option_whitelist( $new_options, $options = '' ) { $whitelist_options[ $page ] = array(); $whitelist_options[ $page ][] = $key; } else { - $pos = array_search( $key, $whitelist_options[ $page ] ); + $pos = array_search( $key, $whitelist_options[ $page ], true ); if ( false === $pos ) { $whitelist_options[ $page ][] = $key; } @@ -2202,7 +2202,7 @@ function remove_option_whitelist( $del_options, $options = '' ) { foreach ( $del_options as $page => $keys ) { foreach ( $keys as $key ) { if ( isset( $whitelist_options[ $page ] ) && is_array( $whitelist_options[ $page ] ) ) { - $pos = array_search( $key, $whitelist_options[ $page ] ); + $pos = array_search( $key, $whitelist_options[ $page ], true ); if ( false !== $pos ) { unset( $whitelist_options[ $page ][ $pos ] ); } diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index b2143d61eb..a554932951 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -548,8 +548,8 @@ function bulk_edit_posts( $post_data = null ) { $children[] = $parent; foreach ( $pages as $page ) { - if ( $page->ID == $parent ) { - $parent = $page->post_parent; + if ( (int) $page->ID === $parent ) { + $parent = (int) $page->post_parent; break; } } @@ -568,7 +568,7 @@ function bulk_edit_posts( $post_data = null ) { $post_type_object = get_post_type_object( get_post_type( $post_ID ) ); if ( ! isset( $post_type_object ) - || ( isset( $children ) && in_array( $post_ID, $children ) ) + || ( isset( $children ) && in_array( $post_ID, $children, true ) ) || ! current_user_can( 'edit_post', $post_ID ) ) { $skipped[] = $post_ID; diff --git a/wp-admin/includes/revision.php b/wp-admin/includes/revision.php index cbf4eaeb38..ac4f7de00e 100644 --- a/wp-admin/includes/revision.php +++ b/wp-admin/includes/revision.php @@ -314,7 +314,7 @@ function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null // Now, grab the initial diff. $compare_two_mode = is_numeric( $from ); if ( ! $compare_two_mode ) { - $found = array_search( $selected_revision_id, array_keys( $revisions ) ); + $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 ); diff --git a/wp-admin/includes/screen.php b/wp-admin/includes/screen.php index db6abfb5ce..75cc14a573 100644 --- a/wp-admin/includes/screen.php +++ b/wp-admin/includes/screen.php @@ -128,10 +128,12 @@ function meta_box_prefs( $screen ) { $widget_title = $box['args']['__widget_basename']; } + $is_hidden = in_array( $box['id'], $hidden, true ); + printf( '', esc_attr( $box['id'] ), - checked( in_array( $box['id'], $hidden, true ), false, false ), + checked( $is_hidden, false, false ), $widget_title ); } diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index 882af9e149..c140849f6e 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -171,7 +171,7 @@ function wp_terms_checklist( $post_id = 0, $args = array() ) { $keys = array_keys( $categories ); foreach ( $keys as $k ) { - if ( in_array( $categories[ $k ]->term_id, $args['selected_cats'] ) ) { + if ( in_array( $categories[ $k ]->term_id, $args['selected_cats'], true ) ) { $checked_categories[] = $categories[ $k ]; unset( $categories[ $k ] ); } @@ -228,13 +228,14 @@ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $ech $tax = get_taxonomy( $taxonomy ); $popular_ids = array(); + foreach ( (array) $terms as $term ) { $popular_ids[] = $term->term_id; if ( ! $echo ) { // Hack for Ajax use. continue; } $id = "popular-$taxonomy-$term->term_id"; - $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : ''; + $checked = in_array( $term->term_id, $checked_terms, true ) ? 'checked="checked"' : ''; ?>
  • '; } } @@ -339,6 +340,7 @@ function get_inline_data( $post ) { } $taxonomy_names = get_object_taxonomies( $post->post_type ); + foreach ( $taxonomy_names as $taxonomy_name ) { $taxonomy = get_taxonomy( $taxonomy_name ); @@ -721,7 +723,6 @@ function meta_form( $post = null ) { $field_key, true ); ?> $field_key, array( 0, 1 ) ) ); ?> /> +
    diff --git a/wp-admin/users.php b/wp-admin/users.php index b8eb3a45c0..04164d7179 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -237,7 +237,7 @@ switch ( $wp_list_table->current_action() ) { $all_userids = $userids; - if ( in_array( $current_user->ID, $userids ) ) { + if ( in_array( $current_user->ID, $userids, true ) ) { $userids = array_diff( $userids, array( $current_user->ID ) ); } diff --git a/wp-includes/class-http.php b/wp-includes/class-http.php index 561a9ce443..fd759b7347 100644 --- a/wp-includes/class-http.php +++ b/wp-includes/class-http.php @@ -1041,7 +1041,7 @@ class WP_Http { // POST requests should not POST to a redirected location. if ( 'POST' == $args['method'] ) { - if ( in_array( $response['response']['code'], array( 302, 303 ) ) ) { + if ( in_array( $response['response']['code'], array( 302, 303 ), true ) ) { $args['method'] = 'GET'; } } diff --git a/wp-includes/class-walker-page.php b/wp-includes/class-walker-page.php index 0b77c422bf..557bf309b0 100644 --- a/wp-includes/class-walker-page.php +++ b/wp-includes/class-walker-page.php @@ -122,12 +122,14 @@ class Walker_Page extends Walker { if ( ! empty( $current_page ) ) { $_current_page = get_post( $current_page ); - if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) { + + if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) { $css_class[] = 'current_page_ancestor'; } + if ( $page->ID == $current_page ) { $css_class[] = 'current_page_item'; - } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) { + } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) { $css_class[] = 'current_page_parent'; } } elseif ( get_option( 'page_for_posts' ) == $page->ID ) { diff --git a/wp-includes/class-wp-http-cookie.php b/wp-includes/class-wp-http-cookie.php index 24bfbbaf94..fb3861383e 100644 --- a/wp-includes/class-wp-http-cookie.php +++ b/wp-includes/class-wp-http-cookie.php @@ -192,7 +192,7 @@ class WP_Http_Cookie { } // Port - supports "port-lists" in the format: "80,8000,8080". - if ( ! empty( $port ) && ! in_array( $url['port'], explode( ',', $port ) ) ) { + if ( ! empty( $port ) && ! in_array( $url['port'], array_map( 'intval', explode( ',', $port ) ), true ) ) { return false; } diff --git a/wp-includes/class-wp-http-curl.php b/wp-includes/class-wp-http-curl.php index 8225a290f4..df62fcc12b 100644 --- a/wp-includes/class-wp-http-curl.php +++ b/wp-includes/class-wp-http-curl.php @@ -238,7 +238,7 @@ class WP_Http_Curl { curl_close( $handle ); return new WP_Error( 'http_request_failed', $curl_error ); } - if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) { + if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) { curl_close( $handle ); return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); } @@ -286,7 +286,7 @@ class WP_Http_Curl { return new WP_Error( 'http_request_failed', $curl_error ); } } - if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ) ) ) { + if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array( 301, 302 ), true ) ) { curl_close( $handle ); return new WP_Error( 'http_request_failed', __( 'Too many redirects.' ) ); } diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 856a6fbc98..b72c9c35aa 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -3137,7 +3137,7 @@ class WP_Query { $sticky_offset = 0; // Loop over posts and relocate stickies to the front. for ( $i = 0; $i < $num_posts; $i++ ) { - if ( in_array( $this->posts[ $i ]->ID, $sticky_posts ) ) { + if ( in_array( $this->posts[ $i ]->ID, $sticky_posts, true ) ) { $sticky_post = $this->posts[ $i ]; // Remove sticky from current position. array_splice( $this->posts, $i, 1 ); @@ -3146,7 +3146,7 @@ class WP_Query { // Increment the sticky offset. The next sticky will be placed at this offset. $sticky_offset++; // Remove post from sticky posts array. - $offset = array_search( $sticky_post->ID, $sticky_posts ); + $offset = array_search( $sticky_post->ID, $sticky_posts, true ); unset( $sticky_posts[ $offset ] ); } } diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php index 190d024a0b..d9368317ae 100644 --- a/wp-includes/class-wp-theme.php +++ b/wp-includes/class-wp-theme.php @@ -276,7 +276,7 @@ final class WP_Theme implements ArrayAccess { $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); // Default themes always trump their pretenders. // Properly identify default themes that are inside a directory within wp-content/themes. - $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes ); + $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes, true ); if ( $default_theme_slug ) { if ( basename( $this->stylesheet ) != $default_theme_slug ) { $this->headers['Name'] .= '/' . $this->stylesheet; diff --git a/wp-includes/customize/class-wp-customize-nav-menu-setting.php b/wp-includes/customize/class-wp-customize-nav-menu-setting.php index c0783bf686..1b0de422ab 100644 --- a/wp-includes/customize/class-wp-customize-nav-menu-setting.php +++ b/wp-includes/customize/class-wp-customize-nav-menu-setting.php @@ -179,7 +179,7 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { $value['auto_add'] = false; if ( isset( $nav_menu_options['auto_add'] ) && is_array( $nav_menu_options['auto_add'] ) ) { - $value['auto_add'] = in_array( $term->term_id, $nav_menu_options['auto_add'] ); + $value['auto_add'] = in_array( $term->term_id, $nav_menu_options['auto_add'], true ); } } } @@ -188,6 +188,7 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { $value = $this->default; } } + return $value; } @@ -602,7 +603,8 @@ class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting { $nav_menu_options['auto_add'] = array(); } - $i = array_search( $menu_id, $nav_menu_options['auto_add'] ); + $i = array_search( $menu_id, $nav_menu_options['auto_add'], true ); + if ( $auto_add && false === $i ) { array_push( $nav_menu_options['auto_add'], $this->term_id ); } elseif ( ! $auto_add && false !== $i ) { diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index d136c357b7..437dd4dc1c 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -2180,9 +2180,10 @@ function get_calendar( $initial = true, $echo = true ) { AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N ); + if ( $dayswithposts ) { foreach ( (array) $dayswithposts as $daywith ) { - $daywithpost[] = $daywith[0]; + $daywithpost[] = (int) $daywith[0]; } } @@ -2209,7 +2210,7 @@ function get_calendar( $initial = true, $echo = true ) { $calendar_output .= ''; } - if ( in_array( $day, $daywithpost ) ) { + if ( in_array( $day, $daywithpost, true ) ) { // Any posts today? $date_format = gmdate( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) ); /* translators: Post calendar label. %s: Date. */ @@ -2223,6 +2224,7 @@ function get_calendar( $initial = true, $echo = true ) { } else { $calendar_output .= $day; } + $calendar_output .= ''; if ( 6 == calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) { @@ -2234,6 +2236,7 @@ function get_calendar( $initial = true, $echo = true ) { if ( 0 != $pad && 7 != $pad ) { $calendar_output .= "\n\t\t" . ' '; } + $calendar_output .= "\n\t\n\t"; $calendar_output .= "\n\t"; diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 5345ec4281..9dbbf4931f 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -3687,13 +3687,15 @@ function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); $blogs = get_blogs_of_user( $user_id ); + if ( is_multisite() && ! user_can( $user_id, 'manage_network' ) && empty( $blogs ) ) { $url = user_admin_url( $path, $scheme ); } elseif ( ! is_multisite() ) { $url = admin_url( $path, $scheme ); } else { $current_blog = get_current_blog_id(); - if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { + + if ( $current_blog && ( user_can( $user_id, 'manage_network' ) || in_array( $current_blog, array_keys( $blogs ), true ) ) ) { $url = admin_url( $path, $scheme ); } else { $active = get_active_blog_for_user( $user_id ); diff --git a/wp-includes/nav-menu-template.php b/wp-includes/nav-menu-template.php index 9dd1601ee5..d49dbeed02 100644 --- a/wp-includes/nav-menu-template.php +++ b/wp-includes/nav-menu-template.php @@ -392,7 +392,9 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { } // If the menu item corresponds to a taxonomy term for the currently queried non-hierarchical post object. - if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) { + if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type + && in_array( (int) $menu_item->object_id, $possible_object_parents, true ) + ) { $active_parent_object_ids[] = (int) $menu_item->object_id; $active_parent_item_ids[] = (int) $menu_item->db_id; $active_object = $queried_object->post_type; @@ -404,7 +406,8 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) || ( 'post_type' == $menu_item->type && $wp_query->is_singular ) || ( 'taxonomy' == $menu_item->type - && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object ) + && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) + && $queried_object->taxonomy == $menu_item->object ) ) ) { $classes[] = 'current-menu-item'; @@ -525,7 +528,7 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { 'post_type' == $parent_item->type && ! empty( $queried_object->post_type ) && is_post_type_hierarchical( $queried_object->post_type ) - && in_array( $parent_item->object_id, $queried_object->ancestors ) + && in_array( (int) $parent_item->object_id, $queried_object->ancestors, true ) && $parent_item->object != $queried_object->ID ) || @@ -533,7 +536,7 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { ( 'taxonomy' == $parent_item->type && isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) - && in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) + && in_array( (int) $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ], true ) && ( ! isset( $queried_object->term_id ) || $parent_item->object_id != $queried_object->term_id diff --git a/wp-includes/post.php b/wp-includes/post.php index 10eee1d25f..47c5648ca6 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -2284,7 +2284,12 @@ function is_sticky( $post_id = 0 ) { $stickies = get_option( 'sticky_posts' ); - $is_sticky = is_array( $stickies ) && in_array( $post_id, $stickies ); + if ( is_array( $stickies ) ) { + $stickies = array_map( 'intval', $stickies ); + $is_sticky = in_array( $post_id, $stickies, true ); + } else { + $is_sticky = false; + } /** * Filters whether a post is sticky. @@ -2511,13 +2516,16 @@ function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { * @param int $post_id Post ID. */ function stick_post( $post_id ) { + $post_id = (int) $post_id; $stickies = get_option( 'sticky_posts' ); if ( ! is_array( $stickies ) ) { - $stickies = array( $post_id ); + $stickies = array(); } - if ( ! in_array( $post_id, $stickies ) ) { + $stickies = array_map( 'intval', $stickies ); + + if ( ! in_array( $post_id, $stickies, true ) ) { $stickies[] = $post_id; } @@ -2545,17 +2553,20 @@ function stick_post( $post_id ) { * @param int $post_id Post ID. */ function unstick_post( $post_id ) { + $post_id = (int) $post_id; $stickies = get_option( 'sticky_posts' ); if ( ! is_array( $stickies ) ) { return; } - if ( ! in_array( $post_id, $stickies ) ) { + $stickies = array_map( 'intval', $stickies ); + + if ( ! in_array( $post_id, $stickies, true ) ) { return; } - $offset = array_search( $post_id, $stickies ); + $offset = array_search( $post_id, $stickies, true ); if ( false === $offset ) { return; } @@ -2841,8 +2852,10 @@ function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { foreach ( (array) $wildcard_mime_types as $type ) { $mimes = array_map( 'trim', explode( ',', $type ) ); foreach ( $mimes as $mime ) { - $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); + $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); + $patternses[][ $type ] = "^$regex$"; + if ( false === strpos( $mime, '/' ) ) { $patternses[][ $type ] = "^$regex/"; $patternses[][ $type ] = $regex; @@ -2854,12 +2867,15 @@ function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { foreach ( $patternses as $patterns ) { foreach ( $patterns as $type => $pattern ) { foreach ( (array) $real_mime_types as $real ) { - if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ] ) ) ) { + if ( preg_match( "#$pattern#", $real ) + && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) ) + ) { $matches[ $type ][] = $real; } } } } + return $matches; } @@ -2914,9 +2930,11 @@ function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { $wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; } } + if ( ! empty( $wheres ) ) { $where = ' AND (' . join( ' OR ', $wheres ) . ') '; } + return $where; } @@ -4436,15 +4454,16 @@ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); + $post = get_post( $post_ID ); + // Prevent new post slugs that could result in URLs that conflict with date archives. - $post = get_post( $post_ID ); $conflicts_with_date_archive = false; if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { $slug_num = intval( $slug ); if ( $slug_num ) { $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); - $postname_index = array_search( '%postname%', $permastructs ); + $postname_index = array_search( '%postname%', $permastructs, true ); /* * Potential date clashes are as follows: @@ -5500,7 +5519,7 @@ function get_pages( $args = array() ) { $num_pages = count( $pages ); for ( $i = 0; $i < $num_pages; $i++ ) { - if ( in_array( $pages[ $i ]->ID, $exclude ) ) { + if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) { unset( $pages[ $i ] ); } } @@ -5811,7 +5830,8 @@ function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { */ function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { $attachment_id = (int) $attachment_id; - $post = get_post( $attachment_id ); + + $post = get_post( $attachment_id ); if ( ! $post ) { return false; } @@ -5845,7 +5865,8 @@ function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { */ function wp_update_attachment_metadata( $attachment_id, $data ) { $attachment_id = (int) $attachment_id; - $post = get_post( $attachment_id ); + + $post = get_post( $attachment_id ); if ( ! $post ) { return false; } @@ -5878,7 +5899,8 @@ function wp_update_attachment_metadata( $attachment_id, $data ) { */ function wp_get_attachment_url( $attachment_id = 0 ) { $attachment_id = (int) $attachment_id; - $post = get_post( $attachment_id ); + + $post = get_post( $attachment_id ); if ( ! $post ) { return false; } diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index 1c91e137ee..c704bdd317 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -596,7 +596,7 @@ function url_to_postid( $url ) { parse_str( $query, $query_vars ); $query = array(); foreach ( (array) $query_vars as $key => $value ) { - if ( in_array( $key, $wp->public_query_vars, true ) ) { + if ( in_array( (string) $key, $wp->public_query_vars, true ) ) { $query[ $key ] = $value; if ( isset( $post_type_query_vars[ $key ] ) ) { $query['post_type'] = $post_type_query_vars[ $key ]; diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 245a63c970..d240740ff2 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -2468,16 +2468,20 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { } $term_info = term_exists( $term, $taxonomy ); + if ( ! $term_info ) { // Skip if a non-existent term ID is passed. if ( is_int( $term ) ) { continue; } + $term_info = wp_insert_term( $term, $taxonomy ); } + if ( is_wp_error( $term_info ) ) { return $term_info; } + $term_ids[] = $term_info['term_id']; $tt_id = $term_info['term_taxonomy_id']; $tt_ids[] = $tt_id; @@ -2497,6 +2501,7 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { * @param string $taxonomy Taxonomy slug. */ do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy ); + $wpdb->insert( $wpdb->term_relationships, array( @@ -2516,6 +2521,7 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { * @param string $taxonomy Taxonomy slug. */ do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy ); + $new_tt_ids[] = $tt_id; } @@ -2539,9 +2545,11 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { } $t = get_taxonomy( $taxonomy ); + if ( ! $append && isset( $t->sort ) && $t->sort ) { - $values = array(); - $term_order = 0; + $values = array(); + $term_order = 0; + $final_tt_ids = wp_get_object_terms( $object_id, $taxonomy, @@ -2550,11 +2558,13 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 'update_term_meta_cache' => false, ) ); + foreach ( $tt_ids as $tt_id ) { - if ( in_array( $tt_id, $final_tt_ids ) ) { + if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) { $values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order ); } } + if ( $values ) { if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) { return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error ); @@ -2578,6 +2588,7 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { * @param array $old_tt_ids Old array of term taxonomy IDs. */ do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ); + return $tt_ids; } @@ -3616,8 +3627,10 @@ function _pad_term_counts( &$terms, $taxonomy ) { $tax_obj = get_taxonomy( $taxonomy ); $object_types = esc_sql( $tax_obj->object_type ); $results = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_keys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" ); + foreach ( $results as $row ) { - $id = $term_ids[ $row->term_taxonomy_id ]; + $id = $term_ids[ $row->term_taxonomy_id ]; + $term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1; } @@ -3627,14 +3640,16 @@ function _pad_term_counts( &$terms, $taxonomy ) { $ancestors = array(); while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) { $ancestors[] = $child; + if ( ! empty( $term_items[ $term_id ] ) ) { foreach ( $term_items[ $term_id ] as $item_id => $touches ) { $term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1; } } + $child = $parent; - if ( in_array( $parent, $ancestors ) ) { + if ( in_array( $parent, $ancestors, true ) ) { break; } } @@ -4522,7 +4537,7 @@ function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) if ( 'taxonomy' === $resource_type ) { $term = get_term( $object_id, $object_type ); - while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { + while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors, true ) ) { $ancestors[] = (int) $term->parent; $term = get_term( $term->parent, $object_type ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 3c326cfb81..8d62bfae18 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-47556'; +$wp_version = '5.5-alpha-47557'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.