• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 14788432316

01 May 2025 06:46PM UTC coverage: 83.59% (-0.03%) from 83.617%
14788432316

push

github

web-flow
Fix/recursive interface definitions (#934)

* fix: recursive interface definitions for Product, etc.

* devops: CI & Linter compliances met

* devops: lint-code.yml updated

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>

7 of 9 new or added lines in 8 files covered. (77.78%)

8 existing lines in 2 files now uncovered.

12424 of 14863 relevant lines covered (83.59%)

72.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/includes/data/connection/class-customer-connection-resolver.php
1
<?php
2
/**
3
 * ConnectionResolver - Customer_Connection_Resolver
4
 *
5
 * Resolves connections to Customers
6
 *
7
 * @package WPGraphQL\WooCommerce\Data\Connection
8
 * @since 0.0.1
9
 */
10

11
namespace WPGraphQL\WooCommerce\Data\Connection;
12

13
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
14
use WPGraphQL\WooCommerce\Model\Coupon;
15

16
/**
17
 * Class Customer_Connection_Resolver
18
 *
19
 * @deprecated v0.10.0
20
 */
21
class Customer_Connection_Resolver extends AbstractConnectionResolver {
22
        /**
23
         * Return the name of the loader to be used with the connection resolver
24
         *
25
         * @return string
26
         */
27
        public function get_loader_name() {
28
                return 'wc_customer';
×
29
        }
30

31
        /**
32
         * Confirms the uses has the privileges to query Customers
33
         *
34
         * @return bool
35
         */
36
        public function should_execute() {
37
                switch ( true ) {
38
                        case current_user_can( 'list_users' ):
×
39
                                return true;
×
40
                        default:
41
                                return false;
×
42
                }
43
        }
44

45
        /**
46
         * Creates query arguments array
47
         */
48
        public function get_query_args() {
49
                /**
50
                 * Prepare for later use
51
                 */
52
                $last       = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
×
53
                $query_args = [];
×
54

55
                /**
56
                 * Set the $query_args based on various defaults and primary input $args
57
                 */
58
                $query_args['count_total'] = false;
×
59
                $query_args['orderby']     = 'ID';
×
60
                $query_args['order']       = ! empty( $this->args['last'] ) ? 'ASC' : 'DESC';
×
61
                $query_args['number']      = $this->get_query_amount() + 1;
×
62

63
                /**
64
                 * Set the cursor args.
65
                 *
66
                 * @see \WPGraphQL\Data\Config::graphql_wp_query_cursor_pagination_support
67
                 */
68
                $query_args['graphql_after_cursor']   = $this->get_after_offset();
×
69
                $query_args['graphql_before_cursor']  = $this->get_before_offset();
×
70
                $query_args['graphql_cursor_compare'] = ! empty( $last ) ? '>' : '<';
×
71

72
                $input_fields = [];
×
73
                if ( ! empty( $this->args['where'] ) ) {
×
74
                        $input_fields = $this->sanitize_input_fields( $this->args['where'] );
×
75
                }
76
                if ( ! empty( $input_fields ) ) {
×
77
                        $query_args = array_merge( $query_args, $input_fields );
×
78
                }
79

80
                if ( true === is_object( $this->source ) ) {
×
81
                        switch ( true ) {
82
                                case is_a( $this->source, Coupon::class ):
×
83
                                        if ( 'usedBy' === $this->info->fieldName ) {
×
84
                                                $query_args['include'] = ! empty( $query_args['include'] )
×
85
                                                        ? array_merge( $query_args['include'], $this->source->used_by_ids )
×
86
                                                        : $this->source->used_by_ids;
×
87
                                        }
88
                                        break;
×
89
                                default:
90
                                        break;
×
91
                        }
92
                }
93

94
                $query_args['fields'] = 'ID';
×
95

96
                /**
97
                 * Map the orderby inputArgs to the WP_User_Query
98
                 */
99
                if ( ! empty( $this->args['where']['orderby'] ) && is_array( $this->args['where']['orderby'] ) ) {
×
100
                        $query_args['orderby'] = [];
×
101
                        foreach ( $this->args['where']['orderby'] as $orderby_input ) {
×
102
                                /**
103
                                 * These orderby options should not include the order parameter.
104
                                 */
105
                                if ( in_array( $orderby_input['field'], [ 'login__in', 'nicename__in' ], true ) ) {
×
106
                                        $query_args['orderby'] = esc_sql( $orderby_input['field'] );
×
107
                                } elseif ( ! empty( $orderby_input['field'] ) ) {
×
108
                                        $query_args['orderby'] = [ $orderby_input['field'] => $orderby_input['order'] ];
×
109
                                }
110
                        }
111
                }
112

113
                /**
114
                 * Convert meta_value_num to seperate meta_value value field which our
115
                 * graphql_wp_term_query_cursor_pagination_support knowns how to handle
116
                 */
117
                if ( isset( $query_args['orderby'] ) && 'meta_value_num' === $query_args['orderby'] ) {
×
118
                        $query_args['orderby'] = [
×
119
                                //phpcs:ignore WordPress.DB.SlowDBQuery
120
                                'meta_value' => empty( $query_args['order'] ) ? 'DESC' : $query_args['order'],
×
121
                        ];
×
122
                        unset( $query_args['order'] );
×
123
                        $query_args['meta_type'] = 'NUMERIC';
×
124
                }
125
                /**
126
                 * If there's no orderby params in the inputArgs, set order based on the first/last argument
127
                 */
128
                if ( empty( $query_args['orderby'] ) ) {
×
129
                        $query_args['order'] = ! empty( $last ) ? 'ASC' : 'DESC';
×
130
                }
131

132
                if (
133
                        empty( $query_args['role'] ) &&
×
134
                        empty( $query_args['role__in'] ) &&
×
135
                        empty( $query_args['role__not_in'] )
×
136
                ) {
137
                        $query_args['role'] = 'customer';
×
138
                }
139

140
                $query_args = apply_filters(
×
141
                        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
142
                        'graphql_customer_connection_query_args',
×
143
                        $query_args,
×
144
                        $this->source,
×
145
                        $this->args,
×
146
                        $this->context,
×
147
                        $this->info
×
148
                );
×
149

150
                return $query_args;
×
151
        }
152

153
        /**
154
         * Executes query
155
         *
156
         * @return \WP_User_Query
157
         */
158
        public function get_query() {
159
                return new \WP_User_Query( $this->get_query_args() );
×
160
        }
161

162
        /**
163
         * Returns an array of items from the query
164
         *
165
         * @return array
166
         */
167
        public function get_ids() {
168
                $results = $this->get_query()->get_results();
×
169
                return ! empty( $results ) ? $results : [];
×
170
        }
171

172
        /**
173
         * This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query
174
         * friendly keys. There's probably a cleaner/more dynamic way to approach this, but
175
         * this was quick. I'd be down to explore more dynamic ways to map this, but for
176
         * now this gets the job done.
177
         *
178
         * @param array $where_args - arguments being used to filter query.
179
         *
180
         * @return array
181
         */
182
        public function sanitize_input_fields( array $where_args ) {
183
                $args = [];
×
184

185
                $key_mapping = [
×
186
                        'search'    => 'search',
×
187
                        'exclude'   => 'exclude',
×
188
                        'include'   => 'include',
×
189
                        'role'      => 'role',
×
190
                        'roleIn'    => 'role__in',
×
191
                        'roleNotIn' => 'role__not_in',
×
192
                ];
×
193

194
                foreach ( $key_mapping as $key => $field ) {
×
195
                        if ( ! empty( $where_args[ $key ] ) ) {
×
196
                                $args[ $field ] = $where_args[ $key ];
×
197
                        }
198
                }
199

200
                // Filter by email.
201
                if ( ! empty( $where_args['email'] ) ) {
×
202
                        $args['search']         = $where_args['email'];
×
203
                        $args['search_columns'] = [ 'user_email' ];
×
204
                }
205

206
                /**
207
                 * Map the orderby inputArgs to the WP_Query
208
                 */
209
                if ( ! empty( $where_args['orderby'] ) ) {
×
210
                        $args['orderby'] = $where_args['orderby'];
×
211
                }
212

213
                /**
214
                 * Map the orderby inputArgs to the WP_Query
215
                 */
216
                if ( ! empty( $where_args['order'] ) ) {
×
217
                        $args['order'] = $where_args['order'];
×
218
                }
219

220
                return $args;
×
221
        }
222

223
        /**
224
         * Determine whether or not the the offset is valid, i.e the user corresponding to the offset exists.
225
         * Offset is equivalent to user_id. So this function is equivalent
226
         * to checking if the user with the given ID exists.
227
         *
228
         * @param integer $offset  User ID.
229
         *
230
         * @return bool
231
         */
232
        public function is_valid_offset( $offset ) {
233
                global $wpdb;
×
234

235
                if ( ! empty( wp_cache_get( $offset, 'users' ) ) ) {
×
236
                        return true;
×
237
                }
238
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery
239
                return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $offset ) );
×
240
        }
241
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc