Added a database option to allow for selecting different MaxMind databases

This commit is contained in:
Christopher Allford 2020-01-13 14:10:08 -08:00
parent f9d8b85c04
commit d32c470cea
4 changed files with 47 additions and 20 deletions

View File

@ -14,12 +14,6 @@ defined( 'ABSPATH' ) || exit;
* @since 3.9.0
*/
class WC_Integration_MaxMind_Database_Service {
/**
* The name of the MaxMind database to utilize.
*/
const DATABASE = 'GeoLite2-Country';
/**
* The extension for the MaxMind database.
*/
@ -32,13 +26,31 @@ class WC_Integration_MaxMind_Database_Service {
*/
private $database_prefix;
/**
* The MaxMind database to download.
*
* @var string
*/
private $database;
/**
* WC_Integration_MaxMind_Database_Service constructor.
*
* @param string|null $database_prefix A prefix for the MaxMind database filename.
* @param string $database The MaxMind database to download.
*/
public function __construct( $database_prefix ) {
public function __construct( $database_prefix, $database ) {
$this->database_prefix = $database_prefix;
$this->database = $database;
}
/**
* Changes the MaxMind database to download.
*
* @param string $database The new MaxMind database to download.
*/
public function set_database( $database ) {
$this->database = $database;
}
/**
@ -53,7 +65,7 @@ class WC_Integration_MaxMind_Database_Service {
if ( ! empty( $this->database_prefix ) ) {
$database_path .= $this->database_prefix . '-';
}
$database_path .= self::DATABASE . self::DATABASE_EXTENSION;
$database_path .= 'MaxMind-Geolocation-Database' . self::DATABASE_EXTENSION;
/**
* Filter the geolocation database storage path.
@ -87,7 +99,7 @@ class WC_Integration_MaxMind_Database_Service {
public function download_database( $license_key ) {
$download_uri = add_query_arg(
array(
'edition_id' => self::DATABASE,
'edition_id' => $this->database,
'license_key' => wc_clean( $license_key ),
'suffix' => 'tar.gz',
),
@ -118,11 +130,11 @@ class WC_Integration_MaxMind_Database_Service {
try {
$file = new PharData( $tmp_archive_path );
$tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION;
$tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . $this->database . self::DATABASE_EXTENSION;
$file->extractTo(
dirname( $tmp_archive_path ),
trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION,
trailingslashit( $file->current()->getFilename() ) . $this->database . self::DATABASE_EXTENSION,
true
);
} catch ( Exception $exception ) {

View File

@ -40,7 +40,10 @@ class WC_Integration_MaxMind_Geolocation extends WC_Integration {
*/
$this->database_service = apply_filters( 'woocommerce_maxmind_geolocation_database_service', null );
if ( null === $this->database_service ) {
$this->database_service = new WC_Integration_MaxMind_Database_Service( $this->get_database_prefix() );
$this->database_service = new WC_Integration_MaxMind_Database_Service(
$this->get_database_prefix(),
$this->get_option( 'database' )
);
}
$this->init_form_fields();
@ -80,11 +83,23 @@ class WC_Integration_MaxMind_Geolocation extends WC_Integration {
*/
public function init_form_fields() {
$this->form_fields = array(
// Note: The database field MUST be first, as the license_key validation relies on the value of it!
'database' => array(
'title' => __( 'Database', 'woocommerce' ),
'type' => 'select',
'description' => __( 'The database chosen decides the precision of the geolocation search. You should note that higher precision database are larger in size.', 'woocommerce' ),
'options' => array(
'GeoLite2-Country' => __( 'GeoLite2 - Country', 'woocommerce' ),
'GeoLite2-City' => __( 'GeoLite2 - City', 'woocommerce' ),
),
'desc_tip' => false,
'default' => 'GeoLite2-Country',
),
'license_key' => array(
'title' => __( 'MaxMind License Key', 'woocommerce' ),
'type' => 'password',
'description' => sprintf(
/* translators: %1$s: Documentation URL */
/* translators: %1$s: Documentation URL */
__(
'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\'s License Key Documentation</a>.',
'woocommerce'

View File

@ -26,10 +26,10 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
* @expectedDeprecated woocommerce_geolocation_local_database_path
*/
public function test_database_path_filters() {
$database_service = new WC_Integration_MaxMind_Database_Service( '' );
$database_service = new WC_Integration_MaxMind_Database_Service( '', '' );
$path = $database_service->get_database_path();
$this->assertEquals( WP_CONTENT_DIR . '/uploads/woocommerce_uploads/' . WC_Integration_MaxMind_Database_Service::DATABASE . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION, $path );
$this->assertEquals( WP_CONTENT_DIR . '/uploads/woocommerce_uploads/MaxMind-Geolocation-Database' . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION, $path );
add_filter( 'woocommerce_geolocation_local_database_path', array( $this, 'filter_database_path_deprecated' ), 1, 2 );
$path = $database_service->get_database_path();
@ -44,17 +44,17 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
$this->assertEquals( '/filter', $path );
// Now perform any tests with a database file prefix.
$database_service = new WC_Integration_MaxMind_Database_Service( 'testing' );
$database_service = new WC_Integration_MaxMind_Database_Service( 'testing', '' );
$path = $database_service->get_database_path();
$this->assertEquals( WP_CONTENT_DIR . '/uploads/woocommerce_uploads/testing-' . WC_Integration_MaxMind_Database_Service::DATABASE . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION, $path );
$this->assertEquals( WP_CONTENT_DIR . '/uploads/woocommerce_uploads/testing-MaxMind-Geolocation-Database' . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION, $path );
}
/**
* Tests that the database download works as expected.
*/
public function test_download_database_works() {
$database_service = new WC_Integration_MaxMind_Database_Service( '' );
$database_service = new WC_Integration_MaxMind_Database_Service( '', 'GeoLite2-Country' );
$expected_database = '/tmp/GeoLite2-Country_20200100/GeoLite2-Country.mmdb';
$result = $database_service->download_database( 'testing_license' );
@ -70,7 +70,7 @@ class WC_Tests_MaxMind_Database extends WC_Unit_Test_Case {
* Tests the that database download wraps the download and extraction errors.
*/
public function test_download_database_wraps_errors() {
$database_service = new WC_Integration_MaxMind_Database_Service( '' );
$database_service = new WC_Integration_MaxMind_Database_Service( '', 'GeoLite2-Country' );
$result = $database_service->download_database( 'invalid_license' );

View File

@ -65,7 +65,7 @@ class WC_Tests_MaxMind_Integration extends WC_Unit_Test_Case {
$this->database_service->expects( $this->once() )
->method( 'download_database' )
->with( 'test_license' )
->willReturn( '/tmp/' . WC_Integration_MaxMind_Database_Service::DATABASE . '.' . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION );
->willReturn( '/tmp/MaxMind-Geolocation-Database.' . WC_Integration_MaxMind_Database_Service::DATABASE_EXTENSION );
$integration = new WC_Integration_MaxMind_Geolocation();
$integration->update_option( 'license_key', 'test_license' );