v1.0.0.9
Publisium
- <?php
-
- declare(strict_types=1);
-
- $db = Database::site();
- $canManageAny = Auth::hasRoleAtLeast(Auth::ROLE_EDITOR_IN_CHIEF);
-
- $loadTrashed = static function (int $id) use ($db): ?array {
- $statement = $db->prepare('SELECT * FROM articles WHERE id = :id AND deleted_at IS NOT NULL LIMIT 1');
- $statement->execute(['id' => $id]);
-
- return $statement->fetch() ?: null;
- };
-
- $canTouch = static fn (array $article): bool => $canManageAny || (int) $article['author_id'] === (int) $currentUser['id'];
-
- $purge = static function (int $id) use ($db): void {
- $db->prepare('DELETE FROM article_versions WHERE article_id = :id')->execute(['id' => $id]);
- $db->prepare('DELETE FROM article_tags WHERE article_id = :id')->execute(['id' => $id]);
- $db->prepare('DELETE FROM articles WHERE id = :id')->execute(['id' => $id]);
- };
-
- $articleId = (int) ($_POST['article_id'] ?? 0);
-
- if ($action === 'restore_article') {
- $article = $articleId > 0 ? $loadTrashed($articleId) : null;
- if ($article === null || !$canTouch($article)) {
- return 0;
- }
-
- $restoredStatus = in_array($article['trashed_status'], ['published', 'pending_review', 'rejected'], true) ? $article['trashed_status'] : 'draft';
-
- $db->prepare('UPDATE articles SET status = :status, deleted_at = NULL, trashed_status = NULL WHERE id = :id')
- ->execute(['status' => $restoredStatus, 'id' => $articleId]);
- ActivityLog::record('article.restore', 'article', $articleId, (string) $article['title']);
-
- return $articleId;
- }
-
- if ($action === 'purge_article') {
- $article = $articleId > 0 ? $loadTrashed($articleId) : null;
- if ($article === null || !$canTouch($article)) {
- return 0;
- }
-
- $purge($articleId);
- ActivityLog::record('article.purge', 'article', $articleId, (string) $article['title']);
-
- return 0;
- }
-
- if ($action === 'empty_trash') {
- $statement = $canManageAny
- ? $db->query('SELECT id FROM articles WHERE deleted_at IS NOT NULL')
- : $db->prepare('SELECT id FROM articles WHERE deleted_at IS NOT NULL AND author_id = :author');
-
- if (!$canManageAny) {
- $statement->execute(['author' => $currentUser['id']]);
- }
-
- $ids = $statement->fetchAll(PDO::FETCH_COLUMN);
- foreach ($ids as $id) {
- $purge((int) $id);
- }
- ActivityLog::record('article.empty_trash', 'article', null, count($ids) . ' articles');
-
- return 0;
- }
-
- return 0;
-