From 0423c4bdea8d9c00c8a5c22ede5b332357687b27 Mon Sep 17 00:00:00 2001 From: mpeshev Date: Tue, 6 Aug 2013 02:08:18 +0300 Subject: [PATCH] Add capabilities list function for users --- php/commands/user.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/php/commands/user.php b/php/commands/user.php index 5f7d010e..e1aca7ac 100644 --- a/php/commands/user.php +++ b/php/commands/user.php @@ -504,6 +504,44 @@ class User_Command extends \WP_CLI\CommandWithDBObject { WP_CLI::success( sprintf( "Removed '%s' cap for %s (%d).", $cap, $user->user_login, $user->ID ) ); } + + /** + * List all user's capabilities. + * + * ## OPTIONS + * + * + * : User ID or user login. + * + * ## EXAMPLES + * + * wp user list-caps admin + * wp user list-caps 21 + * + * @subcommand list-caps + * @synopsis + */ + public function list_caps( $args, $assoc_args ) { + $user = self::get_user( $args[0] ); + $user->get_role_caps(); + + $user_caps_list = $user->allcaps; + $cap_table_titles = array( 'capability', 'status' ); + + // Get all active caps (marked as true) + $active_user_caps = array(); + + foreach( $user_caps_list as $cap => $active ) { + if( $active ) { + $active_user_caps[] = $cap; + } + } + + // Omit formatting from the Utils class due to the assoc array format + $user_caps = implode( ', ', $active_user_caps ); + + WP_CLI::success( sprintf( "User caps (role and individual) are: %s.", $user_caps ) ); + } private static function get_user( $id_or_login ) { if ( is_numeric( $id_or_login ) ) -- 2.11.0