/wp-includes/nav-menu.php

https://bitbucket.org/skyarch-iijima/wordpress · PHP · 1176 lines · 589 code · 158 blank · 429 comment · 148 complexity · 5cdaca092b849167e6fa8f9ce6a3df45 MD5 · raw file

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