PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/nav-menu.php

https://github.com/bbt123/WordPress
PHP | 892 lines | 462 code | 128 blank | 302 comment | 129 complexity | eb14a620a6d5fb0bde6c56f94d713faa MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  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. * @uses get_term
  15. * @uses get_term_by
  16. *
  17. * @param string $menu Menu ID, slug, or name.
  18. * @return mixed false if $menu param isn't supplied or term does not exist, menu object if successful.
  19. */
  20. function wp_get_nav_menu_object( $menu ) {
  21. if ( ! $menu )
  22. return false;
  23. $menu_obj = get_term( $menu, 'nav_menu' );
  24. if ( ! $menu_obj )
  25. $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
  26. if ( ! $menu_obj )
  27. $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
  28. if ( ! $menu_obj )
  29. $menu_obj = false;
  30. return $menu_obj;
  31. }
  32. /**
  33. * Check if the given ID is a navigation menu.
  34. *
  35. * Returns true if it is; false otherwise.
  36. *
  37. * @since 3.0.0
  38. *
  39. * @param int|string $menu The menu to check (ID, slug, or name).
  40. * @return bool Whether the menu exists.
  41. */
  42. function is_nav_menu( $menu ) {
  43. if ( ! $menu )
  44. return false;
  45. $menu_obj = wp_get_nav_menu_object( $menu );
  46. if (
  47. $menu_obj &&
  48. ! is_wp_error( $menu_obj ) &&
  49. ! empty( $menu_obj->taxonomy ) &&
  50. 'nav_menu' == $menu_obj->taxonomy
  51. )
  52. return true;
  53. return false;
  54. }
  55. /**
  56. * Register navigation menus for a theme.
  57. *
  58. * @since 3.0.0
  59. *
  60. * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
  61. */
  62. function register_nav_menus( $locations = array() ) {
  63. global $_wp_registered_nav_menus;
  64. add_theme_support( 'menus' );
  65. $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
  66. }
  67. /**
  68. * Unregisters a navigation menu for a theme.
  69. *
  70. * @param array $location the menu location identifier
  71. *
  72. * @return bool True on success, false on failure.
  73. */
  74. function unregister_nav_menu( $location ) {
  75. global $_wp_registered_nav_menus;
  76. if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
  77. unset( $_wp_registered_nav_menus[$location] );
  78. if ( empty( $_wp_registered_nav_menus ) ) {
  79. _remove_theme_support( 'menus' );
  80. }
  81. return true;
  82. }
  83. return false;
  84. }
  85. /**
  86. * Register a navigation menu for a theme.
  87. *
  88. * @since 3.0.0
  89. *
  90. * @param string $location Menu location identifier, like a slug.
  91. * @param string $description Menu location descriptive text.
  92. */
  93. function register_nav_menu( $location, $description ) {
  94. register_nav_menus( array( $location => $description ) );
  95. }
  96. /**
  97. * Returns an array of all registered navigation menus in a theme
  98. *
  99. * @since 3.0.0
  100. * @return array
  101. */
  102. function get_registered_nav_menus() {
  103. global $_wp_registered_nav_menus;
  104. if ( isset( $_wp_registered_nav_menus ) )
  105. return $_wp_registered_nav_menus;
  106. return array();
  107. }
  108. /**
  109. * Returns an array with the registered navigation menu locations and the menu assigned to it
  110. *
  111. * @since 3.0.0
  112. * @return array
  113. */
  114. function get_nav_menu_locations() {
  115. $locations = get_theme_mod( 'nav_menu_locations' );
  116. return ( is_array( $locations ) ) ? $locations : array();
  117. }
  118. /**
  119. * Whether a registered nav menu location has a menu assigned to it.
  120. *
  121. * @since 3.0.0
  122. * @param string $location Menu location identifier.
  123. * @return bool Whether location has a menu.
  124. */
  125. function has_nav_menu( $location ) {
  126. $locations = get_nav_menu_locations();
  127. return ( ! empty( $locations[ $location ] ) );
  128. }
  129. /**
  130. * Determine whether the given ID is a nav menu item.
  131. *
  132. * @since 3.0.0
  133. *
  134. * @param int $menu_item_id The ID of the potential nav menu item.
  135. * @return bool Whether the given ID is that of a nav menu item.
  136. */
  137. function is_nav_menu_item( $menu_item_id = 0 ) {
  138. return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
  139. }
  140. /**
  141. * Create a Navigation Menu.
  142. *
  143. * @since 3.0.0
  144. *
  145. * @param string $menu_name Menu name.
  146. * @return int|WP_Error Menu ID on success, WP_Error object on failure.
  147. */
  148. function wp_create_nav_menu( $menu_name ) {
  149. return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
  150. }
  151. /**
  152. * Delete a Navigation Menu.
  153. *
  154. * @since 3.0.0
  155. *
  156. * @param string $menu Menu ID, slug, or name.
  157. * @return bool|WP_Error True on success, false or WP_Error object on failure.
  158. */
  159. function wp_delete_nav_menu( $menu ) {
  160. $menu = wp_get_nav_menu_object( $menu );
  161. if ( ! $menu )
  162. return false;
  163. $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
  164. if ( ! empty( $menu_objects ) ) {
  165. foreach ( $menu_objects as $item ) {
  166. wp_delete_post( $item );
  167. }
  168. }
  169. $result = wp_delete_term( $menu->term_id, 'nav_menu' );
  170. // Remove this menu from any locations.
  171. $locations = get_nav_menu_locations();
  172. foreach ( $locations as $location => $menu_id ) {
  173. if ( $menu_id == $menu->term_id )
  174. $locations[ $location ] = 0;
  175. }
  176. set_theme_mod( 'nav_menu_locations', $locations );
  177. if ( $result && !is_wp_error($result) )
  178. /**
  179. * Fires after a navigation menu has been successfully deleted.
  180. *
  181. * @since 3.0.0
  182. *
  183. * @param int $term_id ID of the deleted menu.
  184. */
  185. do_action( 'wp_delete_nav_menu', $menu->term_id );
  186. return $result;
  187. }
  188. /**
  189. * Save the properties of a menu or create a new menu with those properties.
  190. *
  191. * @since 3.0.0
  192. *
  193. * @param int $menu_id The ID of the menu or "0" to create a new menu.
  194. * @param array $menu_data The array of menu data.
  195. * @return int|WP_Error Menu ID on success, WP_Error object on failure.
  196. */
  197. function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
  198. $menu_id = (int) $menu_id;
  199. $_menu = wp_get_nav_menu_object( $menu_id );
  200. $args = array(
  201. 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
  202. 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
  203. 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
  204. 'slug' => null,
  205. );
  206. // double-check that we're not going to have one menu take the name of another
  207. $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
  208. if (
  209. $_possible_existing &&
  210. ! is_wp_error( $_possible_existing ) &&
  211. isset( $_possible_existing->term_id ) &&
  212. $_possible_existing->term_id != $menu_id
  213. )
  214. return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
  215. // menu doesn't already exist, so create a new menu
  216. if ( ! $_menu || is_wp_error( $_menu ) ) {
  217. $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
  218. if ( $menu_exists )
  219. return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
  220. $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
  221. if ( is_wp_error( $_menu ) )
  222. return $_menu;
  223. /**
  224. * Fires after a navigation menu is successfully created.
  225. *
  226. * @since 3.0.0
  227. *
  228. * @param int $term_id ID of the new menu.
  229. * @param array $menu_data An array of menu data.
  230. */
  231. do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
  232. return (int) $_menu['term_id'];
  233. }
  234. if ( ! $_menu || ! isset( $_menu->term_id ) )
  235. return 0;
  236. $menu_id = (int) $_menu->term_id;
  237. $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
  238. if ( is_wp_error( $update_response ) )
  239. return $update_response;
  240. /**
  241. * Fires after a navigation menu has been successfully updated.
  242. *
  243. * @since 3.0.0
  244. *
  245. * @param int $menu_id ID of the updated menu.
  246. * @param array $menu_data An array of menu data.
  247. */
  248. do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
  249. return $menu_id;
  250. }
  251. /**
  252. * Save the properties of a menu item or create a new one.
  253. *
  254. * @since 3.0.0
  255. *
  256. * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
  257. * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
  258. * @param array $menu_item_data The menu item's data.
  259. * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
  260. */
  261. function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
  262. $menu_id = (int) $menu_id;
  263. $menu_item_db_id = (int) $menu_item_db_id;
  264. // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
  265. if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
  266. return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
  267. $menu = wp_get_nav_menu_object( $menu_id );
  268. if ( ! $menu && 0 !== $menu_id ) {
  269. return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
  270. }
  271. if ( is_wp_error( $menu ) ) {
  272. return $menu;
  273. }
  274. $defaults = array(
  275. 'menu-item-db-id' => $menu_item_db_id,
  276. 'menu-item-object-id' => 0,
  277. 'menu-item-object' => '',
  278. 'menu-item-parent-id' => 0,
  279. 'menu-item-position' => 0,
  280. 'menu-item-type' => 'custom',
  281. 'menu-item-title' => '',
  282. 'menu-item-url' => '',
  283. 'menu-item-description' => '',
  284. 'menu-item-attr-title' => '',
  285. 'menu-item-target' => '',
  286. 'menu-item-classes' => '',
  287. 'menu-item-xfn' => '',
  288. 'menu-item-status' => '',
  289. );
  290. $args = wp_parse_args( $menu_item_data, $defaults );
  291. if ( 0 == $menu_id ) {
  292. $args['menu-item-position'] = 1;
  293. } elseif ( 0 == (int) $args['menu-item-position'] ) {
  294. $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
  295. $last_item = array_pop( $menu_items );
  296. $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
  297. }
  298. $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
  299. if ( 'custom' != $args['menu-item-type'] ) {
  300. /* if non-custom menu item, then:
  301. * use original object's URL
  302. * blank default title to sync with original object's
  303. */
  304. $args['menu-item-url'] = '';
  305. $original_title = '';
  306. if ( 'taxonomy' == $args['menu-item-type'] ) {
  307. $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
  308. $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
  309. } elseif ( 'post_type' == $args['menu-item-type'] ) {
  310. $original_object = get_post( $args['menu-item-object-id'] );
  311. $original_parent = (int) $original_object->post_parent;
  312. $original_title = $original_object->post_title;
  313. }
  314. if ( $args['menu-item-title'] == $original_title )
  315. $args['menu-item-title'] = '';
  316. // hack to get wp to create a post object when too many properties are empty
  317. if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
  318. $args['menu-item-description'] = ' ';
  319. }
  320. // Populate the menu item object
  321. $post = array(
  322. 'menu_order' => $args['menu-item-position'],
  323. 'ping_status' => 0,
  324. 'post_content' => $args['menu-item-description'],
  325. 'post_excerpt' => $args['menu-item-attr-title'],
  326. 'post_parent' => $original_parent,
  327. 'post_title' => $args['menu-item-title'],
  328. 'post_type' => 'nav_menu_item',
  329. );
  330. $update = 0 != $menu_item_db_id;
  331. // New menu item. Default is draft status
  332. if ( ! $update ) {
  333. $post['ID'] = 0;
  334. $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
  335. $menu_item_db_id = wp_insert_post( $post );
  336. if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
  337. return $menu_item_db_id;
  338. }
  339. // Associate the menu item with the menu term
  340. // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
  341. if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
  342. wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
  343. }
  344. if ( 'custom' == $args['menu-item-type'] ) {
  345. $args['menu-item-object-id'] = $menu_item_db_id;
  346. $args['menu-item-object'] = 'custom';
  347. }
  348. $menu_item_db_id = (int) $menu_item_db_id;
  349. update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
  350. update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
  351. update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
  352. update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
  353. update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
  354. $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
  355. $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
  356. update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
  357. update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
  358. update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
  359. if ( 0 == $menu_id )
  360. update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
  361. elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
  362. delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
  363. // Update existing menu item. Default is publish status
  364. if ( $update ) {
  365. $post['ID'] = $menu_item_db_id;
  366. $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
  367. wp_update_post( $post );
  368. }
  369. /**
  370. * Fires after a navigation menu item has been updated.
  371. *
  372. * @since 3.0.0
  373. *
  374. * @see wp_update_nav_menu_items()
  375. *
  376. * @param int $menu_id ID of the updated menu.
  377. * @param int $menu_item_db_id ID of the updated menu item.
  378. * @param array $args An array of arguments used to update a menu item.
  379. */
  380. do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
  381. return $menu_item_db_id;
  382. }
  383. /**
  384. * Returns all navigation menu objects.
  385. *
  386. * @since 3.0.0
  387. *
  388. * @param array $args Array of arguments passed on to get_terms().
  389. * @return array menu objects
  390. */
  391. function wp_get_nav_menus( $args = array() ) {
  392. $defaults = array( 'hide_empty' => false, 'orderby' => 'none' );
  393. $args = wp_parse_args( $args, $defaults );
  394. /**
  395. * Filter the navigation menu objects being returned.
  396. *
  397. * @since 3.0.0
  398. *
  399. * @see get_terms()
  400. *
  401. * @param array $menus An array of menu objects.
  402. * @param array $args An array of arguments used to retrieve menu objects.
  403. */
  404. return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
  405. }
  406. /**
  407. * Sort menu items by the desired key.
  408. *
  409. * @since 3.0.0
  410. * @access private
  411. *
  412. * @param object $a The first object to compare
  413. * @param object $b The second object to compare
  414. * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
  415. */
  416. function _sort_nav_menu_items( $a, $b ) {
  417. global $_menu_item_sort_prop;
  418. if ( empty( $_menu_item_sort_prop ) )
  419. return 0;
  420. if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
  421. return 0;
  422. $_a = (int) $a->$_menu_item_sort_prop;
  423. $_b = (int) $b->$_menu_item_sort_prop;
  424. if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
  425. return 0;
  426. elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
  427. return $_a < $_b ? -1 : 1;
  428. else
  429. return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
  430. }
  431. /**
  432. * Returns if a menu item is valid. Bug #13958
  433. *
  434. * @since 3.2.0
  435. * @access private
  436. *
  437. * @param object $menu_item The menu item to check
  438. * @return bool false if invalid, else true.
  439. */
  440. function _is_valid_nav_menu_item( $item ) {
  441. if ( ! empty( $item->_invalid ) )
  442. return false;
  443. return true;
  444. }
  445. /**
  446. * Returns all menu items of a navigation menu.
  447. *
  448. * @since 3.0.0
  449. *
  450. * @param string $menu menu name, id, or slug
  451. * @param string $args
  452. * @return mixed $items array of menu items, else false.
  453. */
  454. function wp_get_nav_menu_items( $menu, $args = array() ) {
  455. $menu = wp_get_nav_menu_object( $menu );
  456. if ( ! $menu )
  457. return false;
  458. static $fetched = array();
  459. $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
  460. if ( empty( $items ) )
  461. return $items;
  462. $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
  463. 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
  464. $args = wp_parse_args( $args, $defaults );
  465. if ( count( $items ) > 1 )
  466. $args['include'] = implode( ',', $items );
  467. else
  468. $args['include'] = $items[0];
  469. $items = get_posts( $args );
  470. if ( is_wp_error( $items ) || ! is_array( $items ) )
  471. return false;
  472. // Get all posts and terms at once to prime the caches
  473. if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
  474. $fetched[$menu->term_id] = true;
  475. $posts = array();
  476. $terms = array();
  477. foreach ( $items as $item ) {
  478. $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
  479. $object = get_post_meta( $item->ID, '_menu_item_object', true );
  480. $type = get_post_meta( $item->ID, '_menu_item_type', true );
  481. if ( 'post_type' == $type )
  482. $posts[$object][] = $object_id;
  483. elseif ( 'taxonomy' == $type)
  484. $terms[$object][] = $object_id;
  485. }
  486. if ( ! empty( $posts ) ) {
  487. foreach ( array_keys($posts) as $post_type ) {
  488. get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
  489. }
  490. }
  491. unset($posts);
  492. if ( ! empty( $terms ) ) {
  493. foreach ( array_keys($terms) as $taxonomy ) {
  494. get_terms($taxonomy, array('include' => $terms[$taxonomy]) );
  495. }
  496. }
  497. unset($terms);
  498. }
  499. $items = array_map( 'wp_setup_nav_menu_item', $items );
  500. if ( ! is_admin() ) // Remove invalid items only in frontend
  501. $items = array_filter( $items, '_is_valid_nav_menu_item' );
  502. if ( ARRAY_A == $args['output'] ) {
  503. $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
  504. usort($items, '_sort_nav_menu_items');
  505. $i = 1;
  506. foreach( $items as $k => $item ) {
  507. $items[$k]->$args['output_key'] = $i++;
  508. }
  509. }
  510. /**
  511. * Filter the navigation menu items being returned.
  512. *
  513. * @since 3.0.0
  514. *
  515. * @param array $items An array of menu item post objects.
  516. * @param object $menu The menu object.
  517. * @param array $args An array of arguments used to retrieve menu item objects.
  518. */
  519. return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
  520. }
  521. /**
  522. * Decorates a menu item object with the shared navigation menu item properties.
  523. *
  524. * Properties:
  525. * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
  526. * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
  527. * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
  528. * - object: The type of object originally represented, such as "category," "post", or "attachment."
  529. * - type_label: The singular label used to describe this type of menu item.
  530. * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
  531. * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
  532. * - url: The URL to which this menu item points.
  533. * - title: The title of this menu item.
  534. * - target: The target attribute of the link element for this menu item.
  535. * - attr_title: The title attribute of the link element for this menu item.
  536. * - classes: The array of class attribute values for the link element of this menu item.
  537. * - xfn: The XFN relationship expressed in the link of this menu item.
  538. * - description: The description of this menu item.
  539. *
  540. * @since 3.0.0
  541. *
  542. * @param object $menu_item The menu item to modify.
  543. * @return object $menu_item The menu item with standard menu item properties.
  544. */
  545. function wp_setup_nav_menu_item( $menu_item ) {
  546. if ( isset( $menu_item->post_type ) ) {
  547. if ( 'nav_menu_item' == $menu_item->post_type ) {
  548. $menu_item->db_id = (int) $menu_item->ID;
  549. $menu_item->menu_item_parent = empty( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
  550. $menu_item->object_id = empty( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
  551. $menu_item->object = empty( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
  552. $menu_item->type = empty( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
  553. if ( 'post_type' == $menu_item->type ) {
  554. $object = get_post_type_object( $menu_item->object );
  555. if ( $object ) {
  556. $menu_item->type_label = $object->labels->singular_name;
  557. } else {
  558. $menu_item->type_label = $menu_item->object;
  559. $menu_item->_invalid = true;
  560. }
  561. $menu_item->url = get_permalink( $menu_item->object_id );
  562. $original_object = get_post( $menu_item->object_id );
  563. $original_title = $original_object->post_title;
  564. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
  565. } elseif ( 'taxonomy' == $menu_item->type ) {
  566. $object = get_taxonomy( $menu_item->object );
  567. if ( $object ) {
  568. $menu_item->type_label = $object->labels->singular_name;
  569. } else {
  570. $menu_item->type_label = $menu_item->object;
  571. $menu_item->_invalid = true;
  572. }
  573. $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
  574. $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
  575. $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
  576. if ( is_wp_error( $original_title ) )
  577. $original_title = false;
  578. $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
  579. } else {
  580. $menu_item->type_label = __('Custom');
  581. $menu_item->title = $menu_item->post_title;
  582. $menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
  583. }
  584. $menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
  585. /**
  586. * Filter a navigation menu item's title attribute.
  587. *
  588. * @since 3.0.0
  589. *
  590. * @param string $item_title The menu item title attribute.
  591. */
  592. $menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
  593. if ( empty( $menu_item->description ) ) {
  594. /**
  595. * Filter a navigation menu item's description.
  596. *
  597. * @since 3.0.0
  598. *
  599. * @param string $description The menu item description.
  600. */
  601. $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
  602. }
  603. $menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
  604. $menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
  605. } else {
  606. $menu_item->db_id = 0;
  607. $menu_item->menu_item_parent = 0;
  608. $menu_item->object_id = (int) $menu_item->ID;
  609. $menu_item->type = 'post_type';
  610. $object = get_post_type_object( $menu_item->post_type );
  611. $menu_item->object = $object->name;
  612. $menu_item->type_label = $object->labels->singular_name;
  613. if ( '' === $menu_item->post_title )
  614. $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
  615. $menu_item->title = $menu_item->post_title;
  616. $menu_item->url = get_permalink( $menu_item->ID );
  617. $menu_item->target = '';
  618. /** This filter is documented in wp-includes/nav-menu.php */
  619. $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
  620. /** This filter is documented in wp-includes/nav-menu.php */
  621. $menu_item->description = apply_filters( 'nav_menu_description', '' );
  622. $menu_item->classes = array();
  623. $menu_item->xfn = '';
  624. }
  625. } elseif ( isset( $menu_item->taxonomy ) ) {
  626. $menu_item->ID = $menu_item->term_id;
  627. $menu_item->db_id = 0;
  628. $menu_item->menu_item_parent = 0;
  629. $menu_item->object_id = (int) $menu_item->term_id;
  630. $menu_item->post_parent = (int) $menu_item->parent;
  631. $menu_item->type = 'taxonomy';
  632. $object = get_taxonomy( $menu_item->taxonomy );
  633. $menu_item->object = $object->name;
  634. $menu_item->type_label = $object->labels->singular_name;
  635. $menu_item->title = $menu_item->name;
  636. $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
  637. $menu_item->target = '';
  638. $menu_item->attr_title = '';
  639. $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
  640. $menu_item->classes = array();
  641. $menu_item->xfn = '';
  642. }
  643. /**
  644. * Filter a navigation menu item object.
  645. *
  646. * @since 3.0.0
  647. *
  648. * @param object $menu_item The menu item object.
  649. */
  650. return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
  651. }
  652. /**
  653. * Get the menu items associated with a particular object.
  654. *
  655. * @since 3.0.0
  656. *
  657. * @param int $object_id The ID of the original object.
  658. * @param string $object_type The type of object, such as "taxonomy" or "post_type."
  659. * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
  660. * @return array The array of menu item IDs; empty array if none;
  661. */
  662. function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
  663. $object_id = (int) $object_id;
  664. $menu_item_ids = array();
  665. $query = new WP_Query;
  666. $menu_items = $query->query(
  667. array(
  668. 'meta_key' => '_menu_item_object_id',
  669. 'meta_value' => $object_id,
  670. 'post_status' => 'any',
  671. 'post_type' => 'nav_menu_item',
  672. 'posts_per_page' => -1,
  673. )
  674. );
  675. foreach( (array) $menu_items as $menu_item ) {
  676. if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
  677. $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
  678. if (
  679. 'post_type' == $object_type &&
  680. 'post_type' == $menu_item_type
  681. ) {
  682. $menu_item_ids[] = (int) $menu_item->ID;
  683. } else if (
  684. 'taxonomy' == $object_type &&
  685. 'taxonomy' == $menu_item_type &&
  686. get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
  687. ) {
  688. $menu_item_ids[] = (int) $menu_item->ID;
  689. }
  690. }
  691. }
  692. return array_unique( $menu_item_ids );
  693. }
  694. /**
  695. * Callback for handling a menu item when its original object is deleted.
  696. *
  697. * @since 3.0.0
  698. * @access private
  699. *
  700. * @param int $object_id The ID of the original object being trashed.
  701. *
  702. */
  703. function _wp_delete_post_menu_item( $object_id = 0 ) {
  704. $object_id = (int) $object_id;
  705. $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
  706. foreach( (array) $menu_item_ids as $menu_item_id ) {
  707. wp_delete_post( $menu_item_id, true );
  708. }
  709. }
  710. /**
  711. * Callback for handling a menu item when its original object is deleted.
  712. *
  713. * @since 3.0.0
  714. * @access private
  715. *
  716. * @param int $object_id The ID of the original object being trashed.
  717. *
  718. */
  719. function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
  720. $object_id = (int) $object_id;
  721. $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
  722. foreach( (array) $menu_item_ids as $menu_item_id ) {
  723. wp_delete_post( $menu_item_id, true );
  724. }
  725. }
  726. /**
  727. * Automatically add newly published page objects to menus with that as an option.
  728. *
  729. * @since 3.0.0
  730. * @access private
  731. *
  732. * @param string $new_status The new status of the post object.
  733. * @param string $old_status The old status of the post object.
  734. * @param object $post The post object being transitioned from one status to another.
  735. * @return void
  736. */
  737. function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
  738. if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
  739. return;
  740. if ( ! empty( $post->post_parent ) )
  741. return;
  742. $auto_add = get_option( 'nav_menu_options' );
  743. if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
  744. return;
  745. $auto_add = $auto_add['auto_add'];
  746. if ( empty( $auto_add ) || ! is_array( $auto_add ) )
  747. return;
  748. $args = array(
  749. 'menu-item-object-id' => $post->ID,
  750. 'menu-item-object' => $post->post_type,
  751. 'menu-item-type' => 'post_type',
  752. 'menu-item-status' => 'publish',
  753. );
  754. foreach ( $auto_add as $menu_id ) {
  755. $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
  756. if ( ! is_array( $items ) )
  757. continue;
  758. foreach ( $items as $item ) {
  759. if ( $post->ID == $item->object_id )
  760. continue 2;
  761. }
  762. wp_update_nav_menu_item( $menu_id, 0, $args );
  763. }
  764. }