[ 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 ( ! $_menu || ! 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();
 462  
 463          if ( 0 !== $menu_id ) {
 464              $menu_items = (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
 465          }
 466  
 467          $last_item = array_pop( $menu_items );
 468  
 469          if ( $last_item && isset( $last_item->menu_order ) ) {
 470              $args['menu-item-position'] = 1 + $last_item->menu_order;
 471          } else {
 472              $args['menu-item-position'] = count( $menu_items );
 473          }
 474      }
 475  
 476      $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
 477  
 478      if ( 'custom' === $args['menu-item-type'] ) {
 479          // If custom menu item, trim the URL.
 480          $args['menu-item-url'] = trim( $args['menu-item-url'] );
 481      } else {
 482          /*
 483           * If non-custom menu item, then:
 484           * - use the original object's URL.
 485           * - blank default title to sync with the original object's title.
 486           */
 487  
 488          $args['menu-item-url'] = '';
 489  
 490          $original_title = '';
 491  
 492          if ( 'taxonomy' === $args['menu-item-type'] ) {
 493              $original_object = get_term( $args['menu-item-object-id'], $args['menu-item-object'] );
 494  
 495              if ( $original_object instanceof WP_Term ) {
 496                  $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
 497                  $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
 498              }
 499          } elseif ( 'post_type' === $args['menu-item-type'] ) {
 500              $original_object = get_post( $args['menu-item-object-id'] );
 501  
 502              if ( $original_object instanceof WP_Post ) {
 503                  $original_parent = (int) $original_object->post_parent;
 504                  $original_title  = $original_object->post_title;
 505              }
 506          } elseif ( 'post_type_archive' === $args['menu-item-type'] ) {
 507              $original_object = get_post_type_object( $args['menu-item-object'] );
 508  
 509              if ( $original_object instanceof WP_Post_Type ) {
 510                  $original_title = $original_object->labels->archives;
 511              }
 512          }
 513  
 514          if ( wp_unslash( $args['menu-item-title'] ) === wp_specialchars_decode( $original_title ) ) {
 515              $args['menu-item-title'] = '';
 516          }
 517  
 518          // Hack to get wp to create a post object when too many properties are empty.
 519          if ( '' === $args['menu-item-title'] && '' === $args['menu-item-description'] ) {
 520              $args['menu-item-description'] = ' ';
 521          }
 522      }
 523  
 524      // Populate the menu item object.
 525      $post = array(
 526          'menu_order'   => $args['menu-item-position'],
 527          'ping_status'  => 0,
 528          'post_content' => $args['menu-item-description'],
 529          'post_excerpt' => $args['menu-item-attr-title'],
 530          'post_parent'  => $original_parent,
 531          'post_title'   => $args['menu-item-title'],
 532          'post_type'    => 'nav_menu_item',
 533      );
 534  
 535      $post_date = wp_resolve_post_date( $args['menu-item-post-date'], $args['menu-item-post-date-gmt'] );
 536      if ( $post_date ) {
 537          $post['post_date'] = $post_date;
 538      }
 539  
 540      $update = 0 !== $menu_item_db_id;
 541  
 542      // New menu item. Default is draft status.
 543      if ( ! $update ) {
 544          $post['ID']          = 0;
 545          $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
 546          $menu_item_db_id     = wp_insert_post( $post, true, $fire_after_hooks );
 547          if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
 548              return $menu_item_db_id;
 549          }
 550  
 551          /**
 552           * Fires immediately after a new navigation menu item has been added.
 553           *
 554           * @since 4.4.0
 555           *
 556           * @see wp_update_nav_menu_item()
 557           *
 558           * @param int   $menu_id         ID of the updated menu.
 559           * @param int   $menu_item_db_id ID of the new menu item.
 560           * @param array $args            An array of arguments used to update/add the menu item.
 561           */
 562          do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args );
 563      }
 564  
 565      /*
 566       * Associate the menu item with the menu term.
 567       * Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
 568       */
 569      if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
 570          $update_terms = wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
 571          if ( is_wp_error( $update_terms ) ) {
 572              return $update_terms;
 573          }
 574      }
 575  
 576      if ( 'custom' === $args['menu-item-type'] ) {
 577          $args['menu-item-object-id'] = $menu_item_db_id;
 578          $args['menu-item-object']    = 'custom';
 579      }
 580  
 581      $menu_item_db_id = (int) $menu_item_db_id;
 582  
 583      // Reset invalid `menu_item_parent`.
 584      if ( (int) $args['menu-item-parent-id'] === $menu_item_db_id ) {
 585          $args['menu-item-parent-id'] = 0;
 586      }
 587  
 588      update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key( $args['menu-item-type'] ) );
 589      update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', (string) ( (int) $args['menu-item-parent-id'] ) );
 590      update_post_meta( $menu_item_db_id, '_menu_item_object_id', (string) ( (int) $args['menu-item-object-id'] ) );
 591      update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key( $args['menu-item-object'] ) );
 592      update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key( $args['menu-item-target'] ) );
 593  
 594      $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
 595      $args['menu-item-xfn']     = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
 596      update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
 597      update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
 598      update_post_meta( $menu_item_db_id, '_menu_item_url', sanitize_url( $args['menu-item-url'] ) );
 599  
 600      if ( 0 === $menu_id ) {
 601          update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
 602      } elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) ) {
 603          delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
 604      }
 605  
 606      // Update existing menu item. Default is publish status.
 607      if ( $update ) {
 608          $post['ID']          = $menu_item_db_id;
 609          $post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
 610  
 611          $update_post = wp_update_post( $post, true );
 612          if ( is_wp_error( $update_post ) ) {
 613              return $update_post;
 614          }
 615      }
 616  
 617      /**
 618       * Fires after a navigation menu item has been updated.
 619       *
 620       * @since 3.0.0
 621       *
 622       * @see wp_update_nav_menu_item()
 623       *
 624       * @param int   $menu_id         ID of the updated menu.
 625       * @param int   $menu_item_db_id ID of the updated menu item.
 626       * @param array $args            An array of arguments used to update a menu item.
 627       */
 628      do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
 629  
 630      return $menu_item_db_id;
 631  }
 632  
 633  /**
 634   * Returns all navigation menu objects.
 635   *
 636   * @since 3.0.0
 637   * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
 638   *              to 'name'.
 639   *
 640   * @param array $args Optional. Array of arguments passed on to get_terms().
 641   *                    Default empty array.
 642   * @return WP_Term[] An array of menu objects.
 643   */
 644  function wp_get_nav_menus( $args = array() ) {
 645      $defaults = array(
 646          'taxonomy'   => 'nav_menu',
 647          'hide_empty' => false,
 648          'orderby'    => 'name',
 649      );
 650      $args     = wp_parse_args( $args, $defaults );
 651  
 652      /**
 653       * Filters the navigation menu objects being returned.
 654       *
 655       * @since 3.0.0
 656       *
 657       * @see get_terms()
 658       *
 659       * @param WP_Term[] $menus An array of menu objects.
 660       * @param array     $args  An array of arguments used to retrieve menu objects.
 661       */
 662      return apply_filters( 'wp_get_nav_menus', get_terms( $args ), $args );
 663  }
 664  
 665  /**
 666   * Determines whether a menu item is valid.
 667   *
 668   * @link https://core.trac.wordpress.org/ticket/13958
 669   *
 670   * @since 3.2.0
 671   * @access private
 672   *
 673   * @param object $item The menu item to check.
 674   * @return bool False if invalid, otherwise true.
 675   */
 676  function _is_valid_nav_menu_item( $item ) {
 677      return empty( $item->_invalid );
 678  }
 679  
 680  /**
 681   * Retrieves all menu items of a navigation menu.
 682   *
 683   * Note: Most arguments passed to the `$args` parameter – save for 'output_key' – are
 684   * specifically for retrieving nav_menu_item posts from get_posts() and may only
 685   * indirectly affect the ultimate ordering and content of the resulting nav menu
 686   * items that get returned from this function.
 687   *
 688   * @since 3.0.0
 689   *
 690   * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
 691   * @param array              $args {
 692   *     Optional. Arguments to pass to get_posts().
 693   *
 694   *     @type string $order                  How to order nav menu items as queried with get_posts().
 695   *                                          Will be ignored if 'output' is ARRAY_A. Default 'ASC'.
 696   *     @type string $orderby                Field to order menu items by as retrieved from get_posts().
 697   *                                          Supply an orderby field via 'output_key' to affect the
 698   *                                          output order of nav menu items. Default 'menu_order'.
 699   *     @type string $post_type              Menu items post type. Default 'nav_menu_item'.
 700   *     @type string $post_status            Menu items post status. Default 'publish'.
 701   *     @type string $output                 How to order outputted menu items. Default ARRAY_A.
 702   *     @type string $output_key             Key to use for ordering the actual menu items that get
 703   *                                          returned. Note that that is not a get_posts() argument
 704   *                                          and will only affect output of menu items processed in
 705   *                                          this function. Default 'menu_order'.
 706   *     @type bool   $nopaging               Whether to retrieve all menu items (true) or paginate
 707   *                                          (false). Default true.
 708   *     @type bool   $update_menu_item_cache Whether to update the menu item cache. Default true.
 709   * }
 710   * @return array|false Array of menu items, otherwise false.
 711   */
 712  function wp_get_nav_menu_items( $menu, $args = array() ) {
 713      $menu = wp_get_nav_menu_object( $menu );
 714  
 715      if ( ! $menu ) {
 716          return false;
 717      }
 718  
 719      if ( ! taxonomy_exists( 'nav_menu' ) ) {
 720          return false;
 721      }
 722  
 723      $defaults = array(
 724          'order'                  => 'ASC',
 725          'orderby'                => 'menu_order',
 726          'post_type'              => 'nav_menu_item',
 727          'post_status'            => 'publish',
 728          'output'                 => ARRAY_A,
 729          'output_key'             => 'menu_order',
 730          'nopaging'               => true,
 731          'update_menu_item_cache' => true,
 732          'tax_query'              => array(
 733              array(
 734                  'taxonomy' => 'nav_menu',
 735                  'field'    => 'term_taxonomy_id',
 736                  'terms'    => $menu->term_taxonomy_id,
 737              ),
 738          ),
 739      );
 740      $args     = wp_parse_args( $args, $defaults );
 741      if ( $menu->count > 0 ) {
 742          $items = get_posts( $args );
 743      } else {
 744          $items = array();
 745      }
 746  
 747      $items = array_map( 'wp_setup_nav_menu_item', $items );
 748  
 749      if ( ! is_admin() ) { // Remove invalid items only on front end.
 750          $items = array_filter( $items, '_is_valid_nav_menu_item' );
 751      }
 752  
 753      if ( ARRAY_A === $args['output'] ) {
 754          $items = wp_list_sort(
 755              $items,
 756              array(
 757                  $args['output_key'] => 'ASC',
 758              )
 759          );
 760  
 761          $i = 1;
 762  
 763          foreach ( $items as $k => $item ) {
 764              $items[ $k ]->{$args['output_key']} = $i++;
 765          }
 766      }
 767  
 768      /**
 769       * Filters the navigation menu items being returned.
 770       *
 771       * @since 3.0.0
 772       *
 773       * @param array  $items An array of menu item post objects.
 774       * @param object $menu  The menu object.
 775       * @param array  $args  An array of arguments used to retrieve menu item objects.
 776       */
 777      return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
 778  }
 779  
 780  /**
 781   * Updates post and term caches for all linked objects for a list of menu items.
 782   *
 783   * @since 6.1.0
 784   *
 785   * @param WP_Post[] $menu_items Array of menu item post objects.
 786   */
 787  function update_menu_item_cache( $menu_items ) {
 788      $post_ids = array();
 789      $term_ids = array();
 790  
 791      foreach ( $menu_items as $menu_item ) {
 792          if ( 'nav_menu_item' !== $menu_item->post_type ) {
 793              continue;
 794          }
 795  
 796          $object_id = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
 797          $type      = get_post_meta( $menu_item->ID, '_menu_item_type', true );
 798  
 799          if ( 'post_type' === $type ) {
 800              $post_ids[] = (int) $object_id;
 801          } elseif ( 'taxonomy' === $type ) {
 802              $term_ids[] = (int) $object_id;
 803          }
 804      }
 805  
 806      if ( ! empty( $post_ids ) ) {
 807          _prime_post_caches( $post_ids, false );
 808      }
 809  
 810      if ( ! empty( $term_ids ) ) {
 811          _prime_term_caches( $term_ids );
 812      }
 813  }
 814  
 815  /**
 816   * Decorates a menu item object with the shared navigation menu item properties.
 817   *
 818   * Properties:
 819   * - ID:               The term_id if the menu item represents a taxonomy term.
 820   * - attr_title:       The title attribute of the link element for this menu item.
 821   * - classes:          The array of class attribute values for the link element of this menu item.
 822   * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
 823   * - description:      The description of this menu item.
 824   * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
 825   * - object:           The type of object originally represented, such as 'category', 'post', or 'attachment'.
 826   * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
 827   * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
 828   * - post_title:       A "no title" label if menu item represents a post that lacks a title.
 829   * - target:           The target attribute of the link element for this menu item.
 830   * - title:            The title of this menu item.
 831   * - type:             The family of objects originally represented, such as 'post_type' or 'taxonomy'.
 832   * - type_label:       The singular label used to describe this type of menu item.
 833   * - url:              The URL to which this menu item points.
 834   * - xfn:              The XFN relationship expressed in the link of this menu item.
 835   * - _invalid:         Whether the menu item represents an object that no longer exists.
 836   *
 837   * @since 3.0.0
 838   *
 839   * @param object $menu_item The menu item to modify.
 840   * @return object The menu item with standard menu item properties.
 841   */
 842  function wp_setup_nav_menu_item( $menu_item ) {
 843  
 844      /**
 845       * Filters whether to short-circuit the wp_setup_nav_menu_item() output.
 846       *
 847       * Returning a non-null value from the filter will short-circuit wp_setup_nav_menu_item(),
 848       * returning that value instead.
 849       *
 850       * @since 6.3.0
 851       *
 852       * @param object|null $modified_menu_item Modified menu item. Default null.
 853       * @param object      $menu_item          The menu item to modify.
 854       */
 855      $pre_menu_item = apply_filters( 'pre_wp_setup_nav_menu_item', null, $menu_item );
 856  
 857      if ( null !== $pre_menu_item ) {
 858          return $pre_menu_item;
 859      }
 860  
 861      if ( isset( $menu_item->post_type ) ) {
 862          if ( 'nav_menu_item' === $menu_item->post_type ) {
 863              $menu_item->db_id            = (int) $menu_item->ID;
 864              $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;
 865              $menu_item->object_id        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
 866              $menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
 867              $menu_item->type             = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
 868  
 869              if ( 'post_type' === $menu_item->type ) {
 870                  $object = get_post_type_object( $menu_item->object );
 871                  if ( $object ) {
 872                      $menu_item->type_label = $object->labels->singular_name;
 873                      // Denote post states for special pages (only in the admin).
 874                      if ( function_exists( 'get_post_states' ) ) {
 875                          $menu_post = get_post( $menu_item->object_id );
 876                          if ( $menu_post instanceof WP_Post ) {
 877                              $post_states = get_post_states( $menu_post );
 878                              if ( $post_states ) {
 879                                  $menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
 880                              }
 881                          }
 882                      }
 883                  } else {
 884                      $menu_item->type_label = $menu_item->object;
 885                      $menu_item->_invalid   = true;
 886                  }
 887  
 888                  if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
 889                      $menu_item->_invalid = true;
 890                  }
 891  
 892                  $original_object = get_post( $menu_item->object_id );
 893  
 894                  if ( $original_object ) {
 895                      $menu_item->url = get_permalink( $original_object->ID );
 896                      /** This filter is documented in wp-includes/post-template.php */
 897                      $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
 898                  } else {
 899                      $menu_item->url      = '';
 900                      $original_title      = '';
 901                      $menu_item->_invalid = true;
 902                  }
 903  
 904                  if ( '' === $original_title ) {
 905                      /* translators: %d: ID of a post. */
 906                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 907                  }
 908  
 909                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 910  
 911              } elseif ( 'post_type_archive' === $menu_item->type ) {
 912                  $object = get_post_type_object( $menu_item->object );
 913                  if ( $object ) {
 914                      $menu_item->title      = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title;
 915                      $post_type_description = $object->description;
 916                  } else {
 917                      $post_type_description = '';
 918                      $menu_item->_invalid   = true;
 919                  }
 920  
 921                  $menu_item->type_label = __( 'Post Type Archive' );
 922                  $post_content          = wp_trim_words( $menu_item->post_content, 200 );
 923                  $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content;
 924                  $menu_item->url        = get_post_type_archive_link( $menu_item->object );
 925  
 926              } elseif ( 'taxonomy' === $menu_item->type ) {
 927                  $object = get_taxonomy( $menu_item->object );
 928                  if ( $object ) {
 929                      $menu_item->type_label = $object->labels->singular_name;
 930                  } else {
 931                      $menu_item->type_label = $menu_item->object;
 932                      $menu_item->_invalid   = true;
 933                  }
 934  
 935                  $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );
 936  
 937                  if ( $original_object && ! is_wp_error( $original_object ) ) {
 938                      $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
 939                      $original_title = $original_object->name;
 940                  } else {
 941                      $menu_item->url      = '';
 942                      $original_title      = '';
 943                      $menu_item->_invalid = true;
 944                  }
 945  
 946                  if ( '' === $original_title ) {
 947                      /* translators: %d: ID of a term. */
 948                      $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
 949                  }
 950  
 951                  $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
 952  
 953              } else {
 954                  $menu_item->type_label = __( 'Custom Link' );
 955                  $menu_item->title      = $menu_item->post_title;
 956                  $menu_item->url        = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
 957              }
 958  
 959              $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
 960  
 961              /**
 962               * Filters a navigation menu item's title attribute.
 963               *
 964               * @since 3.0.0
 965               *
 966               * @param string $item_title The menu item title attribute.
 967               */
 968              $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
 969  
 970              if ( ! isset( $menu_item->description ) ) {
 971                  /**
 972                   * Filters a navigation menu item's description.
 973                   *
 974                   * @since 3.0.0
 975                   *
 976                   * @param string $description The menu item description.
 977                   */
 978                  $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
 979              }
 980  
 981              $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
 982              $menu_item->xfn     = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
 983          } else {
 984              $menu_item->db_id            = 0;
 985              $menu_item->menu_item_parent = 0;
 986              $menu_item->object_id        = (int) $menu_item->ID;
 987              $menu_item->type             = 'post_type';
 988  
 989              $object                = get_post_type_object( $menu_item->post_type );
 990              $menu_item->object     = $object->name;
 991              $menu_item->type_label = $object->labels->singular_name;
 992  
 993              if ( '' === $menu_item->post_title ) {
 994                  /* translators: %d: ID of a post. */
 995                  $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
 996              }
 997  
 998              $menu_item->title  = $menu_item->post_title;
 999              $menu_item->url    = get_permalink( $menu_item->ID );
1000              $menu_item->target = '';
1001  
1002              /** This filter is documented in wp-includes/nav-menu.php */
1003              $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
1004  
1005              /** This filter is documented in wp-includes/nav-menu.php */
1006              $menu_item->description = apply_filters( 'nav_menu_description', '' );
1007              $menu_item->classes     = array();
1008              $menu_item->xfn         = '';
1009          }
1010      } elseif ( isset( $menu_item->taxonomy ) ) {
1011          $menu_item->ID               = $menu_item->term_id;
1012          $menu_item->db_id            = 0;
1013          $menu_item->menu_item_parent = 0;
1014          $menu_item->object_id        = (int) $menu_item->term_id;
1015          $menu_item->post_parent      = (int) $menu_item->parent;
1016          $menu_item->type             = 'taxonomy';
1017  
1018          $object                = get_taxonomy( $menu_item->taxonomy );
1019          $menu_item->object     = $object->name;
1020          $menu_item->type_label = $object->labels->singular_name;
1021  
1022          $menu_item->title       = $menu_item->name;
1023          $menu_item->url         = get_term_link( $menu_item, $menu_item->taxonomy );
1024          $menu_item->target      = '';
1025          $menu_item->attr_title  = '';
1026          $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
1027          $menu_item->classes     = array();
1028          $menu_item->xfn         = '';
1029  
1030      }
1031  
1032      /**
1033       * Filters a navigation menu item object.
1034       *
1035       * @since 3.0.0
1036       *
1037       * @param object $menu_item The menu item object.
1038       */
1039      return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
1040  }
1041  
1042  /**
1043   * Returns the menu items associated with a particular object.
1044   *
1045   * @since 3.0.0
1046   *
1047   * @param int    $object_id   Optional. The ID of the original object. Default 0.
1048   * @param string $object_type Optional. The type of object, such as 'post_type' or 'taxonomy'.
1049   *                            Default 'post_type'.
1050   * @param string $taxonomy    Optional. If $object_type is 'taxonomy', $taxonomy is the name
1051   *                            of the tax that $object_id belongs to. Default empty.
1052   * @return int[] The array of menu item IDs; empty array if none.
1053   */
1054  function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
1055      $object_id     = (int) $object_id;
1056      $menu_item_ids = array();
1057  
1058      $query      = new WP_Query();
1059      $menu_items = $query->query(
1060          array(
1061              'meta_key'       => '_menu_item_object_id',
1062              'meta_value'     => $object_id,
1063              'post_status'    => 'any',
1064              'post_type'      => 'nav_menu_item',
1065              'posts_per_page' => -1,
1066          )
1067      );
1068      foreach ( (array) $menu_items as $menu_item ) {
1069          if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
1070              $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
1071              if (
1072                  'post_type' === $object_type &&
1073                  'post_type' === $menu_item_type
1074              ) {
1075                  $menu_item_ids[] = (int) $menu_item->ID;
1076              } elseif (
1077                  'taxonomy' === $object_type &&
1078                  'taxonomy' === $menu_item_type &&
1079                  get_post_meta( $menu_item->ID, '_menu_item_object', true ) === $taxonomy
1080              ) {
1081                  $menu_item_ids[] = (int) $menu_item->ID;
1082              }
1083          }
1084      }
1085  
1086      return array_unique( $menu_item_ids );
1087  }
1088  
1089  /**
1090   * Callback for handling a menu item when its original object is deleted.
1091   *
1092   * @since 3.0.0
1093   * @access private
1094   *
1095   * @param int $object_id The ID of the original object being trashed.
1096   */
1097  function _wp_delete_post_menu_item( $object_id ) {
1098      $object_id = (int) $object_id;
1099  
1100      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
1101  
1102      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1103          wp_delete_post( $menu_item_id, true );
1104      }
1105  }
1106  
1107  /**
1108   * Serves as a callback for handling a menu item when its original object is deleted.
1109   *
1110   * @since 3.0.0
1111   * @access private
1112   *
1113   * @param int    $object_id The ID of the original object being trashed.
1114   * @param int    $tt_id     Term taxonomy ID. Unused.
1115   * @param string $taxonomy  Taxonomy slug.
1116   */
1117  function _wp_delete_tax_menu_item( $object_id, $tt_id, $taxonomy ) {
1118      $object_id = (int) $object_id;
1119  
1120      $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
1121  
1122      foreach ( (array) $menu_item_ids as $menu_item_id ) {
1123          wp_delete_post( $menu_item_id, true );
1124      }
1125  }
1126  
1127  /**
1128   * Automatically add newly published page objects to menus with that as an option.
1129   *
1130   * @since 3.0.0
1131   * @access private
1132   *
1133   * @param string  $new_status The new status of the post object.
1134   * @param string  $old_status The old status of the post object.
1135   * @param WP_Post $post       The post object being transitioned from one status to another.
1136   */
1137  function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
1138      if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) {
1139          return;
1140      }
1141      if ( ! empty( $post->post_parent ) ) {
1142          return;
1143      }
1144      $auto_add = get_option( 'nav_menu_options' );
1145      if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) {
1146          return;
1147      }
1148      $auto_add = $auto_add['auto_add'];
1149      if ( empty( $auto_add ) || ! is_array( $auto_add ) ) {
1150          return;
1151      }
1152  
1153      $args = array(
1154          'menu-item-object-id' => $post->ID,
1155          'menu-item-object'    => $post->post_type,
1156          'menu-item-type'      => 'post_type',
1157          'menu-item-status'    => 'publish',
1158      );
1159  
1160      foreach ( $auto_add as $menu_id ) {
1161          $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
1162          if ( ! is_array( $items ) ) {
1163              continue;
1164          }
1165          foreach ( $items as $item ) {
1166              if ( $post->ID === (int) $item->object_id ) {
1167                  continue 2;
1168              }
1169          }
1170          wp_update_nav_menu_item( $menu_id, 0, $args );
1171      }
1172  }
1173  
1174  /**
1175   * Deletes auto-draft posts associated with the supplied changeset.
1176   *
1177   * @since 4.8.0
1178   * @access private
1179   *
1180   * @param int $post_id Post ID for the customize_changeset.
1181   */
1182  function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) {
1183      $post = get_post( $post_id );
1184  
1185      if ( ! $post || 'customize_changeset' !== $post->post_type ) {
1186          return;
1187      }
1188  
1189      $data = json_decode( $post->post_content, true );
1190      if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
1191          return;
1192      }
1193      remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1194      foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
1195          if ( empty( $stub_post_id ) ) {
1196              continue;
1197          }
1198          if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
1199              wp_delete_post( $stub_post_id, true );
1200          } elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
1201              wp_trash_post( $stub_post_id );
1202              delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
1203          }
1204      }
1205      add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
1206  }
1207  
1208  /**
1209   * Handles menu config after theme change.
1210   *
1211   * @access private
1212   * @since 4.9.0
1213   */
1214  function _wp_menus_changed() {
1215      $old_nav_menu_locations    = get_option( 'theme_switch_menu_locations', array() );
1216      $new_nav_menu_locations    = get_nav_menu_locations();
1217      $mapped_nav_menu_locations = wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations );
1218  
1219      set_theme_mod( 'nav_menu_locations', $mapped_nav_menu_locations );
1220      delete_option( 'theme_switch_menu_locations' );
1221  }
1222  
1223  /**
1224   * Maps nav menu locations according to assignments in previously active theme.
1225   *
1226   * @since 4.9.0
1227   *
1228   * @param array $new_nav_menu_locations New nav menu locations assignments.
1229   * @param array $old_nav_menu_locations Old nav menu locations assignments.
1230   * @return array Nav menus mapped to new nav menu locations.
1231   */
1232  function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) {
1233      $registered_nav_menus   = get_registered_nav_menus();
1234      $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus );
1235  
1236      // Short-circuit if there are no old nav menu location assignments to map.
1237      if ( empty( $old_nav_menu_locations ) ) {
1238          return $new_nav_menu_locations;
1239      }
1240  
1241      // If old and new theme have just one location, map it and we're done.
1242      if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) {
1243          $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations );
1244          return $new_nav_menu_locations;
1245      }
1246  
1247      $old_locations = array_keys( $old_nav_menu_locations );
1248  
1249      // Map locations with the same slug.
1250      foreach ( $registered_nav_menus as $location => $name ) {
1251          if ( in_array( $location, $old_locations, true ) ) {
1252              $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ];
1253              unset( $old_nav_menu_locations[ $location ] );
1254          }
1255      }
1256  
1257      // If there are no old nav menu locations left, then we're done.
1258      if ( empty( $old_nav_menu_locations ) ) {
1259          return $new_nav_menu_locations;
1260      }
1261  
1262      /*
1263       * If old and new theme both have locations that contain phrases
1264       * from within the same group, make an educated guess and map it.
1265       */
1266      $common_slug_groups = array(
1267          array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ),
1268          array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ),
1269          array( 'social' ),
1270      );
1271  
1272      // Go through each group...
1273      foreach ( $common_slug_groups as $slug_group ) {
1274  
1275          // ...and see if any of these slugs...
1276          foreach ( $slug_group as $slug ) {
1277  
1278              // ...and any of the new menu locations...
1279              foreach ( $registered_nav_menus as $new_location => $name ) {
1280  
1281                  // ...actually match!
1282                  if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) {
1283                      continue;
1284                  } elseif ( is_numeric( $new_location ) && $new_location !== $slug ) {
1285                      continue;
1286                  }
1287  
1288                  // Then see if any of the old locations...
1289                  foreach ( $old_nav_menu_locations as $location => $menu_id ) {
1290  
1291                      // ...and any slug in the same group...
1292                      foreach ( $slug_group as $slug ) {
1293  
1294                          // ... have a match as well.
1295                          if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) {
1296                              continue;
1297                          } elseif ( is_numeric( $location ) && $location !== $slug ) {
1298                              continue;
1299                          }
1300  
1301                          // Make sure this location wasn't mapped and removed previously.
1302                          if ( ! empty( $old_nav_menu_locations[ $location ] ) ) {
1303  
1304                              // We have a match that can be mapped!
1305                              $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ];
1306  
1307                              // Remove the mapped location so it can't be mapped again.
1308                              unset( $old_nav_menu_locations[ $location ] );
1309  
1310                              // Go back and check the next new menu location.
1311                              continue 3;
1312                          }
1313                      } // End foreach ( $slug_group as $slug ).
1314                  } // End foreach ( $old_nav_menu_locations as $location => $menu_id ).
1315              } // End foreach foreach ( $registered_nav_menus as $new_location => $name ).
1316          } // End foreach ( $slug_group as $slug ).
1317      } // End foreach ( $common_slug_groups as $slug_group ).
1318  
1319      return $new_nav_menu_locations;
1320  }
1321  
1322  /**
1323   * Prevents menu items from being their own parent.
1324   *
1325   * Resets menu_item_parent to 0 when the parent is set to the item itself.
1326   * For use before saving `_menu_item_menu_item_parent` in nav-menus.php.
1327   *
1328   * @since 6.2.0
1329   * @access private
1330   *
1331   * @param array $menu_item_data The menu item data array.
1332   * @return array The menu item data with reset menu_item_parent.
1333   */
1334  function _wp_reset_invalid_menu_item_parent( $menu_item_data ) {
1335      if ( ! is_array( $menu_item_data ) ) {
1336          return $menu_item_data;
1337      }
1338  
1339      if (
1340          ! empty( $menu_item_data['ID'] ) &&
1341          ! empty( $menu_item_data['menu_item_parent'] ) &&
1342          (int) $menu_item_data['ID'] === (int) $menu_item_data['menu_item_parent']
1343      ) {
1344          $menu_item_data['menu_item_parent'] = 0;
1345      }
1346  
1347      return $menu_item_data;
1348  }


Generated : Fri Apr 17 08:20:10 2026 Cross-referenced by PHPXref