PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/nav-menu.php

https://github.com/dipakdotyadav/WordPress
PHP | 1320 lines | 973 code | 151 blank | 196 comment | 163 complexity | 497b01b6dda979d366f35009f51c1599 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file