| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Data\Connection; |
| 4 |
|
| 5 |
use WPGraphQL\Model\User; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class PluginConnectionResolver - Connects plugins to other objects |
| 9 |
* |
| 10 |
* @package WPGraphQL\Data\Resolvers |
| 11 |
* @since 0.0.5 |
| 12 |
* @extends \WPGraphQL\Data\Connection\AbstractConnectionResolver<string[]> |
| 13 |
*/ |
| 14 |
class UserRoleConnectionResolver extends AbstractConnectionResolver { |
| 15 |
/** |
| 16 |
* {@inheritDoc} |
| 17 |
*/ |
| 18 |
public function get_ids_from_query() { |
| 19 |
|
| 20 |
// Given a list of role slugs |
| 21 |
$query_args = $this->get_query_args(); |
| 22 |
if ( isset( $query_args['slugIn'] ) ) { |
| 23 |
return $query_args['slugIn']; |
| 24 |
} |
| 25 |
|
| 26 |
$ids = []; |
| 27 |
$queried = $this->get_query(); |
| 28 |
|
| 29 |
if ( empty( $queried ) ) { |
| 30 |
return $ids; |
| 31 |
} |
| 32 |
|
| 33 |
foreach ( $queried as $key => $item ) { |
| 34 |
$ids[ $key ] = $item; |
| 35 |
} |
| 36 |
|
| 37 |
return $ids; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritDoc} |
| 42 |
*/ |
| 43 |
protected function prepare_query_args( array $args ): array { |
| 44 |
// If any args are added to filter/sort the connection |
| 45 |
return []; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* {@inheritDoc} |
| 50 |
*/ |
| 51 |
protected function query( array $query_args ) { |
| 52 |
$wp_roles = wp_roles(); |
| 53 |
|
| 54 |
return ! empty( $wp_roles->get_names() ) ? array_keys( $wp_roles->get_names() ) : []; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritDoc} |
| 59 |
*/ |
| 60 |
protected function loader_name(): string { |
| 61 |
return 'user_role'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritDoc} |
| 66 |
*/ |
| 67 |
public function is_valid_offset( $offset ) { |
| 68 |
return (bool) get_role( $offset ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* {@inheritDoc} |
| 73 |
*/ |
| 74 |
public function should_execute() { |
| 75 |
if ( |
| 76 |
current_user_can( 'list_users' ) || |
| 77 |
( |
| 78 |
$this->source instanceof User && |
| 79 |
get_current_user_id() === $this->source->databaseId |
| 80 |
) |
| 81 |
) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
|