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>
219 lines
7.0 KiB
PHP
219 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Comment action permissions + like/pin primitives.
|
|
*
|
|
* Extracted from jio-frontend-tags.php for size budget.
|
|
*
|
|
* @package Jio
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
if (!function_exists('jio_comment_action_permissions')) :
|
|
/**
|
|
* Single source of truth for per-comment action permissions.
|
|
*
|
|
* @param WP_Comment|int $comment Comment object or ID.
|
|
* @return array{can_edit:bool,can_delete:bool,can_moderate:bool,can_pin:bool}
|
|
*/
|
|
function jio_comment_action_permissions($comment): array {
|
|
$comment = $comment instanceof WP_Comment ? $comment : get_comment($comment);
|
|
$none = ['can_edit' => false, 'can_delete' => false, 'can_moderate' => false, 'can_pin' => false];
|
|
if (!($comment instanceof WP_Comment) || !is_user_logged_in()) {
|
|
return $none;
|
|
}
|
|
$uid = get_current_user_id();
|
|
$is_wp_mod = current_user_can('moderate_comments') || current_user_can('manage_options');
|
|
$is_owner = (int) $comment->user_id > 0 && (int) $comment->user_id === $uid;
|
|
$post_id = (int) $comment->comment_post_ID;
|
|
$post = $post_id > 0 ? get_post($post_id) : null;
|
|
$is_post_author = $post instanceof WP_Post && (int) $post->post_author === $uid;
|
|
$ctx = $post_id > 0 ? $post_id : null;
|
|
|
|
$can = static function (string $cap) use ($uid, $ctx): bool {
|
|
if (function_exists('jio_user_can')) {
|
|
return jio_user_can($cap, $uid, $ctx);
|
|
}
|
|
if (class_exists('\Jio\Settings\Capabilities')) {
|
|
return \Jio\Settings\Capabilities::user_can($cap, $uid, $ctx);
|
|
}
|
|
return false;
|
|
};
|
|
|
|
// 允许用户编辑评论:参考主题 user_edit_comment 总开关(关闭后本人亦不可编辑)。
|
|
$user_edit = true;
|
|
if (class_exists('\Jio\Settings\Settings')) {
|
|
$display = \Jio\Settings\Settings::get_comment_display_settings();
|
|
$user_edit = !isset($display['user_edit']) || !empty($display['user_edit']);
|
|
}
|
|
|
|
$can_edit_own = $is_owner && $user_edit && $can('comment_edit');
|
|
$can_edit_others = (!$is_owner) && (
|
|
($is_post_author && $can('comment_edit_my_post'))
|
|
|| $can('comment_edit_other')
|
|
);
|
|
$can_delete_own = $is_owner && $can('comment_delete');
|
|
$can_delete_others = (!$is_owner) && (
|
|
($is_post_author && $can('comment_delete_my_post'))
|
|
|| $can('comment_delete_other')
|
|
);
|
|
$can_pin = (int) $comment->comment_parent === 0 && (
|
|
($is_post_author && $can('comment_set_topping_my_post'))
|
|
|| $can('comment_set_topping_other')
|
|
);
|
|
|
|
return [
|
|
'can_edit' => $is_wp_mod || $can_edit_own || $can_edit_others,
|
|
'can_delete' => $is_wp_mod || $can_delete_own || $can_delete_others,
|
|
'can_moderate' => $is_wp_mod || $can('comment_audit_other') || ($is_post_author && $can('comment_audit_my_post')),
|
|
'can_pin' => $is_wp_mod || $can_pin,
|
|
];
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_is_comment_pinned')) :
|
|
/**
|
|
* @param int|WP_Comment $comment Comment.
|
|
*/
|
|
function jio_is_comment_pinned($comment): bool {
|
|
$comment_id = $comment instanceof WP_Comment ? (int) $comment->comment_ID : (int) $comment;
|
|
if ($comment_id <= 0) {
|
|
return false;
|
|
}
|
|
return '1' === (string) get_comment_meta($comment_id, '_jio_comment_pinned', true);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_update_comment_counter')) :
|
|
/**
|
|
* Atomically bump a numeric comment-meta counter, floored at zero.
|
|
*
|
|
* @param int $comment_id Comment ID.
|
|
* @param string $meta_key Meta key.
|
|
* @param int $delta Signed delta.
|
|
* @return int Latest value (>= 0).
|
|
*/
|
|
function jio_update_comment_counter($comment_id, $meta_key, $delta) {
|
|
global $wpdb;
|
|
$comment_id = (int) $comment_id;
|
|
$delta = (int) $delta;
|
|
$exists = (int) $wpdb->get_var($wpdb->prepare(
|
|
"SELECT 1 FROM {$wpdb->commentmeta} WHERE comment_id = %d AND meta_key = %s LIMIT 1",
|
|
$comment_id,
|
|
$meta_key
|
|
));
|
|
if ($exists) {
|
|
$wpdb->query($wpdb->prepare(
|
|
"UPDATE {$wpdb->commentmeta}
|
|
SET meta_value = GREATEST(0, CAST(meta_value AS SIGNED) + %d)
|
|
WHERE comment_id = %d AND meta_key = %s",
|
|
$delta,
|
|
$comment_id,
|
|
$meta_key
|
|
));
|
|
} else {
|
|
add_comment_meta($comment_id, $meta_key, max(0, $delta), true);
|
|
}
|
|
wp_cache_delete($comment_id, 'comment_meta');
|
|
return (int) get_comment_meta($comment_id, $meta_key, true);
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_get_comment_likes')) :
|
|
/**
|
|
* @param int|WP_Comment $comment Comment.
|
|
*/
|
|
function jio_get_comment_likes($comment): int {
|
|
$comment_id = $comment instanceof WP_Comment ? (int) $comment->comment_ID : (int) $comment;
|
|
if ($comment_id <= 0) {
|
|
return 0;
|
|
}
|
|
return max(0, (int) get_comment_meta($comment_id, 'jio_comment_likes', true));
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_is_comment_liked')) :
|
|
/**
|
|
* @param int $comment_id Comment ID.
|
|
*/
|
|
function jio_is_comment_liked($comment_id): bool {
|
|
$comment_id = (int) $comment_id;
|
|
if ($comment_id <= 0) {
|
|
return false;
|
|
}
|
|
if (is_user_logged_in()) {
|
|
$liked = get_user_meta(get_current_user_id(), '_jio_liked_comments', true);
|
|
$liked = is_array($liked) ? array_map('intval', $liked) : [];
|
|
return in_array($comment_id, $liked, true);
|
|
}
|
|
$cookie = 'jio_clike_' . $comment_id;
|
|
return !empty($_COOKIE[$cookie]) && '1' === $_COOKIE[$cookie];
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_set_comment_like_state')) :
|
|
/**
|
|
* @param int $comment_id Comment ID.
|
|
* @param bool $active Target state.
|
|
*/
|
|
function jio_set_comment_like_state($comment_id, $active): void {
|
|
$comment_id = (int) $comment_id;
|
|
if ($comment_id <= 0) {
|
|
return;
|
|
}
|
|
if (is_user_logged_in()) {
|
|
$user_id = get_current_user_id();
|
|
$liked = get_user_meta($user_id, '_jio_liked_comments', true);
|
|
$liked = is_array($liked) ? array_map('intval', $liked) : [];
|
|
if ($active) {
|
|
$liked[] = $comment_id;
|
|
} else {
|
|
$liked = array_filter($liked, static function ($id) use ($comment_id) {
|
|
return (int) $id !== $comment_id;
|
|
});
|
|
}
|
|
update_user_meta($user_id, '_jio_liked_comments', array_values(array_unique($liked)));
|
|
return;
|
|
}
|
|
$cookie = 'jio_clike_' . $comment_id;
|
|
$path = defined('COOKIEPATH') && COOKIEPATH ? COOKIEPATH : '/';
|
|
$domain = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '';
|
|
setcookie($cookie, $active ? '1' : '', $active ? time() + YEAR_IN_SECONDS : time() - HOUR_IN_SECONDS, $path, $domain, is_ssl(), true);
|
|
if ($active) {
|
|
$_COOKIE[$cookie] = '1';
|
|
} else {
|
|
unset($_COOKIE[$cookie]);
|
|
}
|
|
}
|
|
endif;
|
|
|
|
if (!function_exists('jio_reorder_pinned_comments')) :
|
|
/**
|
|
* Float pinned top-level comments to the front of the queried list.
|
|
*
|
|
* @param array $comments Comment objects.
|
|
* @param int $post_id Post ID.
|
|
* @return array
|
|
*/
|
|
function jio_reorder_pinned_comments($comments, $post_id) {
|
|
if (!is_array($comments) || count($comments) < 2) {
|
|
return $comments;
|
|
}
|
|
$pinned = [];
|
|
$normal = [];
|
|
foreach ($comments as $comment) {
|
|
if ($comment instanceof WP_Comment && (int) $comment->comment_parent === 0 && jio_is_comment_pinned($comment)) {
|
|
$pinned[] = $comment;
|
|
} else {
|
|
$normal[] = $comment;
|
|
}
|
|
}
|
|
if (empty($pinned)) {
|
|
return $comments;
|
|
}
|
|
return array_merge($pinned, $normal);
|
|
}
|
|
endif;
|
|
|
|
add_filter('comments_array', 'jio_reorder_pinned_comments', 20, 2);
|