Updated implementation of geolite2
This commit is contained in:
parent
46110fa39c
commit
992e13fb08
|
@ -164,7 +164,7 @@ class WC_Geolocation {
|
|||
$geolocation = apply_filters(
|
||||
'woocommerce_get_geolocation',
|
||||
array(
|
||||
'country' => $country_code,
|
||||
'country' => $country_code ? $country_code : '',
|
||||
'state' => '',
|
||||
'city' => '',
|
||||
'postcode' => '',
|
||||
|
@ -225,10 +225,10 @@ class WC_Geolocation {
|
|||
* Fetches the country code from the request headers, if one is available.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @return string|false The country code pulled from the headers, or false if one was not found.
|
||||
* @return string The country code pulled from the headers, or empty string if one was not found.
|
||||
*/
|
||||
private static function get_country_code_from_headers() {
|
||||
$country_code = false;
|
||||
$country_code = '';
|
||||
|
||||
$headers = array(
|
||||
'MM_COUNTRY_CODE',
|
||||
|
|
|
@ -72,7 +72,7 @@ class WC_Integration_MaxMind_Geolocation extends WC_Integration {
|
|||
}
|
||||
|
||||
// Bind to the geolocation filter for MaxMind database lookups.
|
||||
add_filter( 'woocommerce_geolocate_ip', array( $this, 'geolocate_ip' ), 10, 2 );
|
||||
add_filter( 'woocommerce_get_geolocation', array( $this, 'get_geolocation' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -196,22 +196,28 @@ class WC_Integration_MaxMind_Geolocation extends WC_Integration {
|
|||
/**
|
||||
* Performs a geolocation lookup against the MaxMind database for the given IP address.
|
||||
*
|
||||
* @param string $country_code The country code for the IP address.
|
||||
* @param array $data Geolocation data.
|
||||
* @param string $ip_address The IP address to geolocate.
|
||||
* @return string The country code for the IP address.
|
||||
* @return array Geolocation including country code, state, city and postcode based on an IP address.
|
||||
*/
|
||||
public function geolocate_ip( $country_code, $ip_address ) {
|
||||
if ( false !== $country_code ) {
|
||||
return $country_code;
|
||||
public function get_geolocation( $data, $ip_address ) {
|
||||
// WooCommerce look for headers first, and at this moment could be just enough.
|
||||
if ( ! empty( $data['country'] ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ( empty( $ip_address ) ) {
|
||||
return $country_code;
|
||||
return $data;
|
||||
}
|
||||
|
||||
$country_code = $this->database_service->get_iso_country_code_for_ip( $ip_address );
|
||||
|
||||
return $country_code ? $country_code : false;
|
||||
return array(
|
||||
'country' => $country_code ? $country_code : '',
|
||||
'state' => '',
|
||||
'city' => '',
|
||||
'postcode' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -77,18 +77,18 @@ class WC_Tests_MaxMind_Integration extends WC_Unit_Test_Case {
|
|||
* Make sure that the geolocate_ip method does not squash existing country codes.
|
||||
*/
|
||||
public function test_geolocate_ip_returns_existing_country_code() {
|
||||
$country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( 'US', '192.168.1.1' );
|
||||
$data = ( new WC_Integration_MaxMind_Geolocation() )->get_geolocation( array( 'country' => 'US' ), '192.168.1.1' );
|
||||
|
||||
$this->assertEquals( 'US', $country_code );
|
||||
$this->assertEquals( 'US', $data['country'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure that the geolocate_ip method does nothing if IP is not set.
|
||||
*/
|
||||
public function test_geolocate_ip_returns_empty_without_ip_address() {
|
||||
$country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( false, '' );
|
||||
$data = ( new WC_Integration_MaxMind_Geolocation() )->get_geolocation( array(), '' );
|
||||
|
||||
$this->assertFalse( $country_code );
|
||||
$this->assertEmpty( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,9 +100,9 @@ class WC_Tests_MaxMind_Integration extends WC_Unit_Test_Case {
|
|||
->with( '192.168.1.1' )
|
||||
->willReturn( 'US' );
|
||||
|
||||
$country_code = ( new WC_Integration_MaxMind_Geolocation() )->geolocate_ip( false, '192.168.1.1' );
|
||||
$data = ( new WC_Integration_MaxMind_Geolocation() )->get_geolocation( array(), '192.168.1.1' );
|
||||
|
||||
$this->assertEquals( 'US', $country_code );
|
||||
$this->assertEquals( 'US', $data['country'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue