Skip to content

Commit 4ff4207

Browse files
committed
wp-graphql/wp-graphql#1111 connection patch
1 parent b487931 commit 4ff4207

10 files changed

+166
-14
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
* Class Cart_Item_Connection_Resolver
2121
*/
2222
class Cart_Item_Connection_Resolver extends AbstractConnectionResolver {
23+
/**
24+
* Include shared connection functions.
25+
*/
26+
use WC_Connection_Functions;
27+
2328
/**
2429
* Confirms if cart items should be retrieved.
2530
*
@@ -129,4 +134,15 @@ protected function get_cursor_for_node( $node, $key = null ) {
129134
public function get_items() {
130135
return ! empty( $this->query ) ? $this->query : array();
131136
}
137+
138+
/**
139+
* Wrapper for "WC_Connection_Functions::is_valid_cart_item_offset()"
140+
*
141+
* @param integer $offset Post ID.
142+
*
143+
* @return bool
144+
*/
145+
public function is_valid_offset( $offset ) {
146+
return $this->is_valid_cart_item_offset( $offset );
147+
}
132148
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
* Class Coupon_Connection_Resolver
2020
*/
2121
class Coupon_Connection_Resolver extends AbstractConnectionResolver {
22-
use Common_CPT_Input_Sanitize_Functions;
22+
/**
23+
* Include shared connection functions.
24+
*/
25+
use WC_Connection_Functions;
2326

2427
/**
2528
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -105,7 +108,7 @@ public function get_query_args() {
105108
/**
106109
* Collect the input_fields and sanitize them to prepare them for sending to the WP_Query
107110
*/
108-
$input_fields = [];
111+
$input_fields = array();
109112
if ( ! empty( $this->args['where'] ) ) {
110113
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
111114
}
@@ -163,7 +166,7 @@ public function get_query() {
163166
* @return array
164167
*/
165168
public function get_items() {
166-
return ! empty( $this->query->posts ) ? $this->query->posts : [];
169+
return ! empty( $this->query->posts ) ? $this->query->posts : array();
167170
}
168171

169172
/**
@@ -222,4 +225,15 @@ public function sanitize_input_fields( array $where_args ) {
222225

223226
return $args;
224227
}
228+
229+
/**
230+
* Wrapper for "WC_Connection_Functions::is_valid_post_offset()"
231+
*
232+
* @param integer $offset Post ID.
233+
*
234+
* @return bool
235+
*/
236+
public function is_valid_offset( $offset ) {
237+
return $this->is_valid_post_offset( $offset );
238+
}
225239
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
* Class Customer_Connection_Resolver
1919
*/
2020
class Customer_Connection_Resolver extends AbstractConnectionResolver {
21+
/**
22+
* Include shared connection functions.
23+
*/
24+
use WC_Connection_Functions;
25+
2126
/**
2227
* Confirms the uses has the privileges to query Customers
2328
*
@@ -150,4 +155,15 @@ public function sanitize_input_fields( array $where_args ) {
150155

151156
return $args;
152157
}
158+
159+
/**
160+
* Wrapper for "WC_Connection_Functions::is_valid_user_offset()"
161+
*
162+
* @param integer $offset User ID.
163+
*
164+
* @return bool
165+
*/
166+
public function is_valid_offset( $offset ) {
167+
return $this->is_valid_user_offset( $offset );
168+
}
153169
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
* Class Order_Connection_Resolver
2020
*/
2121
class Order_Connection_Resolver extends AbstractConnectionResolver {
22-
use Common_CPT_Input_Sanitize_Functions;
22+
/**
23+
* Include shared connection functions.
24+
*/
25+
use WC_Connection_Functions;
2326

2427
/**
2528
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -274,4 +277,15 @@ public function sanitize_input_fields( array $where_args ) {
274277

275278
return $args;
276279
}
280+
281+
/**
282+
* Wrapper for "WC_Connection_Functions::is_valid_post_offset()"
283+
*
284+
* @param integer $offset Post ID.
285+
*
286+
* @return bool
287+
*/
288+
public function is_valid_offset( $offset ) {
289+
return $this->is_valid_post_offset( $offset );
290+
}
277291
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* Class Product_Connection_Resolver
2323
*/
2424
class Product_Connection_Resolver extends AbstractConnectionResolver {
25-
use Common_CPT_Input_Sanitize_Functions;
25+
/**
26+
* Include shared connection functions.
27+
*/
28+
use WC_Connection_Functions;
2629

2730
/**
2831
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -607,4 +610,15 @@ public function sanitize_input_fields( array $where_args ) {
607610

608611
return $args;
609612
}
613+
614+
/**
615+
* Wrapper for "WC_Connection_Functions::is_valid_post_offset()"
616+
*
617+
* @param integer $offset Post ID.
618+
*
619+
* @return bool
620+
*/
621+
public function is_valid_offset( $offset ) {
622+
return $this->$this->is_valid_post_offset( $offset );
623+
}
610624
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
* Class Refund_Connection_Resolver
2121
*/
2222
class Refund_Connection_Resolver extends AbstractConnectionResolver {
23-
use Common_CPT_Input_Sanitize_Functions;
23+
/**
24+
* Include shared connection functions.
25+
*/
26+
use WC_Connection_Functions;
2427

2528
/**
2629
* The name of the post type, or array of post types the connection resolver is resolving for
@@ -247,4 +250,15 @@ public function sanitize_input_fields( array $where_args ) {
247250

248251
return $args;
249252
}
253+
254+
/**
255+
* Wrapper for "WC_Connection_Functions::is_valid_post_offset()"
256+
*
257+
* @param integer $offset Post ID.
258+
*
259+
* @return bool
260+
*/
261+
public function is_valid_offset( $offset ) {
262+
return $this->is_valid_post_offset( $offset );
263+
}
250264
}

includes/data/connection/class-tax-rate-connection-resolver.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function get_query_args() {
4242
/**
4343
* Collect the input_fields and sanitize them to prepare them for sending to the WP_Query
4444
*/
45-
$input_fields = [];
45+
$input_fields = array();
4646
if ( ! empty( $this->args['where'] ) ) {
4747
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
4848
}
@@ -136,7 +136,7 @@ function( $rate ) {
136136
* @return array
137137
*/
138138
public function get_items() {
139-
return ! empty( $this->query ) ? $this->query : [];
139+
return ! empty( $this->query ) ? $this->query : array();
140140
}
141141

142142
/**
@@ -172,4 +172,17 @@ public function sanitize_input_fields( array $where_args ) {
172172

173173
return $args;
174174
}
175+
176+
/**
177+
* Stub function
178+
*
179+
* @todo Implement pagination on this connection.
180+
*
181+
* @param integer $offset Tax rate index.
182+
*
183+
* @return bool
184+
*/
185+
public function is_valid_offset( $offset ) {
186+
return true;
187+
}
175188
}

includes/data/connection/trait-common-cpt-args-processing.php renamed to includes/data/connection/trait-wc-connection-functions.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
11
<?php
22
/**
3-
* Defines reusable function for sanitizing user input for post-type connections.
3+
* Defines reusable functions for all connection.
44
*
55
* @package WPGraphQL\WooCommerce\Data\Connection
6-
* @since 0.2.2
6+
* @since 0.3.3
77
*/
88

99
namespace WPGraphQL\WooCommerce\Data\Connection;
1010

1111
/**
12-
* Trait Common_CPT_Input_Sanitize_Functions
12+
* Trait WC_Connection_Functions
1313
*/
14-
trait Common_CPT_Input_Sanitize_Functions {
14+
trait WC_Connection_Functions {
15+
/**
16+
* Determine whether or not the the offset is valid, i.e the post corresponding to the offset exists.
17+
* Offset is equivalent to post_id. So this function is equivalent
18+
* to checking if the post with the given ID exists.
19+
*
20+
* @param integer $offset Post ID.
21+
*
22+
* @return bool
23+
*/
24+
public function is_valid_post_offset( $offset ) {
25+
global $wpdb;
26+
27+
if ( ! empty( wp_cache_get( $offset, 'posts' ) ) ) {
28+
return true;
29+
}
30+
31+
return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->posts WHERE ID = %d)", $offset ) );
32+
}
33+
34+
/**
35+
* Determine whether or not the the offset is valid, i.e the cart item corresponding to the offset exists.
36+
* Offset is equivalent to a cart item key. So this function is equivalent
37+
* to checking if the cart item with the given key exists.
38+
*
39+
* @param string $offset Cart item key.
40+
*
41+
* @return bool
42+
*/
43+
public function is_valid_cart_item_offset( $offset ) {
44+
return ! empty( WC()->cart->get_cart_item( $offset ) );
45+
}
46+
47+
/**
48+
* Determine whether or not the the offset is valid, i.e the user corresponding to the offset exists.
49+
* Offset is equivalent to user_id. So this function is equivalent
50+
* to checking if the user with the given ID exists.
51+
*
52+
* @param integer $offset User ID.
53+
*
54+
* @return bool
55+
*/
56+
public function is_valid_user_offset( $offset ) {
57+
global $wpdb;
58+
59+
if ( ! empty( wp_cache_get( $offset, 'users' ) ) ) {
60+
return true;
61+
}
62+
63+
return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $offset ) );
64+
}
65+
1566
/**
1667
* Sanitizes common post-type connection query input.
1768
*

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
'WPGraphQL\\WooCommerce\\Connection\\WC_Terms' => $baseDir . '/includes/connection/class-wc-terms.php',
2828
'WPGraphQL\\WooCommerce\\Core_Schema_Filters' => $baseDir . '/includes/class-core-schema-filters.php',
2929
'WPGraphQL\\WooCommerce\\Data\\Connection\\Cart_Item_Connection_Resolver' => $baseDir . '/includes/data/connection/class-cart-item-connection-resolver.php',
30-
'WPGraphQL\\WooCommerce\\Data\\Connection\\Common_CPT_Input_Sanitize_Functions' => $baseDir . '/includes/data/connection/trait-common-cpt-args-processing.php',
3130
'WPGraphQL\\WooCommerce\\Data\\Connection\\Coupon_Connection_Resolver' => $baseDir . '/includes/data/connection/class-coupon-connection-resolver.php',
3231
'WPGraphQL\\WooCommerce\\Data\\Connection\\Customer_Connection_Resolver' => $baseDir . '/includes/data/connection/class-customer-connection-resolver.php',
3332
'WPGraphQL\\WooCommerce\\Data\\Connection\\Order_Connection_Resolver' => $baseDir . '/includes/data/connection/class-order-connection-resolver.php',
@@ -40,6 +39,7 @@
4039
'WPGraphQL\\WooCommerce\\Data\\Connection\\Shipping_Method_Connection_Resolver' => $baseDir . '/includes/data/connection/class-shipping-method-connection-resolver.php',
4140
'WPGraphQL\\WooCommerce\\Data\\Connection\\Tax_Rate_Connection_Resolver' => $baseDir . '/includes/data/connection/class-tax-rate-connection-resolver.php',
4241
'WPGraphQL\\WooCommerce\\Data\\Connection\\Variation_Attribute_Connection_Resolver' => $baseDir . '/includes/data/connection/class-variation-attribute-connection-resolver.php',
42+
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Connection_Functions' => $baseDir . '/includes/data/connection/trait-wc-connection-functions.php',
4343
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Terms_Connection_Resolver' => $baseDir . '/includes/data/connection/class-wc-terms-connection-resolver.php',
4444
'WPGraphQL\\WooCommerce\\Data\\Factory' => $baseDir . '/includes/data/class-factory.php',
4545
'WPGraphQL\\WooCommerce\\Data\\Loader\\WC_Customer_Loader' => $baseDir . '/includes/data/loader/class-wc-customer-loader.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class ComposerStaticInit8ad1f343d0cd8163c5fbabd591f04ce0
5555
'WPGraphQL\\WooCommerce\\Connection\\WC_Terms' => __DIR__ . '/../..' . '/includes/connection/class-wc-terms.php',
5656
'WPGraphQL\\WooCommerce\\Core_Schema_Filters' => __DIR__ . '/../..' . '/includes/class-core-schema-filters.php',
5757
'WPGraphQL\\WooCommerce\\Data\\Connection\\Cart_Item_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-cart-item-connection-resolver.php',
58-
'WPGraphQL\\WooCommerce\\Data\\Connection\\Common_CPT_Input_Sanitize_Functions' => __DIR__ . '/../..' . '/includes/data/connection/trait-common-cpt-args-processing.php',
5958
'WPGraphQL\\WooCommerce\\Data\\Connection\\Coupon_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-coupon-connection-resolver.php',
6059
'WPGraphQL\\WooCommerce\\Data\\Connection\\Customer_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-customer-connection-resolver.php',
6160
'WPGraphQL\\WooCommerce\\Data\\Connection\\Order_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-order-connection-resolver.php',
@@ -68,6 +67,7 @@ class ComposerStaticInit8ad1f343d0cd8163c5fbabd591f04ce0
6867
'WPGraphQL\\WooCommerce\\Data\\Connection\\Shipping_Method_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-shipping-method-connection-resolver.php',
6968
'WPGraphQL\\WooCommerce\\Data\\Connection\\Tax_Rate_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-tax-rate-connection-resolver.php',
7069
'WPGraphQL\\WooCommerce\\Data\\Connection\\Variation_Attribute_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-variation-attribute-connection-resolver.php',
70+
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Connection_Functions' => __DIR__ . '/../..' . '/includes/data/connection/trait-wc-connection-functions.php',
7171
'WPGraphQL\\WooCommerce\\Data\\Connection\\WC_Terms_Connection_Resolver' => __DIR__ . '/../..' . '/includes/data/connection/class-wc-terms-connection-resolver.php',
7272
'WPGraphQL\\WooCommerce\\Data\\Factory' => __DIR__ . '/../..' . '/includes/data/class-factory.php',
7373
'WPGraphQL\\WooCommerce\\Data\\Loader\\WC_Customer_Loader' => __DIR__ . '/../..' . '/includes/data/loader/class-wc-customer-loader.php',

0 commit comments

Comments
 (0)