✏️ 编辑:class-geoip.php
路径:
/home/forge/kingkrunch.com/wp/wp-content/plugins/wp-defender/src/extra/class-geoip.php
大小:1.41 KB · 修改:2025-10-22 00:15:46 · 权限:0644 · 可写
← 返回目录
👁 查看
⬇ 下载
<?php /** * GeoIP functionality for WP Defender. * * @package WP_Defender\Extra */ namespace WP_Defender\Extra; use MaxMind\Db\Reader; /** * GeoIP class for IP geolocation functionality. */ class GeoIp { /** * MaxMind database reader instance. * * @var Reader */ protected $provider; /** * Constructor. * * @param string $db_path Path to the MaxMind database file. */ public function __construct( $db_path ) { $this->provider = new Reader( $db_path ); } /** * Convert IP address to country information. * * @param string $ip IP address to lookup. * * @return array<string, string>|false Country data array with 'iso' and 'name' keys, or false on failure. * @throws Reader\InvalidDatabaseException When database format is invalid. */ public function ip_to_country( $ip ) { $info = $this->provider->get( $ip ); // Try country first, then fall back to registered_country. $country_data = null; if ( isset( $info['country'] ) && is_array( $info['country'] ) ) { $country_data = $info['country']; } elseif ( isset( $info['registered_country'] ) && is_array( $info['registered_country'] ) ) { $country_data = $info['registered_country']; } if ( null === $country_data ) { return false; } return array( 'iso' => $country_data['iso_code'] ?? '', 'name' => $country_data['names']['en'] ?? '', ); } }
💾 保存
取消
保存为 UTF-8,换行统一为 LF