Fix AJAX auth not always working
This commit is contained in:
parent
9e3d32794d
commit
a2ab9585b6
@ -1266,7 +1266,9 @@ function fictioneer_sanitize_css( $css ) {
|
||||
*/
|
||||
|
||||
function fictioneer_show_auth_content() {
|
||||
return is_user_logged_in() || ! empty( get_option( 'fictioneer_enable_public_cache_compatibility' ) );
|
||||
return is_user_logged_in() ||
|
||||
get_option( 'fictioneer_enable_public_cache_compatibility' ) ||
|
||||
get_option( 'fictioneer_enable_ajax_authentication' );
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
2
js/ajax-bookshelf.min.js
vendored
2
js/ajax-bookshelf.min.js
vendored
@ -1 +1 @@
|
||||
const fcn_bookshelfTarget=_$$$("ajax-bookshelf-target");function fcn_getBookshelfContent(){return fcn_parseJSON(localStorage.getItem("fcnBookshelfContent"))??{html:{},count:{}}}function fcn_updateBookshelfView(e=null,t=null,o=null,n=!1){let a=fcn_getBookshelfContent();const s=(e=e??fcn_bookshelfTarget.dataset.action)+(t=t??fcn_bookshelfTarget.dataset.page)+(o=o??fcn_bookshelfTarget.dataset.order);if(!a.hasOwnProperty("timestamp")||a.timestamp+6e4<Date.now())return localStorage.removeItem("fcnBookshelfContent"),a={html:{},count:{}},void fcn_fetchBookshelfPart(e,t,o,n);a.hasOwnProperty("html")&&a.html.hasOwnProperty(s)?(fcn_bookshelfTarget.innerHTML=a.html[s],fcn_bookshelfTarget.classList.remove("ajax-in-progress"),fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${a.count[e]})`,n&&_$$$("main").scrollIntoView({behavior:"smooth"})):fcn_fetchBookshelfPart(e,t,o,n)}function fcn_browseBookshelfPage(e){fcn_bookshelfTarget.classList.add("ajax-in-progress"),fcn_updateBookshelfView(null,e,null,!0),history.pushState({},"",fcn_buildUrl({tab:fcn_bookshelfTarget.dataset.tab,pg:e,order:fcn_bookshelfTarget.dataset.order}).href)}function fcn_fetchBookshelfPart(e,t,o,n=!1){const a=e+t+o,s=fcn_getBookshelfContent();fcn_ajaxGet({action:e,fcn_fast_ajax:1,page:t,order:o}).then((o=>{o.success?(s.timestamp=Date.now(),s.html[a]=o.data.html,s.count[e]=o.data.count,localStorage.setItem("fcnBookshelfContent",JSON.stringify(s)),fcn_bookshelfTarget.innerHTML=o.data.html,fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${o.data.count})`):(fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(o.data.error)))})).catch((e=>{_$(".item-number").innerHTML="",fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`))})).then((()=>{fcn_bookshelfTarget.classList.remove("ajax-in-progress"),n&&_$$$("main").scrollIntoView({behavior:"smooth"})}))}fcn_theRoot.dataset.ajaxNonce?document.addEventListener("fcnAuthReady",(()=>{fcn_updateBookshelfView()})):fcn_updateBookshelfView(),_$(".bookshelf__list")?.addEventListener("click",(e=>{const t=e.target.closest(".page-numbers[data-page]");t&&fcn_browseBookshelfPage(t.dataset.page)}));
|
||||
const fcn_bookshelfTarget=_$$$("ajax-bookshelf-target");function fcn_getBookshelfContent(){return fcn_parseJSON(localStorage.getItem("fcnBookshelfContent"))??{html:{},count:{}}}function fcn_updateBookshelfView(e=null,t=null,o=null,n=!1){let a=fcn_getBookshelfContent();const s=(e=e??fcn_bookshelfTarget.dataset.action)+(t=t??fcn_bookshelfTarget.dataset.page)+(o=o??fcn_bookshelfTarget.dataset.order);if(!a.hasOwnProperty("timestamp")||a.timestamp+6e4<Date.now())return localStorage.removeItem("fcnBookshelfContent"),a={html:{},count:{}},void fcn_fetchBookshelfPart(e,t,o,n);a.hasOwnProperty("html")&&a.html.hasOwnProperty(s)?(fcn_bookshelfTarget.innerHTML=a.html[s],fcn_bookshelfTarget.classList.remove("ajax-in-progress"),fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${a.count[e]})`,n&&_$$$("main").scrollIntoView({behavior:"smooth"})):fcn_fetchBookshelfPart(e,t,o,n)}function fcn_browseBookshelfPage(e){fcn_bookshelfTarget.classList.add("ajax-in-progress"),fcn_updateBookshelfView(null,e,null,!0),history.pushState({},"",fcn_buildUrl({tab:fcn_bookshelfTarget.dataset.tab,pg:e,order:fcn_bookshelfTarget.dataset.order}).href)}function fcn_fetchBookshelfPart(e,t,o,n=!1){const a=e+t+o,s=fcn_getBookshelfContent();fcn_ajaxGet({action:e,fcn_fast_ajax:1,page:t,order:o}).then((o=>{o.success?(s.timestamp=Date.now(),s.html[a]=o.data.html,s.count[e]=o.data.count,localStorage.setItem("fcnBookshelfContent",JSON.stringify(s)),fcn_bookshelfTarget.innerHTML=o.data.html,fcn_bookshelfTarget.dataset.page=t,_$(".item-number").innerHTML=`(${o.data.count})`):(fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(o.data.error)))})).catch((e=>{_$(".item-number").innerHTML="",fcn_bookshelfTarget.innerHTML="",fcn_bookshelfTarget.appendChild(fcn_buildErrorNotice(`${e.status}: ${e.statusText}`))})).then((()=>{fcn_bookshelfTarget.classList.remove("ajax-in-progress"),n&&_$$$("main").scrollIntoView({behavior:"smooth"})}))}fcn_theRoot.dataset.ajaxAuth?document.addEventListener("fcnAuthReady",(()=>{fcn_updateBookshelfView()})):fcn_updateBookshelfView(),_$(".bookshelf__list")?.addEventListener("click",(e=>{const t=e.target.closest(".page-numbers[data-page]");t&&fcn_browseBookshelfPage(t.dataset.page)}));
|
2
js/ajax-comments.min.js
vendored
2
js/ajax-comments.min.js
vendored
@ -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.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.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
2
js/comments.min.js
vendored
File diff suppressed because one or more lines are too long
2
js/user.min.js
vendored
2
js/user.min.js
vendored
@ -1 +1 @@
|
||||
function fcn_replaceProfileImage(e,t){const a=e.querySelector(".user-icon");if(a){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=t,a.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,t=!0){e&&fcn_isValidUrl(e)&&(t&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((t=>{fcn_replaceProfileImage(t,e)})),!1===fcn_getUserData().loggedIn&&fcn_prepareLogin())}function fcn_getProfileImage(){let e=localStorage.getItem("fcnProfileAvatar");fcn_isLoggedIn?(fcn_isValidUrl(e)||(e=!1),e?fcn_setProfileImage(e):fcn_getUserAvatar()):localStorage.removeItem("fcnProfileAvatar")}function fcn_getUserAvatar(){fcn_ajaxGet({action:"fictioneer_ajax_get_avatar",fcn_fast_ajax:1}).then((e=>{e.success&&fcn_setProfileImage(e.data.url)})).catch((()=>{fcn_theRoot.dataset.defaultAvatar&&fcn_setProfileImage(fcn_theRoot.dataset.defaultAvatar,!1)}))}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){let e=fcn_getUserData();if(fcn_isLoggedIn&&!1===e.loggedIn&&(fcn_prepareLogin(),e=fcn_getUserData()),fcn_ajaxLimitThreshold<e.lastLoaded||!1===e.loggedIn)if(e.loggedIn){const t=new CustomEvent("fcnUserDataReady",{detail:{data:e,time:new Date},bubbles:!1,cancelable:!0});document.dispatchEvent(t)}else{const t=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}else fcn_ajaxGet({action:"fictioneer_ajax_get_user_data",fcn_fast_ajax:1}).then((e=>{if(e.success){let t=fcn_getUserData();t=e.data,t.lastLoaded=Date.now(),fcn_setUserData(t);const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}else{const t=fcn_getUserData();t.lastLoaded=Date.now(),t.loggedIn=!1,fcn_setUserData(t);const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData");const t=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}))}document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_getProfileImage()})),document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_fetchUserData()}));
|
||||
function fcn_replaceProfileImage(e,t){const a=e.querySelector(".user-icon");if(a){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=t,a.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,t=!0){e&&fcn_isValidUrl(e)&&(t&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((t=>{fcn_replaceProfileImage(t,e)})),!1===fcn_getUserData().loggedIn&&fcn_prepareLogin())}function fcn_getProfileImage(){let e=localStorage.getItem("fcnProfileAvatar");fcn_isLoggedIn?(fcn_isValidUrl(e)||(e=!1),e?fcn_setProfileImage(e):fcn_getUserAvatar()):localStorage.removeItem("fcnProfileAvatar")}function fcn_getUserAvatar(){fcn_ajaxGet({action:"fictioneer_ajax_get_avatar",fcn_fast_ajax:1}).then((e=>{e.success&&fcn_setProfileImage(e.data.url)})).catch((()=>{fcn_theRoot.dataset.defaultAvatar&&fcn_setProfileImage(fcn_theRoot.dataset.defaultAvatar,!1)}))}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){let e=fcn_getUserData();if(fcn_isLoggedIn&&!1===e.loggedIn&&(fcn_prepareLogin(),e=fcn_getUserData()),fcn_ajaxLimitThreshold<e.lastLoaded||!1===e.loggedIn)if(e.loggedIn){const t=new CustomEvent("fcnUserDataReady",{detail:{data:e,time:new Date},bubbles:!1,cancelable:!0});document.dispatchEvent(t)}else{const t=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}else fcn_ajaxGet({action:"fictioneer_ajax_get_user_data",fcn_fast_ajax:1}).then((e=>{if(e.success){let t=fcn_getUserData();t=e.data,t.lastLoaded=Date.now(),fcn_setUserData(t);const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}else{const t=fcn_getUserData();t.lastLoaded=Date.now(),t.loggedIn=!1,fcn_setUserData(t);const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData");const t=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}))}document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_getProfileImage()})),fcn_theRoot.dataset.ajaxAuth&&document.addEventListener("fcnAuthReady",(()=>{fcn_getProfileImage()})),document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_fetchUserData()}));
|
@ -5,7 +5,7 @@
|
||||
const fcn_bookshelfTarget = _$$$('ajax-bookshelf-target');
|
||||
|
||||
// Initialize
|
||||
if (fcn_theRoot.dataset.ajaxNonce) {
|
||||
if (fcn_theRoot.dataset.ajaxAuth) {
|
||||
document.addEventListener('fcnAuthReady', () => {
|
||||
fcn_updateBookshelfView();
|
||||
});
|
||||
|
@ -191,8 +191,8 @@ function fcn_jumpToCommentPage() {
|
||||
|
||||
var /** @type {IntersectionObserver} */ fct_commentSectionObserver;
|
||||
|
||||
// In case of AJAx authentication...
|
||||
if (fcn_theRoot.dataset.ajaxNonce) {
|
||||
// In case of AJAX authentication...
|
||||
if (fcn_theRoot.dataset.ajaxAuth) {
|
||||
document.addEventListener('fcnAuthReady', () => {
|
||||
fcn_setupCommentSectionObserver();
|
||||
});
|
||||
@ -227,8 +227,8 @@ function fcn_setupCommentSectionObserver() {
|
||||
// SCROLL NEW COMMENT INTO VIEW SUBMITTING VIA RELOAD
|
||||
// =============================================================================
|
||||
|
||||
// In case of AJAx authentication...
|
||||
if (fcn_theRoot.dataset.ajaxNonce) {
|
||||
// In case of AJAX authentication...
|
||||
if (fcn_theRoot.dataset.ajaxAuth) {
|
||||
document.addEventListener('fcnAuthReady', () => {
|
||||
fcn_loadCommentEarly();
|
||||
});
|
||||
|
@ -933,8 +933,8 @@ const /** @const {HTMLElement} */ fcn_ajaxCommentForm = _$$$('ajax-comment-form-
|
||||
|
||||
// Check whether form target exists...
|
||||
if (fcn_ajaxCommentForm) {
|
||||
// In case of AJAx authentication...
|
||||
if (fcn_theRoot.dataset.ajaxNonce) {
|
||||
// In case of AJAX authentication...
|
||||
if (fcn_theRoot.dataset.ajaxAuth) {
|
||||
// Load after nonce has been fetched
|
||||
document.addEventListener('fcnAuthReady', () => {
|
||||
fcn_setupCommentFormObserver();
|
||||
|
@ -127,6 +127,12 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
}
|
||||
});
|
||||
|
||||
if (fcn_theRoot.dataset.ajaxAuth) {
|
||||
document.addEventListener('fcnAuthReady', () => {
|
||||
fcn_getProfileImage();
|
||||
});
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// FETCH RELEVANT USER DATA
|
||||
// =============================================================================
|
||||
|
Loading…
x
Reference in New Issue
Block a user