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 $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 '' . esc_html($label) . ''; } 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);