feat(ip-geo): full-chain IP 归属地 on comments, user home, center, post

This commit is contained in:
kate522 committed 2026-07-25 14:39:47 +08:00
1 parent 1f2ca3ac8f
commit ac7ff8d4f2
13 files changed
+457 -33

No files matched your search

+43 -12
View File
@@ -2,7 +2,8 @@
/**
* Comment city / IP geo helpers.
*
* Meta key: aksrt_comment_addr (array|string from IPGeo).
* Meta key: aksrt_comment_addr (array from IPGeo).
* Gates: extensions.ip_geo_enabled + ip_geo_display includes comment + comment_city_s.
*
* @package AKSRT
*/
@@ -14,6 +15,21 @@ if (!function_exists('aksrt_comment_city_enabled')) :
* Whether comment city badges are enabled.
*/
function aksrt_comment_city_enabled(): bool {
// Prefer shared display gate when available.
if (function_exists('aksrt_ip_geo_display_enabled')) {
if (!aksrt_ip_geo_display_enabled('comment')) {
// Allow legacy path: comment_city_s alone still works if master is on
// but display list empty — require master + city_s at minimum.
if (!function_exists('aksrt_ip_geo_master_enabled') || !aksrt_ip_geo_master_enabled()) {
return false;
}
// Master on but location not selected → off.
return false;
}
} elseif (function_exists('aksrt_ip_geo_master_enabled') && !aksrt_ip_geo_master_enabled()) {
return false;
}
if (!class_exists('\AKSRT\Settings\Settings')) {
return false;
}
@@ -28,7 +44,7 @@ if (!function_exists('aksrt_comment_city_type')) :
*/
function aksrt_comment_city_type(): string {
if (!class_exists('\AKSRT\Settings\Settings')) {
return 'province_city';
return function_exists('aksrt_ip_geo_format_key') ? aksrt_ip_geo_format_key() : 'province_city';
}
$s = \AKSRT\Settings\Settings::get_comment_display_settings();
$type = sanitize_key((string) ($s['city_type'] ?? 'province_city'));
@@ -54,19 +70,21 @@ if (!function_exists('aksrt_store_comment_addr')) :
$ip = $comment instanceof WP_Comment ? (string) $comment->comment_author_IP : '';
}
$ip = trim((string) $ip);
if ($ip === '' || !class_exists('\AKSRT\Extensions\IPGeo')) {
if ($ip === '') {
return;
}
$geo = \AKSRT\Extensions\IPGeo::get_instance()->lookup($ip);
if (!is_array($geo)) {
if (function_exists('aksrt_lookup_geo_for_ip')) {
$addr = aksrt_lookup_geo_for_ip($ip);
} elseif (class_exists('\AKSRT\Extensions\IPGeo')) {
$geo = \AKSRT\Extensions\IPGeo::get_instance()->lookup($ip);
$addr = [
'country' => (string) ($geo['country'] ?? ''),
'province' => (string) ($geo['province'] ?? ''),
'city' => (string) ($geo['city'] ?? ''),
];
} else {
return;
}
// Store compact array.
$addr = [
'country' => (string) ($geo['country'] ?? ''),
'province' => (string) ($geo['province'] ?? ''),
'city' => (string) ($geo['city'] ?? ''),
];
if ($addr['country'] === '' && $addr['province'] === '' && $addr['city'] === '') {
return;
}
@@ -85,6 +103,9 @@ if (!function_exists('aksrt_get_comment_addr')) :
return [];
}
$raw = get_comment_meta($comment_id, 'aksrt_comment_addr', true);
if (function_exists('aksrt_normalize_geo_addr')) {
return aksrt_normalize_geo_addr($raw);
}
return is_array($raw) ? $raw : [];
}
endif;
@@ -97,6 +118,9 @@ if (!function_exists('aksrt_format_comment_city_label')) :
* @param string $type Format.
*/
function aksrt_format_comment_city_label(array $addr, string $type = 'province_city'): string {
if (function_exists('aksrt_format_geo_label')) {
return aksrt_format_geo_label($addr, $type);
}
$country = trim((string) ($addr['country'] ?? ''));
$province = trim((string) ($addr['province'] ?? ''));
$city = trim((string) ($addr['city'] ?? ''));
@@ -125,7 +149,14 @@ if (!function_exists('aksrt_get_comment_city_badge_html')) :
if (!aksrt_comment_city_enabled()) {
return '';
}
$addr = aksrt_get_comment_addr($comment);
if (function_exists('aksrt_get_geo_badge_html')) {
return aksrt_get_geo_badge_html(
aksrt_get_comment_addr($comment),
aksrt_comment_city_type(),
'badg badg-sm comment-city-badge'
);
}
$addr = aksrt_get_comment_addr($comment);
$label = aksrt_format_comment_city_label($addr, aksrt_comment_city_type());
if ($label === '') {
return '';