PageRenderTime 143ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/geyson/geyson
PHP | 1359 lines | 923 code | 145 blank | 291 comment | 158 complexity | 4aaf745b4cdd6fe77a565f4ca66c529c MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

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

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