refactor(ip-geo): drop dual-read gates; keep single meta and four surfaces
Canonical keys only (ip_geo_display defaults to all four, aksrt_user_addr / aksrt_comment_addr only). Self-view still resolves current IP on read.
This commit is contained in:
1 parent
6e71008d00
commit
3bb7fb0ba6
4 files changed
+87
-177
No files matched your search
@@ -2,8 +2,8 @@
|
||||
/**
|
||||
* Comment city / IP geo helpers.
|
||||
*
|
||||
* Meta key: aksrt_comment_addr (array from IPGeo).
|
||||
* Gates: extensions.ip_geo_enabled + ip_geo_display includes comment + comment_city_s.
|
||||
* Meta: aksrt_comment_addr {country,province,city}
|
||||
* Gates: ip_geo_enabled + ip_geo_display has comment + comment_city_s
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -11,25 +11,10 @@
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
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()) {
|
||||
if (function_exists('aksrt_ip_geo_display_enabled') && !aksrt_ip_geo_display_enabled('comment')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!class_exists('\AKSRT\Settings\Settings')) {
|
||||
return false;
|
||||
}
|
||||
@@ -39,9 +24,6 @@ if (!function_exists('aksrt_comment_city_enabled')) :
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_comment_city_type')) :
|
||||
/**
|
||||
* Display format key for city badge.
|
||||
*/
|
||||
function aksrt_comment_city_type(): string {
|
||||
if (!class_exists('\AKSRT\Settings\Settings')) {
|
||||
return function_exists('aksrt_ip_geo_format_key') ? aksrt_ip_geo_format_key() : 'province_city';
|
||||
@@ -56,8 +38,6 @@ endif;
|
||||
|
||||
if (!function_exists('aksrt_store_comment_addr')) :
|
||||
/**
|
||||
* Resolve IP geo and persist on comment.
|
||||
*
|
||||
* @param int $comment_id Comment ID.
|
||||
* @param string|null $ip Optional IP override.
|
||||
*/
|
||||
@@ -95,25 +75,23 @@ endif;
|
||||
if (!function_exists('aksrt_get_comment_addr')) :
|
||||
/**
|
||||
* @param int|WP_Comment $comment Comment.
|
||||
* @return array{country?:string,province?:string,city?:string}
|
||||
* @return array{country:string,province:string,city:string}
|
||||
*/
|
||||
function aksrt_get_comment_addr($comment): array {
|
||||
$comment_id = $comment instanceof WP_Comment ? (int) $comment->comment_ID : (int) $comment;
|
||||
if ($comment_id <= 0) {
|
||||
return [];
|
||||
return ['country' => '', 'province' => '', 'city' => ''];
|
||||
}
|
||||
$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 : [];
|
||||
return is_array($raw) ? $raw : ['country' => '', 'province' => '', 'city' => ''];
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_format_comment_city_label')) :
|
||||
/**
|
||||
* Format addr array into display string.
|
||||
*
|
||||
* @param array<string,string> $addr Addr.
|
||||
* @param string $type Format.
|
||||
*/
|
||||
@@ -121,30 +99,11 @@ if (!function_exists('aksrt_format_comment_city_label')) :
|
||||
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'] ?? ''));
|
||||
switch ($type) {
|
||||
case 'province':
|
||||
return $province !== '' ? $province : $city;
|
||||
case 'country_province':
|
||||
$parts = array_filter([$country, $province]);
|
||||
return implode(' ', $parts);
|
||||
case 'province_city':
|
||||
default:
|
||||
$parts = array_filter([$province, $city]);
|
||||
if (empty($parts) && $country !== '') {
|
||||
return $country;
|
||||
}
|
||||
return implode(' ', $parts);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_comment_city_badge_html')) :
|
||||
/**
|
||||
* HTML badge for comment city (empty string if disabled/missing).
|
||||
*/
|
||||
function aksrt_get_comment_city_badge_html($comment): string {
|
||||
if (!aksrt_comment_city_enabled()) {
|
||||
return '';
|
||||
@@ -156,16 +115,10 @@ if (!function_exists('aksrt_get_comment_city_badge_html')) :
|
||||
'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 '';
|
||||
}
|
||||
return '<span class="badg badg-sm comment-city-badge" title="' . esc_attr__('IP 属地', 'aksrt') . '">' . esc_html($label) . '</span>';
|
||||
return '';
|
||||
}
|
||||
endif;
|
||||
|
||||
// Persist geo after any successful insert (covers REST + core).
|
||||
add_action('comment_post', static function ($comment_id): void {
|
||||
if (function_exists('aksrt_store_comment_addr')) {
|
||||
aksrt_store_comment_addr((int) $comment_id);
|
||||
|
||||
Reference in new issue
Block a user