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

wp-graphql / wp-graphql-woocommerce / 5603659987

pending completion
5603659987

push

github

web-flow
fix: initialize arrays before using (#765)

* fix!: update deps to required versions

* fix; initialize arrays before using

3 of 3 new or added lines in 2 files covered. (100.0%)

10250 of 12440 relevant lines covered (82.4%)

54.02 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 graphql_cursor_offset which is used by Config::graphql_wp_user_query_cursor_pagination_support
65
                 * to filter the WP_User_Query to support cursor pagination
66
                 */
67
                $cursor_offset                        = $this->get_offset();
×
68
                $query_args['graphql_cursor_offset']  = $cursor_offset;
×
69
                $query_args['graphql_cursor_compare'] = ( ! empty( $last ) ) ? '>' : '<';
×
70

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

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

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

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

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

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

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

149
                return $query_args;
×
150
        }
151

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

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

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

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

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

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

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

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

219
                return $args;
×
220
        }
221

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

234
                if ( ! empty( wp_cache_get( $offset, 'users' ) ) ) {
×
235
                        return true;
×
236
                }
237
                // phpcs:ignore WordPress.DB.DirectDatabaseQuery
238
                return $wpdb->get_var( $wpdb->prepare( "SELECT EXISTS (SELECT 1 FROM $wpdb->users WHERE ID = %d)", $offset ) );
×
239
        }
240
}
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