WebOrbiton
v2.0.0.0

Publisium

36 lines · 1.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. $articleId = (int) ($_POST['article_id'] ?? 0);
  6. $versionId = (int) ($_POST['version_id'] ?? 0);
  7. ​
  8. if ($articleId <= 0 || $versionId <= 0) {
  9. return ['We couldn’t find that version.', 'error', null];
  10. }
  11. ​
  12. $statement = Database::site()->prepare('SELECT author_id, status, deleted_at FROM articles WHERE id = :id LIMIT 1');
  13. $statement->execute(['id' => $articleId]);
  14. $article = $statement->fetch();
  15. ​
  16. if (!$article || $article['deleted_at'] !== null) {
  17. return ['We couldn’t find this article. It may have been deleted.', 'error', null];
  18. }
  19. ​
  20. $isOwner = (int) $article['author_id'] === (int) $currentUser['id'];
  21. if (!$isOwner && !Auth::hasRoleAtLeast(Auth::ROLE_EDITOR_IN_CHIEF)) {
  22. return ['Only the author and editors can restore older versions of this article.', 'error', null];
  23. }
  24. ​
  25. if (in_array($article['status'], ['published', 'scheduled'], true) && !Auth::canPublishDirectly()) {
  26. return ['Switch the article back to a draft first, then restore the older version.', 'error', null];
  27. }
  28. ​
  29. if (!ArticleVersions::restore($articleId, $versionId, (int) $currentUser['id'], (string) $currentUser['display_name'])) {
  30. return ['We couldn’t find that version.', 'error', null];
  31. }
  32. ​
  33. ActivityLog::record('article.restore_version', 'article', $articleId, 'version #' . $versionId);
  34. ​
  35. return ['Older version restored.', 'success', $articleId];
  36. ​