Merge pull request #2631 from woocommerce/PCP-3661-improve-context-detection-in-ajax

Improve context detection
This commit is contained in:
Emili Castells 2024-10-03 10:43:13 +02:00 committed by GitHub
commit 407eff51f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 81 additions and 25 deletions

View file

@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
@ -33,11 +34,20 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCGatewayConfiguration;
/**
* Class AxoModule
*
* @psalm-suppress MissingConstructor
*/
class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
use ModuleClassNameIdTrait;
use ContextTrait;
/**
* The session handler for ContextTrait.
*
* @var SessionHandler|null
*/
protected ?SessionHandler $session_handler;
/**
* {@inheritDoc}
*/
@ -173,6 +183,8 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
function () use ( $c ) {
$module = $this;
$this->session_handler = $c->get( 'session.handler' );
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
@ -184,7 +196,7 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $is_paypal_enabled
|| ! $c->get( 'axo.eligible' )
|| 'continuation' === $c->get( 'button.context' )
|| $this->is_paypal_continuation()
|| $subscription_helper->cart_contains_subscription()
) {
return;

View file

@ -86,20 +86,28 @@ trait ContextTrait {
return true;
}
/**
* The filter returning whether to detect WC checkout ajax requests.
*/
if ( apply_filters( 'ppcp_check_ajax_checkout', true ) ) {
// phpcs:ignore WordPress.Security
$wc_ajax = $_GET['wc-ajax'] ?? '';
if ( in_array( $wc_ajax, array( 'update_order_review' ), true ) ) {
if ( $this->is_checkout_ajax() ) {
return true;
}
}
return false;
}
/**
* Checks if performing the WC checkout ajax requests.
*/
private function is_checkout_ajax(): bool {
/**
* The filter returning whether to detect WC checkout ajax requests.
*/
if ( ! apply_filters( 'ppcp_check_ajax_checkout', true ) ) {
return false;
}
$wc_ajax = $this->wc_ajax_endpoint_name();
return in_array( $wc_ajax, array( 'update_order_review' ), true );
}
/**
* Checks WC is_cart() + WC cart ajax requests.
*/
@ -108,20 +116,40 @@ trait ContextTrait {
return true;
}
/**
* The filter returning whether to detect WC cart ajax requests.
*/
if ( apply_filters( 'ppcp_check_ajax_cart', true ) ) {
// phpcs:ignore WordPress.Security
$wc_ajax = $_GET['wc-ajax'] ?? '';
if ( in_array( $wc_ajax, array( 'update_shipping_method' ), true ) ) {
if ( $this->is_cart_ajax() ) {
return true;
}
}
return false;
}
/**
* Checks if performing the WC cart ajax requests.
*/
private function is_cart_ajax(): bool {
/**
* The filter returning whether to detect WC checkout ajax requests.
*/
if ( ! apply_filters( 'ppcp_check_ajax_cart', true ) ) {
return false;
}
$wc_ajax = $this->wc_ajax_endpoint_name();
return in_array( $wc_ajax, array( 'update_shipping_method' ), true );
}
/**
* Returns the current WC ajax endpoint name or an empty string if not in ajax.
*/
private function wc_ajax_endpoint_name(): string {
// phpcs:ignore WordPress.Security
$wc_ajax = $_GET['wc-ajax'] ?? '';
if ( ! is_string( $wc_ajax ) ) {
return '';
}
return $wc_ajax;
}
/**
* The current context.
*
@ -132,6 +160,14 @@ trait ContextTrait {
$context = 'mini-cart';
switch ( true ) {
case $this->is_cart_ajax():
$context = 'cart';
break;
case $this->is_checkout_ajax() && ! $this->is_paypal_continuation():
$context = 'checkout';
break;
case is_product() || wc_post_content_has_shortcode( 'product_page' ):
// Do this check here instead of reordering outside conditions.
// In order to have more control over the context.
@ -170,10 +206,6 @@ trait ContextTrait {
case $this->is_block_editor():
$context = 'block-editor';
break;
case $this->is_paypal_continuation():
$context = 'continuation';
break;
}
return apply_filters( 'woocommerce_paypal_payments_context', $context );
@ -207,6 +239,16 @@ trait ContextTrait {
* @return bool
*/
private function is_paypal_continuation(): bool {
/**
* Cannot guarantee that initialized in all places where this trait is used,
* the Psalm checks seem to work weird and sometimes ignore missing property.
*
* @psalm-suppress RedundantPropertyInitializationCheck
*/
if ( ! isset( $this->session_handler ) ) {
return false;
}
/**
* Property is already defined in trait consumers.
*

View file

@ -31,8 +31,10 @@ import moduleStorage from './Helper/GooglePayStorage';
}
function bootstrapCheckout() {
if ( context && ! [ 'continuation', 'checkout' ].includes( context ) ) {
// Context must be missing/empty, or "continuation"/"checkout" to proceed.
if ( context
&& ! [ 'checkout' ].includes( context )
&& ! (context === 'mini-cart' && ppcpConfig.continuation) ) {
// Context must be missing/empty, or "checkout"/checkout continuation to proceed.
return;
}
if ( ! CheckoutBootstrap.isPageWithCheckoutForm() ) {