修复了一些问题
This commit is contained in:
@@ -56,11 +56,12 @@ function aksrt_notification_type_label(string $type): string {
|
||||
$map = [
|
||||
'comment_reply' => __('评论回复', 'aksrt'),
|
||||
'comment_approved' => __('评论审核', 'aksrt'),
|
||||
'comment' => __('文章评论', 'aksrt'),
|
||||
'download_ready' => __('下载就绪', 'aksrt'),
|
||||
'asset_points_increase' => __('积分增加', 'aksrt'),
|
||||
'asset_points_decrease' => __('积分扣减', 'aksrt'),
|
||||
'asset_balance_increase' => __('余额增加', 'aksrt'),
|
||||
'asset_balance_decrease' => __('余额扣减', 'aksrt'),
|
||||
'post_favorite' => __('文章收藏', 'aksrt'),
|
||||
'post_like' => __('文章点赞', 'aksrt'),
|
||||
'comment_like' => __('评论点赞', 'aksrt'),
|
||||
'followed' => __('新增关注', 'aksrt'),
|
||||
'pending_comments' => __('待审核评论', 'aksrt'),
|
||||
'notice' => __('通知', 'aksrt'),
|
||||
];
|
||||
@@ -88,38 +89,18 @@ function aksrt_get_user_inbox_notifications(int $user_id = 0): array {
|
||||
|
||||
global $wpdb;
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
if ($table === '') {
|
||||
$items = get_user_meta($user_id, 'aksrt_notifications', true);
|
||||
return is_array($items) ? array_values($items) : [];
|
||||
}
|
||||
|
||||
static $migrated = [];
|
||||
if (empty($migrated[$user_id])) {
|
||||
$legacy = get_user_meta($user_id, 'aksrt_notifications', true);
|
||||
if (is_array($legacy) && !empty($legacy)) {
|
||||
foreach ($legacy as $item) {
|
||||
aksrt_add_user_notification($user_id, [
|
||||
'id' => $item['id'] ?? '',
|
||||
'type' => $item['type'] ?? 'notice',
|
||||
'title' => $item['title'] ?? '',
|
||||
'desc' => $item['desc'] ?? '',
|
||||
'url' => $item['url'] ?? '',
|
||||
'read' => !empty($item['read']),
|
||||
'created_at' => $item['created_at'] ?? current_time('mysql', true),
|
||||
]);
|
||||
}
|
||||
delete_user_meta($user_id, 'aksrt_notifications');
|
||||
}
|
||||
$migrated[$user_id] = true;
|
||||
}
|
||||
if ($table === '') return [];
|
||||
$settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_notification_settings() : [];
|
||||
$limit = max(20, min(5000, (int) ($settings['max_per_user'] ?? 200)));
|
||||
|
||||
$rows = $wpdb->get_results($wpdb->prepare(
|
||||
"SELECT id, type, title, description, target_url, is_read, created_at, read_at
|
||||
FROM {$table}
|
||||
WHERE user_id = %d
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT 50",
|
||||
$user_id
|
||||
LIMIT %d",
|
||||
$user_id,
|
||||
$limit
|
||||
), ARRAY_A);
|
||||
|
||||
if (!is_array($rows)) return [];
|
||||
@@ -220,34 +201,6 @@ endif;
|
||||
add_action('wp', 'aksrt_schedule_notification_cleanup');
|
||||
add_action('aksrt_notification_cleanup', 'aksrt_cleanup_notifications');
|
||||
|
||||
if (!function_exists('aksrt_write_user_inbox_notifications')) :
|
||||
/** 写入用户通知收件箱 */
|
||||
function aksrt_write_user_inbox_notifications(int $user_id, array $items): void {
|
||||
if ($user_id <= 0) return;
|
||||
global $wpdb;
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
if ($table === '') {
|
||||
update_user_meta($user_id, 'aksrt_notifications', array_values(array_slice($items, 0, 50)));
|
||||
return;
|
||||
}
|
||||
|
||||
$wpdb->delete($table, ['user_id' => $user_id], ['%d']);
|
||||
$items = array_values(array_slice($items, 0, 50));
|
||||
foreach ($items as $item) {
|
||||
$wpdb->insert($table, [
|
||||
'user_id' => $user_id,
|
||||
'type' => sanitize_key((string) ($item['type'] ?? 'notice')),
|
||||
'title' => sanitize_text_field((string) ($item['title'] ?? '')),
|
||||
'description' => sanitize_text_field((string) ($item['desc'] ?? '')),
|
||||
'target_url' => esc_url_raw((string) ($item['url'] ?? '')),
|
||||
'is_read' => !empty($item['read']) ? 1 : 0,
|
||||
'created_at' => (string) ($item['created_at'] ?? current_time('mysql', true)),
|
||||
'read_at' => !empty($item['read']) ? current_time('mysql', true) : null,
|
||||
], ['%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s']);
|
||||
}
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_add_user_notification')) :
|
||||
/** 添加一条用户通知 */
|
||||
function aksrt_add_user_notification(int $user_id, array $payload): void {
|
||||
@@ -264,20 +217,7 @@ function aksrt_add_user_notification(int $user_id, array $payload): void {
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
$created_at = (string) ($payload['created_at'] ?? current_time('mysql', true));
|
||||
|
||||
if ($table === '') {
|
||||
$items = aksrt_get_user_inbox_notifications($user_id);
|
||||
array_unshift($items, [
|
||||
'id' => isset($payload['id']) && $payload['id'] !== '' ? sanitize_text_field((string) $payload['id']) : uniqid('notif_', true),
|
||||
'type' => $type,
|
||||
'title' => sanitize_text_field((string) ($payload['title'] ?? '')),
|
||||
'desc' => sanitize_text_field((string) ($payload['desc'] ?? '')),
|
||||
'url' => esc_url_raw((string) ($payload['url'] ?? '')),
|
||||
'read' => !empty($payload['read']) ? 1 : 0,
|
||||
'created_at' => $created_at,
|
||||
]);
|
||||
aksrt_write_user_inbox_notifications($user_id, $items);
|
||||
return;
|
||||
}
|
||||
if ($table === '') return;
|
||||
|
||||
$wpdb->insert($table, [
|
||||
'user_id' => $user_id,
|
||||
@@ -298,17 +238,7 @@ function aksrt_mark_user_notification_read(int $user_id, string $notification_id
|
||||
if ($user_id <= 0) return;
|
||||
global $wpdb;
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
if ($table === '') {
|
||||
$items = aksrt_get_user_inbox_notifications($user_id);
|
||||
foreach ($items as &$item) {
|
||||
if ($mark_all || (($item['id'] ?? '') === $notification_id)) {
|
||||
$item['read'] = 1;
|
||||
}
|
||||
}
|
||||
unset($item);
|
||||
aksrt_write_user_inbox_notifications($user_id, $items);
|
||||
return;
|
||||
}
|
||||
if ($table === '') return;
|
||||
|
||||
if ($mark_all) {
|
||||
$wpdb->update(
|
||||
@@ -337,14 +267,7 @@ function aksrt_delete_user_notification(int $user_id, string $notification_id):
|
||||
if ($user_id <= 0 || $notification_id === '') return;
|
||||
global $wpdb;
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
if ($table === '') {
|
||||
$items = aksrt_get_user_inbox_notifications($user_id);
|
||||
$items = array_values(array_filter($items, static function ($item) use ($notification_id) {
|
||||
return (string) ($item['id'] ?? '') !== $notification_id;
|
||||
}));
|
||||
aksrt_write_user_inbox_notifications($user_id, $items);
|
||||
return;
|
||||
}
|
||||
if ($table === '') return;
|
||||
$wpdb->delete($table, ['id' => absint($notification_id), 'user_id' => $user_id], ['%d', '%d']);
|
||||
}
|
||||
endif;
|
||||
@@ -358,14 +281,7 @@ function aksrt_delete_user_notifications(int $user_id, array $notification_ids):
|
||||
|
||||
global $wpdb;
|
||||
$table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : '';
|
||||
if ($table === '') {
|
||||
$items = aksrt_get_user_inbox_notifications($user_id);
|
||||
$items = array_values(array_filter($items, static function ($item) use ($ids) {
|
||||
return !in_array(absint($item['id'] ?? 0), $ids, true);
|
||||
}));
|
||||
aksrt_write_user_inbox_notifications($user_id, $items);
|
||||
return;
|
||||
}
|
||||
if ($table === '') return;
|
||||
|
||||
$wpdb->query(
|
||||
"DELETE FROM {$table} WHERE user_id = " . (int) $user_id . " AND id IN (" . implode(',', $ids) . ")"
|
||||
@@ -379,7 +295,7 @@ function aksrt_get_user_unread_notification_count(int $user_id = 0): int {
|
||||
$items = aksrt_get_user_inbox_notifications($user_id);
|
||||
$count = 0;
|
||||
foreach ($items as $item) {
|
||||
if (empty($item['read'])) $count++;
|
||||
if (empty($item['read']) && !str_starts_with((string) ($item['type'] ?? ''), 'asset_')) $count++;
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
@@ -498,16 +414,33 @@ add_action('wp_ajax_aksrt_check_new_notifications', function () {
|
||||
wp_send_json_error(['message' => __('安全验证失败', 'aksrt')], 403);
|
||||
}
|
||||
|
||||
$unread = aksrt_get_user_unread_notification_count(get_current_user_id());
|
||||
$counts = function_exists('aksrt_get_message_center_counts') ? aksrt_get_message_center_counts(get_current_user_id()) : [];
|
||||
$unread = isset($counts['unread']) ? (int) $counts['unread'] : aksrt_get_user_unread_notification_count(get_current_user_id());
|
||||
wp_send_json_success([
|
||||
'count' => $unread,
|
||||
'unread' => $unread,
|
||||
]);
|
||||
});
|
||||
|
||||
function aksrt_notify_post_author_of_comment(WP_Comment $comment): void {
|
||||
if ((int) $comment->comment_parent > 0 || get_comment_meta($comment->comment_ID, 'aksrt_post_comment_notified', true)) return;
|
||||
$post = get_post((int) $comment->comment_post_ID);
|
||||
$owner_id = $post instanceof WP_Post ? (int) $post->post_author : 0;
|
||||
if ($owner_id <= 0 || $owner_id === (int) $comment->user_id) return;
|
||||
aksrt_add_user_notification($owner_id, [
|
||||
'type' => 'comment',
|
||||
'title' => __('文章收到评论', 'aksrt'),
|
||||
'desc' => sprintf(__('%1$s 评论了你的文章《%2$s》。', 'aksrt'), get_comment_author($comment), get_the_title($post)),
|
||||
'url' => get_comment_link($comment),
|
||||
]);
|
||||
update_comment_meta($comment->comment_ID, 'aksrt_post_comment_notified', 1);
|
||||
}
|
||||
|
||||
add_action('wp_insert_comment', function ($comment_id, $comment) {
|
||||
if (!$comment instanceof WP_Comment) return;
|
||||
if ((int) $comment->comment_parent <= 0) return;
|
||||
if ((int) $comment->comment_approved !== 1) return;
|
||||
aksrt_notify_post_author_of_comment($comment);
|
||||
if ((int) $comment->comment_parent <= 0) return;
|
||||
if (get_comment_meta($comment_id, 'aksrt_reply_notified', true)) return;
|
||||
|
||||
$parent = get_comment((int) $comment->comment_parent);
|
||||
@@ -540,6 +473,7 @@ add_action('transition_comment_status', function ($new_status, $old_status, $com
|
||||
]);
|
||||
update_comment_meta($comment->comment_ID, 'aksrt_approved_notified', 1);
|
||||
}
|
||||
aksrt_notify_post_author_of_comment($comment);
|
||||
|
||||
if ((int) $comment->comment_parent <= 0) return;
|
||||
if (get_comment_meta($comment->comment_ID, 'aksrt_reply_notified', true)) return;
|
||||
@@ -558,38 +492,55 @@ add_action('transition_comment_status', function ($new_status, $old_status, $com
|
||||
update_comment_meta($comment->comment_ID, 'aksrt_reply_notified', 1);
|
||||
}, 20, 3);
|
||||
|
||||
add_action('aksrt_asset_transaction_committed', function ($result, $command): void {
|
||||
if (!$result instanceof \AKSRT\Ledger\LedgerResult || !$command instanceof \AKSRT\Ledger\LedgerCommand) {
|
||||
return;
|
||||
}
|
||||
$changes = [];
|
||||
foreach ($result->entries as $entry) {
|
||||
$user_id = (int) ($entry['user_id'] ?? 0);
|
||||
$asset = (string) ($entry['asset'] ?? '');
|
||||
if ($user_id <= 0 || !in_array($asset, ['points', 'balance'], true)) continue;
|
||||
$key = $user_id . ':' . $asset;
|
||||
$changes[$key] ??= ['user_id' => $user_id, 'asset' => $asset, 'delta' => 0, 'balance' => 0];
|
||||
$changes[$key]['delta'] += (int) ($entry['delta_minor'] ?? 0);
|
||||
$changes[$key]['balance'] = (int) ($entry['balance_after_minor'] ?? 0);
|
||||
}
|
||||
foreach ($changes as $change) {
|
||||
if ((int) $change['delta'] === 0) continue;
|
||||
$is_points = $change['asset'] === 'points';
|
||||
$positive = $change['delta'] > 0;
|
||||
$amount = $is_points
|
||||
? (string) abs((int) $change['delta'])
|
||||
: \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, abs((int) $change['delta']))->decimal();
|
||||
$balance = $is_points
|
||||
? (string) $change['balance']
|
||||
: \AKSRT\Ledger\AssetAmount::from_minor(\AKSRT\Ledger\AssetType::BALANCE, (int) $change['balance'])->decimal();
|
||||
aksrt_add_user_notification((int) $change['user_id'], [
|
||||
'type' => 'asset_' . $change['asset'] . '_' . ($positive ? 'increase' : 'decrease'),
|
||||
'title' => $is_points ? __('积分变动', 'aksrt') : __('余额变动', 'aksrt'),
|
||||
'desc' => sprintf(__('%1$s:%2$s%3$s,当前余额 %4$s。', 'aksrt'), \AKSRT\Ledger\LedgerPresentation::reason_label($command->reason), $positive ? '+' : '-', $amount, $balance),
|
||||
'url' => aksrt_get_user_center_url('credits'),
|
||||
]);
|
||||
}
|
||||
}, 10, 2);
|
||||
add_action('aksrt_post_favorited', function (int $post_id, int $actor_id, bool $active): void {
|
||||
$post = $active ? get_post($post_id) : null;
|
||||
$owner_id = $post instanceof WP_Post ? (int) $post->post_author : 0;
|
||||
if ($owner_id <= 0 || $owner_id === $actor_id) return;
|
||||
$actor = get_userdata($actor_id);
|
||||
aksrt_add_user_notification($owner_id, [
|
||||
'type' => 'post_favorite',
|
||||
'title' => __('文章被收藏', 'aksrt'),
|
||||
'desc' => sprintf(__('%1$s 收藏了你的文章《%2$s》。', 'aksrt'), $actor instanceof WP_User ? $actor->display_name : __('一位用户', 'aksrt'), get_the_title($post)),
|
||||
'url' => get_permalink($post),
|
||||
]);
|
||||
}, 30, 3);
|
||||
|
||||
add_action('aksrt_post_liked', function (int $post_id, int $actor_id, bool $active): void {
|
||||
$post = $active ? get_post($post_id) : null;
|
||||
$owner_id = $post instanceof WP_Post ? (int) $post->post_author : 0;
|
||||
if ($owner_id <= 0 || $owner_id === $actor_id) return;
|
||||
$actor = get_userdata($actor_id);
|
||||
aksrt_add_user_notification($owner_id, [
|
||||
'type' => 'post_like',
|
||||
'title' => __('文章收到点赞', 'aksrt'),
|
||||
'desc' => sprintf(__('%1$s 点赞了你的文章《%2$s》。', 'aksrt'), $actor instanceof WP_User ? $actor->display_name : __('一位用户', 'aksrt'), get_the_title($post)),
|
||||
'url' => get_permalink($post),
|
||||
]);
|
||||
}, 30, 3);
|
||||
|
||||
add_action('aksrt_comment_liked', function (int $comment_id, int $actor_id, bool $active): void {
|
||||
$comment = $active ? get_comment($comment_id) : null;
|
||||
$owner_id = $comment instanceof WP_Comment ? (int) $comment->user_id : 0;
|
||||
if ($owner_id <= 0 || $owner_id === $actor_id) return;
|
||||
$actor = get_userdata($actor_id);
|
||||
aksrt_add_user_notification($owner_id, [
|
||||
'type' => 'comment_like',
|
||||
'title' => __('评论收到点赞', 'aksrt'),
|
||||
'desc' => sprintf(__('%s 点赞了你的评论。', 'aksrt'), $actor instanceof WP_User ? $actor->display_name : __('一位用户', 'aksrt')),
|
||||
'url' => get_comment_link($comment),
|
||||
]);
|
||||
}, 30, 3);
|
||||
|
||||
add_action('aksrt_user_followed', function (int $follower_id, int $following_id): void {
|
||||
if ($following_id <= 0 || $following_id === $follower_id) return;
|
||||
$follower = get_userdata($follower_id);
|
||||
aksrt_add_user_notification($following_id, [
|
||||
'type' => 'followed',
|
||||
'title' => __('收到新的关注', 'aksrt'),
|
||||
'desc' => sprintf(__('%s 关注了你。', 'aksrt'), $follower instanceof WP_User ? $follower->display_name : __('一位用户', 'aksrt')),
|
||||
'url' => function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($follower_id) : '',
|
||||
]);
|
||||
}, 30, 2);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user