Make WordPress Core


Ignore:
Timestamp:
04/05/2020 03:00:44 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict type check for in_array() and array_search() where strings are involved.

This reduces the number of WordPress.PHP.StrictInArray.MissingTrueStrict issues from 486 to 50.

Includes minor code layout fixes for better readability.

See #49542.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu-template.php

    r47219 r47550  
    175175                 */
    176176                $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    177                 if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
     177
     178                if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags, true ) ) {
    178179                        $show_container = true;
    179180                        $class          = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"';
     
    224225        } else {
    225226                $wrap_id = 'menu-' . $menu->slug;
    226                 while ( in_array( $wrap_id, $menu_id_slugs ) ) {
     227
     228                while ( in_array( $wrap_id, $menu_id_slugs, true ) ) {
    227229                        if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) {
    228230                                $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
     
    398400                        // If the menu item corresponds to the currently queried post or taxonomy object.
    399401                } elseif (
    400                         $menu_item->object_id == $queried_object_id &&
    401                         (
    402                                 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
    403                                 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
    404                                 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
     402                        $menu_item->object_id == $queried_object_id
     403                        && (
     404                                ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id )
     405                                || ( 'post_type' == $menu_item->type && $wp_query->is_singular )
     406                                || ( 'taxonomy' == $menu_item->type
     407                                        && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
    405408                        )
    406409                ) {
     
    410413
    411414                        while (
    412                                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    413                                 ! in_array( $_anc_id, $active_ancestor_item_ids )
     415                                ( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
     416                                && ! in_array( $_anc_id, $active_ancestor_item_ids, true )
    414417                        ) {
    415418                                $active_ancestor_item_ids[] = $_anc_id;
     
    429432                        // If the menu item corresponds to the currently queried post type archive.
    430433                } elseif (
    431                         'post_type_archive' == $menu_item->type &&
    432                         is_post_type_archive( array( $menu_item->object ) )
     434                        'post_type_archive' == $menu_item->type
     435                        && is_post_type_archive( array( $menu_item->object ) )
    433436                ) {
    434437                        $classes[]                   = 'current-menu-item';
     
    437440
    438441                        while (
    439                                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    440                                 ! in_array( $_anc_id, $active_ancestor_item_ids )
     442                                ( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
     443                                && ! in_array( $_anc_id, $active_ancestor_item_ids, true )
    441444                        ) {
    442445                                $active_ancestor_item_ids[] = $_anc_id;
     
    468471                        );
    469472
    470                         if ( $raw_item_url && in_array( $item_url, $matches ) ) {
     473                        if ( $raw_item_url && in_array( $item_url, $matches, true ) ) {
    471474                                $classes[]                   = 'current-menu-item';
    472475                                $menu_items[ $key ]->current = true;
     
    474477
    475478                                while (
    476                                         ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    477                                         ! in_array( $_anc_id, $active_ancestor_item_ids )
     479                                        ( $_anc_id = (int) get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) )
     480                                        && ! in_array( $_anc_id, $active_ancestor_item_ids, true )
    478481                                ) {
    479482                                        $active_ancestor_item_ids[] = $_anc_id;
    480483                                }
    481484
    482                                 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
     485                                if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ), true ) ) {
    483486                                        // Back compat for home link to match wp_page_menu().
    484487                                        $classes[] = 'current_page_item';
     
    516519
    517520                if (
    518                         isset( $parent_item->type ) &&
    519                         (
     521                        isset( $parent_item->type )
     522                        && (
    520523                                // Ancestral post object.
    521524                                (
    522                                         'post_type' == $parent_item->type &&
    523                                         ! empty( $queried_object->post_type ) &&
    524                                         is_post_type_hierarchical( $queried_object->post_type ) &&
    525                                         in_array( $parent_item->object_id, $queried_object->ancestors ) &&
    526                                         $parent_item->object != $queried_object->ID
     525                                        'post_type' == $parent_item->type
     526                                        && ! empty( $queried_object->post_type )
     527                                        && is_post_type_hierarchical( $queried_object->post_type )
     528                                        && in_array( $parent_item->object_id, $queried_object->ancestors )
     529                                        && $parent_item->object != $queried_object->ID
    527530                                ) ||
    528531
    529532                                // Ancestral term.
    530533                                (
    531                                         'taxonomy' == $parent_item->type &&
    532                                         isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
    533                                         in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
    534                                         (
     534                                        'taxonomy' == $parent_item->type
     535                                        && isset( $possible_taxonomy_ancestors[ $parent_item->object ] )
     536                                        && in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] )
     537                                        && (
    535538                                                ! isset( $queried_object->term_id ) ||
    536539                                                $parent_item->object_id != $queried_object->term_id
     
    539542                        )
    540543                ) {
    541                         $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
    542                 }
    543 
    544                 if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
    545                         $classes[]                                 = 'current-menu-ancestor';
     544                        if ( ! empty( $queried_object->taxonomy ) ) {
     545                                $classes[] = 'current-' . $queried_object->taxonomy . '-ancestor';
     546                        } else {
     547                                $classes[] = 'current-' . $queried_object->post_type . '-ancestor';
     548                        }
     549                }
     550
     551                if ( in_array( (int) $parent_item->db_id, $active_ancestor_item_ids, true ) ) {
     552                        $classes[] = 'current-menu-ancestor';
     553
    546554                        $menu_items[ $key ]->current_item_ancestor = true;
    547555                }
    548                 if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
    549                         $classes[]                               = 'current-menu-parent';
     556                if ( in_array( (int) $parent_item->db_id, $active_parent_item_ids, true ) ) {
     557                        $classes[] = 'current-menu-parent';
     558
    550559                        $menu_items[ $key ]->current_item_parent = true;
    551560                }
    552                 if ( in_array( $parent_item->object_id, $active_parent_object_ids ) ) {
     561                if ( in_array( (int) $parent_item->object_id, $active_parent_object_ids, true ) ) {
    553562                        $classes[] = 'current-' . $active_object . '-parent';
    554563                }
     
    556565                if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
    557566                        // Back compat classes for pages to match wp_page_menu().
    558                         if ( in_array( 'current-menu-parent', $classes ) ) {
     567                        if ( in_array( 'current-menu-parent', $classes, true ) ) {
    559568                                $classes[] = 'current_page_parent';
    560569                        }
    561                         if ( in_array( 'current-menu-ancestor', $classes ) ) {
     570                        if ( in_array( 'current-menu-ancestor', $classes, true ) ) {
    562571                                $classes[] = 'current_page_ancestor';
    563572                        }
     
    598607function _nav_menu_item_id_use_once( $id, $item ) {
    599608        static $_used_ids = array();
    600         if ( in_array( $item->ID, $_used_ids ) ) {
     609
     610        if ( in_array( $item->ID, $_used_ids, true ) ) {
    601611                return '';
    602612        }
     613
    603614        $_used_ids[] = $item->ID;
     615
    604616        return $id;
    605617}
Note: See TracChangeset for help on using the changeset viewer.