[ Index ]

PHP Cross Reference of WordPress

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


Generated: Mon Jan 17 01:00:03 2022 Cross-referenced by PHPXref 0.7.1