[ 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 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      if ( $menu->count > 0 ) {
 738          $items = get_posts( $args );
 739      } else {
 740          $items = array();
 741      }
 742  
 743      $items = array_map( 'wp_setup_nav_menu_item', $items );
 744  
 745      if ( ! is_admin() ) { // Remove invalid items only on front end.
 746          $items = array_filter( $items, '_is_valid_nav_menu_item' );
 747      }
 748  
 749      if ( ARRAY_A === $args['output'] ) {
 750          $items = wp_list_sort(
 751              $items,
 752              array(
 753                  $args['output_key'] => 'ASC',
 754              )
 755          );
 756  
 757          $i = 1;
 758  
 759          foreach ( $items as $k => $item ) {
 760              $items[ $k ]->{$args['output_key']} = $i++;
 761          }
 762      }
 763  
 764      /**
 765       * Filters the navigation menu items being returned.
 766       *
 767       * @since 3.0.0
 768       *
 769       * @param array  $items An array of menu item post objects.
 770       * @param object $menu  The menu object.
 771       * @param array  $args  An array of arguments used to retrieve menu item objects.
 772       */
 773      return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
 774  }
 775  
 776  /**
 777   * Updates post and term caches for all linked objects for a list of menu items.
 778   *
 779   * @since 6.1.0
 780   *
 781   * @param WP_Post[] $menu_items Array of menu item post objects.
 782   */
 783  function update_menu_item_cache( $menu_items ) {
 784      $post_ids = array();
 785      $term_ids = array();
 786  
 787      foreach ( $menu_items as $menu_item ) {
 788          if ( 'nav_menu_item' !== $menu_item->post_type ) {
 789              continue;
 790          }
 791  
 792          $object_id = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
 793          $type      = get_post_meta( $menu_item->ID, '_menu_item_type', true );
 794  
 795          if ( 'post_type' === $type ) {
 796              $post_ids[] = (int) $object_id;
 797          } elseif ( 'taxonomy' === $type ) {
 798              $term_ids[] = (int) $object_id;
 799          }
 800      }
 801  
 802      if ( ! empty( $post_ids ) ) {
 803          _prime_post_caches( $post_ids, false );
 804      }
 805  
 806      if ( ! empty( $term_ids ) ) {
 807          _prime_term_caches( $term_ids );
 808      }
 809  }
 810  
 811  /**
 812   * Decorates a menu item object with the shared navigation menu item properties.
 813   *
 814   * Properties:
 815   * - ID:               The term_id if the menu item represents a taxonomy term.
 816   * - attr_title:       The title attribute of the link element for this menu item.
 817   * - classes:          The array of class attribute values for the link element of this menu item.
 818   * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
 819   * - description:      The description of this menu item.
 820   * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
 821   * - object:           The type of object originally represented, such as 'category', 'post', or 'attachment'.
 822   * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
 823   * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
 824   * - post_title:       A "no title" label if menu item represents a post that lacks a title.
 825   * - target:           The target attribute of the link element for this menu item.
 826   * - title:            The title of this menu item.
 827   * - type:             The family of objects originally represented, such as 'post_type' or 'taxonomy'.
 828   * - type_label:       The singular label used to describe this type of menu item.
 829   * - url:              The URL to which this menu item points.
 830   * - xfn:              The XFN relationship expressed in the link of this menu item.
 831   * - _invalid:         Whether the menu item represents an object that no longer exists.
 832   *
 833   * @since 3.0.0
 834   *
 835   * @param object $menu_item The menu item to modify.
 836   * @return object The menu item with standard menu item properties.
 837   */
 838  function wp_setup_nav_menu_item( $menu_item ) {
 839  
 840      /**
 841       * Filters whether to short-circuit the wp_setup_nav_menu_item() output.
 842       *
 843       * Returning a non-null value from the filter will short-circuit wp_setup_nav_menu_item(),
 844       * returning that value instead.
 845       *
 846       * @since 6.3.0
 847       *
 848       * @param object|null $modified_menu_item Modified menu item. Default null.
 849       * @param object      $menu_item          The menu item to modify.
 850       */
 851      $pre_menu_item = apply_filters( 'pre_wp_setup_nav_menu_item', null, $menu_item );
 852  
 853      if ( null !== $pre_menu_item ) {
 854          return $pre_menu_item;
 855      }
 856  
 857      if ( isset( $menu_item->post_type ) ) {
 858          if ( 'nav_menu_item' === $menu_item->post_type ) {
 859              $menu_item->db_id            = (int) $menu_item->ID;
 860              $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;
 861              $menu_item->object_id        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
 862              $menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
 863              $menu_item->type             = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
 864  
 865              if ( 'post_type' === $menu_item->type ) {
 866                  $object = get_post_type_object( $menu_item->object );
 867                  if ( $object ) {
 868                      $menu_item->type_label = $object->labels->singular_name;
 869                      // Denote post states for special pages (only in the admin).
 870                      if ( function_exists( 'get_post_states' ) ) {
 871                          $menu_post = get_post( $menu_item->object_id );
 872                          if ( $menu_post instanceof WP_Post ) {
 873                              $post_states = get_post_states( $menu_post );
 874                              if ( $post_states ) {
 875                                  $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
 876                              }
 877                          }
 878                      }
 879                  } else {
 880                      $menu_item->type_label = $menu_item->object;
 881                      $menu_item->_invalid   = true;
 882                  }
 883  
 884                  if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
 885                      $menu_item->_invalid = true;
 886                  }
 887  
 888                  $original_object = get_post( $menu_item->object_id );
 889  
 890                  if ( $original_object ) {
 891                      $menu_item->url = get_permalink( $original_object->ID );
 892                      /** This filter is documented in wp-includes/post-template.php */
 893                      $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
 894                  } else {
 895                      $menu_item->url      = '';
 896                      $original_title      = '';
 897                      $menu_item->_invalid = true;
 898                  }
 899  
 900                  if ( '' === $original_title ) {
 901                      /* translators: %d: ID of a post. */
 902                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 903                  }
 904  
 905                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 906  
 907              } elseif ( 'post_type_archive' === $menu_item->type ) {
 908                  $object = get_post_type_object( $menu_item->object );
 909                  if ( $object ) {
 910                      $menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
 911                      $post_type_description = $object->description;
 912                  } else {
 913                      $post_type_description = '';
 914                      $menu_item->_invalid   = true;
 915                  }
 916  
 917                  $menu_item->type_label = __( 'Post Type Archive' );
 918                  $post_content          = wp_trim_words( $menu_item->post_content, 200 );
 919                  $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
 920                  $menu_item->url        = get_post_type_archive_link( $menu_item->object );
 921  
 922              } elseif ( 'taxonomy' === $menu_item->type ) {
 923                  $object = get_taxonomy( $menu_item->object );
 924                  if ( $object ) {
 925                      $menu_item->type_label = $object->labels->singular_name;
 926                  } else {
 927                      $menu_item->type_label = $menu_item->object;
 928                      $menu_item->_invalid   = true;
 929                  }
 930  
 931                  $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );
 932  
 933                  if ( $original_object && ! is_wp_error( $original_object ) ) {
 934                      $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
 935                      $original_title = $original_object->name;
 936                  } else {
 937                      $menu_item->url      = '';
 938                      $original_title      = '';
 939                      $menu_item->_invalid = true;
 940                  }
 941  
 942                  if ( '' === $original_title ) {
 943                      /* translators: %d: ID of a term. */
 944                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 945                  }
 946  
 947                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 948  
 949              } else {
 950                  $menu_item->type_label = __( 'Custom Link' );
 951                  $menu_item->title      = $menu_item->post_title;
 952                  $menu_item->url        = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
 953              }
 954  
 955              $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
 956  
 957              /**
 958               * Filters a navigation menu item's title attribute.
 959               *
 960               * @since 3.0.0
 961               *
 962               * @param string $item_title The menu item title attribute.
 963               */
 964              $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
 965  
 966              if ( ! isset( $menu_item->description ) ) {
 967                  /**
 968                   * Filters a navigation menu item's description.
 969                   *
 970                   * @since 3.0.0
 971                   *
 972                   * @param string $description The menu item description.
 973                   */
 974                  $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
 975              }
 976  
 977              $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
 978              $menu_item->xfn     = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
 979          } else {
 980              $menu_item->db_id            = 0;
 981              $menu_item->menu_item_parent = 0;
 982              $menu_item->object_id        = (int) $menu_item->ID;
 983              $menu_item->type             = 'post_type';
 984  
 985              $object                = get_post_type_object( $menu_item->post_type );
 986              $menu_item->object     = $object->name;
 987              $menu_item->type_label = $object->labels->singular_name;
 988  
 989              if ( '' === $menu_item->post_title ) {
 990                  /* translators: %d: ID of a post. */
 991                  $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
 992              }
 993  
 994              $menu_item->title  = $menu_item->post_title;
 995              $menu_item->url    = get_permalink( $menu_item->ID );
 996              $menu_item->target = '';
 997  
 998              /** This filter is documented in wp-includes/nav-menu.php */
 999              $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
1000  
1001              /** This filter is documented in wp-includes/nav-menu.php */
1002              $menu_item->description = apply_filters( 'nav_menu_description', '' );
1003              $menu_item->classes     = array();
1004              $menu_item->xfn         = '';
1005          }
1006      } elseif ( isset( $menu_item->taxonomy ) ) {
1007          $menu_item->ID               = $menu_item->term_id;
1008          $menu_item->db_id            = 0;
1009          $menu_item->menu_item_parent = 0;
1010          $menu_item->object_id        = (int) $menu_item->term_id;
1011          $menu_item->post_parent      = (int) $menu_item->parent;
1012          $menu_item->type             = 'taxonomy';
1013  
1014          $object                = get_taxonomy( $menu_item->taxonomy );
1015          $menu_item->object     = $object->name;
1016          $menu_item->type_label = $object->labels->singular_name;
1017  
1018          $menu_item->title       = $menu_item->name;
1019          $menu_item->url         = get_term_link( $menu_item, $menu_item->taxonomy );
1020          $menu_item->target      = '';
1021          $menu_item->attr_title  = '';
1022          $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
1023          $menu_item->classes     = array();
1024          $menu_item->xfn         = '';
1025  
1026      }
1027  
1028      /**
1029       * Filters a navigation menu item object.
1030       *
1031       * @since 3.0.0
1032       *
1033       * @param object $menu_item The menu item object.
1034       */
1035      return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
1036  }
1037  
1038  /**
1039   * Returns the menu items associated with a particular object.
1040   *
1041   * @since 3.0.0
1042   *
1043   * @param int    $object_id   Optional. The ID of the original object. Default 0.
1044   * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'.
1045   *                            Default 'post_type'.
1046   * @param string $taxonomy    Optional. If $object_type is 'taxonomy', $taxonomy is the name
1047   *                            of the tax that $object_id belongs to. Default empty.
1048   * @return int[] The array of menu item IDs; empty array if none.
1049   */
1050  function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
1051      $object_id     = (int) $object_id;
1052      $menu_item_ids = array();
1053  
1054      $query      = new WP_Query();
1055      $menu_items = $query->query(
1056          array(
1057              'meta_key'       => '_menu_item_object_id',
1058              'meta_value'     => $object_id,
1059              'post_status'    => 'any',
1060              'post_type'      => 'nav_menu_item',
1061              'posts_per_page' => -1,
1062          )
1063      );
1064      foreach ( (array) $menu_items as $menu_item ) {
1065          if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
1066              $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
1067              if (
1068                  'post_type' === $object_type &&
1069                  'post_type' === $menu_item_type
1070              ) {
1071                  $menu_item_ids[] = (int) $menu_item->ID;
1072              } elseif (
1073                  'taxonomy' === $object_type &&
1074                  'taxonomy' === $menu_item_type &&
1075                  get_post_meta( $menu_item->ID, '_menu_item_object', true ) === $taxonomy
1076              ) {
1077                  $menu_item_ids[] = (int) $menu_item->ID;
1078              }
1079          }
1080      }
1081  
1082      return array_unique( $menu_item_ids );
1083  }
1084  
1085  /**
1086   * Callback for handling a menu item when its original object is deleted.
1087   *
1088   * @since 3.0.0
1089   * @access private
1090   *
1091   * @param int $object_id The ID of the original object being trashed.
1092   */
1093  function _wp_delete_post_menu_item( $object_id ) {
1094      $object_id = (int) $object_id;
1095  
1096      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
1097  
1098      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1099          wp_delete_post( $menu_item_id, true );
1100      }
1101  }
1102  
1103  /**
1104   * Serves as a callback for handling a menu item when its original object is deleted.
1105   *
1106   * @since 3.0.0
1107   * @access private
1108   *
1109   * @param int    $object_id The ID of the original object being trashed.
1110   * @param int    $tt_id     Term taxonomy ID. Unused.
1111   * @param string $taxonomy  Taxonomy slug.
1112   */
1113  function _wp_delete_tax_menu_item( $object_id, $tt_id, $taxonomy ) {
1114      $object_id = (int) $object_id;
1115  
1116      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
1117  
1118      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1119          wp_delete_post( $menu_item_id, true );
1120      }
1121  }
1122  
1123  /**
1124   * Automatically add newly published page objects to menus with that as an option.
1125   *
1126   * @since 3.0.0
1127   * @access private
1128   *
1129   * @param string  $new_status The new status of the post object.
1130   * @param string  $old_status The old status of the post object.
1131   * @param WP_Post $post       The post object being transitioned from one status to another.
1132   */
1133  function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
1134      if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) {
1135          return;
1136      }
1137      if ( ! empty( $post->post_parent ) ) {
1138          return;
1139      }
1140      $auto_add = get_option( 'nav_menu_options' );
1141      if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) {
1142          return;
1143      }
1144      $auto_add = $auto_add['auto_add'];
1145      if ( empty( $auto_add ) || ! is_array( $auto_add ) ) {
1146          return;
1147      }
1148  
1149      $args = array(
1150          'menu-item-object-id' => $post->ID,
1151          'menu-item-object'    => $post->post_type,
1152          'menu-item-type'      => 'post_type',
1153          'menu-item-status'    => 'publish',
1154      );
1155  
1156      foreach ( $auto_add as $menu_id ) {
1157          $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
1158          if ( ! is_array( $items ) ) {
1159              continue;
1160          }
1161          foreach ( $items as $item ) {
1162              if ( $post->ID === (int) $item->object_id ) {
1163                  continue 2;
1164              }
1165          }
1166          wp_update_nav_menu_item( $menu_id, 0, $args );
1167      }
1168  }
1169  
1170  /**
1171   * Deletes auto-draft posts associated with the supplied changeset.
1172   *
1173   * @since 4.8.0
1174   * @access private
1175   *
1176   * @param int $post_id Post ID for the customize_changeset.
1177   */
1178  function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) {
1179      $post = get_post( $post_id );
1180  
1181      if ( ! $post || 'customize_changeset' !== $post->post_type ) {
1182          return;
1183      }
1184  
1185      $data = json_decode( $post->post_content, true );
1186      if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
1187          return;
1188      }
1189      remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1190      foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
1191          if ( empty( $stub_post_id ) ) {
1192              continue;
1193          }
1194          if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
1195              wp_delete_post( $stub_post_id, true );
1196          } elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
1197              wp_trash_post( $stub_post_id );
1198              delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
1199          }
1200      }
1201      add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1202  }
1203  
1204  /**
1205   * Handles menu config after theme change.
1206   *
1207   * @access private
1208   * @since 4.9.0
1209   */
1210  function _wp_menus_changed() {
1211      $old_nav_menu_locations    = get_option( 'theme_switch_menu_locations', array() );
1212      $new_nav_menu_locations    = get_nav_menu_locations();
1213      $mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
1214  
1215      set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
1216      delete_option( 'theme_switch_menu_locations' );
1217  }
1218  
1219  /**
1220   * Maps nav menu locations according to assignments in previously active theme.
1221   *
1222   * @since 4.9.0
1223   *
1224   * @param array $new_nav_menu_locations New nav menu locations assignments.
1225   * @param array $old_nav_menu_locations Old nav menu locations assignments.
1226   * @return array Nav menus mapped to new nav menu locations.
1227   */
1228  function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) {
1229      $registered_nav_menus   = get_registered_nav_menus();
1230      $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus );
1231  
1232      // Short-circuit if there are no old nav menu location assignments to map.
1233      if ( empty( $old_nav_menu_locations ) ) {
1234          return $new_nav_menu_locations;
1235      }
1236  
1237      // If old and new theme have just one location, map it and we're done.
1238      if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) {
1239          $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations );
1240          return $new_nav_menu_locations;
1241      }
1242  
1243      $old_locations = array_keys( $old_nav_menu_locations );
1244  
1245      // Map locations with the same slug.
1246      foreach ( $registered_nav_menus as $location => $name ) {
1247          if ( in_array( $location, $old_locations, true ) ) {
1248              $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ];
1249              unset( $old_nav_menu_locations[ $location ] );
1250          }
1251      }
1252  
1253      // If there are no old nav menu locations left, then we're done.
1254      if ( empty( $old_nav_menu_locations ) ) {
1255          return $new_nav_menu_locations;
1256      }
1257  
1258      /*
1259       * If old and new theme both have locations that contain phrases
1260       * from within the same group, make an educated guess and map it.
1261       */
1262      $common_slug_groups = array(
1263          array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ),
1264          array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ),
1265          array( 'social' ),
1266      );
1267  
1268      // Go through each group...
1269      foreach ( $common_slug_groups as $slug_group ) {
1270  
1271          // ...and see if any of these slugs...
1272          foreach ( $slug_group as $slug ) {
1273  
1274              // ...and any of the new menu locations...
1275              foreach ( $registered_nav_menus as $new_location => $name ) {
1276  
1277                  // ...actually match!
1278                  if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) {
1279                      continue;
1280                  } elseif ( ! is_string( $new_location ) ) {
1281                      continue;
1282                  }
1283  
1284                  // Then see if any of the old locations...
1285                  foreach ( $old_nav_menu_locations as $location => $menu_id ) {
1286  
1287                      // ...and any slug in the same group...
1288                      foreach ( $slug_group as $slug ) {
1289  
1290                          // ... have a match as well.
1291                          if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) {
1292                              continue;
1293                          } elseif ( ! is_string( $location ) ) {
1294                              continue;
1295                          }
1296  
1297                          // Make sure this location wasn't mapped and removed previously.
1298                          if ( ! empty( $old_nav_menu_locations[ $location ] ) ) {
1299  
1300                              // We have a match that can be mapped!
1301                              $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ];
1302  
1303                              // Remove the mapped location so it can't be mapped again.
1304                              unset( $old_nav_menu_locations[ $location ] );
1305  
1306                              // Go back and check the next new menu location.
1307                              continue 3;
1308                          }
1309                      } // End foreach ( $slug_group as $slug ).
1310                  } // End foreach ( $old_nav_menu_locations as $location => $menu_id ).
1311              } // End foreach foreach ( $registered_nav_menus as $new_location => $name ).
1312          } // End foreach ( $slug_group as $slug ).
1313      } // End foreach ( $common_slug_groups as $slug_group ).
1314  
1315      return $new_nav_menu_locations;
1316  }
1317  
1318  /**
1319   * Prevents menu items from being their own parent.
1320   *
1321   * Resets menu_item_parent to 0 when the parent is set to the item itself.
1322   * For use before saving `_menu_item_menu_item_parent` in nav-menus.php.
1323   *
1324   * @since 6.2.0
1325   * @access private
1326   *
1327   * @param array $menu_item_data The menu item data array.
1328   * @return array The menu item data with reset menu_item_parent.
1329   */
1330  function _wp_reset_invalid_menu_item_parent( $menu_item_data ) {
1331      if ( ! is_array( $menu_item_data ) ) {
1332          return $menu_item_data;
1333      }
1334  
1335      if (
1336          ! empty( $menu_item_data['ID'] ) &&
1337          ! empty( $menu_item_data['menu_item_parent'] ) &&
1338          (int) $menu_item_data['ID'] === (int) $menu_item_data['menu_item_parent']
1339      ) {
1340          $menu_item_data['menu_item_parent'] = 0;
1341      }
1342  
1343      return $menu_item_data;
1344  }


Generated : Thu Nov 12 08:20:02 2026 Cross-referenced by PHPXref