2020-01-09 05:08:25 +00:00
< ? php
/**
* MaxMind Geolocation Integration
*
* @ version 3.9 . 0
* @ package WooCommerce / Integrations
*/
defined ( 'ABSPATH' ) || exit ;
2020-01-10 10:09:51 +00:00
require_once 'class-wc-integration-maxmind-database-service.php' ;
2020-01-10 05:29:47 +00:00
2020-01-09 05:08:25 +00:00
/**
2020-01-09 23:46:01 +00:00
* WC Integration MaxMind Geolocation
2020-01-09 05:08:25 +00:00
*
2020-01-10 07:46:25 +00:00
* @ since 3.9 . 0
2020-01-09 05:08:25 +00:00
*/
2020-01-09 23:46:01 +00:00
class WC_Integration_MaxMind_Geolocation extends WC_Integration {
2020-01-09 05:08:25 +00:00
2020-01-10 10:09:51 +00:00
/**
* The service responsible for interacting with the MaxMind database .
*
* @ var WC_Integration_MaxMind_Database_Service
*/
private $database_service ;
2020-01-09 05:08:25 +00:00
/**
* Initialize the integration .
*/
public function __construct () {
2020-01-10 18:02:11 +00:00
$this -> id = 'maxmind_geolocation' ;
2020-01-09 05:08:25 +00:00
$this -> method_title = __ ( 'WooCommerce MaxMind Geolocation' , 'woocommerce' );
2020-01-13 22:43:00 +00:00
$this -> method_description = __ ( 'An integration for utilizing MaxMind to do Geolocation lookups. Please note that this integration will only do country lookups.' , 'woocommerce' );
2020-01-09 05:08:25 +00:00
2020-01-10 10:09:51 +00:00
/**
* Supports overriding the database service to be used .
*
* @ since 3.9 . 0
* @ return mixed | null The geolocation database service .
*/
$this -> database_service = apply_filters ( 'woocommerce_maxmind_geolocation_database_service' , null );
if ( null === $this -> database_service ) {
2020-01-13 22:41:24 +00:00
$this -> database_service = new WC_Integration_MaxMind_Database_Service ( $this -> get_database_prefix () );
2020-01-10 10:09:51 +00:00
}
2020-01-09 05:08:25 +00:00
$this -> init_form_fields ();
$this -> init_settings ();
// Bind to the save action for the settings.
add_action ( 'woocommerce_update_options_integration_' . $this -> id , array ( $this , 'process_admin_options' ) );
2020-01-10 07:22:42 +00:00
2020-01-10 18:02:11 +00:00
// Trigger notice if license key is missing.
add_action ( 'update_option_woocommerce_default_customer_address' , array ( $this , 'display_missing_license_key_notice' ), 1000 , 2 );
2020-01-10 07:22:42 +00:00
/**
* Allows for the automatic database update to be disabled .
*
* @ deprecated 3.9 . 0
* @ return bool Whether or not the database should be updated periodically .
*/
$bind_updater = apply_filters_deprecated (
'woocommerce_geolocation_update_database_periodically' ,
array ( true ),
'3.9.0' ,
2020-01-14 19:57:07 +00:00
'woocommerce_maxmind_geolocation_update_database_periodically'
2020-01-10 07:22:42 +00:00
);
2020-01-14 19:57:07 +00:00
/**
* Allows for the automatic database update to be disabled .
* Note that MaxMind ' s TOS requires that the databases be updated or removed periodically .
*
* @ since 3.9 . 0
* @ param bool $bind_updater Whether or not the database should be updated periodically .
*/
$bind_updater = apply_filters ( 'woocommerce_maxmind_geolocation_update_database_periodically' , $bind_updater );
2020-01-10 07:22:42 +00:00
// Bind to the scheduled updater action.
if ( $bind_updater ) {
2020-01-10 10:09:51 +00:00
add_action ( 'woocommerce_geoip_updater' , array ( $this , 'update_database' ) );
2020-01-10 07:22:42 +00:00
}
2020-01-10 07:46:25 +00:00
// Bind to the geolocation filter for MaxMind database lookups.
2020-01-14 19:24:07 +00:00
add_filter ( 'woocommerce_get_geolocation' , array ( $this , 'get_geolocation' ), 10 , 2 );
2020-01-09 05:08:25 +00:00
}
2020-01-14 05:02:22 +00:00
/**
* Override the normal options so we can print the database file path to the admin ,
*/
public function admin_options () {
parent :: admin_options ();
2020-01-14 15:20:46 +00:00
include dirname ( __FILE__ ) . '/views/html-admin-options.php' ;
2020-01-14 05:02:22 +00:00
}
2020-01-09 05:08:25 +00:00
/**
* Initializes the settings fields .
*/
public function init_form_fields () {
$this -> form_fields = array (
'license_key' => array (
'title' => __ ( 'MaxMind License Key' , 'woocommerce' ),
'type' => 'password' ,
2020-01-10 05:39:28 +00:00
'description' => sprintf (
2020-01-13 22:41:24 +00:00
/* translators: %1$s: Documentation URL */
2020-01-10 05:39:28 +00:00
__ (
2020-01-20 16:18:52 +00:00
'The key that will be used when dealing with MaxMind Geolocation services. You can read how to generate one in <a href="%1$s">MaxMind Geolocation Integration documentation</a>.' ,
2020-01-10 05:39:28 +00:00
'woocommerce'
),
2020-01-20 16:18:52 +00:00
'https://docs.woocommerce.com/document/maxmind-geolocation-integration/'
2020-01-10 05:39:28 +00:00
),
2020-01-09 05:08:25 +00:00
'desc_tip' => false ,
'default' => '' ,
),
);
}
2020-01-10 15:22:42 +00:00
/**
* Get database service .
*
* @ return WC_Integration_MaxMind_Database_Service | null
*/
public function get_database_service () {
return $this -> database_service ;
}
2020-01-09 05:08:25 +00:00
/**
* Checks to make sure that the license key is valid .
*
* @ param string $key The key of the field .
* @ param mixed $value The value of the field .
* @ return mixed
* @ throws Exception When the license key is invalid .
*/
public function validate_license_key_field ( $key , $value ) {
// Empty license keys have no need to validate the data.
if ( empty ( $value ) ) {
return $value ;
}
2020-01-22 15:38:10 +00:00
// Trim whitespaces and strip slashes.
$value = $this -> validate_password_field ( $key , $value );
2020-01-09 05:09:35 +00:00
// Check the license key by attempting to download the Geolocation database.
2020-01-10 10:09:51 +00:00
$tmp_database_path = $this -> database_service -> download_database ( $value );
2020-01-10 07:22:42 +00:00
if ( is_wp_error ( $tmp_database_path ) ) {
WC_Admin_Settings :: add_error ( $tmp_database_path -> get_error_message () );
2020-01-09 05:09:35 +00:00
// Throw an exception to keep from changing this value. This will prevent
// users from accidentally losing their license key, which cannot
// be viewed again after generating.
2020-01-10 07:22:42 +00:00
throw new Exception ( $tmp_database_path -> get_error_message () );
2020-01-09 05:09:35 +00:00
}
2020-01-10 07:22:42 +00:00
// We may as well put this archive to good use, now that we've downloaded one.
self :: update_database ( $tmp_database_path );
2020-01-13 20:40:10 +00:00
// Remove missing license key notice.
$this -> remove_missing_license_key_notice ();
2020-01-09 05:08:25 +00:00
return $value ;
}
2020-01-10 07:22:42 +00:00
/**
* Updates the database used for geolocation queries .
*
* @ param string | null $new_database_path The path to the new database file . Null will fetch a new archive .
*/
2020-01-10 10:09:51 +00:00
public function update_database ( $new_database_path = null ) {
2020-01-10 07:22:42 +00:00
// Allow us to easily interact with the filesystem.
require_once ABSPATH . 'wp-admin/includes/file.php' ;
WP_Filesystem ();
global $wp_filesystem ;
// Remove any existing archives to comply with the MaxMind TOS.
2020-01-10 10:09:51 +00:00
$target_database_path = $this -> database_service -> get_database_path ();
2020-01-10 10:47:23 +00:00
// If there's no database path, we can't store the database.
if ( empty ( $target_database_path ) ) {
return ;
}
2020-01-10 07:22:42 +00:00
if ( $wp_filesystem -> exists ( $target_database_path ) ) {
$wp_filesystem -> delete ( $target_database_path );
}
if ( isset ( $new_database_path ) ) {
$tmp_database_path = $new_database_path ;
} else {
// We can't download a database if there's no license key configured.
2020-01-10 10:09:51 +00:00
$license_key = $this -> get_option ( 'license_key' );
2020-01-10 07:22:42 +00:00
if ( empty ( $license_key ) ) {
return ;
}
2020-01-10 10:09:51 +00:00
$tmp_database_path = $this -> database_service -> download_database ( $license_key );
2020-01-10 07:22:42 +00:00
if ( is_wp_error ( $tmp_database_path ) ) {
2020-01-10 07:46:25 +00:00
wc_get_logger () -> notice ( $tmp_database_path -> get_error_message (), array ( 'source' => 'maxmind-geolocation' ) );
2020-01-10 07:22:42 +00:00
return ;
}
}
// Move the new database into position.
$wp_filesystem -> move ( $tmp_database_path , $target_database_path , true );
$wp_filesystem -> delete ( dirname ( $tmp_database_path ) );
}
2020-01-10 07:46:25 +00:00
/**
* Performs a geolocation lookup against the MaxMind database for the given IP address .
*
2020-01-14 19:24:07 +00:00
* @ param array $data Geolocation data .
2020-01-10 07:46:25 +00:00
* @ param string $ip_address The IP address to geolocate .
2020-01-14 19:24:07 +00:00
* @ return array Geolocation including country code , state , city and postcode based on an IP address .
2020-01-10 07:46:25 +00:00
*/
2020-01-14 19:24:07 +00:00
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 ;
2020-01-10 07:46:25 +00:00
}
2020-01-10 08:07:55 +00:00
if ( empty ( $ip_address ) ) {
2020-01-14 19:24:07 +00:00
return $data ;
2020-01-10 08:07:55 +00:00
}
2020-01-10 10:09:51 +00:00
$country_code = $this -> database_service -> get_iso_country_code_for_ip ( $ip_address );
2020-01-10 08:07:55 +00:00
2020-01-14 19:24:07 +00:00
return array (
2020-01-21 05:20:32 +00:00
'country' => $country_code ,
2020-01-14 19:24:07 +00:00
'state' => '' ,
'city' => '' ,
'postcode' => '' ,
);
2020-01-10 07:46:25 +00:00
}
2020-01-10 18:02:11 +00:00
2020-01-13 16:16:44 +00:00
/**
* Fetches the prefix for the MaxMind database file .
*
* @ return string
*/
private function get_database_prefix () {
$prefix = $this -> get_option ( 'database_prefix' );
if ( empty ( $prefix ) ) {
$prefix = wp_generate_password ( 32 , false );
$this -> update_option ( 'database_prefix' , $prefix );
}
return $prefix ;
}
2020-01-10 18:02:11 +00:00
/**
* Add missing license key notice .
*/
private function add_missing_license_key_notice () {
if ( ! class_exists ( 'WC_Admin_Notices' ) ) {
include_once WC_ABSPATH . 'includes/admin/class-wc-admin-notices.php' ;
}
WC_Admin_Notices :: add_notice ( 'maxmind_license_key' );
}
/**
* Remove missing license key notice .
*/
private function remove_missing_license_key_notice () {
if ( ! class_exists ( 'WC_Admin_Notices' ) ) {
include_once WC_ABSPATH . 'includes/admin/class-wc-admin-notices.php' ;
}
WC_Admin_Notices :: remove_notice ( 'maxmind_license_key' );
}
/**
* Display notice if license key is missing .
*
* @ param mixed $old_value Option old value .
* @ param mixed $new_value Current value .
*/
public function display_missing_license_key_notice ( $old_value , $new_value ) {
if ( ! apply_filters ( 'woocommerce_maxmind_geolocation_display_notices' , true ) ) {
return ;
}
if ( ! in_array ( $new_value , array ( 'geolocation' , 'geolocation_ajax' ), true ) ) {
$this -> remove_missing_license_key_notice ();
return ;
}
$license_key = $this -> get_option ( 'license_key' );
if ( ! empty ( $license_key ) ) {
return ;
}
$this -> add_missing_license_key_notice ();
}
2020-01-09 05:08:25 +00:00
}