diff --git a/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-database-service.php b/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-database-service.php index e56730372fe..40e4a5b56d4 100644 --- a/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-database-service.php +++ b/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-database-service.php @@ -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 ) { diff --git a/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-geolocation.php b/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-geolocation.php index b390fe94106..1cd51f6488c 100644 --- a/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-geolocation.php +++ b/includes/integrations/maxmind-geolocation/class-wc-integration-maxmind-geolocation.php @@ -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 MaxMind\'s License Key Documentation.', 'woocommerce' diff --git a/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-database.php b/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-database.php index aba5a35a1af..64a0c83bef1 100644 --- a/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-database.php +++ b/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-database.php @@ -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' ); diff --git a/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-integration.php b/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-integration.php index 53c40bd523a..8da1685c850 100644 --- a/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-integration.php +++ b/tests/unit-tests/integrations/maxmind-geolocation/class-wc-tests-maxmind-integration.php @@ -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' );