From d1f363c3565be3a5676e9ee53fc4dd9ba46c42c5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 1 Apr 2016 11:14:24 +0100 Subject: [PATCH] Exclude geolocation of bots by user agent Closes #10626 --- includes/class-wc-frontend-scripts.php | 7 ++++++- includes/wc-core-functions.php | 15 +++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/includes/class-wc-frontend-scripts.php b/includes/class-wc-frontend-scripts.php index 82a23703a47..a220175e793 100644 --- a/includes/class-wc-frontend-scripts.php +++ b/includes/class-wc-frontend-scripts.php @@ -204,7 +204,12 @@ class WC_Frontend_Scripts { self::enqueue_script( 'wc-single-product' ); } if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) { - self::enqueue_script( 'wc-geolocation', $frontend_script_path . 'geolocation' . $suffix . '.js', array( 'jquery' ) ); + // Exclude common bots from geolocation by user agent. + $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : ''; + + if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) { + self::enqueue_script( 'wc-geolocation', $frontend_script_path . 'geolocation' . $suffix . '.js', array( 'jquery' ) ); + } } // Global frontend scripts diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index a7ee8644e4f..f51bb6f1d6f 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -718,14 +718,16 @@ function wc_get_base_location() { * @return array */ function wc_get_customer_default_location() { + $location = array(); + switch ( get_option( 'woocommerce_default_customer_address' ) ) { case 'geolocation_ajax' : case 'geolocation' : - $location = WC_Geolocation::geolocate_ip( '', true, false ); + // Exclude common bots from geolocation by user agent. + $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : ''; - // Base fallback. - if ( empty( $location['country'] ) ) { - $location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) ); + if ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) { + $location = WC_Geolocation::geolocate_ip( '', true, false ); } break; case 'base' : @@ -736,6 +738,11 @@ function wc_get_customer_default_location() { break; } + // Base fallback. + if ( empty( $location['country'] ) ) { + $location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) ); + } + return apply_filters( 'woocommerce_customer_default_location_array', $location ); }