WordPress.org

Make WordPress Core

Changeset 14295


Ignore:
Timestamp:
04/29/2010 07:33:56 AM (8 years ago)
Author:
nacin
Message:

When posts/terms are deleted/trashed, update associated menu items. Deleted post = deleted menu item, trashed post = menu item becomes a draft. props filosofo, see #13174

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r14263 r14295  
    828828    if ( isset( $_POST['menu-item'] ) ) {
    829829        $item_ids = wp_save_nav_menu_item( $menu_id, $_POST['menu-item'] );
     830        if ( is_wp_error( $item_ids ) )
     831            die('-1');
    830832    } else {
    831833        $item_ids = array();
  • trunk/wp-admin/includes/nav-menu.php

    r14293 r14295  
    809809 * @param int $menu_id The menu ID for which to save this item.
    810810 * @param array $menu_data The unsanitized posted menu item data.
    811  * @return array The database IDs of the items saved.
     811 * @return array The database IDs of the items saved
    812812 */
    813813function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) {
  • trunk/wp-admin/nav-menus.php

    r14288 r14295  
    3939
    4040// Container for any messages displayed to the user
    41 $messages_div = '';
     41$messages = array();
    4242
    4343// Container that stores the name of the active menu
     
    170170            if ( wp_delete_post( $menu_item_id, true ) ) {
    171171               
    172                 $messages_div = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
     172                $messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
    173173            }
    174174        }
     
    181181
    182182            if ( is_wp_error($delete_nav_menu) ) {
    183                 $messages_div = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
     183                $messages[] = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
    184184            } else {
    185                 $messages_div = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
     185                $messages[] = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
    186186                $nav_menu_selected_id = 0; // Reset the selected menu
    187187            }
     
    202202
    203203                    if ( is_wp_error( $_nav_menu_selected_id ) ) {
    204                         $messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
     204                        $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
    205205                    } else {
    206206                        $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
    207207                        $nav_menu_selected_id = $_nav_menu_selected_id;
    208208                        $nav_menu_selected_title = $_menu_object->name;
    209                         $messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
     209                        $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
    210210                    }
    211211                } else {
    212                     $messages_div = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
     212                    $messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
    213213                }
    214214            }
     
    223223                if ( is_wp_error( $_nav_menu_selected_id ) ) {
    224224                    $_menu_object = $_nav_menu_selected_id;
    225                     $messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
     225                    $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
    226226                } else {
    227227                    $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
     
    249249                        $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
    250250
    251                         if ( ! is_wp_error( $menu_item_db_id ) && isset( $menu_items[$menu_item_db_id] ) ) {
     251                        if ( is_wp_error( $menu_item_db_id ) )
     252                            $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
     253                        elseif ( isset( $menu_items[$menu_item_db_id] ) )
    252254                            unset( $menu_items[$menu_item_db_id] );
    253                         }
    254255                    }
    255256                }
     
    266267                do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
    267268
    268                 $messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
     269                $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
    269270                unset( $menu_items );
    270271            }
     
    311312    // Set up nav menu
    312313    wp_nav_menu_setup();
    313     $messages_div = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the &#8220;Navigation Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p></div>';
     314    $messages[] = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the &#8220;Navigation Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p></div>';
    314315
    315316// The theme supports neither menus nor widgets.
    316317} else {
    317318    remove_meta_box( 'create-menu', 'nav-menus', 'side' );
    318     $messages_div = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
     319    $messages[] = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
    319320}
    320321
     
    325326    <?php screen_icon(); ?>
    326327    <h2><?php esc_html_e('Menus'); ?></h2>
    327     <?php echo $messages_div; ?>
     328    <?php
     329    foreach( $messages as $message ) :
     330        echo $message . "\n";
     331    endforeach;
     332    ?>
    328333   
    329334    <?php if ( current_theme_supports('nav-menus') || current_theme_supports('widgets') ) : ?>
  • trunk/wp-includes/default-filters.php

    r14283 r14295  
    225225add_action( 'transition_post_status',     '_transition_post_status',  5, 3 );
    226226add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce'        );
    227 add_action( 'wp_scheduled_delete',        'wp_scheduled_delete' );
     227add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'            );
     228add_action( 'trash_post',                 '_wp_trash_menu_item'            );
     229add_action( 'untrash_post',               '_wp_untrash_menu_item'          );
     230add_action( 'delete_post',                '_wp_delete_post_menu_item'      );
     231add_action( 'delete_term',                '_wp_delete_tax_menu_item'       );
    228232
    229233// Post Thumbnail CSS class filtering
  • trunk/wp-includes/nav-menu.php

    r14285 r14295  
    269269            $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
    270270        } elseif ( 'post_type' == $args['menu-item-type'] ) {
     271
    271272            $original_object = get_post( $args['menu-item-object-id'] );
    272273            $original_title = $original_object->post_title;
     274
     275            if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) {
     276                $post_type_object = get_post_type_object( $args['menu-item-object'] );
     277                if ( isset( $post_type_object->singular_label ) )
     278                    return new WP_Error('update_nav_menu_item_failed', sprintf(__('The menu item "%1$s" belongs to a %2$s that is in the trash, so it cannot be updated.'), $args['menu-item-title'], $post_type_object->singular_label ) );
     279                else
     280                    return new WP_Error('update_nav_menu_item_failed', sprintf(__('The menu item "%1$s" belongs to something that is in the trash, so it cannot be updated.'), $args['menu-item-title'] ) );
     281            }
    273282        }
    274283
     
    529538    return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
    530539}
     540
     541/**
     542 * Get the menu items associated with a particular object.
     543 *
     544 * @since 3.0.0
     545 *
     546 * @param int $object_id The ID of the original object.
     547 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
     548 * @return array The array of menu item IDs; empty array if none;
     549 */
     550function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type' ) {
     551    $object_id = (int) $object_id;
     552    $menu_item_ids = array();
     553
     554    $query = new WP_Query;
     555    $menu_items = $query->query(
     556        array(
     557            'meta_key' => '_menu_item_object_id',
     558            'meta_value' => $object_id,
     559            'post_status' => 'any',
     560            'post_type' => 'nav_menu_item',
     561            'showposts' => -1,
     562        )
     563    );
     564    foreach( (array) $menu_items as $menu_item ) {
     565        if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
     566            if ( get_post_meta($menu_item->ID, '_menu_item_type', true) != $object_type )
     567                continue;
     568
     569            $menu_item_ids[] = (int) $menu_item->ID;
     570        }
     571    }
     572
     573    return array_unique( $menu_item_ids );
     574}
     575
     576/**
     577 * Callback for handling a menu item when its original object is trashed.
     578 *
     579 * @since 3.0.0
     580 * @access private
     581 *
     582 * @param int $object_id The ID of the original object being trashed.
     583 *
     584 */
     585function _wp_trash_menu_item( $object_id = 0 ) {
     586    $object_id = (int) $object_id;
     587
     588    $menu_item_ids = wp_get_associated_nav_menu_items( $object_id );
     589
     590    foreach( (array) $menu_item_ids as $menu_item_id ) {
     591        $menu_item = get_post( $menu_item_id, ARRAY_A );
     592        $menu_item['post_status'] = 'draft';
     593        wp_insert_post($menu_item);
     594    }
     595}
     596
     597/**
     598 * Callback for handling a menu item when its original object is un-trashed.
     599 *
     600 * @since 3.0.0
     601 * @access private
     602 *
     603 * @param int $object_id The ID of the original object being untrashed.
     604 *
     605 */
     606function _wp_untrash_menu_item( $object_id = 0 ) {
     607    $object_id = (int) $object_id;
     608
     609    $menu_item_ids = wp_get_associated_nav_menu_items( $object_id );
     610
     611    foreach( (array) $menu_item_ids as $menu_item_id ) {
     612        $menu_item = get_post( $menu_item_id, ARRAY_A );
     613        $menu_item['post_status'] = 'publish';
     614        wp_insert_post($menu_item);
     615    }
     616}
     617
     618/**
     619 * Callback for handling a menu item when its original object is deleted.
     620 *
     621 * @since 3.0.0
     622 * @access private
     623 *
     624 * @param int $object_id The ID of the original object being trashed.
     625 *
     626 */
     627function _wp_delete_post_menu_item( $object_id = 0 ) {
     628    $object_id = (int) $object_id;
     629
     630    $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
     631
     632    foreach( (array) $menu_item_ids as $menu_item_id ) {
     633        wp_delete_post( $menu_item_id, true );
     634    }
     635}
     636
     637/**
     638 * Callback for handling a menu item when its original object is deleted.
     639 *
     640 * @since 3.0.0
     641 * @access private
     642 *
     643 * @param int $object_id The ID of the original object being trashed.
     644 *
     645 */
     646function _wp_delete_tax_menu_item( $object_id = 0 ) {
     647    $object_id = (int) $object_id;
     648
     649    $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy' );
     650
     651    foreach( (array) $menu_item_ids as $menu_item_id ) {
     652        wp_delete_post( $menu_item_id, true );
     653    }
     654}
     655
    531656?>
  • trunk/wp-includes/post.php

    r14262 r14295  
    21682168            $parent_post = get_post($post_parent);
    21692169            // Check for circular dependency
    2170             if ( $parent_post->post_parent == $post_ID )
     2170            if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
    21712171                $post_parent = 0;
    21722172        }
Note: See TracChangeset for help on using the changeset viewer.