From f77d08133df143c0d1d096cea1db2a57161d841e Mon Sep 17 00:00:00 2001 From: "Alex P." Date: Mon, 23 Sep 2024 09:09:18 +0300 Subject: [PATCH] Check cart/checkout ajax before other context checks --- .../ppcp-button/src/Helper/ContextTrait.php | 72 ++++++++++++++----- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-button/src/Helper/ContextTrait.php b/modules/ppcp-button/src/Helper/ContextTrait.php index 9f58cbcdf..44119d67a 100644 --- a/modules/ppcp-button/src/Helper/ContextTrait.php +++ b/modules/ppcp-button/src/Helper/ContextTrait.php @@ -53,20 +53,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 ) ) { - return 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. */ @@ -75,20 +83,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 ) ) { - return 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. * @@ -99,6 +127,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.