[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> nav-menu-template.php (source)

   1  <?php
   2  /**
   3   * Nav Menu API: Template functions
   4   *
   5   * @package WordPress
   6   * @subpackage Nav_Menus
   7   * @since 3.0.0
   8   */
   9  
  10  /** Walker_Nav_Menu class */
  11  require_once  ABSPATH . WPINC . '/class-walker-nav-menu.php';
  12  
  13  /**
  14   * Displays a navigation menu.
  15   *
  16   * @since 3.0.0
  17   * @since 4.7.0 Added the `item_spacing` argument.
  18   *
  19   * @staticvar array $menu_id_slugs
  20   *
  21   * @param array $args {
  22   *     Optional. Array of nav menu arguments.
  23   *
  24   *     @type int|string|WP_Term $menu            Desired menu. Accepts a menu ID, slug, name, or object. Default empty.
  25   *     @type string             $menu_class      CSS class to use for the ul element which forms the menu. Default 'menu'.
  26   *     @type string             $menu_id         The ID that is applied to the ul element which forms the menu.
  27   *                                               Default is the menu slug, incremented.
  28   *     @type string             $container       Whether to wrap the ul, and what to wrap it with. Default 'div'.
  29   *     @type string             $container_class Class that is applied to the container. Default 'menu-{menu slug}-container'.
  30   *     @type string             $container_id    The ID that is applied to the container. Default empty.
  31   *     @type callable|bool      $fallback_cb     If the menu doesn't exist, a callback function will fire.
  32   *                                               Default is 'wp_page_menu'. Set to false for no fallback.
  33   *     @type string             $before          Text before the link markup. Default empty.
  34   *     @type string             $after           Text after the link markup. Default empty.
  35   *     @type string             $link_before     Text before the link text. Default empty.
  36   *     @type string             $link_after      Text after the link text. Default empty.
  37   *     @type bool               $echo            Whether to echo the menu or return it. Default true.
  38   *     @type int                $depth           How many levels of the hierarchy are to be included. 0 means all. Default 0.
  39   *     @type object             $walker          Instance of a custom walker class. Default empty.
  40   *     @type string             $theme_location  Theme location to be used. Must be registered with register_nav_menu()
  41   *                                               in order to be selectable by the user.
  42   *     @type string             $items_wrap      How the list items should be wrapped. Default is a ul with an id and class.
  43   *                                               Uses printf() format with numbered placeholders.
  44   *     @type string             $item_spacing    Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. Default 'preserve'.
  45   * }
  46   * @return void|string|false Void if 'echo' argument is true, menu output if 'echo' is false.
  47   *                           False if there are no items or no menu was found.
  48   */
  49  function wp_nav_menu( $args = array() ) {
  50      static $menu_id_slugs = array();
  51  
  52      $defaults = array(
  53          'menu'            => '',
  54          'container'       => 'div',
  55          'container_class' => '',
  56          'container_id'    => '',
  57          'menu_class'      => 'menu',
  58          'menu_id'         => '',
  59          'echo'            => true,
  60          'fallback_cb'     => 'wp_page_menu',
  61          'before'          => '',
  62          'after'           => '',
  63          'link_before'     => '',
  64          'link_after'      => '',
  65          'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
  66          'item_spacing'    => 'preserve',
  67          'depth'           => 0,
  68          'walker'          => '',
  69          'theme_location'  => '',
  70      );
  71  
  72      $args = wp_parse_args( $args, $defaults );
  73  
  74      if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
  75          // Invalid value, fall back to default.
  76          $args['item_spacing'] = $defaults['item_spacing'];
  77      }
  78  
  79      /**
  80       * Filters the arguments used to display a navigation menu.
  81       *
  82       * @since 3.0.0
  83       *
  84       * @see wp_nav_menu()
  85       *
  86       * @param array $args Array of wp_nav_menu() arguments.
  87       */
  88      $args = apply_filters( 'wp_nav_menu_args', $args );
  89      $args = (object) $args;
  90  
  91      /**
  92       * Filters whether to short-circuit the wp_nav_menu() output.
  93       *
  94       * Returning a non-null value to the filter will short-circuit
  95       * wp_nav_menu(), echoing that value if $args->echo is true,
  96       * returning that value otherwise.
  97       *
  98       * @since 3.9.0
  99       *
 100       * @see wp_nav_menu()
 101       *
 102       * @param string|null $output Nav menu output to short-circuit with. Default null.
 103       * @param stdClass    $args   An object containing wp_nav_menu() arguments.
 104       */
 105      $nav_menu = apply_filters( 'pre_wp_nav_menu', null, $args );
 106  
 107      if ( null !== $nav_menu ) {
 108          if ( $args->echo ) {
 109              echo $nav_menu;
 110              return;
 111          }
 112  
 113          return $nav_menu;
 114      }
 115  
 116      // Get the nav menu based on the requested menu.
 117      $menu = wp_get_nav_menu_object( $args->menu );
 118  
 119      // Get the nav menu based on the theme_location.
 120      $locations = get_nav_menu_locations();
 121      if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) {
 122          $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
 123      }
 124  
 125      // Get the first menu that has items if we still can't find a menu.
 126      if ( ! $menu && ! $args->theme_location ) {
 127          $menus = wp_get_nav_menus();
 128          foreach ( $menus as $menu_maybe ) {
 129              $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) );
 130              if ( $menu_items ) {
 131                  $menu = $menu_maybe;
 132                  break;
 133              }
 134          }
 135      }
 136  
 137      if ( empty( $args->menu ) ) {
 138          $args->menu = $menu;
 139      }
 140  
 141      // If the menu exists, get its items.
 142      if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) {
 143          $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
 144      }
 145  
 146      /*
 147       * If no menu was found:
 148       *  - Fall back (if one was specified), or bail.
 149       *
 150       * If no menu items were found:
 151       *  - Fall back, but only if no theme location was specified.
 152       *  - Otherwise, bail.
 153       */
 154      if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) )
 155          && isset( $args->fallback_cb ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) {
 156              return call_user_func( $args->fallback_cb, (array) $args );
 157      }
 158  
 159      if ( ! $menu || is_wp_error( $menu ) ) {
 160          return false;
 161      }
 162  
 163      $nav_menu = '';
 164      $items    = '';
 165  
 166      $show_container = false;
 167      if ( $args->container ) {
 168          /**
 169           * Filters the list of HTML tags that are valid for use as menu containers.
 170           *
 171           * @since 3.0.0
 172           *
 173           * @param string[] $tags The acceptable HTML tags for use as menu containers.
 174           *                       Default is array containing 'div' and 'nav'.
 175           */
 176          $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
 177          if ( is_string( $args->container ) && in_array( $args->container, $allowed_tags ) ) {
 178              $show_container = true;
 179              $class          = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-' . $menu->slug . '-container"';
 180              $id             = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
 181              $nav_menu      .= '<' . $args->container . $id . $class . '>';
 182          }
 183      }
 184  
 185      // Set up the $menu_item variables.
 186      _wp_menu_item_classes_by_context( $menu_items );
 187  
 188      $sorted_menu_items        = array();
 189      $menu_items_with_children = array();
 190      foreach ( (array) $menu_items as $menu_item ) {
 191          $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
 192          if ( $menu_item->menu_item_parent ) {
 193              $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
 194          }
 195      }
 196  
 197      // Add the menu-item-has-children class where applicable.
 198      if ( $menu_items_with_children ) {
 199          foreach ( $sorted_menu_items as &$menu_item ) {
 200              if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
 201                  $menu_item->classes[] = 'menu-item-has-children';
 202              }
 203          }
 204      }
 205  
 206      unset( $menu_items, $menu_item );
 207  
 208      /**
 209       * Filters the sorted list of menu item objects before generating the menu's HTML.
 210       *
 211       * @since 3.1.0
 212       *
 213       * @param array    $sorted_menu_items The menu items, sorted by each menu item's menu order.
 214       * @param stdClass $args              An object containing wp_nav_menu() arguments.
 215       */
 216      $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );
 217  
 218      $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
 219      unset( $sorted_menu_items );
 220  
 221      // Attributes.
 222      if ( ! empty( $args->menu_id ) ) {
 223          $wrap_id = $args->menu_id;
 224      } else {
 225          $wrap_id = 'menu-' . $menu->slug;
 226          while ( in_array( $wrap_id, $menu_id_slugs ) ) {
 227              if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) {
 228                  $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
 229              } else {
 230                  $wrap_id = $wrap_id . '-1';
 231              }
 232          }
 233      }
 234      $menu_id_slugs[] = $wrap_id;
 235  
 236      $wrap_class = $args->menu_class ? $args->menu_class : '';
 237  
 238      /**
 239       * Filters the HTML list content for navigation menus.
 240       *
 241       * @since 3.0.0
 242       *
 243       * @see wp_nav_menu()
 244       *
 245       * @param string   $items The HTML list content for the menu items.
 246       * @param stdClass $args  An object containing wp_nav_menu() arguments.
 247       */
 248      $items = apply_filters( 'wp_nav_menu_items', $items, $args );
 249      /**
 250       * Filters the HTML list content for a specific navigation menu.
 251       *
 252       * @since 3.0.0
 253       *
 254       * @see wp_nav_menu()
 255       *
 256       * @param string   $items The HTML list content for the menu items.
 257       * @param stdClass $args  An object containing wp_nav_menu() arguments.
 258       */
 259      $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
 260  
 261      // Don't print any markup if there are no items at this point.
 262      if ( empty( $items ) ) {
 263          return false;
 264      }
 265  
 266      $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
 267      unset( $items );
 268  
 269      if ( $show_container ) {
 270          $nav_menu .= '</' . $args->container . '>';
 271      }
 272  
 273      /**
 274       * Filters the HTML content for navigation menus.
 275       *
 276       * @since 3.0.0
 277       *
 278       * @see wp_nav_menu()
 279       *
 280       * @param string   $nav_menu The HTML content for the navigation menu.
 281       * @param stdClass $args     An object containing wp_nav_menu() arguments.
 282       */
 283      $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
 284  
 285      if ( $args->echo ) {
 286          echo $nav_menu;
 287      } else {
 288          return $nav_menu;
 289      }
 290  }
 291  
 292  /**
 293   * Add the class property classes for the current context, if applicable.
 294   *
 295   * @access private
 296   * @since 3.0.0
 297   *
 298   * @global WP_Query   $wp_query   WordPress Query object.
 299   * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 300   *
 301   * @param array $menu_items The current menu item objects to which to add the class property information.
 302   */
 303  function _wp_menu_item_classes_by_context( &$menu_items ) {
 304      global $wp_query, $wp_rewrite;
 305  
 306      $queried_object    = $wp_query->get_queried_object();
 307      $queried_object_id = (int) $wp_query->queried_object_id;
 308  
 309      $active_object               = '';
 310      $active_ancestor_item_ids    = array();
 311      $active_parent_item_ids      = array();
 312      $active_parent_object_ids    = array();
 313      $possible_taxonomy_ancestors = array();
 314      $possible_object_parents     = array();
 315      $home_page_id                = (int) get_option( 'page_for_posts' );
 316  
 317      if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {
 318          foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {
 319              if ( is_taxonomy_hierarchical( $taxonomy ) ) {
 320                  $term_hierarchy = _get_term_hierarchy( $taxonomy );
 321                  $terms          = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );
 322                  if ( is_array( $terms ) ) {
 323                      $possible_object_parents = array_merge( $possible_object_parents, $terms );
 324                      $term_to_ancestor        = array();
 325                      foreach ( (array) $term_hierarchy as $anc => $descs ) {
 326                          foreach ( (array) $descs as $desc ) {
 327                              $term_to_ancestor[ $desc ] = $anc;
 328                          }
 329                      }
 330  
 331                      foreach ( $terms as $desc ) {
 332                          do {
 333                              $possible_taxonomy_ancestors[ $taxonomy ][] = $desc;
 334                              if ( isset( $term_to_ancestor[ $desc ] ) ) {
 335                                  $_desc = $term_to_ancestor[ $desc ];
 336                                  unset( $term_to_ancestor[ $desc ] );
 337                                  $desc = $_desc;
 338                              } else {
 339                                  $desc = 0;
 340                              }
 341                          } while ( ! empty( $desc ) );
 342                      }
 343                  }
 344              }
 345          }
 346      } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {
 347          $term_hierarchy   = _get_term_hierarchy( $queried_object->taxonomy );
 348          $term_to_ancestor = array();
 349          foreach ( (array) $term_hierarchy as $anc => $descs ) {
 350              foreach ( (array) $descs as $desc ) {
 351                  $term_to_ancestor[ $desc ] = $anc;
 352              }
 353          }
 354          $desc = $queried_object->term_id;
 355          do {
 356              $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;
 357              if ( isset( $term_to_ancestor[ $desc ] ) ) {
 358                  $_desc = $term_to_ancestor[ $desc ];
 359                  unset( $term_to_ancestor[ $desc ] );
 360                  $desc = $_desc;
 361              } else {
 362                  $desc = 0;
 363              }
 364          } while ( ! empty( $desc ) );
 365      }
 366  
 367      $possible_object_parents = array_filter( $possible_object_parents );
 368  
 369      $front_page_url         = home_url();
 370      $front_page_id          = (int) get_option( 'page_on_front' );
 371      $privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
 372  
 373      foreach ( (array) $menu_items as $key => $menu_item ) {
 374  
 375          $menu_items[ $key ]->current = false;
 376  
 377          $classes   = (array) $menu_item->classes;
 378          $classes[] = 'menu-item';
 379          $classes[] = 'menu-item-type-' . $menu_item->type;
 380          $classes[] = 'menu-item-object-' . $menu_item->object;
 381  
 382          // This menu item is set as the 'Front Page'.
 383          if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) {
 384              $classes[] = 'menu-item-home';
 385          }
 386  
 387          // This menu item is set as the 'Privacy Policy Page'.
 388          if ( 'post_type' === $menu_item->type && $privacy_policy_page_id === (int) $menu_item->object_id ) {
 389              $classes[] = 'menu-item-privacy-policy';
 390          }
 391  
 392          // If the menu item corresponds to a taxonomy term for the currently queried non-hierarchical post object.
 393          if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) {
 394              $active_parent_object_ids[] = (int) $menu_item->object_id;
 395              $active_parent_item_ids[]   = (int) $menu_item->db_id;
 396              $active_object              = $queried_object->post_type;
 397  
 398              // If the menu item corresponds to the currently queried post or taxonomy object.
 399          } elseif (
 400              $menu_item->object_id == $queried_object_id &&
 401              (
 402                  ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) ||
 403                  ( 'post_type' == $menu_item->type && $wp_query->is_singular ) ||
 404                  ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object )
 405              )
 406          ) {
 407              $classes[]                   = 'current-menu-item';
 408              $menu_items[ $key ]->current = true;
 409              $_anc_id                     = (int) $menu_item->db_id;
 410  
 411              while (
 412                  ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
 413                  ! in_array( $_anc_id, $active_ancestor_item_ids )
 414              ) {
 415                  $active_ancestor_item_ids[] = $_anc_id;
 416              }
 417  
 418              if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) {
 419                  // Back compat classes for pages to match wp_page_menu().
 420                  $classes[] = 'page_item';
 421                  $classes[] = 'page-item-' . $menu_item->object_id;
 422                  $classes[] = 'current_page_item';
 423              }
 424  
 425              $active_parent_item_ids[]   = (int) $menu_item->menu_item_parent;
 426              $active_parent_object_ids[] = (int) $menu_item->post_parent;
 427              $active_object              = $menu_item->object;
 428  
 429              // If the menu item corresponds to the currently queried post type archive.
 430          } elseif (
 431              'post_type_archive' == $menu_item->type &&
 432              is_post_type_archive( array( $menu_item->object ) )
 433          ) {
 434              $classes[]                   = 'current-menu-item';
 435              $menu_items[ $key ]->current = true;
 436              $_anc_id                     = (int) $menu_item->db_id;
 437  
 438              while (
 439                  ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
 440                  ! in_array( $_anc_id, $active_ancestor_item_ids )
 441              ) {
 442                  $active_ancestor_item_ids[] = $_anc_id;
 443              }
 444  
 445              $active_parent_item_ids[] = (int) $menu_item->menu_item_parent;
 446  
 447              // If the menu item corresponds to the currently requested URL.
 448          } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) {
 449              $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] );
 450  
 451              // If it's the customize page then it will strip the query var off the URL before entering the comparison block.
 452              if ( is_customize_preview() ) {
 453                  $_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' );
 454              }
 455  
 456              $current_url        = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current );
 457              $raw_item_url       = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url;
 458              $item_url           = set_url_scheme( untrailingslashit( $raw_item_url ) );
 459              $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) );
 460  
 461              $matches = array(
 462                  $current_url,
 463                  urldecode( $current_url ),
 464                  $_indexless_current,
 465                  urldecode( $_indexless_current ),
 466                  $_root_relative_current,
 467                  urldecode( $_root_relative_current ),
 468              );
 469  
 470              if ( $raw_item_url && in_array( $item_url, $matches ) ) {
 471                  $classes[]                   = 'current-menu-item';
 472                  $menu_items[ $key ]->current = true;
 473                  $_anc_id                     = (int) $menu_item->db_id;
 474  
 475                  while (
 476                      ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) &&
 477                      ! in_array( $_anc_id, $active_ancestor_item_ids )
 478                  ) {
 479                      $active_ancestor_item_ids[] = $_anc_id;
 480                  }
 481  
 482                  if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) {
 483                      // Back compat for home link to match wp_page_menu().
 484                      $classes[] = 'current_page_item';
 485                  }
 486                  $active_parent_item_ids[]   = (int) $menu_item->menu_item_parent;
 487                  $active_parent_object_ids[] = (int) $menu_item->post_parent;
 488                  $active_object              = $menu_item->object;
 489  
 490                  // Give front page item the 'current-menu-item' class when extra query arguments are involved.
 491              } elseif ( $item_url == $front_page_url && is_front_page() ) {
 492                  $classes[] = 'current-menu-item';
 493              }
 494  
 495              if ( untrailingslashit( $item_url ) == home_url() ) {
 496                  $classes[] = 'menu-item-home';
 497              }
 498          }
 499  
 500          // Back-compat with wp_page_menu(): add "current_page_parent" to static home page link for any non-page query.
 501          if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ) {
 502              $classes[] = 'current_page_parent';
 503          }
 504  
 505          $menu_items[ $key ]->classes = array_unique( $classes );
 506      }
 507      $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) );
 508      $active_parent_item_ids   = array_filter( array_unique( $active_parent_item_ids ) );
 509      $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) );
 510  
 511      // Set parent's class.
 512      foreach ( (array) $menu_items as $key => $parent_item ) {
 513          $classes                                   = (array) $parent_item->classes;
 514          $menu_items[ $key ]->current_item_ancestor = false;
 515          $menu_items[ $key ]->current_item_parent   = false;
 516  
 517          if (
 518              isset( $parent_item->type ) &&
 519              (
 520                  // Ancestral post object.
 521                  (
 522                      'post_type' == $parent_item->type &&
 523                      ! empty( $queried_object->post_type ) &&
 524                      is_post_type_hierarchical( $queried_object->post_type ) &&
 525                      in_array( $parent_item->object_id, $queried_object->ancestors ) &&
 526                      $parent_item->object != $queried_object->ID
 527                  ) ||
 528  
 529                  // Ancestral term.
 530                  (
 531                      'taxonomy' == $parent_item->type &&
 532                      isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
 533                      in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) &&
 534                      (
 535                          ! isset( $queried_object->term_id ) ||
 536                          $parent_item->object_id != $queried_object->term_id
 537                      )
 538                  )
 539              )
 540          ) {
 541              $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor';
 542          }
 543  
 544          if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) {
 545              $classes[]                                 = 'current-menu-ancestor';
 546              $menu_items[ $key ]->current_item_ancestor = true;
 547          }
 548          if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) {
 549              $classes[]                               = 'current-menu-parent';
 550              $menu_items[ $key ]->current_item_parent = true;
 551          }
 552          if ( in_array( $parent_item->object_id, $active_parent_object_ids ) ) {
 553              $classes[] = 'current-' . $active_object . '-parent';
 554          }
 555  
 556          if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) {
 557              // Back compat classes for pages to match wp_page_menu().
 558              if ( in_array( 'current-menu-parent', $classes ) ) {
 559                  $classes[] = 'current_page_parent';
 560              }
 561              if ( in_array( 'current-menu-ancestor', $classes ) ) {
 562                  $classes[] = 'current_page_ancestor';
 563              }
 564          }
 565  
 566          $menu_items[ $key ]->classes = array_unique( $classes );
 567      }
 568  }
 569  
 570  /**
 571   * Retrieve the HTML list content for nav menu items.
 572   *
 573   * @uses Walker_Nav_Menu to create HTML list content.
 574   * @since 3.0.0
 575   *
 576   * @param array    $items The menu items, sorted by each menu item's menu order.
 577   * @param int      $depth Depth of the item in reference to parents.
 578   * @param stdClass $r     An object containing wp_nav_menu() arguments.
 579   * @return string The HTML list content for the menu items.
 580   */
 581  function walk_nav_menu_tree( $items, $depth, $r ) {
 582      $walker = ( empty( $r->walker ) ) ? new Walker_Nav_Menu : $r->walker;
 583  
 584      return $walker->walk( $items, $depth, $r );
 585  }
 586  
 587  /**
 588   * Prevents a menu item ID from being used more than once.
 589   *
 590   * @since 3.0.1
 591   * @access private
 592   *
 593   * @staticvar array $used_ids
 594   * @param string $id
 595   * @param object $item
 596   * @return string
 597   */
 598  function _nav_menu_item_id_use_once( $id, $item ) {
 599      static $_used_ids = array();
 600      if ( in_array( $item->ID, $_used_ids ) ) {
 601          return '';
 602      }
 603      $_used_ids[] = $item->ID;
 604      return $id;
 605  }


Generated : Wed Apr 1 08:20:01 2020 Cross-referenced by PHPXref