Make WordPress Core

Changeset 20739


Ignore:
Timestamp:
05/08/2012 05:01:50 PM (12 years ago)
Author:
ryan
Message:

Don't delete nav menu items when the user that owns them is deleted.

  • Introduce delete_with_user flag to register_post_type
  • Set delete_with_user to false for the nav_menu_item post type
  • Set it to true for all other core post types
  • If delete_with_user is not set, fallback to post_type_supports('author')

Props nacin
Fixes #16358

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/user.php

    r20581 r20739  
    244244
    245245    if ( 'novalue' === $reassign || null === $reassign ) {
    246         $post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
    247 
     246        $post_types_to_delete = array();
     247        foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
     248            if ( $post_type->delete_with_user ) {
     249                $post_types_to_delete[] = $post_type->name;
     250            } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
     251                $post_types_to_delete[] = $post_type->name;
     252            }
     253        }
     254
     255        $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
     256        $post_types_to_delete = implode( "', '", $post_types_to_delete );
     257        $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) );
    248258        if ( $post_ids ) {
    249259            foreach ( $post_ids as $post_id )
    250                 wp_delete_post($post_id);
     260                wp_delete_post( $post_id );
    251261        }
    252262
  • trunk/wp-includes/post.php

    r20734 r20739  
    3030        'rewrite' => false,
    3131        'query_var' => false,
     32        'delete_with_user' => true,
    3233        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
    3334    ) );
     
    4647        'rewrite' => false,
    4748        'query_var' => false,
     49        'delete_with_user' => true,
    4850        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
    4951    ) );
     
    6466        'query_var' => false,
    6567        'show_in_nav_menus' => false,
    66         'supports' => array( 'comments' ),
     68        'delete_with_user' => true,
     69        'supports' => array( 'comments', 'author' ),
    6770    ) );
    6871
     
    8184        'query_var' => false,
    8285        'can_export' => false,
     86        'delete_with_user' => true,
     87        'supports' => array( 'author' ),
    8388    ) );
    8489
     
    9297        'hierarchical' => false,
    9398        'rewrite' => false,
     99        'delete_with_user' => false,
    94100        'query_var' => false,
    95101    ) );
     
    934940 *     * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
    935941 * - can_export - Allows this post type to be exported. Defaults to true.
     942 * - delete_with_user - Whether to delete posts of this type when deleting a user.
     943 *     * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
     944 *     * If false, posts of this type belonging to the user will *not* be trashed or deleted.
     945 *     * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
    936946 * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
    937947 * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
     
    960970        'can_export' => true,
    961971        'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
     972        'delete_with_user' => null,
    962973    );
    963974    $args = wp_parse_args($args, $defaults);
Note: See TracChangeset for help on using the changeset viewer.