From e8297a96417b99924fa150426021889841a97fd7 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 5 Mar 2024 13:39:26 -0500 Subject: [PATCH 1/2] Add user exists command --- features/user.feature | 11 +++++++++++ src/User_Command.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/features/user.feature b/features/user.feature index 53d380a4..aace66ad 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 28de29aa..ec96b468 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -647,6 +647,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. * From c80b404dbd6ad86b6e2b5964d3298561072a5b0c Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 7 Mar 2024 14:25:27 +0800 Subject: [PATCH 2/2] Update README with new command --- README.md | 30 ++++++++++++++++++++++++++++++ composer.json | 1 + 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 443b4e98..f525572c 100644 --- a/README.md +++ b/README.md @@ -4769,6 +4769,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 40a75742..5a7f46de 100644 --- a/composer.json +++ b/composer.json @@ -158,6 +158,7 @@ "user add-role", "user create", "user delete", + "user exists", "user generate", "user get", "user import-csv",