Ticket #13579: pending-menu-items.13579.2.diff
File pending-menu-items.13579.2.diff, 10.9 KB (added by , 14 years ago) |
---|
-
wp-includes/nav-menu.php
247 247 * 248 248 * @since 3.0.0 249 249 * 250 * @param int $menu_id The ID of the menu. Required. 250 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. 251 251 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. 252 252 * @param array $menu_item_data The menu item's data. 253 253 * @return int The menu item's database ID or WP_Error object on failure. … … 257 257 $menu_item_db_id = (int) $menu_item_db_id; 258 258 259 259 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects 260 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) {260 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) 261 261 return new WP_Error('update_nav_menu_item_failed', __('The given object ID is not that of a menu item.')); 262 }263 262 264 263 $menu = wp_get_nav_menu_object( $menu_id ); 265 264 266 if ( ! $menu || is_wp_error( $menu ) ) {265 if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) ) 267 266 return $menu; 268 }269 267 270 $menu_items = (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );268 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); 271 269 272 270 $count = count( $menu_items ); 273 271 … … 289 287 ); 290 288 291 289 $args = wp_parse_args( $menu_item_data, $defaults ); 292 293 if ( 0 == (int) $args['menu-item-position'] ) { 290 291 if ( 0 == $menu_id ) { 292 $args['menu-item-position'] = 1; 293 } elseif ( 0 == (int) $args['menu-item-position'] ) { 294 294 $last_item = array_pop( $menu_items ); 295 295 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : $count; 296 296 } … … 339 339 'post_parent' => $original_parent, 340 340 'post_title' => $args['menu-item-title'], 341 341 'post_type' => 'nav_menu_item', 342 'tax_input' => array( 'nav_menu' => array( intval( $menu->term_id ) ) ),343 342 ); 344 343 344 if ( 0 != $menu_id ) 345 $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) ); 346 345 347 // New menu item. Default is draft status 346 348 if ( 0 == $menu_item_db_id ) { 347 349 $post['ID'] = 0; … … 374 376 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) ); 375 377 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] ); 376 378 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); 377 378 // @todo: only save custom link urls.379 379 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) ); 380 381 if ( 0 == $menu_id ) 382 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', time() ); 383 else 384 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' ); 380 385 381 386 do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args ); 382 387 } … … 740 745 } 741 746 } 742 747 748 /** 749 * Automatically add newly published page objects to menus with that as an option. 750 * 751 * @since 3.0.0 752 * @access private 753 * 754 * @param string $new_status The new status of the post object. 755 * @param string $old_status The old status of the post object. 756 * @param object $post The post object being transitioned from one status to another. 757 * @return void 758 */ 743 759 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { 744 760 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) 745 761 return; -
wp-admin/admin-ajax.php
816 816 817 817 require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 818 818 819 $menu_id = (int) $_POST['menu']; 820 if ( isset( $_POST['menu-item'] ) ) { 821 $item_ids = wp_save_nav_menu_items( $menu_id, $_POST['menu-item'] ); 822 if ( is_wp_error( $item_ids ) ) 823 die('-1'); 824 } else { 825 $item_ids = array(); 826 } 819 $item_ids = wp_save_nav_menu_items( 0, $_POST['menu-item'] ); 820 if ( is_wp_error( $item_ids ) ) 821 die('-1'); 827 822 828 823 foreach ( (array) $item_ids as $menu_item_id ) { 829 824 $menu_obj = get_post( $menu_item_id ); -
wp-admin/includes/nav-menu.php
58 58 $original_object = get_post( $item->object_id ); 59 59 $original_title = $original_object->post_title; 60 60 } 61 62 $classes = array( 63 'menu-item menu-item-depth-' . $depth, 64 'menu-item-' . esc_attr( $item->object ), 65 'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'), 66 ); 67 68 $title = $item->title; 69 70 if ( isset( $item->post_status ) && 'draft' == $item->post_status ) { 71 $classes[] = 'pending'; 72 /* translators: %s: title of menu item in draft status */ 73 $title = sprintf( __('%s (Pending)'), $item->title ); 74 } 75 61 76 ?> 62 <li id="menu-item-<?php echo $item_id; ?>" class=" menu-item menu-item-depth-<?php echo $depth; ?> menu-item-<?php echo esc_attr( $item->object ); ?> menu-item-edit-<?php echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'; ?>">77 <li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>"> 63 78 <dl class="menu-item-bar"> 64 79 <dt class="menu-item-handle"> 65 <span class="item-title"><?php echo esc_html( $ item->title ); ?></span>80 <span class="item-title"><?php echo esc_html( $title ); ?></span> 66 81 <span class="item-controls"> 67 82 <span class="item-type"><?php echo esc_html( $item->type_label ); ?></span> 68 83 <span class="item-order"> … … 871 886 * 872 887 * @since 3.0.0 873 888 * 874 * @param int $menu_id The menu ID for which to save this item. 889 * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item. 875 890 * @param array $menu_data The unsanitized posted menu item data. 876 891 * @return array The database IDs of the items saved 877 892 */ … … 879 894 $menu_id = (int) $menu_id; 880 895 $items_saved = array(); 881 896 882 if ( is_nav_menu( $menu_id ) ) {897 if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) { 883 898 884 899 // Loop through all the menu items' POST values 885 900 foreach( (array) $menu_data as $_possible_db_id => $_item_object_data ) { … … 888 903 ( 889 904 ! isset( $_item_object_data['menu-item-type'] ) || // and item type either isn't set 890 905 in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || // or URL is the default 891 'custom' != $_item_object_data['menu-item-type'] || // or it's not a custom menu item906 ! ( '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) 892 907 ! empty( $_item_object_data['menu-item-db-id'] ) // or it *is* a custom menu item that already exists 893 908 ) 894 909 ) { … … 1004 1019 else 1005 1020 return new WP_Error( 'menu_walker_not_exist', sprintf( __('The Walker class named <strong>%s</strong> does not exist.'), $walker_class_name ) ); 1006 1021 1022 $some_pending_menu_items = false; 1023 foreach( (array) $menu_items as $menu_item ) { 1024 if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status ) 1025 $some_pending_menu_items = true; 1026 } 1027 1028 if ( $some_pending_menu_items ) 1029 $result .= '<div class="updated">' . __('Click <em>Save Menu</em> to make pending menu items public.') . '</div>'; 1030 1007 1031 $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) ); 1008 1032 return $result; 1009 1033 } elseif ( is_wp_error( $menu ) ) { … … 1032 1056 ); 1033 1057 } 1034 1058 1059 /** 1060 * Deletes orphaned draft menu items 1061 * 1062 * @access private 1063 * @since 3.0.0 1064 * 1065 */ 1066 function _wp_delete_orphaned_draft_menu_items() { 1067 global $wpdb; 1068 $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS); 1069 1070 // delete orphaned draft menu items 1071 $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 ) ); 1072 1073 foreach( (array) $menu_items_to_delete as $menu_item_id ) 1074 wp_delete_post( $menu_item_id, true ); 1075 } 1076 1077 add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items'); 1078 1035 1079 ?> -
wp-admin/nav-menus.php
543 543 <br class="clear" /> 544 544 <div class="publishing-action"> 545 545 <input class="button-primary menu-save" name="save_menu" type="submit" value="<?php empty($nav_menu_selected_id) ? esc_attr_e('Create Menu') : esc_attr_e('Save Menu'); ?>" /> 546 </div><!-- END .publishing-action-->546 </div><!-- END .publishing-action --> 547 547 548 548 <?php if ( ! empty( $nav_menu_selected_id ) ) : ?> 549 549 <div class="delete-action"> 550 550 <a class="submitdelete deletion menu-delete" href="<?php echo esc_url( wp_nonce_url( admin_url('nav-menus.php?action=delete&menu=' . $nav_menu_selected_id), 'delete-nav_menu-' . $nav_menu_selected_id ) ); ?>"><?php _e('Delete Menu'); ?></a> 551 </div><!-- END .delete-action-->551 </div><!-- END .delete-action --> 552 552 <?php endif; ?> 553 </div><!-- END .major-publishing-actions-->554 </div><!-- END #submitpost .submitbox-->553 </div><!-- END .major-publishing-actions --> 554 </div><!-- END #submitpost .submitbox --> 555 555 <?php 556 556 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); 557 557 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); … … 559 559 ?> 560 560 <input type="hidden" name="action" value="update" /> 561 561 <input type="hidden" name="menu" id="menu" value="<?php echo esc_attr( $nav_menu_selected_id ); ?>" /> 562 </div><!-- END #nav-menu-header-->562 </div><!-- END #nav-menu-header --> 563 563 <div id="post-body"> 564 564 <div id="post-body-content"> 565 565 <?php if ( is_nav_menu( $nav_menu_selected_id ) ) : ?> … … 580 580 echo '<p>' . sprintf( __('For more information on this feature, see the <a href="%s">Custom Menus</a> article in the Codex.'), _x('http://codex.wordpress.org/Custom_Menus', 'Custom Menus codex page') ) . '</p>'; 581 581 echo '</div>'; 582 582 endif; ?> 583 </div><!-- /#post-body-content -->584 </div><!-- -/#post-body -->585 </form><!-- /#update-nav-menu-->583 </div><!-- /#post-body-content --> 584 </div><!-- /#post-body --> 585 </form><!-- /#update-nav-menu --> 586 586 </div><!-- /.menu-edit --> 587 587 </div><!-- /#menu-management --> 588 588 </div><!-- /#menu-management-liquid --> 589 </div><!-- /#nav-menus-frame -->589 </div><!-- /#nav-menus-frame --> 590 590 </div><!-- /.wrap--> 591 591 592 592