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

wp-graphql / wp-graphql-woocommerce / 5441823169

pending completion
5441823169

push

github

web-flow
feat: Account URL added to Auth URLs. (#755)

* feat: Account URL added to Auth URLs.

* fix: Account URL issues resolved + Variation Attribute connections refactored.

128 of 128 new or added lines in 7 files covered. (100.0%)

10250 of 12439 relevant lines covered (82.4%)

54.01 hits per line

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

90.98
/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\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use GraphQLRelay\Relay;
16
use WPGraphQL\AppContext;
17
use WPGraphQL\WooCommerce\Data\Factory;
18
use WPGraphQL\WooCommerce\Data\Connection\Downloadable_Item_Connection_Resolver;
19
use WPGraphQL\WooCommerce\Utils\QL_Session_Handler;
20

21
/**
22
 * Class Customer_Type
23
 */
24
class Customer_Type {
25

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

131
                                                if ( (string) $session->get_customer_id() === (string) $source->ID ) {
1✔
132
                                                        $session_data = $session->get_session_data();
1✔
133
                                                        $session      = [];
1✔
134
                                                        foreach ( $session_data as $key => $value ) {
1✔
135
                                                                $meta        = new \stdClass();
1✔
136
                                                                $meta->id    = null;
1✔
137
                                                                $meta->key   = $key;
1✔
138
                                                                $meta->value = maybe_unserialize( $value );
1✔
139
                                                                $session[]   = $meta;
1✔
140
                                                        }
141

142
                                                        return $session;
1✔
143
                                                }
144

145
                                                throw new UserError( __( 'It\'s not possible to access another user\'s session data', 'wp-graphql-woocommerce' ) );
×
146
                                        },
109✔
147
                                ],
109✔
148
                        ],
109✔
149
                        $other_fields,
109✔
150
                );
109✔
151
        }
152

153
        /**
154
         * Returns the "Customer" type connections.
155
         *
156
         * @param array $other_connections Extra connections configs to be added or override the default connection definitions.
157
         *
158
         * @return array
159
         */
160
        public static function get_connections( $other_connections = [] ) {
161
                return array_merge(
109✔
162
                        [
109✔
163
                                'downloadableItems' => [
109✔
164
                                        'toType'         => 'DownloadableItem',
109✔
165
                                        'connectionArgs' => [
109✔
166
                                                'active'                => [
109✔
167
                                                        'type'        => 'Boolean',
109✔
168
                                                        'description' => __( 'Limit results to downloadable items that can be downloaded now.', 'wp-graphql-woocommerce' ),
109✔
169
                                                ],
109✔
170
                                                'expired'               => [
109✔
171
                                                        'type'        => 'Boolean',
109✔
172
                                                        'description' => __( 'Limit results to downloadable items that are expired.', 'wp-graphql-woocommerce' ),
109✔
173
                                                ],
109✔
174
                                                'hasDownloadsRemaining' => [
109✔
175
                                                        'type'        => 'Boolean',
109✔
176
                                                        'description' => __( 'Limit results to downloadable items that have downloads remaining.', 'wp-graphql-woocommerce' ),
109✔
177
                                                ],
109✔
178
                                        ],
109✔
179
                                        'resolve'        => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
109✔
180
                                                $resolver = new Downloadable_Item_Connection_Resolver( $source, $args, $context, $info );
1✔
181

182
                                                return $resolver->get_connection();
1✔
183
                                        },
109✔
184
                                ],
109✔
185
                        ],
109✔
186
                        $other_connections
109✔
187
                );
109✔
188
        }
189

190
        /**
191
         * Registers Customer WPObject type and related fields.
192
         *
193
         * @return void
194
         */
195
        public static function register() {
196
                register_graphql_object_type(
109✔
197
                        'Customer',
109✔
198
                        [
109✔
199
                                'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
109✔
200
                                'interfaces'  => [ 'Node' ],
109✔
201
                                /**
202
                                 * Allows for a decisive filtering of the order fields.
203
                                 * Note: Only use if deregisteration or renaming the field(s) has failed.
204
                                 *
205
                                 * @param array $fields  Customer field definitions.
206
                                 * @return array
207
                                 */
208
                                'fields'      => apply_filters( 'woographql_customer_field_definitions', self::get_fields() ),
109✔
209
                                /**
210
                                 * Allows for a decisive filtering of the order connections.
211
                                 * Note: Only use if deregisteration or renaming the connection(s) has failed.
212
                                 *
213
                                 * @param array $connections  Customer connection definitions.
214
                                 * @return array
215
                                 */
216
                                'connections' => apply_filters( 'woographql_customer_connection_definitions', self::get_connections() ),
109✔
217
                        ]
109✔
218
                );
109✔
219

220
                /**
221
                 * Register "availablePaymentMethods" field to "Customer" type.
222
                 */
223
                register_graphql_fields(
109✔
224
                        'Customer',
109✔
225
                        [
109✔
226
                                'availablePaymentMethods'   => [
109✔
227
                                        'type'        => [ 'list_of' => 'PaymentToken' ],
109✔
228
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
109✔
229
                                        'resolve'     => function( $source ) {
109✔
230
                                                if ( get_current_user_id() === $source->ID ) {
1✔
231
                                                        return array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) );
1✔
232
                                                }
233

234
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
235
                                        },
109✔
236
                                ],
109✔
237
                                'availablePaymentMethodsCC' => [
109✔
238
                                        'type'        => [ 'list_of' => 'PaymentTokenCC' ],
109✔
239
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
109✔
240
                                        'resolve'     => function( $source ) {
109✔
241
                                                if ( get_current_user_id() === $source->ID ) {
1✔
242
                                                        $tokens = array_filter(
1✔
243
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
244
                                                                function ( $token ) {
1✔
245
                                                                        return 'CC' === $token->get_type();
1✔
246
                                                                }
1✔
247
                                                        );
1✔
248
                                                        return $tokens;
1✔
249
                                                }
250

251
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
252
                                        },
109✔
253
                                ],
109✔
254
                                'availablePaymentMethodsEC' => [
109✔
255
                                        'type'        => [ 'list_of' => 'PaymentTokenECheck' ],
109✔
256
                                        'description' => __( 'Customer\'s stored payment tokens.', 'wp-graphql-woocommerce' ),
109✔
257
                                        'resolve'     => function( $source ) {
109✔
258
                                                if ( get_current_user_id() === $source->ID ) {
1✔
259
                                                        $tokens = array_filter(
1✔
260
                                                                array_values( \WC_Payment_Tokens::get_customer_tokens( $source->ID ) ),
1✔
261
                                                                function ( $token ) {
1✔
262
                                                                        return 'eCheck' === $token->get_type();
1✔
263
                                                                }
1✔
264
                                                        );
1✔
265
                                                        return $tokens;
1✔
266
                                                }
267

268
                                                throw new UserError( __( 'Not authorized to view this user\'s payment methods.', 'wp-graphql-woocommerce' ) );
1✔
269
                                        },
109✔
270
                                ],
109✔
271
                        ]
109✔
272
                );
109✔
273
        }
274

275
        /**
276
         * Registers fields that require the "QL_Session_Handler" class to work.
277
         *
278
         * @return void
279
         */
280
        public static function register_session_handler_fields() {
281
                /**
282
                 * Register the "sessionToken" field to the "Customer" type.
283
                 */
284
                register_graphql_field(
109✔
285
                        'Customer',
109✔
286
                        'sessionToken',
109✔
287
                        [
109✔
288
                                'type'        => 'String',
109✔
289
                                'description' => __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' ),
109✔
290
                                'resolve'     => function( $source ) {
109✔
291
                                        if ( \get_current_user_id() === $source->ID || 'guest' === $source->id ) {
×
292
                                                /**
293
                                                 * Session handler.
294
                                                 *
295
                                                 * @var QL_Session_Handler $session
296
                                                 */
297
                                                $session = \WC()->session;
×
298

299
                                                return apply_filters( 'graphql_customer_session_token', $session->build_token() );
×
300
                                        }
301

302
                                        return null;
×
303
                                },
109✔
304
                        ]
109✔
305
                );
109✔
306
                /**
307
                 * Register the "wooSessionToken" field to the "User" type.
308
                 */
309
                register_graphql_field(
109✔
310
                        'User',
109✔
311
                        'wooSessionToken',
109✔
312
                        [
109✔
313
                                'type'        => 'String',
109✔
314
                                'description' => __( 'A JWT token that can be used in future requests to for WooCommerce session identification', 'wp-graphql-woocommerce' ),
109✔
315
                                'resolve'     => function( $source ) {
109✔
316
                                        if ( \get_current_user_id() === $source->userId ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
×
317
                                                /**
318
                                                 * Session handler
319
                                                 *
320
                                                 * @var QL_Session_Handler $session
321
                                                 */
322
                                                $session = \WC()->session;
×
323

324
                                                return apply_filters( 'graphql_customer_session_token', $session->build_token() );
×
325
                                        }
326

327
                                        return null;
×
328
                                },
109✔
329
                        ]
109✔
330
                );
109✔
331
        }
332

333

334
        /**
335
         * Registers selected authorizing_url_fields
336
         *
337
         * @param array $fields_to_register  Slugs of fields.
338
         * @return void
339
         */
340
        public static function register_authorizing_url_fields( $fields_to_register ) {
341
                if ( in_array( 'cart_url', $fields_to_register, true ) ) {
109✔
342
                        register_graphql_fields(
109✔
343
                                'Customer',
109✔
344
                                [
109✔
345
                                        'cartUrl'   => [
109✔
346
                                                'type'        => 'String',
109✔
347
                                                'description' => __( 'A nonced link to the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
109✔
348
                                                'resolve'     => function( $source ) {
109✔
349
                                                        // Get current customer and user ID.
350
                                                        $customer_id     = $source->ID;
1✔
351
                                                        $current_user_id = get_current_user_id();
1✔
352

353
                                                        // Return null if current user not user being queried.
354
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
355
                                                                return null;
1✔
356
                                                        }
357

358
                                                        // Build nonced url as an unauthenticated user.
359
                                                        $nonce_name = woographql_setting( 'cart_url_nonce_param', '_wc_cart' );
1✔
360
                                                        $url        = add_query_arg(
1✔
361
                                                                [
1✔
362
                                                                        'session_id' => $customer_id,
1✔
363
                                                                        $nonce_name  => woographql_create_nonce( "load-cart_{$customer_id}" ),
1✔
364
                                                                ],
1✔
365
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
366
                                                        );
1✔
367

368
                                                        return esc_url_raw( $url );
1✔
369
                                                },
109✔
370
                                        ],
109✔
371
                                        'cartNonce' => [
109✔
372
                                                'type'        => 'String',
109✔
373
                                                'description' => __( 'A nonce for the cart page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
109✔
374
                                                'resolve'     => function( $source ) {
109✔
375
                                                        // Get current customer and user ID.
376
                                                        $customer_id     = $source->ID;
1✔
377
                                                        $current_user_id = get_current_user_id();
1✔
378

379
                                                        // Return null if current user not user being queried.
380
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
381
                                                                return null;
1✔
382
                                                        }
383

384
                                                        return woographql_create_nonce( "load-cart_{$customer_id}" );
1✔
385
                                                },
109✔
386
                                        ],
109✔
387
                                ]
109✔
388
                        );
109✔
389
                }//end if
390

391
                if ( in_array( 'checkout_url', $fields_to_register, true ) ) {
109✔
392
                        register_graphql_fields(
109✔
393
                                'Customer',
109✔
394
                                [
109✔
395
                                        'checkoutUrl'   => [
109✔
396
                                                'type'        => 'String',
109✔
397
                                                'description' => __( 'A nonce link to the checkout page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
109✔
398
                                                'resolve'     => function( $source ) {
109✔
399
                                                        // Get current customer and user ID.
400
                                                        $customer_id     = $source->ID;
1✔
401
                                                        $current_user_id = get_current_user_id();
1✔
402

403
                                                        // Return null if current user not user being queried.
404
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
405
                                                                return null;
1✔
406
                                                        }
407

408
                                                        // Build nonced url as an unauthenticated user.
409
                                                        $nonce_name = woographql_setting( 'checkout_url_nonce_param', '_wc_checkout' );
1✔
410
                                                        $url        = add_query_arg(
1✔
411
                                                                [
1✔
412
                                                                        'session_id' => $customer_id,
1✔
413
                                                                        $nonce_name  => woographql_create_nonce( "load-checkout_{$customer_id}" ),
1✔
414
                                                                ],
1✔
415
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
416
                                                        );
1✔
417

418
                                                        return esc_url_raw( $url );
1✔
419
                                                },
109✔
420
                                        ],
109✔
421
                                        'checkoutNonce' => [
109✔
422
                                                'type'        => 'String',
109✔
423
                                                'description' => __( 'A nonce for the checkout page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
109✔
424
                                                'resolve'     => function( $source ) {
109✔
425
                                                        // Get current customer and user ID.
426
                                                        $customer_id     = $source->ID;
1✔
427
                                                        $current_user_id = get_current_user_id();
1✔
428

429
                                                        // Return null if current user not user being queried.
430
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
431
                                                                return null;
1✔
432
                                                        }
433

434
                                                        return woographql_create_nonce( "load-checkout_{$customer_id}" );
1✔
435
                                                },
109✔
436
                                        ],
109✔
437
                                ]
109✔
438
                        );
109✔
439
                }//end if
440

441
                if ( in_array( 'account_url', $fields_to_register, true ) ) {
109✔
442
                        register_graphql_fields(
109✔
443
                                'Customer',
109✔
444
                                [
109✔
445
                                        'accountUrl'   => [
109✔
446
                                                'type'        => 'String',
109✔
447
                                                'description' => __( 'A nonce link to the account page for session user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
109✔
448
                                                'resolve'     => function( $source ) {
109✔
449
                                                        if ( ! is_user_logged_in() ) {
×
450
                                                                return null;
×
451
                                                        }
452

453
                                                        // Get current customer and user ID.
454
                                                        $customer_id     = $source->ID;
×
455
                                                        $current_user_id = get_current_user_id();
×
456

457
                                                        // Return null if current user not user being queried.
458
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
×
459
                                                                return null;
×
460
                                                        }
461

462
                                                        // Build nonced url as an unauthenticated user.
463
                                                        $nonce_name = woographql_setting( 'account_url_nonce_param', '_wc_account' );
×
464
                                                        $url        = add_query_arg(
×
465
                                                                [
×
466
                                                                        'session_id' => $customer_id,
×
467
                                                                        $nonce_name  => woographql_create_nonce( "load-account_{$customer_id}" ),
×
468
                                                                ],
×
469
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
×
470
                                                        );
×
471

472
                                                        return esc_url_raw( $url );
×
473
                                                },
109✔
474
                                        ],
109✔
475
                                        'accountNonce' => [
109✔
476
                                                'type'        => 'String',
109✔
477
                                                'description' => __( 'A nonce for the account page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
109✔
478
                                                'resolve'     => function( $source ) {
109✔
479
                                                        if ( ! is_user_logged_in() ) {
×
480
                                                                return null;
×
481
                                                        }
482

483
                                                        // Get current customer and user ID.
484
                                                        $customer_id     = $source->ID;
×
485
                                                        $current_user_id = get_current_user_id();
×
486

487
                                                        // Return null if current user not user being queried.
488
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
×
489
                                                                return null;
×
490
                                                        }
491

492
                                                        return woographql_create_nonce( "load-account_{$customer_id}" );
×
493
                                                },
109✔
494
                                        ],
109✔
495
                                ]
109✔
496
                        );
109✔
497
                }//end if
498

499
                if ( in_array( 'add_payment_method_url', $fields_to_register, true ) ) {
109✔
500
                        register_graphql_fields(
109✔
501
                                'Customer',
109✔
502
                                [
109✔
503
                                        'addPaymentMethodUrl'   => [
109✔
504
                                                'type'        => 'String',
109✔
505
                                                'description' => __( 'A nonce link to the add payment method page for the authenticated user. Expires in 24 hours.', 'wp-graphql-woocommerce' ),
109✔
506
                                                'resolve'     => function( $source ) {
109✔
507
                                                        if ( ! is_user_logged_in() ) {
1✔
508
                                                                return null;
×
509
                                                        }
510

511
                                                        // Get current customer and user ID.
512
                                                        $customer_id     = $source->ID;
1✔
513
                                                        $current_user_id = get_current_user_id();
1✔
514

515
                                                        // Return null if current user not user being queried.
516
                                                        if ( $current_user_id !== $customer_id ) {
1✔
517
                                                                return null;
1✔
518
                                                        }
519

520
                                                        // Build nonced url as an unauthenticated user.
521
                                                        $nonce_name = woographql_setting( 'add_payment_method_url_nonce_param', '_wc_payment' );
1✔
522
                                                        $url        = add_query_arg(
1✔
523
                                                                [
1✔
524
                                                                        'session_id' => $customer_id,
1✔
525
                                                                        $nonce_name  => woographql_create_nonce( "add-payment-method_{$customer_id}" ),
1✔
526
                                                                ],
1✔
527
                                                                site_url( woographql_setting( 'authorizing_url_endpoint', 'transfer-session' ) )
1✔
528
                                                        );
1✔
529

530
                                                        return esc_url_raw( $url );
1✔
531
                                                },
109✔
532
                                        ],
109✔
533
                                        'addPaymentMethodNonce' => [
109✔
534
                                                'type'        => 'String',
109✔
535
                                                'description' => __( 'A nonce for the add payment method page. By default, it expires in 1 hour.', 'wp-graphql-woocommerce' ),
109✔
536
                                                'resolve'     => function( $source ) {
109✔
537
                                                        if ( ! is_user_logged_in() ) {
1✔
538
                                                                return null;
×
539
                                                        }
540

541
                                                        // Get current customer and user ID.
542
                                                        $customer_id     = $source->ID;
1✔
543
                                                        $current_user_id = get_current_user_id();
1✔
544

545
                                                        // Return null if current user not user being queried.
546
                                                        if ( 0 !== $current_user_id && $current_user_id !== $customer_id ) {
1✔
547
                                                                return null;
1✔
548
                                                        }
549

550
                                                        return woographql_create_nonce( "add-payment-method_{$customer_id}" );
1✔
551
                                                },
109✔
552
                                        ],
109✔
553
                                ]
109✔
554
                        );
109✔
555
                }//end if
556
        }
557
}
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