Update fast AJAX mu-plugin

This commit is contained in:
Tetrakern 2024-05-07 14:39:01 +02:00
parent 6bbf3a5208
commit b7f2fba0b4
6 changed files with 26 additions and 37 deletions

View File

@ -1 +1 @@
const fcn_commentSection=_$("#comments[data-ajax-comments]");function fcn_getCommentSection(e=null,t=null,n=null,o=!1){if(!fcn_commentSection)return;let c,m="",a=_$(fictioneer_comments.form_selector??"#comment");if(a&&(m=a.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),t||(t=fcn_urlParams.pg??1),n||(n=n??fcn_commentSection.dataset.order??"desc"),!fcn_commentSection)return;const r={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(t),corder:n};fcn_urlParams.commentcode&&(r.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(r).then((e=>{if(e.success){t=e.data.page,fcn_commentSection.dataset.page=t;const c=document.createElement("div");if(c.innerHTML=e.data.html,c.querySelector("#comment_post_ID")){c.querySelector("#comment_post_ID").value=e.data.postId,c.querySelector("#cancel-comment-reply-link").href="#respond";const t=c.querySelector(".logout-link");t&&(t.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=c.innerHTML,c.remove(),e.data.disabled||(a=_$(fictioneer_comments.form_selector??"#comment"),a.value=m,fcn_applyCommentStack(a)),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const r=location.hash.includes("#comment")?location.hash:".respond",s=document.querySelector(r)??_$$$("respond");o&&s.scrollIntoView({behavior:"smooth"});const i=window.location.protocol+"//"+window.location.host+window.location.pathname;let d="";fcn_urlParams.commentcode&&(d+=`?commentcode=${fcn_urlParams.commentcode}`),t>1&&(d+=d.length>1?`&pg=${t}`:`?pg=${t}`),"desc"!=n&&(d+=d.length>1?`&corder=${n}`:`?corder=${n}`),window.history.pushState({path:i},"",i+d+location.hash)}else c=fcn_buildErrorNotice(e.data.error)})).catch((e=>{c=fcn_buildErrorNotice(e)})).then((()=>{fcn_commentSection.classList.remove("ajax-in-progress"),c&&(fcn_commentSection.innerHTML="",fcn_commentSection.appendChild(c))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,null,!0)}function fcn_jumpToCommentPage(){const e=parseInt(window.prompt(fictioneer_tl.notification.enterPageNumber));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")&&(_$(fictioneer_comments.form_selector??"#comment")||(fct_commentSectionObserver.disconnect(),fcn_reloadCommentsPage()))}function fcn_toggleCommentOrder(e){const t=e.closest(".fictioneer-comments"),n="desc"==t.dataset.order;t.dataset.order=n?"asc":"desc",e.classList.toggle("_on",!n),e.classList.toggle("_off",n),fcn_reloadCommentsPage(t.dataset.page)}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("button[data-page-jump]"))return void fcn_jumpToCommentPage();const t=e.target.closest("button[data-page]");if(t)return void fcn_reloadCommentsPage(t.dataset.page);const n=e.target.closest("button[data-toggle-order]");n&&fcn_toggleCommentOrder(n)}));
const fcn_commentSection=_$("#comments[data-ajax-comments]");function fcn_getCommentSection(e=null,t=null,n=null,o=!1){if(!fcn_commentSection)return;let c,m="",a=_$(fictioneer_comments.form_selector??"#comment");if(a&&(m=a.value),fcn_commentSection.classList.contains("ajax-in-progress"))return;if(fcn_commentSection.classList.add("ajax-in-progress"),t||(t=fcn_urlParams.pg??1),n||(n=n??fcn_commentSection.dataset.order??"desc"),!fcn_commentSection)return;const r={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(t),corder:n,fcn_fast_comment_ajax:1};fcn_urlParams.commentcode&&(r.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(r).then((e=>{if(e.success){t=e.data.page,fcn_commentSection.dataset.page=t;const c=document.createElement("div");if(c.innerHTML=e.data.html,c.querySelector("#comment_post_ID")){c.querySelector("#comment_post_ID").value=e.data.postId,c.querySelector("#cancel-comment-reply-link").href="#respond";const t=c.querySelector(".logout-link");t&&(t.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=c.innerHTML,c.remove(),e.data.disabled||(a=_$(fictioneer_comments.form_selector??"#comment"),a.value=m,fcn_applyCommentStack(a)),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const r=location.hash.includes("#comment")?location.hash:".respond",s=document.querySelector(r)??_$$$("respond");o&&s.scrollIntoView({behavior:"smooth"});const i=window.location.protocol+"//"+window.location.host+window.location.pathname;let d="";fcn_urlParams.commentcode&&(d+=`?commentcode=${fcn_urlParams.commentcode}`),t>1&&(d+=d.length>1?`&pg=${t}`:`?pg=${t}`),"desc"!=n&&(d+=d.length>1?`&corder=${n}`:`?corder=${n}`),window.history.pushState({path:i},"",i+d+location.hash)}else c=fcn_buildErrorNotice(e.data.error)})).catch((e=>{c=fcn_buildErrorNotice(e)})).then((()=>{fcn_commentSection.classList.remove("ajax-in-progress"),c&&(fcn_commentSection.innerHTML="",fcn_commentSection.appendChild(c))}))}function fcn_reloadCommentsPage(e=null){fcn_getCommentSection(null,e,null,!0)}function fcn_jumpToCommentPage(){const e=parseInt(window.prompt(fictioneer_tl.notification.enterPageNumber));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")&&(_$(fictioneer_comments.form_selector??"#comment")||(fct_commentSectionObserver.disconnect(),fcn_reloadCommentsPage()))}function fcn_toggleCommentOrder(e){const t=e.closest(".fictioneer-comments"),n="desc"==t.dataset.order;t.dataset.order=n?"asc":"desc",e.classList.toggle("_on",!n),e.classList.toggle("_off",n),fcn_reloadCommentsPage(t.dataset.page)}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("button[data-page-jump]"))return void fcn_jumpToCommentPage();const t=e.target.closest("button[data-page]");if(t)return void fcn_reloadCommentsPage(t.dataset.page);const n=e.target.closest("button[data-toggle-order]");n&&fcn_toggleCommentOrder(n)}));

2
js/comments.min.js vendored

File diff suppressed because one or more lines are too long

6
js/complete.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
/**
* Plugin Name: Fictioneer Fast Requests
* Description: Skips plugins for faster requests.
* Version: 1.0
* Version: 1.1.0
* Author: Tetrakern
* Author URI: https://github.com/Tetrakern
* Donate link: https://ko-fi.com/tetrakern
@ -11,12 +11,7 @@
*/
// Check if AJAX request
if (
( defined( 'DOING_AJAX' ) && DOING_AJAX ) &&
isset( $_REQUEST['fcn_fast_ajax'] ) &&
strpos( $_REQUEST['action'] ?? '', 'fictioneer_ajax' ) === 0 &&
strpos( $_REQUEST['action'] ?? '', '_comment' ) === false
) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['fcn_fast_ajax'] ) ) {
add_filter( 'option_active_plugins', 'fictioneer_exclude_plugins' );
}
@ -31,6 +26,7 @@ if ( strpos( $request_uri, 'wp-json/fictioneer/' ) !== false ) {
* Filters the list of active plugins
*
* @since 1.0.0
* @since 1.1.0 - Made more generic.
*
* @param array $plugins An array of active plugin paths.
*
@ -40,27 +36,15 @@ if ( strpos( $request_uri, 'wp-json/fictioneer/' ) !== false ) {
function fictioneer_exclude_plugins( $plugins ) {
// Setup
$allow_list = array(
'plugin-name/plugin-name.php' // Example!
'advanced-database-cleaner/advanced-db-cleaner.php' // Example!
);
// Remove not allowed plugins
foreach ( $plugins as $index => $plugin ) {
if ( ! in_array( $plugin, $allow_list ) && strpos( $plugin, 'fictioneer' ) === false ) {
unset( $plugins[ $index ] );
}
}
// Continue filter
return $plugins;
// Filter and continue
return array_intersect( $plugins, $allow_list );
}
// Check if AJAX comment request
if (
get_option( 'fictioneer_enable_fast_ajax_comments' ) &&
( defined( 'DOING_AJAX' ) && DOING_AJAX ) &&
strpos( $_REQUEST['action'] ?? '', 'fictioneer_ajax' ) === 0 &&
strpos( $_REQUEST['action'] ?? '', '_comment' ) !== false
) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['fcn_fast_comment_ajax'] ) ) {
add_filter( 'option_active_plugins', 'fictioneer_exclude_plugins_while_commenting' );
}
@ -93,5 +77,3 @@ function fictioneer_exclude_plugins_while_commenting( $plugins ) {
// Filter and continue
return array_intersect( $plugins, $allow_list );
}
?>

View File

@ -68,7 +68,8 @@ function fcn_getCommentSection(post_id = null, page = null, order = null, scroll
'action': 'fictioneer_ajax_get_comment_section',
'post_id': post_id ?? fcn_commentSection.dataset.postId,
'page': parseInt(page),
'corder': order
'corder': order,
'fcn_fast_comment_ajax': 1
};
if (fcn_urlParams.commentcode) {

View File

@ -77,7 +77,8 @@ function fcn_moderateComment(id, operation) {
fcn_ajaxPost({
'action': 'fictioneer_ajax_moderate_comment',
'operation': operation,
'id': id
'id': id,
'fcn_fast_comment_ajax': 1
})
.then(response => {
if (response.success) {
@ -194,7 +195,8 @@ function fcn_flagComment(source) {
fcn_ajaxPost({
'action': 'fictioneer_ajax_report_comment',
'id': comment.dataset.id,
'dubious': reportButton.classList.contains('_dubious')
'dubious': reportButton.classList.contains('_dubious'),
'fcn_fast_comment_ajax': 1
})
.then(response => {
// Comment successfully reported?
@ -420,7 +422,8 @@ function fcn_bindAJAXCommentSubmit() {
'privacy_consent': privacy_consent?.checked ?? 0,
'unfiltered_html': _$$$('_wp_unfiltered_html_comment_disabled')?.value ?? '',
'depth': parent ? parseInt(parent.dataset.depth) + 1 : 1,
'fictioneer_comment_validator': jsValidator?.value ?? 0
'fictioneer_comment_validator': jsValidator?.value ?? 0,
'fcn_fast_comment_ajax': 1
};
// Optional payload
@ -647,7 +650,8 @@ function fcn_submitInlineCommentEdit(source) {
fcn_ajaxPost({
'action': 'fictioneer_ajax_edit_comment',
'comment_id': red.id.replace('comment-', ''),
'content': content
'content': content,
'fcn_fast_comment_ajax': 1
})
.then(response => {
// Comment successfully updated?
@ -843,7 +847,8 @@ function fcn_deleteMyComment(button) {
// Request
fcn_ajaxPost({
'action': 'fictioneer_ajax_delete_my_comment',
'comment_id': comment.dataset.id
'comment_id': comment.dataset.id,
'fcn_fast_comment_ajax': 1
})
.then(response => {
if (response.success) {
@ -919,7 +924,8 @@ function fcn_getCommentForm() {
// Request
fcn_ajaxGet({
'action': 'fictioneer_ajax_get_comment_form',
'post_id': _$$$('comments').dataset.postId
'post_id': _$$$('comments').dataset.postId,
'fcn_fast_comment_ajax': 1
})
.then(response => {
if (response.success) {