public function addCommentToPost(array $post, int $blogPostId, bool $published = false) : bool
{
$replyTo = isset($post['reply_to']) ? $this->checkCommentReplyTo((int) $post['reply_to'], $blogPostId) : 0;
// Enforce maximum comment reply depth.
if ($replyTo) {
$depth = $this->getCommentDepth($replyTo);
$conf = \Airship\LensFunctions\cabin_custom_config();
if ($depth + 1 > $conf['blog']['comments']['depth_max']) {
$replyTo = null;
}
}
if ($replyTo === 0) {
$replyTo = null;
}
if (!empty($post['author'])) {
$authors = $this->getAuthorsForUser($this->getActiveUserId());
if (!\in_array($post['author'], $authors)) {
$this->db->rollBack();
return false;
}
$author = $post['author'];
$metadata = null;
} else {
$author = null;
$metadata = \json_encode(['name' => $post['name'], 'email' => $post['email'], 'url' => $post['url']]);
}
// We're going to do this inside of a transaction:
$this->db->beginTransaction();
// Create the new comment:
$commentId = $this->db->insertGet('hull_blog_comments', ['blogpost' => $blogPostId, 'replyto' => $replyTo, 'author' => $author, 'approved' => $published ?? false, 'metadata' => $metadata], 'commentid');
if (!empty($commentId)) {
// Insert the comment
$this->db->insert('hull_blog_comment_versions', ['comment' => $commentId, 'approved' => $published ?? false, 'message' => $post['message']]);
// Get the unique ID for this blog post:
$unique = $this->getBlogPostUniqueId($blogPostId);
// Delete the cached entry:
$this->getCommentCache()->delete($unique);
// Hooray!
return $this->db->commit();
}
$this->db->rollBack();
return false;
}