Exclude geolocation of bots by user agent

Closes #10626
This commit is contained in:
Mike Jolley 2016-04-01 11:14:24 +01:00
parent b2dafc3b33
commit d1f363c356
2 changed files with 17 additions and 5 deletions

View File

@ -204,7 +204,12 @@ class WC_Frontend_Scripts {
self::enqueue_script( 'wc-single-product' ); self::enqueue_script( 'wc-single-product' );
} }
if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) { 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 // Global frontend scripts

View File

@ -718,14 +718,16 @@ function wc_get_base_location() {
* @return array * @return array
*/ */
function wc_get_customer_default_location() { function wc_get_customer_default_location() {
$location = array();
switch ( get_option( 'woocommerce_default_customer_address' ) ) { switch ( get_option( 'woocommerce_default_customer_address' ) ) {
case 'geolocation_ajax' : case 'geolocation_ajax' :
case 'geolocation' : 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 ( ! strstr( $ua, 'bot' ) && ! strstr( $ua, 'spider' ) && ! strstr( $ua, 'crawl' ) ) {
if ( empty( $location['country'] ) ) { $location = WC_Geolocation::geolocate_ip( '', true, false );
$location = wc_format_country_state_string( apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) ) );
} }
break; break;
case 'base' : case 'base' :
@ -736,6 +738,11 @@ function wc_get_customer_default_location() {
break; 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 ); return apply_filters( 'woocommerce_customer_default_location_array', $location );
} }