PageRenderTime 342ms CodeModel.GetById 44ms RepoModel.GetById 9ms app.codeStats 1ms

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

https://gitlab.com/math4youbyusgroupillinois/WordPress
PHP | 1324 lines | 921 code | 143 blank | 260 comment | 157 complexity | ef4a379342d48ee8d00fdf9055a45056 MD5 | raw file

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

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