Fix AJAX comments when commenting disabled

This commit is contained in:
Tetrakern 2023-10-29 21:27:33 +01:00
parent a2f6c3ebbb
commit 0e7269bac0
4 changed files with 19 additions and 7 deletions

View File

@ -1439,7 +1439,7 @@ if ( ! function_exists( 'fictioneer_is_commenting_disabled' ) ) {
$story_id = fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
if ( $story_id ) {
return fictioneer_get_field( 'fictioneer_disable_commenting', $story_id );
return fictioneer_get_field( 'fictioneer_disable_commenting', $story_id ) == true;
}
}

View File

@ -150,6 +150,8 @@ function fictioneer_ajax_get_comment_section() {
} else {
comment_form( fictioneer_comment_form_args( [], $post_id ), $post_id );
}
} else {
echo '<div class="fictioneer-comments__disabled">' . __( 'Comments are disabled.', 'fictioneer' ) . '</div>';
}
// List
@ -191,7 +193,15 @@ function fictioneer_ajax_get_comment_section() {
$output = ob_get_clean();
// Return buffer
wp_send_json_success( array( 'html' => $output, 'postId' => $post_id, 'page' => $page, 'mustLogin' => $must_login ) );
wp_send_json_success(
array(
'html' => $output,
'postId' => $post_id,
'page' => $page,
'mustLogin' => $must_login,
'disabled' => fictioneer_is_commenting_disabled( $post_id )
)
);
}
if ( get_option( 'fictioneer_enable_ajax_comments' ) ) {

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(),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.ajaxNonce?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentSectionObserver()})):fcn_setupCommentSectionObserver(),fcn_theRoot.dataset.ajaxNonce?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_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.ajaxNonce?document.addEventListener("fcnAuthReady",(()=>{fcn_setupCommentSectionObserver()})):fcn_setupCommentSectionObserver(),fcn_theRoot.dataset.ajaxNonce?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)}));

View File

@ -96,11 +96,13 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
temp.remove();
// Append stored content (in case of pagination)
commentTextarea = _$$$('comment');
commentTextarea.value = commentText;
if (!response.data.disabled) {
commentTextarea = _$$$('comment');
commentTextarea.value = commentText;
// Append stack contents (if any)
fcn_applyCommentStack(commentTextarea);
// Append stack contents (if any)
fcn_applyCommentStack(commentTextarea);
}
// Bind events
fcn_addModerationEvents();