@@ -870,6 +870,19 @@ public function remove_cap( $args, $assoc_args ) {
870870 * - yaml
871871 * ---
872872 *
873+ * [--origin=<origin>]
874+ * : Render output in a particular format.
875+ * ---
876+ * default: all
877+ * options:
878+ * - all
879+ * - user
880+ * - role
881+ * ---
882+ *
883+ * [--exclude-role-names]
884+ * : Exclude capabilities that match role names from output.
885+ *
873886 * ## EXAMPLES
874887 *
875888 * $ wp user list-caps 21
@@ -879,37 +892,69 @@ public function remove_cap( $args, $assoc_args ) {
879892 * @subcommand list-caps
880893 */
881894 public function list_caps ( $ args , $ assoc_args ) {
882- $ user = $ this ->fetcher ->get_check ( $ args [0 ] );
895+ $ user = $ this ->fetcher ->get_check ( $ args [0 ] );
896+ $ exclude_role_names = Utils \get_flag_value ( $ assoc_args , 'exclude-role-names ' );
883897
884- if ( $ user ) {
885- $ user ->get_role_caps ();
898+ $ active_user_cap_list = [];
886899
887- $ user_caps_list = $ user ->allcaps ;
900+ $ user_roles = $ user ->roles ;
888901
889- $ active_user_cap_list = [];
902+ switch ( $ assoc_args ['origin ' ] ) {
903+ case 'all ' :
904+ $ user ->get_role_caps ();
905+ $ user_caps_list = $ user ->allcaps ;
890906
891- foreach ( $ user_caps_list as $ cap => $ active ) {
892- if ( $ active ) {
893- $ active_user_cap_list [] = $ cap ;
907+ foreach ( $ user_caps_list as $ capability => $ active ) {
908+ if ( $ exclude_role_names && in_array ( $ capability , $ user_roles , true ) ) {
909+ continue ;
910+ }
911+ if ( $ active ) {
912+ $ active_user_cap_list [] = $ capability ;
913+ }
894914 }
895- }
896-
897- if ( 'list ' === $ assoc_args ['format ' ] ) {
898- foreach ( $ active_user_cap_list as $ cap ) {
899- WP_CLI ::line ( $ cap );
915+ break ;
916+ case 'user ' :
917+ // Get the user's capabilities
918+ $ user_capabilities = get_user_meta ( $ user ->ID , 'wp_capabilities ' , true );
919+
920+ // Loop through each capability and only return the non-inherited ones
921+ foreach ( $ user_capabilities as $ capability => $ active ) {
922+ if ( true === $ active && ! in_array ( $ capability , $ user_roles , true ) ) {
923+ $ active_user_cap_list [] = $ capability ;
924+ }
900925 }
901- } else {
902- $ output_caps = [];
903- foreach ( $ active_user_cap_list as $ cap ) {
904- $ output_cap = new stdClass ();
926+ break ;
927+ case 'role ' :
928+ // Get the user's capabilities
929+ $ user_capabilities = get_user_meta ( $ user ->ID , 'wp_capabilities ' , true );
930+
931+ // Loop through each capability and only return the inherited ones (including the role name)
932+ foreach ( $ user ->get_role_caps () as $ capability => $ active ) {
933+ if ( true === $ active && ! isset ( $ user_capabilities [ $ capability ] ) ) {
934+ $ active_user_cap_list [] = $ capability ;
935+ }
936+ if ( true === $ active && ! $ exclude_role_names && in_array ( $ capability , $ user_roles , true ) ) {
937+ $ active_user_cap_list [] = $ capability ;
938+ }
939+ }
940+ break ;
941+ }
942+
943+ if ( 'list ' === $ assoc_args ['format ' ] ) {
944+ foreach ( $ active_user_cap_list as $ cap ) {
945+ WP_CLI ::line ( $ cap );
946+ }
947+ } else {
948+ $ output_caps = [];
949+ foreach ( $ active_user_cap_list as $ cap ) {
950+ $ output_cap = new stdClass ();
905951
906- $ output_cap ->name = $ cap ;
952+ $ output_cap ->name = $ cap ;
907953
908- $ output_caps [] = $ output_cap ;
909- }
910- $ formatter = new Formatter ( $ assoc_args , $ this ->cap_fields );
911- $ formatter ->display_items ( $ output_caps );
954+ $ output_caps [] = $ output_cap ;
912955 }
956+ $ formatter = new Formatter ( $ assoc_args , $ this ->cap_fields );
957+ $ formatter ->display_items ( $ output_caps );
913958 }
914959 }
915960
0 commit comments