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

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

View file

@ -167,26 +167,8 @@ function displayCommentsSection($postId)
<h3><?php echo i18n("Comments"); ?></h3>
</div>
--->
<?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 class="comment-alert-status" id="comment-alert-status" style="display:none;">
</div>
<?php elseif ($hash === 'comment-error'): ?>
<div class="alert alert-danger">
<?php echo i18n('Comment_submission_error'); ?>
</div>
<?php endif; ?>
<?php displayComments($postId); ?>
@ -256,6 +238,66 @@ function displayCommentsSection($postId)
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>
<?php
}

View file

@ -239,23 +239,23 @@ function validateComment($data)
// Validate name
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
if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Valid email is required';
$errors[] = 'comment_submission_error_email';
}
// Validate comment text
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)
if (comments_config('comments.honeypot') === 'true') {
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) {
return array(
'success' => false,
'message' => 'Failed to save comment'
'message' => 'comment_submission_error'
);
}
@ -329,7 +329,7 @@ function commentInsert($postId, $data)
return array(
'success' => true,
'comment_id' => $commentId,
'message' => $comment['published'] ? 'Comment published successfully' : 'Comment submitted for moderation'
'message' => $comment['published'] ? 'comment_submission_success' : 'comment_submission_moderation'
);
}