Skip to content

Commit 76e8db5

Browse files
authored
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
1 parent d65c6a7 commit 76e8db5

24 files changed

+457
-84
lines changed

access-functions.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
* @return bool - True if $haystack starts with $needle, false otherwise.
2020
*/
2121
function str_starts_with( $haystack, $needle ) {
22-
$length = strlen( $needle );
23-
return ( substr( $haystack, 0, $length ) === $needle );
22+
return 0 === strpos( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.str_starts_with
2423
}
2524
}
2625

@@ -38,11 +37,8 @@ function str_starts_with( $haystack, $needle ) {
3837
*/
3938
function str_ends_with( $haystack, $needle ) {
4039
$length = strlen( $needle );
41-
if ( 0 === $length ) {
42-
return true;
43-
}
44-
45-
return ( substr( $haystack, -$length ) === $needle );
40+
return 0 === $length
41+
|| strpos( $haystack, $needle, - $length ) === $length - 1;
4642
}
4743
}//end if
4844

codeception.dist.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ modules:
6666
uploads: '/wp-content/uploads'
6767
WPLoader:
6868
wpRootFolder: '%WP_CORE_DIR%'
69-
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
70-
dbName: '%DB_NAME%'
7169
dbHost: '%DB_HOST%'
70+
dbName: '%DB_NAME%'
7271
dbUser: '%DB_USER%'
7372
dbPassword: '%DB_PASSWORD%'
73+
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
7474
tablePrefix: '%WP_TABLE_PREFIX%'
7575
domain: '%WORDPRESS_DOMAIN%'
7676
adminEmail: '%ADMIN_EMAIL%'

includes/admin/class-general.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ public static function get_fields() {
7777
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'on' : woographql_setting( 'disable_ql_session_handler', 'off' ),
7878
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
7979
],
80+
[
81+
'name' => 'enable_ql_session_handler_on_ajax',
82+
'label' => __( 'Enable QL Session Handler on WC AJAX requests.', 'wp-graphql-woocommerce' ),
83+
'desc' => __( 'Enabling this will enable JSON Web Tokens usage on WC AJAX requests.', 'wp-graphql-woocommerce' )
84+
. ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
85+
'type' => 'checkbox',
86+
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_ajax', 'off' ),
87+
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
88+
],
89+
[
90+
'name' => 'enable_ql_session_handler_on_rest',
91+
'label' => __( 'Enable QL Session Handler on WP REST requests.', 'wp-graphql-woocommerce' ),
92+
'desc' => __( 'Enabling this will enable JSON Web Tokens usage on WP REST requests.', 'wp-graphql-woocommerce' )
93+
. ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
94+
'type' => 'checkbox',
95+
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_rest', 'off' ),
96+
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
97+
],
8098
[
8199
'name' => 'enable_unsupported_product_type',
82100
'label' => __( 'Enable Unsupported types', 'wp-graphql-woocommerce' ),

includes/class-type-registry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public function init() {
199199
Mutation\Tax_Rate_Create::register_mutation();
200200
Mutation\Tax_Rate_Delete::register_mutation();
201201
Mutation\Tax_Rate_Update::register_mutation();
202-
Mutation\Update_Session::register_mutation();
202+
Mutation\Session_Delete::register_mutation();
203+
Mutation\Session_Update::register_mutation();
203204
}
204205
}

includes/class-woocommerce-filters.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,34 @@ public static function get_authorizing_url_nonce_param_name( $field ) {
8282
return woographql_setting( "{$field}_nonce_param", null );
8383
}
8484

85+
/**
86+
* Returns true if the session handler should be loaded.
87+
*
88+
* @return boolean
89+
*/
90+
public static function should_load_session_handler() {
91+
switch ( true ) {
92+
case \WPGraphQL\Router::is_graphql_http_request():
93+
//phpcs:disable
94+
case 'on' === woographql_setting( 'enable_ql_session_handler_on_ajax', 'off' )
95+
&& ( ! empty( $_GET['wc-ajax'] ) || defined( 'WC_DOING_AJAX' ) ):
96+
//phpcs:enable
97+
case 'on' === woographql_setting( 'enable_ql_session_handler_on_rest', 'off' )
98+
&& ( defined( 'REST_REQUEST' ) && REST_REQUEST ):
99+
return true;
100+
default:
101+
return false;
102+
}
103+
}
104+
85105
/**
86106
* WooCommerce Session Handler callback
87107
*
88108
* @param string $session_class Class name of WooCommerce Session Handler.
89109
* @return string
90110
*/
91111
public static function woocommerce_session_handler( $session_class ) {
92-
if ( \WPGraphQL\Router::is_graphql_http_request() ) {
112+
if ( self::should_load_session_handler() ) {
93113
$session_class = '\WPGraphQL\WooCommerce\Utils\QL_Session_Handler';
94114
} elseif ( WooGraphQL::auth_router_is_enabled() ) {
95115
require_once get_includes_directory() . 'utils/class-protected-router.php';

includes/class-wp-graphql-woocommerce.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ private function includes() {
343343
require $include_directory_path . 'mutation/class-review-update.php';
344344
require $include_directory_path . 'mutation/class-payment-method-delete.php';
345345
require $include_directory_path . 'mutation/class-payment-method-set-default.php';
346+
require $include_directory_path . 'mutation/class-session-delete.php';
347+
require $include_directory_path . 'mutation/class-session-update.php';
346348
require $include_directory_path . 'mutation/class-shipping-zone-create.php';
347349
require $include_directory_path . 'mutation/class-shipping-zone-delete.php';
348350
require $include_directory_path . 'mutation/class-shipping-zone-locations-clear.php';
@@ -356,7 +358,6 @@ private function includes() {
356358
require $include_directory_path . 'mutation/class-tax-rate-create.php';
357359
require $include_directory_path . 'mutation/class-tax-rate-delete.php';
358360
require $include_directory_path . 'mutation/class-tax-rate-update.php';
359-
require $include_directory_path . 'mutation/class-update-session.php';
360361

361362
// Include connection class/function files.
362363
require $include_directory_path . 'connection/wc-cpt-connection-args.php';

includes/mutation/class-cart-add-fee.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public static function mutate_and_get_payload() {
106106
// Add cart fee.
107107
\WC()->cart->add_fee( ...$cart_fee_args );
108108

109+
do_action( 'woographql_update_session', true );
110+
109111
// Return payload.
110112
return [ 'id' => \sanitize_title( $input['name'] ) ];
111113
};

includes/mutation/class-cart-apply-coupon.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public static function mutate_and_get_payload() {
7676
$reason = '';
7777
// If validate and successful applied to cart, return payload.
7878
if ( Cart_Mutation::validate_coupon( $input['code'], $reason ) && \WC()->cart->apply_coupon( $input['code'] ) ) {
79+
do_action( 'woographql_update_session', true );
80+
7981
return [ 'code' => $input['code'] ];
8082
}
8183

includes/mutation/class-cart-empty.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public static function mutate_and_get_payload() {
9494
*/
9595
do_action( 'graphql_woocommerce_after_empty_cart', $cloned_cart, $input, $context, $info );
9696

97+
do_action( 'woographql_update_session', true );
98+
9799
return [ 'cart' => $cloned_cart ];
98100
};
99101
}

includes/mutation/class-cart-fill.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ public static function mutate_and_get_payload() {
270270
// Recalculate totals.
271271
\WC()->cart->calculate_totals();
272272

273+
do_action( 'woographql_update_session', true );
274+
273275
// Return payload.
274276
return compact(
275277
'added',

0 commit comments

Comments
 (0)