diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php index 0ff21cbdb7..5040f81e20 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway-voucher.php @@ -210,7 +210,7 @@ public function get_icon() { * @since 5.8.0 */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -225,7 +225,7 @@ public function payment_scripts() { public function javascript_params() { $stripe_params = parent::javascript_params(); - if ( $this->is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order_id = absint( get_query_var( 'order-pay' ) ); $stripe_params['stripe_order_key'] = ! empty( $order_id ) ? wc_get_order( $order_id )->get_order_key() : null; } diff --git a/includes/abstracts/abstract-wc-stripe-payment-gateway.php b/includes/abstracts/abstract-wc-stripe-payment-gateway.php index c4c0046164..4d879bb692 100644 --- a/includes/abstracts/abstract-wc-stripe-payment-gateway.php +++ b/includes/abstracts/abstract-wc-stripe-payment-gateway.php @@ -1946,62 +1946,24 @@ public function is_valid_us_zip_code( $zip ) { * Checks if the current page is the pay for order page and the current user is allowed to pay for the order. * * @return bool + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_valid_pay_for_order_endpoint() instead. */ public function is_valid_pay_for_order_endpoint(): bool { - - // If not on the pay for order page, return false. - if ( ! is_wc_endpoint_url( 'order-pay' ) || ! isset( $_GET['key'] ) ) { - return false; - } - - $order_id = absint( get_query_var( 'order-pay' ) ); - $order = wc_get_order( $order_id ); - - // If the order is not found or the param `key` is not set or the order key does not match the order key in the URL param, return false. - if ( ! $order || ! isset( $_GET['key'] ) || wc_clean( wp_unslash( $_GET['key'] ) ) !== $order->get_order_key() ) { - return false; - } - - // If the order doesn't need payment, we don't need to prepare the payment page. - if ( ! $order->needs_payment() ) { - return false; - } - - return current_user_can( 'pay_for_order', $order->get_id() ); + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_valid_pay_for_order_endpoint()' ); + return WC_Stripe_Page_Helper::is_valid_pay_for_order(); } /** * Checks if the current page is the order received page and the current user is allowed to manage the order. * * @return bool + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_valid_order_received_endpoint() instead. */ public function is_valid_order_received_endpoint(): bool { - // Verify nonce. Duplicated here in order to avoid PHPCS warnings. - if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wpnonce'] ) ), 'wc_stripe_process_redirect_order_nonce' ) ) { - return false; - } - - // If not on the order-received page, return false. - if ( ! is_wc_endpoint_url( 'order-received' ) || ! isset( $_GET['key'] ) ) { - return false; - } - - $order_id_from_order_key = absint( wc_get_order_id_by_order_key( wc_clean( wp_unslash( $_GET['key'] ) ) ) ); - $order_id_from_query_var = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : null; - - // If the order ID is not found or the order ID does not match the given order ID, return false. - if ( ! $order_id_from_order_key || ( $order_id_from_query_var !== $order_id_from_order_key ) ) { - return false; - } - - $order = wc_get_order( $order_id_from_order_key ); - - // If the order doesn't need payment, return false. - if ( ! $order->needs_payment() ) { - return false; - } - - return current_user_can( 'pay_for_order', $order->get_id() ); + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_valid_order_received_endpoint()' ); + return WC_Stripe_Page_Helper::is_valid_order_received(); } /** @@ -2050,22 +2012,22 @@ public function get_localized_error_message_from_response( $response ) { * @version 4.0.0 */ public function payment_scripts() { - if ( ( ! is_product() - && ! WC_Stripe_Helper::has_cart_or_checkout_on_current_page() - && ! $this->is_valid_pay_for_order_endpoint() + if ( ( ! WC_Stripe_Page_Helper::is_product() + && ! WC_Stripe_Page_Helper::is_cart_or_checkout() + && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() - && ! isset( $_GET['change_payment_method'] ) // phpcs:ignore WordPress.Security.NonceVerification + && ! WC_Stripe_Page_Helper::is_change_payment_method() && ! ( ! empty( get_query_var( 'view-subscription' ) ) && is_callable( 'WCS_Early_Renewal_Manager::is_early_renewal_via_modal_enabled' ) && WCS_Early_Renewal_Manager::is_early_renewal_via_modal_enabled() ) // @phpstan-ignore-line (Class WCS_Early_Renewal_Manager is checked already) - ) || ( is_order_received_page() ) + ) || WC_Stripe_Page_Helper::is_order_received() ) { return; } - if ( is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) { + if ( WC_Stripe_Page_Helper::is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) { return; } - if ( is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) { + if ( WC_Stripe_Page_Helper::is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) { return; } @@ -2150,7 +2112,7 @@ public function javascript_params() { ]; // If we're on the pay page we need to pass stripe.js the address of the order. - if ( $this->is_valid_pay_for_order_endpoint() || $this->is_changing_payment_method_for_subscription() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() || $this->is_changing_payment_method_for_subscription() ) { $order_id = absint( get_query_var( 'order-pay' ) ); $order = wc_get_order( $order_id ); @@ -2183,7 +2145,7 @@ public function javascript_params() { $stripe_params['sepa_mandate_notification'] = apply_filters( 'wc_stripe_sepa_mandate_notification', 'email' ); $stripe_params['allow_prepaid_card'] = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no'; $stripe_params['inline_cc_form'] = ( isset( $this->inline_cc_form ) && $this->inline_cc_form ) ? 'yes' : 'no'; - $stripe_params['is_checkout'] = ( is_checkout() && empty( $_GET['pay_for_order'] ) ) ? 'yes' : 'no'; // wpcs: csrf ok. + $stripe_params['is_checkout'] = ( WC_Stripe_Page_Helper::is_checkout() && empty( $_GET['pay_for_order'] ) ) ? 'yes' : 'no'; // wpcs: csrf ok. $stripe_params['return_url'] = $this->get_stripe_return_url(); $stripe_params['ajaxurl'] = WC_AJAX::get_endpoint( '%%endpoint%%' ); $stripe_params['stripe_nonce'] = wp_create_nonce( '_wc_stripe_nonce' ); @@ -2191,9 +2153,9 @@ public function javascript_params() { $stripe_params['elements_options'] = apply_filters( 'wc_stripe_elements_options', [] ); $stripe_params['sepa_elements_options'] = $sepa_elements_options; $stripe_params['invalid_owner_name'] = __( 'Billing First Name and Last Name are required.', 'woocommerce-gateway-stripe' ); - $stripe_params['is_change_payment_page'] = isset( $_GET['change_payment_method'] ) ? 'yes' : 'no'; // wpcs: csrf ok. + $stripe_params['is_change_payment_page'] = WC_Stripe_Page_Helper::is_change_payment_method(); $stripe_params['is_add_payment_page'] = is_wc_endpoint_url( 'add-payment-method' ) ? 'yes' : 'no'; - $stripe_params['is_pay_for_order_page'] = is_wc_endpoint_url( 'order-pay' ) ? 'yes' : 'no'; + $stripe_params['is_pay_for_order_page'] = WC_Stripe_Page_Helper::is_pay_for_order() ? 'yes' : 'no'; $stripe_params['elements_styling'] = apply_filters( 'wc_stripe_elements_styling', false ); $stripe_params['elements_classes'] = apply_filters( 'wc_stripe_elements_classes', false ); $stripe_params['add_card_nonce'] = wp_create_nonce( 'wc_stripe_create_si' ); diff --git a/includes/admin/class-wc-stripe-admin-notices.php b/includes/admin/class-wc-stripe-admin-notices.php index 0279338d4a..64356d93b3 100644 --- a/includes/admin/class-wc-stripe-admin-notices.php +++ b/includes/admin/class-wc-stripe-admin-notices.php @@ -200,10 +200,7 @@ public function stripe_check_environment() { if ( isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) { // Check if Stripe is in test mode. if ( $testmode ) { - // phpcs:ignore - $is_stripe_settings_page = isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' ); - - if ( $is_stripe_settings_page ) { + if ( WC_Stripe_Page_Helper::is_admin_settings() ) { $testmode_notice_message = sprintf( /* translators: 1) HTML strong open tag 2) HTML strong closing tag */ __( '%1$sTest mode active:%2$s All transactions are simulated. Customers can\'t make real purchases through Stripe.', 'woocommerce-gateway-stripe' ), @@ -271,7 +268,7 @@ public function stripe_check_environment() { if ( empty( $show_keys_notice ) ) { $secret = WC_Stripe_API::get_secret_key(); // phpcs:ignore - $should_show_notice_on_page = ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' ) ); + $should_show_notice_on_page = ! WC_Stripe_Page_Helper::is_admin_settings(); if ( empty( $secret ) && $should_show_notice_on_page ) { $setting_link = $this->get_setting_link(); @@ -398,7 +395,7 @@ public function payment_methods_check_environment() { $payment_methods = $this->get_payment_methods(); // phpcs:ignore - $is_stripe_settings_page = isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' ); + $is_stripe_settings_page = WC_Stripe_Page_Helper::is_admin_settings(); $currency_messages = ''; foreach ( $payment_methods as $method => $class ) { diff --git a/includes/class-wc-gateway-stripe.php b/includes/class-wc-gateway-stripe.php index 51e8f46024..10957a4f4a 100644 --- a/includes/class-wc-gateway-stripe.php +++ b/includes/class-wc-gateway-stripe.php @@ -145,7 +145,7 @@ public function __construct() { */ public function get_title() { // Change the title on the payment methods settings page to include the number of enabled payment methods. - if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] ) { + if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && WC_Stripe_Page_Helper::is_admin_settings() ) { $enabled_payment_methods_count = count( WC_Stripe_Helper::get_legacy_enabled_payment_method_ids() ); $this->title = $enabled_payment_methods_count ? /* translators: $1. Count of enabled payment methods. */ @@ -214,7 +214,7 @@ public function init_form_fields() { public function payment_fields() { global $wp; $user = wp_get_current_user(); - $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; + $display_tokenization = $this->supports( 'tokenization' ) && WC_Stripe_Page_Helper::is_checkout() && $this->saved_cards; $user_email = ''; $description = $this->get_description(); $description = ! empty( $description ) ? $description : ''; @@ -222,7 +222,7 @@ public function payment_fields() { $lastname = ''; // If paying for order, we need to get email from the order not the user account. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $user_email = $order->get_billing_email(); } elseif ( $user->ID ) { @@ -260,8 +260,7 @@ public function payment_fields() { $this->elements_form(); - if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) { // wpcs: csrf ok. - + if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! WC_Stripe_Page_Helper::is_change_payment_method() ) { $this->save_payment_method_checkbox(); } @@ -488,7 +487,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source = // If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings. WC_Stripe_Helper::set_payment_awaiting_action( $order ); - if ( is_wc_endpoint_url( 'order-pay' ) ) { + if ( WC_Stripe_Page_Helper::is_pay_for_order() ) { $redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) ); return [ @@ -659,7 +658,7 @@ public function retry_after_error( $response, $order, $retry, $force_save_source * @return WC_Payment_Gateway[] Either the same list or an empty one in the right conditions. */ public function prepare_order_pay_page( $gateways ) { - if ( ! is_wc_endpoint_url( 'order-pay' ) || ! isset( $_GET['wc-stripe-confirmation'] ) ) { // wpcs: csrf ok. + if ( ! WC_Stripe_Page_Helper::is_pay_for_order() || ! isset( $_GET['wc-stripe-confirmation'] ) ) { // wpcs: csrf ok. return $gateways; } diff --git a/includes/class-wc-stripe-helper.php b/includes/class-wc-stripe-helper.php index f3cf3b77d6..44f15ef54e 100644 --- a/includes/class-wc-stripe-helper.php +++ b/includes/class-wc-stripe-helper.php @@ -1181,8 +1181,11 @@ public static function convert_wc_locale_to_stripe_locale( $wc_locale ) { * * @since 5.2.3 * @return boolean + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_cart_or_checkout() instead */ public static function has_cart_or_checkout_on_current_page() { + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_cart_or_checkout()' ); return is_cart() || is_checkout() || has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ); } diff --git a/includes/class-wc-stripe-order-handler.php b/includes/class-wc-stripe-order-handler.php index 81401cf317..84506f08e3 100644 --- a/includes/class-wc-stripe-order-handler.php +++ b/includes/class-wc-stripe-order-handler.php @@ -265,7 +265,7 @@ public function maybe_process_redirect_order() { * @since 8.3.0 */ private function maybe_process_legacy_redirect() { - if ( ! is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) { + if ( ! WC_Stripe_Page_Helper::is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) { return; } diff --git a/includes/class-wc-stripe-page-helper.php b/includes/class-wc-stripe-page-helper.php new file mode 100644 index 0000000000..deaa7bfff9 --- /dev/null +++ b/includes/class-wc-stripe-page-helper.php @@ -0,0 +1,175 @@ +get_order_key() ) { // phpcs:ignore WordPress.Security.NonceVerification + return false; + } + + // If the order doesn't need payment, we don't need to prepare the payment page. + if ( ! $order->needs_payment() ) { + return false; + } + + return current_user_can( 'pay_for_order', $order->get_id() ); + } + + /** + * Checks if the current page is the order received page and the current user is allowed to manage the order. + * + * @return bool + */ + public static function is_valid_order_received(): bool { + // If not on the order-received page, return false. + if ( ! static::is_order_received( true ) ) { + return false; + } + + // Verify nonce. Duplicated here in order to avoid PHPCS warnings. + if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wpnonce'] ) ), 'wc_stripe_process_redirect_order_nonce' ) ) { + return false; + } + + $order_id_from_order_key = absint( wc_get_order_id_by_order_key( wc_clean( wp_unslash( $_GET['key'] ) ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated + $order_id_from_query_var = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : null; + + // If the order ID is not found or the order ID does not match the given order ID, return false. + if ( ! $order_id_from_order_key || ( $order_id_from_query_var !== $order_id_from_order_key ) ) { + return false; + } + + $order = wc_get_order( $order_id_from_order_key ); + + // If the order doesn't need payment, return false. + if ( ! $order->needs_payment() ) { + return false; + } + + return current_user_can( 'pay_for_order', $order->get_id() ); + } + + /** + * Checks if this is the Pay for Order page. + * + * @param bool $check_key Optional. If true, check if the key is set in the URL. + * @return boolean + */ + public static function is_pay_for_order( $check_key = false ) { + $is_pay_for_order = is_checkout_pay_page() || is_wc_endpoint_url( 'order-pay' ) || isset( $_GET['pay_for_order'] ); // phpcs:ignore WordPress.Security.NonceVerification + return $is_pay_for_order && ( ! $check_key || isset( $_GET['key'] ) ); // phpcs:ignore WordPress.Security.NonceVerification + } + + /** + * Checks if this is the order received page. + * + * @param $check_key bool Optional. If true, check if the key is set in the URL. + * @return bool + */ + public static function is_order_received( $check_key = false ) { + $is_order_received = is_order_received_page() || is_wc_endpoint_url( 'order-received' ); // phpcs:ignore WordPress.Security.NonceVerification + return $is_order_received && ( ! $check_key || isset( $_GET['key'] ) ); // phpcs:ignore WordPress.Security.NonceVerification + } + + /** + * Checks if this page is a cart page. + * + * @return bool + */ + public static function is_cart() { + return is_cart() || has_block( 'woocommerce/cart' ); + } + + /** + * Checks if this page is a checkout page. + * + * @return bool + */ + public static function is_checkout() { + return is_checkout() || has_block( 'woocommerce/checkout' ); + } + + /** + * Checks if this page is a cart or checkout page. + * + * @return boolean + */ + public static function is_cart_or_checkout() { + return static::is_cart() || static::is_checkout(); + } + + /** + * Checks if this page is a checkout or pay for order page. + * + * @return bool + */ + public static function is_checkout_or_pay_for_order() { + return static::is_checkout() || static::is_pay_for_order(); + } + + /** + * Checks if this is a product page or content contains a product_page shortcode. + * + * @return boolean + */ + public static function is_product() { + return is_product() || wc_post_content_has_shortcode( 'product_page' ); + } + + /** + * Checks if this is the change payment method page. + * + * @return boolean + */ + public static function is_change_payment_method() { + return isset( $_GET['change_payment_method'] ); // phpcs:ignore WordPress.Security.NonceVerification + } + + /** + * Returns true when viewing payment methods page. + * + * @return bool + */ + public static function is_payment_methods() { + global $wp; + + $page_id = wc_get_page_id( 'myaccount' ); + + return ( $page_id && is_page( $page_id ) && ( isset( $wp->query_vars['payment-methods'] ) ) ); + } + + /** + * Checks if this is the Stripe settings page. + * + * @return bool + */ + public static function is_admin_settings() { + // phpcs:disable WordPress.Security.NonceVerification + return is_admin() + && isset( $_GET['page'], $_GET['tab'], $_GET['section'] ) + && 'wc-settings' === $_GET['page'] + && 'checkout' === $_GET['tab'] + && 0 === strpos( wc_clean( wp_unslash( $_GET['section'] ) ), 'stripe' ); + // phpcs:enable WordPress.Security.NonceVerification + } +} diff --git a/includes/compat/trait-wc-stripe-subscriptions-utilities.php b/includes/compat/trait-wc-stripe-subscriptions-utilities.php index 6a073ea3e5..e62c0f24e7 100644 --- a/includes/compat/trait-wc-stripe-subscriptions-utilities.php +++ b/includes/compat/trait-wc-stripe-subscriptions-utilities.php @@ -50,8 +50,8 @@ function_exists( 'wcs_order_contains_subscription' ) * @return bool */ public function is_changing_payment_method_for_subscription() { - if ( isset( $_GET['change_payment_method'] ) && function_exists( 'wcs_is_subscription' ) ) { // phpcs:ignore WordPress.Security.NonceVerification - return wcs_is_subscription( wc_clean( wp_unslash( $_GET['change_payment_method'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification + if ( WC_Stripe_Page_Helper::is_change_payment_method() && function_exists( 'wcs_is_subscription' ) ) { // phpcs:ignore WordPress.Security.NonceVerification + return wcs_is_subscription( wc_clean( wp_unslash( $_GET['change_payment_method'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotValidated } return false; } diff --git a/includes/payment-methods/class-wc-gateway-stripe-alipay.php b/includes/payment-methods/class-wc-gateway-stripe-alipay.php index d771d4dbd8..3329582387 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-alipay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-alipay.php @@ -166,7 +166,7 @@ public function get_icon() { * @version 4.0.0 */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -191,7 +191,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php index 6ce7d793ca..88c4a88d00 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-bancontact.php +++ b/includes/payment-methods/class-wc-gateway-stripe-bancontact.php @@ -153,7 +153,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -178,7 +178,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-boleto.php b/includes/payment-methods/class-wc-gateway-stripe-boleto.php index 1fc40f4bc4..ce9e3efc9f 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-boleto.php +++ b/includes/payment-methods/class-wc-gateway-stripe-boleto.php @@ -126,7 +126,7 @@ public function add_allowed_payment_processing_statuses( $allowed_statuses, $ord * @since 5.8.0 */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } diff --git a/includes/payment-methods/class-wc-gateway-stripe-eps.php b/includes/payment-methods/class-wc-gateway-stripe-eps.php index cf878df4c5..a6f69a7de5 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-eps.php +++ b/includes/payment-methods/class-wc-gateway-stripe-eps.php @@ -153,7 +153,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -178,7 +178,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-giropay.php b/includes/payment-methods/class-wc-gateway-stripe-giropay.php index f2753048e9..22fb6e9867 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-giropay.php +++ b/includes/payment-methods/class-wc-gateway-stripe-giropay.php @@ -149,7 +149,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -174,7 +174,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-ideal.php b/includes/payment-methods/class-wc-gateway-stripe-ideal.php index 82d79045cd..ecd3bf9847 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-ideal.php +++ b/includes/payment-methods/class-wc-gateway-stripe-ideal.php @@ -153,7 +153,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -178,7 +178,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php index a502483902..84a3edfe11 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-multibanco.php +++ b/includes/payment-methods/class-wc-gateway-stripe-multibanco.php @@ -157,7 +157,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -182,7 +182,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-p24.php b/includes/payment-methods/class-wc-gateway-stripe-p24.php index 8c3eab980a..fffb4e6aaa 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-p24.php +++ b/includes/payment-methods/class-wc-gateway-stripe-p24.php @@ -154,7 +154,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -179,7 +179,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-sepa.php b/includes/payment-methods/class-wc-gateway-stripe-sepa.php index 0b7cd93ae9..cb9c721f71 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sepa.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sepa.php @@ -166,7 +166,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -227,12 +227,12 @@ public function form() { public function payment_fields() { global $wp; $total = WC()->cart->total; - $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; + $display_tokenization = $this->supports( 'tokenization' ) && WC_Stripe_Page_Helper::is_checkout() && $this->saved_cards; $description = $this->get_description(); $description = ! empty( $description ) ? $description : ''; // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } @@ -261,7 +261,7 @@ public function payment_fields() { $this->form(); - if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) { + if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! WC_Stripe_Page_Helper::is_change_payment_method() ) { $this->save_payment_method_checkbox(); } diff --git a/includes/payment-methods/class-wc-gateway-stripe-sofort.php b/includes/payment-methods/class-wc-gateway-stripe-sofort.php index c916d5248a..397a8e72ea 100644 --- a/includes/payment-methods/class-wc-gateway-stripe-sofort.php +++ b/includes/payment-methods/class-wc-gateway-stripe-sofort.php @@ -153,7 +153,7 @@ public function get_icon() { * Outputs scripts used for stripe payment */ public function payment_scripts() { - if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) { + if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } @@ -178,7 +178,7 @@ public function payment_fields() { $description = $this->get_description(); // If paying from order, we need to get total from order not cart. - if ( parent::is_valid_pay_for_order_endpoint() ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) { $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) ); $total = $order->get_total(); } diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-element.php b/includes/payment-methods/class-wc-stripe-express-checkout-element.php index 84bdb13306..79917fe2c5 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-element.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-element.php @@ -84,7 +84,7 @@ public function init() { } // Don't load for change payment method page. - if ( isset( $_GET['change_payment_method'] ) ) { + if ( WC_Stripe_Page_Helper::is_change_payment_method() ) { return; } @@ -131,7 +131,7 @@ public function set_session() { // Don't set session cookies on product pages to allow for caching when payment request // buttons are disabled. But keep cookies if there is already an active WC session in place. if ( - ! ( $this->express_checkout_helper->is_product() && $this->express_checkout_helper->should_show_express_checkout_button() ) + ! ( WC_Stripe_Page_Helper::is_product() && $this->express_checkout_helper->should_show_express_checkout_button() ) || ( isset( WC()->session ) && WC()->session->has_session() ) ) { return; @@ -213,13 +213,13 @@ public function javascript_params() { ], 'checkout' => $this->express_checkout_helper->get_checkout_data(), 'button' => $this->express_checkout_helper->get_button_settings(), - 'is_pay_for_order' => $this->express_checkout_helper->is_pay_for_order_page(), + 'is_pay_for_order' => WC_Stripe_Page_Helper::is_pay_for_order(), 'has_block' => has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ), 'login_confirmation' => $this->express_checkout_helper->get_login_confirmation_settings(), - 'is_product_page' => $this->express_checkout_helper->is_product(), - 'is_checkout_page' => $this->express_checkout_helper->is_checkout(), + 'is_product_page' => WC_Stripe_Page_Helper::is_product(), + 'is_checkout_page' => WC_Stripe_Page_Helper::is_checkout(), 'product' => $this->express_checkout_helper->get_product_data(), - 'is_cart_page' => is_cart(), + 'is_cart_page' => WC_Stripe_Page_Helper::is_cart(), 'taxes_based_on_billing' => wc_tax_enabled() && get_option( 'woocommerce_tax_based_on' ) === 'billing', ]; } @@ -493,11 +493,11 @@ public function add_order_attribution_inputs() { * Display express checkout button separator. */ public function display_express_checkout_button_separator_html() { - if ( ! is_checkout() && ! is_wc_endpoint_url( 'order-pay' ) ) { + if ( ! WC_Stripe_Page_Helper::is_checkout_or_pay_for_order() ) { return; } - if ( is_checkout() && ! in_array( 'checkout', $this->express_checkout_helper->get_button_locations(), true ) ) { + if ( WC_Stripe_Page_Helper::is_checkout() && ! in_array( 'checkout', $this->express_checkout_helper->get_button_locations(), true ) ) { return; } diff --git a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php index 087d3125be..bcbd04cfea 100644 --- a/includes/payment-methods/class-wc-stripe-express-checkout-helper.php +++ b/includes/payment-methods/class-wc-stripe-express-checkout-helper.php @@ -214,7 +214,7 @@ public function get_product_price( $product, $is_deposit = null, $deposit_plan_i * @return mixed Returns false if not on a product page, the product information otherwise. */ public function get_product_data() { - if ( ! $this->is_product() ) { + if ( ! WC_Stripe_Page_Helper::is_product() ) { return false; } @@ -515,7 +515,7 @@ public function has_subscription_product() { return false; } - if ( $this->is_product() ) { + if ( WC_Stripe_Page_Helper::is_product() ) { $product = $this->get_product(); if ( ! $product ) { return false; @@ -523,7 +523,7 @@ public function has_subscription_product() { if ( WC_Subscriptions_Product::is_subscription( $product ) ) { return true; } - } elseif ( WC_Stripe_Helper::has_cart_or_checkout_on_current_page() ) { + } elseif ( WC_Stripe_Page_Helper::is_cart_or_checkout() ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); if ( WC_Subscriptions_Product::is_subscription( $_product ) ) { @@ -539,9 +539,12 @@ public function has_subscription_product() { * Checks if this is a product page or content contains a product_page shortcode. * * @return boolean + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_product() instead. */ public function is_product() { - return is_product() || wc_post_content_has_shortcode( 'product_page' ); + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_product()' ); + return WC_Stripe_Page_Helper::is_product(); } /** @@ -552,9 +555,7 @@ public function is_product() { public function get_product() { global $post; - if ( is_product() ) { - return wc_get_product( $post->ID ); - } elseif ( wc_post_content_has_shortcode( 'product_page' ) ) { + if ( WC_Stripe_Page_Helper::is_product() ) { // Get id from product_page shortcode. preg_match( '/\[product_page id="(?\d+)"\]/', $post->post_content, $shortcode_match ); @@ -574,9 +575,9 @@ public function get_product() { * @return boolean True if the current page is supported, false otherwise. */ public function is_page_supported() { - return $this->is_product() - || WC_Stripe_Helper::has_cart_or_checkout_on_current_page() - || is_wc_endpoint_url( 'order-pay' ); + return WC_Stripe_Page_Helper::is_product() + || WC_Stripe_Page_Helper::is_cart_or_checkout() + || WC_Stripe_Page_Helper::is_pay_for_order(); } /** @@ -616,18 +617,18 @@ public function should_show_express_checkout_button() { } // Don't show on cart if disabled. - if ( is_cart() && ! $this->should_show_ece_on_cart_page() ) { + if ( WC_Stripe_Page_Helper::is_cart() && ! $this->should_show_ece_on_cart_page() ) { WC_Stripe_Logger::log( 'Stripe Express Checkout buttons display on cart is disabled. ' ); return false; } // Don't show on checkout if disabled. - if ( is_checkout() && ! $this->should_show_ece_on_checkout_page() ) { + if ( WC_Stripe_Page_Helper::is_checkout() && ! $this->should_show_ece_on_checkout_page() ) { WC_Stripe_Logger::log( 'Stripe Express Checkout buttons display on checkout is disabled. ' ); return false; } - $is_product = $this->is_product(); + $is_product = WC_Stripe_Page_Helper::is_product(); // Don't show if product page ECE is disabled. if ( $is_product && ! $this->should_show_ece_on_product_pages() ) { @@ -698,7 +699,7 @@ public function should_show_express_checkout_button() { private function should_hide_ece_based_on_tax_setup() { // We do not need to hide if on the Pay for Order page, as we expect the taxes to // be manually inputted when the merchant creates the order. - if ( $this->is_pay_for_order_page() ) { + if ( WC_Stripe_Page_Helper::is_pay_for_order() ) { return false; } @@ -720,7 +721,7 @@ private function should_hide_ece_based_on_tax_setup() { */ private function is_product_or_cart_taxable() { // Product page: check the product's tax status. - if ( is_product() ) { + if ( WC_Stripe_Page_Helper::is_product() ) { $product = $this->get_product(); if ( ! $product ) { return false; @@ -758,7 +759,7 @@ private function is_product_or_cart_taxable() { */ private function product_or_cart_needs_shipping() { // Product page. - if ( is_product() ) { + if ( WC_Stripe_Page_Helper::is_product() ) { $product = $this->get_product(); if ( ! $product ) { return false; @@ -1287,18 +1288,24 @@ public function get_button_settings() { * Checks if this is the Pay for Order page. * * @return boolean + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helpers::is_pay_for_order_page() instead. */ public function is_pay_for_order_page() { - return is_checkout() && isset( $_GET['pay_for_order'] ); // phpcs:ignore WordPress.Security.NonceVerification + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helpers::is_pay_for_order_page()' ); + return WC_Stripe_Page_Helper::is_pay_for_order(); } /** * Checks if this is the checkout page or content contains a checkout block. * * @return boolean + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helpers::is_checkout() instead. */ public function is_checkout() { - return is_checkout() || has_block( 'woocommerce/checkout' ); + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helpers::is_checkout()' ); + return WC_Stripe_Page_Helper::is_checkout(); } /** diff --git a/includes/payment-methods/class-wc-stripe-payment-request.php b/includes/payment-methods/class-wc-stripe-payment-request.php index 21fd8d8540..715d05dc52 100644 --- a/includes/payment-methods/class-wc-stripe-payment-request.php +++ b/includes/payment-methods/class-wc-stripe-payment-request.php @@ -100,7 +100,7 @@ public function __construct() { } // Don't load for change payment method page. - if ( isset( $_GET['change_payment_method'] ) ) { + if ( WC_Stripe_Page_Helper::is_change_payment_method() ) { return; } @@ -190,7 +190,7 @@ public function set_session() { // Don't set session cookies on product pages to allow for caching when payment request // buttons are disabled. But keep cookies if there is already an active WC session in place. if ( - ! ( $this->is_product() && $this->should_show_payment_request_button() ) + ! ( WC_Stripe_Page_Helper::is_product() && $this->should_show_payment_request_button() ) || ( isset( WC()->session ) && WC()->session->has_session() ) ) { return; @@ -391,7 +391,7 @@ public function get_product_price( $product ) { * @return mixed Returns false if not on a product page, the product information otherwise. */ public function get_product_data() { - if ( ! $this->is_product() ) { + if ( ! WC_Stripe_Page_Helper::is_product() ) { return false; } @@ -724,12 +724,12 @@ public function has_subscription_product() { return false; } - if ( $this->is_product() ) { + if ( WC_Stripe_Page_Helper::is_product() ) { $product = $this->get_product(); if ( WC_Subscriptions_Product::is_subscription( $product ) ) { return true; } - } elseif ( WC_Stripe_Helper::has_cart_or_checkout_on_current_page() ) { + } elseif ( WC_Stripe_Page_Helper::is_cart_or_checkout() ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); if ( WC_Subscriptions_Product::is_subscription( $_product ) ) { @@ -746,9 +746,12 @@ public function has_subscription_product() { * * @since 5.2.0 * @return boolean + * + * @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_product() instead. */ public function is_product() { - return is_product() || wc_post_content_has_shortcode( 'product_page' ); + _deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_product()' ); + return WC_Stripe_Page_Helper::is_product(); } /** @@ -760,7 +763,7 @@ public function is_product() { public function get_product() { global $post; - if ( is_product() ) { + if ( WC_Stripe_Page_Helper::is_product() ) { return wc_get_product( $post->ID ); } elseif ( wc_post_content_has_shortcode( 'product_page' ) ) { // Get id from product_page shortcode. @@ -840,7 +843,7 @@ public function javascript_params() { ], 'button' => $this->get_button_settings(), 'login_confirmation' => $this->get_login_confirmation_settings(), - 'is_product_page' => $this->is_product(), + 'is_product_page' => WC_Stripe_Page_Helper::is_product(), 'product' => $this->get_product_data(), ]; } @@ -889,9 +892,9 @@ public function scripts() { * @return boolean True if the current page is supported, false otherwise. */ private function is_page_supported() { - return $this->is_product() - || WC_Stripe_Helper::has_cart_or_checkout_on_current_page() - || is_wc_endpoint_url( 'order-pay' ); + return WC_Stripe_Page_Helper::is_product() + || WC_Stripe_Page_Helper::is_cart_or_checkout() + || WC_Stripe_Page_Helper::is_pay_for_order(); } /** @@ -943,11 +946,11 @@ public function display_payment_request_button_separator_html() { return; } - if ( ! is_checkout() && ! is_wc_endpoint_url( 'order-pay' ) ) { + if ( ! WC_Stripe_Page_Helper::is_checkout_or_pay_for_order() ) { return; } - if ( is_checkout() && ! in_array( 'checkout', $this->get_button_locations(), true ) ) { + if ( WC_Stripe_Page_Helper::is_checkout() && ! in_array( 'checkout', $this->get_button_locations(), true ) ) { return; } ?> @@ -1008,26 +1011,26 @@ public function should_show_payment_request_button() { } // Don't show on cart if disabled. - if ( is_cart() && ! $this->should_show_prb_on_cart_page() ) { + if ( WC_Stripe_Page_Helper::is_cart() && ! $this->should_show_prb_on_cart_page() ) { return false; } // Don't show on checkout if disabled. - if ( is_checkout() && ! $this->should_show_prb_on_checkout_page() ) { + if ( WC_Stripe_Page_Helper::is_checkout() && ! $this->should_show_prb_on_checkout_page() ) { return false; } // Don't show if product page PRB is disabled. - if ( $this->is_product() && ! $this->should_show_prb_on_product_pages() ) { + if ( WC_Stripe_Page_Helper::is_product() && ! $this->should_show_prb_on_product_pages() ) { return false; } // Don't show if product on current page is not supported. - if ( $this->is_product() && ! $this->is_product_supported( $this->get_product() ) ) { + if ( WC_Stripe_Page_Helper::is_product() && ! $this->is_product_supported( $this->get_product() ) ) { return false; } - if ( $this->is_product() && in_array( $this->get_product()->get_type(), [ 'variable', 'variable-subscription' ], true ) ) { + if ( WC_Stripe_Page_Helper::is_product() && in_array( $this->get_product()->get_type(), [ 'variable', 'variable-subscription' ], true ) ) { $stock_availability = array_column( $this->get_product()->get_available_variations(), 'is_in_stock' ); // Don't show if all product variations are out-of-stock. if ( ! in_array( true, $stock_availability, true ) ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php index 68e30e2ce8..c26fe99d53 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php @@ -249,7 +249,7 @@ public function __construct() { $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : ''; // When feature flags are enabled, title shows the count of enabled payment methods in settings page only. - if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && WC_Stripe_Feature_Flags::is_upe_preview_enabled() && isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] ) { + if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && WC_Stripe_Feature_Flags::is_upe_preview_enabled() && WC_Stripe_Page_Helper::is_admin_settings() ) { $enabled_payment_methods_count = count( $enabled_payment_methods ); $this->title = $enabled_payment_methods_count ? /* translators: $1. Count of enabled payment methods. */ @@ -397,18 +397,18 @@ public function init_form_fields() { */ public function payment_scripts() { if ( - ! is_product() - && ! WC_Stripe_Helper::has_cart_or_checkout_on_current_page() - && ! parent::is_valid_pay_for_order_endpoint() + ! WC_Stripe_Page_Helper::is_product() + && ! WC_Stripe_Page_Helper::is_cart_or_checkout() + && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) { return; } - if ( is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) { + if ( WC_Stripe_Page_Helper::is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) { return; } - if ( is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) { + if ( WC_Stripe_Page_Helper::is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) { return; } @@ -497,7 +497,7 @@ public function javascript_params() { $express_checkout_helper = new WC_Stripe_Express_Checkout_Helper(); - $stripe_params['isCheckout'] = ( is_checkout() || has_block( 'woocommerce/checkout' ) ) && empty( $_GET['pay_for_order'] ); // wpcs: csrf ok. + $stripe_params['isCheckout'] = WC_Stripe_Page_Helper::is_checkout() && empty( $_GET['pay_for_order'] ); // wpcs: csrf ok. $stripe_params['return_url'] = $this->get_stripe_return_url(); $stripe_params['ajax_url'] = WC_AJAX::get_endpoint( '%%endpoint%%' ); $stripe_params['theme_name'] = get_option( 'stylesheet' ); @@ -553,7 +553,7 @@ public function javascript_params() { $stripe_params['cartTotal'] = WC_Stripe_Helper::get_stripe_amount( $cart_total, strtolower( $currency ) ); $stripe_params['currency'] = $currency; - if ( parent::is_valid_pay_for_order_endpoint() || $is_change_payment_method ) { + if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() || $is_change_payment_method ) { $order_id = absint( get_query_var( 'order-pay' ) ); $order = wc_get_order( $order_id ); @@ -772,7 +772,7 @@ private function get_stripe_supported_payment_methods() { */ public function payment_fields() { try { - $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; + $display_tokenization = $this->supports( 'tokenization' ) && WC_Stripe_Page_Helper::is_checkout() && $this->saved_cards; // Output the form HTML. ?> @@ -1446,7 +1446,7 @@ public function process_payment_with_saved_payment_method( $order_id, $can_retry * @version 5.6.0 */ public function maybe_process_upe_redirect() { - if ( $this->is_payment_methods_page() || $this->is_changing_payment_method_for_subscription() ) { + if ( WC_Stripe_Page_Helper::is_payment_methods() || $this->is_changing_payment_method_for_subscription() ) { if ( $this->is_setup_intent_success_creation_redirection() ) { if ( isset( $_GET['redirect_status'] ) && 'succeeded' === $_GET['redirect_status'] ) { $user_id = wp_get_current_user()->ID; @@ -1486,7 +1486,7 @@ public function maybe_process_upe_redirect() { return; } - if ( ! parent::is_valid_order_received_endpoint() ) { + if ( ! WC_Stripe_Page_Helper::is_valid_order_received() ) { return; } @@ -1795,7 +1795,7 @@ public function retry_after_error( $response, $order, $retry, $force_save_source * @return bool Whether a payment is necessary. */ public function is_payment_needed( $order_id = null ) { - $is_pay_for_order_page = parent::is_valid_pay_for_order_endpoint(); + $is_pay_for_order_page = WC_Stripe_Page_Helper::is_valid_pay_for_order(); // Check if the cart contains a pre-order product. Ignore the cart if we're on the Pay for Order page. if ( $this->is_pre_order_item_in_cart() && ! $is_pay_for_order_page ) { @@ -2128,19 +2128,6 @@ public function get_metadata_from_order( $order ) { return apply_filters( 'wc_stripe_intent_metadata', $metadata, $order ); } - /** - * Returns true when viewing payment methods page. - * - * @return bool - */ - private function is_payment_methods_page() { - global $wp; - - $page_id = wc_get_page_id( 'myaccount' ); - - return ( $page_id && is_page( $page_id ) && ( isset( $wp->query_vars['payment-methods'] ) ) ); - } - /** * True if the request contains the values that indicates a redirection after a successful setup intent creation. * @@ -2958,7 +2945,7 @@ private function get_return_url_for_redirect( $order, $save_payment_method ) { 'wc_payment_method' => self::ID, '_wpnonce' => wp_create_nonce( 'wc_stripe_process_redirect_order_nonce' ), 'save_payment_method' => $save_payment_method ? 'yes' : 'no', - 'pay_for_order' => parent::is_valid_pay_for_order_endpoint() ? 'yes' : 'no', + 'pay_for_order' => WC_Stripe_Page_Helper::is_valid_pay_for_order() ? 'yes' : 'no', ], $this->get_return_url( $order ) ) @@ -3258,7 +3245,7 @@ public function filter_my_account_my_orders_actions( $actions, $order ) { $methods_with_delayed_confirmation = [ WC_Stripe_Payment_Methods::BACS_DEBIT_LABEL, ]; - if ( is_order_received_page() && in_array( $order->get_payment_method_title(), $methods_with_delayed_confirmation, true ) && $order->has_status( OrderStatus::PENDING ) ) { + if ( WC_Stripe_Page_Helper::is_order_received() && in_array( $order->get_payment_method_title(), $methods_with_delayed_confirmation, true ) && $order->has_status( OrderStatus::PENDING ) ) { unset( $actions['pay'], $actions['cancel'] ); } return $actions; diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method-bacs-debit.php b/includes/payment-methods/class-wc-stripe-upe-payment-method-bacs-debit.php index a23d741f26..32d2562a9c 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method-bacs-debit.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method-bacs-debit.php @@ -134,7 +134,7 @@ public function should_hide_bacs_on_add_payment_method_page() { * @return bool True if Bacs should be hidden, false otherwise. */ public function should_hide_bacs_for_pre_orders_charge_upon_release() { - if ( is_checkout() && class_exists( 'WC_Pre_Orders_Cart' ) && WC_Pre_Orders_Cart::cart_contains_pre_order() ) { + if ( WC_Stripe_Page_Helper::is_checkout() && class_exists( 'WC_Pre_Orders_Cart' ) && WC_Pre_Orders_Cart::cart_contains_pre_order() ) { $cart = WC()->cart->get_cart(); // Iteration is unnecessary since only one pre-order product can be in the cart. $product_id = reset( $cart )['product_id']; @@ -155,7 +155,7 @@ public function should_hide_bacs_for_pre_orders_charge_upon_release() { */ public function should_hide_bacs_for_subscriptions_with_free_trials() { $is_update_order_review_ajax_request = defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['wc-ajax'] ) && 'update_order_review' === $_REQUEST['wc-ajax']; - if ( is_checkout() || $is_update_order_review_ajax_request ) { + if ( WC_Stripe_Page_Helper::is_checkout() || $is_update_order_review_ajax_request ) { // Checking if the amount is zero allows us to process orders that include subscriptions with a free trial, // as long as another product increases the total amount, ensuring compatibility with Bacs. if ( class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_free_trial() && (float) WC()->cart->total === 0.00 ) { diff --git a/includes/payment-methods/class-wc-stripe-upe-payment-method.php b/includes/payment-methods/class-wc-stripe-upe-payment-method.php index 55bf514e96..6efb278ab8 100644 --- a/includes/payment-methods/class-wc-stripe-upe-payment-method.php +++ b/includes/payment-methods/class-wc-stripe-upe-payment-method.php @@ -286,7 +286,7 @@ public function is_enabled_at_checkout( $order_id = null, $account_domestic_curr $current_store_currency = $this->get_woocommerce_currency(); $currencies = $this->get_supported_currencies(); if ( ! empty( $currencies ) ) { - if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['key'] ) ) { + if ( WC_Stripe_Page_Helper::is_pay_for_order( true ) ) { $order = wc_get_order( $order_id ? $order_id : absint( get_query_var( 'order-pay' ) ) ); $order_currency = $order->get_currency(); if ( ! in_array( $order_currency, $currencies, true ) ) { @@ -621,7 +621,7 @@ public function get_option( $key, $empty_value = null ) { */ public function payment_fields() { try { - $display_tokenization = $this->is_reusable() && is_checkout(); + $display_tokenization = $this->is_reusable() && WC_Stripe_Page_Helper::is_checkout(); if ( $this->testmode && ! empty( $this->get_testing_instructions() ) ) : ?>

get_testing_instructions() ); ?>

@@ -703,7 +703,7 @@ public function get_limits_per_currency(): array { * @return float|int|string */ public function get_current_order_amount() { - if ( is_wc_endpoint_url( 'order-pay' ) && isset( $_GET['key'] ) ) { + if ( WC_Stripe_Page_Helper::is_pay_for_order( true ) ) { $order = wc_get_order( absint( get_query_var( 'order-pay' ) ) ); return $order->get_total( '' ); } elseif ( WC()->cart ) { diff --git a/woocommerce-gateway-stripe.php b/woocommerce-gateway-stripe.php index f8a484c094..3bddb855da 100644 --- a/woocommerce-gateway-stripe.php +++ b/woocommerce-gateway-stripe.php @@ -203,6 +203,7 @@ public function init() { require_once __DIR__ . '/includes/class-wc-stripe-payment-method-configurations.php'; include_once __DIR__ . '/includes/class-wc-stripe-api.php'; include_once __DIR__ . '/includes/class-wc-stripe-mode.php'; + include_once __DIR__ . '/includes/class-wc-stripe-page-helper.php'; require_once __DIR__ . '/includes/compat/class-wc-stripe-subscriptions-helper.php'; require_once __DIR__ . '/includes/compat/trait-wc-stripe-subscriptions-utilities.php'; require_once __DIR__ . '/includes/compat/trait-wc-stripe-subscriptions.php';