PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Data / Connection / UserRoleConnectionResolver.php

UserRoleConnectionResolver.php in WPGraphQL trunk, at src/Data/Connection/UserRoleConnectionResolver.php

88 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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