[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Wed Dec 2 08:20:02 2026 Cross-referenced by PHPXref