WordPress.org

Make WordPress Core

Changeset 37640


Ignore:
Timestamp:
06/06/2016 03:17:46 PM (4 years ago)
Author:
DrewAPicture
Message:

Nav Menus: Move the Walker_Nav_Menu class to its own file.

The new class-walker-nav-menu.php file is loaded in nav-menu-template.php for backward compatibility purposes.

Fixes #37035. See #33413.

Location:
trunk/src/wp-includes
Files:
1 edited
1 copied

Legend:

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

    r37639 r37640  
    11<?php
    22/**
    3  * Navigation Menu template functions
     3 * Nav Menu API: Walker_Nav_Menu class
    44 *
    55 * @package WordPress
    66 * @subpackage Nav_Menus
    7  * @since 3.0.0
     7 * @since 4.6.0
    88 */
    99
    1010/**
    11  * Create HTML list of nav menu items.
     11 * Core class used to implement an HTML list of nav menu items.
    1212 *
    1313 * @since 3.0.0
    14  * @uses Walker
     14 *
     15 * @see Walker
    1516 */
    1617class Walker_Nav_Menu extends Walker {
     
    220221
    221222} // Walker_Nav_Menu
    222 
    223 /**
    224  * Displays a navigation menu.
    225  *
    226  * @since 3.0.0
    227  *
    228  * @staticvar array $menu_id_slugs
    229  *
    230  * @param array $args {
    231  *     Optional. Array of nav menu arguments.
    232  *
    233  *     @type string        $menu            Desired menu. Accepts (matching in order) id, slug, name. Default empty.
    234  *     @type string        $menu_class      CSS class to use for the ul element which forms the menu. Default 'menu'.
    235  *     @type string        $menu_id         The ID that is applied to the ul element which forms the menu.
    236  *                                          Default is the menu slug, incremented.
    237  *     @type string        $container       Whether to wrap the ul, and what to wrap it with. Default 'div'.
    238  *     @type string        $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'.
    239  *     @type string        $container_id    The ID that is applied to the container. Default empty.
    240  *     @type callable|bool $fallback_cb     If the menu doesn't exists, a callback function will fire.
    241  *                                          Default is 'wp_page_menu'. Set to false for no fallback.
    242  *     @type string        $before          Text before the link markup. Default empty.
    243  *     @type string        $after           Text after the link markup. Default empty.
    244  *     @type string        $link_before     Text before the link text. Default empty.
    245  *     @type string        $link_after      Text after the link text. Default empty.
    246  *     @type bool          $echo            Whether to echo the menu or return it. Default true.
    247  *     @type int           $depth           How many levels of the hierarchy are to be included. 0 means all. Default 0.
    248  *     @type object        $walker          Instance of a custom walker class. Default empty.
    249  *     @type string        $theme_location  Theme location to be used. Must be registered with register_nav_menu()
    250  *                                          in order to be selectable by the user.
    251  *     @type string        $items_wrap      How the list items should be wrapped. Default is a ul with an id and class.
    252  *                                          Uses printf() format with numbered placeholders.
    253  * }
    254  * @return object|false|void Menu output if $echo is false, false if there are no items or no menu was found.
    255  */
    256 function wp_nav_menu( $args = array() ) {
    257     static $menu_id_slugs = array();
    258 
    259     $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    260     '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>',
    261     'depth' => 0, 'walker' => '', 'theme_location' => '' );
    262 
    263     $args = wp_parse_args( $args, $defaults );
    264     /**
    265      * Filters the arguments used to display a navigation menu.
    266      *
    267      * @since 3.0.0
    268      *
    269      * @see wp_nav_menu()
    270      *
    271      * @param array $args Array of wp_nav_menu() arguments.
    272      */
    273     $args = apply_filters( 'wp_nav_menu_args', $args );
    274     $args = (object) $args;
    275 
    276     /**
    277      * Filters whether to short-circuit the wp_nav_menu() output.
    278      *
    279      * Returning a non-null value to the filter will short-circuit
    280      * wp_nav_menu(), echoing that value if $args->echo is true,
    281      * returning that value otherwise.
    282      *
    283      * @since 3.9.0
    284      *
    285      * @see wp_nav_menu()
    286      *
    287      * @param string|null $output Nav menu output to short-circuit with. Default null.
    288      * @param object      $args   An object containing wp_nav_menu() arguments.
    289      */
    290     $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );
    291 
    292     if ( null !== $nav_menu ) {
    293         if ( $args->echo ) {
    294             echo $nav_menu;
    295             return;
    296         }
    297 
    298         return $nav_menu;
    299     }
    300 
    301     // Get the nav menu based on the requested menu
    302     $menu = wp_get_nav_menu_object( $args->menu );
    303 
    304     // Get the nav menu based on the theme_location
    305     if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
    306         $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
    307 
    308     // get the first menu that has items if we still can't find a menu
    309     if ( ! $menu && !$args->theme_location ) {
    310         $menus = wp_get_nav_menus();
    311         foreach ( $menus as $menu_maybe ) {
    312             if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
    313                 $menu = $menu_maybe;
    314                 break;
    315             }
    316         }
    317     }
    318 
    319     if ( empty( $args->menu ) ) {
    320         $args->menu = $menu;
    321     }
    322 
    323     // If the menu exists, get its items.
    324     if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
    325         $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
    326 
    327     /*
    328      * If no menu was found:
    329      *  - Fall back (if one was specified), or bail.
    330      *
    331      * If no menu items were found:
    332      *  - Fall back, but only if no theme location was specified.
    333      *  - Otherwise, bail.
    334      */
    335     if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
    336         && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) )
    337             return call_user_func( $args->fallback_cb, (array) $args );
    338 
    339     if ( ! $menu || is_wp_error( $menu ) )
    340         return false;
    341 
    342     $nav_menu = $items = '';
    343 
    344     $show_container = false;
    345     if ( $args->container ) {
    346         /**
    347          * Filters the list of HTML tags that are valid for use as menu containers.
    348          *
    349          * @since 3.0.0
    350          *
    351          * @param array $tags The acceptable HTML tags for use as menu containers.
    352          *                    Default is array containing 'div' and 'nav'.
    353          */
    354         $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    355         if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
    356             $show_container = true;
    357             $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
    358             $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
    359             $nav_menu .= '<'. $args->container . $id . $class . '>';
    360         }
    361     }
    362 
    363     // Set up the $menu_item variables
    364     _wp_menu_item_classes_by_context( $menu_items );
    365 
    366     $sorted_menu_items = $menu_items_with_children = array();
    367     foreach ( (array) $menu_items as $menu_item ) {
    368         $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
    369         if ( $menu_item->menu_item_parent )
    370             $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
    371     }
    372 
    373     // Add the menu-item-has-children class where applicable
    374     if ( $menu_items_with_children ) {
    375         foreach ( $sorted_menu_items as &$menu_item ) {
    376             if ( isset( $menu_items_with_children[ $menu_item->ID ] ) )
    377                 $menu_item->classes[] = 'menu-item-has-children';
    378         }
    379     }
    380 
    381     unset( $menu_items, $menu_item );
    382 
    383     /**
    384      * Filters the sorted list of menu item objects before generating the menu's HTML.
    385      *
    386      * @since 3.1.0
    387      *
    388      * @param array  $sorted_menu_items The menu items, sorted by each menu item's menu order.
    389      * @param object $args              An object containing wp_nav_menu() arguments.
    390      */
    391     $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
    392 
    393     $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    394     unset($sorted_menu_items);
    395 
    396     // Attributes
    397     if ( ! empty( $args->menu_id ) ) {
    398         $wrap_id = $args->menu_id;
    399     } else {
    400         $wrap_id = 'menu-' . $menu->slug;
    401         while ( in_array( $wrap_id, $menu_id_slugs ) ) {
    402             if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
    403                 $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
    404             else
    405                 $wrap_id = $wrap_id . '-1';
    406         }
    407     }
    408     $menu_id_slugs[] = $wrap_id;
    409 
    410     $wrap_class = $args->menu_class ? $args->menu_class : '';
    411 
    412     /**
    413      * Filters the HTML list content for navigation menus.
    414      *
    415      * @since 3.0.0
    416      *
    417      * @see wp_nav_menu()
    418      *
    419      * @param string $items The HTML list content for the menu items.
    420      * @param object $args  An object containing wp_nav_menu() arguments.
    421      */
    422     $items = apply_filters( 'wp_nav_menu_items', $items, $args );
    423     /**
    424      * Filters the HTML list content for a specific navigation menu.
    425      *
    426      * @since 3.0.0
    427      *
    428      * @see wp_nav_menu()
    429      *
    430      * @param string $items The HTML list content for the menu items.
    431      * @param object $args  An object containing wp_nav_menu() arguments.
    432      */
    433     $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
    434 
    435     // Don't print any markup if there are no items at this point.
    436     if ( empty( $items ) )
    437         return false;
    438 
    439     $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
    440     unset( $items );
    441 
    442     if ( $show_container )
    443         $nav_menu .= '</' . $args->container . '>';
    444 
    445     /**
    446      * Filters the HTML content for navigation menus.
    447      *
    448      * @since 3.0.0
    449      *
    450      * @see wp_nav_menu()
    451      *
    452      * @param string $nav_menu The HTML content for the navigation menu.
    453      * @param object $args     An object containing wp_nav_menu() arguments.
    454      */
    455     $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
    456 
    457     if ( $args->echo )
    458         echo $nav_menu;
    459     else
    460         return $nav_menu;
    461 }
    462 
    463 /**
    464  * Add the class property classes for the current context, if applicable.
    465  *
    466  * @access private
    467  * @since 3.0.0
    468  *
    469  * @global WP_Query   $wp_query
    470  * @global WP_Rewrite $wp_rewrite
    471  *
    472  * @param array $menu_items The current menu item objects to which to add the class property information.
    473  */
    474 function _wp_menu_item_classes_by_context( &$menu_items ) {
    475     global $wp_query, $wp_rewrite;
    476 
    477     $queried_object = $wp_query->get_queried_object();
    478     $queried_object_id = (int) $wp_query->queried_object_id;
    479 
    480     $active_object = '';
    481     $active_ancestor_item_ids = array();
    482     $active_parent_item_ids = array();
    483     $active_parent_object_ids = array();
    484     $possible_taxonomy_ancestors = array();
    485     $possible_object_parents = array();
    486     $home_page_id = (int) get_option( 'page_for_posts' );
    487 
    488     if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
    489         foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
    490             if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    491                 $term_hierarchy = _get_term_hierarchy( $taxonomy );
    492                 $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
    493                 if ( is_array( $terms ) ) {
    494                     $possible_object_parents = array_merge( $possible_object_parents, $terms );
    495                     $term_to_ancestor = array();
    496                     foreach ( (array) $term_hierarchy as $anc => $descs ) {
    497                         foreach ( (array) $descs as $desc )
    498                             $term_to_ancestor[ $desc ] = $anc;
    499                     }
    500 
    501                     foreach ( $terms as $desc ) {
    502                         do {
    503                             $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
    504                             if ( isset( $term_to_ancestor[ $desc ] ) ) {
    505                                 $_desc = $term_to_ancestor[ $desc ];
    506                                 unset( $term_to_ancestor[ $desc ] );
    507                                 $desc = $_desc;
    508                             } else {
    509                                 $desc = 0;
    510                             }
    511                         } while ( ! empty( $desc ) );
    512                     }
    513                 }
    514             }
    515         }
    516     } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
    517         $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy );
    518         $term_to_ancestor = array();
    519         foreach ( (array) $term_hierarchy as $anc => $descs ) {
    520             foreach ( (array) $descs as $desc )
    521                 $term_to_ancestor[ $desc ] = $anc;
    522         }
    523         $desc = $queried_object->term_id;
    524         do {
    525             $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
    526             if ( isset( $term_to_ancestor[ $desc ] ) ) {
    527                 $_desc = $term_to_ancestor[ $desc ];
    528                 unset( $term_to_ancestor[ $desc ] );
    529                 $desc = $_desc;
    530             } else {
    531                 $desc = 0;
    532             }
    533         } while ( ! empty( $desc ) );
    534     }
    535 
    536     $possible_object_parents = array_filter( $possible_object_parents );
    537 
    538     $front_page_url = home_url();
    539 
    540     foreach ( (array) $menu_items as $key => $menu_item ) {
    541 
    542         $menu_items[$key]->current = false;
    543 
    544         $classes = (array) $menu_item->classes;
    545         $classes[] = 'menu-item';
    546         $classes[] = 'menu-item-type-' . $menu_item->type;
    547         $classes[] = 'menu-item-object-' . $menu_item->object;
    548 
    549         // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object
    550         if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
    551             $active_parent_object_ids[] = (int) $menu_item->object_id;
    552             $active_parent_item_ids[] = (int) $menu_item->db_id;
    553             $active_object = $queried_object->post_type;
    554 
    555         // if the menu item corresponds to the currently-queried post or taxonomy object
    556         } elseif (
    557             $menu_item->object_id == $queried_object_id &&
    558             (
    559                 ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
    560                 ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
    561                 ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
    562             )
    563         ) {
    564             $classes[] = 'current-menu-item';
    565             $menu_items[$key]->current = true;
    566             $_anc_id = (int) $menu_item->db_id;
    567 
    568             while(
    569                 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    570                 ! in_array( $_anc_id, $active_ancestor_item_ids )
    571             ) {
    572                 $active_ancestor_item_ids[] = $_anc_id;
    573             }
    574 
    575             if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
    576                 // Back compat classes for pages to match wp_page_menu()
    577                 $classes[] = 'page_item';
    578                 $classes[] = 'page-item-' . $menu_item->object_id;
    579                 $classes[] = 'current_page_item';
    580             }
    581             $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
    582             $active_parent_object_ids[] = (int) $menu_item->post_parent;
    583             $active_object = $menu_item->object;
    584 
    585         // if the menu item corresponds to the currently-queried post type archive
    586         } elseif (
    587             'post_type_archive' == $menu_item->type &&
    588             is_post_type_archive( array( $menu_item->object ) )
    589         ) {
    590             $classes[] = 'current-menu-item';
    591             $menu_items[$key]->current = true;
    592         // if the menu item corresponds to the currently-requested URL
    593         } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) {
    594             $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
    595             $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
    596             $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
    597             $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) );
    598             $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
    599 
    600             if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) {
    601                 $classes[] = 'current-menu-item';
    602                 $menu_items[$key]->current = true;
    603                 $_anc_id = (int) $menu_item->db_id;
    604 
    605                 while(
    606                     ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
    607                     ! in_array( $_anc_id, $active_ancestor_item_ids )
    608                 ) {
    609                     $active_ancestor_item_ids[] = $_anc_id;
    610                 }
    611 
    612                 if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
    613                     // Back compat for home link to match wp_page_menu()
    614                     $classes[] = 'current_page_item';
    615                 }
    616                 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
    617                 $active_parent_object_ids[] = (int) $menu_item->post_parent;
    618                 $active_object = $menu_item->object;
    619 
    620             // give front page item current-menu-item class when extra query arguments involved
    621             } elseif ( $item_url == $front_page_url && is_front_page() ) {
    622                 $classes[] = 'current-menu-item';
    623             }
    624 
    625             if ( untrailingslashit($item_url) == home_url() )
    626                 $classes[] = 'menu-item-home';
    627         }
    628 
    629         // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query
    630         if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
    631             $classes[] = 'current_page_parent';
    632 
    633         $menu_items[$key]->classes = array_unique( $classes );
    634     }
    635     $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
    636     $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) );
    637     $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
    638 
    639     // set parent's class
    640     foreach ( (array) $menu_items as $key => $parent_item ) {
    641         $classes = (array) $parent_item->classes;
    642         $menu_items[$key]->current_item_ancestor = false;
    643         $menu_items[$key]->current_item_parent = false;
    644 
    645         if (
    646             isset( $parent_item->type ) &&
    647             (
    648                 // ancestral post object
    649                 (
    650                     'post_type' == $parent_item->type &&
    651                     ! empty( $queried_object->post_type ) &&
    652                     is_post_type_hierarchical( $queried_object->post_type ) &&
    653                     in_array( $parent_item->object_id, $queried_object->ancestors ) &&
    654                     $parent_item->object != $queried_object->ID
    655                 ) ||
    656 
    657                 // ancestral term
    658                 (
    659                     'taxonomy' == $parent_item->type &&
    660                     isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
    661                     in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
    662                     (
    663                         ! isset( $queried_object->term_id ) ||
    664                         $parent_item->object_id != $queried_object->term_id
    665                     )
    666                 )
    667             )
    668         ) {
    669             $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
    670         }
    671 
    672         if ( in_array(  intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
    673             $classes[] = 'current-menu-ancestor';
    674             $menu_items[$key]->current_item_ancestor = true;
    675         }
    676         if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
    677             $classes[] = 'current-menu-parent';
    678             $menu_items[$key]->current_item_parent = true;
    679         }
    680         if ( in_array( $parent_item->object_id, $active_parent_object_ids ) )
    681             $classes[] = 'current-' . $active_object . '-parent';
    682 
    683         if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
    684             // Back compat classes for pages to match wp_page_menu()
    685             if ( in_array('current-menu-parent', $classes) )
    686                 $classes[] = 'current_page_parent';
    687             if ( in_array('current-menu-ancestor', $classes) )
    688                 $classes[] = 'current_page_ancestor';
    689         }
    690 
    691         $menu_items[$key]->classes = array_unique( $classes );
    692     }
    693 }
    694 
    695 /**
    696  * Retrieve the HTML list content for nav menu items.
    697  *
    698  * @uses Walker_Nav_Menu to create HTML list content.
    699  * @since 3.0.0
    700  *
    701  * @param array  $items
    702  * @param int    $depth
    703  * @param object $r
    704  * @return string
    705  */
    706 function walk_nav_menu_tree( $items, $depth, $r ) {
    707     $walker = ( empty($r->walker) ) ? new Walker_Nav_Menu : $r->walker;
    708     $args = array( $items, $depth, $r );
    709 
    710     return call_user_func_array( array( $walker, 'walk' ), $args );
    711 }
    712 
    713 /**
    714  * Prevents a menu item ID from being used more than once.
    715  *
    716  * @since 3.0.1
    717  * @access private
    718  *
    719  * @staticvar array $used_ids
    720  * @param string $id
    721  * @param object $item
    722  * @return string
    723  */
    724 function _nav_menu_item_id_use_once( $id, $item ) {
    725     static $_used_ids = array();
    726     if ( in_array( $item->ID, $_used_ids ) ) {
    727         return '';
    728     }
    729     $_used_ids[] = $item->ID;
    730     return $id;
    731 }
  • trunk/src/wp-includes/nav-menu-template.php

    r37639 r37640  
    11<?php
    22/**
    3  * Navigation Menu template functions
     3 * Nav Menu API: Template functions
    44 *
    55 * @package WordPress
     
    88 */
    99
    10 /**
    11  * Create HTML list of nav menu items.
    12  *
    13  * @since 3.0.0
    14  * @uses Walker
    15  */
    16 class Walker_Nav_Menu extends Walker {
    17     /**
    18      * What the class handles.
    19      *
    20      * @since 3.0.0
    21      * @access public
    22      * @var string
    23      *
    24      * @see Walker::$tree_type
    25      */
    26     public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
    27 
    28     /**
    29      * Database fields to use.
    30      *
    31      * @since 3.0.0
    32      * @access public
    33      * @todo Decouple this.
    34      * @var array
    35      *
    36      * @see Walker::$db_fields
    37      */
    38     public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
    39 
    40     /**
    41      * Starts the list before the elements are added.
    42      *
    43      * @since 3.0.0
    44      *
    45      * @see Walker::start_lvl()
    46      *
    47      * @param string $output Passed by reference. Used to append additional content.
    48      * @param int    $depth  Depth of menu item. Used for padding.
    49      * @param array  $args   An array of wp_nav_menu() arguments.
    50      */
    51     public function start_lvl( &$output, $depth = 0, $args = array() ) {
    52         $indent = str_repeat("\t", $depth);
    53         $output .= "\n$indent<ul class=\"sub-menu\">\n";
    54     }
    55 
    56     /**
    57      * Ends the list of after the elements are added.
    58      *
    59      * @since 3.0.0
    60      *
    61      * @see Walker::end_lvl()
    62      *
    63      * @param string $output Passed by reference. Used to append additional content.
    64      * @param int    $depth  Depth of menu item. Used for padding.
    65      * @param array  $args   An array of wp_nav_menu() arguments.
    66      */
    67     public function end_lvl( &$output, $depth = 0, $args = array() ) {
    68         $indent = str_repeat("\t", $depth);
    69         $output .= "$indent</ul>\n";
    70     }
    71 
    72     /**
    73      * Starts the element output.
    74      *
    75      * @since 3.0.0
    76      * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
    77      *
    78      * @see Walker::start_el()
    79      *
    80      * @param string $output Passed by reference. Used to append additional content.
    81      * @param object $item   Menu item data object.
    82      * @param int    $depth  Depth of menu item. Used for padding.
    83      * @param array  $args   An array of wp_nav_menu() arguments.
    84      * @param int    $id     Current item ID.
    85      */
    86     public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    87         $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    88 
    89         $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    90         $classes[] = 'menu-item-' . $item->ID;
    91 
    92         /**
    93          * Filters the arguments for a single nav menu item.
    94          *
    95          * @since 4.4.0
    96          *
    97          * @param array  $args  An array of arguments.
    98          * @param object $item  Menu item data object.
    99          * @param int    $depth Depth of menu item. Used for padding.
    100          */
    101         $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
    102 
    103         /**
    104          * Filters the CSS class(es) applied to a menu item's list item element.
    105          *
    106          * @since 3.0.0
    107          * @since 4.1.0 The `$depth` parameter was added.
    108          *
    109          * @param array  $classes The CSS classes that are applied to the menu item's `<li>` element.
    110          * @param object $item    The current menu item.
    111          * @param array  $args    An array of wp_nav_menu() arguments.
    112          * @param int    $depth   Depth of menu item. Used for padding.
    113          */
    114         $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
    115         $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    116 
    117         /**
    118          * Filters the ID applied to a menu item's list item element.
    119          *
    120          * @since 3.0.1
    121          * @since 4.1.0 The `$depth` parameter was added.
    122          *
    123          * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
    124          * @param object $item    The current menu item.
    125          * @param array  $args    An array of wp_nav_menu() arguments.
    126          * @param int    $depth   Depth of menu item. Used for padding.
    127          */
    128         $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
    129         $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    130 
    131         $output .= $indent . '<li' . $id . $class_names .'>';
    132 
    133         $atts = array();
    134         $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    135         $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    136         $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    137         $atts['href']   = ! empty( $item->url )        ? $item->url        : '';
    138 
    139         /**
    140          * Filters the HTML attributes applied to a menu item's anchor element.
    141          *
    142          * @since 3.6.0
    143          * @since 4.1.0 The `$depth` parameter was added.
    144          *
    145          * @param array $atts {
    146          *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
    147          *
    148          *     @type string $title  Title attribute.
    149          *     @type string $target Target attribute.
    150          *     @type string $rel    The rel attribute.
    151          *     @type string $href   The href attribute.
    152          * }
    153          * @param object $item  The current menu item.
    154          * @param array  $args  An array of wp_nav_menu() arguments.
    155          * @param int    $depth Depth of menu item. Used for padding.
    156          */
    157         $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
    158 
    159         $attributes = '';
    160         foreach ( $atts as $attr => $value ) {
    161             if ( ! empty( $value ) ) {
    162                 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
    163                 $attributes .= ' ' . $attr . '="' . $value . '"';
    164             }
    165         }
    166 
    167         /** This filter is documented in wp-includes/post-template.php */
    168         $title = apply_filters( 'the_title', $item->title, $item->ID );
    169 
    170         /**
    171          * Filters a menu item's title.
    172          *
    173          * @since 4.4.0
    174          *
    175          * @param string $title The menu item's title.
    176          * @param object $item  The current menu item.
    177          * @param array  $args  An array of wp_nav_menu() arguments.
    178          * @param int    $depth Depth of menu item. Used for padding.
    179          */
    180         $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
    181 
    182         $item_output = $args->before;
    183         $item_output .= '<a'. $attributes .'>';
    184         $item_output .= $args->link_before . $title . $args->link_after;
    185         $item_output .= '</a>';
    186         $item_output .= $args->after;
    187 
    188         /**
    189          * Filters a menu item's starting output.
    190          *
    191          * The menu item's starting output only includes `$args->before`, the opening `<a>`,
    192          * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
    193          * no filter for modifying the opening and closing `<li>` for a menu item.
    194          *
    195          * @since 3.0.0
    196          *
    197          * @param string $item_output The menu item's starting HTML output.
    198          * @param object $item        Menu item data object.
    199          * @param int    $depth       Depth of menu item. Used for padding.
    200          * @param array  $args        An array of wp_nav_menu() arguments.
    201          */
    202         $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    203     }
    204 
    205     /**
    206      * Ends the element output, if needed.
    207      *
    208      * @since 3.0.0
    209      *
    210      * @see Walker::end_el()
    211      *
    212      * @param string $output Passed by reference. Used to append additional content.
    213      * @param object $item   Page data object. Not used.
    214      * @param int    $depth  Depth of page. Not Used.
    215      * @param array  $args   An array of wp_nav_menu() arguments.
    216      */
    217     public function end_el( &$output, $item, $depth = 0, $args = array() ) {
    218         $output .= "</li>\n";
    219     }
    220 
    221 } // Walker_Nav_Menu
     10/** Walker_Nav_Menu class */
     11require_once ABSPATH . WPINC . '/class-walker-nav-menu.php';
    22212
    22313/**
Note: See TracChangeset for help on using the changeset viewer.