Reworked success/fail status messages after comment insertion.

This commit is contained in:
Emidio Reggiani 2025-11-08 11:29:11 +01:00
commit 6a38bbabbb
4 changed files with 76 additions and 31 deletions

View file

@ -394,8 +394,12 @@ notify_new_comments = "Notify me of new comments in this thread"
post_reply = "Post Reply" post_reply = "Post Reply"
post_comment = "Post Comment" post_comment = "Post Comment"
reply = "Reply" reply = "Reply"
comment_submitted_success = "Your comment has been posted successfully!" comment_submission_success = "Your comment has been posted successfully!"
comment_submitted_moderation = "Your comment has been submitted and is awaiting moderation." comment_submission_moderation = "Your comment has been submitted and is awaiting moderation."
comment_submission_error = "There was an error submitting your comment. Please try again." comment_submission_error = "There was an error submitting your comment. Please try again."
comment_submission_error_shortname = "Name is required and must be at least 2 characters."
comment_submission_error_email = "Valid email is required."
comment_submission_error_short = "Comment is required and must be at least 3 characters."
comment_submission_error_spam = "SPAM detected!"
pending_comments = "Pending Comments" pending_comments = "Pending Comments"
level = "Level" level = "Level"

View file

@ -6065,14 +6065,13 @@ post('/comments/submit', function () {
$result = commentInsert($postId, $data); $result = commentInsert($postId, $data);
// Kept separate for future use
if ($result['success']) { if ($result['success']) {
// Redirect back to post with success anchor // Redirect back to post with success anchor
$redir = site_url() . $postId . '#comment-success'; $redir = site_url() . $postId . '#comment-status+' . $result['message'];
// $redir = site_url() . $postId . '#comment-' . $result['comment_id'];
} else { } else {
// Redirect back to post with error // Redirect back to post with error
$redir = site_url() . $postId . '#comment-error'; $redir = site_url() . $postId . '#comment-status+' . $result['message'][0];
// $redir = site_url() . $postId . '#comment-error?' . $result['message'];
} }
header("location: $redir"); header("location: $redir");

View file

@ -167,26 +167,8 @@ function displayCommentsSection($postId)
<h3><?php echo i18n("Comments"); ?></h3> <h3><?php echo i18n("Comments"); ?></h3>
</div> </div>
---> --->
<div class="comment-alert-status" id="comment-alert-status" style="display:none;">
<?php
// Show success/error messages
$hash = isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT) : '';
if ($hash === 'comment-success'):
?>
<div class="alert alert-success">
<?php
if (comments_config('comments.moderation') === 'true') {
echo i18n('Comment_submitted_moderation');
} else {
echo i18n('Comment_submitted_success');
}
?>
</div> </div>
<?php elseif ($hash === 'comment-error'): ?>
<div class="alert alert-danger">
<?php echo i18n('Comment_submission_error'); ?>
</div>
<?php endif; ?>
<?php displayComments($postId); ?> <?php displayComments($postId); ?>
@ -256,6 +238,66 @@ function displayCommentsSection($postId)
container.innerHTML = ''; container.innerHTML = '';
} }
} }
function handleCommentStatus() {
// Setting messages
const messages = {
comment_submission_success: "<?php echo i18n('comment_submission_success'); ?>",
comment_submission_moderation: "<?php echo i18n('comment_submission_moderation'); ?>",
comment_submission_error: "<?php echo i18n('comment_submission_error'); ?>",
comment_submission_error_shortname: "<?php echo i18n('comment_submission_error_shortname'); ?>",
comment_submission_error_email: "<?php echo i18n('comment_submission_error_email'); ?>",
comment_submission_error_short: "<?php echo i18n('comment_submission_error_short'); ?>",
comment_submission_error_spam: "<?php echo i18n('comment_submission_error_spam'); ?>"
};
// Get the hash in the URL
const hash = window.location.hash;
// Check if there's #comment-status
if (hash.startsWith('#comment-status')) {
// Get the part after +
const parts = hash.split('+');
if (parts.length > 1) {
const statusKey = parts[1];
const alertDiv = document.querySelector('.comment-alert-status');
if (alertDiv && messages[statusKey]) {
// Set message to display
alertDiv.textContent = messages[statusKey];
// Set div colors (classes) based on message type
if (statusKey.includes('error')) {
alertDiv.className = 'comment-alert-status comment-alert-status-error'
} else if (statusKey.includes('success')) {
alertDiv.className = 'comment-alert-status comment-alert-status-success'
} else if (statusKey.includes('moderation')) {
alertDiv.className = 'comment-alert-status comment-alert-status-warning'
}
// Showing status message div
alertDiv.style.display = 'block';
// Scroll to status message
document.getElementById('comments').scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
}
}
// Esegui la funzione quando il DOM è caricato
document.addEventListener('DOMContentLoaded', handleCommentStatus);
// Esegui anche quando l'hash cambia (se navighi sulla stessa pagina)
window.addEventListener('hashchange', handleCommentStatus);
</script> </script>
<?php <?php
} }

View file

@ -239,23 +239,23 @@ function validateComment($data)
// Validate name // Validate name
if (empty($data['name']) || strlen(trim($data['name'])) < 2) { if (empty($data['name']) || strlen(trim($data['name'])) < 2) {
$errors[] = 'Name is required and must be at least 2 characters'; $errors[] = 'comment_submission_error_shortname';
} }
// Validate email // Validate email
if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Valid email is required'; $errors[] = 'comment_submission_error_email';
} }
// Validate comment text // Validate comment text
if (empty($data['comment']) || strlen(trim($data['comment'])) < 3) { if (empty($data['comment']) || strlen(trim($data['comment'])) < 3) {
$errors[] = 'Comment is required and must be at least 3 characters'; $errors[] = 'comment_submission_error_short';
} }
// Validate honeypot (if enabled) // Validate honeypot (if enabled)
if (comments_config('comments.honeypot') === 'true') { if (comments_config('comments.honeypot') === 'true') {
if (!empty($data['website'])) { if (!empty($data['website'])) {
$errors[] = 'Spam detected'; $errors[] = 'comment_submission_error_spam';
} }
} }
@ -319,7 +319,7 @@ function commentInsert($postId, $data)
if (file_put_contents($file, $json, LOCK_EX) === false) { if (file_put_contents($file, $json, LOCK_EX) === false) {
return array( return array(
'success' => false, 'success' => false,
'message' => 'Failed to save comment' 'message' => 'comment_submission_error'
); );
} }
@ -329,7 +329,7 @@ function commentInsert($postId, $data)
return array( return array(
'success' => true, 'success' => true,
'comment_id' => $commentId, 'comment_id' => $commentId,
'message' => $comment['published'] ? 'Comment published successfully' : 'Comment submitted for moderation' 'message' => $comment['published'] ? 'comment_submission_success' : 'comment_submission_moderation'
); );
} }