ensure_cap(); echo '

' . esc_html__('下载记录', 'aksrt') . '

'; if (!class_exists('AKSRT_DB_Migrator')) { echo '

' . esc_html__('数据库服务不可用。', 'aksrt') . '

'; return; } $table = AKSRT_DB_Migrator::table('dl_token'); if ($table === '') { echo '

' . esc_html__('下载令牌表未初始化。', 'aksrt') . '

'; return; } $post_id = isset($_GET['post_id']) ? max(0, (int) $_GET['post_id']) : 0; $user_id = isset($_GET['user_id']) ? max(0, (int) $_GET['user_id']) : 0; $only_used = !isset($_GET['used']) || (string) $_GET['used'] !== '0'; $page_num = max(1, (int) ($_GET['paged'] ?? 1)); $per_page = 30; $result = $this->query_logs($table, [ 'post_id' => $post_id, 'user_id' => $user_id, 'only_used' => $only_used, 'limit' => $per_page, 'offset' => ($page_num - 1) * $per_page, ]); $items = $result['items']; $total = $result['total']; $pages = max(1, (int) ceil($total / $per_page)); echo '

' . esc_html__('基于下载令牌表(aksrt_dl_token)。已使用记录会保留一段时间后清理;未使用且过期的令牌会被定期删除。', 'aksrt') . '

'; echo '
'; echo ''; echo ' '; echo ' '; echo ' '; // When unchecked, pass used=0 explicitly. if (!$only_used) { // no-op; checkbox not checked } submit_button(__('筛选', 'aksrt'), 'secondary', '', false); echo ' ' . esc_html__('重置', 'aksrt') . ''; echo '
'; // Fix: unchecked checkbox means used filter off — re-read without default if form submitted with no used. // Already handled via isset($_GET['used']). echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; if (!$items) { echo ''; } foreach ($items as $row) { $uid = (int) ($row['user_id'] ?? 0); $pid = (int) ($row['post_id'] ?? 0); $user = $uid > 0 ? get_userdata($uid) : null; $title = $pid > 0 ? get_the_title($pid) : ''; if ($title === '') { $title = $pid > 0 ? sprintf(__('(已删除 #%d)', 'aksrt'), $pid) : '—'; } $edit = $pid > 0 ? get_edit_post_link($pid, 'raw') : ''; $used = (int) ($row['used_count'] ?? 0); $max = (int) ($row['max_uses'] ?? 1); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html__('时间', 'aksrt') . '' . esc_html__('用户', 'aksrt') . '' . esc_html__('文章', 'aksrt') . '' . esc_html__('源序号', 'aksrt') . '' . esc_html__('积分消耗', 'aksrt') . '' . esc_html__('使用', 'aksrt') . '' . esc_html__('过期', 'aksrt') . '' . esc_html__('IP', 'aksrt') . '
' . esc_html__('暂无下载记录。', 'aksrt') . '
' . esc_html((string) ($row['created_at'] ?? '')) . ''; if ($user) { echo esc_html($user->display_name . ' (#' . $uid . ')'); echo '
' . esc_html__('仅此用户', 'aksrt') . ''; } else { echo esc_html((string) $uid); } echo '
'; if ($edit) { echo '' . esc_html($title) . ''; } else { echo esc_html($title); } if ($pid > 0) { echo '
' . esc_html__('仅此文章', 'aksrt') . ''; } echo '
' . esc_html((string) (int) ($row['source_index'] ?? 0)) . '' . esc_html((string) (int) ($row['cost'] ?? 0)) . '' . esc_html($used . ' / ' . $max) . ($used > 0 ? ' ' : '') . '' . esc_html((string) ($row['expires_at'] ?? '')) . '' . esc_html((string) ($row['ip_bind'] ?? '')) . '
'; if ($pages > 1) { echo '
'; echo paginate_links([ 'base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => '«', 'next_text' => '»', 'total' => $pages, 'current' => $page_num, ]); echo '
'; } echo ''; } /** * @param array{post_id?:int,user_id?:int,only_used?:bool,limit?:int,offset?:int} $args * @return array{items: array>, total:int} */ private function query_logs(string $table, array $args): array { global $wpdb; $where = ['1=1']; $params = []; $post_id = (int) ($args['post_id'] ?? 0); if ($post_id > 0) { $where[] = 'post_id = %d'; $params[] = $post_id; } $user_id = (int) ($args['user_id'] ?? 0); if ($user_id > 0) { $where[] = 'user_id = %d'; $params[] = $user_id; } if (!empty($args['only_used'])) { $where[] = 'used_count > 0'; } $where_sql = implode(' AND ', $where); $limit = max(1, min(100, (int) ($args['limit'] ?? 30))); $offset = max(0, (int) ($args['offset'] ?? 0)); $count_sql = "SELECT COUNT(*) FROM {$table} WHERE {$where_sql}"; $total = $params ? (int) $wpdb->get_var($wpdb->prepare($count_sql, $params)) : (int) $wpdb->get_var($count_sql); $sql = "SELECT * FROM {$table} WHERE {$where_sql} ORDER BY created_at DESC LIMIT %d OFFSET %d"; $list_params = $params; $list_params[] = $limit; $list_params[] = $offset; $rows = $wpdb->get_results($wpdb->prepare($sql, $list_params), ARRAY_A); return [ 'items' => is_array($rows) ? $rows : [], 'total' => $total, ]; } private function ensure_cap(): void { if (!current_user_can(self::CAPABILITY)) { wp_die(esc_html__('您没有权限访问此页面。', 'aksrt')); } } }