- Rewrite post_list/comment schema (PHP + JS) with the reference theme's field set, order, defaults and dependencies; smilie, comment location and quick-reply fields excluded - Map PostDisplay::get_comment_display_settings() to the new keys without legacy fallback: close_comments, comment_paginate_type (ajax_lists), user_edit_comment master switch, WP comment_order for sorting - Wire comment_box_hide visibility gate (signin / vip / vip_2 / auth) - Drop dead comment settings (order/per_page/depth/avatar/hot threshold/ empty text/city), the dead post_close_comments field, and city readers now fully owned by the IP-geo extension - Add comment settings reference contract test
130 lines
4.3 KiB
PHP
130 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Comment city / IP geo helpers.
|
|
*
|
|
* Meta: aksrt_comment_addr {country,province,city}
|
|
* Gates: ip_geo_enabled + ip_geo_display has comment(扩展&增强 / IP归属地统一控制)
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!function_exists('aksrt_comment_city_enabled')) :
|
|
function aksrt_comment_city_enabled(): bool {
|
|
// 评论区位置显示的开关与精度统一由「扩展&增强 / IP归属地」配置提供。
|
|
return function_exists('aksrt_ip_geo_display_enabled') && aksrt_ip_geo_display_enabled('comment');
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_comment_city_type')) :
|
|
function aksrt_comment_city_type(): string {
|
|
return function_exists('aksrt_ip_geo_format_key') ? aksrt_ip_geo_format_key() : 'province_city';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_store_comment_addr')) :
|
|
/**
|
|
* @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);
|
|
|
|
// Private / empty stored IP (CDN edge without proxy headers): resolve live client IP once.
|
|
$needs_live = ($ip === '' || $ip === '0.0.0.0');
|
|
if (!$needs_live && function_exists('filter_var')) {
|
|
$needs_live = !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
|
|
}
|
|
if ($needs_live && function_exists('aksrt_get_client_ip')) {
|
|
$live = trim((string) aksrt_get_client_ip());
|
|
if ($live !== '' && $live !== '0.0.0.0'
|
|
&& filter_var($live, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)
|
|
) {
|
|
$ip = $live;
|
|
}
|
|
}
|
|
|
|
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 ['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 : ['country' => '', 'province' => '', 'city' => ''];
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_format_comment_city_label')) :
|
|
/**
|
|
* @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);
|
|
}
|
|
return '';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('aksrt_get_comment_city_badge_html')) :
|
|
function aksrt_get_comment_city_badge_html($comment): string {
|
|
if (!aksrt_comment_city_enabled()) {
|
|
return '';
|
|
}
|
|
// legacy 契约:无属地数据时不渲染徽章(不使用查询失败兜底文案)。
|
|
$addr = aksrt_get_comment_addr($comment);
|
|
$label = function_exists('aksrt_format_geo_label')
|
|
? aksrt_format_geo_label(is_array($addr) ? $addr : [], aksrt_comment_city_type())
|
|
: '';
|
|
if ($label === '') {
|
|
return '';
|
|
}
|
|
return '<span class="badg badg-sm comment-city-badge aksrt-ip-geo-badge" title="' . esc_attr__('IP 属地', 'aksrt') . '">' . esc_html($label) . '</span>';
|
|
}
|
|
endif;
|
|
|
|
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);
|