[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Navigation Menu functions
   4   *
   5   * @package WordPress
   6   * @subpackage Nav_Menus
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Returns a navigation menu object.
  12   *
  13   * @since 3.0.0
  14   *
  15   * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
  16   * @return WP_Term|false False if $menu param isn't supplied or term does not exist, menu object if successful.
  17   */
  18  function wp_get_nav_menu_object( $menu ) {
  19      $menu_obj = false;
  20  
  21      if ( is_object( $menu ) ) {
  22          $menu_obj = $menu;
  23      }
  24  
  25      if ( $menu && ! $menu_obj ) {
  26          $menu_obj = get_term( $menu, 'nav_menu' );
  27  
  28          if ( ! $menu_obj ) {
  29              $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
  30          }
  31  
  32          if ( ! $menu_obj ) {
  33              $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
  34          }
  35      }
  36  
  37      if ( ! $menu_obj || is_wp_error( $menu_obj ) ) {
  38          $menu_obj = false;
  39      }
  40  
  41      /**
  42       * Filters the nav_menu term retrieved for wp_get_nav_menu_object().
  43       *
  44       * @since 4.3.0
  45       *
  46       * @param WP_Term|false      $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
  47       * @param int|string|WP_Term $menu     The menu ID, slug, name, or object passed to wp_get_nav_menu_object().
  48       */
  49      return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu );
  50  }
  51  
  52  /**
  53   * Check if the given ID is a navigation menu.
  54   *
  55   * Returns true if it is; false otherwise.
  56   *
  57   * @since 3.0.0
  58   *
  59   * @param int|string|WP_Term $menu Menu ID, slug, name, or object of menu to check.
  60   * @return bool Whether the menu exists.
  61   */
  62  function is_nav_menu( $menu ) {
  63      if ( ! $menu ) {
  64          return false;
  65      }
  66  
  67      $menu_obj = wp_get_nav_menu_object( $menu );
  68  
  69      if (
  70          $menu_obj &&
  71          ! is_wp_error( $menu_obj ) &&
  72          ! empty( $menu_obj->taxonomy ) &&
  73          'nav_menu' == $menu_obj->taxonomy
  74      ) {
  75          return true;
  76      }
  77  
  78      return false;
  79  }
  80  
  81  /**
  82   * Registers navigation menu locations for a theme.
  83   *
  84   * @since 3.0.0
  85   *
  86   * @global array $_wp_registered_nav_menus
  87   *
  88   * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
  89   */
  90  function register_nav_menus( $locations = array() ) {
  91      global $_wp_registered_nav_menus;
  92  
  93      add_theme_support( 'menus' );
  94  
  95      foreach ( $locations as $key => $value ) {
  96          if ( is_int( $key ) ) {
  97              _doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' );
  98              break;
  99          }
 100      }
 101  
 102      $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
 103  }
 104  
 105  /**
 106   * Unregisters a navigation menu location for a theme.
 107   *
 108   * @since 3.1.0
 109   * @global array $_wp_registered_nav_menus
 110   *
 111   * @param string $location The menu location identifier.
 112   * @return bool True on success, false on failure.
 113   */
 114  function unregister_nav_menu( $location ) {
 115      global $_wp_registered_nav_menus;
 116  
 117      if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[ $location ] ) ) {
 118          unset( $_wp_registered_nav_menus[ $location ] );
 119          if ( empty( $_wp_registered_nav_menus ) ) {
 120              _remove_theme_support( 'menus' );
 121          }
 122          return true;
 123      }
 124      return false;
 125  }
 126  
 127  /**
 128   * Registers a navigation menu location for a theme.
 129   *
 130   * @since 3.0.0
 131   *
 132   * @param string $location    Menu location identifier, like a slug.
 133   * @param string $description Menu location descriptive text.
 134   */
 135  function register_nav_menu( $location, $description ) {
 136      register_nav_menus( array( $location => $description ) );
 137  }
 138  /**
 139   * Retrieves all registered navigation menu locations in a theme.
 140   *
 141   * @since 3.0.0
 142   *
 143   * @global array $_wp_registered_nav_menus
 144   *
 145   * @return array Registered navigation menu locations. If none are registered, an empty array.
 146   */
 147  function get_registered_nav_menus() {
 148      global $_wp_registered_nav_menus;
 149      if ( isset( $_wp_registered_nav_menus ) ) {
 150          return $_wp_registered_nav_menus;
 151      }
 152      return array();
 153  }
 154  
 155  /**
 156   * Retrieves all registered navigation menu locations and the menus assigned to them.
 157   *
 158   * @since 3.0.0
 159   *
 160   * @return array Registered navigation menu locations and the menus assigned them.
 161   *               If none are registered, an empty array.
 162   */
 163  
 164  function get_nav_menu_locations() {
 165      $locations = get_theme_mod( 'nav_menu_locations' );
 166      return ( is_array( $locations ) ) ? $locations : array();
 167  }
 168  
 169  /**
 170   * Determines whether a registered nav menu location has a menu assigned to it.
 171   *
 172   * @since 3.0.0
 173   *
 174   * @param string $location Menu location identifier.
 175   * @return bool Whether location has a menu.
 176   */
 177  function has_nav_menu( $location ) {
 178      $has_nav_menu = false;
 179  
 180      $registered_nav_menus = get_registered_nav_menus();
 181      if ( isset( $registered_nav_menus[ $location ] ) ) {
 182          $locations    = get_nav_menu_locations();
 183          $has_nav_menu = ! empty( $locations[ $location ] );
 184      }
 185  
 186      /**
 187       * Filters whether a nav menu is assigned to the specified location.
 188       *
 189       * @since 4.3.0
 190       *
 191       * @param bool   $has_nav_menu Whether there is a menu assigned to a location.
 192       * @param string $location     Menu location.
 193       */
 194      return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
 195  }
 196  
 197  /**
 198   * Returns the name of a navigation menu.
 199   *
 200   * @since 4.9.0
 201   *
 202   * @param string $location Menu location identifier.
 203   * @return string Menu name.
 204   */
 205  function wp_get_nav_menu_name( $location ) {
 206      $menu_name = '';
 207  
 208      $locations = get_nav_menu_locations();
 209  
 210      if ( isset( $locations[ $location ] ) ) {
 211          $menu = wp_get_nav_menu_object( $locations[ $location ] );
 212  
 213          if ( $menu && $menu->name ) {
 214              $menu_name = $menu->name;
 215          }
 216      }
 217  
 218      /**
 219       * Filters the navigation menu name being returned.
 220       *
 221       * @since 4.9.0
 222       *
 223       * @param string $menu_name Menu name.
 224       * @param string $location  Menu location identifier.
 225       */
 226      return apply_filters( 'wp_get_nav_menu_name', $menu_name, $location );
 227  }
 228  
 229  /**
 230   * Determines whether the given ID is a nav menu item.
 231   *
 232   * @since 3.0.0
 233   *
 234   * @param int $menu_item_id The ID of the potential nav menu item.
 235   * @return bool Whether the given ID is that of a nav menu item.
 236   */
 237  function is_nav_menu_item( $menu_item_id = 0 ) {
 238      return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
 239  }
 240  
 241  /**
 242   * Creates a navigation menu.
 243   *
 244   * Note that `$menu_name` is expected to be pre-slashed.
 245   *
 246   * @since 3.0.0
 247   *
 248   * @param string $menu_name Menu name.
 249   * @return int|WP_Error Menu ID on success, WP_Error object on failure.
 250   */
 251  function wp_create_nav_menu( $menu_name ) {
 252      // expected_slashed ($menu_name)
 253      return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
 254  }
 255  
 256  /**
 257   * Delete a Navigation Menu.
 258   *
 259   * @since 3.0.0
 260   *
 261   * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
 262   * @return bool|WP_Error True on success, false or WP_Error object on failure.
 263   */
 264  function wp_delete_nav_menu( $menu ) {
 265      $menu = wp_get_nav_menu_object( $menu );
 266      if ( ! $menu ) {
 267          return false;
 268      }
 269  
 270      $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
 271      if ( ! empty( $menu_objects ) ) {
 272          foreach ( $menu_objects as $item ) {
 273              wp_delete_post( $item );
 274          }
 275      }
 276  
 277      $result = wp_delete_term( $menu->term_id, 'nav_menu' );
 278  
 279      // Remove this menu from any locations.
 280      $locations = get_nav_menu_locations();
 281      foreach ( $locations as $location => $menu_id ) {
 282          if ( $menu_id == $menu->term_id ) {
 283              $locations[ $location ] = 0;
 284          }
 285      }
 286      set_theme_mod( 'nav_menu_locations', $locations );
 287  
 288      if ( $result && ! is_wp_error( $result ) ) {
 289  
 290          /**
 291           * Fires after a navigation menu has been successfully deleted.
 292           *
 293           * @since 3.0.0
 294           *
 295           * @param int $term_id ID of the deleted menu.
 296           */
 297          do_action( 'wp_delete_nav_menu', $menu->term_id );
 298      }
 299  
 300      return $result;
 301  }
 302  
 303  /**
 304   * Save the properties of a menu or create a new menu with those properties.
 305   *
 306   * Note that `$menu_data` is expected to be pre-slashed.
 307   *
 308   * @since 3.0.0
 309   *
 310   * @param int   $menu_id   The ID of the menu or "0" to create a new menu.
 311   * @param array $menu_data The array of menu data.
 312   * @return int|WP_Error Menu ID on success, WP_Error object on failure.
 313   */
 314  function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
 315      // expected_slashed ($menu_data)
 316      $menu_id = (int) $menu_id;
 317  
 318      $_menu = wp_get_nav_menu_object( $menu_id );
 319  
 320      $args = array(
 321          'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
 322          'name'        => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
 323          'parent'      => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
 324          'slug'        => null,
 325      );
 326  
 327      // double-check that we're not going to have one menu take the name of another
 328      $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
 329      if (
 330          $_possible_existing &&
 331          ! is_wp_error( $_possible_existing ) &&
 332          isset( $_possible_existing->term_id ) &&
 333          $_possible_existing->term_id != $menu_id
 334      ) {
 335          return new WP_Error(
 336              'menu_exists',
 337              sprintf(
 338                  /* translators: %s: Menu name. */
 339                  __( 'The menu name %s conflicts with another menu name. Please try another.' ),
 340                  '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
 341              )
 342          );
 343      }
 344  
 345      // menu doesn't already exist, so create a new menu
 346      if ( ! $_menu || is_wp_error( $_menu ) ) {
 347          $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
 348  
 349          if ( $menu_exists ) {
 350              return new WP_Error(
 351                  'menu_exists',
 352                  sprintf(
 353                      /* translators: %s: Menu name. */
 354                      __( 'The menu name %s conflicts with another menu name. Please try another.' ),
 355                      '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
 356                  )
 357              );
 358          }
 359  
 360          $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
 361  
 362          if ( is_wp_error( $_menu ) ) {
 363              return $_menu;
 364          }
 365  
 366          /**
 367           * Fires after a navigation menu is successfully created.
 368           *
 369           * @since 3.0.0
 370           *
 371           * @param int   $term_id   ID of the new menu.
 372           * @param array $menu_data An array of menu data.
 373           */
 374          do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
 375  
 376          return (int) $_menu['term_id'];
 377      }
 378  
 379      if ( ! $_menu || ! isset( $_menu->term_id ) ) {
 380          return 0;
 381      }
 382  
 383      $menu_id = (int) $_menu->term_id;
 384  
 385      $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
 386  
 387      if ( is_wp_error( $update_response ) ) {
 388          return $update_response;
 389      }
 390  
 391      $menu_id = (int) $update_response['term_id'];
 392  
 393      /**
 394       * Fires after a navigation menu has been successfully updated.
 395       *
 396       * @since 3.0.0
 397       *
 398       * @param int   $menu_id   ID of the updated menu.
 399       * @param array $menu_data An array of menu data.
 400       */
 401      do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
 402      return $menu_id;
 403  }
 404  
 405  /**
 406   * Save the properties of a menu item or create a new one.
 407   *
 408   * The menu-item-title, menu-item-description, and menu-item-attr-title are expected
 409   * to be pre-slashed since they are passed directly into `wp_insert_post()`.
 410   *
 411   * @since 3.0.0
 412   *
 413   * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
 414   * @param int   $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
 415   * @param array $menu_item_data  The menu item's data.
 416   * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
 417   */
 418  function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
 419      $menu_id         = (int) $menu_id;
 420      $menu_item_db_id = (int) $menu_item_db_id;
 421  
 422      // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
 423      if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) {
 424          return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
 425      }
 426  
 427      $menu = wp_get_nav_menu_object( $menu_id );
 428  
 429      if ( ! $menu && 0 !== $menu_id ) {
 430          return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
 431      }
 432  
 433      if ( is_wp_error( $menu ) ) {
 434          return $menu;
 435      }
 436  
 437      $defaults = array(
 438          'menu-item-db-id'       => $menu_item_db_id,
 439          'menu-item-object-id'   => 0,
 440          'menu-item-object'      => '',
 441          'menu-item-parent-id'   => 0,
 442          'menu-item-position'    => 0,
 443          'menu-item-type'        => 'custom',
 444          'menu-item-title'       => '',
 445          'menu-item-url'         => '',
 446          'menu-item-description' => '',
 447          'menu-item-attr-title'  => '',
 448          'menu-item-target'      => '',
 449          'menu-item-classes'     => '',
 450          'menu-item-xfn'         => '',
 451          'menu-item-status'      => '',
 452      );
 453  
 454      $args = wp_parse_args( $menu_item_data, $defaults );
 455  
 456      if ( 0 == $menu_id ) {
 457          $args['menu-item-position'] = 1;
 458      } elseif ( 0 == (int) $args['menu-item-position'] ) {
 459          $menu_items                 = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
 460          $last_item                  = array_pop( $menu_items );
 461          $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
 462      }
 463  
 464      $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
 465  
 466      if ( 'custom' === $args['menu-item-type'] ) {
 467          // If custom menu item, trim the URL.
 468          $args['menu-item-url'] = trim( $args['menu-item-url'] );
 469      } else {
 470          /*
 471           * If non-custom menu item, then:
 472           * - use the original object's URL.
 473           * - blank default title to sync with the original object's title.
 474           */
 475  
 476          $args['menu-item-url'] = '';
 477  
 478          $original_title = '';
 479          if ( 'taxonomy' == $args['menu-item-type'] ) {
 480              $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
 481              $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
 482          } elseif ( 'post_type' == $args['menu-item-type'] ) {
 483  
 484              $original_object = get_post( $args['menu-item-object-id'] );
 485              $original_parent = (int) $original_object->post_parent;
 486              $original_title  = $original_object->post_title;
 487          } elseif ( 'post_type_archive' == $args['menu-item-type'] ) {
 488              $original_object = get_post_type_object( $args['menu-item-object'] );
 489              if ( $original_object ) {
 490                  $original_title = $original_object->labels->archives;
 491              }
 492          }
 493  
 494          if ( $args['menu-item-title'] == $original_title ) {
 495              $args['menu-item-title'] = '';
 496          }
 497  
 498          // hack to get wp to create a post object when too many properties are empty
 499          if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] ) {
 500              $args['menu-item-description'] = ' ';
 501          }
 502      }
 503  
 504      // Populate the menu item object
 505      $post = array(
 506          'menu_order'   => $args['menu-item-position'],
 507          'ping_status'  => 0,
 508          'post_content' => $args['menu-item-description'],
 509          'post_excerpt' => $args['menu-item-attr-title'],
 510          'post_parent'  => $original_parent,
 511          'post_title'   => $args['menu-item-title'],
 512          'post_type'    => 'nav_menu_item',
 513      );
 514  
 515      $update = 0 != $menu_item_db_id;
 516  
 517      // New menu item. Default is draft status
 518      if ( ! $update ) {
 519          $post['ID']          = 0;
 520          $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
 521          $menu_item_db_id     = wp_insert_post( $post );
 522          if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
 523              return $menu_item_db_id;
 524          }
 525  
 526          /**
 527           * Fires immediately after a new navigation menu item has been added.
 528           *
 529           * @since 4.4.0
 530           *
 531           * @see wp_update_nav_menu_item()
 532           *
 533           * @param int   $menu_id         ID of the updated menu.
 534           * @param int   $menu_item_db_id ID of the new menu item.
 535           * @param array $args            An array of arguments used to update/add the menu item.
 536           */
 537          do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args );
 538      }
 539  
 540      // Associate the menu item with the menu term
 541      // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
 542      if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
 543          wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
 544      }
 545  
 546      if ( 'custom' == $args['menu-item-type'] ) {
 547          $args['menu-item-object-id'] = $menu_item_db_id;
 548          $args['menu-item-object']    = 'custom';
 549      }
 550  
 551      $menu_item_db_id = (int) $menu_item_db_id;
 552  
 553      update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) );
 554      update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
 555      update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
 556      update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) );
 557      update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) );
 558  
 559      $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
 560      $args['menu-item-xfn']     = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
 561      update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
 562      update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
 563      update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) );
 564  
 565      if ( 0 == $menu_id ) {
 566          update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
 567      } elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) ) {
 568          delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
 569      }
 570  
 571      // Update existing menu item. Default is publish status
 572      if ( $update ) {
 573          $post['ID']          = $menu_item_db_id;
 574          $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
 575          wp_update_post( $post );
 576      }
 577  
 578      /**
 579       * Fires after a navigation menu item has been updated.
 580       *
 581       * @since 3.0.0
 582       *
 583       * @see wp_update_nav_menu_item()
 584       *
 585       * @param int   $menu_id         ID of the updated menu.
 586       * @param int   $menu_item_db_id ID of the updated menu item.
 587       * @param array $args            An array of arguments used to update a menu item.
 588       */
 589      do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
 590  
 591      return $menu_item_db_id;
 592  }
 593  
 594  /**
 595   * Returns all navigation menu objects.
 596   *
 597   * @since 3.0.0
 598   * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
 599   *              to 'name'.
 600   *
 601   * @param array $args Optional. Array of arguments passed on to get_terms().
 602   *                    Default empty array.
 603   * @return WP_Term[] An array of menu objects.
 604   */
 605  function wp_get_nav_menus( $args = array() ) {
 606      $defaults = array(
 607          'taxonomy'   => 'nav_menu',
 608          'hide_empty' => false,
 609          'orderby'    => 'name',
 610      );
 611      $args     = wp_parse_args( $args, $defaults );
 612  
 613      /**
 614       * Filters the navigation menu objects being returned.
 615       *
 616       * @since 3.0.0
 617       *
 618       * @see get_terms()
 619       *
 620       * @param WP_Term[] $menus An array of menu objects.
 621       * @param array     $args  An array of arguments used to retrieve menu objects.
 622       */
 623      return apply_filters( 'wp_get_nav_menus', get_terms( $args ), $args );
 624  }
 625  
 626  /**
 627   * Return if a menu item is valid.
 628   *
 629   * @link https://core.trac.wordpress.org/ticket/13958
 630   *
 631   * @since 3.2.0
 632   * @access private
 633   *
 634   * @param object $item The menu item to check.
 635   * @return bool False if invalid, otherwise true.
 636   */
 637  function _is_valid_nav_menu_item( $item ) {
 638      return empty( $item->_invalid );
 639  }
 640  
 641  /**
 642   * Retrieves all menu items of a navigation menu.
 643   *
 644   * Note: Most arguments passed to the `$args` parameter – save for 'output_key' – are
 645   * specifically for retrieving nav_menu_item posts from get_posts() and may only
 646   * indirectly affect the ultimate ordering and content of the resulting nav menu
 647   * items that get returned from this function.
 648   *
 649   * @since 3.0.0
 650   *
 651   * @staticvar array $fetched
 652   *
 653   * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
 654   * @param array              $args {
 655   *     Optional. Arguments to pass to get_posts().
 656   *
 657   *     @type string $order       How to order nav menu items as queried with get_posts(). Will be ignored
 658   *                               if 'output' is ARRAY_A. Default 'ASC'.
 659   *     @type string $orderby     Field to order menu items by as retrieved from get_posts(). Supply an orderby
 660   *                               field via 'output_key' to affect the output order of nav menu items.
 661   *                               Default 'menu_order'.
 662   *     @type string $post_type   Menu items post type. Default 'nav_menu_item'.
 663   *     @type string $post_status Menu items post status. Default 'publish'.
 664   *     @type string $output      How to order outputted menu items. Default ARRAY_A.
 665   *     @type string $output_key  Key to use for ordering the actual menu items that get returned. Note that
 666   *                               that is not a get_posts() argument and will only affect output of menu items
 667   *                               processed in this function. Default 'menu_order'.
 668   *     @type bool   $nopaging    Whether to retrieve all menu items (true) or paginate (false). Default true.
 669   * }
 670   * @return false|array $items Array of menu items, otherwise false.
 671   */
 672  function wp_get_nav_menu_items( $menu, $args = array() ) {
 673      $menu = wp_get_nav_menu_object( $menu );
 674  
 675      if ( ! $menu ) {
 676          return false;
 677      }
 678  
 679      static $fetched = array();
 680  
 681      $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
 682      if ( is_wp_error( $items ) ) {
 683          return false;
 684      }
 685  
 686      $defaults        = array(
 687          'order'       => 'ASC',
 688          'orderby'     => 'menu_order',
 689          'post_type'   => 'nav_menu_item',
 690          'post_status' => 'publish',
 691          'output'      => ARRAY_A,
 692          'output_key'  => 'menu_order',
 693          'nopaging'    => true,
 694      );
 695      $args            = wp_parse_args( $args, $defaults );
 696      $args['include'] = $items;
 697  
 698      if ( ! empty( $items ) ) {
 699          $items = get_posts( $args );
 700      } else {
 701          $items = array();
 702      }
 703  
 704      // Get all posts and terms at once to prime the caches
 705      if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) {
 706          $fetched[ $menu->term_id ] = true;
 707          $posts                     = array();
 708          $terms                     = array();
 709          foreach ( $items as $item ) {
 710              $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
 711              $object    = get_post_meta( $item->ID, '_menu_item_object', true );
 712              $type      = get_post_meta( $item->ID, '_menu_item_type', true );
 713  
 714              if ( 'post_type' == $type ) {
 715                  $posts[ $object ][] = $object_id;
 716              } elseif ( 'taxonomy' == $type ) {
 717                  $terms[ $object ][] = $object_id;
 718              }
 719          }
 720  
 721          if ( ! empty( $posts ) ) {
 722              foreach ( array_keys( $posts ) as $post_type ) {
 723                  get_posts(
 724                      array(
 725                          'post__in'               => $posts[ $post_type ],
 726                          'post_type'              => $post_type,
 727                          'nopaging'               => true,
 728                          'update_post_term_cache' => false,
 729                      )
 730                  );
 731              }
 732          }
 733          unset( $posts );
 734  
 735          if ( ! empty( $terms ) ) {
 736              foreach ( array_keys( $terms ) as $taxonomy ) {
 737                  get_terms(
 738                      array(
 739                          'taxonomy'     => $taxonomy,
 740                          'include'      => $terms[ $taxonomy ],
 741                          'hierarchical' => false,
 742                      )
 743                  );
 744              }
 745          }
 746          unset( $terms );
 747      }
 748  
 749      $items = array_map( 'wp_setup_nav_menu_item', $items );
 750  
 751      if ( ! is_admin() ) { // Remove invalid items only in front end
 752          $items = array_filter( $items, '_is_valid_nav_menu_item' );
 753      }
 754  
 755      if ( ARRAY_A == $args['output'] ) {
 756          $items = wp_list_sort(
 757              $items,
 758              array(
 759                  $args['output_key'] => 'ASC',
 760              )
 761          );
 762          $i     = 1;
 763          foreach ( $items as $k => $item ) {
 764              $items[ $k ]->{$args['output_key']} = $i++;
 765          }
 766      }
 767  
 768      /**
 769       * Filters the navigation menu items being returned.
 770       *
 771       * @since 3.0.0
 772       *
 773       * @param array  $items An array of menu item post objects.
 774       * @param object $menu  The menu object.
 775       * @param array  $args  An array of arguments used to retrieve menu item objects.
 776       */
 777      return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
 778  }
 779  
 780  /**
 781   * Decorates a menu item object with the shared navigation menu item properties.
 782   *
 783   * Properties:
 784   * - ID:               The term_id if the menu item represents a taxonomy term.
 785   * - attr_title:       The title attribute of the link element for this menu item.
 786   * - classes:          The array of class attribute values for the link element of this menu item.
 787   * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
 788   * - description:      The description of this menu item.
 789   * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
 790   * - object:           The type of object originally represented, such as "category," "post", or "attachment."
 791   * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
 792   * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
 793   * - post_title:       A "no title" label if menu item represents a post that lacks a title.
 794   * - target:           The target attribute of the link element for this menu item.
 795   * - title:            The title of this menu item.
 796   * - type:             The family of objects originally represented, such as "post_type" or "taxonomy."
 797   * - type_label:       The singular label used to describe this type of menu item.
 798   * - url:              The URL to which this menu item points.
 799   * - xfn:              The XFN relationship expressed in the link of this menu item.
 800   * - _invalid:         Whether the menu item represents an object that no longer exists.
 801   *
 802   * @since 3.0.0
 803   *
 804   * @param object $menu_item The menu item to modify.
 805   * @return object $menu_item The menu item with standard menu item properties.
 806   */
 807  function wp_setup_nav_menu_item( $menu_item ) {
 808      if ( isset( $menu_item->post_type ) ) {
 809          if ( 'nav_menu_item' == $menu_item->post_type ) {
 810              $menu_item->db_id            = (int) $menu_item->ID;
 811              $menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
 812              $menu_item->object_id        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
 813              $menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
 814              $menu_item->type             = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
 815  
 816              if ( 'post_type' == $menu_item->type ) {
 817                  $object = get_post_type_object( $menu_item->object );
 818                  if ( $object ) {
 819                      $menu_item->type_label = $object->labels->singular_name;
 820                  } else {
 821                      $menu_item->type_label = $menu_item->object;
 822                      $menu_item->_invalid   = true;
 823                  }
 824  
 825                  if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
 826                      $menu_item->_invalid = true;
 827                  }
 828  
 829                  $original_object = get_post( $menu_item->object_id );
 830  
 831                  if ( $original_object ) {
 832                      $menu_item->url = get_permalink( $original_object->ID );
 833                      /** This filter is documented in wp-includes/post-template.php */
 834                      $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
 835                  } else {
 836                      $menu_item->url      = '';
 837                      $original_title      = '';
 838                      $menu_item->_invalid = true;
 839                  }
 840  
 841                  if ( '' === $original_title ) {
 842                      /* translators: %d: ID of a post. */
 843                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 844                  }
 845  
 846                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 847  
 848              } elseif ( 'post_type_archive' == $menu_item->type ) {
 849                  $object = get_post_type_object( $menu_item->object );
 850                  if ( $object ) {
 851                      $menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
 852                      $post_type_description = $object->description;
 853                  } else {
 854                      $post_type_description = '';
 855                      $menu_item->_invalid   = true;
 856                  }
 857  
 858                  $menu_item->type_label = __( 'Post Type Archive' );
 859                  $post_content          = wp_trim_words( $menu_item->post_content, 200 );
 860                  $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
 861                  $menu_item->url        = get_post_type_archive_link( $menu_item->object );
 862  
 863              } elseif ( 'taxonomy' == $menu_item->type ) {
 864                  $object = get_taxonomy( $menu_item->object );
 865                  if ( $object ) {
 866                      $menu_item->type_label = $object->labels->singular_name;
 867                  } else {
 868                      $menu_item->type_label = $menu_item->object;
 869                      $menu_item->_invalid   = true;
 870                  }
 871  
 872                  $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );
 873  
 874                  if ( $original_object && ! is_wp_error( $original_object ) ) {
 875                      $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
 876                      $original_title = $original_object->name;
 877                  } else {
 878                      $menu_item->url      = '';
 879                      $original_title      = '';
 880                      $menu_item->_invalid = true;
 881                  }
 882  
 883                  if ( '' === $original_title ) {
 884                      /* translators: %d: ID of a term. */
 885                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 886                  }
 887  
 888                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 889  
 890              } else {
 891                  $menu_item->type_label = __( 'Custom Link' );
 892                  $menu_item->title      = $menu_item->post_title;
 893                  $menu_item->url        = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
 894              }
 895  
 896              $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
 897  
 898              /**
 899               * Filters a navigation menu item's title attribute.
 900               *
 901               * @since 3.0.0
 902               *
 903               * @param string $item_title The menu item title attribute.
 904               */
 905              $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
 906  
 907              if ( ! isset( $menu_item->description ) ) {
 908                  /**
 909                   * Filters a navigation menu item's description.
 910                   *
 911                   * @since 3.0.0
 912                   *
 913                   * @param string $description The menu item description.
 914                   */
 915                  $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
 916              }
 917  
 918              $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
 919              $menu_item->xfn     = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
 920          } else {
 921              $menu_item->db_id            = 0;
 922              $menu_item->menu_item_parent = 0;
 923              $menu_item->object_id        = (int) $menu_item->ID;
 924              $menu_item->type             = 'post_type';
 925  
 926              $object                = get_post_type_object( $menu_item->post_type );
 927              $menu_item->object     = $object->name;
 928              $menu_item->type_label = $object->labels->singular_name;
 929  
 930              if ( '' === $menu_item->post_title ) {
 931                  /* translators: %d: ID of a post. */
 932                  $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
 933              }
 934  
 935              $menu_item->title  = $menu_item->post_title;
 936              $menu_item->url    = get_permalink( $menu_item->ID );
 937              $menu_item->target = '';
 938  
 939              /** This filter is documented in wp-includes/nav-menu.php */
 940              $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
 941  
 942              /** This filter is documented in wp-includes/nav-menu.php */
 943              $menu_item->description = apply_filters( 'nav_menu_description', '' );
 944              $menu_item->classes     = array();
 945              $menu_item->xfn         = '';
 946          }
 947      } elseif ( isset( $menu_item->taxonomy ) ) {
 948          $menu_item->ID               = $menu_item->term_id;
 949          $menu_item->db_id            = 0;
 950          $menu_item->menu_item_parent = 0;
 951          $menu_item->object_id        = (int) $menu_item->term_id;
 952          $menu_item->post_parent      = (int) $menu_item->parent;
 953          $menu_item->type             = 'taxonomy';
 954  
 955          $object                = get_taxonomy( $menu_item->taxonomy );
 956          $menu_item->object     = $object->name;
 957          $menu_item->type_label = $object->labels->singular_name;
 958  
 959          $menu_item->title       = $menu_item->name;
 960          $menu_item->url         = get_term_link( $menu_item, $menu_item->taxonomy );
 961          $menu_item->target      = '';
 962          $menu_item->attr_title  = '';
 963          $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
 964          $menu_item->classes     = array();
 965          $menu_item->xfn         = '';
 966  
 967      }
 968  
 969      /**
 970       * Filters a navigation menu item object.
 971       *
 972       * @since 3.0.0
 973       *
 974       * @param object $menu_item The menu item object.
 975       */
 976      return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
 977  }
 978  
 979  /**
 980   * Get the menu items associated with a particular object.
 981   *
 982   * @since 3.0.0
 983   *
 984   * @param int    $object_id   The ID of the original object.
 985   * @param string $object_type The type of object, such as "taxonomy" or "post_type."
 986   * @param string $taxonomy    If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
 987   * @return int[] The array of menu item IDs; empty array if none;
 988   */
 989  function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
 990      $object_id     = (int) $object_id;
 991      $menu_item_ids = array();
 992  
 993      $query      = new WP_Query;
 994      $menu_items = $query->query(
 995          array(
 996              'meta_key'       => '_menu_item_object_id',
 997              'meta_value'     => $object_id,
 998              'post_status'    => 'any',
 999              'post_type'      => 'nav_menu_item',
1000              'posts_per_page' => -1,
1001          )
1002      );
1003      foreach ( (array) $menu_items as $menu_item ) {
1004          if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
1005              $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
1006              if (
1007                  'post_type' == $object_type &&
1008                  'post_type' == $menu_item_type
1009              ) {
1010                  $menu_item_ids[] = (int) $menu_item->ID;
1011              } elseif (
1012                  'taxonomy' == $object_type &&
1013                  'taxonomy' == $menu_item_type &&
1014                  get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
1015              ) {
1016                  $menu_item_ids[] = (int) $menu_item->ID;
1017              }
1018          }
1019      }
1020  
1021      return array_unique( $menu_item_ids );
1022  }
1023  
1024  /**
1025   * Callback for handling a menu item when its original object is deleted.
1026   *
1027   * @since 3.0.0
1028   * @access private
1029   *
1030   * @param int $object_id The ID of the original object being trashed.
1031   */
1032  function _wp_delete_post_menu_item( $object_id = 0 ) {
1033      $object_id = (int) $object_id;
1034  
1035      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
1036  
1037      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1038          wp_delete_post( $menu_item_id, true );
1039      }
1040  }
1041  
1042  /**
1043   * Serves as a callback for handling a menu item when its original object is deleted.
1044   *
1045   * @since 3.0.0
1046   * @access private
1047   *
1048   * @param int    $object_id Optional. The ID of the original object being trashed. Default 0.
1049   * @param int    $tt_id     Term taxonomy ID. Unused.
1050   * @param string $taxonomy  Taxonomy slug.
1051   */
1052  function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
1053      $object_id = (int) $object_id;
1054  
1055      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
1056  
1057      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1058          wp_delete_post( $menu_item_id, true );
1059      }
1060  }
1061  
1062  /**
1063   * Automatically add newly published page objects to menus with that as an option.
1064   *
1065   * @since 3.0.0
1066   * @access private
1067   *
1068   * @param string $new_status The new status of the post object.
1069   * @param string $old_status The old status of the post object.
1070   * @param object $post       The post object being transitioned from one status to another.
1071   */
1072  function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
1073      if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) {
1074          return;
1075      }
1076      if ( ! empty( $post->post_parent ) ) {
1077          return;
1078      }
1079      $auto_add = get_option( 'nav_menu_options' );
1080      if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) {
1081          return;
1082      }
1083      $auto_add = $auto_add['auto_add'];
1084      if ( empty( $auto_add ) || ! is_array( $auto_add ) ) {
1085          return;
1086      }
1087  
1088      $args = array(
1089          'menu-item-object-id' => $post->ID,
1090          'menu-item-object'    => $post->post_type,
1091          'menu-item-type'      => 'post_type',
1092          'menu-item-status'    => 'publish',
1093      );
1094  
1095      foreach ( $auto_add as $menu_id ) {
1096          $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
1097          if ( ! is_array( $items ) ) {
1098              continue;
1099          }
1100          foreach ( $items as $item ) {
1101              if ( $post->ID == $item->object_id ) {
1102                  continue 2;
1103              }
1104          }
1105          wp_update_nav_menu_item( $menu_id, 0, $args );
1106      }
1107  }
1108  
1109  /**
1110   * Delete auto-draft posts associated with the supplied changeset.
1111   *
1112   * @since 4.8.0
1113   * @access private
1114   *
1115   * @param int $post_id Post ID for the customize_changeset.
1116   */
1117  function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) {
1118      $post = get_post( $post_id );
1119  
1120      if ( ! $post || 'customize_changeset' !== $post->post_type ) {
1121          return;
1122      }
1123  
1124      $data = json_decode( $post->post_content, true );
1125      if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
1126          return;
1127      }
1128      remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1129      foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
1130          if ( empty( $stub_post_id ) ) {
1131              continue;
1132          }
1133          if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
1134              wp_delete_post( $stub_post_id, true );
1135          } elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
1136              wp_trash_post( $stub_post_id );
1137              delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
1138          }
1139      }
1140      add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1141  }
1142  
1143  /**
1144   * Handle menu config after theme change.
1145   *
1146   * @access private
1147   * @since 4.9.0
1148   */
1149  function _wp_menus_changed() {
1150      $old_nav_menu_locations    = get_option( 'theme_switch_menu_locations', array() );
1151      $new_nav_menu_locations    = get_nav_menu_locations();
1152      $mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
1153  
1154      set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
1155      delete_option( 'theme_switch_menu_locations' );
1156  }
1157  
1158  /**
1159   * Maps nav menu locations according to assignments in previously active theme.
1160   *
1161   * @since 4.9.0
1162   *
1163   * @param array $new_nav_menu_locations New nav menu locations assignments.
1164   * @param array $old_nav_menu_locations Old nav menu locations assignments.
1165   * @return array Nav menus mapped to new nav menu locations.
1166   */
1167  function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) {
1168      $registered_nav_menus   = get_registered_nav_menus();
1169      $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus );
1170  
1171      // Short-circuit if there are no old nav menu location assignments to map.
1172      if ( empty( $old_nav_menu_locations ) ) {
1173          return $new_nav_menu_locations;
1174      }
1175  
1176      // If old and new theme have just one location, map it and we're done.
1177      if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) {
1178          $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations );
1179          return $new_nav_menu_locations;
1180      }
1181  
1182      $old_locations = array_keys( $old_nav_menu_locations );
1183  
1184      // Map locations with the same slug.
1185      foreach ( $registered_nav_menus as $location => $name ) {
1186          if ( in_array( $location, $old_locations, true ) ) {
1187              $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ];
1188              unset( $old_nav_menu_locations[ $location ] );
1189          }
1190      }
1191  
1192      // If there are no old nav menu locations left, then we're done.
1193      if ( empty( $old_nav_menu_locations ) ) {
1194          return $new_nav_menu_locations;
1195      }
1196  
1197      /*
1198       * If old and new theme both have locations that contain phrases
1199       * from within the same group, make an educated guess and map it.
1200       */
1201      $common_slug_groups = array(
1202          array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ),
1203          array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ),
1204          array( 'social' ),
1205      );
1206  
1207      // Go through each group...
1208      foreach ( $common_slug_groups as $slug_group ) {
1209  
1210          // ...and see if any of these slugs...
1211          foreach ( $slug_group as $slug ) {
1212  
1213              // ...and any of the new menu locations...
1214              foreach ( $registered_nav_menus as $new_location => $name ) {
1215  
1216                  // ...actually match!
1217                  if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) {
1218                      continue;
1219                  } elseif ( is_numeric( $new_location ) && $new_location !== $slug ) {
1220                      continue;
1221                  }
1222  
1223                  // Then see if any of the old locations...
1224                  foreach ( $old_nav_menu_locations as $location => $menu_id ) {
1225  
1226                      // ...and any slug in the same group...
1227                      foreach ( $slug_group as $slug ) {
1228  
1229                          // ... have a match as well.
1230                          if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) {
1231                              continue;
1232                          } elseif ( is_numeric( $location ) && $location !== $slug ) {
1233                              continue;
1234                          }
1235  
1236                          // Make sure this location wasn't mapped and removed previously.
1237                          if ( ! empty( $old_nav_menu_locations[ $location ] ) ) {
1238  
1239                              // We have a match that can be mapped!
1240                              $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ];
1241  
1242                              // Remove the mapped location so it can't be mapped again.
1243                              unset( $old_nav_menu_locations[ $location ] );
1244  
1245                              // Go back and check the next new menu location.
1246                              continue 3;
1247                          }
1248                      } // endforeach ( $slug_group as $slug )
1249                  } // endforeach ( $old_nav_menu_locations as $location => $menu_id )
1250              } // endforeach foreach ( $registered_nav_menus as $new_location => $name )
1251          } // endforeach ( $slug_group as $slug )
1252      } // endforeach ( $common_slug_groups as $slug_group )
1253  
1254      return $new_nav_menu_locations;
1255  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7