Sin descripción

class-wc-integration-maxmind-geolocation.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * MaxMind Geolocation Integration
  4. *
  5. * @version 3.9.0
  6. * @package WooCommerce\Integrations
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. require_once __DIR__ . '/class-wc-integration-maxmind-database-service.php';
  10. /**
  11. * WC Integration MaxMind Geolocation
  12. *
  13. * @since 3.9.0
  14. */
  15. class WC_Integration_MaxMind_Geolocation extends WC_Integration {
  16. /**
  17. * The service responsible for interacting with the MaxMind database.
  18. *
  19. * @var WC_Integration_MaxMind_Database_Service
  20. */
  21. private $database_service;
  22. /**
  23. * Initialize the integration.
  24. */
  25. public function __construct() {
  26. $this->id = 'maxmind_geolocation';
  27. $this->method_title = __( 'MaxMind Geolocation', 'woocommerce' );
  28. $this->method_description = __( 'An integration for utilizing MaxMind to do Geolocation lookups. Please note that this integration will only do country lookups.', 'woocommerce' );
  29. /**
  30. * Supports overriding the database service to be used.
  31. *
  32. * @since 3.9.0
  33. * @return mixed|null The geolocation database service.
  34. */
  35. $this->database_service = apply_filters( 'woocommerce_maxmind_geolocation_database_service', null );
  36. if ( null === $this->database_service ) {
  37. $this->database_service = new WC_Integration_MaxMind_Database_Service( $this->get_database_prefix() );
  38. }
  39. $this->init_form_fields();
  40. $this->init_settings();
  41. // Bind to the save action for the settings.
  42. add_action( 'woocommerce_update_options_integration_' . $this->id, array( $this, 'process_admin_options' ) );
  43. // Trigger notice if license key is missing.
  44. add_action( 'update_option_woocommerce_default_customer_address', array( $this, 'display_missing_license_key_notice' ), 1000, 2 );
  45. /**
  46. * Allows for the automatic database update to be disabled.
  47. *
  48. * @deprecated 3.9.0
  49. * @return bool Whether or not the database should be updated periodically.
  50. */
  51. $bind_updater = apply_filters_deprecated(
  52. 'woocommerce_geolocation_update_database_periodically',
  53. array( true ),
  54. '3.9.0',
  55. 'woocommerce_maxmind_geolocation_update_database_periodically'
  56. );
  57. /**
  58. * Allows for the automatic database update to be disabled.
  59. * Note that MaxMind's TOS requires that the databases be updated or removed periodically.
  60. *
  61. * @since 3.9.0
  62. * @param bool $bind_updater Whether or not the database should be updated periodically.
  63. */
  64. $bind_updater = apply_filters( 'woocommerce_maxmind_geolocation_update_database_periodically', $bind_updater );
  65. // Bind to the scheduled updater action.
  66. if ( $bind_updater ) {
  67. add_action( 'woocommerce_geoip_updater', array( $this, 'update_database' ) );
  68. }
  69. // Bind to the geolocation filter for MaxMind database lookups.
  70. add_filter( 'woocommerce_get_geolocation', array( $this, 'get_geolocation' ), 10, 2 );
  71. }
  72. /**
  73. * Override the normal options so we can print the database file path to the admin,
  74. */
  75. public function admin_options() {
  76. parent::admin_options();
  77. include dirname( __FILE__ ) . '/views/html-admin-options.php';
  78. }
  79. /**
  80. * Initializes the settings fields.
  81. */
  82. public function init_form_fields() {
  83. $this->form_fields = array(
  84. 'license_key' => array(
  85. 'title' => __( 'MaxMind License Key', 'woocommerce' ),
  86. 'type' => 'password',
  87. 'description' => sprintf(
  88. /* translators: %1$s: Documentation URL */
  89. __(
  90. '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>.',
  91. 'woocommerce'
  92. ),
  93. 'https://docs.woocommerce.com/document/maxmind-geolocation-integration/'
  94. ),
  95. 'desc_tip' => false,
  96. 'default' => '',
  97. ),
  98. );
  99. }
  100. /**
  101. * Get database service.
  102. *
  103. * @return WC_Integration_MaxMind_Database_Service|null
  104. */
  105. public function get_database_service() {
  106. return $this->database_service;
  107. }
  108. /**
  109. * Checks to make sure that the license key is valid.
  110. *
  111. * @param string $key The key of the field.
  112. * @param mixed $value The value of the field.
  113. * @return mixed
  114. * @throws Exception When the license key is invalid.
  115. */
  116. public function validate_license_key_field( $key, $value ) {
  117. // Trim whitespaces and strip slashes.
  118. $value = $this->validate_password_field( $key, $value );
  119. // Empty license keys have no need test downloading a database.
  120. if ( empty( $value ) ) {
  121. return $value;
  122. }
  123. // Check the license key by attempting to download the Geolocation database.
  124. $tmp_database_path = $this->database_service->download_database( $value );
  125. if ( is_wp_error( $tmp_database_path ) ) {
  126. WC_Admin_Settings::add_error( $tmp_database_path->get_error_message() );
  127. // Throw an exception to keep from changing this value. This will prevent
  128. // users from accidentally losing their license key, which cannot
  129. // be viewed again after generating.
  130. throw new Exception( $tmp_database_path->get_error_message() );
  131. }
  132. // We may as well put this archive to good use, now that we've downloaded one.
  133. self::update_database( $tmp_database_path );
  134. // Remove missing license key notice.
  135. $this->remove_missing_license_key_notice();
  136. return $value;
  137. }
  138. /**
  139. * Updates the database used for geolocation queries.
  140. *
  141. * @param string|null $new_database_path The path to the new database file. Null will fetch a new archive.
  142. */
  143. public function update_database( $new_database_path = null ) {
  144. // Allow us to easily interact with the filesystem.
  145. require_once ABSPATH . 'wp-admin/includes/file.php';
  146. WP_Filesystem();
  147. global $wp_filesystem;
  148. // Remove any existing archives to comply with the MaxMind TOS.
  149. $target_database_path = $this->database_service->get_database_path();
  150. // If there's no database path, we can't store the database.
  151. if ( empty( $target_database_path ) ) {
  152. return;
  153. }
  154. if ( $wp_filesystem->exists( $target_database_path ) ) {
  155. $wp_filesystem->delete( $target_database_path );
  156. }
  157. if ( isset( $new_database_path ) ) {
  158. $tmp_database_path = $new_database_path;
  159. } else {
  160. // We can't download a database if there's no license key configured.
  161. $license_key = $this->get_option( 'license_key' );
  162. if ( empty( $license_key ) ) {
  163. return;
  164. }
  165. $tmp_database_path = $this->database_service->download_database( $license_key );
  166. if ( is_wp_error( $tmp_database_path ) ) {
  167. wc_get_logger()->notice( $tmp_database_path->get_error_message(), array( 'source' => 'maxmind-geolocation' ) );
  168. return;
  169. }
  170. }
  171. // Move the new database into position.
  172. $wp_filesystem->move( $tmp_database_path, $target_database_path, true );
  173. $wp_filesystem->delete( dirname( $tmp_database_path ) );
  174. }
  175. /**
  176. * Performs a geolocation lookup against the MaxMind database for the given IP address.
  177. *
  178. * @param array $data Geolocation data.
  179. * @param string $ip_address The IP address to geolocate.
  180. * @return array Geolocation including country code, state, city and postcode based on an IP address.
  181. */
  182. public function get_geolocation( $data, $ip_address ) {
  183. // WooCommerce look for headers first, and at this moment could be just enough.
  184. if ( ! empty( $data['country'] ) ) {
  185. return $data;
  186. }
  187. if ( empty( $ip_address ) ) {
  188. return $data;
  189. }
  190. $country_code = $this->database_service->get_iso_country_code_for_ip( $ip_address );
  191. return array(
  192. 'country' => $country_code,
  193. 'state' => '',
  194. 'city' => '',
  195. 'postcode' => '',
  196. );
  197. }
  198. /**
  199. * Fetches the prefix for the MaxMind database file.
  200. *
  201. * @return string
  202. */
  203. private function get_database_prefix() {
  204. $prefix = $this->get_option( 'database_prefix' );
  205. if ( empty( $prefix ) ) {
  206. $prefix = wp_generate_password( 32, false );
  207. $this->update_option( 'database_prefix', $prefix );
  208. }
  209. return $prefix;
  210. }
  211. /**
  212. * Add missing license key notice.
  213. */
  214. private function add_missing_license_key_notice() {
  215. if ( ! class_exists( 'WC_Admin_Notices' ) ) {
  216. include_once WC_ABSPATH . 'includes/admin/class-wc-admin-notices.php';
  217. }
  218. WC_Admin_Notices::add_notice( 'maxmind_license_key' );
  219. }
  220. /**
  221. * Remove missing license key notice.
  222. */
  223. private function remove_missing_license_key_notice() {
  224. if ( ! class_exists( 'WC_Admin_Notices' ) ) {
  225. include_once WC_ABSPATH . 'includes/admin/class-wc-admin-notices.php';
  226. }
  227. WC_Admin_Notices::remove_notice( 'maxmind_license_key' );
  228. }
  229. /**
  230. * Display notice if license key is missing.
  231. *
  232. * @param mixed $old_value Option old value.
  233. * @param mixed $new_value Current value.
  234. */
  235. public function display_missing_license_key_notice( $old_value, $new_value ) {
  236. if ( ! apply_filters( 'woocommerce_maxmind_geolocation_display_notices', true ) ) {
  237. return;
  238. }
  239. if ( ! in_array( $new_value, array( 'geolocation', 'geolocation_ajax' ), true ) ) {
  240. $this->remove_missing_license_key_notice();
  241. return;
  242. }
  243. $license_key = $this->get_option( 'license_key' );
  244. if ( ! empty( $license_key ) ) {
  245. return;
  246. }
  247. $this->add_missing_license_key_notice();
  248. }
  249. }