Skip to content

Commit 6ecf307

Browse files
Provided flag to remove role forcefully
1 parent 84f3aa4 commit 6ecf307

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

features/user.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,16 @@ Feature: Manage WordPress users
455455
Then the return code should be 1
456456
And STDERR should be:
457457
"""
458-
Error: There is a role similar to 'contributor' capability. Use `wp user remove-role` instead.
458+
Error: There is a role similar to 'contributor' capability. Use `wp user remove-cap {USER_ID} contributor --force` to remove capability.
459459
"""
460460
And STDOUT should be empty
461461
462+
When I run `wp user remove-cap {USER_ID} contributor --force`
463+
Then STDOUT should be:
464+
"""
465+
Success: Removed 'contributor' cap for testuser2 ({USER_ID}).
466+
"""
467+
462468
Scenario: Show password when creating a user
463469
Given a WP install
464470

src/User_Command.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,9 @@ public function add_cap( $args, $assoc_args ) {
861861
* <cap>
862862
* : The capability to be removed.
863863
*
864+
* [--force]
865+
* : Forcefully remove a capability.
866+
*
864867
* ## EXAMPLES
865868
*
866869
* $ wp user remove-cap 11 publish_newsletters
@@ -872,6 +875,9 @@ public function add_cap( $args, $assoc_args ) {
872875
* $ wp user remove-cap 11 nonexistent_cap
873876
* Error: No such 'nonexistent_cap' cap for supervisor (11).
874877
*
878+
* $ wp user remove-cap 11 publish_newsletters --force
879+
* Success: Removed 'publish_newsletters' cap for supervisor (11).
880+
*
875881
* @subcommand remove-cap
876882
*/
877883
public function remove_cap( $args, $assoc_args ) {
@@ -886,8 +892,8 @@ public function remove_cap( $args, $assoc_args ) {
886892
}
887893

888894
$user_roles = $user->roles;
889-
if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) ) {
890-
WP_CLI::error( "There is a role similar to '{$cap}' capability. Use `wp user remove-role` instead." );
895+
if ( ! empty( $user_roles ) && in_array( $cap, $user_roles, true ) && ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
896+
WP_CLI::error( "There is a role similar to '{$cap}' capability. Use `wp user remove-cap {$user->ID} {$cap} --force` to remove capability." );
891897
}
892898
$user->remove_cap( $cap );
893899

0 commit comments

Comments
 (0)