feat(comments): full 1:1 zibll comment kernel for posts, forum, shop
Extract shared comment helpers (content/item/form/geo/actions), port GIF [g=] smilies + FixedInput/moveForm UX, wire close_comments/city/quick settings, REST comment get, and align product review chrome with zibll.
This commit is contained in:
1 parent
9edd39c33d
commit
276a2891ba
116 files changed
+2658
-1848
No files matched your search
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
/**
|
||||
* Comment city / IP geo helpers.
|
||||
*
|
||||
* Meta key: aksrt_comment_addr (array|string from IPGeo).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_comment_city_enabled')) :
|
||||
/**
|
||||
* Whether comment city badges are enabled.
|
||||
*/
|
||||
function aksrt_comment_city_enabled(): bool {
|
||||
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 '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 === '' || !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<string,string> $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 '<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);
|
||||
Reference in new issue
Block a user