Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --show-grant argument to wp cap list and --grant to wp cap add #19

Merged
merged 5 commits into from
Apr 19, 2018
Merged
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
82 changes: 82 additions & 0 deletions features/cap.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ Feature: Manage Cap
Success: Added 2 capabilities to 'contributor' role.
"""

When I run `wp cap add contributor detect --no-grant`
Then STDOUT should contain:
"""
Success: Added 1 capability to 'contributor' role as false.
"""

When I run `wp cap add contributor discover examine --no-grant`
Then STDOUT should contain:
"""
Success: Added 2 capabilities to 'contributor' role as false.
"""

When I run `wp cap list contributor`
Then STDOUT should contain:
"""
Expand All @@ -39,6 +51,44 @@ Feature: Manage Cap
"""
observe
"""
Then STDOUT should not contain:
"""
detect
"""
Then STDOUT should not contain:
"""
discover
"""
Then STDOUT should not contain:
"""
examine
"""

When I run `wp cap list contributor --show-grant`
Then STDOUT should contain:
"""
spectate,true
"""
And STDOUT should contain:
"""
behold,true
"""
And STDOUT should contain:
"""
observe,true
"""
Then STDOUT should contain:
"""
detect,false
"""
Then STDOUT should contain:
"""
discover,false
"""
Then STDOUT should contain:
"""
examine,false
"""

When I run `wp cap remove contributor spectate`
Then STDOUT should contain:
Expand All @@ -52,6 +102,12 @@ Feature: Manage Cap
Success: Removed 2 capabilities from 'contributor' role.
"""

When I run `wp cap remove contributor detect discover examine`
Then STDOUT should contain:
"""
Success: Removed 3 capabilities from 'contributor' role.
"""

When I run `wp cap list contributor`
Then STDOUT should not contain:
"""
Expand All @@ -66,6 +122,32 @@ Feature: Manage Cap
observe
"""

When I run `wp cap list contributor --show-grant`
Then STDOUT should not contain:
"""
spectate,true
"""
And STDOUT should not contain:
"""
behold,true
"""
And STDOUT should not contain:
"""
observe,true
"""
Then STDOUT should not contain:
"""
detect,false
"""
And STDOUT should not contain:
"""
discover,false
"""
And STDOUT should not contain:
"""
examine,false
"""

When I try `wp cap add role-not-available spectate`
Then STDERR should be:
"""
Expand Down
71 changes: 56 additions & 15 deletions src/Capabilities_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Capabilities_Command extends WP_CLI_Command {
* - count
* - yaml
* ---
*
* [--show-grant]
* : Display all capabilities defined for a role including grant.
* ---
* default: false
* ---
*
* ## EXAMPLES
*
Expand All @@ -60,21 +66,38 @@ class Capabilities_Command extends WP_CLI_Command {
*/
public function list_( $args, $assoc_args ) {
$role_obj = self::get_role( $args[0] );

$show_grant = ! empty( $assoc_args['show-grant'] );

if ( $show_grant ) {
array_push( $this->fields, 'grant' );
$capabilities = $role_obj->capabilities;
}
else {
$capabilities = array_filter( $role_obj->capabilities );
}

$output_caps = array();
foreach ( $capabilities as $cap => $grant ) {
$output_cap = new StdClass;

$output_cap->name = $cap;
$output_cap->grant = ( $grant ) ? 'true' : 'false';

$output_caps[] = $output_cap;
}

if ( 'list' === $assoc_args['format'] ) {
foreach ( array_keys( $role_obj->capabilities ) as $cap ) {
WP_CLI::line( $cap );
foreach ( $output_caps as $cap ) {
if ( $show_grant ) {
WP_CLI::line( implode( ',', array( $cap->name, $cap->grant ) ) );
}
else {
WP_CLI::line( $cap->name );
}
}
}
else {
$output_caps = array();
foreach ( array_keys( $role_obj->capabilities ) as $cap ) {
$output_cap = new stdClass;

$output_cap->name = $cap;

$output_caps[] = $output_cap;
}
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->fields );
$formatter->display_items( $output_caps );
}
Expand All @@ -90,32 +113,50 @@ public function list_( $args, $assoc_args ) {
*
* <cap>...
* : One or more capabilities to add.
*
* [--grant]
* : Adds the capability as an explicit boolean value, instead of implicitly defaulting to `true`.
* ---
* default: true
* options:
* - true
* - false
* ---
*
* ## EXAMPLES
*
* # Add 'spectate' capability to 'author' role.
* $ wp cap add author spectate
* Success: Added 1 capability to 'author' role.
*/
public function add( $args ) {
public function add( $args, $assoc_args ) {
self::persistence_check();

$role = array_shift( $args );

$role_obj = self::get_role( $role );

$grant = ! isset( $assoc_args['grant'] ) || ! empty( $assoc_args['grant'] );

$count = 0;

foreach ( $args as $cap ) {
if ( $role_obj->has_cap( $cap ) )
if ( true === $grant && $role_obj->has_cap( $cap ) )
continue;

if ( false === $grant && isset( $role_obj->capabilities[ $cap ] ) && false === $role_obj->capabilities[ $cap ] )
continue;

$role_obj->add_cap( $cap );
$role_obj->add_cap( $cap, $grant );

$count++;
}

$message = ( 1 === $count ) ? "Added %d capability to '%s' role." : "Added %d capabilities to '%s' role.";
if ( $grant ) {
$message = ( 1 === $count ) ? "Added %d capability to '%s' role." : "Added %d capabilities to '%s' role.";
} else {
$message = ( 1 === $count ) ? "Added %d capability to '%s' role as false." : "Added %d capabilities to '%s' role as false.";
}
WP_CLI::success( sprintf( $message, $count, $role ) );
}

Expand Down Expand Up @@ -146,7 +187,7 @@ public function remove( $args ) {
$count = 0;

foreach ( $args as $cap ) {
if ( !$role_obj->has_cap( $cap ) )
if ( ! isset( $role_obj->capabilities[ $cap ] ) )
continue;

$role_obj->remove_cap( $cap );
Expand Down