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


Generated: Tue Aug 20 08:20:01 2019 Cross-referenced by PHPXref 0.7