Skip to content

Commit

Permalink
fix: More cart session save triggered implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Jun 20, 2024
1 parent 6edbc8b commit 6ad9d11
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 32 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 $length === 0
|| $length - 1 === strpos( $haystack, $needle, - $length );
}
}//end if

Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
3 changes: 3 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,9 @@ 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
2 changes: 1 addition & 1 deletion includes/type/object/class-root-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function register_fields() {
throw new UserError( $token_invalid );
}

$cart = Factory::resolve_cart();
$cart = new \WC_Cart();
if ( ! empty( $args['recalculateTotals'] ) ) {
$cart->calculate_totals();
}
Expand Down
11 changes: 11 additions & 0 deletions includes/utils/class-ql-session-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ public function init() {
}
}

/**
* Mark the session as dirty.
*
* To trigger a save of the session data.
*
* @return void
*/
public function mark_dirty() {
$this->_dirty = true;
}

/**
* Setup token and customer ID.
*
Expand Down
7 changes: 7 additions & 0 deletions includes/utils/class-session-transaction-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ public function __construct( &$session_handler ) {

add_action( 'graphql_before_resolve_field', [ $this, 'update_transaction_queue' ], 10, 4 );
add_action( 'graphql_mutation_response', [ $this, 'pop_transaction_id' ], 20, 6 );

add_action( 'woographql_session_transaction_complete', [ $this->session_handler, 'save_if_dirty' ], 10 );

add_action( 'woocommerce_add_to_cart', [ $this->session_handler, 'mark_dirty' ] );
add_action( 'woocommerce_cart_item_removed', [ $this->session_handler, 'mark_dirty' ] );
add_action( 'woocommerce_cart_item_restored', [ $this->session_handler, 'mark_dirty' ] );
add_action( 'woocommerce_cart_item_set_quantity', [ $this->session_handler, 'mark_dirty' ] );
add_action( 'woocommerce_cart_emptied', [ $this->session_handler, 'mark_dirty' ] );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/CartTransactionQueueCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public function _startAuthenticatedSession( $I ) {
);

// Retrieve JWT Authorization Token for later use.
$auth_token = $I->lodashGet( $success, 'login.authToken' );
$auth_token = $I->lodashGet( $success, 'data.login.authToken' );

// Retrieve session token. Add as "Session %s" in the woocommerce-session HTTP header to future requests
// so WooCommerce can identify the user session associated with actions made in the GraphQL requests.
// You can also retrieve the token from the "woocommerce-session" HTTP response header.
$initial_session_token = $I->lodashGet( $success, 'login.sessionToken' );
$initial_session_token = $I->lodashGet( $success, 'data.login.sessionToken' );

$headers = [
'Authorization' => "Bearer {$auth_token}",
Expand Down
14 changes: 4 additions & 10 deletions vendor-prefixed/firebase/php-jwt/src/CachedKeySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,15 @@ private function rateLimitExceeded(): bool
}

$cacheItem = $this->cache->getItem($this->rateLimitCacheKey);

$cacheItemData = [];
if ($cacheItem->isHit() && \is_array($data = $cacheItem->get())) {
$cacheItemData = $data;
if (!$cacheItem->isHit()) {
$cacheItem->expiresAfter(1); // # of calls are cached each minute
}

$callsPerMinute = $cacheItemData['callsPerMinute'] ?? 0;
$expiry = $cacheItemData['expiry'] ?? new \DateTime('+60 seconds', new \DateTimeZone('UTC'));

$callsPerMinute = (int) $cacheItem->get();
if (++$callsPerMinute > $this->maxCallsPerMinute) {
return true;
}

$cacheItem->set(['expiry' => $expiry, 'callsPerMinute' => $callsPerMinute]);
$cacheItem->expiresAt($expiry);
$cacheItem->set($callsPerMinute);
$this->cache->save($cacheItem);
return false;
}
Expand Down
3 changes: 0 additions & 3 deletions vendor-prefixed/firebase/php-jwt/src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,6 @@ public static function sign(
return \hash_hmac($algorithm, $msg, $key, true);
case 'openssl':
$signature = '';
if (!\is_resource($key) && !openssl_pkey_get_private($key)) {
throw new DomainException('OpenSSL unable to validate key');
}
$success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line
if (!$success) {
throw new DomainException('OpenSSL unable to sign data');
Expand Down

0 comments on commit 6ad9d11

Please sign in to comment.