Skip to content

Commit d607531

Browse files
feat: 🎸 ORN-1134 implement default AND relations and add tests
✅ Closes: ORN-1134
1 parent 6f5e92b commit d607531

File tree

2 files changed

+652
-46
lines changed

2 files changed

+652
-46
lines changed

‎src/filter-query.php

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace WPGraphQLFilterQuery;
99

10+
use GraphQL\Type\Definition\ResolveInfo;
11+
use WPGraphQL\AppContext;
12+
1013
/**
1114
* Main class.
1215
*/
@@ -152,11 +155,11 @@ public function extend_wp_graphql_fields() {
152155
[
153156
'description' => __( 'String Field Match Arguments', 'wp-graphql-filter-query' ),
154157
'fields' => [
155-
'in' => [
158+
'in' => [
156159
'type' => [ 'list_of' => 'String' ],
157160
'description' => __( 'For This To Be Truthy, At Least One Item Of The String Array Arg Passed Here Must Be Contained Within The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
158161
],
159-
'notIn' => [
162+
'notIn' => [
160163
'type' => [ 'list_of' => 'String' ],
161164
'description' => __( 'For This To Be Truthy, Not One Item Of The String Array Arg Passed Here Can Be Contained Within The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
162165
],
@@ -168,11 +171,11 @@ public function extend_wp_graphql_fields() {
168171
'type' => 'String',
169172
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Not Relate To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
170173
],
171-
'eq' => [
174+
'eq' => [
172175
'type' => 'String',
173176
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Be An Exact Match To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
174177
],
175-
'notEq' => [
178+
'notEq' => [
176179
'type' => 'String',
177180
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Not Match To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
178181
],
@@ -185,11 +188,11 @@ public function extend_wp_graphql_fields() {
185188
[
186189
'description' => __( 'Integer Field Match Arguments', 'wp-graphql-filter-query' ),
187190
'fields' => [
188-
'in' => [
191+
'in' => [
189192
'type' => [ 'list_of' => 'Integer' ],
190193
'description' => __( 'For This To Be Truthy, At Least One Item Of The String Array Arg Passed Here Must Be Contained Within The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
191194
],
192-
'notIn' => [
195+
'notIn' => [
193196
'type' => [ 'list_of' => 'Integer' ],
194197
'description' => __( 'For This To Be Truthy, Not One Item Of The String Array Arg Passed Here Can Be Contained Within The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
195198
],
@@ -201,11 +204,11 @@ public function extend_wp_graphql_fields() {
201204
'type' => 'Integer',
202205
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Not Relate To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
203206
],
204-
'eq' => [
207+
'eq' => [
205208
'type' => 'Integer',
206209
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Be An Exact Match To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
207210
],
208-
'notEq' => [
211+
'notEq' => [
209212
'type' => 'Integer',
210213
'description' => __( 'For This To Be Truthy, The Arg Passed Here Must Not Match To The Calling Taxonomy Field, By Way Of Predefined Aggregates', 'wp-graphql-filter-query' ),
211214
],
@@ -270,8 +273,18 @@ public function extend_wp_graphql_fields() {
270273
);
271274
}
272275

276+
/**
277+
* Apply facet filters using graphql_post_object_connection_query_args filter hook.
278+
*
279+
* @param array $query_args arguments that come from previous filter and will be passed to WP_Query.
280+
* @param mixed $source Not used.
281+
* @param array $args WPGraphQL input arguments.
282+
* @param AppContext $context Not used.
283+
* @param ResolveInfo $info Not used.
284+
*
285+
* @return array|mixed
286+
*/
273287
public function apply_filters( $query_args, $source, $args, $context, $info ) {
274-
275288
if ( empty( $args['where']['filter'] ) ) {
276289
return $query_args;
277290
}
@@ -283,34 +296,45 @@ public function apply_filters( $query_args, $source, $args, $context, $info ) {
283296
'like' => 'IN',
284297
'notLike' => 'NOT IN',
285298
);
299+
$c = 0;
286300
foreach ( $args['where']['filter'] as $taxonomy_input => $data ) {
287-
$field_name = array_key_first( $data );
288-
$operator = array_key_first( $data[ $field_name ] );
289-
$terms = $data[ $field_name ][ $operator];
290-
$mapped_operator = $operator_mappings[ $operator ] ?? 'IN';
291-
$is_like_operator = $this->exists_like_operator($operator);
292-
$taxonomy = $taxonomy_input === 'tag' ? 'post_tag' : 'category';
301+
foreach ( $data as $field_name => $field_data ) {
302+
foreach ( $field_data as $operator => $terms ) {
303+
$mapped_operator = $operator_mappings[ $operator ] ?? 'IN';
304+
$is_like_operator = $this->exists_like_operator( $operator );
305+
$taxonomy = $taxonomy_input === 'tag' ? 'post_tag' : 'category';
293306

294-
$terms = ! $is_like_operator ? $terms : get_terms( [ // Get terms "%LIKE%"
295-
'name__like' => esc_attr( $terms ),
296-
'fields' => 'ids',
297-
'taxonomy' => $taxonomy
298-
] );
307+
$terms = ! $is_like_operator ? $terms : get_terms(
308+
array(
309+
'name__like' => esc_attr( $terms ),
310+
'fields' => 'ids',
311+
'taxonomy' => $taxonomy,
312+
)
313+
);
299314

300-
$result = array(
301-
'terms' => $terms,
302-
'taxonomy' => $taxonomy,
303-
'operator' => $mapped_operator,
304-
'field' => ( $field_name === 'id' || $is_like_operator ) ? 'term_id' : 'name',
305-
);
315+
$result = array(
316+
'terms' => $terms,
317+
'taxonomy' => $taxonomy,
318+
'operator' => $mapped_operator,
319+
'field' => ( $field_name === 'id' || $is_like_operator ) ? 'term_id' : 'name',
320+
);
306321

307-
$query_args['tax_query'][] = $result;
322+
$query_args['tax_query'][] = $result;
323+
$c ++;
324+
}
325+
}
308326
}
309327

310-
return $query_args ;
328+
if ( $c > 1 ) {
329+
$query_args['tax_query']['relation'] = 'AND';
330+
}
331+
332+
return $query_args;
311333
}
312334

313335
/**
336+
* Check if operator is like or notLike
337+
*
314338
* @param string $operator Received operator - not mapped.
315339
*
316340
* @return bool

0 commit comments

Comments
 (0)