Нет описания

class-wc-geolite-integration.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Wrapper for MaxMind GeoLite2 Reader
  4. *
  5. * This class provide an interface to handle geolocation and error handling.
  6. *
  7. * Requires PHP 5.4+.
  8. *
  9. * @package WooCommerce\Classes
  10. * @since 3.4.0
  11. * @deprecated 3.9.0
  12. */
  13. defined( 'ABSPATH' ) || exit;
  14. /**
  15. * Geolite integration class.
  16. *
  17. * @deprecated 3.9.0
  18. */
  19. class WC_Geolite_Integration {
  20. /**
  21. * MaxMind GeoLite2 database path.
  22. *
  23. * @var string
  24. */
  25. private $database = '';
  26. /**
  27. * Logger instance.
  28. *
  29. * @var WC_Logger
  30. */
  31. private $log = null;
  32. /**
  33. * Constructor.
  34. *
  35. * @param string $database MaxMind GeoLite2 database path.
  36. */
  37. public function __construct( $database ) {
  38. $this->database = $database;
  39. }
  40. /**
  41. * Get country 2-letters ISO by IP address.
  42. * Returns empty string when not able to find any ISO code.
  43. *
  44. * @param string $ip_address User IP address.
  45. * @return string
  46. * @deprecated 3.9.0
  47. */
  48. public function get_country_iso( $ip_address ) {
  49. wc_deprecated_function( 'get_country_iso', '3.9.0' );
  50. $iso_code = '';
  51. try {
  52. $reader = new MaxMind\Db\Reader( $this->database ); // phpcs:ignore PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
  53. $data = $reader->get( $ip_address );
  54. if ( isset( $data['country']['iso_code'] ) ) {
  55. $iso_code = $data['country']['iso_code'];
  56. }
  57. $reader->close();
  58. } catch ( Exception $e ) {
  59. $this->log( $e->getMessage(), 'warning' );
  60. }
  61. return sanitize_text_field( strtoupper( $iso_code ) );
  62. }
  63. /**
  64. * Logging method.
  65. *
  66. * @param string $message Log message.
  67. * @param string $level Log level.
  68. * Available options: 'emergency', 'alert',
  69. * 'critical', 'error', 'warning', 'notice',
  70. * 'info' and 'debug'.
  71. * Defaults to 'info'.
  72. */
  73. private function log( $message, $level = 'info' ) {
  74. if ( is_null( $this->log ) ) {
  75. $this->log = wc_get_logger();
  76. }
  77. $this->log->log( $level, $message, array( 'source' => 'geoip' ) );
  78. }
  79. }