Changeset 42343 for trunk/src/wp-includes/nav-menu-template.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/nav-menu-template.php (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/nav-menu-template.php
r41008 r42343 49 49 static $menu_id_slugs = array(); 50 50 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 ); 54 70 55 71 $args = wp_parse_args( $args, $defaults ); … … 101 117 102 118 // 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 ] ) ) { 104 120 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); 121 } 105 122 106 123 // 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 ) { 108 125 $menus = wp_get_nav_menus(); 109 126 foreach ( $menus as $menu_maybe ) { … … 120 137 121 138 // 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 ) ) { 123 140 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); 141 } 124 142 125 143 /* … … 131 149 * - Otherwise, bail. 132 150 */ 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 ) ) { 135 153 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 ) ) { 138 157 return false; 158 } 139 159 140 160 $nav_menu = $items = ''; … … 153 173 if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) { 154 174 $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 . '>'; 158 178 } 159 179 } … … 165 185 foreach ( (array) $menu_items as $menu_item ) { 166 186 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; 167 if ( $menu_item->menu_item_parent ) 187 if ( $menu_item->menu_item_parent ) { 168 188 $menu_items_with_children[ $menu_item->menu_item_parent ] = true; 189 } 169 190 } 170 191 … … 172 193 if ( $menu_items_with_children ) { 173 194 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 ] ) ) { 175 196 $menu_item->classes[] = 'menu-item-has-children'; 197 } 176 198 } 177 199 } … … 190 212 191 213 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); 192 unset( $sorted_menu_items);214 unset( $sorted_menu_items ); 193 215 194 216 // Attributes … … 198 220 $wrap_id = 'menu-' . $menu->slug; 199 221 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 else222 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) { 223 $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); 224 } else { 203 225 $wrap_id = $wrap_id . '-1'; 226 } 204 227 } 205 228 } … … 232 255 233 256 // Don't print any markup if there are no items at this point. 234 if ( empty( $items ) ) 257 if ( empty( $items ) ) { 235 258 return false; 259 } 236 260 237 261 $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); 238 262 unset( $items ); 239 263 240 if ( $show_container ) 264 if ( $show_container ) { 241 265 $nav_menu .= '</' . $args->container . '>'; 266 } 242 267 243 268 /** … … 253 278 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); 254 279 255 if ( $args->echo ) 280 if ( $args->echo ) { 256 281 echo $nav_menu; 257 else282 } else { 258 283 return $nav_menu; 284 } 259 285 } 260 286 … … 273 299 global $wp_query, $wp_rewrite; 274 300 275 $queried_object = $wp_query->get_queried_object();301 $queried_object = $wp_query->get_queried_object(); 276 302 $queried_object_id = (int) $wp_query->queried_object_id; 277 303 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(); 282 308 $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' ); 285 311 286 312 if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) { … … 288 314 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 289 315 $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' ) ); 291 317 if ( is_array( $terms ) ) { 292 318 $possible_object_parents = array_merge( $possible_object_parents, $terms ); 293 $term_to_ancestor = array();319 $term_to_ancestor = array(); 294 320 foreach ( (array) $term_hierarchy as $anc => $descs ) { 295 foreach ( (array) $descs as $desc ) 321 foreach ( (array) $descs as $desc ) { 296 322 $term_to_ancestor[ $desc ] = $anc; 323 } 297 324 } 298 325 … … 313 340 } 314 341 } 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 ); 316 343 $term_to_ancestor = array(); 317 344 foreach ( (array) $term_hierarchy as $anc => $descs ) { 318 foreach ( (array) $descs as $desc ) 345 foreach ( (array) $descs as $desc ) { 319 346 $term_to_ancestor[ $desc ] = $anc; 347 } 320 348 } 321 349 $desc = $queried_object->term_id; … … 339 367 foreach ( (array) $menu_items as $key => $menu_item ) { 340 368 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; 344 372 $classes[] = 'menu-item'; 345 373 $classes[] = 'menu-item-type-' . $menu_item->type; … … 354 382 if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) { 355 383 $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 object384 $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 360 388 } elseif ( 361 389 $menu_item->object_id == $queried_object_id && … … 366 394 ) 367 395 ) { 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 ( 373 401 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && 374 402 ! in_array( $_anc_id, $active_ancestor_item_ids ) … … 384 412 } 385 413 386 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;414 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 387 415 $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 archive416 $active_object = $menu_item->object; 417 418 // if the menu item corresponds to the currently-queried post type archive 391 419 } elseif ( 392 420 'post_type_archive' == $menu_item->type && 393 421 is_post_type_archive( array( $menu_item->object ) ) 394 422 ) { 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 ( 400 428 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && 401 429 ! in_array( $_anc_id, $active_ancestor_item_ids ) … … 406 434 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 407 435 408 // if the menu item corresponds to the currently-requested URL436 // if the menu item corresponds to the currently-requested URL 409 437 } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) { 410 438 $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); … … 414 442 $_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' ); 415 443 } 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 ) ); 419 447 $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); 420 448 421 449 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 ( 427 455 ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && 428 456 ! in_array( $_anc_id, $active_ancestor_item_ids ) … … 435 463 $classes[] = 'current_page_item'; 436 464 } 437 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;465 $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 438 466 $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 involved467 $active_object = $menu_item->object; 468 469 // give front page item current-menu-item class when extra query arguments involved 442 470 } elseif ( $item_url == $front_page_url && is_front_page() ) { 443 471 $classes[] = 'current-menu-item'; 444 472 } 445 473 446 if ( untrailingslashit( $item_url) == home_url() )474 if ( untrailingslashit( $item_url ) == home_url() ) { 447 475 $classes[] = 'menu-item-home'; 476 } 448 477 } 449 478 450 479 // 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 ) { 452 481 $classes[] = 'current_page_parent'; 453 454 $menu_items[$key]->classes = array_unique( $classes ); 482 } 483 484 $menu_items[ $key ]->classes = array_unique( $classes ); 455 485 } 456 486 $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 ) ); 458 488 $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) ); 459 489 460 490 // set parent's class 461 491 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; 465 495 466 496 if ( … … 491 521 } 492 522 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; 496 526 } 497 527 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 ) ) { 502 532 $classes[] = 'current-' . $active_object . '-parent'; 533 } 503 534 504 535 if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) { 505 536 // 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 ) ) { 507 538 $classes[] = 'current_page_parent'; 508 if ( in_array('current-menu-ancestor', $classes) ) 539 } 540 if ( in_array( 'current-menu-ancestor', $classes ) ) { 509 541 $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 ); 513 546 } 514 547 } … … 526 559 */ 527 560 function 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 ); 530 563 531 564 return call_user_func_array( array( $walker, 'walk' ), $args );
Note: See TracChangeset
for help on using the changeset viewer.