Full-project rename. `git grep -i aksrt` is now empty apart from the
DBMigrator 3.3.0 changelog note that documents the rename itself.
Code identifiers
- PHP namespace AKSRT\ -> Jio\ (composer PSR-4 + 378 declarations)
- global constants AKSRT_* -> JIO_* (VERSION/DIR/URI, PAY_TYPE_*, CAPTCHA_*)
- procedural functions, hooks, options, meta keys, table names,
cookies, query vars, ajax actions and nonces: aksrt_ -> jio_
- text domain aksrt -> jio; Theme Name -> Jio
- JS globals: aksrtData/aksrtAdminData/... -> jioData/jioAdminData/...,
window.AKSRTCaptcha & friends -> window.JioCaptcha, Aksrt* -> Jio*
- Vue components Aks*.vue -> Jio*.vue (15 files renamed + imports)
- CSS classes/data-attrs/custom properties: aksrt- -> jio-
- REST namespace aksrt/v1 -> jio/v1 (incl. payment gateway callbacks)
Storage layer
- DB tables {prefix}aksrt_* -> {prefix}jio_*; SCHEMA_VERSION 3.2.0 -> 3.3.0
so dbDelta creates the new names on install
- widget id_base aksrt_widget_* -> jio_widget_*
- uninstall.php drop list rebuilt from the actual migrator tables
(dropped 3 stale entries, added the 17 missing ones)
Removed
- hardcoded https://api.aksrt.com/version.json default update endpoint;
get_endpoint() now reads the 「更新端点」 setting and do_check() no-ops
when it is empty
Files renamed: inc/helpers/aksrt-*.php -> jio-*.php (54),
assets/admin/src/components/form/Aks*.vue -> Jio*.vue (15).
Build artifacts regenerated: assets/css/build, assets/js/runtime,
assets/admin/build.
Verified: audit-psr4-paths errors=0, audit-admin-contracts errors=0
(warnings=56, unchanged baseline), 813/813 widget-system tests,
44/44 phpunit, php -l clean across all PHP.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
300 lines
8.8 KiB
PHP
300 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* IP 归属地 — storage, format, and display (no dual-read / no legacy keys).
|
|
*
|
|
* Gates:
|
|
* master: ip_geo_enabled
|
|
* surface: ip_geo_display ∈ {comment, user_home, user_center, post}
|
|
* comments: display + format fully owned by this extension via comments/geo.php
|
|
*
|
|
* Meta:
|
|
* comment → jio_comment_addr {country,province,city}
|
|
* user → jio_user_addr, jio_register_ip
|
|
*
|
|
* @package Jio
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!function_exists('jio_get_ip_geo_config')) :
|
|
/**
|
|
* @return array<string,mixed>
|
|
*/
|
|
function jio_get_ip_geo_config(): array {
|
|
if (class_exists('\Jio\Settings\Settings') && method_exists('\Jio\Settings\Settings', 'get_ip_geo_settings')) {
|
|
return \Jio\Settings\Settings::get_ip_geo_settings();
|
|
}
|
|
return [
|
|
'ip_geo_enabled' => false,
|
|
'ip_geo_display' => ['comment', 'user_home', 'user_center', 'post'],
|
|
'ip_geo_format' => 'province_city',
|
|
'ip_geo_fallback_text' => '',
|
|
];
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_ip_geo_master_enabled')) :
|
|
function jio_ip_geo_master_enabled(): bool {
|
|
return !empty(jio_get_ip_geo_config()['ip_geo_enabled']);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_ip_geo_display_list')) :
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
function jio_ip_geo_display_list(): array {
|
|
$c = jio_get_ip_geo_config();
|
|
$display = $c['ip_geo_display'] ?? [];
|
|
if (!is_array($display)) {
|
|
$display = [];
|
|
}
|
|
$allowed = ['comment', 'user_home', 'user_center', 'post'];
|
|
$out = [];
|
|
foreach ($display as $item) {
|
|
$key = sanitize_key((string) $item);
|
|
if (in_array($key, $allowed, true) && !in_array($key, $out, true)) {
|
|
$out[] = $key;
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_ip_geo_display_enabled')) :
|
|
/**
|
|
* @param string $location comment|user_home|user_center|post
|
|
*/
|
|
function jio_ip_geo_display_enabled(string $location): bool {
|
|
if (!jio_ip_geo_master_enabled()) {
|
|
return false;
|
|
}
|
|
$location = sanitize_key($location);
|
|
if (!in_array($location, jio_ip_geo_display_list(), true)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_ip_geo_format_key')) :
|
|
function jio_ip_geo_format_key(): string {
|
|
$format = sanitize_key((string) (jio_get_ip_geo_config()['ip_geo_format'] ?? 'province_city'));
|
|
return in_array($format, ['province_city', 'province', 'country_province'], true)
|
|
? $format
|
|
: 'province_city';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_normalize_geo_addr')) :
|
|
/**
|
|
* @param mixed $raw Meta value.
|
|
* @return array{country:string,province:string,city:string}
|
|
*/
|
|
function jio_normalize_geo_addr($raw): array {
|
|
if (!is_array($raw)) {
|
|
return ['country' => '', 'province' => '', 'city' => ''];
|
|
}
|
|
return [
|
|
'country' => trim((string) ($raw['country'] ?? '')),
|
|
'province' => trim((string) ($raw['province'] ?? '')),
|
|
'city' => trim((string) ($raw['city'] ?? '')),
|
|
];
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_format_geo_label')) :
|
|
/**
|
|
* @param array<string,string> $addr Addr.
|
|
* @param string $type Format key.
|
|
*/
|
|
function jio_format_geo_label(array $addr, string $type = 'province_city'): string {
|
|
$addr = jio_normalize_geo_addr($addr);
|
|
if (class_exists('\Jio\Extensions\IPGeo')) {
|
|
$label = \Jio\Extensions\IPGeo::get_instance()->format($addr, $type);
|
|
if (is_string($label) && $label !== '') {
|
|
return $label;
|
|
}
|
|
}
|
|
$country = $addr['country'];
|
|
$province = $addr['province'];
|
|
$city = $addr['city'];
|
|
|
|
// Drop duplicated province+city when provider returns the same token twice.
|
|
if ($province !== '' && $city !== '' && $province === $city) {
|
|
$city = '';
|
|
}
|
|
|
|
switch ($type) {
|
|
case 'province':
|
|
if ($province !== '') {
|
|
return $province;
|
|
}
|
|
return $city !== '' ? $city : $country;
|
|
case 'country_province':
|
|
$joined = trim($country . ' ' . $province);
|
|
if ($joined !== '') {
|
|
return $joined;
|
|
}
|
|
return $city;
|
|
case 'province_city':
|
|
default:
|
|
$parts = array_filter([$province, $city]);
|
|
if (!empty($parts)) {
|
|
return implode(' ', $parts);
|
|
}
|
|
return $country;
|
|
}
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_get_geo_badge_html')) :
|
|
/**
|
|
* @param array<string,string>|mixed $addr Addr meta.
|
|
* @param string $type Format.
|
|
* @param string $class CSS classes.
|
|
* @param string $title Title attr.
|
|
*/
|
|
function jio_get_geo_badge_html($addr, string $type = 'province_city', string $class = 'badg badg-sm', string $title = ''): string {
|
|
$label = jio_format_geo_label(is_array($addr) ? $addr : [], $type);
|
|
if ($label === '') {
|
|
$fallback = (string) (jio_get_ip_geo_config()['ip_geo_fallback_text'] ?? '');
|
|
if ($fallback === '') {
|
|
return '';
|
|
}
|
|
$label = $fallback;
|
|
}
|
|
if ($title === '') {
|
|
$title = __('IP 属地', 'jio');
|
|
}
|
|
$class = trim($class . ' jio-ip-geo-badge');
|
|
return '<span class="' . esc_attr($class) . '" title="' . esc_attr($title) . '">' . esc_html($label) . '</span>';
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_lookup_geo_for_ip')) :
|
|
/**
|
|
* @return array{country:string,province:string,city:string}
|
|
*/
|
|
function jio_lookup_geo_for_ip(string $ip): array {
|
|
$ip = trim($ip);
|
|
if ($ip === '' || !class_exists('\Jio\Extensions\IPGeo')) {
|
|
return ['country' => '', 'province' => '', 'city' => ''];
|
|
}
|
|
$geo = \Jio\Extensions\IPGeo::get_instance()->lookup($ip);
|
|
return jio_normalize_geo_addr(is_array($geo) ? $geo : []);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_store_user_addr')) :
|
|
/**
|
|
* Resolve IP and write jio_user_addr (login / register / own-page refresh).
|
|
*/
|
|
function jio_store_user_addr(int $user_id, ?string $ip = null): void {
|
|
if ($user_id <= 0 || !jio_ip_geo_master_enabled()) {
|
|
return;
|
|
}
|
|
$need = in_array('post', jio_ip_geo_display_list(), true)
|
|
|| in_array('user_home', jio_ip_geo_display_list(), true)
|
|
|| in_array('user_center', jio_ip_geo_display_list(), true);
|
|
if (!$need) {
|
|
return;
|
|
}
|
|
|
|
if ($ip === null || $ip === '') {
|
|
$ip = function_exists('jio_get_client_ip') ? (string) jio_get_client_ip() : (string) ($_SERVER['REMOTE_ADDR'] ?? '');
|
|
}
|
|
$ip = trim($ip);
|
|
if ($ip === '') {
|
|
return;
|
|
}
|
|
|
|
$addr = jio_lookup_geo_for_ip($ip);
|
|
if ($addr['country'] === '' && $addr['province'] === '' && $addr['city'] === '') {
|
|
return;
|
|
}
|
|
update_user_meta($user_id, 'jio_user_addr', $addr);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_get_user_addr')) :
|
|
/**
|
|
* Read jio_user_addr; if empty and viewing self, resolve current client IP once.
|
|
*
|
|
* @return array{country:string,province:string,city:string}
|
|
*/
|
|
function jio_get_user_addr(int $user_id): array {
|
|
if ($user_id <= 0) {
|
|
return ['country' => '', 'province' => '', 'city' => ''];
|
|
}
|
|
$addr = jio_normalize_geo_addr(get_user_meta($user_id, 'jio_user_addr', true));
|
|
if ($addr['country'] !== '' || $addr['province'] !== '' || $addr['city'] !== '') {
|
|
return $addr;
|
|
}
|
|
// Self view: write-on-read from current IP (same outcome as next login).
|
|
if ((int) get_current_user_id() === $user_id && jio_ip_geo_master_enabled()) {
|
|
jio_store_user_addr($user_id);
|
|
return jio_normalize_geo_addr(get_user_meta($user_id, 'jio_user_addr', true));
|
|
}
|
|
return $addr;
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_get_user_city_badge_html')) :
|
|
/**
|
|
* @param int $user_id User.
|
|
* @param string $location user_home|user_center
|
|
* @param string $class CSS.
|
|
*/
|
|
function jio_get_user_city_badge_html(int $user_id, string $location = 'user_home', string $class = 'badg'): string {
|
|
if ($user_id <= 0 || !jio_ip_geo_display_enabled($location)) {
|
|
return '';
|
|
}
|
|
return jio_get_geo_badge_html(jio_get_user_addr($user_id), jio_ip_geo_format_key(), $class);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_get_post_author_city_badge_html')) :
|
|
/**
|
|
* Article author IP 属地 (author jio_user_addr).
|
|
*/
|
|
function jio_get_post_author_city_badge_html(int $author_id = 0, string $class = 'badg badg-sm'): string {
|
|
if (!jio_ip_geo_display_enabled('post')) {
|
|
return '';
|
|
}
|
|
if ($author_id <= 0) {
|
|
$author_id = (int) get_the_author_meta('ID');
|
|
}
|
|
if ($author_id <= 0) {
|
|
return '';
|
|
}
|
|
return jio_get_geo_badge_html(jio_get_user_addr($author_id), jio_ip_geo_format_key(), $class);
|
|
}
|
|
endif;
|
|
|
|
add_action('wp_login', static function ($user_login, $user = null): void {
|
|
$user_id = 0;
|
|
if ($user instanceof WP_User) {
|
|
$user_id = (int) $user->ID;
|
|
} elseif (is_string($user_login) && $user_login !== '') {
|
|
$u = get_user_by('login', $user_login);
|
|
$user_id = $u instanceof WP_User ? (int) $u->ID : 0;
|
|
}
|
|
if ($user_id > 0) {
|
|
jio_store_user_addr($user_id);
|
|
}
|
|
}, 20, 2);
|
|
|
|
add_action('user_register', static function ($user_id): void {
|
|
$user_id = (int) $user_id;
|
|
if ($user_id <= 0) {
|
|
return;
|
|
}
|
|
$ip = function_exists('jio_get_client_ip') ? (string) jio_get_client_ip() : (string) ($_SERVER['REMOTE_ADDR'] ?? '');
|
|
$ip = trim($ip);
|
|
if ($ip !== '') {
|
|
update_user_meta($user_id, 'jio_register_ip', $ip);
|
|
}
|
|
jio_store_user_addr($user_id, $ip !== '' ? $ip : null);
|
|
}, 20, 1);
|