> */ public static function all(): array { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '') { return []; } $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY sort_order ASC, id ASC", ARRAY_A); // phpcs:ignore WordPress.DB.DirectDatabaseQuery if (!is_array($rows)) { return []; } $out = []; foreach ($rows as $row) { $out[] = [ 'id' => (int) ($row['id'] ?? 0), 'content' => (string) ($row['content'] ?? ''), 'sort_order' => (int) ($row['sort_order'] ?? 0), 'created_at' => (string) ($row['created_at'] ?? ''), ]; } return $out; } /** * 前台只暴露 id + content。 * * @return list> */ public static function public_list(): array { $out = []; foreach (self::all() as $row) { $out[] = ['id' => $row['id'], 'content' => $row['content']]; } return $out; } /** @return array|null */ public static function find(int $id): ?array { foreach (self::all() as $row) { if ((int) $row['id'] === $id) { return $row; } } return null; } /** 新建时取当前最大 sort_order + 1,追加到末尾。 */ private static function next_sort_order(): int { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '') { return 0; } $max = $wpdb->get_var("SELECT MAX(sort_order) FROM {$table}"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return (int) $max + 1; } public static function create(string $content): int { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '') { return 0; } $inserted = $wpdb->insert($table, [ 'content' => $content, 'sort_order' => self::next_sort_order(), 'created_at' => current_time('mysql'), ], ['%s', '%d', '%s']); return $inserted === 1 ? (int) $wpdb->insert_id : 0; } public static function update(int $id, string $content): bool { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '' || $id <= 0) { return false; } return $wpdb->update($table, ['content' => $content], ['id' => $id], ['%s'], ['%d']) !== false; } public static function delete(int $id): bool { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '' || $id <= 0) { return false; } return $wpdb->delete($table, ['id' => $id], ['%d']) !== false; } /** * 排序:入参必须是全量 ID(数量不符即拒绝,避免漏排导致顺序错乱)。 * * @param list $ids */ public static function reorder(array $ids): bool { global $wpdb; $table = \AKSRT\Core\DBMigrator::table('ticket_prereply'); if ($table === '') { return false; } $ids = array_values(array_filter(array_map('intval', $ids), static fn(int $id): bool => $id > 0)); $existing = array_map(static fn(array $row): int => (int) $row['id'], self::all()); if (count($ids) !== count($existing) || array_diff($ids, $existing) !== []) { return false; } foreach ($ids as $position => $id) { $wpdb->update($table, ['sort_order' => (int) $position], ['id' => $id], ['%d'], ['%d']); } return true; } }