PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/nav-menu.php

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