nav-menu.php 41 KB

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