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

wp-graphql / wp-graphql-woocommerce / 27386231983

12 Jun 2026 12:25AM UTC coverage: 91.791%. Remained the same
27386231983

Pull #1019

github

web-flow
Merge 46a421a18 into 01876f534
Pull Request #1019: fix: address WordPress.org plugin review (rename + prefixing + headers)

1327 of 1584 new or added lines in 200 files covered. (83.78%)

1 existing line in 1 file now uncovered.

18505 of 20160 relevant lines covered (91.79%)

151.6 hits per line

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

89.62
/includes/type/object/class-customer-type.php
1
<?php
2
/**
3
 * WPObject Type - Customer_Type
4
 *
5
 * Registers WPObject type for WooCommerce customers
6
 *
7
 * @package WPGraphQL\WooCommerce\Type\WPObject
8
 * @since   0.0.1
9
 */
10

11
namespace WPGraphQL\WooCommerce\Type\WPObject;
12

13
use GraphQL\Deferred;
14
use GraphQL\Error\UserError;
15
use GraphQL\Type\Definition\ResolveInfo;
16
use WPGraphQL\AppContext;
17
use WPGraphQL\WooCommerce\Data\Connection\Downloadable_Item_Connection_Resolver;
18
use WPGraphQL\WooCommerce\Data\Factory;
19

20
/**
21
 * Class Customer_Type
22
 */
23
class Customer_Type {
24
        /**
25
         * Returns the "Customer" type fields.
26
         *
27
         * @param array $other_fields Extra fields configs to be added or override the default field definitions.
28
         *
29
         * @return array
30
         */
31
        public static function get_fields( $other_fields = [] ) {
32
                return array_merge(
296✔
33
                        [
296✔
34
                                'id'                    => [
296✔
35
                                        'type'        => [ 'non_null' => 'ID' ],
296✔
36
                                        'description' => static function () {
296✔
37
                                                return __( 'The globally unique identifier for the customer', 'graphql-for-ecommerce' );
2✔
38
                                        },
296✔
39
                                ],
296✔
40
                                'databaseId'            => [
296✔
41
                                        'type'        => 'Int',
296✔
42
                                        'description' => static function () {
296✔
43
                                                return __( 'The ID of the customer in the database', 'graphql-for-ecommerce' );
2✔
44
                                        },
296✔
45
                                        'resolve'     => static function ( $source ) {
296✔
46
                                                $database_id = absint( $source->ID );
30✔
47
                                                return ! empty( $database_id ) ? $database_id : null;
30✔
48
                                        },
296✔
49
                                ],
296✔
50
                                'isVatExempt'           => [
296✔
51
                                        'type'        => 'Boolean',
296✔
52
                                        'description' => static function () {
296✔
53
                                                return __( 'Is customer VAT exempt?', 'graphql-for-ecommerce' );
2✔
54
                                        },
296✔
55
                                ],
296✔
56
                                'hasCalculatedShipping' => [
296✔
57
                                        'type'        => 'Boolean',
296✔
58
                                        'description' => static function () {
296✔
59
                                                return __( 'Has calculated shipping?', 'graphql-for-ecommerce' );
2✔
60
                                        },
296✔
61
                                ],
296✔
62
                                'calculatedShipping'    => [
296✔
63
                                        'type'        => 'Boolean',
296✔
64
                                        'description' => static function () {
296✔
65
                                                return __( 'Has customer calculated shipping?', 'graphql-for-ecommerce' );
2✔
66
                                        },
296✔
67
                                ],
296✔
68
                                'lastOrder'             => [
296✔
69
                                        'type'        => 'Order',
296✔
70
                                        'description' => static function () {
296✔
71
                                                return __( 'Gets the customers last order.', 'graphql-for-ecommerce' );
2✔
72
                                        },
296✔
73
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
296✔
74
                                                return Factory::resolve_crud_object( $source->last_order_id, $context );
1✔
75
                                        },
296✔
76
                                ],
296✔
77
                                'orderCount'            => [
296✔
78
                                        'type'        => 'Int',
296✔
79
                                        'description' => static function () {
296✔
80
                                                return __( 'Return the number of orders this customer has.', 'graphql-for-ecommerce' );
2✔
81
                                        },
296✔
82
                                ],
296✔
83
                                'totalSpent'            => [
296✔
84
                                        'type'        => 'Float',
296✔
85
                                        'description' => static function () {
296✔
86
                                                return __( 'Return how much money this customer has spent.', 'graphql-for-ecommerce' );
2✔
87
                                        },
296✔
88
                                ],
296✔
89
                                'username'              => [
296✔
90
                                        'type'        => 'String',
296✔
91
                                        'description' => static function () {
296✔
92
                                                return __( 'Return the customer\'s username.', 'graphql-for-ecommerce' );
2✔
93
                                        },
296✔
94
                                ],
296✔
95
                                'email'                 => [
296✔
96
                                        'type'        => 'String',
296✔
97
                                        'description' => static function () {
296✔
98
                                                return __( 'Return the customer\'s email.', 'graphql-for-ecommerce' );
2✔
99
                                        },
296✔
100
                                ],
296✔
101
                                'firstName'             => [
296✔
102
                                        'type'        => 'String',
296✔
103
                                        'description' => static function () {
296✔
104
                                                return __( 'Return the customer\'s first name.', 'graphql-for-ecommerce' );
2✔
105
                                        },
296✔
106
                                ],
296✔
107
                                'lastName'              => [
296✔
108
                                        'type'        => 'String',
296✔
109
                                        'description' => static function () {
296✔
110
                                                return __( 'Return the customer\'s last name.', 'graphql-for-ecommerce' );
2✔
111
                                        },
296✔
112
                                ],
296✔
113
                                'displayName'           => [
296✔
114
                                        'type'        => 'String',
296✔
115
                                        'description' => static function () {
296✔
116
                                                return __( 'Return the customer\'s display name.', 'graphql-for-ecommerce' );
2✔
117
                                        },
296✔
118
                                ],
296✔
119
                                'role'                  => [
296✔
120
                                        'type'        => 'String',
296✔
121
                                        'description' => static function () {
296✔
122
                                                return __( 'Return the customer\'s user role.', 'graphql-for-ecommerce' );
2✔
123
                                        },
296✔
124
                                ],
296✔
125
                                'date'                  => [
296✔
126
                                        'type'        => 'String',
296✔
127
                                        'description' => static function () {
296✔
128
                                                return __( 'Return the date customer was created', 'graphql-for-ecommerce' );
2✔
129
                                        },
296✔
130
                                ],
296✔
131
                                'modified'              => [
296✔
132
                                        'type'        => 'String',
296✔
133
                                        'description' => static function () {
296✔
134
                                                return __( 'Return the date customer was last updated', 'graphql-for-ecommerce' );
2✔
135
                                        },
296✔
136
                                ],
296✔
137
                                'billing'               => [
296✔
138
                                        'type'        => 'CustomerAddress',
296✔
139
                                        'description' => static function () {
296✔
140
                                                return __( 'Return the date customer billing address properties', 'graphql-for-ecommerce' );
2✔
141
                                        },
296✔
142
                                ],
296✔
143
                                'shipping'              => [
296✔
144
                                        'type'        => 'CustomerAddress',
296✔
145
                                        'description' => static function () {
296✔
146
                                                return __( 'Return the date customer shipping address properties', 'graphql-for-ecommerce' );
2✔
147
                                        },
296✔
148
                                ],
296✔
149
                                'isPayingCustomer'      => [
296✔
150
                                        'type'        => 'Boolean',
296✔
151
                                        'description' => static function () {
296✔
152
                                                return __( 'Return the date customer was last updated', 'graphql-for-ecommerce' );
2✔
153
                                        },
296✔
154
                                ],
296✔
155
                                'metaData'              => Meta_Data_Type::get_metadata_field_definition(),
296✔
156
                                'session'               => [
296✔
157
                                        'type'        => [ 'list_of' => 'MetaData' ],
296✔
158
                                        'description' => static function () {
296✔
159
                                                return __( 'Session data for the viewing customer', 'graphql-for-ecommerce' );
2✔
160
                                        },
296✔
161
                                        'resolve'     => static function ( $source ) {
296✔
162
                                                /**
163
                                                 * Session Handler.
164
                                                 *
165
                                                 * @var \WC_Session_Handler $session
166
                                                 */
167
                                                $session = \WC()->session;
1✔
168

169
                                                if ( (string) $session->get_customer_id() === (string) $source->ID ) {
1✔
170
                                                        $session_data = $session->get_session_data();
1✔
171
                                                        $session      = [];
1✔
172
                                                        foreach ( $session_data as $key => $value ) {
1✔
173
                                                                $meta        = new \stdClass();
1✔
174
                                                                $meta->id    = null;
1✔
175
                                                                $meta->key   = $key;
1✔
176
                                                                $meta->value = maybe_unserialize( $value );
1✔
177
                                                                $session[]   = $meta;
1✔
178
                                                        }
179

180
                                                        return $session;
1✔
181
                                                }
182

NEW
183
                                                throw new UserError( __( 'It\'s not possible to access another user\'s session data', 'graphql-for-ecommerce' ) );
×
184
                                        },
296✔
185
                                ],
296✔
186
                        ],
296✔
187
                        $other_fields
296✔
188
                );
296✔
189
        }
190

191
        /**
192
         * Returns the "Customer" type connections.
193
         *
194
         * @param array $other_connections Extra connections configs to be added or override the default connection definitions.
195
         *
196
         * @return array
197
         */
198
        public static function get_connections( $other_connections = [] ) {
199
                return array_merge(
296✔
200
                        [
296✔
201
                                'downloadableItems' => [
296✔
202
                                        'toType'         => 'DownloadableItem',
296✔
203
                                        'connectionArgs' => [
296✔
204
                                                'active'                => [
296✔
205
                                                        'type'        => 'Boolean',
296✔
206
                                                        'description' => static function () {
296✔
207
                                                                return __( 'Limit results to downloadable items that can be downloaded now.', 'graphql-for-ecommerce' );
2✔
208
                                                        },
296✔
209
                                                ],
296✔
210
                                                'expired'               => [
296✔
211
                                                        'type'        => 'Boolean',
296✔
212
                                                        'description' => static function () {
296✔
213
                                                                return __( 'Limit results to downloadable items that are expired.', 'graphql-for-ecommerce' );
2✔
214
                                                        },
296✔
215
                                                ],
296✔
216
                                                'hasDownloadsRemaining' => [
296✔
217
                                                        'type'        => 'Boolean',
296✔
218
                                                        'description' => static function () {
296✔
219
                                                                return __( 'Limit results to downloadable items that have downloads remaining.', 'graphql-for-ecommerce' );
2✔
220
                                                        },
296✔
221
                                                ],
296✔
222
                                        ],
296✔
223
                                        'resolve'        => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
296✔
224
                                                $resolver = new Downloadable_Item_Connection_Resolver( $source, $args, $context, $info );
5✔
225

226
                                                return $resolver->get_connection();
5✔
227
                                        },
296✔
228
                                ],
296✔
229
                        ],
296✔
230
                        $other_connections
296✔
231
                );
296✔
232
        }
233

234
        /**
235
         * Registers Customer WPObject type and related fields.
236
         *
237
         * @return void
238
         */
239
        public static function register() {
240
                register_graphql_object_type(
296✔
241
                        'Customer',
296✔
242
                        [
296✔
243
                                'description' => static function () {
296✔
244
                                        return __( 'A customer object', 'graphql-for-ecommerce' );
2✔
245
                                },
296✔
246
                                'interfaces'  => [ 'Node' ],
296✔
247
                                /**
248
                                 * Allows for a decisive filtering of the order fields.
249
                                 * Note: Only use if deregisteration or renaming the field(s) has failed.
250
                                 *
251
                                 * @param array $fields  Customer field definitions.
252
                                 * @return array
253
                                 */
254
                                'fields'      => apply_filters( 'woographql_customer_field_definitions', self::get_fields() ),
296✔
255
                                /**
256
                                 * Allows for a decisive filtering of the order connections.
257
                                 * Note: Only use if deregisteration or renaming the connection(s) has failed.
258
                                 *
259
                                 * @param array $connections  Customer connection definitions.
260
                                 * @return array
261
                                 */
262
                                'connections' => apply_filters( 'woographql_customer_connection_definitions', self::get_connections() ),
296✔
263
                        ]
296✔
264
                );
296✔
265

266
                /**
267
                 * Register "availablePaymentMethods" field to "Customer" type.
268
                 */
269
                register_graphql_fields(
296✔
270
                        'Customer',
296✔
271
                        [
296✔
272
                                'availablePaymentMethods'   => [
296✔
273
                                        'type'        => [ 'list_of' => 'PaymentTokenInterface' ],
296✔
274
                                        'description' => static function () {
296✔
275
                                                return __( 'Customer\'s stored payment tokens.', 'graphql-for-ecommerce' );
2✔
276
                                        },
296✔
277
                                        'resolve'     => static function ( $source ) {
296✔
278
                                                if ( get_current_user_id() === $source->ID ) {
1✔
279
                                                        return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) );
1✔
280
                                                }
281

282
                                                if ( get_current_user_id() === 0 ) {
1✔
283
                                                        return [];
×
284
                                                }
285

286
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'graphql-for-ecommerce' ) );
1✔
287
                                        },
296✔
288
                                ],
296✔
289
                                'availablePaymentMethodsCC' => [
296✔
290
                                        'type'        => [ 'list_of' => 'PaymentTokenCC' ],
296✔
291
                                        'description' => static function () {
296✔
292
                                                return __( 'Customer\'s stored payment tokens.', 'graphql-for-ecommerce' );
2✔
293
                                        },
296✔
294
                                        'resolve'     => static function ( $source ) {
296✔
295
                                                if ( get_current_user_id() === $source->ID ) {
1✔
296
                                                        return array_filter(
1✔
297
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
298
                                                                static function ( $token ) {
1✔
299
                                                                        return 'CC' === $token->get_type();
1✔
300
                                                                }
1✔
301
                                                        );
1✔
302
                                                }
303

304
                                                if ( get_current_user_id() === 0 ) {
1✔
305
                                                        return [];
×
306
                                                }
307

308
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'graphql-for-ecommerce' ) );
1✔
309
                                        },
296✔
310
                                ],
296✔
311
                                'availablePaymentMethodsEC' => [
296✔
312
                                        'type'        => [ 'list_of' => 'PaymentTokenECheck' ],
296✔
313
                                        'description' => static function () {
296✔
314
                                                return __( 'Customer\'s stored payment tokens.', 'graphql-for-ecommerce' );
2✔
315
                                        },
296✔
316
                                        'resolve'     => static function ( $source ) {
296✔
317
                                                if ( get_current_user_id() === $source->ID ) {
1✔
318
                                                        return array_filter(
1✔
319
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
320
                                                                static function ( $token ) {
1✔
321
                                                                        return 'eCheck' === $token->get_type();
1✔
322
                                                                }
1✔
323
                                                        );
1✔
324
                                                }
325

326
                                                if ( get_current_user_id() === 0 ) {
1✔
327
                                                        return [];
×
328
                                                }
329

330
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'graphql-for-ecommerce' ) );
1✔
331
                                        },
296✔
332
                                ],
296✔
333
                        ]
296✔
334
                );
296✔
335
        }
336

337
        /**
338
         * Registers fields that require the "QL_Session_Handler" class to work.
339
         *
340
         * @return void
341
         */
342
        public static function register_session_handler_fields() {
343
                $token_type = woographql_setting( 'set_session_token_type', 'legacy' );
296✔
344
                if ( in_array( $token_type, [ 'legacy', 'both' ], true ) ) {
296✔
345
                        /**
346
                         * Register the "sessionToken" field to the "Customer" type.
347
                         */
348
                        register_graphql_field(
295✔
349
                                'Customer',
295✔
350
                                'sessionToken',
295✔
351
                                [
295✔
352
                                        'type'        => 'String',
295✔
353
                                        'description' => static function () {
295✔
354
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'graphql-for-ecommerce' );
2✔
355
                                        },
295✔
356
                                        'resolve'     => static function ( $source ) {
295✔
357
                                                if ( \get_current_user_id() === $source->ID || 'guest' === $source->id ) {
1✔
358
                                                        return new Deferred(
1✔
359
                                                                static function () {
1✔
360
                                                                        /**
361
                                                                         * Session handler.
362
                                                                         *
363
                                                                         * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
364
                                                                         */
365
                                                                        $session = \WC()->session;
1✔
366

367
                                                                        return apply_filters( 'graphql_customer_session_token', $session->build_token() );
1✔
368
                                                                }
1✔
369
                                                        );
1✔
370
                                                }
371

372
                                                return null;
×
373
                                        },
295✔
374
                                ]
295✔
375
                        );
295✔
376

377
                        /**
378
                         * Register the "wooSessionToken" field to the "User" type.
379
                         */
380
                        register_graphql_field(
295✔
381
                                'User',
295✔
382
                                'wooSessionToken',
295✔
383
                                [
295✔
384
                                        'type'        => 'String',
295✔
385
                                        'description' => static function () {
295✔
386
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'graphql-for-ecommerce' );
2✔
387
                                        },
295✔
388
                                        'resolve'     => static function ( $source ) {
295✔
389
                                                if ( \get_current_user_id() === $source->userId ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
×
390
                                                        return new Deferred(
×
391
                                                                static function () {
×
392
                                                                        /**
393
                                                                         * Session handler
394
                                                                         *
395
                                                                         * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
396
                                                                         */
397
                                                                        $session = \WC()->session;
×
398

399
                                                                        return apply_filters( 'graphql_customer_session_token', $session->build_token() );
×
400
                                                                }
×
401
                                                        );
×
402
                                                }
403

404
                                                return null;
×
405
                                        },
295✔
406
                                ]
295✔
407
                        );
295✔
408
                }
409

410
                if ( in_array( $token_type, [ 'store-api', 'both' ], true ) ) {
296✔
411
                        /**
412
                         * Register the "cartToken" field to the "Customer" type.
413
                         */
414
                        register_graphql_field(
2✔
415
                                'Customer',
2✔
416
                                'cartToken',
2✔
417
                                [
2✔
418
                                        'type'        => 'String',
2✔
419
                                        'description' => static function () {
2✔
NEW
420
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'graphql-for-ecommerce' );
×
421
                                        },
2✔
422
                                        'resolve'     => static function ( $source ) {
2✔
423
                                                if ( \get_current_user_id() === $source->ID || 'guest' === $source->id ) {
×
424
                                                        return new Deferred(
×
425
                                                                static function () {
×
426
                                                                        /**
427
                                                                         * Session handler.
428
                                                                         *
429
                                                                         * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
430
                                                                         */
431
                                                                        $session = \WC()->session;
×
432

433
                                                                        return apply_filters( 'graphql_cart_token', $session->build_cart_token() );
×
434
                                                                }
×
435
                                                        );
×
436
                                                }
437

438
                                                return null;
×
439
                                        },
2✔
440
                                ]
2✔
441
                        );
2✔
442

443
                        /**
444
                         * Register the "cartToken" field to the "User" type.
445
                         */
446
                        register_graphql_field(
2✔
447
                                'User',
2✔
448
                                'cartToken',
2✔
449
                                [
2✔
450
                                        'type'        => 'String',
2✔
451
                                        'description' => static function () {
2✔
NEW
452
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'graphql-for-ecommerce' );
×
453
                                        },
2✔
454
                                        'resolve'     => static function ( $source ) {
2✔
455
                                                if ( \get_current_user_id() === $source->userId ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
×
456
                                                        return new Deferred(
×
457
                                                                static function () {
×
458
                                                                        /**
459
                                                                         * Session handler
460
                                                                         *
461
                                                                         * @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
462
                                                                         */
463
                                                                        $session = \WC()->session;
×
464

465
                                                                        return apply_filters( 'graphql_cart_token', $session->build_cart_token() );
×
466
                                                                }
×
467
                                                        );
×
468
                                                }
469

470
                                                return null;
×
471
                                        },
2✔
472
                                ]
2✔
473
                        );
2✔
474
                }
475
        }
476

477
        /**
478
         * Registers selected authorizing_url_fields
479
         *
480
         * @param array $fields_to_register  Slugs of fields.
481
         * @return void
482
         */
483
        public static function register_authorizing_url_fields( $fields_to_register ) {
484
                if ( in_array( 'cart_url', $fields_to_register, true ) ) {
278✔
485
                        register_graphql_fields(
278✔
486
                                'Customer',
278✔
487
                                [
278✔
488
                                        'cartUrl'   => [
278✔
489
                                                'type'        => 'String',
278✔
490
                                                'description' => static function () {
278✔
491
                                                        return __( 'A nonced link to the cart page. By default, it expires in 1 hour.', 'graphql-for-ecommerce' );
2✔
492
                                                },
278✔
493
                                                'resolve'     => static function ( $source ) {
278✔
494
                                                        // Get current customer and user ID.
495
                                                        $customer_id     = $source->ID;
1✔
496
                                                        $current_user_id = get_current_user_id();
1✔
497

498
                                                        // Return null if current user not user being queried.
499
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
500
                                                                return null;
1✔
501
                                                        }
502

503
                                                        // Build nonced url as an unauthenticated user.
504
                                                        $nonce_name   = woographql_setting( 'cart_url_nonce_param', '_wc_cart' );
1✔
505
                                                        $query_params = [
1✔
506
                                                                'session_id' => $customer_id,
1✔
507
                                                                $nonce_name  => woographql_create_nonce( "load-cart_{$customer_id}" ),
1✔
508
                                                        ];
1✔
509
                                                        $query_params = apply_filters( 'graphql_cart_url_query_params', $query_params, $customer_id, $source );
1✔
510
                                                        $url          = add_query_arg(
1✔
511
                                                                $query_params,
1✔
512
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
513
                                                        );
1✔
514

515
                                                        return esc_url_raw( $url );
1✔
516
                                                },
278✔
517
                                        ],
278✔
518
                                        'cartNonce' => [
278✔
519
                                                'type'        => 'String',
278✔
520
                                                'description' => static function () {
278✔
521
                                                        return __( 'A nonce for the cart page. By default, it expires in 1 hour.', 'graphql-for-ecommerce' );
2✔
522
                                                },
278✔
523
                                                'resolve'     => static function ( $source ) {
278✔
524
                                                        // Get current customer and user ID.
525
                                                        $customer_id     = $source->ID;
2✔
526
                                                        $current_user_id = get_current_user_id();
2✔
527

528
                                                        // Return null if current user not user being queried.
529
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
2✔
530
                                                                return null;
1✔
531
                                                        }
532

533
                                                        return woographql_create_nonce( "load-cart_{$customer_id}" );
2✔
534
                                                },
278✔
535
                                        ],
278✔
536
                                ]
278✔
537
                        );
278✔
538
                }//end if
539

540
                if ( in_array( 'checkout_url', $fields_to_register, true ) ) {
278✔
541
                        register_graphql_fields(
278✔
542
                                'Customer',
278✔
543
                                [
278✔
544
                                        'checkoutUrl'   => [
278✔
545
                                                'type'        => 'String',
278✔
546
                                                'description' => static function () {
278✔
547
                                                        return __( 'A nonce link to the checkout page for session user. Expires in 24 hours.', 'graphql-for-ecommerce' );
2✔
548
                                                },
278✔
549
                                                'resolve'     => static function ( $source ) {
278✔
550
                                                        // Get current customer and user ID.
551
                                                        $customer_id     = $source->ID;
2✔
552
                                                        $current_user_id = get_current_user_id();
2✔
553

554
                                                        // Return null if current user not user being queried.
555
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
2✔
556
                                                                return null;
1✔
557
                                                        }
558

559
                                                        // Build nonced url as an unauthenticated user.
560
                                                        $nonce_name   = woographql_setting( 'checkout_url_nonce_param', '_wc_checkout' );
2✔
561
                                                        $query_params = [
2✔
562
                                                                'session_id' => $customer_id,
2✔
563
                                                                $nonce_name  => woographql_create_nonce( "load-checkout_{$customer_id}" ),
2✔
564
                                                        ];
2✔
565
                                                        $query_params = apply_filters( 'graphql_checkout_url_query_params', $query_params, $customer_id, $source );
2✔
566
                                                        $url          = add_query_arg(
2✔
567
                                                                $query_params,
2✔
568
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
2✔
569
                                                        );
2✔
570

571
                                                        return esc_url_raw( $url );
2✔
572
                                                },
278✔
573
                                        ],
278✔
574
                                        'checkoutNonce' => [
278✔
575
                                                'type'        => 'String',
278✔
576
                                                'description' => static function () {
278✔
577
                                                        return __( 'A nonce for the checkout page. By default, it expires in 1 hour.', 'graphql-for-ecommerce' );
2✔
578
                                                },
278✔
579
                                                'resolve'     => static function ( $source ) {
278✔
580
                                                        // Get current customer and user ID.
581
                                                        $customer_id     = $source->ID;
3✔
582
                                                        $current_user_id = get_current_user_id();
3✔
583

584
                                                        // Return null if current user not user being queried.
585
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
3✔
586
                                                                return null;
1✔
587
                                                        }
588

589
                                                        return woographql_create_nonce( "load-checkout_{$customer_id}" );
3✔
590
                                                },
278✔
591
                                        ],
278✔
592
                                ]
278✔
593
                        );
278✔
594
                }//end if
595

596
                if ( in_array( 'account_url', $fields_to_register, true ) ) {
278✔
597
                        register_graphql_fields(
278✔
598
                                'Customer',
278✔
599
                                [
278✔
600
                                        'accountUrl'   => [
278✔
601
                                                'type'        => 'String',
278✔
602
                                                'description' => static function () {
278✔
603
                                                        return __( 'A nonce link to the account page for session user. Expires in 24 hours.', 'graphql-for-ecommerce' );
2✔
604
                                                },
278✔
605
                                                'resolve'     => static function ( $source ) {
278✔
606
                                                        if ( ! is_user_logged_in() ) {
×
607
                                                                return null;
×
608
                                                        }
609

610
                                                        // Get current customer and user ID.
611
                                                        $customer_id     = $source->ID;
×
612
                                                        $current_user_id = get_current_user_id();
×
613

614
                                                        // Return null if current user not user being queried.
615
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
×
616
                                                                return null;
×
617
                                                        }
618

619
                                                        // Build nonced url as an unauthenticated user.
620
                                                        $nonce_name   = woographql_setting( 'account_url_nonce_param', '_wc_account' );
×
621
                                                        $query_params = [
×
622
                                                                'session_id' => $customer_id,
×
623
                                                                $nonce_name  => woographql_create_nonce( "load-account_{$customer_id}" ),
×
624
                                                        ];
×
625
                                                        $query_params = apply_filters( 'graphql_account_url_query_params', $query_params, $customer_id, $source );
×
626
                                                        $url          = add_query_arg(
×
627
                                                                $query_params,
×
628
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
×
629
                                                        );
×
630

631
                                                        return esc_url_raw( $url );
×
632
                                                },
278✔
633
                                        ],
278✔
634
                                        'accountNonce' => [
278✔
635
                                                'type'        => 'String',
278✔
636
                                                'description' => static function () {
278✔
637
                                                        return __( 'A nonce for the account page. By default, it expires in 1 hour.', 'graphql-for-ecommerce' );
2✔
638
                                                },
278✔
639
                                                'resolve'     => static function ( $source ) {
278✔
640
                                                        if ( ! is_user_logged_in() ) {
1✔
641
                                                                return null;
×
642
                                                        }
643

644
                                                        // Get current customer and user ID.
645
                                                        $customer_id     = $source->ID;
1✔
646
                                                        $current_user_id = get_current_user_id();
1✔
647

648
                                                        // Return null if current user not user being queried.
649
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
650
                                                                return null;
×
651
                                                        }
652

653
                                                        return woographql_create_nonce( "load-account_{$customer_id}" );
1✔
654
                                                },
278✔
655
                                        ],
278✔
656
                                ]
278✔
657
                        );
278✔
658
                }//end if
659

660
                if ( in_array( 'add_payment_method_url', $fields_to_register, true ) ) {
278✔
661
                        register_graphql_fields(
278✔
662
                                'Customer',
278✔
663
                                [
278✔
664
                                        'addPaymentMethodUrl'   => [
278✔
665
                                                'type'        => 'String',
278✔
666
                                                'description' => static function () {
278✔
667
                                                        return __( 'A nonce link to the add payment method page for the authenticated user. Expires in 24 hours.', 'graphql-for-ecommerce' );
2✔
668
                                                },
278✔
669
                                                'resolve'     => static function ( $source ) {
278✔
670
                                                        if ( ! is_user_logged_in() ) {
1✔
671
                                                                return null;
×
672
                                                        }
673

674
                                                        // Get current customer and user ID.
675
                                                        $customer_id     = $source->ID;
1✔
676
                                                        $current_user_id = get_current_user_id();
1✔
677

678
                                                        // Return null if current user not user being queried.
679
                                                        if ( $current_user_id !== $customer_id ) {
1✔
680
                                                                return null;
1✔
681
                                                        }
682

683
                                                        // Build nonced url as an unauthenticated user.
684
                                                        $nonce_name = woographql_setting( 'add_payment_method_url_nonce_param', '_wc_payment' );
1✔
685
                                                        $url        = add_query_arg(
1✔
686
                                                                [
1✔
687
                                                                        'session_id' => $customer_id,
1✔
688
                                                                        $nonce_name  => woographql_create_nonce( "add-payment-method_{$customer_id}" ),
1✔
689
                                                                ],
1✔
690
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
691
                                                        );
1✔
692

693
                                                        return esc_url_raw( $url );
1✔
694
                                                },
278✔
695
                                        ],
278✔
696
                                        'addPaymentMethodNonce' => [
278✔
697
                                                'type'        => 'String',
278✔
698
                                                'description' => static function () {
278✔
699
                                                        return __( 'A nonce for the add payment method page. By default, it expires in 1 hour.', 'graphql-for-ecommerce' );
2✔
700
                                                },
278✔
701
                                                'resolve'     => static function ( $source ) {
278✔
702
                                                        if ( ! is_user_logged_in() ) {
2✔
703
                                                                return null;
×
704
                                                        }
705

706
                                                        // Get current customer and user ID.
707
                                                        $customer_id     = $source->ID;
2✔
708
                                                        $current_user_id = get_current_user_id();
2✔
709

710
                                                        // Return null if current user not user being queried.
711
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
2✔
712
                                                                return null;
1✔
713
                                                        }
714

715
                                                        return woographql_create_nonce( "add-payment-method_{$customer_id}" );
2✔
716
                                                },
278✔
717
                                        ],
278✔
718
                                ]
278✔
719
                        );
278✔
720
                }//end if
721
        }
722
}
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