getMessage(), 1); } } } function deleteDir(string $dirPath): void { if (!is_dir($dirPath)) { throw new InvalidArgumentException("$dirPath must be a directory"); } if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { $dirPath .= '/'; } $files = glob($dirPath . '*', GLOB_MARK); foreach ($files as $file) { if (is_dir($file)) { deleteDir($file); } else { unlink($file); } } rmdir($dirPath); } function setFiles($location) { $destination = __DIR__ . '/../../../../assets/front-comments'; if ($location === 'media') { if (is_dir($destination)) { deleteDir($destination); } } else { $source = __DIR__ . '/../assets'; if (!is_dir($destination)) { mkdir($destination, 0755, true); } rec_copy($source, $destination); } } function getCommentData($comments, $commentId) { $comment = array_filter( $comments, function ($item) use ($commentId) { return $item['id'] == $commentId; } ); $comment = current($comment); return $comment; } function deleteComment($pageUri, $commentId) { $page = Find::page($pageUri); $comments = $page->comments()->toData('yaml'); $newComments = []; foreach ($comments as $item) { $comment = new Comment($item, $page); if ($comment->id() == $commentId) { if ($comment->hasIssue()) { $comment->closeIssue(); } } else { $newComments[] = $comment->data(); } } $newPage = $page->update( [ 'comments' => $newComments ] ); return $newPage->comments()->toData('yaml'); } function cacheFrontComments($newPage) { if ($newPage->comments()->exists() && $newPage->comments()->isNotEmpty()) { $cache = kirby()->cache('adrienpayet.front-comments'); $comments = $cache->getOrSet('commented-pages', function () { return []; }); $comments[$newPage->uuid()->id()] = [ 'uri' => $newPage->uri(), 'title' => $newPage->title()->value(), 'url' => $newPage->url(), 'comments' => $newPage->comments()->yaml(), ]; $cache->set('commented-pages', $comments); } }