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,219 @@
|
||||
<?php
|
||||
/**
|
||||
* Comment action permissions + like/pin primitives.
|
||||
*
|
||||
* Extracted from aksrt-frontend-tags.php for size budget.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
if (!function_exists('aksrt_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 aksrt_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('aksrt_user_can')) {
|
||||
return aksrt_user_can($cap, $uid, $ctx);
|
||||
}
|
||||
if (class_exists('\AKSRT\Settings\Capabilities')) {
|
||||
return \AKSRT\Settings\Capabilities::user_can($cap, $uid, $ctx);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
$edit_window = 10;
|
||||
if (class_exists('\AKSRT\Settings\Settings')) {
|
||||
$display = \AKSRT\Settings\Settings::get_comment_display_settings();
|
||||
$edit_window = (int) ($display['edit_window'] ?? 10);
|
||||
}
|
||||
$within_window = $edit_window > 0
|
||||
&& (time() - strtotime((string) $comment->comment_date_gmt)) < ($edit_window * MINUTE_IN_SECONDS);
|
||||
|
||||
$can_edit_own = $is_owner && $within_window && $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('aksrt_is_comment_pinned')) :
|
||||
/**
|
||||
* @param int|WP_Comment $comment Comment.
|
||||
*/
|
||||
function aksrt_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, '_aksrt_comment_pinned', true);
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_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 aksrt_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('aksrt_get_comment_likes')) :
|
||||
/**
|
||||
* @param int|WP_Comment $comment Comment.
|
||||
*/
|
||||
function aksrt_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, 'aksrt_comment_likes', true));
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_is_comment_liked')) :
|
||||
/**
|
||||
* @param int $comment_id Comment ID.
|
||||
*/
|
||||
function aksrt_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(), '_aksrt_liked_comments', true);
|
||||
$liked = is_array($liked) ? array_map('intval', $liked) : [];
|
||||
return in_array($comment_id, $liked, true);
|
||||
}
|
||||
$cookie = 'aksrt_clike_' . $comment_id;
|
||||
return !empty($_COOKIE[$cookie]) && '1' === $_COOKIE[$cookie];
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_set_comment_like_state')) :
|
||||
/**
|
||||
* @param int $comment_id Comment ID.
|
||||
* @param bool $active Target state.
|
||||
*/
|
||||
function aksrt_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, '_aksrt_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, '_aksrt_liked_comments', array_values(array_unique($liked)));
|
||||
return;
|
||||
}
|
||||
$cookie = 'aksrt_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('aksrt_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 aksrt_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 && aksrt_is_comment_pinned($comment)) {
|
||||
$pinned[] = $comment;
|
||||
} else {
|
||||
$normal[] = $comment;
|
||||
}
|
||||
}
|
||||
if (empty($pinned)) {
|
||||
return $comments;
|
||||
}
|
||||
return array_merge($pinned, $normal);
|
||||
}
|
||||
endif;
|
||||
|
||||
add_filter('comments_array', 'aksrt_reorder_pinned_comments', 20, 2);
|
||||
Reference in new issue
Block a user