Skip to content
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
33 changes: 30 additions & 3 deletions features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Feature: Manage WordPress users
Then STDOUT should be a table containing rows:
| Field | Value |
| roles | |

Scenario: Managing user capabilities
Given a WP install

Expand All @@ -178,13 +178,13 @@ Feature: Manage WordPress users
"""
Success: Added 'edit_vip_product' capability for admin (1).
"""

And I run `wp user list-caps 1 | tail -n 1`
Then STDOUT should be:
"""
edit_vip_product
"""

And I run `wp user remove-cap 1 edit_vip_product`
Then STDOUT should be:
"""
Expand Down Expand Up @@ -238,3 +238,30 @@ Feature: Manage WordPress users
"""
testsubscriber
"""

Scenario: Listing user capabilities
Given a WP install

When I run `wp user create bob bob@gmail.com --role=contributor`
And I run `wp user list-caps bob`
Then STDOUT should be:
"""
edit_posts
read
level_1
level_0
delete_posts
contributor
"""

And I run `wp user list-caps bob --format=json`
Then STDOUT should be:
"""
[{"name":"edit_posts"},{"name":"read"},{"name":"level_1"},{"name":"level_0"},{"name":"delete_posts"},{"name":"contributor"}]
"""

And I run `wp user list-caps bob --format=count`
Then STDOUT should be:
"""
6
"""
40 changes: 39 additions & 1 deletion php/commands/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class User_Command extends \WP_CLI\CommandWithDBObject {
'roles'
);

private $cap_fields = array(
'name'
);

public function __construct() {
$this->fetcher = new \WP_CLI\Fetchers\User;
}
Expand Down Expand Up @@ -645,6 +649,19 @@ public function remove_cap( $args, $assoc_args ) {
* <user>
* : User ID, user email, or login.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: list
* options:
* - list
* - table
* - csv
* - json
* - count
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp user list-caps 21
Expand All @@ -661,11 +678,32 @@ public function list_caps( $args, $assoc_args ) {

$user_caps_list = $user->allcaps;

$active_user_cap_list = array();

foreach ( $user_caps_list as $cap => $active ) {
if ( $active ) {
\cli\line( $cap );
$active_user_cap_list[] = $cap;
}
}

if ( 'list' === $assoc_args['format'] ) {
foreach ( $active_user_cap_list as $cap ) {
WP_CLI::line( $cap );
}
}
else {
$output_caps = array();
foreach ( $active_user_cap_list as $cap ) {
$output_cap = new stdClass;

$output_cap->name = $cap;

$output_caps[] = $output_cap;
}
$formatter = new \WP_CLI\Formatter( $assoc_args, $this->cap_fields );
$formatter->display_items( $output_caps );
}

}
}

Expand Down