Created WC_Geolite_Integration class to handle with GeoLite2 integration
This commit is contained in:
parent
29b9e9c085
commit
7f6f4b848d
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
/**
|
||||
* Wrapper for MaxMind GeoLite2 Reader
|
||||
*
|
||||
* This class provide an interface to handle geolocation and error handling.
|
||||
*
|
||||
* Requires PHP 5.4+.
|
||||
*
|
||||
* @package WooCommerce\Classes
|
||||
* @since 3.4.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Geolite integration class.
|
||||
*/
|
||||
class WC_Geolite_Integration {
|
||||
|
||||
/**
|
||||
* MaxMind GeoLite2 database path.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $database = '';
|
||||
|
||||
/**
|
||||
* Logger instance.
|
||||
*
|
||||
* @var WC_Logger
|
||||
*/
|
||||
private $log = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $database MaxMind GeoLite2 database path.
|
||||
*/
|
||||
public function __construct( $database ) {
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get country 2-letters ISO by IP address.
|
||||
* Retuns empty string when not able to find any ISO code.
|
||||
*
|
||||
* @param string $ip_address User IP address.
|
||||
* @return string
|
||||
*/
|
||||
public function get_country_iso( $ip_address ) {
|
||||
if ( ! class_exists( 'MaxMind\\Db\\Reader', false ) ) {
|
||||
$this->require_geolite_library();
|
||||
}
|
||||
|
||||
$iso_code = '';
|
||||
|
||||
try {
|
||||
$reader = new MaxMind\Db\Reader( $this->database ); // phpcs:ignore PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
|
||||
$data = $reader->get( $ip_address );
|
||||
$iso_code = $data['country']['iso_code'];
|
||||
|
||||
$reader->close();
|
||||
} catch ( Exception $e ) {
|
||||
$this->log( $e->getMessage(), 'error' );
|
||||
}
|
||||
|
||||
return sanitize_text_field( strtoupper( $iso_code ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Logging method.
|
||||
*
|
||||
* @param string $message Log message.
|
||||
* @param string $level Log level.
|
||||
* Available options: 'emergency', 'alert',
|
||||
* 'critical', 'error', 'warning', 'notice',
|
||||
* 'info' and 'debug'.
|
||||
* Defaults to 'info'.
|
||||
*/
|
||||
private function log( $message, $level = 'info' ) {
|
||||
if ( is_null( $this->log ) ) {
|
||||
$this->log = wc_get_logger();
|
||||
}
|
||||
|
||||
$this->log->log( $level, $message, array( 'source' => 'geoip' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Require geolite library.
|
||||
*/
|
||||
private function require_geolite_library() {
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Decoder.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/InvalidDatabaseException.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Metadata.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Util.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader.php';
|
||||
}
|
||||
}
|
|
@ -252,17 +252,6 @@ class WC_Geolocation {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Require geolite library.
|
||||
*/
|
||||
private static function require_geolite_library() {
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Decoder.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/InvalidDatabaseException.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Metadata.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader/Util.php';
|
||||
require_once WC_ABSPATH . 'includes/libraries/geolite2/Reader.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Use MAXMIND GeoLite database to geolocation the user.
|
||||
*
|
||||
|
@ -271,25 +260,13 @@ class WC_Geolocation {
|
|||
* @return string
|
||||
*/
|
||||
private static function geolocate_via_db( $ip_address, $database ) {
|
||||
if ( ! class_exists( 'MaxMind\\Db\\Reader', false ) ) {
|
||||
self::require_geolite_library();
|
||||
if ( ! class_exists( 'WC_Geolite_Integration', false ) ) {
|
||||
require_once WC_ABSPATH . 'includes/class-wc-geolite-integration.php';
|
||||
}
|
||||
|
||||
$country_code = '';
|
||||
$geolite = new WC_Geolite_Integration( $database );
|
||||
|
||||
try {
|
||||
$reader = new MaxMind\Db\Reader( $database ); // phpcs:ignore PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
|
||||
|
||||
$data = $reader->get( $ip_address );
|
||||
$country_code = $data['country']['iso_code'];
|
||||
|
||||
$reader->close();
|
||||
} catch ( Exception $e ) {
|
||||
$log = wc_get_logger();
|
||||
$log->log( 'error', $e->getMessage(), array( 'source' => 'geoip' ) );
|
||||
}
|
||||
|
||||
return sanitize_text_field( strtoupper( $country_code ) );
|
||||
return $geolite->get_country_iso( $ip_address );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue