WC_Cache_Helper::geolocation_ajax_redirect()public staticWC 1.0

When using geolocation via ajax, to bust cache, redirect if the location hash does not equal the querystring.

This prevents caching of the wrong data for this request.

Method of the class: WC_Cache_Helper{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = WC_Cache_Helper::geolocation_ajax_redirect();

WC_Cache_Helper::geolocation_ajax_redirect() code WC 8.7.0

public static function geolocation_ajax_redirect() {
	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 ) {
			global $wp;

			$redirect_url = trailingslashit( home_url( $wp->request ) );

			if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { // WPCS: Input var ok.
				$redirect_url = add_query_arg( wp_unslash( $_SERVER['QUERY_STRING'] ), '', $redirect_url ); // WPCS: sanitization ok, Input var ok.
			}

			if ( ! get_option( 'permalink_structure' ) ) {
				$redirect_url = add_query_arg( $wp->query_string, '', $redirect_url );
			}

			$redirect_url = add_query_arg( 'v', $location_hash, remove_query_arg( 'v', $redirect_url ) );

			wp_safe_redirect( esc_url_raw( $redirect_url ), 307 );
			exit;
		}
	}
}