WebOrbiton
v2.0.0.1

Publisium

42 lines · 2.0 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. $db = Database::site();
  6. $action = (string) ($_POST['action'] ?? '');
  7. $commentId = (int) ($_POST['comment_id'] ?? 0);
  8. ​
  9. if ($action === 'reply_comment' && $commentId > 0) {
  10. [$flashMessage, $flashType] = SiteComments::replyAsTeam($commentId, $currentUser, (string) ($_POST['reply_body'] ?? ''), $canModerate);
  11. if ($flashType === 'success') {
  12. ActivityLog::record('comments.reply', 'comment', $commentId);
  13. }
  14. return;
  15. }
  16. ​
  17. if (!$canModerate) {
  18. $flashMessage = 'Only editors can moderate comments.';
  19. $flashType = 'error';
  20. return;
  21. }
  22. ​
  23. if ($action === 'approve_comment' && $commentId > 0) {
  24. $db->prepare("UPDATE article_comments SET status = 'approved' WHERE id = :id")->execute(['id' => $commentId]);
  25. ActivityLog::record('comments.approve', 'comment', $commentId);
  26. $flashMessage = 'Comment approved. It’s visible on the site now.';
  27. } elseif ($action === 'hide_comment' && $commentId > 0) {
  28. $db->prepare("UPDATE article_comments SET status = 'pending' WHERE id = :id")->execute(['id' => $commentId]);
  29. ActivityLog::record('comments.hide', 'comment', $commentId);
  30. $flashMessage = 'Comment hidden. It’s back in the waiting list.';
  31. } elseif ($action === 'delete_comment' && $commentId > 0) {
  32. $db->prepare('DELETE FROM article_comments WHERE id = :id OR parent_id = :parent')->execute(['id' => $commentId, 'parent' => $commentId]);
  33. ActivityLog::record('comments.delete', 'comment', $commentId);
  34. $flashMessage = 'Comment deleted, together with its replies.';
  35. } elseif ($action === 'delete_pending_comments') {
  36. // Replies under a waiting comment would be left without a thread, so they go too.
  37. $db->exec("DELETE r FROM article_comments r JOIN article_comments p ON p.id = r.parent_id WHERE p.status = 'pending'");
  38. $deleted = $db->exec("DELETE FROM article_comments WHERE status = 'pending'");
  39. ActivityLog::record('comments.delete_pending', null, null, (string) $deleted . ' deleted');
  40. $flashMessage = 'All waiting comments were deleted.';
  41. }
  42. ​