Files
wordpress-theme-aksrt/inc/helpers/comments/geo.php
T

174 lines
5.4 KiB
PHP

<?php
/**
* 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.
*
* @package AKSRT
*/
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()) {
return false;
}
if (!class_exists('\AKSRT\Settings\Settings')) {
return false;
}
$s = \AKSRT\Settings\Settings::get_comment_display_settings();
return !empty($s['city_s']);
}
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';
}
$s = \AKSRT\Settings\Settings::get_comment_display_settings();
$type = sanitize_key((string) ($s['city_type'] ?? 'province_city'));
return in_array($type, ['province_city', 'province', 'country_province'], true)
? $type
: 'province_city';
}
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.
*/
function aksrt_store_comment_addr(int $comment_id, ?string $ip = null): void {
if ($comment_id <= 0 || !aksrt_comment_city_enabled()) {
return;
}
if ($ip === null || $ip === '') {
$comment = get_comment($comment_id);
$ip = $comment instanceof WP_Comment ? (string) $comment->comment_author_IP : '';
}
$ip = trim((string) $ip);
if ($ip === '') {
return;
}
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;
}
if ($addr['country'] === '' && $addr['province'] === '' && $addr['city'] === '') {
return;
}
update_comment_meta($comment_id, 'aksrt_comment_addr', $addr);
}
endif;
if (!function_exists('aksrt_get_comment_addr')) :
/**
* @param int|WP_Comment $comment Comment.
* @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 [];
}
$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;
if (!function_exists('aksrt_format_comment_city_label')) :
/**
* Format addr array into display string.
*
* @param array<string,string> $addr Addr.
* @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'] ?? ''));
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);
}
}
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 '';
}
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 '';
}
return '<span class="badg badg-sm comment-city-badge" title="' . esc_attr__('IP 属地', 'aksrt') . '">' . esc_html($label) . '</span>';
}
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);
}
}, 20, 1);