nav-menu.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. <?php
  2. /**
  3. * Core Navigation Menu API
  4. *
  5. * @package WordPress
  6. * @subpackage Nav_Menus
  7. * @since 3.0.0
  8. */
  9. /** Walker_Nav_Menu_Edit class */
  10. require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php' );
  11. /** Walker_Nav_Menu_Checklist class */
  12. require_once( ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php' );
  13. /**
  14. * Prints the appropriate response to a menu quick search.
  15. *
  16. * @since 3.0.0
  17. *
  18. * @param array $request The unsanitized request values.
  19. */
  20. function _wp_ajax_menu_quick_search( $request = array() ) {
  21. $args = array();
  22. $type = isset( $request['type'] ) ? $request['type'] : '';
  23. $object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
  24. $query = isset( $request['q'] ) ? $request['q'] : '';
  25. $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';
  26. if ( 'markup' == $response_format ) {
  27. $args['walker'] = new Walker_Nav_Menu_Checklist;
  28. }
  29. if ( 'get-post-item' == $type ) {
  30. if ( post_type_exists( $object_type ) ) {
  31. if ( isset( $request['ID'] ) ) {
  32. $object_id = (int) $request['ID'];
  33. if ( 'markup' == $response_format ) {
  34. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
  35. } elseif ( 'json' == $response_format ) {
  36. echo wp_json_encode(
  37. array(
  38. 'ID' => $object_id,
  39. 'post_title' => get_the_title( $object_id ),
  40. 'post_type' => get_post_type( $object_id ),
  41. )
  42. );
  43. echo "\n";
  44. }
  45. }
  46. } elseif ( taxonomy_exists( $object_type ) ) {
  47. if ( isset( $request['ID'] ) ) {
  48. $object_id = (int) $request['ID'];
  49. if ( 'markup' == $response_format ) {
  50. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
  51. } elseif ( 'json' == $response_format ) {
  52. $post_obj = get_term( $object_id, $object_type );
  53. echo wp_json_encode(
  54. array(
  55. 'ID' => $object_id,
  56. 'post_title' => $post_obj->name,
  57. 'post_type' => $object_type,
  58. )
  59. );
  60. echo "\n";
  61. }
  62. }
  63. }
  64. } elseif ( preg_match( '/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches ) ) {
  65. if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
  66. $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
  67. $args = array_merge(
  68. $args,
  69. array(
  70. 'no_found_rows' => true,
  71. 'update_post_meta_cache' => false,
  72. 'update_post_term_cache' => false,
  73. 'posts_per_page' => 10,
  74. 'post_type' => $matches[2],
  75. 's' => $query,
  76. )
  77. );
  78. if ( isset( $post_type_obj->_default_query ) ) {
  79. $args = array_merge( $args, (array) $post_type_obj->_default_query );
  80. }
  81. $search_results_query = new WP_Query( $args );
  82. if ( ! $search_results_query->have_posts() ) {
  83. return;
  84. }
  85. while ( $search_results_query->have_posts() ) {
  86. $post = $search_results_query->next_post();
  87. if ( 'markup' == $response_format ) {
  88. $var_by_ref = $post->ID;
  89. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
  90. } elseif ( 'json' == $response_format ) {
  91. echo wp_json_encode(
  92. array(
  93. 'ID' => $post->ID,
  94. 'post_title' => get_the_title( $post->ID ),
  95. 'post_type' => $matches[2],
  96. )
  97. );
  98. echo "\n";
  99. }
  100. }
  101. } elseif ( 'taxonomy' == $matches[1] ) {
  102. $terms = get_terms(
  103. array(
  104. 'taxonomy' => $matches[2],
  105. 'name__like' => $query,
  106. 'number' => 10,
  107. )
  108. );
  109. if ( empty( $terms ) || is_wp_error( $terms ) ) {
  110. return;
  111. }
  112. foreach ( (array) $terms as $term ) {
  113. if ( 'markup' == $response_format ) {
  114. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
  115. } elseif ( 'json' == $response_format ) {
  116. echo wp_json_encode(
  117. array(
  118. 'ID' => $term->term_id,
  119. 'post_title' => $term->name,
  120. 'post_type' => $matches[2],
  121. )
  122. );
  123. echo "\n";
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Register nav menu meta boxes and advanced menu items.
  131. *
  132. * @since 3.0.0
  133. */
  134. function wp_nav_menu_setup() {
  135. // Register meta boxes
  136. wp_nav_menu_post_type_meta_boxes();
  137. add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
  138. wp_nav_menu_taxonomy_meta_boxes();
  139. // Register advanced menu items (columns)
  140. add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
  141. // If first time editing, disable advanced items by default.
  142. if ( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
  143. $user = wp_get_current_user();
  144. update_user_option(
  145. $user->ID,
  146. 'managenav-menuscolumnshidden',
  147. array(
  148. 0 => 'link-target',
  149. 1 => 'css-classes',
  150. 2 => 'xfn',
  151. 3 => 'description',
  152. 4 => 'title-attribute',
  153. ),
  154. true
  155. );
  156. }
  157. }
  158. /**
  159. * Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
  160. *
  161. * @since 3.0.0
  162. *
  163. * @global array $wp_meta_boxes
  164. */
  165. function wp_initial_nav_menu_meta_boxes() {
  166. global $wp_meta_boxes;
  167. if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array( $wp_meta_boxes ) ) {
  168. return;
  169. }
  170. $initial_meta_boxes = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category' );
  171. $hidden_meta_boxes = array();
  172. foreach ( array_keys( $wp_meta_boxes['nav-menus'] ) as $context ) {
  173. foreach ( array_keys( $wp_meta_boxes['nav-menus'][ $context ] ) as $priority ) {
  174. foreach ( $wp_meta_boxes['nav-menus'][ $context ][ $priority ] as $box ) {
  175. if ( in_array( $box['id'], $initial_meta_boxes ) ) {
  176. unset( $box['id'] );
  177. } else {
  178. $hidden_meta_boxes[] = $box['id'];
  179. }
  180. }
  181. }
  182. }
  183. $user = wp_get_current_user();
  184. update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
  185. }
  186. /**
  187. * Creates meta boxes for any post type menu item..
  188. *
  189. * @since 3.0.0
  190. */
  191. function wp_nav_menu_post_type_meta_boxes() {
  192. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
  193. if ( ! $post_types ) {
  194. return;
  195. }
  196. foreach ( $post_types as $post_type ) {
  197. /**
  198. * Filters whether a menu items meta box will be added for the current
  199. * object type.
  200. *
  201. * If a falsey value is returned instead of an object, the menu items
  202. * meta box for the current meta box object will not be added.
  203. *
  204. * @since 3.0.0
  205. *
  206. * @param object $meta_box_object The current object to add a menu items
  207. * meta box for.
  208. */
  209. $post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
  210. if ( $post_type ) {
  211. $id = $post_type->name;
  212. // Give pages a higher priority.
  213. $priority = ( 'page' == $post_type->name ? 'core' : 'default' );
  214. add_meta_box( "add-post-type-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
  215. }
  216. }
  217. }
  218. /**
  219. * Creates meta boxes for any taxonomy menu item.
  220. *
  221. * @since 3.0.0
  222. */
  223. function wp_nav_menu_taxonomy_meta_boxes() {
  224. $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
  225. if ( ! $taxonomies ) {
  226. return;
  227. }
  228. foreach ( $taxonomies as $tax ) {
  229. /** This filter is documented in wp-admin/includes/nav-menu.php */
  230. $tax = apply_filters( 'nav_menu_meta_box_object', $tax );
  231. if ( $tax ) {
  232. $id = $tax->name;
  233. add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
  234. }
  235. }
  236. }
  237. /**
  238. * Check whether to disable the Menu Locations meta box submit button
  239. *
  240. * @since 3.6.0
  241. *
  242. * @global bool $one_theme_location_no_menus to determine if no menus exist
  243. *
  244. * @param int|string $nav_menu_selected_id (id, name or slug) of the currently-selected menu
  245. * @return string Disabled attribute if at least one menu exists, false if not
  246. */
  247. function wp_nav_menu_disabled_check( $nav_menu_selected_id ) {
  248. global $one_theme_location_no_menus;
  249. if ( $one_theme_location_no_menus ) {
  250. return false;
  251. }
  252. return disabled( $nav_menu_selected_id, 0 );
  253. }
  254. /**
  255. * Displays a meta box for the custom links menu item.
  256. *
  257. * @since 3.0.0
  258. *
  259. * @global int $_nav_menu_placeholder
  260. * @global int|string $nav_menu_selected_id
  261. */
  262. function wp_nav_menu_item_link_meta_box() {
  263. global $_nav_menu_placeholder, $nav_menu_selected_id;
  264. $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
  265. ?>
  266. <div class="customlinkdiv" id="customlinkdiv">
  267. <input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
  268. <p id="menu-item-url-wrap" class="wp-clearfix">
  269. <label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label>
  270. <input id="custom-menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" type="text" class="code menu-item-textbox" placeholder="https://" />
  271. </p>
  272. <p id="menu-item-name-wrap" class="wp-clearfix">
  273. <label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label>
  274. <input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text" class="regular-text menu-item-textbox" />
  275. </p>
  276. <p class="button-controls wp-clearfix">
  277. <span class="add-to-menu">
  278. <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
  279. <span class="spinner"></span>
  280. </span>
  281. </p>
  282. </div><!-- /.customlinkdiv -->
  283. <?php
  284. }
  285. /**
  286. * Displays a meta box for a post type menu item.
  287. *
  288. * @since 3.0.0
  289. *
  290. * @global int $_nav_menu_placeholder
  291. * @global int|string $nav_menu_selected_id
  292. *
  293. * @param string $object Not used.
  294. * @param array $box {
  295. * Post type menu item meta box arguments.
  296. *
  297. * @type string $id Meta box 'id' attribute.
  298. * @type string $title Meta box title.
  299. * @type string $callback Meta box display callback.
  300. * @type WP_Post_Type $args Extra meta box arguments (the post type object for this meta box).
  301. * }
  302. */
  303. function wp_nav_menu_item_post_type_meta_box( $object, $box ) {
  304. global $_nav_menu_placeholder, $nav_menu_selected_id;
  305. $post_type_name = $box['args']->name;
  306. // Paginate browsing for large numbers of post objects.
  307. $per_page = 50;
  308. $pagenum = isset( $_REQUEST[ $post_type_name . '-tab' ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
  309. $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
  310. $args = array(
  311. 'offset' => $offset,
  312. 'order' => 'ASC',
  313. 'orderby' => 'title',
  314. 'posts_per_page' => $per_page,
  315. 'post_type' => $post_type_name,
  316. 'suppress_filters' => true,
  317. 'update_post_term_cache' => false,
  318. 'update_post_meta_cache' => false,
  319. );
  320. if ( isset( $box['args']->_default_query ) ) {
  321. $args = array_merge( $args, (array) $box['args']->_default_query );
  322. }
  323. /*
  324. * If we're dealing with pages, let's prioritize the Front Page,
  325. * Posts Page and Privacy Policy Page at the top of the list.
  326. */
  327. $important_pages = array();
  328. if ( 'page' == $post_type_name ) {
  329. $suppress_page_ids = array();
  330. // Insert Front Page or custom Home link.
  331. $front_page = 'page' == get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
  332. $front_page_obj = null;
  333. if ( ! empty( $front_page ) ) {
  334. $front_page_obj = get_post( $front_page );
  335. $front_page_obj->front_or_home = true;
  336. $important_pages[] = $front_page_obj;
  337. $suppress_page_ids[] = $front_page_obj->ID;
  338. } else {
  339. $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval( $_nav_menu_placeholder ) - 1 : -1;
  340. $front_page_obj = (object) array(
  341. 'front_or_home' => true,
  342. 'ID' => 0,
  343. 'object_id' => $_nav_menu_placeholder,
  344. 'post_content' => '',
  345. 'post_excerpt' => '',
  346. 'post_parent' => '',
  347. 'post_title' => _x( 'Home', 'nav menu home label' ),
  348. 'post_type' => 'nav_menu_item',
  349. 'type' => 'custom',
  350. 'url' => home_url( '/' ),
  351. );
  352. $important_pages[] = $front_page_obj;
  353. }
  354. // Insert Posts Page.
  355. $posts_page = 'page' == get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0;
  356. if ( ! empty( $posts_page ) ) {
  357. $posts_page_obj = get_post( $posts_page );
  358. $posts_page_obj->posts_page = true;
  359. $important_pages[] = $posts_page_obj;
  360. $suppress_page_ids[] = $posts_page_obj->ID;
  361. }
  362. // Insert Privacy Policy Page.
  363. $privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
  364. if ( ! empty( $privacy_policy_page_id ) ) {
  365. $privacy_policy_page = get_post( $privacy_policy_page_id );
  366. if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
  367. $privacy_policy_page->privacy_policy_page = true;
  368. $important_pages[] = $privacy_policy_page;
  369. $suppress_page_ids[] = $privacy_policy_page->ID;
  370. }
  371. }
  372. // Add suppression array to arguments for WP_Query.
  373. if ( ! empty( $suppress_page_ids ) ) {
  374. $args['post__not_in'] = $suppress_page_ids;
  375. }
  376. }
  377. // @todo transient caching of these results with proper invalidation on updating of a post of this type
  378. $get_posts = new WP_Query;
  379. $posts = $get_posts->query( $args );
  380. // Only suppress and insert when more than just suppression pages available.
  381. if ( ! $get_posts->post_count ) {
  382. if ( ! empty( $suppress_page_ids ) ) {
  383. unset( $args['post__not_in'] );
  384. $get_posts = new WP_Query;
  385. $posts = $get_posts->query( $args );
  386. } else {
  387. echo '<p>' . __( 'No items.' ) . '</p>';
  388. return;
  389. }
  390. } elseif ( ! empty( $important_pages ) ) {
  391. $posts = array_merge( $important_pages, $posts );
  392. }
  393. $num_pages = $get_posts->max_num_pages;
  394. $page_links = paginate_links(
  395. array(
  396. 'base' => add_query_arg(
  397. array(
  398. $post_type_name . '-tab' => 'all',
  399. 'paged' => '%#%',
  400. 'item-type' => 'post_type',
  401. 'item-object' => $post_type_name,
  402. )
  403. ),
  404. 'format' => '',
  405. 'prev_text' => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '&laquo;' ) . '</span>',
  406. 'next_text' => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '&raquo;' ) . '</span>',
  407. 'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',
  408. 'total' => $num_pages,
  409. 'current' => $pagenum,
  410. )
  411. );
  412. $db_fields = false;
  413. if ( is_post_type_hierarchical( $post_type_name ) ) {
  414. $db_fields = array(
  415. 'parent' => 'post_parent',
  416. 'id' => 'ID',
  417. );
  418. }
  419. $walker = new Walker_Nav_Menu_Checklist( $db_fields );
  420. $current_tab = 'most-recent';
  421. if ( isset( $_REQUEST[ $post_type_name . '-tab' ] ) && in_array( $_REQUEST[ $post_type_name . '-tab' ], array( 'all', 'search' ) ) ) {
  422. $current_tab = $_REQUEST[ $post_type_name . '-tab' ];
  423. }
  424. if ( ! empty( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) {
  425. $current_tab = 'search';
  426. }
  427. $removed_args = array(
  428. 'action',
  429. 'customlink-tab',
  430. 'edit-menu-item',
  431. 'menu-item',
  432. 'page-tab',
  433. '_wpnonce',
  434. );
  435. $most_recent_url = '';
  436. $view_all_url = '';
  437. $search_url = '';
  438. if ( $nav_menu_selected_id ) {
  439. $most_recent_url = esc_url( add_query_arg( $post_type_name . '-tab', 'most-recent', remove_query_arg( $removed_args ) ) );
  440. $view_all_url = esc_url( add_query_arg( $post_type_name . '-tab', 'all', remove_query_arg( $removed_args ) ) );
  441. $search_url = esc_url( add_query_arg( $post_type_name . '-tab', 'search', remove_query_arg( $removed_args ) ) );
  442. }
  443. ?>
  444. <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
  445. <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
  446. <li <?php echo ( 'most-recent' == $current_tab ? ' class="tabs"' : '' ); ?>>
  447. <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-most-recent" href="<?php echo $most_recent_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent">
  448. <?php _e( 'Most Recent' ); ?>
  449. </a>
  450. </li>
  451. <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
  452. <a class="nav-tab-link" data-type="<?php echo esc_attr( $post_type_name ); ?>-all" href="<?php echo $view_all_url; ?>#<?php echo $post_type_name; ?>-all">
  453. <?php _e( 'View All' ); ?>
  454. </a>
  455. </li>
  456. <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
  457. <a class="nav-tab-link" data-type="tabs-panel-posttype-<?php echo esc_attr( $post_type_name ); ?>-search" href="<?php echo $search_url; ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
  458. <?php _e( 'Search' ); ?>
  459. </a>
  460. </li>
  461. </ul><!-- .posttype-tabs -->
  462. <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php echo ( 'most-recent' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
  463. <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
  464. <?php
  465. $recent_args = array_merge(
  466. $args,
  467. array(
  468. 'orderby' => 'post_date',
  469. 'order' => 'DESC',
  470. 'posts_per_page' => 15,
  471. )
  472. );
  473. $most_recent = $get_posts->query( $recent_args );
  474. $args['walker'] = $walker;
  475. /**
  476. * Filters the posts displayed in the 'Most Recent' tab of the current
  477. * post type's menu items meta box.
  478. *
  479. * The dynamic portion of the hook name, `$post_type_name`, refers to the post type name.
  480. *
  481. * @since 4.3.0
  482. * @since 4.9.0 Added the `$recent_args` parameter.
  483. *
  484. * @param WP_Post[] $most_recent An array of post objects being listed.
  485. * @param array $args An array of `WP_Query` arguments for the meta box.
  486. * @param array $box Arguments passed to `wp_nav_menu_item_post_type_meta_box()`.
  487. * @param array $recent_args An array of `WP_Query` arguments for 'Most Recent' tab.
  488. */
  489. $most_recent = apply_filters( "nav_menu_items_{$post_type_name}_recent", $most_recent, $args, $box, $recent_args );
  490. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $most_recent ), 0, (object) $args );
  491. ?>
  492. </ul>
  493. </div><!-- /.tabs-panel -->
  494. <div class="tabs-panel <?php echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
  495. <?php
  496. if ( isset( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) {
  497. $searched = esc_attr( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] );
  498. $search_results = get_posts(
  499. array(
  500. 's' => $searched,
  501. 'post_type' => $post_type_name,
  502. 'fields' => 'all',
  503. 'order' => 'DESC',
  504. )
  505. );
  506. } else {
  507. $searched = '';
  508. $search_results = array();
  509. }
  510. ?>
  511. <p class="quick-search-wrap">
  512. <label for="quick-search-posttype-<?php echo $post_type_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
  513. <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" id="quick-search-posttype-<?php echo $post_type_name; ?>" />
  514. <span class="spinner"></span>
  515. <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-posttype-' . $post_type_name ) ); ?>
  516. </p>
  517. <ul id="<?php echo $post_type_name; ?>-search-checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear">
  518. <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
  519. <?php
  520. $args['walker'] = $walker;
  521. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $search_results ), 0, (object) $args );
  522. ?>
  523. <?php elseif ( is_wp_error( $search_results ) ) : ?>
  524. <li><?php echo $search_results->get_error_message(); ?></li>
  525. <?php elseif ( ! empty( $searched ) ) : ?>
  526. <li><?php _e( 'No results found.' ); ?></li>
  527. <?php endif; ?>
  528. </ul>
  529. </div><!-- /.tabs-panel -->
  530. <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>">
  531. <?php if ( ! empty( $page_links ) ) : ?>
  532. <div class="add-menu-item-pagelinks">
  533. <?php echo $page_links; ?>
  534. </div>
  535. <?php endif; ?>
  536. <ul id="<?php echo $post_type_name; ?>checklist" data-wp-lists="list:<?php echo $post_type_name; ?>" class="categorychecklist form-no-clear">
  537. <?php
  538. $args['walker'] = $walker;
  539. $post_type = get_post_type_object( $post_type_name );
  540. if ( $post_type->has_archive ) {
  541. $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval( $_nav_menu_placeholder ) - 1 : -1;
  542. array_unshift(
  543. $posts,
  544. (object) array(
  545. 'ID' => 0,
  546. 'object_id' => $_nav_menu_placeholder,
  547. 'object' => $post_type_name,
  548. 'post_content' => '',
  549. 'post_excerpt' => '',
  550. 'post_title' => $post_type->labels->archives,
  551. 'post_type' => 'nav_menu_item',
  552. 'type' => 'post_type_archive',
  553. 'url' => get_post_type_archive_link( $post_type_name ),
  554. )
  555. );
  556. }
  557. /**
  558. * Filters the posts displayed in the 'View All' tab of the current
  559. * post type's menu items meta box.
  560. *
  561. * The dynamic portion of the hook name, `$post_type_name`, refers
  562. * to the slug of the current post type.
  563. *
  564. * @since 3.2.0
  565. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
  566. *
  567. * @see WP_Query::query()
  568. *
  569. * @param object[] $posts The posts for the current post type. Mostly `WP_Post` objects, but
  570. * can also contain "fake" post objects to represent other menu items.
  571. * @param array $args An array of `WP_Query` arguments.
  572. * @param WP_Post_Type $post_type The current post type object for this menu item meta box.
  573. */
  574. $posts = apply_filters( "nav_menu_items_{$post_type_name}", $posts, $args, $post_type );
  575. $checkbox_items = walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $posts ), 0, (object) $args );
  576. echo $checkbox_items;
  577. ?>
  578. </ul>
  579. <?php if ( ! empty( $page_links ) ) : ?>
  580. <div class="add-menu-item-pagelinks">
  581. <?php echo $page_links; ?>
  582. </div>
  583. <?php endif; ?>
  584. </div><!-- /.tabs-panel -->
  585. <p class="button-controls wp-clearfix" data-items-type="posttype-<?php echo esc_attr( $post_type_name ); ?>">
  586. <span class="list-controls hide-if-no-js">
  587. <input type="checkbox" id="<?php echo esc_attr( $post_type_name . '-tab' ); ?>" class="select-all" />
  588. <label for="<?php echo esc_attr( $post_type_name . '-tab' ); ?>"><?php _e( 'Select All' ); ?></label>
  589. </span>
  590. <span class="add-to-menu">
  591. <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-post-type-menu-item" id="<?php echo esc_attr( 'submit-posttype-' . $post_type_name ); ?>" />
  592. <span class="spinner"></span>
  593. </span>
  594. </p>
  595. </div><!-- /.posttypediv -->
  596. <?php
  597. }
  598. /**
  599. * Displays a meta box for a taxonomy menu item.
  600. *
  601. * @since 3.0.0
  602. *
  603. * @global int|string $nav_menu_selected_id
  604. *
  605. * @param string $object Not used.
  606. * @param array $box {
  607. * Taxonomy menu item meta box arguments.
  608. *
  609. * @type string $id Meta box 'id' attribute.
  610. * @type string $title Meta box title.
  611. * @type string $callback Meta box display callback.
  612. * @type object $args Extra meta box arguments (the taxonomy object for this meta box).
  613. * }
  614. */
  615. function wp_nav_menu_item_taxonomy_meta_box( $object, $box ) {
  616. global $nav_menu_selected_id;
  617. $taxonomy_name = $box['args']->name;
  618. $taxonomy = get_taxonomy( $taxonomy_name );
  619. // Paginate browsing for large numbers of objects.
  620. $per_page = 50;
  621. $pagenum = isset( $_REQUEST[ $taxonomy_name . '-tab' ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
  622. $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
  623. $args = array(
  624. 'taxonomy' => $taxonomy_name,
  625. 'child_of' => 0,
  626. 'exclude' => '',
  627. 'hide_empty' => false,
  628. 'hierarchical' => 1,
  629. 'include' => '',
  630. 'number' => $per_page,
  631. 'offset' => $offset,
  632. 'order' => 'ASC',
  633. 'orderby' => 'name',
  634. 'pad_counts' => false,
  635. );
  636. $terms = get_terms( $args );
  637. if ( ! $terms || is_wp_error( $terms ) ) {
  638. echo '<p>' . __( 'No items.' ) . '</p>';
  639. return;
  640. }
  641. $num_pages = ceil(
  642. wp_count_terms(
  643. $taxonomy_name,
  644. array_merge(
  645. $args,
  646. array(
  647. 'number' => '',
  648. 'offset' => '',
  649. )
  650. )
  651. ) / $per_page
  652. );
  653. $page_links = paginate_links(
  654. array(
  655. 'base' => add_query_arg(
  656. array(
  657. $taxonomy_name . '-tab' => 'all',
  658. 'paged' => '%#%',
  659. 'item-type' => 'taxonomy',
  660. 'item-object' => $taxonomy_name,
  661. )
  662. ),
  663. 'format' => '',
  664. 'prev_text' => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '&laquo;' ) . '</span>',
  665. 'next_text' => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '&raquo;' ) . '</span>',
  666. 'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',
  667. 'total' => $num_pages,
  668. 'current' => $pagenum,
  669. )
  670. );
  671. $db_fields = false;
  672. if ( is_taxonomy_hierarchical( $taxonomy_name ) ) {
  673. $db_fields = array(
  674. 'parent' => 'parent',
  675. 'id' => 'term_id',
  676. );
  677. }
  678. $walker = new Walker_Nav_Menu_Checklist( $db_fields );
  679. $current_tab = 'most-used';
  680. if ( isset( $_REQUEST[ $taxonomy_name . '-tab' ] ) && in_array( $_REQUEST[ $taxonomy_name . '-tab' ], array( 'all', 'most-used', 'search' ) ) ) {
  681. $current_tab = $_REQUEST[ $taxonomy_name . '-tab' ];
  682. }
  683. if ( ! empty( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] ) ) {
  684. $current_tab = 'search';
  685. }
  686. $removed_args = array(
  687. 'action',
  688. 'customlink-tab',
  689. 'edit-menu-item',
  690. 'menu-item',
  691. 'page-tab',
  692. '_wpnonce',
  693. );
  694. $most_used_url = '';
  695. $view_all_url = '';
  696. $search_url = '';
  697. if ( $nav_menu_selected_id ) {
  698. $most_used_url = esc_url( add_query_arg( $taxonomy_name . '-tab', 'most-used', remove_query_arg( $removed_args ) ) );
  699. $view_all_url = esc_url( add_query_arg( $taxonomy_name . '-tab', 'all', remove_query_arg( $removed_args ) ) );
  700. $search_url = esc_url( add_query_arg( $taxonomy_name . '-tab', 'search', remove_query_arg( $removed_args ) ) );
  701. }
  702. ?>
  703. <div id="taxonomy-<?php echo $taxonomy_name; ?>" class="taxonomydiv">
  704. <ul id="taxonomy-<?php echo $taxonomy_name; ?>-tabs" class="taxonomy-tabs add-menu-item-tabs">
  705. <li <?php echo ( 'most-used' == $current_tab ? ' class="tabs"' : '' ); ?>>
  706. <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-pop" href="<?php echo $most_used_url; ?>#tabs-panel-<?php echo $taxonomy_name; ?>-pop">
  707. <?php echo esc_html( $taxonomy->labels->most_used ); ?>
  708. </a>
  709. </li>
  710. <li <?php echo ( 'all' == $current_tab ? ' class="tabs"' : '' ); ?>>
  711. <a class="nav-tab-link" data-type="tabs-panel-<?php echo esc_attr( $taxonomy_name ); ?>-all" href="<?php echo $view_all_url; ?>#tabs-panel-<?php echo $taxonomy_name; ?>-all">
  712. <?php _e( 'View All' ); ?>
  713. </a>
  714. </li>
  715. <li <?php echo ( 'search' == $current_tab ? ' class="tabs"' : '' ); ?>>
  716. <a class="nav-tab-link" data-type="tabs-panel-search-taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>" href="<?php echo $search_url; ?>#tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
  717. <?php _e( 'Search' ); ?>
  718. </a>
  719. </li>
  720. </ul><!-- .taxonomy-tabs -->
  721. <div id="tabs-panel-<?php echo $taxonomy_name; ?>-pop" class="tabs-panel <?php echo ( 'most-used' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?> ">
  722. <ul id="<?php echo $taxonomy_name; ?>checklist-pop" class="categorychecklist form-no-clear" >
  723. <?php
  724. $popular_terms = get_terms(
  725. array(
  726. 'taxonomy' => $taxonomy_name,
  727. 'orderby' => 'count',
  728. 'order' => 'DESC',
  729. 'number' => 10,
  730. 'hierarchical' => false,
  731. )
  732. );
  733. $args['walker'] = $walker;
  734. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $popular_terms ), 0, (object) $args );
  735. ?>
  736. </ul>
  737. </div><!-- /.tabs-panel -->
  738. <div id="tabs-panel-<?php echo $taxonomy_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php echo ( 'all' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?> ">
  739. <?php if ( ! empty( $page_links ) ) : ?>
  740. <div class="add-menu-item-pagelinks">
  741. <?php echo $page_links; ?>
  742. </div>
  743. <?php endif; ?>
  744. <ul id="<?php echo $taxonomy_name; ?>checklist" data-wp-lists="list:<?php echo $taxonomy_name; ?>" class="categorychecklist form-no-clear">
  745. <?php
  746. $args['walker'] = $walker;
  747. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $terms ), 0, (object) $args );
  748. ?>
  749. </ul>
  750. <?php if ( ! empty( $page_links ) ) : ?>
  751. <div class="add-menu-item-pagelinks">
  752. <?php echo $page_links; ?>
  753. </div>
  754. <?php endif; ?>
  755. </div><!-- /.tabs-panel -->
  756. <div class="tabs-panel <?php echo ( 'search' == $current_tab ? 'tabs-panel-active' : 'tabs-panel-inactive' ); ?>" id="tabs-panel-search-taxonomy-<?php echo $taxonomy_name; ?>">
  757. <?php
  758. if ( isset( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] ) ) {
  759. $searched = esc_attr( $_REQUEST[ 'quick-search-taxonomy-' . $taxonomy_name ] );
  760. $search_results = get_terms(
  761. array(
  762. 'taxonomy' => $taxonomy_name,
  763. 'name__like' => $searched,
  764. 'fields' => 'all',
  765. 'orderby' => 'count',
  766. 'order' => 'DESC',
  767. 'hierarchical' => false,
  768. )
  769. );
  770. } else {
  771. $searched = '';
  772. $search_results = array();
  773. }
  774. ?>
  775. <p class="quick-search-wrap">
  776. <label for="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" class="screen-reader-text"><?php _e( 'Search' ); ?></label>
  777. <input type="search" class="quick-search" value="<?php echo $searched; ?>" name="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" id="quick-search-taxonomy-<?php echo $taxonomy_name; ?>" />
  778. <span class="spinner"></span>
  779. <?php submit_button( __( 'Search' ), 'small quick-search-submit hide-if-js', 'submit', false, array( 'id' => 'submit-quick-search-taxonomy-' . $taxonomy_name ) ); ?>
  780. </p>
  781. <ul id="<?php echo $taxonomy_name; ?>-search-checklist" data-wp-lists="list:<?php echo $taxonomy_name; ?>" class="categorychecklist form-no-clear">
  782. <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
  783. <?php
  784. $args['walker'] = $walker;
  785. echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $search_results ), 0, (object) $args );
  786. ?>
  787. <?php elseif ( is_wp_error( $search_results ) ) : ?>
  788. <li><?php echo $search_results->get_error_message(); ?></li>
  789. <?php elseif ( ! empty( $searched ) ) : ?>
  790. <li><?php _e( 'No results found.' ); ?></li>
  791. <?php endif; ?>
  792. </ul>
  793. </div><!-- /.tabs-panel -->
  794. <p class="button-controls wp-clearfix" data-items-type="taxonomy-<?php echo esc_attr( $taxonomy_name ); ?>">
  795. <span class="list-controls hide-if-no-js">
  796. <input type="checkbox" id="<?php echo esc_attr( $taxonomy_name . '-tab' ); ?>" class="select-all" />
  797. <label for="<?php echo esc_attr( $taxonomy_name . '-tab' ); ?>"><?php _e( 'Select All' ); ?></label>
  798. </span>
  799. <span class="add-to-menu">
  800. <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-taxonomy-menu-item" id="<?php echo esc_attr( 'submit-taxonomy-' . $taxonomy_name ); ?>" />
  801. <span class="spinner"></span>
  802. </span>
  803. </p>
  804. </div><!-- /.taxonomydiv -->
  805. <?php
  806. }
  807. /**
  808. * Save posted nav menu item data.
  809. *
  810. * @since 3.0.0
  811. *
  812. * @param int $menu_id The menu ID for which to save this item. Value of 0 makes a draft, orphaned menu item. Default 0.
  813. * @param array[] $menu_data The unsanitized POSTed menu item data.
  814. * @return int[] The database IDs of the items saved
  815. */
  816. function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) {
  817. $menu_id = (int) $menu_id;
  818. $items_saved = array();
  819. if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) {
  820. // Loop through all the menu items' POST values.
  821. foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) {
  822. if (
  823. // Checkbox is not checked.
  824. empty( $_item_object_data['menu-item-object-id'] ) &&
  825. (
  826. // And item type either isn't set.
  827. ! isset( $_item_object_data['menu-item-type'] ) ||
  828. // Or URL is the default.
  829. in_array( $_item_object_data['menu-item-url'], array( 'https://', 'http://', '' ) ) ||
  830. ! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page)
  831. // Or it *is* a custom menu item that already exists.
  832. ! empty( $_item_object_data['menu-item-db-id'] )
  833. )
  834. ) {
  835. // Then this potential menu item is not getting added to this menu.
  836. continue;
  837. }
  838. // If this possible menu item doesn't actually have a menu database ID yet.
  839. if (
  840. empty( $_item_object_data['menu-item-db-id'] ) ||
  841. ( 0 > $_possible_db_id ) ||
  842. $_possible_db_id != $_item_object_data['menu-item-db-id']
  843. ) {
  844. $_actual_db_id = 0;
  845. } else {
  846. $_actual_db_id = (int) $_item_object_data['menu-item-db-id'];
  847. }
  848. $args = array(
  849. 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),
  850. 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),
  851. 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),
  852. 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),
  853. 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),
  854. 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),
  855. 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),
  856. 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),
  857. 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),
  858. 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),
  859. 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),
  860. 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),
  861. 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),
  862. );
  863. $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args );
  864. }
  865. }
  866. return $items_saved;
  867. }
  868. /**
  869. * Adds custom arguments to some of the meta box object types.
  870. *
  871. * @since 3.0.0
  872. *
  873. * @access private
  874. *
  875. * @param object $object The post type or taxonomy meta-object.
  876. * @return object The post type of taxonomy object.
  877. */
  878. function _wp_nav_menu_meta_box_object( $object = null ) {
  879. if ( isset( $object->name ) ) {
  880. if ( 'page' == $object->name ) {
  881. $object->_default_query = array(
  882. 'orderby' => 'menu_order title',
  883. 'post_status' => 'publish',
  884. );
  885. // Posts should show only published items.
  886. } elseif ( 'post' == $object->name ) {
  887. $object->_default_query = array(
  888. 'post_status' => 'publish',
  889. );
  890. // Categories should be in reverse chronological order.
  891. } elseif ( 'category' == $object->name ) {
  892. $object->_default_query = array(
  893. 'orderby' => 'id',
  894. 'order' => 'DESC',
  895. );
  896. // Custom post types should show only published items.
  897. } else {
  898. $object->_default_query = array(
  899. 'post_status' => 'publish',
  900. );
  901. }
  902. }
  903. return $object;
  904. }
  905. /**
  906. * Returns the menu formatted to edit.
  907. *
  908. * @since 3.0.0
  909. *
  910. * @param int $menu_id Optional. The ID of the menu to format. Default 0.
  911. * @return string|WP_Error $output The menu formatted to edit or error object on failure.
  912. */
  913. function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
  914. $menu = wp_get_nav_menu_object( $menu_id );
  915. // If the menu exists, get its items.
  916. if ( is_nav_menu( $menu ) ) {
  917. $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_status' => 'any' ) );
  918. $result = '<div id="menu-instructions" class="post-body-plain';
  919. $result .= ( ! empty( $menu_items ) ) ? ' menu-instructions-inactive">' : '">';
  920. $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
  921. $result .= '</div>';
  922. if ( empty( $menu_items ) ) {
  923. return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';
  924. }
  925. /**
  926. * Filters the Walker class used when adding nav menu items.
  927. *
  928. * @since 3.0.0
  929. *
  930. * @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
  931. * @param int $menu_id ID of the menu being rendered.
  932. */
  933. $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );
  934. if ( class_exists( $walker_class_name ) ) {
  935. $walker = new $walker_class_name;
  936. } else {
  937. return new WP_Error(
  938. 'menu_walker_not_exist',
  939. sprintf(
  940. /* translators: %s: Walker class name. */
  941. __( 'The Walker class named %s does not exist.' ),
  942. '<strong>' . $walker_class_name . '</strong>'
  943. )
  944. );
  945. }
  946. $some_pending_menu_items = false;
  947. $some_invalid_menu_items = false;
  948. foreach ( (array) $menu_items as $menu_item ) {
  949. if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status ) {
  950. $some_pending_menu_items = true;
  951. }
  952. if ( ! empty( $menu_item->_invalid ) ) {
  953. $some_invalid_menu_items = true;
  954. }
  955. }
  956. if ( $some_pending_menu_items ) {
  957. $result .= '<div class="notice notice-info notice-alt inline"><p>' . __( 'Click Save Menu to make pending menu items public.' ) . '</p></div>';
  958. }
  959. if ( $some_invalid_menu_items ) {
  960. $result .= '<div class="notice notice-error notice-alt inline"><p>' . __( 'There are some invalid menu items. Please check or delete them.' ) . '</p></div>';
  961. }
  962. $result .= '<ul class="menu" id="menu-to-edit"> ';
  963. $result .= walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $menu_items ), 0, (object) array( 'walker' => $walker ) );
  964. $result .= ' </ul> ';
  965. return $result;
  966. } elseif ( is_wp_error( $menu ) ) {
  967. return $menu;
  968. }
  969. }
  970. /**
  971. * Returns the columns for the nav menus page.
  972. *
  973. * @since 3.0.0
  974. *
  975. * @return array Columns.
  976. */
  977. function wp_nav_menu_manage_columns() {
  978. return array(
  979. '_title' => __( 'Show advanced menu properties' ),
  980. 'cb' => '<input type="checkbox" />',
  981. 'link-target' => __( 'Link Target' ),
  982. 'title-attribute' => __( 'Title Attribute' ),
  983. 'css-classes' => __( 'CSS Classes' ),
  984. 'xfn' => __( 'Link Relationship (XFN)' ),
  985. 'description' => __( 'Description' ),
  986. );
  987. }
  988. /**
  989. * Deletes orphaned draft menu items
  990. *
  991. * @access private
  992. * @since 3.0.0
  993. *
  994. * @global wpdb $wpdb WordPress database abstraction object.
  995. */
  996. function _wp_delete_orphaned_draft_menu_items() {
  997. global $wpdb;
  998. $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
  999. // Delete orphaned draft menu items.
  1000. $menu_items_to_delete = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < %d", $delete_timestamp ) );
  1001. foreach ( (array) $menu_items_to_delete as $menu_item_id ) {
  1002. wp_delete_post( $menu_item_id, true );
  1003. }
  1004. }
  1005. /**
  1006. * Saves nav menu items
  1007. *
  1008. * @since 3.6.0
  1009. *
  1010. * @param int|string $nav_menu_selected_id (id, slug, or name ) of the currently-selected menu
  1011. * @param string $nav_menu_selected_title Title of the currently-selected menu
  1012. * @return array $messages The menu updated message
  1013. */
  1014. function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) {
  1015. $unsorted_menu_items = wp_get_nav_menu_items(
  1016. $nav_menu_selected_id,
  1017. array(
  1018. 'orderby' => 'ID',
  1019. 'output' => ARRAY_A,
  1020. 'output_key' => 'ID',
  1021. 'post_status' => 'draft,publish',
  1022. )
  1023. );
  1024. $messages = array();
  1025. $menu_items = array();
  1026. // Index menu items by db ID
  1027. foreach ( $unsorted_menu_items as $_item ) {
  1028. $menu_items[ $_item->db_id ] = $_item;
  1029. }
  1030. $post_fields = array(
  1031. 'menu-item-db-id',
  1032. 'menu-item-object-id',
  1033. 'menu-item-object',
  1034. 'menu-item-parent-id',
  1035. 'menu-item-position',
  1036. 'menu-item-type',
  1037. 'menu-item-title',
  1038. 'menu-item-url',
  1039. 'menu-item-description',
  1040. 'menu-item-attr-title',
  1041. 'menu-item-target',
  1042. 'menu-item-classes',
  1043. 'menu-item-xfn',
  1044. );
  1045. wp_defer_term_counting( true );
  1046. // Loop through all the menu items' POST variables
  1047. if ( ! empty( $_POST['menu-item-db-id'] ) ) {
  1048. foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) {
  1049. // Menu item title can't be blank
  1050. if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] ) {
  1051. continue;
  1052. }
  1053. $args = array();
  1054. foreach ( $post_fields as $field ) {
  1055. $args[ $field ] = isset( $_POST[ $field ][ $_key ] ) ? $_POST[ $field ][ $_key ] : '';
  1056. }
  1057. $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][ $_key ] != $_key ? 0 : $_key ), $args );
  1058. if ( is_wp_error( $menu_item_db_id ) ) {
  1059. $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
  1060. } else {
  1061. unset( $menu_items[ $menu_item_db_id ] );
  1062. }
  1063. }
  1064. }
  1065. // Remove menu items from the menu that weren't in $_POST
  1066. if ( ! empty( $menu_items ) ) {
  1067. foreach ( array_keys( $menu_items ) as $menu_item_id ) {
  1068. if ( is_nav_menu_item( $menu_item_id ) ) {
  1069. wp_delete_post( $menu_item_id );
  1070. }
  1071. }
  1072. }
  1073. // Store 'auto-add' pages.
  1074. $auto_add = ! empty( $_POST['auto-add-pages'] );
  1075. $nav_menu_option = (array) get_option( 'nav_menu_options' );
  1076. if ( ! isset( $nav_menu_option['auto_add'] ) ) {
  1077. $nav_menu_option['auto_add'] = array();
  1078. }
  1079. if ( $auto_add ) {
  1080. if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) {
  1081. $nav_menu_option['auto_add'][] = $nav_menu_selected_id;
  1082. }
  1083. } else {
  1084. $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] );
  1085. if ( false !== $key ) {
  1086. unset( $nav_menu_option['auto_add'][ $key ] );
  1087. }
  1088. }
  1089. // Remove nonexistent/deleted menus
  1090. $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) );
  1091. update_option( 'nav_menu_options', $nav_menu_option );
  1092. wp_defer_term_counting( false );
  1093. /** This action is documented in wp-includes/nav-menu.php */
  1094. do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
  1095. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' .
  1096. sprintf(
  1097. /* translators: %s: Nav menu title. */
  1098. __( '%s has been updated.' ),
  1099. '<strong>' . $nav_menu_selected_title . '</strong>'
  1100. ) . '</p></div>';
  1101. unset( $menu_items, $unsorted_menu_items );
  1102. return $messages;
  1103. }
  1104. /**
  1105. * If a JSON blob of navigation menu data is in POST data, expand it and inject
  1106. * it into `$_POST` to avoid PHP `max_input_vars` limitations. See #14134.
  1107. *
  1108. * @ignore
  1109. * @since 4.5.3
  1110. * @access private
  1111. */
  1112. function _wp_expand_nav_menu_post_data() {
  1113. if ( ! isset( $_POST['nav-menu-data'] ) ) {
  1114. return;
  1115. }
  1116. $data = json_decode( stripslashes( $_POST['nav-menu-data'] ) );
  1117. if ( ! is_null( $data ) && $data ) {
  1118. foreach ( $data as $post_input_data ) {
  1119. // For input names that are arrays (e.g. `menu-item-db-id[3][4][5]`),
  1120. // derive the array path keys via regex and set the value in $_POST.
  1121. preg_match( '#([^\[]*)(\[(.+)\])?#', $post_input_data->name, $matches );
  1122. $array_bits = array( $matches[1] );
  1123. if ( isset( $matches[3] ) ) {
  1124. $array_bits = array_merge( $array_bits, explode( '][', $matches[3] ) );
  1125. }
  1126. $new_post_data = array();
  1127. // Build the new array value from leaf to trunk.
  1128. for ( $i = count( $array_bits ) - 1; $i >= 0; $i -- ) {
  1129. if ( $i == count( $array_bits ) - 1 ) {
  1130. $new_post_data[ $array_bits[ $i ] ] = wp_slash( $post_input_data->value );
  1131. } else {
  1132. $new_post_data = array( $array_bits[ $i ] => $new_post_data );
  1133. }
  1134. }
  1135. $_POST = array_replace_recursive( $_POST, $new_post_data );
  1136. }
  1137. }
  1138. }