Skip to content

Commit

Permalink
use native wp_doing_ajax instead of custom is_ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Nov 9, 2021
1 parent d64c449 commit 4f39c25
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/admin/class-wc-admin.php
Expand Up @@ -157,7 +157,7 @@ public function admin_redirects() {
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' );

Expand Down
2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/class-wc-cache-helper.php
Expand Up @@ -186,7 +186,7 @@ public static function prevent_caching() {
* 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 ) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/woocommerce/includes/class-wc-checkout.php
Expand Up @@ -985,7 +985,7 @@ protected function process_order_payment( $order_id, $payment_method ) {

$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;
Expand All @@ -1006,7 +1006,7 @@ protected function process_order_without_payment( $order_id ) {
$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 )
);
Expand Down Expand Up @@ -1106,7 +1106,7 @@ protected function process_customer( $data ) {
* 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 );
Expand Down
2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/class-wc-https.php
Expand Up @@ -106,7 +106,7 @@ public static function unforce_https_template_redirect() {
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'] ) );
Expand Down
2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/class-wc-order-item-meta.php
Expand Up @@ -168,7 +168,7 @@ public function get_formatted( $hideprefix = '_' ) {
* @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' );
}

Expand Down
12 changes: 0 additions & 12 deletions plugins/woocommerce/includes/wc-conditional-functions.php
Expand Up @@ -256,18 +256,6 @@ function 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' ) ) {

/**
Expand Down
22 changes: 18 additions & 4 deletions plugins/woocommerce/includes/wc-deprecated-functions.php
Expand Up @@ -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}." : '';
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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' );
}
}
2 changes: 1 addition & 1 deletion plugins/woocommerce/includes/wc-term-functions.php
Expand Up @@ -520,7 +520,7 @@ function wc_recount_after_stock_change( $product_id ) {
* @return array
*/
function wc_change_term_counts( $terms, $taxonomies ) {
if ( is_admin() || is_ajax() ) {
if ( is_admin() || wp_doing_ajax() ) {
return $terms;
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/woocommerce/templates/checkout/payment.php
Expand Up @@ -17,7 +17,7 @@

defined( 'ABSPATH' ) || exit;

if ( ! is_ajax() ) {
if ( ! wp_doing_ajax() ) {
do_action( 'woocommerce_review_order_before_payment' );
}
?>
Expand Down Expand Up @@ -56,6 +56,6 @@
</div>
</div>
<?php
if ( ! is_ajax() ) {
if ( ! wp_doing_ajax() ) {
do_action( 'woocommerce_review_order_after_payment' );
}

0 comments on commit 4f39c25

Please sign in to comment.