From 4f39c2599108ce75318f4093ea7a5010eca3bd3d Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Tue, 9 Nov 2021 17:55:02 +0100 Subject: [PATCH 1/2] use native wp_doing_ajax instead of custom is_ajax --- .../includes/admin/class-wc-admin.php | 2 +- .../includes/class-wc-cache-helper.php | 2 +- .../includes/class-wc-checkout.php | 6 ++--- .../woocommerce/includes/class-wc-https.php | 2 +- .../includes/class-wc-order-item-meta.php | 2 +- .../includes/wc-conditional-functions.php | 12 ---------- .../includes/wc-deprecated-functions.php | 22 +++++++++++++++---- .../includes/wc-term-functions.php | 2 +- .../templates/checkout/payment.php | 4 ++-- 9 files changed, 28 insertions(+), 26 deletions(-) diff --git a/plugins/woocommerce/includes/admin/class-wc-admin.php b/plugins/woocommerce/includes/admin/class-wc-admin.php index 215f061789b..3785f3c43f8 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin.php @@ -157,7 +157,7 @@ class WC_Admin { public function prevent_admin_access() { $prevent_access = false; - if ( apply_filters( 'woocommerce_disable_admin_bar', true ) && ! is_ajax() && isset( $_SERVER['SCRIPT_FILENAME'] ) && basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) !== 'admin-post.php' ) { + if ( apply_filters( 'woocommerce_disable_admin_bar', true ) && ! wp_doing_ajax() && isset( $_SERVER['SCRIPT_FILENAME'] ) && basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) !== 'admin-post.php' ) { $has_cap = false; $access_caps = array( 'edit_posts', 'manage_woocommerce', 'view_admin_dashboard' ); diff --git a/plugins/woocommerce/includes/class-wc-cache-helper.php b/plugins/woocommerce/includes/class-wc-cache-helper.php index 4a9c0604eab..32500527c5e 100644 --- a/plugins/woocommerce/includes/class-wc-cache-helper.php +++ b/plugins/woocommerce/includes/class-wc-cache-helper.php @@ -186,7 +186,7 @@ class WC_Cache_Helper { * This prevents caching of the wrong data for this request. */ public static function geolocation_ajax_redirect() { - if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! is_ajax() && empty( $_POST ) ) { // WPCS: CSRF ok, input var ok. + if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! wp_doing_ajax() && empty( $_POST ) ) { // WPCS: CSRF ok, input var ok. $location_hash = self::geolocation_ajax_get_location_hash(); $current_hash = isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. if ( empty( $current_hash ) || $current_hash !== $location_hash ) { diff --git a/plugins/woocommerce/includes/class-wc-checkout.php b/plugins/woocommerce/includes/class-wc-checkout.php index 3133be81505..2c28a6e4af2 100644 --- a/plugins/woocommerce/includes/class-wc-checkout.php +++ b/plugins/woocommerce/includes/class-wc-checkout.php @@ -985,7 +985,7 @@ class WC_Checkout { $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); - if ( ! is_ajax() ) { + if ( ! wp_doing_ajax() ) { // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect wp_redirect( $result['redirect'] ); exit; @@ -1006,7 +1006,7 @@ class WC_Checkout { $order->payment_complete(); wc_empty_cart(); - if ( ! is_ajax() ) { + if ( ! wp_doing_ajax() ) { wp_safe_redirect( apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $order->get_checkout_order_received_url(), $order ) ); @@ -1106,7 +1106,7 @@ class WC_Checkout { * If checkout failed during an AJAX call, send failure response. */ protected function send_ajax_failure_response() { - if ( is_ajax() ) { + if ( wp_doing_ajax() ) { // Only print notices if not reloading the checkout, otherwise they're lost in the page reload. if ( ! isset( WC()->session->reload_checkout ) ) { $messages = wc_print_notices( true ); diff --git a/plugins/woocommerce/includes/class-wc-https.php b/plugins/woocommerce/includes/class-wc-https.php index 84e3aeddd7b..045d594dc2b 100644 --- a/plugins/woocommerce/includes/class-wc-https.php +++ b/plugins/woocommerce/includes/class-wc-https.php @@ -106,7 +106,7 @@ class WC_HTTPS { return; } - if ( ! wc_site_is_https() && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! is_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) { + if ( ! wc_site_is_https() && is_ssl() && $_SERVER['REQUEST_URI'] && ! is_checkout() && ! wp_doing_ajax() && ! is_account_page() && apply_filters( 'woocommerce_unforce_ssl_checkout', true ) ) { if ( 0 === strpos( $_SERVER['REQUEST_URI'], 'http' ) ) { wp_safe_redirect( preg_replace( '|^https://|', 'http://', $_SERVER['REQUEST_URI'] ) ); diff --git a/plugins/woocommerce/includes/class-wc-order-item-meta.php b/plugins/woocommerce/includes/class-wc-order-item-meta.php index 693be232328..badc1ba7d01 100644 --- a/plugins/woocommerce/includes/class-wc-order-item-meta.php +++ b/plugins/woocommerce/includes/class-wc-order-item-meta.php @@ -168,7 +168,7 @@ class WC_Order_Item_Meta { * @return array */ public function get_formatted_legacy( $hideprefix = '_' ) { - if ( ! is_ajax() ) { + if ( ! wp_doing_ajax() ) { wc_deprecated_argument( 'WC_Order_Item_Meta::get_formatted', '2.4', 'Item Meta Data is being called with legacy arguments' ); } diff --git a/plugins/woocommerce/includes/wc-conditional-functions.php b/plugins/woocommerce/includes/wc-conditional-functions.php index 641066cb657..13c8eca298b 100644 --- a/plugins/woocommerce/includes/wc-conditional-functions.php +++ b/plugins/woocommerce/includes/wc-conditional-functions.php @@ -256,18 +256,6 @@ if ( ! function_exists( 'is_lost_password_page' ) ) { } } -if ( ! function_exists( 'is_ajax' ) ) { - - /** - * Is_ajax - Returns true when the page is loaded via ajax. - * - * @return bool - */ - function is_ajax() { - return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : Constants::is_defined( 'DOING_AJAX' ); - } -} - if ( ! function_exists( 'is_store_notice_showing' ) ) { /** diff --git a/plugins/woocommerce/includes/wc-deprecated-functions.php b/plugins/woocommerce/includes/wc-deprecated-functions.php index ea5f57f7191..9810d6a6c7e 100644 --- a/plugins/woocommerce/includes/wc-deprecated-functions.php +++ b/plugins/woocommerce/includes/wc-deprecated-functions.php @@ -45,7 +45,7 @@ function wc_do_deprecated_action( $tag, $args, $version, $replacement = null, $m */ function wc_deprecated_function( $function, $version, $replacement = null ) { // @codingStandardsIgnoreStart - if ( is_ajax() || WC()->is_rest_api_request() ) { + if ( wp_doing_ajax() || WC()->is_rest_api_request() ) { do_action( 'deprecated_function_run', $function, $replacement, $version ); $log_string = "The {$function} function is deprecated since version {$version}."; $log_string .= $replacement ? " Replace with {$replacement}." : ''; @@ -67,7 +67,7 @@ function wc_deprecated_function( $function, $version, $replacement = null ) { */ function wc_deprecated_hook( $hook, $version, $replacement = null, $message = null ) { // @codingStandardsIgnoreStart - if ( is_ajax() || WC()->is_rest_api_request() ) { + if ( wp_doing_ajax() || WC()->is_rest_api_request() ) { do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message ); $message = empty( $message ) ? '' : ' ' . $message; @@ -111,7 +111,7 @@ function wc_doing_it_wrong( $function, $message, $version ) { // @codingStandardsIgnoreStart $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); - if ( is_ajax() || WC()->is_rest_api_request() ) { + if ( wp_doing_ajax() || WC()->is_rest_api_request() ) { do_action( 'doing_it_wrong_run', $function, $message, $version ); error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); } else { @@ -129,7 +129,7 @@ function wc_doing_it_wrong( $function, $message, $version ) { * @param string $replacement */ function wc_deprecated_argument( $argument, $version, $message = null ) { - if ( is_ajax() || WC()->is_rest_api_request() ) { + if ( wp_doing_ajax() || WC()->is_rest_api_request() ) { do_action( 'deprecated_argument_run', $argument, $message, $version ); error_log( "The {$argument} argument is deprecated since version {$version}. {$message}" ); } else { @@ -1123,3 +1123,17 @@ function get_woocommerce_term_meta( $term_id, $key, $single = true ) { wc_deprecated_function( 'get_woocommerce_term_meta', '3.6', 'get_term_meta' ); return function_exists( 'get_term_meta' ) ? get_term_meta( $term_id, $key, $single ) : get_metadata( 'woocommerce_term', $term_id, $key, $single ); } + +if ( ! function_exists( 'is_ajax' ) ) { + + /** + * Is_ajax - Returns true when the page is loaded via ajax. + * + * @deprecated 6.0.0 + * @return bool + */ + function is_ajax() { + wc_deprecated_function( 'is_ajax', '6.0', 'wp_doing_ajax' ); + return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : Constants::is_defined( 'DOING_AJAX' ); + } +} diff --git a/plugins/woocommerce/includes/wc-term-functions.php b/plugins/woocommerce/includes/wc-term-functions.php index 163a1e476c8..0a2fd5cef67 100644 --- a/plugins/woocommerce/includes/wc-term-functions.php +++ b/plugins/woocommerce/includes/wc-term-functions.php @@ -520,7 +520,7 @@ add_action( 'woocommerce_product_set_stock_status', 'wc_recount_after_stock_chan * @return array */ function wc_change_term_counts( $terms, $taxonomies ) { - if ( is_admin() || is_ajax() ) { + if ( is_admin() || wp_doing_ajax() ) { return $terms; } diff --git a/plugins/woocommerce/templates/checkout/payment.php b/plugins/woocommerce/templates/checkout/payment.php index a5467a71c72..7da3423e324 100644 --- a/plugins/woocommerce/templates/checkout/payment.php +++ b/plugins/woocommerce/templates/checkout/payment.php @@ -17,7 +17,7 @@ defined( 'ABSPATH' ) || exit; -if ( ! is_ajax() ) { +if ( ! wp_doing_ajax() ) { do_action( 'woocommerce_review_order_before_payment' ); } ?> @@ -56,6 +56,6 @@ if ( ! is_ajax() ) { Date: Tue, 7 Dec 2021 08:59:17 -0800 Subject: [PATCH 2/2] Deprecation will be as of 6.1.0. --- plugins/woocommerce/includes/wc-deprecated-functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/includes/wc-deprecated-functions.php b/plugins/woocommerce/includes/wc-deprecated-functions.php index 9810d6a6c7e..fe195c292a5 100644 --- a/plugins/woocommerce/includes/wc-deprecated-functions.php +++ b/plugins/woocommerce/includes/wc-deprecated-functions.php @@ -1129,11 +1129,11 @@ if ( ! function_exists( 'is_ajax' ) ) { /** * Is_ajax - Returns true when the page is loaded via ajax. * - * @deprecated 6.0.0 + * @deprecated 6.1.0 * @return bool */ function is_ajax() { - wc_deprecated_function( 'is_ajax', '6.0', 'wp_doing_ajax' ); + wc_deprecated_function( 'is_ajax', '6.1.0', 'wp_doing_ajax' ); return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : Constants::is_defined( 'DOING_AJAX' ); } }