WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/29/2010 07:33:56 AM (11 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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?>
Note: See TracChangeset for help on using the changeset viewer.