*/ function aksrt_message_center_views(): array { return [ 'unread' => __('未读消息', 'aksrt'), 'posts' => __('文章评论', 'aksrt'), 'like' => __('点赞喜欢', 'aksrt'), 'system' => __('系统通知', 'aksrt'), ]; } function aksrt_normalize_message_view(string $view): string { $view = sanitize_key($view); return array_key_exists($view, aksrt_message_center_views()) ? $view : 'unread'; } function aksrt_get_message_center_paginate_type(): string { $settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::notification_settings() : []; $type = sanitize_key((string) ($settings['paginate_type'] ?? 'ajax_lists')); return in_array($type, ['ajax_lists', 'default'], true) ? $type : 'ajax_lists'; } function aksrt_message_category_for_type(string $type): string { $type = sanitize_key($type); if (str_starts_with($type, 'asset_')) { return ''; } if ( 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', 'comment_reply', 'comment_approved', 'pending_comments', 'favorite', 'post_favorite'], true) || str_starts_with($type, 'comment_') || str_starts_with($type, 'favorite_') ) { return 'posts'; } return 'system'; } /** @return array> */ function aksrt_get_message_center_items(int $user_id, string $view): array { $view = aksrt_normalize_message_view($view); $items = function_exists('aksrt_get_user_all_notifications') ? aksrt_get_user_all_notifications($user_id) : []; return array_values(array_filter($items, static function (array $item) use ($view): bool { $category = aksrt_message_category_for_type((string) ($item['type'] ?? 'notice')); if ($category === '') { return false; } if ($view === 'unread') { return empty($item['read']); } return $category === $view; })); } /** @return array{unread:int,posts:int,like:int,system:int} */ function aksrt_get_message_center_counts(int $user_id): array { $counts = ['unread' => 0, 'posts' => 0, 'like' => 0, 'system' => 0]; $items = function_exists('aksrt_get_user_all_notifications') ? aksrt_get_user_all_notifications($user_id) : []; foreach ($items as $item) { $category = aksrt_message_category_for_type((string) ($item['type'] ?? 'notice')); if ($category === '' || !empty($item['read'])) { continue; } $counts['unread']++; $counts[$category]++; } return $counts; } /** @return array|null */ function aksrt_get_message_center_item(int $user_id, int $notification_id): ?array { if ($user_id <= 0 || $notification_id <= 0) { return null; } global $wpdb; $table = class_exists('\AKSRT\Core\DBMigrator') ? \AKSRT\Core\DBMigrator::table('notification') : ''; if ($table === '') return null; $row = $wpdb->get_row($wpdb->prepare( "SELECT id, type, title, description, target_url, is_read, created_at, read_at FROM {$table} WHERE id = %d AND user_id = %d", $notification_id, $user_id ), ARRAY_A); if (!is_array($row) || aksrt_message_category_for_type((string) ($row['type'] ?? 'notice')) === '') return null; return [ 'id' => (string) $row['id'], 'type' => (string) $row['type'], 'title' => (string) $row['title'], 'desc' => (string) ($row['description'] ?? ''), 'url' => (string) ($row['target_url'] ?? ''), 'read' => !empty($row['is_read']) ? 1 : 0, 'created_at' => (string) ($row['created_at'] ?? ''), 'read_at' => (string) ($row['read_at'] ?? ''), ]; } function aksrt_message_icon_class(string $type): string { $icons = [ 'comment_reply' => 'fa fas fa-comment-dots', 'comment_approved' => 'fa fas fa-check-circle', 'pending_comments' => 'fa fas fa-clock', 'post_favorite' => 'fa fas fa-star', 'post_like' => 'fa fas fa-heart', 'comment_like' => 'fa fas fa-thumbs-up', 'followed' => 'fa fas fa-user-plus', 'download_ready' => 'fa fas fa-download', 'upload_moderation'=> 'fa fas fa-image', 'verify_email' => 'fa fas fa-envelope-open', 'pay' => 'fa fas fa-file-alt', 'order_paid' => 'fa fas fa-file-alt', 'order_refund' => 'fa fas fa-undo', 'vip' => 'fa fas fa-certificate', 'pay_vip' => 'fa fas fa-certificate', 'medal' => 'fa fas fa-certificate', 'badge' => 'fa fas fa-certificate', 'ban_appeal' => 'fa fas fa-gavel', 'user_ban' => 'fa fas fa-ban', 'auth_apply' => 'fa fas fa-id-badge', 'auth_apply_reply' => 'fa fas fa-id-badge', ]; return $icons[$type] ?? 'fa fas fa-bell'; } function aksrt_message_time(array $item): string { if (!empty($item['stateful'])) { return __('状态提醒', 'aksrt'); } $created_at = (string) ($item['created_at'] ?? ''); return $created_at !== '' ? get_date_from_gmt($created_at, 'Y-m-d H:i') : ''; } function aksrt_render_message_center_nav(string $view, array $counts): void { $view = aksrt_normalize_message_view($view); ?>

1) : ?> $number >= 1 && $number <= $total_pages))); sort($numbers); $previous_number = 0; ?>

__('请先登录', 'aksrt')], 401); } $nonce = isset($_POST['nonce']) ? (string) wp_unslash($_POST['nonce']) : ''; if (!wp_verify_nonce($nonce, 'aksrt_public_nonce')) { wp_send_json_error(['message' => __('安全验证失败', 'aksrt')], 403); } $user_id = get_current_user_id(); $view = aksrt_normalize_message_view((string) ($_POST['view'] ?? 'unread')); $page = max(1, absint($_POST['page'] ?? 1)); $notification_id = absint($_POST['notification_id'] ?? 0); $detail = null; if ($notification_id > 0) { $detail = aksrt_get_message_center_item($user_id, $notification_id); if (!$detail) { wp_send_json_error(['message' => __('消息不存在或已删除', 'aksrt')], 404); } aksrt_mark_user_notification_read($user_id, (string) $notification_id); $detail['read'] = 1; } $counts = aksrt_get_message_center_counts($user_id); ob_start(); aksrt_render_message_center_nav($view, $counts); $nav_html = (string) ob_get_clean(); ob_start(); if ($detail) { aksrt_render_message_center_detail($detail, $view); } else { aksrt_render_message_center_list($user_id, $view, $page); } $content_html = (string) ob_get_clean(); wp_send_json_success([ 'view' => $view, 'page' => $page, 'detail' => $notification_id, 'paginate_type' => aksrt_get_message_center_paginate_type(), 'nav' => $nav_html, 'content' => $content_html, 'unread' => $counts['unread'], ]); });