diff --git a/README.md b/README.md index 108bdfab..362a8b5e 100644 --- a/README.md +++ b/README.md @@ -5423,6 +5423,36 @@ make sure to reassign their posts prior to deleting the user. +### wp user exists + +Verifies whether a user exists. + +~~~ +wp user exists +~~~ + +Displays a success message if the user does exist. + +**OPTIONS** + + + The ID of the user to check. + +**EXAMPLES** + + # The user exists. + $ wp user exists 1337 + Success: User with ID 1337 exists. + $ echo $? + 0 + + # The user does not exist. + $ wp user exists 10000 + $ echo $? + 1 + + + ### wp user generate Generates some users. diff --git a/composer.json b/composer.json index bc070ffe..163b0bd4 100644 --- a/composer.json +++ b/composer.json @@ -177,6 +177,7 @@ "user application-password update", "user create", "user delete", + "user exists", "user generate", "user get", "user import-csv", diff --git a/features/user.feature b/features/user.feature index 808f3097..3306ebfb 100644 --- a/features/user.feature +++ b/features/user.feature @@ -17,6 +17,17 @@ Feature: Manage WordPress users | ID | {USER_ID} | | roles | author | + When I run `wp user exists {USER_ID}` + Then STDOUT should be: + """ + Success: User with ID {USER_ID} exists. + """ + And the return code should be 0 + + When I try `wp user exists 1000` + And STDOUT should be empty + And the return code should be 1 + When I run `wp user get {USER_ID} --field=user_registered` Then STDOUT should not contain: """ diff --git a/src/User_Command.php b/src/User_Command.php index b03c984c..666ec18a 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -651,6 +651,37 @@ public function generate( $args, $assoc_args ) { } } + /** + * Verifies whether a user exists. + * + * Displays a success message if the user does exist. + * + * ## OPTIONS + * + * + * : The ID of the user to check. + * + * ## EXAMPLES + * + * # The user exists. + * $ wp user exists 1337 + * Success: User with ID 1337 exists. + * $ echo $? + * 0 + * + * # The user does not exist. + * $ wp user exists 10000 + * $ echo $? + * 1 + */ + public function exists( $args ) { + if ( $this->fetcher->get( $args[0] ) ) { + WP_CLI::success( "User with ID {$args[0]} exists." ); + } else { + WP_CLI::halt( 1 ); + } + } + /** * Sets the user role. *