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


git-svn-id: http://core.svn.wordpress.org/trunk@20739 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2012-05-08 17:01:50 +00:00
parent c6dbd4b6a1
commit f5366a2ccf
2 changed files with 24 additions and 3 deletions

View File

@ -243,11 +243,21 @@ function wp_delete_user( $id, $reassign = 'novalue' ) {
do_action('delete_user', $id);
if ( 'novalue' === $reassign || null === $reassign ) {
$post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );
$post_types_to_delete = array();
foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
if ( $post_type->delete_with_user ) {
$post_types_to_delete[] = $post_type->name;
} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
$post_types_to_delete[] = $post_type->name;
}
}
$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
$post_types_to_delete = implode( "', '", $post_types_to_delete );
$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 ) );
if ( $post_ids ) {
foreach ( $post_ids as $post_id )
wp_delete_post($post_id);
wp_delete_post( $post_id );
}
// Clean links

View File

@ -29,6 +29,7 @@ function create_initial_post_types() {
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
) );
@ -45,6 +46,7 @@ function create_initial_post_types() {
'hierarchical' => true,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
) );
@ -63,7 +65,8 @@ function create_initial_post_types() {
'rewrite' => false,
'query_var' => false,
'show_in_nav_menus' => false,
'supports' => array( 'comments' ),
'delete_with_user' => true,
'supports' => array( 'comments', 'author' ),
) );
register_post_type( 'revision', array(
@ -80,6 +83,8 @@ function create_initial_post_types() {
'rewrite' => false,
'query_var' => false,
'can_export' => false,
'delete_with_user' => true,
'supports' => array( 'author' ),
) );
register_post_type( 'nav_menu_item', array(
@ -91,6 +96,7 @@ function create_initial_post_types() {
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'hierarchical' => false,
'rewrite' => false,
'delete_with_user' => false,
'query_var' => false,
) );
@ -933,6 +939,10 @@ function get_post_types( $args = array(), $output = 'names', $operator = 'and' )
* * If false, a post type cannot be loaded at ?{query_var}={post_slug}
* * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
* - can_export - Allows this post type to be exported. Defaults to true.
* - delete_with_user - Whether to delete posts of this type when deleting a user.
* * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
* * If false, posts of this type belonging to the user will *not* be trashed or deleted.
* * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
* - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
* - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
*
@ -959,6 +969,7 @@ function register_post_type( $post_type, $args = array() ) {
'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
'can_export' => true,
'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
'delete_with_user' => null,
);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;