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 ['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 ''; } 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' ); } return ''; } 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);