You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

980 lines
32 KiB

  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, or name - or the menu 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 object|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
  40. * @param string $menu The menu ID, slug, or name 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 $menu The menu to check (ID, slug, or name).
  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. * @global array $_wp_registered_nav_menus
  85. *
  86. * @param string $location The menu location identifier.
  87. * @return bool True on success, false on failure.
  88. */
  89. function unregister_nav_menu( $location ) {
  90. global $_wp_registered_nav_menus;
  91. if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
  92. unset( $_wp_registered_nav_menus[$location] );
  93. if ( empty( $_wp_registered_nav_menus ) ) {
  94. _remove_theme_support( 'menus' );
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * Registers a navigation menu location for a theme.
  102. *
  103. * @since 3.0.0
  104. *
  105. * @param string $location Menu location identifier, like a slug.
  106. * @param string $description Menu location descriptive text.
  107. */
  108. function register_nav_menu( $location, $description ) {
  109. register_nav_menus( array( $location => $description ) );
  110. }
  111. /**
  112. * Retrieves all registered navigation menu locations in a theme.
  113. *
  114. * @since 3.0.0
  115. *
  116. * @global array $_wp_registered_nav_menus
  117. *
  118. * @return array Registered navigation menu locations. If none are registered, an empty array.
  119. */
  120. function get_registered_nav_menus() {
  121. global $_wp_registered_nav_menus;
  122. if ( isset( $_wp_registered_nav_menus ) )
  123. return $_wp_registered_nav_menus;
  124. return array();
  125. }
  126. /**
  127. * Retrieves all registered navigation menu locations and the menus assigned to them.
  128. *
  129. * @since 3.0.0
  130. *
  131. * @return array Registered navigation menu locations and the menus assigned them.
  132. * If none are registered, an empty array.
  133. */
  134. function get_nav_menu_locations() {
  135. $locations = get_theme_mod( 'nav_menu_locations' );
  136. return ( is_array( $locations ) ) ? $locations : array();
  137. }
  138. /**
  139. * Determines whether a registered nav menu location has a menu assigned to it.
  140. *
  141. * @since 3.0.0
  142. *
  143. * @param string $location Menu location identifier.
  144. * @return bool Whether location has a menu.
  145. */
  146. function has_nav_menu( $location ) {
  147. $has_nav_menu = false;
  148. $registered_nav_menus = get_registered_nav_menus();
  149. if ( isset( $registered_nav_menus[ $location ] ) ) {
  150. $locations = get_nav_menu_locations();
  151. $has_nav_menu = ! empty( $locations[ $location ] );
  152. }
  153. /**
  154. * Filters whether a nav menu is assigned to the specified location.
  155. *
  156. * @since 4.3.0
  157. *
  158. * @param bool $has_nav_menu Whether there is a menu assigned to a location.
  159. * @param string $location Menu location.
  160. */
  161. return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
  162. }
  163. /**
  164. * Determines whether the given ID is a nav menu item.
  165. *
  166. * @since 3.0.0
  167. *
  168. * @param int $menu_item_id The ID of the potential nav menu item.
  169. * @return bool Whether the given ID is that of a nav menu item.
  170. */
  171. function is_nav_menu_item( $menu_item_id = 0 ) {
  172. return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
  173. }
  174. /**
  175. * Creates a navigation menu.
  176. *
  177. * Note that `$menu_name` is expected to be pre-slashed.
  178. *
  179. * @since 3.0.0
  180. *
  181. * @param string $menu_name Menu name.
  182. * @return int|WP_Error Menu ID on success, WP_Error object on failure.
  183. */
  184. function wp_create_nav_menu( $menu_name ) {
  185. // expected_slashed ($menu_name)
  186. return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
  187. }
  188. /**
  189. * Delete a Navigation Menu.
  190. *
  191. * @since 3.0.0
  192. *
  193. * @param string $menu Menu ID, slug, or name.
  194. * @return bool|WP_Error True on success, false or WP_Error object on failure.
  195. */
  196. function wp_delete_nav_menu( $menu ) {
  197. $menu = wp_get_nav_menu_object( $menu );
  198. if ( ! $menu )
  199. return false;
  200. $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
  201. if ( ! empty( $menu_objects ) ) {
  202. foreach ( $menu_objects as $item ) {
  203. wp_delete_post( $item );
  204. }
  205. }
  206. $result = wp_delete_term( $menu->term_id, 'nav_menu' );
  207. // Remove this menu from any locations.
  208. $locations = get_nav_menu_locations();
  209. foreach ( $locations as $location => $menu_id ) {
  210. if ( $menu_id == $menu->term_id )
  211. $locations[ $location ] = 0;
  212. }
  213. set_theme_mod( 'nav_menu_locations', $locations );
  214. if ( $result && !is_wp_error($result) )
  215. /**
  216. * Fires after a navigation menu has been successfully deleted.
  217. *
  218. * @since 3.0.0
  219. *
  220. * @param int $term_id ID of the deleted menu.
  221. */
  222. do_action( 'wp_delete_nav_menu', $menu->term_id );
  223. return $result;
  224. }
  225. /**
  226. * Save the properties of a menu or create a new menu with those properties.
  227. *
  228. * Note that `$menu_data` is expected to be pre-slashed.
  229. *
  230. * @since 3.0.0
  231. *
  232. * @param int $menu_id The ID of the menu or "0" to create a new menu.
  233. * @param array $menu_data The array of menu data.
  234. * @return int|WP_Error Menu ID on success, WP_Error object on failure.
  235. */
  236. function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
  237. // expected_slashed ($menu_data)
  238. $menu_id = (int) $menu_id;
  239. $_menu = wp_get_nav_menu_object( $menu_id );
  240. $args = array(
  241. 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
  242. 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
  243. 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
  244. 'slug' => null,
  245. );
  246. // double-check that we're not going to have one menu take the name of another
  247. $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
  248. if (
  249. $_possible_existing &&
  250. ! is_wp_error( $_possible_existing ) &&
  251. isset( $_possible_existing->term_id ) &&
  252. $_possible_existing->term_id != $menu_id
  253. ) {
  254. return new WP_Error( 'menu_exists',
  255. /* translators: %s: menu name */
  256. sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
  257. '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
  258. )
  259. );
  260. }
  261. // menu doesn't already exist, so create a new menu
  262. if ( ! $_menu || is_wp_error( $_menu ) ) {
  263. $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
  264. if ( $menu_exists ) {
  265. return new WP_Error( 'menu_exists',
  266. /* translators: %s: menu name */
  267. sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
  268. '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
  269. )
  270. );
  271. }
  272. $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
  273. if ( is_wp_error( $_menu ) )
  274. return $_menu;
  275. /**
  276. * Fires after a navigation menu is successfully created.
  277. *
  278. * @since 3.0.0
  279. *
  280. * @param int $term_id ID of the new menu.
  281. * @param array $menu_data An array of menu data.
  282. */
  283. do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
  284. return (int) $_menu['term_id'];
  285. }
  286. if ( ! $_menu || ! isset( $_menu->term_id ) )
  287. return 0;
  288. $menu_id = (int) $_menu->term_id;
  289. $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
  290. if ( is_wp_error( $update_response ) )
  291. return $update_response;
  292. $menu_id = (int) $update_response['term_id'];
  293. /**
  294. * Fires after a navigation menu has been successfully updated.
  295. *
  296. * @since 3.0.0
  297. *
  298. * @param int $menu_id ID of the updated menu.
  299. * @param array $menu_data An array of menu data.
  300. */
  301. do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
  302. return $menu_id;
  303. }
  304. /**
  305. * Save the properties of a menu item or create a new one.
  306. *
  307. * The menu-item-title, menu-item-description, and menu-item-attr-title are expected
  308. * to be pre-slashed since they are passed directly into `wp_insert_post()`.
  309. *
  310. * @since 3.0.0
  311. *
  312. * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
  313. * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
  314. * @param array $menu_item_data The menu item's data.
  315. * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
  316. */
  317. function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
  318. $menu_id = (int) $menu_id;
  319. $menu_item_db_id = (int) $menu_item_db_id;
  320. // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
  321. if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
  322. return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
  323. $menu = wp_get_nav_menu_object( $menu_id );
  324. if ( ! $menu && 0 !== $menu_id ) {
  325. return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
  326. }
  327. if ( is_wp_error( $menu ) ) {
  328. return $menu;
  329. }
  330. $defaults = array(
  331. 'menu-item-db-id' => $menu_item_db_id,
  332. 'menu-item-object-id' => 0,
  333. 'menu-item-object' => '',
  334. 'menu-item-parent-id' => 0,
  335. 'menu-item-position' => 0,
  336. 'menu-item-type' => 'custom',
  337. 'menu-item-title' => '',
  338. 'menu-item-url' => '',
  339. 'menu-item-description' => '',
  340. 'menu-item-attr-title' => '',
  341. 'menu-item-target' => '',
  342. 'menu-item-classes' => '',
  343. 'menu-item-xfn' => '',
  344. 'menu-item-status' => '',
  345. );
  346. $args = wp_parse_args( $menu_item_data, $defaults );
  347. if ( 0 == $menu_id ) {
  348. $args['menu-item-position'] = 1;
  349. } elseif ( 0 == (int) $args['menu-item-position'] ) {
  350. $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
  351. $last_item = array_pop( $menu_items );
  352. $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
  353. }
  354. $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
  355. if ( 'custom' != $args['menu-item-type'] ) {
  356. /* if non-custom menu item, then:
  357. * use original object's URL
  358. * blank default title to sync with original object's
  359. */
  360. $args['menu-item-url'] = '';
  361. $original_title = '';
  362. if ( 'taxonomy' == $args['menu-item-type'] ) {
  363. $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
  364. $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
  365. } elseif ( 'post_type' == $args['menu-item-type'] ) {
  366. $original_object = get_post( $args['menu-item-object-id'] );
  367. $original_parent = (int) $original_object->post_parent;
  368. $original_title = $original_object->post_title;
  369. } elseif ( 'post_type_archive' == $args['menu-item-type'] ) {
  370. $original_object = get_post_type_object( $args['menu-item-object'] );
  371. if ( $original_object ) {
  372. $original_title = $original_object->labels->archives;
  373. }
  374. }
  375. if ( $args['menu-item-title'] == $original_title )
  376. $args['menu-item-title'] = '';
  377. // hack to get wp to create a post object when too many properties are empty
  378. if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
  379. $args['menu-item-description'] = ' ';
  380. }
  381. // Populate the menu item object
  382. $post = array(
  383. 'menu_order' => $args['menu-item-position'],
  384. 'ping_status' => 0,
  385. 'post_content' => $args['menu-item-description'],
  386. 'post_excerpt' => $args['menu-item-attr-title'],
  387. 'post_parent' => $original_parent,
  388. 'post_title' => $args['menu-item-title'],
  389. 'post_type' => 'nav_menu_item',
  390. );
  391. $update = 0 != $menu_item_db_id;
  392. // New menu item. Default is draft status
  393. if ( ! $update ) {
  394. $post['ID'] = 0;
  395. $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
  396. $menu_item_db_id = wp_insert_post( $post );
  397. if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
  398. return $menu_item_db_id;
  399. /**
  400. * Fires immediately after a new navigation menu item has been added.
  401. *
  402. * @since 4.4.0
  403. *
  404. * @see wp_update_nav_menu_item()
  405. *
  406. * @param int $menu_id ID of the updated menu.
  407. * @param int $menu_item_db_id ID of the new menu item.
  408. * @param array $args An array of arguments used to update/add the menu item.
  409. */
  410. do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args );
  411. }
  412. // Associate the menu item with the menu term
  413. // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
  414. if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
  415. wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
  416. }
  417. if ( 'custom' == $args['menu-item-type'] ) {
  418. $args['menu-item-object-id'] = $menu_item_db_id;
  419. $args['menu-item-object'] = 'custom';
  420. }
  421. $menu_item_db_id = (int) $menu_item_db_id;
  422. update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
  423. update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
  424. update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
  425. update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
  426. update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
  427. $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
  428. $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
  429. update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
  430. update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
  431. update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
  432. if ( 0 == $menu_id )
  433. update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
  434. elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
  435. delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
  436. // Update existing menu item. Default is publish status
  437. if ( $update ) {
  438. $post['ID'] = $menu_item_db_id;
  439. $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
  440. wp_update_post( $post );
  441. }
  442. /**
  443. * Fires after a navigation menu item has been updated.
  444. *
  445. * @since 3.0.0
  446. *
  447. * @see wp_update_nav_menu_item()
  448. *
  449. * @param int $menu_id ID of the updated menu.
  450. * @param int $menu_item_db_id ID of the updated menu item.
  451. * @param array $args An array of arguments used to update a menu item.
  452. */
  453. do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
  454. return $menu_item_db_id;
  455. }
  456. /**
  457. * Returns all navigation menu objects.
  458. *
  459. * @since 3.0.0
  460. * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
  461. * to 'name'.
  462. *
  463. * @param array $args Optional. Array of arguments passed on to get_terms().
  464. * Default empty array.
  465. * @return array Menu objects.
  466. */
  467. function wp_get_nav_menus( $args = array() ) {
  468. $defaults = array( 'hide_empty' => false, 'orderby' => 'name' );
  469. $args = wp_parse_args( $args, $defaults );
  470. /**
  471. * Filters the navigation menu objects being returned.
  472. *
  473. * @since 3.0.0
  474. *
  475. * @see get_terms()
  476. *
  477. * @param array $menus An array of menu objects.
  478. * @param array $args An array of arguments used to retrieve menu objects.
  479. */
  480. return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
  481. }
  482. /**
  483. * Return if a menu item is valid.
  484. *
  485. * @link https://core.trac.wordpress.org/ticket/13958
  486. *
  487. * @since 3.2.0
  488. * @access private
  489. *
  490. * @param object $item The menu item to check.
  491. * @return bool False if invalid, otherwise true.
  492. */
  493. function _is_valid_nav_menu_item( $item ) {
  494. return empty( $item->_invalid );
  495. }
  496. /**
  497. * Return all menu items of a navigation menu.
  498. *
  499. * @since 3.0.0
  500. *
  501. * @global string $_menu_item_sort_prop
  502. * @staticvar array $fetched
  503. *
  504. * @param string $menu Menu name, ID, or slug.
  505. * @param array $args Optional. Arguments to pass to get_posts().
  506. * @return false|array $items Array of menu items, otherwise false.
  507. */
  508. function wp_get_nav_menu_items( $menu, $args = array() ) {
  509. $menu = wp_get_nav_menu_object( $menu );
  510. if ( ! $menu ) {
  511. return false;
  512. }
  513. static $fetched = array();
  514. $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
  515. if ( is_wp_error( $items ) ) {
  516. return false;
  517. }
  518. $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
  519. 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
  520. $args = wp_parse_args( $args, $defaults );
  521. $args['include'] = $items;
  522. if ( ! empty( $items ) ) {
  523. $items = get_posts( $args );
  524. } else {
  525. $items = array();
  526. }
  527. // Get all posts and terms at once to prime the caches
  528. if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
  529. $fetched[$menu->term_id] = true;
  530. $posts = array();
  531. $terms = array();
  532. foreach ( $items as $item ) {
  533. $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
  534. $object = get_post_meta( $item->ID, '_menu_item_object', true );
  535. $type = get_post_meta( $item->ID, '_menu_item_type', true );
  536. if ( 'post_type' == $type )
  537. $posts[$object][] = $object_id;
  538. elseif ( 'taxonomy' == $type)
  539. $terms[$object][] = $object_id;
  540. }
  541. if ( ! empty( $posts ) ) {
  542. foreach ( array_keys($posts) as $post_type ) {
  543. get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
  544. }
  545. }
  546. unset($posts);
  547. if ( ! empty( $terms ) ) {
  548. foreach ( array_keys($terms) as $taxonomy ) {
  549. get_terms( $taxonomy, array(
  550. 'include' => $terms[ $taxonomy ],
  551. 'hierarchical' => false,
  552. ) );
  553. }
  554. }
  555. unset($terms);
  556. }
  557. $items = array_map( 'wp_setup_nav_menu_item', $items );
  558. if ( ! is_admin() ) { // Remove invalid items only in front end
  559. $items = array_filter( $items, '_is_valid_nav_menu_item' );
  560. }
  561. if ( ARRAY_A == $args['output'] ) {
  562. $items = wp_list_sort( $items, array(
  563. $args['output_key'] => 'ASC',
  564. ) );
  565. $i = 1;
  566. foreach ( $items as $k => $item ) {
  567. $items[$k]->{$args['output_key']} = $i++;
  568. }
  569. }
  570. /**
  571. * Filters the navigation menu items being returned.
  572. *
  573. * @since 3.0.0
  574. *
  575. * @param array $items An array of menu item post objects.
  576. * @param object $menu The menu object.
  577. * @param array $args An array of arguments used to retrieve menu item objects.
  578. */
  579. return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
  580. }
  581. /**
  582. * Decorates a menu item object with the shared navigation menu item properties.
  583. *
  584. * Properties:
  585. * - ID: The term_id if the menu item represents a taxonomy term.
  586. * - attr_title: The title attribute of the link element for this menu item.
  587. * - classes: The array of class attribute values for the link element of this menu item.
  588. * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
  589. * - description: The description of this menu item.
  590. * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
  591. * - object: The type of object originally represented, such as "category," "post", or "attachment."
  592. * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
  593. * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
  594. * - post_title: A "no title" label if menu item represents a post that lacks a title.
  595. * - target: The target attribute of the link element for this menu item.
  596. * - title: The title of this menu item.
  597. * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
  598. * - type_label: The singular label used to describe this type of menu item.
  599. * - url: The URL to which this menu item points.
  600. * - xfn: The XFN relationship expressed in the link of this menu item.
  601. * - _invalid: Whether the menu item represents an object that no longer exists.
  602. *
  603. * @since 3.0.0
  604. *
  605. * @param object $menu_item The menu item to modify.
  606. * @return object $menu_item The menu item with standard menu item properties.
  607. */
  608. function wp_setup_nav_menu_item( $menu_item ) {
  609. if ( isset( $menu_item->post_type ) ) {
  610. if ( 'nav_menu_item' == $menu_item->post_type ) {
  611. $menu_item->db_id = (int) $menu_item->ID;
  612. $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;
  613. $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
  614. $menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
  615. $menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
  616. if ( 'post_type' == $menu_item->type ) {
  617. $object = get_post_type_object( $menu_item->object );
  618. if ( $object ) {
  619. $menu_item->type_label = $object->labels->singular_name;
  620. } else {
  621. $menu_item->type_label = $menu_item->object;
  622. $menu_item->_invalid = true;
  623. }
  624. if ( 'trash' === get_post_status( $menu_item->object_id ) ) {
  625. $menu_item->_invalid = true;
  626. }
  627. $menu_item->url = get_permalink( $menu_item->object_id );
  628. $original_object = get_post( $menu_item->object_id );
  629. /** This filter is documented in wp-includes/post-template.php */
  630. $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
  631. if ( '' === $original_title ) {
  632. /* translators: %d: ID of a post */
  633. $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
  634. }
  635. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
  636. } elseif ( 'post_type_archive' == $menu_item->type ) {
  637. $object = get_post_type_object( $menu_item->object );
  638. if ( $object ) {
  639. $menu_item->title = '' == $menu_item->post_title ? $object->labels->archives : $menu_item->post_title;
  640. $post_type_description = $object->description;
  641. } else {
  642. $menu_item->_invalid = true;
  643. $post_type_description = '';
  644. }
  645. $menu_item->type_label = __( 'Post Type Archive' );
  646. $post_content = wp_trim_words( $menu_item->post_content, 200 );
  647. $post_type_description = '' == $post_content ? $post_type_description : $post_content;
  648. $menu_item->url = get_post_type_archive_link( $menu_item->object );
  649. } elseif ( 'taxonomy' == $menu_item->type ) {
  650. $object = get_taxonomy( $menu_item->object );
  651. if ( $object ) {
  652. $menu_item->type_label = $object->labels->singular_name;
  653. } else {
  654. $menu_item->type_label = $menu_item->object;
  655. $menu_item->_invalid = true;
  656. }
  657. $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
  658. $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
  659. $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
  660. if ( is_wp_error( $original_title ) )
  661. $original_title = false;
  662. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
  663. } else {
  664. $menu_item->type_label = __('Custom Link');
  665. $menu_item->title = $menu_item->post_title;
  666. $menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
  667. }
  668. $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
  669. /**
  670. * Filters a navigation menu item's title attribute.
  671. *
  672. * @since 3.0.0
  673. *
  674. * @param string $item_title The menu item title attribute.
  675. */
  676. $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
  677. if ( ! isset( $menu_item->description ) ) {
  678. /**
  679. * Filters a navigation menu item's description.
  680. *
  681. * @since 3.0.0
  682. *
  683. * @param string $description The menu item description.
  684. */
  685. $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
  686. }
  687. $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
  688. $menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
  689. } else {
  690. $menu_item->db_id = 0;
  691. $menu_item->menu_item_parent = 0;
  692. $menu_item->object_id = (int) $menu_item->ID;
  693. $menu_item->type = 'post_type';
  694. $object = get_post_type_object( $menu_item->post_type );
  695. $menu_item->object = $object->name;
  696. $menu_item->type_label = $object->labels->singular_name;
  697. if ( '' === $menu_item->post_title ) {
  698. /* translators: %d: ID of a post */
  699. $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
  700. }
  701. $menu_item->title = $menu_item->post_title;
  702. $menu_item->url = get_permalink( $menu_item->ID );
  703. $menu_item->target = '';
  704. /** This filter is documented in wp-includes/nav-menu.php */
  705. $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
  706. /** This filter is documented in wp-includes/nav-menu.php */
  707. $menu_item->description = apply_filters( 'nav_menu_description', '' );
  708. $menu_item->classes = array();
  709. $menu_item->xfn = '';
  710. }
  711. } elseif ( isset( $menu_item->taxonomy ) ) {
  712. $menu_item->ID = $menu_item->term_id;
  713. $menu_item->db_id = 0;
  714. $menu_item->menu_item_parent = 0;
  715. $menu_item->object_id = (int) $menu_item->term_id;
  716. $menu_item->post_parent = (int) $menu_item->parent;
  717. $menu_item->type = 'taxonomy';
  718. $object = get_taxonomy( $menu_item->taxonomy );
  719. $menu_item->object = $object->name;
  720. $menu_item->type_label = $object->labels->singular_name;
  721. $menu_item->title = $menu_item->name;
  722. $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
  723. $menu_item->target = '';
  724. $menu_item->attr_title = '';
  725. $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
  726. $menu_item->classes = array();
  727. $menu_item->xfn = '';
  728. }
  729. /**
  730. * Filters a navigation menu item object.
  731. *
  732. * @since 3.0.0
  733. *
  734. * @param object $menu_item The menu item object.
  735. */
  736. return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
  737. }
  738. /**
  739. * Get the menu items associated with a particular object.
  740. *
  741. * @since 3.0.0
  742. *
  743. * @param int $object_id The ID of the original object.
  744. * @param string $object_type The type of object, such as "taxonomy" or "post_type."
  745. * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
  746. * @return array The array of menu item IDs; empty array if none;
  747. */
  748. function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
  749. $object_id = (int) $object_id;
  750. $menu_item_ids = array();
  751. $query = new WP_Query;
  752. $menu_items = $query->query(
  753. array(
  754. 'meta_key' => '_menu_item_object_id',
  755. 'meta_value' => $object_id,
  756. 'post_status' => 'any',
  757. 'post_type' => 'nav_menu_item',
  758. 'posts_per_page' => -1,
  759. )
  760. );
  761. foreach ( (array) $menu_items as $menu_item ) {
  762. if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
  763. $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
  764. if (
  765. 'post_type' == $object_type &&
  766. 'post_type' == $menu_item_type
  767. ) {
  768. $menu_item_ids[] = (int) $menu_item->ID;
  769. } elseif (
  770. 'taxonomy' == $object_type &&
  771. 'taxonomy' == $menu_item_type &&
  772. get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
  773. ) {
  774. $menu_item_ids[] = (int) $menu_item->ID;
  775. }
  776. }
  777. }
  778. return array_unique( $menu_item_ids );
  779. }
  780. /**
  781. * Callback for handling a menu item when its original object is deleted.
  782. *
  783. * @since 3.0.0
  784. * @access private
  785. *
  786. * @param int $object_id The ID of the original object being trashed.
  787. *
  788. */
  789. function _wp_delete_post_menu_item( $object_id = 0 ) {
  790. $object_id = (int) $object_id;
  791. $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
  792. foreach ( (array) $menu_item_ids as $menu_item_id ) {
  793. wp_delete_post( $menu_item_id, true );
  794. }
  795. }
  796. /**
  797. * Serves as a callback for handling a menu item when its original object is deleted.
  798. *
  799. * @since 3.0.0
  800. * @access private
  801. *
  802. * @param int $object_id Optional. The ID of the original object being trashed. Default 0.
  803. * @param int $tt_id Term taxonomy ID. Unused.
  804. * @param string $taxonomy Taxonomy slug.
  805. */
  806. function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
  807. $object_id = (int) $object_id;
  808. $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
  809. foreach ( (array) $menu_item_ids as $menu_item_id ) {
  810. wp_delete_post( $menu_item_id, true );
  811. }
  812. }
  813. /**
  814. * Automatically add newly published page objects to menus with that as an option.
  815. *
  816. * @since 3.0.0
  817. * @access private
  818. *
  819. * @param string $new_status The new status of the post object.
  820. * @param string $old_status The old status of the post object.
  821. * @param object $post The post object being transitioned from one status to another.
  822. */
  823. function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
  824. if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
  825. return;
  826. if ( ! empty( $post->post_parent ) )
  827. return;
  828. $auto_add = get_option( 'nav_menu_options' );
  829. if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
  830. return;
  831. $auto_add = $auto_add['auto_add'];
  832. if ( empty( $auto_add ) || ! is_array( $auto_add ) )
  833. return;
  834. $args = array(
  835. 'menu-item-object-id' => $post->ID,
  836. 'menu-item-object' => $post->post_type,
  837. 'menu-item-type' => 'post_type',
  838. 'menu-item-status' => 'publish',
  839. );
  840. foreach ( $auto_add as $menu_id ) {
  841. $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
  842. if ( ! is_array( $items ) )
  843. continue;
  844. foreach ( $items as $item ) {
  845. if ( $post->ID == $item->object_id )
  846. continue 2;
  847. }
  848. wp_update_nav_menu_item( $menu_id, 0, $args );
  849. }
  850. }