Created
May 15, 2018 20:28
-
-
Save jasonbahl/13a1d9aa1be991c61796cbd3353e6032 to your computer and use it in GitHub Desktop.
List what user roles are associated with each capability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_filter( 'graphql_root_queries', function( $fields ) { | |
| $fields['userRoles'] = [ | |
| 'type' => \WPGraphQL\Types::list_of( new \WPGraphQL\Type\WPObjectType([ | |
| 'name' => 'UserRole', | |
| 'fields' => [ | |
| 'name' => [ | |
| 'type' => \WPGraphQL\Types::string(), | |
| ], | |
| 'capabilities' => [ | |
| 'type' => \WPGraphQL\Types::list_of( \WPGraphQL\Types::string() ), | |
| 'resolve' => function( $roles ) { | |
| $capabilities = []; | |
| if ( ! empty( $roles['capabilities'] ) && is_array( $roles['capabilities'] ) ) { | |
| $capabilities = array_keys( $roles['capabilities'] ); | |
| } | |
| sort($capabilities ); | |
| return $capabilities; | |
| } | |
| ], | |
| ], | |
| ])), | |
| 'resolve' => function( $root, $args, $context, $info ) { | |
| global $wp_roles; | |
| $roles = function_exists( 'get_editable_roles' ) ? get_editable_roles() : apply_filters( 'editable_roles', $wp_roles->roles ); | |
| return ! empty( $roles ) ? $roles : null; | |
| } | |
| ]; | |
| return $fields; | |
| } ); | |
| add_action( 'init', function() { | |
| $query = ' | |
| { | |
| userRoles { | |
| name | |
| capabilities | |
| } | |
| } | |
| '; | |
| $roles = do_graphql_request( $query ); | |
| $all_capabilities = []; | |
| if ( ! empty( $roles['data']['userRoles'] ) ) { | |
| foreach ( $roles['data']['userRoles'] as $role ) { | |
| foreach ( $role['capabilities'] as $key => $capability ) { | |
| if ( ! isset( $all_capabilities[ $capability ] ) ) { | |
| $all_capabilities[ $capability ] = []; | |
| } | |
| array_push( $all_capabilities[ $capability ], $role['name'] ); | |
| } | |
| } | |
| } | |
| foreach ( $all_capabilities as $cap => $roles ) { | |
| print_r( $cap ); | |
| print_r( '<br/>' ); | |
| } | |
| die(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment