mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Merge pull request #2631 from woocommerce/PCP-3661-improve-context-detection-in-ajax
Improve context detection
This commit is contained in:
commit
407eff51f1
3 changed files with 81 additions and 25 deletions
|
@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\Axo\Gateway\AxoGateway;
|
||||||
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
|
||||||
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer;
|
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\ExecutableModule;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
|
||||||
|
@ -33,11 +34,20 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCGatewayConfiguration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class AxoModule
|
* Class AxoModule
|
||||||
|
*
|
||||||
|
* @psalm-suppress MissingConstructor
|
||||||
*/
|
*/
|
||||||
class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
||||||
use ModuleClassNameIdTrait;
|
use ModuleClassNameIdTrait;
|
||||||
use ContextTrait;
|
use ContextTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The session handler for ContextTrait.
|
||||||
|
*
|
||||||
|
* @var SessionHandler|null
|
||||||
|
*/
|
||||||
|
protected ?SessionHandler $session_handler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -173,6 +183,8 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
||||||
function () use ( $c ) {
|
function () use ( $c ) {
|
||||||
$module = $this;
|
$module = $this;
|
||||||
|
|
||||||
|
$this->session_handler = $c->get( 'session.handler' );
|
||||||
|
|
||||||
$settings = $c->get( 'wcgateway.settings' );
|
$settings = $c->get( 'wcgateway.settings' );
|
||||||
assert( $settings instanceof 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.
|
// Check if the module is applicable, correct country, currency, ... etc.
|
||||||
if ( ! $is_paypal_enabled
|
if ( ! $is_paypal_enabled
|
||||||
|| ! $c->get( 'axo.eligible' )
|
|| ! $c->get( 'axo.eligible' )
|
||||||
|| 'continuation' === $c->get( 'button.context' )
|
|| $this->is_paypal_continuation()
|
||||||
|| $subscription_helper->cart_contains_subscription()
|
|| $subscription_helper->cart_contains_subscription()
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -86,20 +86,28 @@ trait ContextTrait {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ( $this->is_checkout_ajax() ) {
|
||||||
* The filter returning whether to detect WC checkout ajax requests.
|
return true;
|
||||||
*/
|
|
||||||
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 ) ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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.
|
* Checks WC is_cart() + WC cart ajax requests.
|
||||||
*/
|
*/
|
||||||
|
@ -108,20 +116,40 @@ trait ContextTrait {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ( $this->is_cart_ajax() ) {
|
||||||
* The filter returning whether to detect WC cart ajax requests.
|
return true;
|
||||||
*/
|
|
||||||
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 ) ) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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.
|
* The current context.
|
||||||
*
|
*
|
||||||
|
@ -132,6 +160,14 @@ trait ContextTrait {
|
||||||
$context = 'mini-cart';
|
$context = 'mini-cart';
|
||||||
|
|
||||||
switch ( true ) {
|
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' ):
|
case is_product() || wc_post_content_has_shortcode( 'product_page' ):
|
||||||
// Do this check here instead of reordering outside conditions.
|
// Do this check here instead of reordering outside conditions.
|
||||||
// In order to have more control over the context.
|
// In order to have more control over the context.
|
||||||
|
@ -170,10 +206,6 @@ trait ContextTrait {
|
||||||
case $this->is_block_editor():
|
case $this->is_block_editor():
|
||||||
$context = 'block-editor';
|
$context = 'block-editor';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case $this->is_paypal_continuation():
|
|
||||||
$context = 'continuation';
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return apply_filters( 'woocommerce_paypal_payments_context', $context );
|
return apply_filters( 'woocommerce_paypal_payments_context', $context );
|
||||||
|
@ -207,6 +239,16 @@ trait ContextTrait {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function is_paypal_continuation(): 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.
|
* Property is already defined in trait consumers.
|
||||||
*
|
*
|
||||||
|
|
|
@ -31,8 +31,10 @@ import moduleStorage from './Helper/GooglePayStorage';
|
||||||
}
|
}
|
||||||
|
|
||||||
function bootstrapCheckout() {
|
function bootstrapCheckout() {
|
||||||
if ( context && ! [ 'continuation', 'checkout' ].includes( context ) ) {
|
if ( context
|
||||||
// Context must be missing/empty, or "continuation"/"checkout" to proceed.
|
&& ! [ 'checkout' ].includes( context )
|
||||||
|
&& ! (context === 'mini-cart' && ppcpConfig.continuation) ) {
|
||||||
|
// Context must be missing/empty, or "checkout"/checkout continuation to proceed.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ( ! CheckoutBootstrap.isPageWithCheckoutForm() ) {
|
if ( ! CheckoutBootstrap.isPageWithCheckoutForm() ) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue