v2.0.0.0
Publisium
- <?php
-
- declare(strict_types=1);
-
- $articleId = (int) ($_POST['article_id'] ?? 0);
- $versionId = (int) ($_POST['version_id'] ?? 0);
-
- if ($articleId <= 0 || $versionId <= 0) {
- return ['We couldn’t find that version.', 'error', null];
- }
-
- $statement = Database::site()->prepare('SELECT author_id, status, deleted_at FROM articles WHERE id = :id LIMIT 1');
- $statement->execute(['id' => $articleId]);
- $article = $statement->fetch();
-
- if (!$article || $article['deleted_at'] !== null) {
- return ['We couldn’t find this article. It may have been deleted.', 'error', null];
- }
-
- $isOwner = (int) $article['author_id'] === (int) $currentUser['id'];
- if (!$isOwner && !Auth::hasRoleAtLeast(Auth::ROLE_EDITOR_IN_CHIEF)) {
- return ['Only the author and editors can restore older versions of this article.', 'error', null];
- }
-
- if (in_array($article['status'], ['published', 'scheduled'], true) && !Auth::canPublishDirectly()) {
- return ['Switch the article back to a draft first, then restore the older version.', 'error', null];
- }
-
- if (!ArticleVersions::restore($articleId, $versionId, (int) $currentUser['id'], (string) $currentUser['display_name'])) {
- return ['We couldn’t find that version.', 'error', null];
- }
-
- ActivityLog::record('article.restore_version', 'article', $articleId, 'version #' . $versionId);
-
- return ['Older version restored.', 'success', $articleId];
-