WooCommerce Code Reference

WC_Geolocation
in package

WC_Geolocation Class.

Table of Contents

GEOLITE2_DB  = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz'
GeoLite2 DB.
GEOLITE_DB  = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'
GeoLite IPv4 DB.
GEOLITE_IPV6_DB  = 'http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz'
GeoLite IPv6 DB.
$geoip_apis  : array<string|int, mixed>
API endpoints for geolocating an IP address
$ip_lookup_apis  : array<string|int, mixed>
API endpoints for looking up user IP address.
disable_geolocation_on_legacy_php()  : string
Prevent geolocation via MaxMind when using legacy versions of php.
geolocate_ip()  : array<string|int, mixed>
Geolocate an IP address.
get_external_ip_address()  : string
Get user IP Address using an external service.
get_ip_address()  : string
Get current user IP Address.
get_local_database_path()  : string
Path to our local db.
init()  : null
Hook in geolocation functionality.
maybe_update_database()  : string
Maybe trigger a DB update for the first time.
update_database()  : mixed
Update geoip database.
geolocate_via_api()  : string
Use APIs to Geolocate the user.
get_country_code_from_headers()  : string
Fetches the country code from the request headers, if one is available.
is_geolocation_enabled()  : bool
Check if geolocation is enabled.

Constants

GEOLITE2_DB

GeoLite2 DB.

public mixed GEOLITE2_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz'
Tags
since
3.4.0
deprecated
3.9.0

GEOLITE_DB

GeoLite IPv4 DB.

public mixed GEOLITE_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz'
Tags
deprecated
3.4.0

GEOLITE_IPV6_DB

GeoLite IPv6 DB.

public mixed GEOLITE_IPV6_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz'
Tags
deprecated
3.4.0

Properties

$geoip_apis

API endpoints for geolocating an IP address

private static array<string|int, mixed> $geoip_apis = array('ipinfo.io' => 'https://ipinfo.io/%s/json', 'ip-api.com' => 'http://ip-api.com/json/%s')

$ip_lookup_apis

API endpoints for looking up user IP address.

private static array<string|int, mixed> $ip_lookup_apis = array('ipify' => 'http://api.ipify.org/', 'ipecho' => 'http://ipecho.net/plain', 'ident' => 'http://ident.me', 'tnedi' => 'http://tnedi.me')

Methods

disable_geolocation_on_legacy_php()

Prevent geolocation via MaxMind when using legacy versions of php.

public static disable_geolocation_on_legacy_php(string $default_customer_address) : string
Parameters
$default_customer_address : string

current value.

Tags
deprecated
3.9.0
since
3.4.0
Return values
string

geolocate_ip()

Geolocate an IP address.

public static geolocate_ip([string $ip_address = '' ][, bool $fallback = false ][, bool $api_fallback = true ]) : array<string|int, mixed>
Parameters
$ip_address : string = ''

IP Address.

$fallback : bool = false

If true, fallbacks to alternative IP detection (can be slower).

$api_fallback : bool = true

If true, uses geolocation APIs if the database file doesn't exist (can be slower).

Return values
array<string|int, mixed>

get_external_ip_address()

Get user IP Address using an external service.

public static get_external_ip_address() : string

This can be used as a fallback for users on localhost where get_ip_address() will be a local IP and non-geolocatable.

Return values
string

get_local_database_path()

Path to our local db.

public static get_local_database_path([string $deprecated = '2' ]) : string
Parameters
$deprecated : string = '2'

Deprecated since 3.4.0.

Tags
deprecated
3.9.0
Return values
string

maybe_update_database()

Maybe trigger a DB update for the first time.

public static maybe_update_database(string $new_value, string $old_value) : string
Parameters
$new_value : string

New value.

$old_value : string

Old value.

Tags
deprecated
3.9.0
Return values
string

update_database()

Update geoip database.

public static update_database() : mixed
Tags
deprecated
3.9.0

Extract files with PharData. Tool built into PHP since 5.3.

Return values
mixed

geolocate_via_api()

Use APIs to Geolocate the user.

private static geolocate_via_api(string $ip_address) : string

Geolocation APIs can be added through the use of the woocommerce_geolocation_geoip_apis filter. Provide a name=>value pair for service-slug=>endpoint.

If APIs are defined, one will be chosen at random to fulfil the request. After completing, the result will be cached in a transient.

Parameters
$ip_address : string

IP address.

Return values
string

get_country_code_from_headers()

Fetches the country code from the request headers, if one is available.

private static get_country_code_from_headers() : string
Tags
since
3.9.0
Return values
stringThe country code pulled from the headers, or empty string if one was not found.

is_geolocation_enabled()

Check if geolocation is enabled.

private static is_geolocation_enabled(string $current_settings) : bool
Parameters
$current_settings : string

Current geolocation settings.

Tags
since
3.4.0
Return values
bool