[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sat Jul 4 01:00:03 2020 Cross-referenced by PHPXref 0.7.1