Refactor comment moderation

This commit is contained in:
Tetrakern 2024-02-02 22:16:04 +01:00
parent 22e04cfce5
commit 13307a786f
8 changed files with 81 additions and 153 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -456,12 +456,12 @@ if ( ! function_exists( 'fictioneer_comment_mod_menu' ) ) {
<i class="fa-solid fa-gear mod-menu-toggle-icon"></i>
<div class="popup-menu hide-if-logged-out only-moderators _top _justify-right _fixed-position">
<?php if ( get_option( 'fictioneer_enable_ajax_comment_moderation' ) ) : ?>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="trash"><?php _e( 'Trash', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="spam"><?php _e( 'Spam', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="unapprove"><?php _e( 'Unapprove', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="approve"><?php _e( 'Approve', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="close"><?php _e( 'Close', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="open"><?php _e( 'Open', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="trash"><?php _e( 'Trash', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="spam"><?php _e( 'Spam', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="unapprove"><?php _e( 'Unapprove', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="approve"><?php _e( 'Approve', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="close"><?php _e( 'Close', 'fictioneer' ); ?></button>
<button data-id="<?php echo $comment_id; ?>" data-click="ajax-mod-action" data-action="open"><?php _e( 'Open', 'fictioneer' ); ?></button>
<?php else : ?>
<?php
echo fictioneer_get_comment_action_link( $comment_id, 'trash', '#comments' );
@ -474,39 +474,18 @@ if ( ! function_exists( 'fictioneer_comment_mod_menu' ) ) {
}
?>
<?php endif; ?>
<?php if ( get_option( 'fictioneer_enable_sticky_comments' ) ) : ?>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="sticky"><?php _e( 'Sticky', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="unsticky"><?php _e( 'Unsticky', 'fictioneer' ); ?></button>
<?php
if (
get_option( 'fictioneer_enable_sticky_comments' ) &&
get_option( 'fictioneer_enable_ajax_comment_moderation' )
) :
?>
<button data-click="ajax-mod-action" data-id="<?php echo $comment_id; ?>" data-action="sticky"><?php _e( 'Sticky', 'fictioneer' ); ?></button>
<button data-click="ajax-mod-action" data-id="<?php echo $comment_id; ?>" data-action="unsticky"><?php _e( 'Unsticky', 'fictioneer' ); ?></button>
<?php endif; ?>
<?php echo fictioneer_get_comment_action_link( $comment_id, 'edit' ); ?>
</div>
</div>
<div class="popup-menu hide-if-logged-out only-moderators hidden hide-on-ajax">
<?php if ( get_option( 'fictioneer_enable_ajax_comment_moderation' ) ) : ?>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="trash"><?php _e( 'Trash', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="spam"><?php _e( 'Spam', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="unapprove"><?php _e( 'Unapprove', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="approve"><?php _e( 'Approve', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="close"><?php _e( 'Close', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="open"><?php _e( 'Open', 'fictioneer' ); ?></button>
<?php else : ?>
<?php
echo fictioneer_get_comment_action_link( $comment_id, 'trash', '#comments' );
echo fictioneer_get_comment_action_link( $comment_id, 'spam', '#comments' );
if ( $comment->comment_approved == '0' ) {
echo fictioneer_get_comment_action_link( $comment_id, 'approve', get_comment_link( $comment_id ) );
} else {
echo fictioneer_get_comment_action_link( $comment_id, 'unapprove', get_comment_link( $comment_id ) );
}
?>
<?php endif; ?>
<?php if ( get_option( 'fictioneer_enable_sticky_comments' ) ) : ?>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="sticky"><?php _e( 'Sticky', 'fictioneer' ); ?></button>
<button class="button-ajax-moderate-comment" data-id="<?php echo $comment_id; ?>" data-action="unsticky"><?php _e( 'Unsticky', 'fictioneer' ); ?></button>
<?php endif; ?>
<?php echo fictioneer_get_comment_action_link( $comment_id, 'edit' ); ?>
</div>
<?php // <--- End HTML
echo ob_get_clean();
}

View File

@ -1 +1 @@
const fcn_commentSection=_$$$("comments");function fcn_getCommentSection(e=null,n=null,t=!1){if(!fcn_commentSection)return;let o,c="",m=_$$$("comment");if(m&&(c=m.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),n||(n=fcn_urlParams.pg??1),!fcn_commentSection)return;const a={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(n)};fcn_urlParams.commentcode&&(a.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(a).then((e=>{if(e.success){n=e.data.page;const o=document.createElement("div");if(o.innerHTML=e.data.html,o.querySelector("#comment_post_ID")){o.querySelector("#comment_post_ID").value=e.data.postId,o.querySelector("#cancel-comment-reply-link").href="#respond";const n=o.querySelector(".logout-link");n&&(n.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=o.innerHTML,o.remove(),e.data.disabled||(m=_$$$("comment"),m.value=c,fcn_applyCommentStack(m)),fcn_addModerationEvents(),fcn_addCommentMouseleaveEvents(),fcn_addTextareaEvents(),fcn_addCommentFormEvents(),fcn_addPrivateToggleEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const a=location.hash.includes("#comment")?location.hash:".respond",r=document.querySelector(a)??_$$$("respond");t&&r.scrollIntoView({behavior:"smooth"});const i=window.location.protocol+"//"+window.location.host+window.location.pathname;let s="";fcn_urlParams.commentcode&&(s+=`?commentcode=${fcn_urlParams.commentcode}`),n>1&&(s+=s.length>1?`&pg=${n}`:`?pg=${n}`),window.history.pushState({path:i},"",i+s+location.hash)}else o=fcn_buildErrorNotice(e.data.error)})).catch((e=>{o=fcn_buildErrorNotice(e)})).then((()=>{fcn_commentSection.classList.remove("ajax-in-progress"),o&&(fcn_commentSection.innerHTML="",fcn_commentSection.appendChild(o))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,!0)}function fcn_jumpToCommentPage(){const e=parseInt(window.prompt(_x("Enter page number:","Pagination jump prompt.","fictioneer")));e>0&&fcn_reloadCommentsPage(e)}var fct_commentSectionObserver;function fcn_setupCommentSectionObserver(){fct_commentSectionObserver=new IntersectionObserver((([e])=>{e.isIntersecting&&(fcn_getCommentSection(),fct_commentSectionObserver.disconnect())}),{rootMargin:"450px",threshold:1}),fcn_commentSection&&fct_commentSectionObserver.observe(fcn_commentSection)}function fcn_loadCommentEarly(){fcn_commentSection&&location.hash.includes("#comment")&&(_$$$("comment")||(fct_commentSectionObserver.disconnect(),fcn_reloadCommentsPage()))}fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentSectionObserver()})):fcn_setupCommentSectionObserver(),fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_loadCommentEarly()})):fcn_loadCommentEarly(),_$(".fictioneer-comments")?.addEventListener("click",(e=>{if(e.target.closest("[data-page-jump]"))return void fcn_jumpToCommentPage();const n=e.target.closest("[data-page]");n&&fcn_reloadCommentsPage(n.dataset.page)}));
const fcn_commentSection=_$$$("comments");function fcn_getCommentSection(e=null,n=null,t=!1){if(!fcn_commentSection)return;let o,c="",m=_$$$("comment");if(m&&(c=m.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),n||(n=fcn_urlParams.pg??1),!fcn_commentSection)return;const a={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(n)};fcn_urlParams.commentcode&&(a.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(a).then((e=>{if(e.success){n=e.data.page;const o=document.createElement("div");if(o.innerHTML=e.data.html,o.querySelector("#comment_post_ID")){o.querySelector("#comment_post_ID").value=e.data.postId,o.querySelector("#cancel-comment-reply-link").href="#respond";const n=o.querySelector(".logout-link");n&&(n.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=o.innerHTML,o.remove(),e.data.disabled||(m=_$$$("comment"),m.value=c,fcn_applyCommentStack(m)),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const a=location.hash.includes("#comment")?location.hash:".respond",r=document.querySelector(a)??_$$$("respond");t&&r.scrollIntoView({behavior:"smooth"});const i=window.location.protocol+"//"+window.location.host+window.location.pathname;let s="";fcn_urlParams.commentcode&&(s+=`?commentcode=${fcn_urlParams.commentcode}`),n>1&&(s+=s.length>1?`&pg=${n}`:`?pg=${n}`),window.history.pushState({path:i},"",i+s+location.hash)}else o=fcn_buildErrorNotice(e.data.error)})).catch((e=>{o=fcn_buildErrorNotice(e)})).then((()=>{fcn_commentSection.classList.remove("ajax-in-progress"),o&&(fcn_commentSection.innerHTML="",fcn_commentSection.appendChild(o))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,!0)}function fcn_jumpToCommentPage(){const e=parseInt(window.prompt(_x("Enter page number:","Pagination jump prompt.","fictioneer")));e>0&&fcn_reloadCommentsPage(e)}var fct_commentSectionObserver;function fcn_setupCommentSectionObserver(){fct_commentSectionObserver=new IntersectionObserver((([e])=>{e.isIntersecting&&(fcn_getCommentSection(),fct_commentSectionObserver.disconnect())}),{rootMargin:"450px",threshold:1}),fcn_commentSection&&fct_commentSectionObserver.observe(fcn_commentSection)}function fcn_loadCommentEarly(){fcn_commentSection&&location.hash.includes("#comment")&&(_$$$("comment")||(fct_commentSectionObserver.disconnect(),fcn_reloadCommentsPage()))}fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentSectionObserver()})):fcn_setupCommentSectionObserver(),fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_loadCommentEarly()})):fcn_loadCommentEarly(),_$(".fictioneer-comments")?.addEventListener("click",(e=>{if(e.target.closest("[data-page-jump]"))return void fcn_jumpToCommentPage();const n=e.target.closest("[data-page]");n&&fcn_reloadCommentsPage(n.dataset.page)}));

2
js/comments.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -105,11 +105,8 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
}
// Bind events
fcn_addModerationEvents();
fcn_addCommentMouseleaveEvents();
fcn_addTextareaEvents();
fcn_addCommentFormEvents();
fcn_addPrivateToggleEvents()
fcn_bindAJAXCommentSubmit();
// JS trap (if active)
@ -120,8 +117,8 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
fcn_revealDeleteButton();
// Scroll to top of comment section
const scrollTargetSelector = location.hash.includes('#comment') ? location.hash : '.respond',
scrollTarget = document.querySelector(scrollTargetSelector) ?? _$$$('respond');
const scrollTargetSelector = location.hash.includes('#comment') ? location.hash : '.respond';
const scrollTarget = document.querySelector(scrollTargetSelector) ?? _$$$('respond');
if (scroll) {
scrollTarget.scrollIntoView({ behavior: 'smooth' });

View File

@ -57,8 +57,8 @@ fcn_addJSTrap();
function fcn_moderateComment(id, operation) {
// Setup
const comment = _$$$(`comment-${id}`),
menuToggleIcon = comment.querySelector('.mod-menu-toggle-icon');
const comment = _$$$(`comment-${id}`);
const menuToggleIcon = comment.querySelector('.mod-menu-toggle-icon');
// Abort if another AJAX action is in progress
if (comment.classList.contains('ajax-in-progress')) {
@ -137,34 +137,12 @@ function fcn_moderateComment(id, operation) {
// Remove progress state
comment.classList.remove('ajax-in-progress');
// Close menu
if ( fcn_lastClicked ) fcn_lastClicked.classList.remove('last-clicked');
// Close mod menu
fcn_lastClicked?.classList.remove('last-clicked');
fcn_lastClicked = null;
});
}
/**
* Listen to clicks on moderation action.
*
* @since 4.7.0
*/
function fcn_addModerationEvents() {
_$$('.button-ajax-moderate-comment').forEach(element => {
element.addEventListener(
'click',
e => {
fcn_moderateComment(
e.currentTarget.dataset.id,
e.currentTarget.dataset.action
);
}
);
});
}
fcn_addModerationEvents();
/**
* Listen to mouseleave to close the moderation menu.
*
@ -175,13 +153,10 @@ function fcn_addCommentMouseleaveEvents() {
_$$('.fictioneer-comment__container').forEach(element => {
element.addEventListener(
'mouseleave',
e => {
if (fcn_lastClicked) {
fcn_lastClicked.classList.remove('last-clicked');
}
event => {
fcn_lastClicked?.classList.remove('last-clicked');
fcn_lastClicked = null;
e.stopPropagation();
event.stopPropagation();
}
);
});
@ -296,45 +271,9 @@ fcn_addCommentFormEvents();
function fcn_textareaAdjust(area) {
area.style.height = 'auto'; // Reset if lines are removed
area.style.height = area.scrollHeight + 'px';
area.style.height = `${area.scrollHeight}px`;
}
/**
* Listen for input on adaptive textareas.
*
* @since 4.7.0
*/
function fcn_addTextareaEvents() {
_$$('.adaptive-textarea').forEach(element => {
element.addEventListener(
'input',
e => {
fcn_textareaAdjust(e.currentTarget);
}
);
});
}
fcn_addTextareaEvents();
/**
* Listen for changes of the private toggle.
*
* @since 5.0.0
*/
function fcn_addPrivateToggleEvents() {
_$$$('fictioneer-private-comment-toggle')?.addEventListener(
'change',
e => {
_$$$('respond')?.classList.toggle('_private', e.currentTarget.checked);
}
);
}
fcn_addPrivateToggleEvents();
// =============================================================================
// THEME COMMENTS FORMATTING BUTTONS
// =============================================================================
@ -349,13 +288,13 @@ fcn_addPrivateToggleEvents();
*/
function fcn_wrapInTag(element, tag, options = {}) {
const href = options.href ? ' href="' + options.href + '" target="_blank" rel="nofollow noreferrer noopener"' : '',
brackets = options.shortcode ? ['[', ']'] : ['<', '>'],
start = element.selectionStart,
end = element.selectionEnd,
open = brackets[0] + tag + href + brackets[1],
close = brackets[0] + '/' + tag + brackets[1],
text = open + element.value.substring(start, end) + close;
const href = options.href ? ' href="' + options.href + '" target="_blank" rel="nofollow noreferrer noopener"' : '';
const brackets = options.shortcode ? ['[', ']'] : ['<', '>'];
const start = element.selectionStart;
const end = element.selectionEnd;
const open = brackets[0] + tag + href + brackets[1];
const close = brackets[0] + '/' + tag + brackets[1];
const text = open + element.value.substring(start, end) + close;
element.value = element.value.substring(0, start) + text + element.value.substring(end, element.value.length);
element.setSelectionRange((start + open.length), (end + open.length));
@ -427,22 +366,22 @@ function fcn_bindAJAXCommentSubmit() {
}
// Get comment form input
const form = e.currentTarget,
button = _$$$('submit'),
content = _$$$('comment'),
author = _$$$('author'),
email = _$$$('email'),
cookie_consent = _$$$('wp-comment-cookies-consent'),
privacy_consent = _$$$('fictioneer-privacy-policy-consent'),
jsValidator = _$$$('fictioneer-comment-validator'),
parentId = _$$$('comment_parent').value,
parent = _$$$(`comment-${parentId}`),
private_comment = _$$$('fictioneer-private-comment-toggle'),
notification = _$$$('fictioneer-comment-notification-toggle');
const form = e.currentTarget;
const button = _$$$('submit');
const content = _$$$('comment');
const author = _$$$('author');
const email = _$$$('email');
const cookie_consent = _$$$('wp-comment-cookies-consent');
const privacy_consent = _$$$('fictioneer-privacy-policy-consent');
const jsValidator = _$$$('fictioneer-comment-validator');
const parentId = _$$$('comment_parent').value;
const parent = _$$$(`comment-${parentId}`);
const private_comment = _$$$('fictioneer-private-comment-toggle');
const notification = _$$$('fictioneer-comment-notification-toggle');
let emailValidation = true,
contentValidation = true,
privacyValidation = true;
let emailValidation = true;
let contentValidation = true;
let privacyValidation = true;
// Validate content
contentValidation = content.value.length > 1;
@ -580,7 +519,6 @@ function fcn_bindAJAXCommentSubmit() {
}
// Bind events
fcn_addModerationEvents();
fcn_addCommentMouseleaveEvents();
// Clean-up form
@ -655,9 +593,9 @@ function fcn_triggerInlineCommentEdit(source) {
// Main comment container found...
if (red) {
const content = red.querySelector('.fictioneer-comment__content'),
edit = red.querySelector('.fictioneer-comment__edit'),
textarea = red.querySelector('.comment-inline-edit-content');
const content = red.querySelector('.fictioneer-comment__content');
const edit = red.querySelector('.fictioneer-comment__edit');
const textarea = red.querySelector('.comment-inline-edit-content');
// Append buttons
edit.appendChild(fcn_commentEditActionsTemplate.content.cloneNode(true));
@ -669,7 +607,7 @@ function fcn_triggerInlineCommentEdit(source) {
red.classList.add('_editing');
content.hidden = true;
edit.hidden = false;
textarea.style.height = textarea.scrollHeight + 'px';
textarea.style.height = `${textarea.scrollHeight}px`;
}
}
@ -682,9 +620,9 @@ function fcn_triggerInlineCommentEdit(source) {
function fcn_submitInlineCommentEdit(source) {
// Setup
const red = source.closest('.fictioneer-comment'), // Red makes it faster!
edit = red.querySelector('.fictioneer-comment__edit'),
content = red.querySelector('.comment-inline-edit-content').value;
const red = source.closest('.fictioneer-comment'); // Red makes it faster!
const edit = red.querySelector('.fictioneer-comment__edit');
const content = red.querySelector('.comment-inline-edit-content').value;
let editNote = red.querySelector('.fictioneer-comment__edit-note');
@ -987,9 +925,9 @@ function fcn_getCommentForm() {
temp.innerHTML = response.data.html;
// Get form elements
const commentPostId = temp.querySelector('#comment_post_ID'),
cancelReplyLink = temp.querySelector('#cancel-comment-reply-link'),
logoutLink = temp.querySelector('.logout-link');
const commentPostId = temp.querySelector('#comment_post_ID');
const cancelReplyLink = temp.querySelector('#cancel-comment-reply-link');
const logoutLink = temp.querySelector('.logout-link');
// Fix form elements
if (commentPostId) {
@ -1012,9 +950,7 @@ function fcn_getCommentForm() {
fcn_applyCommentStack();
// Bind events
fcn_addTextareaEvents();
fcn_addCommentFormEvents();
fcn_addPrivateToggleEvents()
if (fcn_theRoot.dataset.ajaxSubmit) {
fcn_bindAJAXCommentSubmit();
@ -1075,15 +1011,15 @@ function fcn_applyCommentStack(textarea = null) {
// EVENT DELEGATES
// =============================================================================
// CLICK
_$('.fictioneer-comments')?.addEventListener('click', event => {
const clickTarget = event.target.closest('[data-click]'),
clickAction = clickTarget?.dataset.click;
const clickTarget = event.target.closest('[data-click]');
if (!clickAction) {
if (!clickTarget) {
return;
}
switch (clickAction) {
switch (clickTarget?.dataset.click) {
case 'submit-inline-comment-edit':
fcn_submitInlineCommentEdit(clickTarget);
break;
@ -1099,5 +1035,21 @@ _$('.fictioneer-comments')?.addEventListener('click', event => {
case 'flag-comment':
fcn_flagComment(clickTarget);
break;
case 'ajax-mod-action':
fcn_moderateComment(clickTarget.dataset.id, clickTarget.dataset.action);
break;
}
});
// INPUT
_$('.fictioneer-comments')?.addEventListener('input', event => {
// Adaptive textareas
if (event.target.matches('.adaptive-textarea')) {
fcn_textareaAdjust(event.target);
}
// Private comment toggle
if (event.target.closest('#fictioneer-private-comment-toggle')) {
_$$$('respond')?.classList.toggle('_private', event.currentTarget.checked);
}
});

View File

@ -36,7 +36,7 @@ body:not(.is-moderator):not(.is-admin):not(.is-post-author) {
}
body:not(.is-moderator):not(.is-admin) {
:is(.comment-edit-link, .button-ajax-moderate-comment[data-action="trash"]) {
:is(.comment-edit-link, [data-action="trash"]) {
display: none;
}
}