/wp-includes/nav-menu.php

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