From be8716e16f3b91342a501f894d94b75a5a7a687b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Thu, 29 Feb 2024 11:03:21 +0545 Subject: [PATCH 1/3] Validate reassign user in user delete command --- src/User_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/User_Command.php b/src/User_Command.php index 0ff3eda5..b7fdb002 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -280,8 +280,10 @@ public function delete( $args, $assoc_args ) { WP_CLI::error( 'Reassigning content to a different user is not supported on multisite.' ); } - if ( ! $reassign ) { - WP_CLI::confirm( '--reassign parameter not passed. All associated posts will be deleted. Proceed?', $assoc_args ); + $is_reassign_valid = ( false === get_userdata( $reassign ) ) ? false : true; + + if ( ! $reassign || ! $is_reassign_valid ) { + WP_CLI::confirm( '--reassign parameter not passed or invalid. All associated posts will be deleted. Proceed?', $assoc_args ); } $users = $this->fetcher->get_many( $args ); From cb938cc782452d8684cd6b996c3e9a9427e86d8b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Mon, 4 Mar 2024 12:44:56 +0545 Subject: [PATCH 2/3] Split reassign messages --- features/user.feature | 43 +++++++++++++++++++++++++++++++++++++++++++ src/User_Command.php | 6 ++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/features/user.feature b/features/user.feature index 3302b105..53017b96 100644 --- a/features/user.feature +++ b/features/user.feature @@ -136,6 +136,49 @@ Feature: Manage WordPress users 3 """ + Scenario: Delete user with invalid reassign + Given a WP install + And a session_no file: + """ + n + """ + And a session_yes file: + """ + y + """ + + When I run `wp user create bobjones bob@example.com --role=author --porcelain` + And save STDOUT as {BOB_ID} + + When I run `wp post list --format=count` + And save STDOUT as {TOTAL_POSTS} + + When I run `wp post generate --count=3 --format=ids --post_author=bobjones` + And I run `wp post list --author={BOB_ID} --format=count` + Then STDOUT should be: + """ + 3 + """ + + When I run `wp user delete bobjones < session_no` + Then STDOUT should contain: + """ + --reassign parameter not passed. All associated posts will be deleted. Proceed? [y/n] + """ + + When I run `wp user delete bobjones --reassign=99999 < session_no` + Then STDOUT should contain: + """ + --reassign parameter is invalid. All associated posts will be deleted. Proceed? [y/n] + """ + + When I run `wp user delete bobjones < session_yes` + And I run `wp post list --format=count` + Then STDOUT should be: + """ + {TOTAL_POSTS} + """ + Scenario: Deleting user from the whole network Given a WP multisite install diff --git a/src/User_Command.php b/src/User_Command.php index b7fdb002..34f2908c 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -282,8 +282,10 @@ public function delete( $args, $assoc_args ) { $is_reassign_valid = ( false === get_userdata( $reassign ) ) ? false : true; - if ( ! $reassign || ! $is_reassign_valid ) { - WP_CLI::confirm( '--reassign parameter not passed or invalid. All associated posts will be deleted. Proceed?', $assoc_args ); + if ( ! $reassign ) { + WP_CLI::confirm( '--reassign parameter not passed. All associated posts will be deleted. Proceed?', $assoc_args ); + } elseif ( ! $is_reassign_valid ) { + WP_CLI::confirm( '--reassign parameter is invalid. All associated posts will be deleted. Proceed?', $assoc_args ); } $users = $this->fetcher->get_many( $args ); From 872693de0b9f5381153338d4bc35b1a8a2cba8bc Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 4 Mar 2024 18:55:10 -0800 Subject: [PATCH 3/3] Avoid DB hit unless `$reassign` is provided --- src/User_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/User_Command.php b/src/User_Command.php index 34f2908c..f41cea5b 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -280,7 +280,7 @@ public function delete( $args, $assoc_args ) { WP_CLI::error( 'Reassigning content to a different user is not supported on multisite.' ); } - $is_reassign_valid = ( false === get_userdata( $reassign ) ) ? false : true; + $is_reassign_valid = ( $reassign && false === get_userdata( $reassign ) ) ? false : true; if ( ! $reassign ) { WP_CLI::confirm( '--reassign parameter not passed. All associated posts will be deleted. Proceed?', $assoc_args );