修复了一些问题

This commit is contained in:
2026-08-25 17:57:20 +08:00
parent 8eb0079bc4
commit dbb1f776f2
19 changed files with 785 additions and 634 deletions
+1 -1
View File
@@ -568,7 +568,7 @@ trait ScriptsPart1b {
}
// 消息中心页
if (is_page_template('templates/pages/page-messages.php')) {
if (get_query_var('aksrt_message_view')) {
$messages_css = get_template_directory() . '/assets/css/frontend/messages.css';
if (file_exists($messages_css)) {
wp_enqueue_style(
-1
View File
@@ -46,7 +46,6 @@ class Templates {
'templates/pages/page-posts-navs.php' => __('AKSRT - 文章导航', 'aksrt'),
'templates/pages/page-friends.php' => __('AKSRT - 友情链接', 'aksrt'),
'templates/pages/page-ban-appeal.php' => __('AKSRT - 禁封申诉', 'aksrt'),
'templates/pages/page-messages.php' => __('AKSRT - 消息中心', 'aksrt'),
];
}
+17 -71
View File
@@ -16,33 +16,21 @@ class Message {
return self::$instance;
}
public function init(): void {
add_action('wp_ajax_aksrt_mark_message_read', [$this, 'ajax_mark_read']);
add_action('wp_ajax_aksrt_get_unread_count', [$this, 'ajax_get_unread_count']);
}
/**
* 获取用户未读消息数
*/
public function get_unread_count(int $user_id): int {
if ($user_id <= 0) return 0;
// 优先从表中实时统计
$table = $this->table();
if ($table !== '') {
global $wpdb;
$count = (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$table} WHERE user_id = %d AND is_read = 0",
return (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM {$table} WHERE user_id = %d AND is_read = 0 AND type NOT LIKE 'asset\\_%'",
$user_id
));
// 同步到 user_meta 以兼容旧读取方
update_user_meta($user_id, 'aksrt_unread_message_count', $count);
return $count;
}
// 降级:从 user_meta 读取
$count = (int) get_user_meta($user_id, 'aksrt_unread_message_count', true);
return max(0, $count);
return 0;
}
/**
@@ -64,12 +52,7 @@ class Message {
return $result !== false;
}
// 降级:仅更新计数
$current = $this->get_unread_count($user_id);
if ($current > 0) {
update_user_meta($user_id, 'aksrt_unread_message_count', max(0, $current - 1));
}
return true;
return false;
}
/**
@@ -90,8 +73,7 @@ class Message {
);
}
update_user_meta($user_id, 'aksrt_unread_message_count', 0);
return true;
return $table !== '';
}
/**
@@ -107,22 +89,18 @@ class Message {
if ($to_user_id <= 0 || empty($content)) return false;
$table = $this->table();
if ($table !== '') {
global $wpdb;
$wpdb->insert($table, [
'user_id' => $to_user_id,
'type' => sanitize_key($type),
'title' => sanitize_text_field((string) ($data['title'] ?? $content)),
'description' => sanitize_textarea_field($content),
'target_url' => esc_url_raw((string) ($data['target_url'] ?? '')),
'is_read' => 0,
'created_at' => current_time('mysql'),
], ['%d', '%s', '%s', '%s', '%s', '%d', '%s']);
}
// 同步更新 user_meta 未读计数
$current = (int) get_user_meta($to_user_id, 'aksrt_unread_message_count', true);
update_user_meta($to_user_id, 'aksrt_unread_message_count', max(0, $current) + 1);
if ($table === '') return false;
global $wpdb;
$inserted = $wpdb->insert($table, [
'user_id' => $to_user_id,
'type' => sanitize_key($type),
'title' => sanitize_text_field((string) ($data['title'] ?? $content)),
'description' => sanitize_textarea_field($content),
'target_url' => esc_url_raw((string) ($data['target_url'] ?? '')),
'is_read' => 0,
'created_at' => current_time('mysql'),
], ['%d', '%s', '%s', '%s', '%s', '%d', '%s']);
if ($inserted === false) return false;
// 触发通知钩子
do_action('aksrt_notification_sent', $to_user_id, $type, $data);
@@ -181,36 +159,4 @@ class Message {
return '';
}
/**
* AJAX 标记已读
*/
public function ajax_mark_read(): void {
check_ajax_referer('aksrt_message', '_nonce');
$user_id = get_current_user_id();
if ($user_id <= 0) {
wp_send_json_error(['message' => __('请先登录', 'aksrt')]);
}
$message_id = sanitize_text_field((string) ($_POST['message_id'] ?? ''));
if ($this->mark_read($user_id, $message_id)) {
wp_send_json_success(['unread' => $this->get_unread_count($user_id)]);
}
wp_send_json_error(['message' => __('操作失败', 'aksrt')]);
}
/**
* AJAX 获取未读数
*/
public function ajax_get_unread_count(): void {
check_ajax_referer('aksrt_message', '_nonce');
$user_id = get_current_user_id();
if ($user_id <= 0) {
wp_send_json_error(['message' => __('请先登录', 'aksrt')]);
}
wp_send_json_success(['unread' => $this->get_unread_count($user_id)]);
}
}
-4
View File
@@ -286,10 +286,6 @@ class Integration {
'comment_reply' => true,
'comment_approved' => true,
'download_ready' => true,
'asset_points_increase' => true,
'asset_points_decrease' => true,
'asset_balance_increase' => true,
'asset_balance_decrease' => true,
'pending_comments' => true,
'upload_moderation' => true,
],
+8 -9
View File
@@ -69,21 +69,20 @@ final class NotificationPreferences {
public static function category_for_type(string $notification_type): string {
$type = sanitize_key($notification_type);
if (
in_array($type, ['posts', 'post', 'comment', 'favorite', 'pending_comments'], true)
|| str_starts_with($type, 'post_')
|| str_starts_with($type, 'comment_')
|| str_starts_with($type, 'favorite_')
) {
return 'posts';
}
if (
in_array($type, ['like', 'follow', 'followed', 'hot'], true)
in_array($type, ['like', 'post_like', 'comment_like', 'follow', 'followed', 'hot'], true)
|| str_starts_with($type, 'like_')
|| str_starts_with($type, 'follow_')
|| str_starts_with($type, 'followed_')
) {
return 'like';
}
if (
in_array($type, ['posts', 'post', 'comment', 'favorite', 'post_favorite', 'pending_comments'], true)
|| str_starts_with($type, 'comment_')
|| str_starts_with($type, 'favorite_')
) {
return 'posts';
}
return 'system';
}