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,8 +204,13 @@ class WC_Frontend_Scripts {
self::enqueue_script( 'wc-single-product' );
}
if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) ) {
// 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
self::enqueue_script( 'woocommerce', $frontend_script_path . 'woocommerce' . $suffix . '.js', array( 'jquery', 'jquery-blockui' ) );

View File

@ -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 );
}