designtopack/public/site/plugins/comments/routes/delete.php
2024-11-22 07:53:43 +01:00

46 lines
1.3 KiB
PHP

<?php
return [
'pattern' => '(:all)delete-comment.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
$page = page($data->location->page->uri);
$project = page($data->location->project->uri);
$file = $page->file($data->location->file->uuid);
$isReply = $data->parentId ?? false;
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
foreach ($comments as $key => &$comment) {
if ($isReply) {
if ($comment['id'] === $data->parentId) {
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]);
}
}
}
$comments = array_values($comments);
$newFile = $file->update([
'comments' => $comments
]);
echo json_encode(getFileData($newFile));
kirby()->user()->deleteNotification($project, $data->id);
exit;
}
];