Skip to content

Commit 66690f0

Browse files
committed
"Customer_Connection_Resolver" deprecated.
1 parent a98998d commit 66690f0

15 files changed

+896
-821
lines changed

includes/class-core-schema-filters.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public static function add_filters() {
7373
10,
7474
3
7575
);
76+
77+
add_filter(
78+
'graphql_dataloader_get_model',
79+
array( '\WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader', 'inject_user_loader_models' ),
80+
10,
81+
3
82+
);
83+
7684
add_filter(
7785
'graphql_post_object_connection_query_args',
7886
array( '\WPGraphQL\WooCommerce\Connection\Orders', 'post_object_connection_query_args' ),
@@ -100,6 +108,13 @@ public static function add_filters() {
100108
10,
101109
7
102110
);
111+
112+
add_filter(
113+
'graphql_map_input_fields_to_wp_user_query',
114+
array( '\WPGraphQL\WooCommerce\Connection\Customers', 'map_input_fields_to_wp_query' ),
115+
10,
116+
6
117+
);
103118
}
104119

105120
/**

includes/connection/class-customers.php

Lines changed: 118 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use GraphQL\Type\Definition\ResolveInfo;
1313
use WPGraphQL\AppContext;
14+
use WPGraphQL\Data\Connection\UserConnectionResolver;
1415
use WPGraphQL\WooCommerce\Data\Factory;
1516

1617
/**
@@ -23,43 +24,62 @@ class Customers {
2324
*/
2425
public static function register_connections() {
2526
register_graphql_connection(
26-
self::get_connection_config(
27-
array(
28-
'fromType' => 'RootQuery',
29-
'toType' => 'Customer',
30-
'fromFieldName' => 'customers',
31-
)
27+
array(
28+
'fromType' => 'RootQuery',
29+
'toType' => 'Customer',
30+
'fromFieldName' => 'customers',
31+
'connectionArgs' => self::get_connection_args(),
32+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
33+
$resolver = new UserConnectionResolver( $source, $args, $context, $info );
34+
35+
if ( ! self::should_execute() ) {
36+
return array(
37+
'nodes' => array(),
38+
'edges' => array(),
39+
);
40+
}
41+
42+
$resolver->set_query_arg( 'role', 'customer' );
43+
44+
return $resolver->get_connection();
45+
},
3246
)
3347
);
3448

3549
register_graphql_connection(
36-
self::get_connection_config(
37-
array(
38-
'fromType' => 'Coupon',
39-
'toType' => 'Customer',
40-
'fromFieldName' => 'usedBy',
41-
)
50+
array(
51+
'fromType' => 'Coupon',
52+
'toType' => 'Customer',
53+
'fromFieldName' => 'usedBy',
54+
'connectionArgs' => self::get_connection_args(),
55+
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
56+
$resolver = new UserConnectionResolver( $source, $args, $context, $info );
57+
58+
$resolver->set_query_arg( 'include', $source->used_by_ids );
59+
$resolver->set_query_arg( 'role', 'customer' );
60+
61+
if ( ! self::should_execute() ) {
62+
return array();
63+
}
64+
65+
return $resolver->get_connection();
66+
},
4267
)
4368
);
4469
}
4570

4671
/**
47-
* Given an array of $args, this returns the connection config, merging the provided args
48-
* with the defaults
72+
* Confirms the uses has the privileges to query Customer
4973
*
50-
* @param array $args - Connection configuration.
51-
* @return array
74+
* @return bool
5275
*/
53-
public static function get_connection_config( $args ): array {
54-
return array_merge(
55-
array(
56-
'connectionArgs' => self::get_connection_args(),
57-
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
58-
return Factory::resolve_customer_connection( $source, $args, $context, $info );
59-
},
60-
),
61-
$args
62-
);
76+
public static function should_execute() {
77+
switch ( true ) {
78+
case current_user_can( 'list_users' ):
79+
return true;
80+
default:
81+
return false;
82+
}
6383
}
6484

6585
/**
@@ -85,18 +105,6 @@ public static function get_connection_args(): array {
85105
'type' => 'String',
86106
'description' => __( 'Limit result set to resources with a specific email.', 'wp-graphql-woocommerce' ),
87107
),
88-
'role' => array(
89-
'type' => 'UserRoleEnum',
90-
'description' => __( 'Limit result set to resources with a specific role.', 'wp-graphql-woocommerce' ),
91-
),
92-
'roleIn' => array(
93-
'type' => array( 'list_of' => 'UserRoleEnum' ),
94-
'description' => __( 'Limit result set to resources with a specific group of roles.', 'wp-graphql-woocommerce' ),
95-
),
96-
'roleNotIn' => array(
97-
'type' => array( 'list_of' => 'UserRoleEnum' ),
98-
'description' => __( 'Limit result set to resources not within a specific group of roles.', 'wp-graphql-woocommerce' ),
99-
),
100108
'orderby' => array(
101109
'type' => 'CustomerConnectionOrderbyEnum',
102110
'description' => __( 'Order results by a specific field.', 'wp-graphql-woocommerce' ),
@@ -107,4 +115,76 @@ public static function get_connection_args(): array {
107115
),
108116
);
109117
}
118+
119+
/**
120+
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
121+
* from a GraphQL Query to the WP_Query
122+
*
123+
* @param array $query_args The mapped query arguments.
124+
* @param array $where_args Query "where" args.
125+
* @param mixed $source The query results for a query calling this.
126+
* @param array $args All of the arguments for the query (not just the "where" args).
127+
* @param AppContext $context The AppContext object.
128+
* @param ResolveInfo $info The ResolveInfo object.
129+
*
130+
* @return array Query arguments.
131+
*/
132+
public static function map_input_fields_to_wp_query( $query_args, $where_args, $source, $args, $context, $info ) {
133+
134+
$key_mapping = array(
135+
'search' => 'search',
136+
'exclude' => 'exclude',
137+
'include' => 'include',
138+
);
139+
140+
foreach ( $key_mapping as $key => $field ) {
141+
if ( ! empty( $where_args[ $key ] ) ) {
142+
$query_args[ $field ] = $where_args[ $key ];
143+
}
144+
}
145+
146+
// Filter by email.
147+
if ( ! empty( $where_args['email'] ) ) {
148+
$query_args['search'] = $where_args['email'];
149+
$query_args['search_columns'] = array( 'user_email' );
150+
}
151+
152+
/**
153+
* Map the orderby inputArgs to the WP_Query
154+
*/
155+
if ( ! empty( $where_args['orderby'] ) ) {
156+
$query_args['orderby'] = $where_args['orderby'];
157+
}
158+
159+
/**
160+
* Map the orderby inputArgs to the WP_Query
161+
*/
162+
if ( ! empty( $where_args['order'] ) ) {
163+
$query_args['order'] = $where_args['order'];
164+
}
165+
166+
/**
167+
* Filter the input fields
168+
* This allows plugins/themes to hook in and alter what $args should be allowed to be passed
169+
* from a GraphQL Query to the WP_Query
170+
*
171+
* @param array $args The mapped query arguments
172+
* @param array $where_args Query "where" args
173+
* @param mixed $source The query results for a query calling this
174+
* @param array $all_args All of the arguments for the query (not just the "where" args)
175+
* @param AppContext $context The AppContext object
176+
* @param ResolveInfo $info The ResolveInfo object
177+
*/
178+
$query_args = apply_filters(
179+
'graphql_map_input_fields_to_customer_query',
180+
$query_args,
181+
$where_args,
182+
$source,
183+
$args,
184+
$context,
185+
$info
186+
);
187+
188+
return $query_args;
189+
}
110190
}

includes/connection/class-products.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use GraphQL\Type\Definition\ResolveInfo;
1313
use WPGraphQL\AppContext;
14-
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
1514
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
1615

1716
/**

includes/data/connection/class-cart-item-connection-resolver.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
* Class Cart_Item_Connection_Resolver
2222
*/
2323
class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
24-
/**
25-
* Include Db Loader connection common functions.
26-
*/
27-
use WC_Db_Loader_Common;
2824

2925
/**
3026
* Return the name of the loader to be used with the connection resolver

includes/data/connection/class-customer-connection-resolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
/**
1818
* Class Customer_Connection_Resolver
19+
*
20+
* @deprecated v0.10.0
1921
*/
2022
class Customer_Connection_Resolver extends AbstractConnectionResolver {
2123
/**

includes/data/connection/class-downloadable-item-connection-resolver.php

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
* Class Downloadable_Item_Connection_Resolver
2323
*/
2424
class Downloadable_Item_Connection_Resolver extends AbstractConnectionResolver {
25-
/**
26-
* Include Db Loader connection common functions.
27-
*/
28-
use WC_Db_Loader_Common;
29-
30-
const PREFIX = 'DI';
3125

3226
/**
3327
* Return the name of the loader to be used with the connection resolver
@@ -133,15 +127,27 @@ public function get_query() {
133127
}
134128
}
135129

136-
$cursor_key = $this->get_offset();
137-
$cursor_offset = array_search( $cursor_key, array_column( $items, 'download_id' ), true ) ?? null;
130+
$cursor = $this->get_offset();
131+
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
132+
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
138133

139-
if ( ! empty( $this->args['after'] ) ) {
140-
$items = array_splice( $items, $cursor_offset + 1 );
141-
} elseif ( $cursor_offset ) {
142-
$items = array_splice( $items, 0, $cursor_offset );
134+
// MUST DO FOR SANITY ~ If last, reverse list for correct slicing.
135+
if ( $last ) {
136+
$items = array_reverse( $items );
137+
}
138+
139+
// Set offset.
140+
$offset = $cursor
141+
? array_search( $cursor, array_column( $items, 'download_id' ), true )
142+
: 0;
143+
144+
// If cursor set, move index up one to ensure cursor not included in keys.
145+
if ( $cursor ) {
146+
$offset++;
143147
}
144148

149+
$items = array_slice( $items, $offset, $this->query_amount + 1 );
150+
145151
// Cache items for later.
146152
foreach ( $items as $item ) {
147153
$this->loader->prime( $item['download_id'], $item );
@@ -150,36 +156,6 @@ public function get_query() {
150156
return array_column( $items, 'download_id' );
151157
}
152158

153-
/**
154-
* This returns the offset to be used in the $query_args based on the $args passed to the
155-
* GraphQL query.
156-
*
157-
* @return int|mixed
158-
*/
159-
public function get_offset() {
160-
$offset = null;
161-
162-
// Get the offset.
163-
if ( ! empty( $this->args['after'] ) ) {
164-
$offset = $this->cursor_to_offset( self::PREFIX, $this->args['after'] );
165-
} elseif ( ! empty( $this->args['before'] ) ) {
166-
$offset = $this->cursor_to_offset( self::PREFIX, $this->args['before'] );
167-
}
168-
169-
return $offset;
170-
}
171-
172-
/**
173-
* Create cursor for downloadable item node.
174-
*
175-
* @param string $id Downloadable item ID.
176-
*
177-
* @return string
178-
*/
179-
protected function get_cursor_for_node( $id ) {
180-
return $this->offset_to_cursor( self::PREFIX, $id );
181-
}
182-
183159
/**
184160
* Return an array of items from the query
185161
*
@@ -213,4 +189,36 @@ public function is_valid_offset( $offset ) {
213189
protected function is_valid_model( $model ) {
214190
return isset( $model ) && ! empty( $model['download_id'] );
215191
}
192+
193+
194+
/**
195+
* Get_offset
196+
*
197+
* This returns the offset to be used in the $query_args based on the $args passed to the
198+
* GraphQL query.
199+
*
200+
* @return int|mixed
201+
*/
202+
public function get_offset() {
203+
/**
204+
* Defaults
205+
*/
206+
$offset = 0;
207+
208+
/**
209+
* Get the $after offset
210+
*/
211+
if ( ! empty( $this->args['after'] ) ) {
212+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
213+
$offset = substr( base64_decode( $this->args['after'] ), strlen( 'arrayconnection:' ) );
214+
} elseif ( ! empty( $this->args['before'] ) ) {
215+
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
216+
$offset = substr( base64_decode( $this->args['before'] ), strlen( 'arrayconnection:' ) );
217+
}
218+
219+
/**
220+
* Return the higher of the two values
221+
*/
222+
return $offset;
223+
}
216224
}

includes/data/loader/class-wc-customer-loader.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,22 @@ public function loadKeys( array $keys ) {
6565

6666
return $all_customers;
6767
}
68+
69+
/**
70+
* Callback for inject the User dataloader with the WC_Customer model.
71+
*
72+
* @param null $model Possible model instance to be loader.
73+
* @param mixed $entry Data source.
74+
* @param mixed $key Data key/ID.
75+
* @return \WPGraphQL\Model\Model|null
76+
*/
77+
public static function inject_user_loader_models( $model, $entry, $key ) {
78+
if ( is_null( $model ) && is_a( $entry, \WP_User::class ) ) {
79+
if ( in_array( 'customer', (array) $entry->roles ) ) {
80+
$model = new Customer( $key );
81+
}
82+
}
83+
84+
return $model;
85+
}
6886
}

0 commit comments

Comments
 (0)