Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

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

    r41008 r42343  
    4949    static $menu_id_slugs = array();
    5050
    51     $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    52     'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 'item_spacing' => 'preserve',
    53     'depth' => 0, 'walker' => '', 'theme_location' => '' );
     51    $defaults = array(
     52        'menu'            => '',
     53        'container'       => 'div',
     54        'container_class' => '',
     55        'container_id'    => '',
     56        'menu_class'      => 'menu',
     57        'menu_id'         => '',
     58        'echo'            => true,
     59        'fallback_cb'     => 'wp_page_menu',
     60        'before'          => '',
     61        'after'           => '',
     62        'link_before'     => '',
     63        'link_after'      => '',
     64        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
     65        'item_spacing'    => 'preserve',
     66        'depth'           => 0,
     67        'walker'          => '',
     68        'theme_location'  => '',
     69    );
    5470
    5571    $args = wp_parse_args( $args, $defaults );
     
    101117
    102118    // Get the nav menu based on the theme_location
    103     if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
     119    if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) {
    104120        $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
     121    }
    105122
    106123    // get the first menu that has items if we still can't find a menu
    107     if ( ! $menu && !$args->theme_location ) {
     124    if ( ! $menu && ! $args->theme_location ) {
    108125        $menus = wp_get_nav_menus();
    109126        foreach ( $menus as $menu_maybe ) {
     
    120137
    121138    // If the menu exists, get its items.
    122     if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
     139    if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) {
    123140        $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
     141    }
    124142
    125143    /*
     
    131149     *  - Otherwise, bail.
    132150     */
    133     if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
    134         && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) )
     151    if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) )
     152        && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) {
    135153            return call_user_func( $args->fallback_cb, (array) $args );
    136 
    137     if ( ! $menu || is_wp_error( $menu ) )
     154    }
     155
     156    if ( ! $menu || is_wp_error( $menu ) ) {
    138157        return false;
     158    }
    139159
    140160    $nav_menu = $items = '';
     
    153173        if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
    154174            $show_container = true;
    155             $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
    156             $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
    157             $nav_menu .= '<'. $args->container . $id . $class . '>';
     175            $class          = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"';
     176            $id             = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
     177            $nav_menu      .= '<' . $args->container . $id . $class . '>';
    158178        }
    159179    }
     
    165185    foreach ( (array) $menu_items as $menu_item ) {
    166186        $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
    167         if ( $menu_item->menu_item_parent )
     187        if ( $menu_item->menu_item_parent ) {
    168188            $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
     189        }
    169190    }
    170191
     
    172193    if ( $menu_items_with_children ) {
    173194        foreach ( $sorted_menu_items as &$menu_item ) {
    174             if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
     195            if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
    175196                $menu_item->classes[] = 'menu-item-has-children';
     197            }
    176198        }
    177199    }
     
    190212
    191213    $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    192     unset($sorted_menu_items);
     214    unset( $sorted_menu_items );
    193215
    194216    // Attributes
     
    198220        $wrap_id = 'menu-' . $menu->slug;
    199221        while ( in_array( $wrap_id, $menu_id_slugs ) ) {
    200             if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
    201                 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
    202             else
     222            if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) {
     223                $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
     224            } else {
    203225                $wrap_id = $wrap_id . '-1';
     226            }
    204227        }
    205228    }
     
    232255
    233256    // Don't print any markup if there are no items at this point.
    234     if ( empty( $items ) )
     257    if ( empty( $items ) ) {
    235258        return false;
     259    }
    236260
    237261    $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
    238262    unset( $items );
    239263
    240     if ( $show_container )
     264    if ( $show_container ) {
    241265        $nav_menu .= '</' . $args->container . '>';
     266    }
    242267
    243268    /**
     
    253278    $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
    254279
    255     if ( $args->echo )
     280    if ( $args->echo ) {
    256281        echo $nav_menu;
    257     else
     282    } else {
    258283        return $nav_menu;
     284    }
    259285}
    260286
     
    273299    global $wp_query, $wp_rewrite;
    274300
    275     $queried_object = $wp_query->get_queried_object();
     301    $queried_object    = $wp_query->get_queried_object();
    276302    $queried_object_id = (int) $wp_query->queried_object_id;
    277303
    278     $active_object = '';
    279     $active_ancestor_item_ids = array();
    280     $active_parent_item_ids = array();
    281     $active_parent_object_ids = array();
     304    $active_object               = '';
     305    $active_ancestor_item_ids    = array();
     306    $active_parent_item_ids      = array();
     307    $active_parent_object_ids    = array();
    282308    $possible_taxonomy_ancestors = array();
    283     $possible_object_parents = array();
    284     $home_page_id = (int) get_option( 'page_for_posts' );
     309    $possible_object_parents     = array();
     310    $home_page_id                = (int) get_option( 'page_for_posts' );
    285311
    286312    if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
     
    288314            if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    289315                $term_hierarchy = _get_term_hierarchy( $taxonomy );
    290                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
     316                $terms          = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
    291317                if ( is_array( $terms ) ) {
    292318                    $possible_object_parents = array_merge( $possible_object_parents, $terms );
    293                     $term_to_ancestor = array();
     319                    $term_to_ancestor        = array();
    294320                    foreach ( (array) $term_hierarchy as $anc => $descs ) {
    295                         foreach ( (array) $descs as $desc )
     321                        foreach ( (array) $descs as $desc ) {
    296322                            $term_to_ancestor[ $desc ] = $anc;
     323                        }
    297324                    }
    298325
     
    313340        }
    314341    } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
    315         $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
     342        $term_hierarchy   = _get_term_hierarchy( $queried_object->taxonomy );
    316343        $term_to_ancestor = array();
    317344        foreach ( (array) $term_hierarchy as $anc => $descs ) {
    318             foreach ( (array) $descs as $desc )
     345            foreach ( (array) $descs as $desc ) {
    319346                $term_to_ancestor[ $desc ] = $anc;
     347            }
    320348        }
    321349        $desc = $queried_object->term_id;
     
    339367    foreach ( (array) $menu_items as $key => $menu_item ) {
    340368
    341         $menu_items[$key]->current = false;
    342 
    343         $classes = (array) $menu_item->classes;
     369        $menu_items[ $key ]->current = false;
     370
     371        $classes   = (array) $menu_item->classes;
    344372        $classes[] = 'menu-item';
    345373        $classes[] = 'menu-item-type-' . $menu_item->type;
     
    354382        if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
    355383            $active_parent_object_ids[] = (int) $menu_item->object_id;
    356             $active_parent_item_ids[] = (int) $menu_item->db_id;
    357             $active_object = $queried_object->post_type;
    358 
    359         // if the menu item corresponds to the currently-queried post or taxonomy object
     384            $active_parent_item_ids[]   = (int) $menu_item->db_id;
     385            $active_object              = $queried_object->post_type;
     386
     387            // if the menu item corresponds to the currently-queried post or taxonomy object
    360388        } elseif (
    361389            $menu_item->object_id == $queried_object_id &&
     
    366394            )
    367395        ) {
    368             $classes[] = 'current-menu-item';
    369             $menu_items[$key]->current = true;
    370             $_anc_id = (int) $menu_item->db_id;
    371 
    372             while(
     396            $classes[]                   = 'current-menu-item';
     397            $menu_items[ $key ]->current = true;
     398            $_anc_id                     = (int) $menu_item->db_id;
     399
     400            while (
    373401                ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    374402                ! in_array( $_anc_id, $active_ancestor_item_ids )
     
    384412            }
    385413
    386             $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
     414            $active_parent_item_ids[]   = (int) $menu_item->menu_item_parent;
    387415            $active_parent_object_ids[] = (int) $menu_item->post_parent;
    388             $active_object = $menu_item->object;
    389 
    390         // if the menu item corresponds to the currently-queried post type archive
     416            $active_object              = $menu_item->object;
     417
     418            // if the menu item corresponds to the currently-queried post type archive
    391419        } elseif (
    392420            'post_type_archive' == $menu_item->type &&
    393421            is_post_type_archive( array( $menu_item->object ) )
    394422        ) {
    395             $classes[] = 'current-menu-item';
    396             $menu_items[$key]->current = true;
    397             $_anc_id = (int) $menu_item->db_id;
    398 
    399             while(
     423            $classes[]                   = 'current-menu-item';
     424            $menu_items[ $key ]->current = true;
     425            $_anc_id                     = (int) $menu_item->db_id;
     426
     427            while (
    400428                ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    401429                ! in_array( $_anc_id, $active_ancestor_item_ids )
     
    406434            $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
    407435
    408         // if the menu item corresponds to the currently-requested URL
     436            // if the menu item corresponds to the currently-requested URL
    409437        } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) {
    410438            $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
     
    414442                $_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' );
    415443            }
    416             $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
    417             $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
    418             $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) );
     444            $current_url        = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
     445            $raw_item_url       = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
     446            $item_url           = set_url_scheme( untrailingslashit( $raw_item_url ) );
    419447            $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
    420448
    421449            if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
    422                 $classes[] = 'current-menu-item';
    423                 $menu_items[$key]->current = true;
    424                 $_anc_id = (int) $menu_item->db_id;
    425 
    426                 while(
     450                $classes[]                   = 'current-menu-item';
     451                $menu_items[ $key ]->current = true;
     452                $_anc_id                     = (int) $menu_item->db_id;
     453
     454                while (
    427455                    ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    428456                    ! in_array( $_anc_id, $active_ancestor_item_ids )
     
    435463                    $classes[] = 'current_page_item';
    436464                }
    437                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
     465                $active_parent_item_ids[]   = (int) $menu_item->menu_item_parent;
    438466                $active_parent_object_ids[] = (int) $menu_item->post_parent;
    439                 $active_object = $menu_item->object;
    440 
    441             // give front page item current-menu-item class when extra query arguments involved
     467                $active_object              = $menu_item->object;
     468
     469                // give front page item current-menu-item class when extra query arguments involved
    442470            } elseif ( $item_url == $front_page_url && is_front_page() ) {
    443471                $classes[] = 'current-menu-item';
    444472            }
    445473
    446             if ( untrailingslashit($item_url) == home_url() )
     474            if ( untrailingslashit( $item_url ) == home_url() ) {
    447475                $classes[] = 'menu-item-home';
     476            }
    448477        }
    449478
    450479        // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
    451         if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
     480        if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ) {
    452481            $classes[] = 'current_page_parent';
    453 
    454         $menu_items[$key]->classes = array_unique( $classes );
     482        }
     483
     484        $menu_items[ $key ]->classes = array_unique( $classes );
    455485    }
    456486    $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
    457     $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
     487    $active_parent_item_ids   = array_filter( array_unique( $active_parent_item_ids ) );
    458488    $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
    459489
    460490    // set parent's class
    461491    foreach ( (array) $menu_items as $key => $parent_item ) {
    462         $classes = (array) $parent_item->classes;
    463         $menu_items[$key]->current_item_ancestor = false;
    464         $menu_items[$key]->current_item_parent = false;
     492        $classes                                   = (array) $parent_item->classes;
     493        $menu_items[ $key ]->current_item_ancestor = false;
     494        $menu_items[ $key ]->current_item_parent  = false;
    465495
    466496        if (
     
    491521        }
    492522
    493         if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
    494             $classes[] = 'current-menu-ancestor';
    495             $menu_items[$key]->current_item_ancestor = true;
     523        if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
     524            $classes[]                                 = 'current-menu-ancestor';
     525            $menu_items[ $key ]->current_item_ancestor = true;
    496526        }
    497527        if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
    498             $classes[] = 'current-menu-parent';
    499             $menu_items[$key]->current_item_parent = true;
    500         }
    501         if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
     528            $classes[]                               = 'current-menu-parent';
     529            $menu_items[ $key ]->current_item_parent = true;
     530        }
     531        if ( in_array( $parent_item->object_id, $active_parent_object_ids ) ) {
    502532            $classes[] = 'current-' . $active_object . '-parent';
     533        }
    503534
    504535        if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
    505536            // Back compat classes for pages to match wp_page_menu()
    506             if ( in_array('current-menu-parent', $classes) )
     537            if ( in_array( 'current-menu-parent', $classes ) ) {
    507538                $classes[] = 'current_page_parent';
    508             if ( in_array('current-menu-ancestor', $classes) )
     539            }
     540            if ( in_array( 'current-menu-ancestor', $classes ) ) {
    509541                $classes[] = 'current_page_ancestor';
    510         }
    511 
    512         $menu_items[$key]->classes = array_unique( $classes );
     542            }
     543        }
     544
     545        $menu_items[ $key ]->classes = array_unique( $classes );
    513546    }
    514547}
     
    526559 */
    527560function walk_nav_menu_tree( $items, $depth, $r ) {
    528     $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
    529     $args = array( $items, $depth, $r );
     561    $walker = ( empty( $r->walker ) ) ? new Walker_Nav_Menu : $r->walker;
     562    $args   = array( $items, $depth, $r );
    530563
    531564    return call_user_func_array( array( $walker, 'walk' ), $args );
Note: See TracChangeset for help on using the changeset viewer.