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

wp-graphql / wp-graphql-woocommerce / 23779262545

31 Mar 2026 03:37AM UTC coverage: 91.718% (+1.0%) from 90.757%
23779262545

push

github

web-flow
perf: Migrate type descriptions to lazy lambdas (#1005)

* perf: Migrate type descriptions to lazy lambdas

Convert all eager description strings from:
  'description' => __( '...', 'wp-graphql-woocommerce' ),
to deferred lambdas:
  'description' => static function () { return __( '...', 'wp-graphql-woocommerce' ); },

This defers the __() translation call until the description is actually
needed (e.g. during schema introspection), avoiding unnecessary
translation overhead on every GraphQL request. Mirrors the pattern
used in WPGraphQL core.

1206 descriptions converted across 169 files.

* chore: Linter compliances met

3598 of 3630 new or added lines in 167 files covered. (99.12%)

1 existing line in 1 file now uncovered.

18328 of 19983 relevant lines covered (91.72%)

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

183
                                                throw new UserError( __( 'It\'s not possible to access another user\'s session data', 'wp-graphql-woocommerce' ) );
×
184
                                        },
285✔
185
                                ],
285✔
186
                        ],
285✔
187
                        $other_fields
285✔
188
                );
285✔
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(
285✔
200
                        [
285✔
201
                                'downloadableItems' => [
285✔
202
                                        'toType'         => 'DownloadableItem',
285✔
203
                                        'connectionArgs' => [
285✔
204
                                                'active'                => [
285✔
205
                                                        'type'        => 'Boolean',
285✔
206
                                                        'description' => static function () {
285✔
207
                                                                return __( 'Limit results to downloadable items that can be downloaded now.', 'wp-graphql-woocommerce' );
2✔
208
                                                        },
285✔
209
                                                ],
285✔
210
                                                'expired'               => [
285✔
211
                                                        'type'        => 'Boolean',
285✔
212
                                                        'description' => static function () {
285✔
213
                                                                return __( 'Limit results to downloadable items that are expired.', 'wp-graphql-woocommerce' );
2✔
214
                                                        },
285✔
215
                                                ],
285✔
216
                                                'hasDownloadsRemaining' => [
285✔
217
                                                        'type'        => 'Boolean',
285✔
218
                                                        'description' => static function () {
285✔
219
                                                                return __( 'Limit results to downloadable items that have downloads remaining.', 'wp-graphql-woocommerce' );
2✔
220
                                                        },
285✔
221
                                                ],
285✔
222
                                        ],
285✔
223
                                        'resolve'        => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
285✔
224
                                                $resolver = new Downloadable_Item_Connection_Resolver( $source, $args, $context, $info );
5✔
225

226
                                                return $resolver->get_connection();
5✔
227
                                        },
285✔
228
                                ],
285✔
229
                        ],
285✔
230
                        $other_connections
285✔
231
                );
285✔
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(
285✔
241
                        'Customer',
285✔
242
                        [
285✔
243
                                'description' => static function () {
285✔
244
                                        return __( 'A customer object', 'wp-graphql-woocommerce' );
2✔
245
                                },
285✔
246
                                'interfaces'  => [ 'Node' ],
285✔
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() ),
285✔
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() ),
285✔
263
                        ]
285✔
264
                );
285✔
265

266
                /**
267
                 * Register "availablePaymentMethods" field to "Customer" type.
268
                 */
269
                register_graphql_fields(
285✔
270
                        'Customer',
285✔
271
                        [
285✔
272
                                'availablePaymentMethods'   => [
285✔
273
                                        'type'        => [ 'list_of' => 'PaymentTokenInterface' ],
285✔
274
                                        'description' => static function () {
285✔
275
                                                return __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' );
2✔
276
                                        },
285✔
277
                                        'resolve'     => static function ( $source ) {
285✔
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.', 'wp-graphql-woocommerce' ) );
1✔
287
                                        },
285✔
288
                                ],
285✔
289
                                'availablePaymentMethodsCC' => [
285✔
290
                                        'type'        => [ 'list_of' => 'PaymentTokenCC' ],
285✔
291
                                        'description' => static function () {
285✔
292
                                                return __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' );
2✔
293
                                        },
285✔
294
                                        'resolve'     => static function ( $source ) {
285✔
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.', 'wp-graphql-woocommerce' ) );
1✔
309
                                        },
285✔
310
                                ],
285✔
311
                                'availablePaymentMethodsEC' => [
285✔
312
                                        'type'        => [ 'list_of' => 'PaymentTokenECheck' ],
285✔
313
                                        'description' => static function () {
285✔
314
                                                return __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' );
2✔
315
                                        },
285✔
316
                                        'resolve'     => static function ( $source ) {
285✔
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.', 'wp-graphql-woocommerce' ) );
1✔
331
                                        },
285✔
332
                                ],
285✔
333
                        ]
285✔
334
                );
285✔
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' );
285✔
344
                if ( in_array( $token_type, [ 'legacy', 'both' ], true ) ) {
285✔
345
                        /**
346
                         * Register the "sessionToken" field to the "Customer" type.
347
                         */
348
                        register_graphql_field(
284✔
349
                                'Customer',
284✔
350
                                'sessionToken',
284✔
351
                                [
284✔
352
                                        'type'        => 'String',
284✔
353
                                        'description' => static function () {
284✔
354
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' );
2✔
355
                                        },
284✔
356
                                        'resolve'     => static function ( $source ) {
284✔
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
                                        },
284✔
374
                                ]
284✔
375
                        );
284✔
376

377
                        /**
378
                         * Register the "wooSessionToken" field to the "User" type.
379
                         */
380
                        register_graphql_field(
284✔
381
                                'User',
284✔
382
                                'wooSessionToken',
284✔
383
                                [
284✔
384
                                        'type'        => 'String',
284✔
385
                                        'description' => static function () {
284✔
386
                                                return __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' );
2✔
387
                                        },
284✔
388
                                        'resolve'     => static function ( $source ) {
284✔
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
                                        },
284✔
406
                                ]
284✔
407
                        );
284✔
408
                }
409

410
                if ( in_array( $token_type, [ 'store-api', 'both' ], true ) ) {
285✔
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', 'wp-graphql-woocommerce' );
×
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', 'wp-graphql-woocommerce' );
×
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 ) ) {
269✔
485
                        register_graphql_fields(
269✔
486
                                'Customer',
269✔
487
                                [
269✔
488
                                        'cartUrl'   => [
269✔
489
                                                'type'        => 'String',
269✔
490
                                                'description' => static function () {
269✔
491
                                                        return __( 'A nonced link to the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' );
2✔
492
                                                },
269✔
493
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
517
                                        ],
269✔
518
                                        'cartNonce' => [
269✔
519
                                                'type'        => 'String',
269✔
520
                                                'description' => static function () {
269✔
521
                                                        return __( 'A nonce for the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' );
2✔
522
                                                },
269✔
523
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
535
                                        ],
269✔
536
                                ]
269✔
537
                        );
269✔
538
                }//end if
539

540
                if ( in_array( 'checkout_url', $fields_to_register, true ) ) {
269✔
541
                        register_graphql_fields(
269✔
542
                                'Customer',
269✔
543
                                [
269✔
544
                                        'checkoutUrl'   => [
269✔
545
                                                'type'        => 'String',
269✔
546
                                                'description' => static function () {
269✔
547
                                                        return __( 'A nonce link to the checkout page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' );
2✔
548
                                                },
269✔
549
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
573
                                        ],
269✔
574
                                        'checkoutNonce' => [
269✔
575
                                                'type'        => 'String',
269✔
576
                                                'description' => static function () {
269✔
577
                                                        return __( 'A nonce for the checkout page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' );
2✔
578
                                                },
269✔
579
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
591
                                        ],
269✔
592
                                ]
269✔
593
                        );
269✔
594
                }//end if
595

596
                if ( in_array( 'account_url', $fields_to_register, true ) ) {
269✔
597
                        register_graphql_fields(
269✔
598
                                'Customer',
269✔
599
                                [
269✔
600
                                        'accountUrl'   => [
269✔
601
                                                'type'        => 'String',
269✔
602
                                                'description' => static function () {
269✔
603
                                                        return __( 'A nonce link to the account page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' );
2✔
604
                                                },
269✔
605
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
633
                                        ],
269✔
634
                                        'accountNonce' => [
269✔
635
                                                'type'        => 'String',
269✔
636
                                                'description' => static function () {
269✔
637
                                                        return __( 'A nonce for the account page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' );
2✔
638
                                                },
269✔
639
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
655
                                        ],
269✔
656
                                ]
269✔
657
                        );
269✔
658
                }//end if
659

660
                if ( in_array( 'add_payment_method_url', $fields_to_register, true ) ) {
269✔
661
                        register_graphql_fields(
269✔
662
                                'Customer',
269✔
663
                                [
269✔
664
                                        'addPaymentMethodUrl'   => [
269✔
665
                                                'type'        => 'String',
269✔
666
                                                'description' => static function () {
269✔
667
                                                        return __( 'A nonce link to the add payment method page for the authenticated user. Expires in 24 hours.', 'wp-graphql-woocommerce' );
2✔
668
                                                },
269✔
669
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
695
                                        ],
269✔
696
                                        'addPaymentMethodNonce' => [
269✔
697
                                                'type'        => 'String',
269✔
698
                                                'description' => static function () {
269✔
699
                                                        return __( 'A nonce for the add payment method page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' );
2✔
700
                                                },
269✔
701
                                                'resolve'     => static function ( $source ) {
269✔
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
                                                },
269✔
717
                                        ],
269✔
718
                                ]
269✔
719
                        );
269✔
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