-
Notifications
You must be signed in to change notification settings - Fork 442
/
UserConnectionResolver.php
284 lines (250 loc) · 8.21 KB
/
UserConnectionResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
namespace WPGraphQL\Data\Connection;
use GraphQL\Error\UserError;
use WPGraphQL\Utils\Utils;
/**
* Class UserConnectionResolver
*
* @package WPGraphQL\Data\Connection
*/
class UserConnectionResolver extends AbstractConnectionResolver {
/**
* {@inheritDoc}
*
* A custom class is assumed to have the same core functions as WP_User_Query.
*
* @var \WP_User_Query|object
*/
protected $query;
/**
* {@inheritDoc}
*/
protected function loader_name(): string {
return 'user';
}
/**
* {@inheritDoc}
*
* @throws \Exception
*/
public function get_query_args() {
$query_args = [];
/**
* Prepare for later use
*/
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
/**
* Set the $query_args based on various defaults and primary input $args
*/
$query_args['count_total'] = false;
/**
* Pass the graphql $args to the WP_Query
*/
$query_args['graphql_args'] = $this->args;
/**
* Set the graphql_cursor_compare to determine what direction the
* query should be paginated
*/
$query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';
$query_args['graphql_after_cursor'] = $this->get_after_offset();
$query_args['graphql_before_cursor'] = $this->get_before_offset();
/**
* Set the number, ensuring it doesn't exceed the amount set as the $max_query_amount
*
* We query one extra than what is being asked for so that we can determine if there is a next
* page.
*/
$query_args['number'] = $this->get_query_amount() + 1;
/**
* Take any of the input $args (under the "where" input) that were part of the GraphQL query and map and
* sanitize their GraphQL input to apply to the WP_Query
*/
$input_fields = [];
if ( ! empty( $this->args['where'] ) ) {
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
}
/**
* Merge the default $query_args with the $args that were entered in the query.
*
* @since 0.0.5
*/
if ( ! empty( $input_fields ) ) {
$query_args = array_merge( $query_args, $input_fields );
}
/**
* Only query the IDs and let deferred resolution query the nodes
*/
$query_args['fields'] = 'ID';
/**
* If the request is not authenticated, limit the query to users that have
* published posts, as they're considered publicly facing users.
*/
if ( ! is_user_logged_in() && empty( $query_args['has_published_posts'] ) ) {
$query_args['has_published_posts'] = true;
}
/**
* If `has_published_posts` is set to `attachment`, throw a warning.
*
* @todo Remove this when the `hasPublishedPosts` enum type changes.
*
* @see https://github.com/wp-graphql/wp-graphql/issues/2963
*/
if ( ! empty( $query_args['has_published_posts'] ) && 'attachment' === $query_args['has_published_posts'] ) {
graphql_debug(
__( 'The `hasPublishedPosts` where arg does not support the `ATTACHMENT` value, and will be removed from the possible enum values in a future release.', 'wp-graphql' ),
[
'operationName' => $this->context->operationName ?? '',
'query' => $this->context->query ?? '',
'variables' => $this->context->variables ?? '',
]
);
}
if ( ! empty( $query_args['search'] ) ) {
$query_args['search'] = '*' . $query_args['search'] . '*';
$query_args['orderby'] = 'user_login';
$query_args['order'] = ! empty( $last ) ? 'DESC' : 'ASC';
}
/**
* Map the orderby inputArgs to the WP_User_Query
*/
if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) {
foreach ( $this->args['where']['orderby'] as $orderby_input ) {
/**
* These orderby options should not include the order parameter.
*/
if ( in_array(
$orderby_input['field'],
[
'login__in',
'nicename__in',
],
true
) ) {
$query_args['orderby'] = esc_sql( $orderby_input['field'] );
} elseif ( ! empty( $orderby_input['field'] ) ) {
$order = $orderby_input['order'];
if ( ! empty( $this->args['last'] ) ) {
if ( 'ASC' === $order ) {
$order = 'DESC';
} else {
$order = 'ASC';
}
}
$query_args['orderby'] = esc_sql( $orderby_input['field'] );
$query_args['order'] = esc_sql( $order );
}
}
}
/**
* Convert meta_value_num to separate meta_value value field which our
* graphql_wp_term_query_cursor_pagination_support knowns how to handle
*/
if ( isset( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) {
$query_args['orderby'] = [
'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
];
unset( $query_args['order'] );
$query_args['meta_type'] = 'NUMERIC';
}
/**
* If there's no orderby params in the inputArgs, set order based on the first/last argument
*/
if ( empty( $query_args['order'] ) ) {
$query_args['order'] = ! empty( $last ) ? 'DESC' : 'ASC';
}
return $query_args;
}
/**
* {@inheritDoc}
*
* @return object|\WP_User_Query
* @throws \Exception
*/
public function get_query() {
// Get query class.
$queryClass = ! empty( $this->context->queryClass )
? $this->context->queryClass
: '\WP_User_Query';
return new $queryClass( $this->query_args );
}
/**
* {@inheritDoc}
*
* @return int[]
*/
public function get_ids_from_query() {
$ids = method_exists( $this->query, 'get_results' ) ? $this->query->get_results() : [];
// If we're going backwards, we need to reverse the array.
if ( ! empty( $this->args['last'] ) ) {
$ids = array_reverse( $ids );
}
return $ids;
}
/**
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_User_Query
* friendly keys.
*
* There's probably a cleaner/more dynamic way to approach this, but this was quick. I'd be
* down to explore more dynamic ways to map this, but for now this gets the job done.
*
* @param array<string,mixed> $args The query "where" args
*
* @return array<string,mixed>
* @throws \GraphQL\Error\UserError If the user does not have the "list_users" capability.
* @since 0.0.5
*/
protected function sanitize_input_fields( array $args ) {
/**
* Only users with the "list_users" capability can filter users by roles
*/
if (
(
! empty( $args['roleIn'] ) ||
! empty( $args['roleNotIn'] ) ||
! empty( $args['role'] )
) &&
! current_user_can( 'list_users' )
) {
throw new UserError( esc_html__( 'Sorry, you are not allowed to filter users by role.', 'wp-graphql' ) );
}
$arg_mapping = [
'roleIn' => 'role__in',
'roleNotIn' => 'role__not_in',
'searchColumns' => 'search_columns',
'hasPublishedPosts' => 'has_published_posts',
'nicenameIn' => 'nicename__in',
'nicenameNotIn' => 'nicename__not_in',
'loginIn' => 'login__in',
'loginNotIn' => 'login__not_in',
];
/**
* Map and sanitize the input args to the WP_User_Query compatible args
*/
$query_args = Utils::map_input( $args, $arg_mapping );
/**
* Filter the input fields
*
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
* from a GraphQL Query to the WP_User_Query
*
* @param array<string,mixed> $query_args The mapped query args
* @param array<string,mixed> $args The query "where" args
* @param mixed $source The query results of the query calling this relation
* @param array<string,mixed> $all_args Array of all the query args (not just the "where" args)
* @param \WPGraphQL\AppContext $context The AppContext object
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo object
*
* @since 0.0.5
*/
$query_args = apply_filters( 'graphql_map_input_fields_to_wp_user_query', $query_args, $args, $this->source, $this->args, $this->context, $this->info );
return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : [];
}
/**
* {@inheritDoc}
*
* @param int $offset The ID of the node used as the offset in the cursor.
*/
public function is_valid_offset( $offset ) {
return (bool) get_user_by( 'ID', absint( $offset ) );
}
}