Skip to content

Commit

Permalink
Add user exists command (#486)
Browse files Browse the repository at this point in the history
* Add user exists command

* Update README with new command

---------

Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
  • Loading branch information
shawnhooper and danielbachhuber authored Mar 7, 2024
1 parent 1ab4ad0 commit b180a66
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id>
~~~

Displays a success message if the user does exist.

**OPTIONS**

<id>
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.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
"user application-password update",
"user create",
"user delete",
"user exists",
"user generate",
"user get",
"user import-csv",
Expand Down
11 changes: 11 additions & 0 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
31 changes: 31 additions & 0 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* <id>
* : 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.
*
Expand Down

0 comments on commit b180a66

Please sign in to comment.