2020-01-09 05:09:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-01-09 23:46:01 +00:00
|
|
|
* WC Integration MaxMind Geolocation Database
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
|
|
|
* @version 3.9.0
|
|
|
|
* @package WooCommerce/Integrations
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
2020-01-09 23:46:01 +00:00
|
|
|
* WC Integration MaxMind Geolocation Database
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
|
|
|
* @version 3.9.0
|
|
|
|
*/
|
2020-01-09 23:46:01 +00:00
|
|
|
class WC_Integration_MaxMind_Geolocation_Database {
|
2020-01-09 05:09:35 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-09 05:48:05 +00:00
|
|
|
* The name of the MaxMind database to utilize.
|
|
|
|
*/
|
|
|
|
const DATABASE = 'GeoLite2-Country';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The extension for the MaxMind database.
|
2020-01-09 05:09:35 +00:00
|
|
|
*/
|
2020-01-09 05:48:05 +00:00
|
|
|
const DATABASE_EXTENSION = '.mmdb';
|
2020-01-09 05:09:35 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-09 05:48:05 +00:00
|
|
|
* Fetches the path that the database should be stored.
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
2020-01-09 05:48:05 +00:00
|
|
|
* @return string The local database path.
|
2020-01-09 05:09:35 +00:00
|
|
|
*/
|
2020-01-09 05:59:04 +00:00
|
|
|
public static function get_database_path() {
|
2020-01-09 05:48:05 +00:00
|
|
|
$database_path = WP_CONTENT_DIR . '/uploads/' . self::DATABASE . self::DATABASE_EXTENSION;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the geolocation database storage path.
|
|
|
|
*
|
|
|
|
* @param string $database_path The path to the database.
|
|
|
|
* @param int $version Deprecated since 3.4.0.
|
|
|
|
* @deprecated 3.9.0
|
|
|
|
*/
|
|
|
|
$database_path = apply_filters_deprecated(
|
|
|
|
'woocommerce_geolocation_local_database_path',
|
|
|
|
array( $database_path, 2 ),
|
|
|
|
'3.9.0',
|
|
|
|
'woocommerce_maxmind_geolocation_database_path'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the geolocation database storage path.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param string $database_path The path to the database.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'woocommerce_maxmind_geolocation_database_path', $database_path );
|
2020-01-09 05:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the database from the MaxMind service.
|
|
|
|
*
|
2020-01-09 05:48:05 +00:00
|
|
|
* @param string $license_key The license key to be used when downloading the database.
|
2020-01-09 05:09:35 +00:00
|
|
|
* @return string|WP_Error The path to the database file or an error if invalid.
|
|
|
|
*/
|
2020-01-09 05:59:04 +00:00
|
|
|
public static function download_database( $license_key ) {
|
2020-01-09 05:09:35 +00:00
|
|
|
$download_uri = 'https://download.maxmind.com/app/geoip_download?';
|
|
|
|
$download_uri .= http_build_query(
|
|
|
|
array(
|
2020-01-09 05:48:05 +00:00
|
|
|
'edition_id' => self::DATABASE,
|
|
|
|
'license_key' => $license_key,
|
2020-01-09 05:09:35 +00:00
|
|
|
'suffix' => 'tar.gz',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Needed for the download_url call right below.
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
|
|
|
|
|
|
$tmp_archive_path = download_url( $download_uri );
|
|
|
|
if ( is_wp_error( $tmp_archive_path ) ) {
|
|
|
|
// Transform the error into something more informative.
|
|
|
|
$error_data = $tmp_archive_path->get_error_data();
|
|
|
|
if ( isset( $error_data['code'] ) ) {
|
|
|
|
switch ( $error_data['code'] ) {
|
|
|
|
case 401:
|
|
|
|
return new WP_Error(
|
|
|
|
'woocommerce_maxmind_geolocation_database_license_key',
|
|
|
|
__( 'The MaxMind license key is invalid.', 'woocommerce' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error( 'woocommerce_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the database from the archive.
|
|
|
|
try {
|
|
|
|
$file = new PharData( $tmp_archive_path );
|
|
|
|
|
2020-01-09 05:48:05 +00:00
|
|
|
$tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION;
|
2020-01-09 05:09:35 +00:00
|
|
|
|
|
|
|
$file->extractTo(
|
2020-01-09 06:56:24 +00:00
|
|
|
dirname( $tmp_archive_path ),
|
2020-01-09 05:48:05 +00:00
|
|
|
trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION,
|
2020-01-09 05:09:35 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
} catch ( Exception $exception ) {
|
|
|
|
return new WP_Error( 'woocommerce_maxmind_geolocation_database_archive', $exception->getMessage() );
|
|
|
|
} finally {
|
|
|
|
// Remove the archive since we only care about a single file in it.
|
|
|
|
unlink( $tmp_archive_path );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tmp_database_path;
|
|
|
|
}
|
|
|
|
}
|