Skip to content

Commit

Permalink
feat: QL Session Handler refactored to handle non-GraphQL requests (#870
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
kidunot89 authored Aug 7, 2024
1 parent d65c6a7 commit 76e8db5
Show file tree
Hide file tree
Showing 24 changed files with 457 additions and 84 deletions.
10 changes: 3 additions & 7 deletions access-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
* @return bool - True if $haystack starts with $needle, false otherwise.
*/
function str_starts_with( $haystack, $needle ) {
$length = strlen( $needle );
return ( substr( $haystack, 0, $length ) === $needle );
return 0 === strpos( $haystack, $needle ); // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters.str_starts_with
}
}

Expand All @@ -38,11 +37,8 @@ function str_starts_with( $haystack, $needle ) {
*/
function str_ends_with( $haystack, $needle ) {
$length = strlen( $needle );
if ( 0 === $length ) {
return true;
}

return ( substr( $haystack, -$length ) === $needle );
return 0 === $length
|| strpos( $haystack, $needle, - $length ) === $length - 1;
}
}//end if

Expand Down
4 changes: 2 additions & 2 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ modules:
uploads: '/wp-content/uploads'
WPLoader:
wpRootFolder: '%WP_CORE_DIR%'
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
dbName: '%DB_NAME%'
dbHost: '%DB_HOST%'
dbName: '%DB_NAME%'
dbUser: '%DB_USER%'
dbPassword: '%DB_PASSWORD%'
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
tablePrefix: '%WP_TABLE_PREFIX%'
domain: '%WORDPRESS_DOMAIN%'
adminEmail: '%ADMIN_EMAIL%'
Expand Down
18 changes: 18 additions & 0 deletions includes/admin/class-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ public static function get_fields() {
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'on' : woographql_setting( 'disable_ql_session_handler', 'off' ),
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
],
[
'name' => 'enable_ql_session_handler_on_ajax',
'label' => __( 'Enable QL Session Handler on WC AJAX requests.', 'wp-graphql-woocommerce' ),
'desc' => __( 'Enabling this will enable JSON Web Tokens usage on WC AJAX requests.', 'wp-graphql-woocommerce' )
. ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
'type' => 'checkbox',
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_ajax', 'off' ),
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
],
[
'name' => 'enable_ql_session_handler_on_rest',
'label' => __( 'Enable QL Session Handler on WP REST requests.', 'wp-graphql-woocommerce' ),
'desc' => __( 'Enabling this will enable JSON Web Tokens usage on WP REST requests.', 'wp-graphql-woocommerce' )
. ( defined( 'NO_QL_SESSION_HANDLER' ) ? __( ' This setting is disabled. The "NO_QL_SESSION_HANDLER" flag has been triggered with code', 'wp-graphql-woocommerce' ) : '' ),
'type' => 'checkbox',
'value' => defined( 'NO_QL_SESSION_HANDLER' ) ? 'off' : woographql_setting( 'enable_ql_session_handler_on_rest', 'off' ),
'disabled' => defined( 'NO_QL_SESSION_HANDLER' ),
],
[
'name' => 'enable_unsupported_product_type',
'label' => __( 'Enable Unsupported types', 'wp-graphql-woocommerce' ),
Expand Down
3 changes: 2 additions & 1 deletion includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public function init() {
Mutation\Tax_Rate_Create::register_mutation();
Mutation\Tax_Rate_Delete::register_mutation();
Mutation\Tax_Rate_Update::register_mutation();
Mutation\Update_Session::register_mutation();
Mutation\Session_Delete::register_mutation();
Mutation\Session_Update::register_mutation();
}
}
22 changes: 21 additions & 1 deletion includes/class-woocommerce-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,34 @@ public static function get_authorizing_url_nonce_param_name( $field ) {
return woographql_setting( "{$field}_nonce_param", null );
}

/**
* Returns true if the session handler should be loaded.
*
* @return boolean
*/
public static function should_load_session_handler() {
switch ( true ) {
case \WPGraphQL\Router::is_graphql_http_request():
//phpcs:disable
case 'on' === woographql_setting( 'enable_ql_session_handler_on_ajax', 'off' )
&& ( ! empty( $_GET['wc-ajax'] ) || defined( 'WC_DOING_AJAX' ) ):
//phpcs:enable
case 'on' === woographql_setting( 'enable_ql_session_handler_on_rest', 'off' )
&& ( defined( 'REST_REQUEST' ) && REST_REQUEST ):
return true;
default:
return false;
}
}

/**
* WooCommerce Session Handler callback
*
* @param string $session_class Class name of WooCommerce Session Handler.
* @return string
*/
public static function woocommerce_session_handler( $session_class ) {
if ( \WPGraphQL\Router::is_graphql_http_request() ) {
if ( self::should_load_session_handler() ) {
$session_class = '\WPGraphQL\WooCommerce\Utils\QL_Session_Handler';
} elseif ( WooGraphQL::auth_router_is_enabled() ) {
require_once get_includes_directory() . 'utils/class-protected-router.php';
Expand Down
3 changes: 2 additions & 1 deletion includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ private function includes() {
require $include_directory_path . 'mutation/class-review-update.php';
require $include_directory_path . 'mutation/class-payment-method-delete.php';
require $include_directory_path . 'mutation/class-payment-method-set-default.php';
require $include_directory_path . 'mutation/class-session-delete.php';
require $include_directory_path . 'mutation/class-session-update.php';
require $include_directory_path . 'mutation/class-shipping-zone-create.php';
require $include_directory_path . 'mutation/class-shipping-zone-delete.php';
require $include_directory_path . 'mutation/class-shipping-zone-locations-clear.php';
Expand All @@ -356,7 +358,6 @@ private function includes() {
require $include_directory_path . 'mutation/class-tax-rate-create.php';
require $include_directory_path . 'mutation/class-tax-rate-delete.php';
require $include_directory_path . 'mutation/class-tax-rate-update.php';
require $include_directory_path . 'mutation/class-update-session.php';

// Include connection class/function files.
require $include_directory_path . 'connection/wc-cpt-connection-args.php';
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-add-fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public static function mutate_and_get_payload() {
// Add cart fee.
\WC()->cart->add_fee( ...$cart_fee_args );

do_action( 'woographql_update_session', true );

// Return payload.
return [ 'id' => \sanitize_title( $input['name'] ) ];
};
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-apply-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public static function mutate_and_get_payload() {
$reason = '';
// If validate and successful applied to cart, return payload.
if ( Cart_Mutation::validate_coupon( $input['code'], $reason ) && \WC()->cart->apply_coupon( $input['code'] ) ) {
do_action( 'woographql_update_session', true );

return [ 'code' => $input['code'] ];
}

Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-empty.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static function mutate_and_get_payload() {
*/
do_action( 'graphql_woocommerce_after_empty_cart', $cloned_cart, $input, $context, $info );

do_action( 'woographql_update_session', true );

return [ 'cart' => $cloned_cart ];
};
}
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-fill.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ public static function mutate_and_get_payload() {
// Recalculate totals.
\WC()->cart->calculate_totals();

do_action( 'woographql_update_session', true );

// Return payload.
return compact(
'added',
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-remove-coupons.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public static function mutate_and_get_payload() {
}
}

do_action( 'woographql_update_session', true );

// Return payload.
return [ 'cart' => \WC()->cart ];
};
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-remove-items.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public static function mutate_and_get_payload() {
}
}

do_action( 'woographql_update_session', true );

// Return payload.
return [ 'items' => $cart_items ];
};
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-restore-items.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public static function mutate_and_get_payload() {

$cart_items = Cart_Mutation::retrieve_cart_items( $input, $context, $info, 'restore' );

do_action( 'woographql_update_session', true );

// Return payload.
return [ 'items' => $cart_items ];
};
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-update-item-quantities.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ static function ( $value ) {
$info
);

do_action( 'woographql_update_session', true );

return [
'removed' => $removed_items,
'updated' => array_keys( $updated ),
Expand Down
2 changes: 2 additions & 0 deletions includes/mutation/class-cart-update-shipping-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public static function mutate_and_get_payload() {
// Recalculate totals.
\WC()->cart->calculate_totals();

do_action( 'woographql_update_session', true );

return [];
};
}
Expand Down
4 changes: 4 additions & 0 deletions includes/mutation/class-customer-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ public static function mutate_and_get_payload() {
// Save customer and get customer ID.
$customer->save();

if ( $session_only ) {
do_action( 'woographql_update_session', true );
}

// Return payload.
return ! empty( $payload ) ? $payload : [ 'id' => 'session' ];
};
Expand Down
95 changes: 95 additions & 0 deletions includes/mutation/class-session-delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Mutation - forgetSession
*
* Registers mutation for deleting sessions from the DB.
*
* @package WPGraphQL\WooCommerce\Mutation
* @since TBD
*/

namespace WPGraphQL\WooCommerce\Mutation;

use WPGraphQL\WooCommerce\Data\Mutation\Cart_Mutation;

/**
* Class - Session_Delete
*/
class Session_Delete {
/**
* Registers mutation
*
* @return void
*/
public static function register_mutation() {
register_graphql_mutation(
'forgetSession',
[
'inputFields' => [],
'outputFields' => self::get_output_fields(),
'mutateAndGetPayload' => self::mutate_and_get_payload(),
]
);
}

/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return [
'session' => [
'type' => [ 'list_of' => 'MetaData' ],
'resolve' => static function ( $payload ) {
// Guard against missing session data.
if ( empty( $payload['session'] ) ) {
return [];
}

// Prepare session data.
$session = [];
foreach ( $payload['session'] as $key => $value ) {
$meta = new \stdClass();
$meta->id = null;
$meta->key = $key;
$meta->value = maybe_unserialize( $value );
$session[] = $meta;
}

return $session;
},
],
];
}

/**
* Defines the mutation data modification closure.
*
* @return callable
*/
public static function mutate_and_get_payload() {
return static function ( $input ) {
Cart_Mutation::check_session_token();

/**
* Session handler.
*
* @var \WPGraphQL\WooCommerce\Utils\QL_Session_Handler $session
*/
$session = \WC()->session;

// Get session data.
$session_data = $session->get_session_data();
do_action( 'woographql_before_forget_session', $session_data, $input, $session );

// Clear session data.
$session->forget_session();

do_action( 'woographql_after_forget_session', $session_data, $input, $session );

// Return payload.
return [ 'session' => $session_data ];
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use WPGraphQL\WooCommerce\Model\Customer;

/**
* Class - Update_Session
* Class - Session_Update
*/
class Update_Session {
class Session_Update {
/**
* Registers mutation
*
Expand Down
Loading

0 comments on commit 76e8db5

Please sign in to comment.