Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions features/roles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,25 @@ Feature: Manage WordPress roles
"""
10
"""

Scenario: Cloning a role keeps denied capabilities denied
When I run `wp cap add author spectate --grant=false`
And I run `wp role create reporter Reporter --clone=author`
Then STDOUT should be:
"""
Success: Role with key 'reporter' created. Cloned capabilities from 'author'.
"""

When I run `wp cap list reporter --show-grant`
Then STDOUT should contain:
"""
spectate,false
"""
And STDOUT should not contain:
"""
spectate,true
"""
And STDOUT should contain:
"""
upload_files,true
"""
10 changes: 3 additions & 7 deletions src/Role_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,17 @@ public function create( $args, $assoc_args ) {
WP_CLI::error( "Can't create role, insufficient information provided." );
}

$capabilities = false;
$capabilities = array();
if ( ! empty( $assoc_args['clone'] ) ) {
$role_obj = $wp_roles->get_role( $assoc_args['clone'] );
if ( ! $role_obj ) {
WP_CLI::error( "'{$assoc_args['clone']}' role not found." );
}
$capabilities = array_keys( $role_obj->capabilities );
$capabilities = $role_obj->capabilities;
}

if ( add_role( $role_key, $role_name ) ) {
if ( add_role( $role_key, $role_name, $capabilities ) ) {
if ( ! empty( $capabilities ) ) {
$role_obj = $wp_roles->get_role( $role_key );
foreach ( $capabilities as $cap ) {
$role_obj->add_cap( $cap );
}
WP_CLI::success( "Role with key '{$role_key}' created. Cloned capabilities from '{$assoc_args['clone']}'." );
} else {
WP_CLI::success( "Role with key '{$role_key}' created." );
Expand Down
Loading