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

wp-graphql / wp-graphql-woocommerce / 10288284022

07 Aug 2024 04:39PM UTC coverage: 84.506% (-0.08%) from 84.583%
10288284022

push

github

web-flow
feat: QL Session Handler refactored to handle non-GraphQL requests (#870)

* feat: QL Session Handler functionality expanded to support cookies on non-GraphQL requests

* chore: Linter and PHPStan compliance met

* devops: QLSessionHandlerTest patched for suite testing

* chore: Linter and PHPStan compliance met

* fix: More cart session save triggered implemented

* fix: More cart session save triggered implemented

* chore: Linter compliance met

* chore: Linter compliance met

* feat: forgetSession mutation added

* feat: forgetSession mutation added

84 of 124 new or added lines in 18 files covered. (67.74%)

1 existing line in 1 file now uncovered.

12484 of 14773 relevant lines covered (84.51%)

72.58 hits per line

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

4.92
/includes/admin/class-general.php
1
<?php
2
/**
3
 * Defines WooGraphQL's general settings.
4
 *
5
 * @package WPGraphQL\WooCommerce\Admin
6
 */
7

8
namespace WPGraphQL\WooCommerce\Admin;
9

10
/**
11
 * General class
12
 */
13
class General extends Section {
14
        /**
15
         * Returns the other nonce values besides the one provided.
16
         *
17
         * @param string $excluded  Slug of nonce value to be excluded.
18
         *
19
         * @return array
20
         */
21
        public static function get_other_nonce_values( $excluded ) {
22
                $nonce_values = apply_filters(
×
23
                        'woographql_authorizing_url_nonce_values',
×
24
                        [
×
25
                                'cart_url'               => woographql_setting( 'cart_url_nonce_param', '_wc_cart' ),
×
26
                                'checkout_url'           => woographql_setting( 'checkout_url_nonce_param', '_wc_checkout' ),
×
27
                                'account_url'            => woographql_setting( 'account_url_nonce_param', '_wc_account' ),
×
28
                                'add_payment_method_url' => woographql_setting( 'add_payment_method_url_nonce_param', '_wc_payment' ),
×
29
                        ]
×
30
                );
×
31

32
                return array_values( array_diff_key( $nonce_values, [ $excluded => '' ] ) );
×
33
        }
34

35
        /**
36
         * Returns the enabled authorizing URL fields.
37
         *
38
         * @return array
39
         */
40
        public static function enabled_authorizing_url_fields_value() {
41
                return apply_filters(
146✔
42
                        'woographql_enabled_authorizing_url_fields',
146✔
43
                        [
146✔
44
                                'cart_url'               => 'cart_url',
146✔
45
                                'checkout_url'           => 'checkout_url',
146✔
46
                                'account_url'            => 'account_url',
146✔
47
                                'add_payment_method_url' => 'add_payment_method_url',
146✔
48
                        ]
146✔
49
                );
146✔
50
        }
51

52
        /**
53
         * Returns General settings fields.
54
         *
55
         * @return array
56
         */
57
        public static function get_fields() {
58
                $custom_endpoint                = apply_filters( 'woographql_authorizing_url_endpoint', null );
×
59
                $enabled_authorizing_url_fields = woographql_setting( 'enable_authorizing_url_fields', [] );
×
60
                $enabled_authorizing_url_fields = ! empty( $enabled_authorizing_url_fields ) ? array_keys( $enabled_authorizing_url_fields ) : [];
×
61
                $all_urls_checked               = self::enabled_authorizing_url_fields_value();
×
62

63
                $cart_url_hardcoded               = defined( 'CART_URL_NONCE_PARAM' ) && ! empty( constant( 'CART_URL_NONCE_PARAM' ) );
×
64
                $checkout_url_hardcoded           = defined( 'CHECKOUT_URL_NONCE_PARAM' ) && ! empty( constant( 'CHECKOUT_URL_NONCE_PARAM' ) );
×
65
                $account_url_hardcoded            = defined( 'ACCOUNT_URL_NONCE_PARAM' ) && ! empty( constant( 'ACCOUNT_URL_NONCE_PARAM' ) );
×
66
                $add_payment_method_url_hardcoded = defined( 'ADD_PAYMENT_METHOD_URL_NONCE_PARAM' ) && ! empty( constant( 'ADD_PAYMENT_METHOD_URL_NONCE_PARAM' ) );
×
67

68
                $enable_auth_urls_hardcoded = defined( 'WPGRAPHQL_WOOCOMMERCE_ENABLE_AUTH_URLS' ) && ! empty( constant( 'WPGRAPHQL_WOOCOMMERCE_ENABLE_AUTH_URLS' ) );
×
69

70
                return [
×
71
                        [
×
72
                                'name'     => 'disable_ql_session_handler',
×
73
                                'label'    => __( 'Disable QL Session Handler', 'wp-graphql-woocommerce' ),
×
74
                                'desc'     => __( 'The QL Session Handler takes over management of WooCommerce Session Management on WPGraphQL request replacing the usage of HTTP Cookies with JSON Web Tokens.', 'wp-graphql-woocommerce' )
×
75
                                        . ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
×
76
                                'type'     => 'checkbox',
×
77
                                'value'    => defined( 'NO_QL_SESSION_HANDLER' ) ? 'on' : woographql_setting( 'disable_ql_session_handler', 'off' ),
×
78
                                'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
×
79
                        ],
×
NEW
80
                        [
×
NEW
81
                                'name'     => 'enable_ql_session_handler_on_ajax',
×
NEW
82
                                'label'    => __( 'Enable QL Session Handler on WC AJAX requests.', 'wp-graphql-woocommerce' ),
×
NEW
83
                                'desc'     => __( 'Enabling this will enable JSON Web Tokens usage on WC AJAX requests.', 'wp-graphql-woocommerce' )
×
NEW
84
                                        . ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
×
NEW
85
                                'type'     => 'checkbox',
×
NEW
86
                                'value'    => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_ajax', 'off' ),
×
NEW
87
                                'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
×
NEW
88
                        ],
×
NEW
89
                        [
×
NEW
90
                                'name'     => 'enable_ql_session_handler_on_rest',
×
NEW
91
                                'label'    => __( 'Enable QL Session Handler on WP REST requests.', 'wp-graphql-woocommerce' ),
×
NEW
92
                                'desc'     => __( 'Enabling this will enable JSON Web Tokens usage on WP REST requests.', 'wp-graphql-woocommerce' )
×
NEW
93
                                        . ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
×
NEW
94
                                'type'     => 'checkbox',
×
NEW
95
                                'value'    => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_rest', 'off' ),
×
NEW
96
                                'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
×
NEW
97
                        ],
×
98
                        [
×
99
                                'name'    => 'enable_unsupported_product_type',
×
100
                                'label'   => __( 'Enable Unsupported types', 'wp-graphql-woocommerce' ),
×
101
                                'desc'    => __( 'Substitute unsupported product types with SimpleProduct', 'wp-graphql-woocommerce' ),
×
102
                                'type'    => 'checkbox',
×
103
                                'default' => 'off',
×
104
                        ],
×
105
                        [
×
106
                                'name'              => 'enable_authorizing_url_fields',
×
107
                                'label'             => __( 'Enable User Session transferring URLs', 'wp-graphql-woocommerce' ),
×
108
                                'desc'              => __( 'URL fields to add to the <strong>Customer</strong> type.', 'wp-graphql-woocommerce' )
×
109
                                        . ( $enable_auth_urls_hardcoded ? __( ' This setting is disabled. The "WPGRAPHQL_WOOCOMMERCE_ENABLE_AUTH_URLS" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
×
110
                                'type'              => 'multicheck',
×
111
                                'options'           => apply_filters(
×
112
                                        'woographql_settings_enable_authorizing_url_options',
×
113
                                        [
×
114
                                                'cart_url'               => __( 'Cart URL. Field name: <strong>cartUrl</strong>', 'wp-graphql-woocommerce' ),
×
115
                                                'checkout_url'           => __( 'Checkout URL. Field name: <strong>checkoutUrl</strong>', 'wp-graphql-woocommerce' ),
×
116
                                                'account_url'            => __( 'Account URL. Field name: <strong>accountUrl</strong>', 'wp-graphql-woocommerce' ),
×
117
                                                'add_payment_method_url' => __( 'Add Payment Method URL. Field name: <strong>addPaymentMethodUrl</strong>', 'wp-graphql-woocommerce' ),
×
118
                                        ]
×
119
                                ),
×
120
                                'value'             => $enable_auth_urls_hardcoded ? $all_urls_checked : woographql_setting( 'enable_authorizing_url_fields', [] ),
×
121
                                'disabled'          => $enable_auth_urls_hardcoded,
×
122
                                'sanitize_callback' => static function ( $value ) {
×
123
                                        if ( empty( $value ) ) {
×
124
                                                return [];
×
125
                                        }
126

127
                                        return $value;
×
128
                                },
×
129
                        ],
×
130
                        [
×
131
                                'name'     => 'authorizing_url_endpoint',
×
132
                                'label'    => __( 'Endpoint for Authorizing URLs', 'wp-graphql-woocommerce' ),
×
133
                                'desc'     => sprintf(
×
134
                                        /* translators: %1$s: Site URL, %2$s: WooGraphQL Auth Endpoint */
135
                                        __( 'The endpoint (path) for transferring user sessions on the site. <a target="_blank" href="%1$s/%2$s">%1$s/%2$s</a>.', 'wp-graphql-woocommerce' ),
×
136
                                        site_url(),
×
137
                                        woographql_setting( 'authorizing_url_endpoint', 'transfer-session' )
×
138
                                ),
×
139
                                'type'     => 'text',
×
140
                                'default'  => ! empty( $custom_endpoint ) ? $custom_endpoint : 'transfer-session',
×
141
                                'disabled' => empty( $enabled_authorizing_url_fields ),
×
142
                        ],
×
143
                        [
×
144
                                'name'              => 'cart_url_nonce_param',
×
145
                                'label'             => __( 'Cart URL nonce name', 'wp-graphql-woocommerce' ),
×
146
                                'desc'              => __( 'Query parameter name of the nonce included in the "cartUrl" field', 'wp-graphql-woocommerce' )
×
147
                                        . ( $cart_url_hardcoded ? __( ' This setting is disabled. The "CART_URL_NONCE_PARAM" flag has been set with code', 'wp-graphql-woocommerce' ) : '' ),
×
148
                                'type'              => 'text',
×
149
                                'value'             => $cart_url_hardcoded ? CART_URL_NONCE_PARAM : woographql_setting( 'cart_url_nonce_param', '_wc_cart' ),
×
150
                                'disabled'          => defined( 'CART_URL_NONCE_PARAM' ) || ! in_array( 'cart_url', $enabled_authorizing_url_fields, true ),
×
151
                                'sanitize_callback' => static function ( $value ) {
×
152
                                        $other_nonces = self::get_other_nonce_values( 'cart_url' );
×
153
                                        if ( in_array( $value, $other_nonces, true ) ) {
×
154
                                                add_settings_error(
×
155
                                                        'cart_url_nonce_param',
×
156
                                                        'unique',
×
157
                                                        __( 'The <strong>Cart URL nonce name</strong> field must be unique', 'wp-graphql-woocommerce' ),
×
158
                                                        'error'
×
159
                                                );
×
160

161
                                                return '_wc_cart';
×
162
                                        }
163

164
                                        return $value;
×
165
                                },
×
166
                        ],
×
167
                        [
×
168
                                'name'              => 'checkout_url_nonce_param',
×
169
                                'label'             => __( 'Checkout URL nonce name', 'wp-graphql-woocommerce' ),
×
170
                                'desc'              => __( 'Query parameter name of the nonce included in the "checkoutUrl" field', 'wp-graphql-woocommerce' )
×
171
                                        . ( $checkout_url_hardcoded ? __( ' This setting is disabled. The "CHECKOUT_URL_NONCE_PARAM" flag has been set with code', 'wp-graphql-woocommerce' ) : '' ),
×
172
                                'type'              => 'text',
×
173
                                'value'             => $checkout_url_hardcoded ? CHECKOUT_URL_NONCE_PARAM : woographql_setting( 'checkout_url_nonce_param', '_wc_checkout' ),
×
174
                                'disabled'          => defined( 'CHECKOUT_URL_NONCE_PARAM' ) || ! in_array( 'checkout_url', $enabled_authorizing_url_fields, true ),
×
175
                                'sanitize_callback' => static function ( $value ) {
×
176
                                        $other_nonces = self::get_other_nonce_values( 'checkout_url' );
×
177
                                        if ( in_array( $value, $other_nonces, true ) ) {
×
178
                                                add_settings_error(
×
179
                                                        'checkout_url_nonce_param',
×
180
                                                        'unique',
×
181
                                                        __( 'The <strong>Checkout URL nonce name</strong> field must be unique', 'wp-graphql-woocommerce' ),
×
182
                                                        'error'
×
183
                                                );
×
184

185
                                                return '_wc_checkout';
×
186
                                        }
187

188
                                        return $value;
×
189
                                },
×
190
                        ],
×
191
                        [
×
192
                                'name'              => 'account_url_nonce_param',
×
193
                                'label'             => __( 'Account URL nonce name', 'wp-graphql-woocommerce' ),
×
194
                                'desc'              => __( 'Query parameter name of the nonce included in the "accountUrl" field', 'wp-graphql-woocommerce' )
×
195
                                        . ( $account_url_hardcoded ? __( ' This setting is disabled. The "ACCOUNT_URL_NONCE_PARAM" flag has been set with code', 'wp-graphql-woocommerce' ) : '' ),
×
196
                                'type'              => 'text',
×
197
                                'value'             => $account_url_hardcoded ? ACCOUNT_URL_NONCE_PARAM : woographql_setting( 'account_url_nonce_param', '_wc_account' ),
×
198
                                'disabled'          => defined( 'ACCOUNT_URL_NONCE_PARAM' ) || ! in_array( 'account_url', $enabled_authorizing_url_fields, true ),
×
199
                                'sanitize_callback' => static function ( $value ) {
×
200
                                        $other_nonces = self::get_other_nonce_values( 'account_url' );
×
201
                                        if ( in_array( $value, $other_nonces, true ) ) {
×
202
                                                add_settings_error(
×
203
                                                        'account_url_nonce_param',
×
204
                                                        'unique',
×
205
                                                        __( 'The <strong>Account URL nonce name</strong> field must be unique', 'wp-graphql-woocommerce' ),
×
206
                                                        'error'
×
207
                                                );
×
208

209
                                                return '_wc_account';
×
210
                                        }
211

212
                                        return $value;
×
213
                                },
×
214
                        ],
×
215
                        [
×
216
                                'name'              => 'add_payment_method_url_nonce_param',
×
217
                                'label'             => __( 'Add Payment Method URL nonce name', 'wp-graphql-woocommerce' ),
×
218
                                'desc'              => __( 'Query parameter name of the nonce included in the "addPaymentMethodUrl" field', 'wp-graphql-woocommerce' )
×
219
                                        . ( $add_payment_method_url_hardcoded ? __( ' This setting is disabled. The "ADD_PAYMENT_METHOD_URL_NONCE_PARAM" flag has been set with code', 'wp-graphql-woocommerce' ) : '' ),
×
220
                                'type'              => 'text',
×
221
                                'value'             => $add_payment_method_url_hardcoded ? ADD_PAYMENT_METHOD_URL_NONCE_PARAM : woographql_setting( 'add_payment_method_url_nonce_param', '_wc_payment' ),
×
222
                                'disabled'          => defined( 'ADD_PAYMENT_METHOD_URL_NONCE_PARAM' ) || ! in_array( 'add_payment_method_url', $enabled_authorizing_url_fields, true ),
×
223
                                'sanitize_callback' => static function ( $value ) {
×
224
                                        $other_nonces = self::get_other_nonce_values( 'add_payment_method_url' );
×
225
                                        if ( in_array( $value, $other_nonces, true ) ) {
×
226
                                                add_settings_error(
×
227
                                                        'add_payment_method_url_nonce_param',
×
228
                                                        'unique',
×
229
                                                        __( 'The <strong>Add Payment Method URL nonce name</strong> field must be unique', 'wp-graphql-woocommerce' ),
×
230
                                                        'error'
×
231
                                                );
×
232

233
                                                return '_wc_payment';
×
234
                                        }
235

236
                                        return $value;
×
237
                                },
×
238
                        ],
×
239
                ];
×
240
        }
241
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc