|
12 | 12 |
|
13 | 13 | use GraphQL\Type\Definition\ResolveInfo; |
14 | 14 | use GraphQLRelay\Relay; |
| 15 | +use GraphQLRelay\Connection\ArrayConnection; |
15 | 16 | use WPGraphQL\AppContext; |
| 17 | +use WPGraphQL\Data\Connection\AbstractConnectionResolver; |
16 | 18 |
|
17 | 19 | /** |
18 | 20 | * Class Cart_Item_Connection_Resolver |
19 | 21 | */ |
20 | | -class Cart_Item_Connection_Resolver { |
| 22 | +class Cart_Item_Connection_Resolver extends AbstractConnectionResolver { |
21 | 23 | /** |
22 | | - * Returns an array of cart items filtered based upon query arguments |
| 24 | + * Confirms if cart items should be retrieved. |
23 | 25 | * |
24 | | - * @param array $items - Cart items. |
25 | | - * @param array $args - Query arguments. |
26 | | - * |
27 | | - * @return array |
| 26 | + * @return bool |
28 | 27 | */ |
29 | | - public function filter( $items, $args = array() ) { |
30 | | - $filter_items = array_values( $items ); |
| 28 | + public function should_execute() { |
| 29 | + return true; |
| 30 | + } |
31 | 31 |
|
32 | | - usort( |
33 | | - $filter_items, |
34 | | - function( $item_a, $item_b ) { |
35 | | - return strcmp( $item_a['key'], $item_b['key'] ); |
| 32 | + /** |
| 33 | + * Creates query arguments array |
| 34 | + */ |
| 35 | + public function get_query_args() { |
| 36 | + $query_args = array(); |
| 37 | + if ( ! empty( $this->args['where'] ) ) { |
| 38 | + $where_args = $this->args['where']; |
| 39 | + if ( ! empty( $where_args['needShipping'] ) ) { |
| 40 | + $query_args['filters'] = array(); |
| 41 | + $query_args['filters'][] = function( $cart_item ) { |
| 42 | + $product = \WC()->product_factory->get_product( $cart_item['product_id'] ); |
| 43 | + if ( $product ) { |
| 44 | + return $product->needs_shipping(); |
| 45 | + } |
| 46 | + }; |
36 | 47 | } |
37 | | - ); |
| 48 | + } |
38 | 49 |
|
39 | | - return $filter_items; |
| 50 | + return $query_args; |
40 | 51 | } |
41 | 52 |
|
42 | 53 | /** |
43 | | - * Creates connection |
| 54 | + * Executes query |
44 | 55 | * |
45 | | - * @param mixed $source - Connection source Model instance. |
46 | | - * @param array $args - Connection arguments. |
47 | | - * @param AppContext $context - AppContext object. |
48 | | - * @param ResolveInfo $info - ResolveInfo object. |
| 56 | + * @return \WP_Query |
49 | 57 | */ |
50 | | - public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) { |
51 | | - $items = $this->filter( $source->get_cart(), $args ); |
52 | | - |
53 | | - $connection = Relay::connectionFromArray( $items, $args ); |
54 | | - $nodes = array(); |
55 | | - if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) { |
56 | | - foreach ( $connection['edges'] as $edge ) { |
57 | | - $nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null; |
| 58 | + public function get_query() { |
| 59 | + $cart_items = array_values( $this->source->get_cart() ); |
| 60 | + |
| 61 | + if ( ! empty( $this->query_args['filters'] ) ) { |
| 62 | + if ( is_array( $this->query_args['filters'] ) ) { |
| 63 | + foreach ( $this->query_args['filters'] as $filter ) { |
| 64 | + $cart_items = array_filter( $cart_items, $filter ); |
| 65 | + } |
| 66 | + } else { |
| 67 | + $cart_items = array_filter( $cart_items, $this->query_args['filters'] ); |
58 | 68 | } |
59 | 69 | } |
60 | | - $connection['nodes'] = ! empty( $nodes ) ? $nodes : array(); |
61 | | - return $connection; |
| 70 | + |
| 71 | + $cursor_key = $this->get_offset(); |
| 72 | + $cursor_offset = array_search( $cursor_key, \array_column( $cart_items, 'key' ), true ); |
| 73 | + |
| 74 | + if ( ! empty( $this->args['after'] ) ) { |
| 75 | + $cart_items = array_splice( $cart_items, $cursor_offset + 1 ); |
| 76 | + } elseif ( $cursor_offset ) { |
| 77 | + $cart_items = array_splice( $cart_items, 0, $cursor_offset ); |
| 78 | + } |
| 79 | + |
| 80 | + return array_values( $cart_items ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * This returns the offset to be used in the $query_args based on the $args passed to the |
| 85 | + * GraphQL query. |
| 86 | + * |
| 87 | + * @return int|mixed |
| 88 | + */ |
| 89 | + public function get_offset() { |
| 90 | + $offset = null; |
| 91 | + |
| 92 | + // Get the offset. |
| 93 | + if ( ! empty( $this->args['after'] ) ) { |
| 94 | + $offset = $this->args['after']; |
| 95 | + } elseif ( ! empty( $this->args['before'] ) ) { |
| 96 | + $offset = $this->args['before']; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return the higher of the two values |
| 101 | + */ |
| 102 | + return $offset; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Create cursor for cart item node. |
| 107 | + * |
| 108 | + * @param array $node Cart item. |
| 109 | + * @param string $key Cart item key. |
| 110 | + * |
| 111 | + * @return string |
| 112 | + */ |
| 113 | + protected function get_cursor_for_node( $node, $key = null ) { |
| 114 | + return $node['key']; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Return an array of items from the query |
| 119 | + * |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + public function get_items() { |
| 123 | + return ! empty( $this->query ) ? $this->query : array(); |
62 | 124 | } |
63 | 125 | } |
0 commit comments