Skip to content

Reassigning posts while deleting a user can result in lost posts if the target user does not exist #461

@franz-josef-kaiser

Description

@franz-josef-kaiser

Feature Request

Describe your use case and the problem you are facing

When deleting a user, associated posts get deleted as well. If one uses the --reassign=<user-ID>, the soon-to-be-deleted users posts get assigned to the user whose ID gets passed with the --reassign flag. The problem is that if the user ID points to a non-existing user, the posts are lost to a non-existing user.

Describe the solution you'd like

A possible fix would be to check if the user that should get the posts assigned does not exist, an exception gets thrown, the command does not proceed and the user gets informed that the target user does not exist. Maybe the cli could even tell to re-check the list of possible target users by using wp users list.

Addendum

The "fault" here is not WP CLI (link to source), but WordPress core command wp_delete_user() in its current 6.4 version:

# source: wp_delete_user()
$wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) );

WordPress does not perform a check as it assumes that the user was properly pre-selected. This is something that can, due to the absence of an GUI that might stop without a form value somewhere, hardly be assumed in an CLI environment.

A possible Solution might be to utilize the WP_User::get_data_by() method:

} else {
+ 	if ( ! WP_User::get_data_by( 'id', $reassign ) {
+ 		return ['error', $message = "No user with ID `{$user_id}` found.\nTry using `wp users list` to identify the correct user." ]
+ 	}
	$result  = wp_delete_user( $user_id, $reassign );
	$message = "Removed user {$user_id} from " . home_url() . '.';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions