designtopack/public/site/plugins/comments/routes/delete.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2024-10-23 11:32:51 +02:00
<?php
return [
'pattern' => '(:all)delete-comment.json',
'method' => 'POST',
2024-10-23 11:32:51 +02:00
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
// kirby()->impersonate('kirby');
2024-10-23 11:32:51 +02:00
$page = page($data->location->page->uri);
$file = $page->file($data->location->file->uuid);
2024-10-28 16:52:59 +01:00
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
2024-10-23 11:32:51 +02:00
2025-01-15 14:18:48 +01:00
$isReply = $data->location->parent->id ?? false;
2024-10-30 16:32:13 +01:00
foreach ($comments as $key => &$comment) {
if ($isReply) {
if ($comment['id'] === $data->location->parent->id) {
foreach ($comment['replies'] as $replyKey => $reply) {
if ($reply['id'] === $data->id) {
unset($comment['replies'][$replyKey]);
$comment['replies'] = array_values($comment['replies']);
}
}
}
} else {
if ($comment['id'] === $data->id) {
unset($comments[$key]);
2024-10-30 16:32:13 +01:00
}
}
}
2024-11-17 12:01:20 +01:00
$comments = array_values($comments);
2024-10-30 16:32:13 +01:00
2024-10-23 11:32:51 +02:00
$newFile = $file->update([
'comments' => $comments,
2024-10-23 11:32:51 +02:00
]);
2024-11-17 12:01:20 +01:00
echo json_encode(getFileData($newFile));
// Note: Les notifications sont maintenant dérivées des commentaires.
// La suppression du commentaire supprime automatiquement la notification.
2024-11-17 12:01:20 +01:00
exit;
},
2024-10-30 16:32:13 +01:00
];