Changeset 37640 for trunk/src/wp-includes/class-walker-nav-menu.php
- Timestamp:
- 06/06/2016 03:17:46 PM (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-walker-nav-menu.php
r37639 r37640 1 1 <?php 2 2 /** 3 * Nav igation Menu template functions3 * Nav Menu API: Walker_Nav_Menu class 4 4 * 5 5 * @package WordPress 6 6 * @subpackage Nav_Menus 7 * @since 3.0.07 * @since 4.6.0 8 8 */ 9 9 10 10 /** 11 * C reateHTML list of nav menu items.11 * Core class used to implement an HTML list of nav menu items. 12 12 * 13 13 * @since 3.0.0 14 * @uses Walker 14 * 15 * @see Walker 15 16 */ 16 17 class Walker_Nav_Menu extends Walker { … … 220 221 221 222 } // Walker_Nav_Menu 222 223 /**224 * Displays a navigation menu.225 *226 * @since 3.0.0227 *228 * @staticvar array $menu_id_slugs229 *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.0268 *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-circuit280 * wp_nav_menu(), echoing that value if $args->echo is true,281 * returning that value otherwise.282 *283 * @since 3.9.0284 *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 menu302 $menu = wp_get_nav_menu_object( $args->menu );303 304 // Get the nav menu based on the theme_location305 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 menu309 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.0350 *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 variables364 _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 applicable374 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.0387 *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 // Attributes397 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 else405 $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.0416 *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.0427 *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.0449 *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 else460 return $nav_menu;461 }462 463 /**464 * Add the class property classes for the current context, if applicable.465 *466 * @access private467 * @since 3.0.0468 *469 * @global WP_Query $wp_query470 * @global WP_Rewrite $wp_rewrite471 *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 object550 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 object556 } 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 archive586 } 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 URL593 } 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 involved621 } 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 query630 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 class640 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 object649 (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->ID655 ) ||656 657 // ancestral term658 (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_id665 )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.0700 *701 * @param array $items702 * @param int $depth703 * @param object $r704 * @return string705 */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.1717 * @access private718 *719 * @staticvar array $used_ids720 * @param string $id721 * @param object $item722 * @return string723 */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 }
Note: See TracChangeset
for help on using the changeset viewer.