nav-menu.php 40 KB

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