Create New Item
×
Item Type
File
Folder
Item Name
×
Search file in folder and subfolders...
File Manager
/
wp-content
/
plugins
/
woocommerce
/
includes
/
integrations
/
maxmind-geolocation
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?php defined('ABSPATH') || exit; require_once __DIR__ . '/class-wc-integration-maxmind-database-service.php'; class WC_Integration_MaxMind_Geolocation extends WC_Integration { private $database_service; public function __construct() { $this->id = 'maxmind_geolocation'; $this->method_title = __('MaxMind Geolocation', 'woocommerce'); $this->method_description = __('An integration for utilizing MaxMind to do Geolocation lookups. Please note that this integration will only do country lookups.', 'woocommerce'); $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->init_form_fields(); $this->init_settings(); add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options')); add_action('update_option_woocommerce_default_customer_address', array($this, 'display_missing_license_key_notice'), 1000, 2); $bind_updater = apply_filters_deprecated('woocommerce_geolocation_update_database_periodically', array(true), '3.9.0', 'woocommerce_maxmind_geolocation_update_database_periodically'); $bind_updater = apply_filters('woocommerce_maxmind_geolocation_update_database_periodically', $bind_updater); if ($bind_updater) { add_action('woocommerce_geoip_updater', array($this, 'update_database')); } add_filter('woocommerce_get_geolocation', array($this, 'get_geolocation'), 10, 2); } public function admin_options() { parent::admin_options(); include dirname(__FILE__) . '/views/html-admin-options.php'; } public function init_form_fields() { $this->form_fields = array('license_key' => array('title' => __('MaxMind License Key', 'woocommerce'), 'type' => 'password', 'description' => sprintf(__('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>.', 'woocommerce'), 'https://docs.woocommerce.com/document/maxmind-geolocation-integration/'), 'desc_tip' => false, 'default' => '')); } public function get_database_service() { return $this->database_service; } public function validate_license_key_field($key, $value) { $value = $this->validate_password_field($key, $value); if (empty($value)) { return $value; } $tmp_database_path = $this->database_service->download_database($value); if (is_wp_error($tmp_database_path)) { WC_Admin_Settings::add_error($tmp_database_path->get_error_message()); throw new Exception($tmp_database_path->get_error_message()); } self::update_database($tmp_database_path); $this->remove_missing_license_key_notice(); return $value; } public function update_database($new_database_path = null) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; $target_database_path = $this->database_service->get_database_path(); if (empty($target_database_path)) { return; } 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 { $license_key = $this->get_option('license_key'); if (empty($license_key)) { return; } $tmp_database_path = $this->database_service->download_database($license_key); if (is_wp_error($tmp_database_path)) { wc_get_logger()->notice($tmp_database_path->get_error_message(), array('source' => 'maxmind-geolocation')); return; } } $wp_filesystem->move($tmp_database_path, $target_database_path, true); $wp_filesystem->delete(dirname($tmp_database_path)); } public function get_geolocation($data, $ip_address) { if (!empty($data['country'])) { return $data; } if (empty($ip_address)) { return $data; } $country_code = $this->database_service->get_iso_country_code_for_ip($ip_address); return array('country' => $country_code, 'state' => '', 'city' => '', 'postcode' => ''); } 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; } 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'); } 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'); } 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(); } }