Skip to content

Commit b180a66

Browse files
Add user exists command (#486)
* Add user exists command * Update README with new command --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 1ab4ad0 commit b180a66

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5423,6 +5423,36 @@ make sure to reassign their posts prior to deleting the user.
54235423

54245424

54255425

5426+
### wp user exists
5427+
5428+
Verifies whether a user exists.
5429+
5430+
~~~
5431+
wp user exists <id>
5432+
~~~
5433+
5434+
Displays a success message if the user does exist.
5435+
5436+
**OPTIONS**
5437+
5438+
<id>
5439+
The ID of the user to check.
5440+
5441+
**EXAMPLES**
5442+
5443+
# The user exists.
5444+
$ wp user exists 1337
5445+
Success: User with ID 1337 exists.
5446+
$ echo $?
5447+
0
5448+
5449+
# The user does not exist.
5450+
$ wp user exists 10000
5451+
$ echo $?
5452+
1
5453+
5454+
5455+
54265456
### wp user generate
54275457

54285458
Generates some users.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
"user application-password update",
178178
"user create",
179179
"user delete",
180+
"user exists",
180181
"user generate",
181182
"user get",
182183
"user import-csv",

features/user.feature

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ Feature: Manage WordPress users
1717
| ID | {USER_ID} |
1818
| roles | author |
1919

20+
When I run `wp user exists {USER_ID}`
21+
Then STDOUT should be:
22+
"""
23+
Success: User with ID {USER_ID} exists.
24+
"""
25+
And the return code should be 0
26+
27+
When I try `wp user exists 1000`
28+
And STDOUT should be empty
29+
And the return code should be 1
30+
2031
When I run `wp user get {USER_ID} --field=user_registered`
2132
Then STDOUT should not contain:
2233
"""

src/User_Command.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,37 @@ public function generate( $args, $assoc_args ) {
651651
}
652652
}
653653

654+
/**
655+
* Verifies whether a user exists.
656+
*
657+
* Displays a success message if the user does exist.
658+
*
659+
* ## OPTIONS
660+
*
661+
* <id>
662+
* : The ID of the user to check.
663+
*
664+
* ## EXAMPLES
665+
*
666+
* # The user exists.
667+
* $ wp user exists 1337
668+
* Success: User with ID 1337 exists.
669+
* $ echo $?
670+
* 0
671+
*
672+
* # The user does not exist.
673+
* $ wp user exists 10000
674+
* $ echo $?
675+
* 1
676+
*/
677+
public function exists( $args ) {
678+
if ( $this->fetcher->get( $args[0] ) ) {
679+
WP_CLI::success( "User with ID {$args[0]} exists." );
680+
} else {
681+
WP_CLI::halt( 1 );
682+
}
683+
}
684+
654685
/**
655686
* Sets the user role.
656687
*

0 commit comments

Comments
 (0)