diff --git a/features/user.feature b/features/user.feature index 3b8662b308..4ca85cc0c6 100644 --- a/features/user.feature +++ b/features/user.feature @@ -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 @@ -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: """ @@ -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 + """ diff --git a/php/commands/user.php b/php/commands/user.php index 673cbbe756..4db4b5482f 100644 --- a/php/commands/user.php +++ b/php/commands/user.php @@ -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; } @@ -645,6 +649,19 @@ public function remove_cap( $args, $assoc_args ) { * * : User ID, user email, or login. * + * [--format=] + * : Render output in a particular format. + * --- + * default: list + * options: + * - list + * - table + * - csv + * - json + * - count + * - yaml + * --- + * * ## EXAMPLES * * $ wp user list-caps 21 @@ -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 ); + } + } }