comment_author_IP : ''; } $ip = trim((string) $ip); if ($ip === '' || !class_exists('\AKSRT\Extensions\IPGeo')) { return; } $geo = \AKSRT\Extensions\IPGeo::get_instance()->lookup($ip); if (!is_array($geo)) { 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; } 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); return is_array($raw) ? $raw : []; } endif; if (!function_exists('aksrt_format_comment_city_label')) : /** * Format addr array into display string. * * @param array $addr Addr. * @param string $type Format. */ function aksrt_format_comment_city_label(array $addr, string $type = 'province_city'): string { $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 ''; } $addr = aksrt_get_comment_addr($comment); $label = aksrt_format_comment_city_label($addr, aksrt_comment_city_type()); if ($label === '') { return ''; } return '' . esc_html($label) . ''; } 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);