Add custom order param to comments query
This commit is contained in:
parent
8ea32feec2
commit
fdab283909
10
comments.php
10
comments.php
@ -21,12 +21,14 @@ if ( post_password_required() ) {
|
||||
$post_id = get_the_ID();
|
||||
$user = wp_get_current_user();
|
||||
$comments_count = get_comments_number();
|
||||
$order = array_intersect( [ strtolower( $_GET['comments-order'] ?? 0 ) ], ['desc', 'asc'] );
|
||||
$order = reset( $order ) ?: get_option( 'comment_order' ); // Sanitized
|
||||
$logout_url = fictioneer_get_logout_url( get_permalink() );
|
||||
$is_ajax_comments = get_option( 'fictioneer_enable_ajax_comments' );
|
||||
|
||||
?>
|
||||
|
||||
<div id="comments" class="fictioneer-comments scroll-margin-top" data-post-id="<?php echo $post_id; ?>" data-logout-url="<?php echo esc_url( $logout_url ); ?>" <?php echo $is_ajax_comments ? 'data-ajax-comments' : ''; ?>>
|
||||
<div id="comments" class="fictioneer-comments scroll-margin-top" data-post-id="<?php echo $post_id; ?>" data-order="<?php echo $order; ?>" data-logout-url="<?php echo esc_url( $logout_url ); ?>" <?php echo $is_ajax_comments ? 'data-ajax-comments' : ''; ?>>
|
||||
|
||||
<?php
|
||||
|
||||
@ -34,11 +36,13 @@ $is_ajax_comments = get_option( 'fictioneer_enable_ajax_comments' );
|
||||
fictioneer_comments_ajax_skeleton( $comments_count ); // AJAX loading skeleton
|
||||
} else {
|
||||
// Query arguments
|
||||
$query_args = array( 'post_id' => $post_id );
|
||||
$query_args = array(
|
||||
'post_id' => $post_id,
|
||||
'order' => $order
|
||||
);
|
||||
|
||||
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
|
||||
$query_args['type'] = ['comment', 'private'];
|
||||
$query_args['order'] = get_option( 'comment_order' );
|
||||
} else {
|
||||
$query_args['type'] = ['comment'];
|
||||
}
|
||||
|
@ -98,6 +98,8 @@ function fictioneer_ajax_get_comment_section() {
|
||||
$post_id = absint( $_GET['post_id'] );
|
||||
$post = get_post( $post_id ); // Called later anyway; no performance loss
|
||||
$page = absint( $_GET['page'] ?? 1 ) ?: 1;
|
||||
$order = array_intersect( [ strtolower( $_GET['order'] ?? 0 ) ], ['desc', 'asc'] );
|
||||
$order = reset( $order ) ?: get_option( 'comment_order' ); // Sanitized
|
||||
$commentcode = ( $_GET['commentcode'] ?? 0 ) ?: false;
|
||||
$must_login = get_option( 'comment_registration' ) && ! is_user_logged_in();
|
||||
|
||||
@ -121,7 +123,7 @@ function fictioneer_ajax_get_comment_section() {
|
||||
|
||||
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
|
||||
$query_args['type'] = ['comment', 'private'];
|
||||
$query_args['order'] = get_option( 'comment_order' );
|
||||
$query_args['order'] = $order;
|
||||
} else {
|
||||
// Still hide private comments but do not limit the types preemptively
|
||||
$query_args = array( 'type__not_in' => 'private' );
|
||||
@ -166,7 +168,8 @@ function fictioneer_ajax_get_comment_section() {
|
||||
array(
|
||||
'commentcode' => $commentcode,
|
||||
'post_author_id' => $post->post_author,
|
||||
'post_id' => $post_id
|
||||
'post_id' => $post_id,
|
||||
'order' => $order
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -209,7 +209,7 @@ if ( ! function_exists( 'fictioneer_ajax_list_comments' ) ) {
|
||||
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
|
||||
$list_args['page'] = $page;
|
||||
$list_args['reverse_top_level'] = false;
|
||||
$list_args['reverse_children'] = true;
|
||||
$list_args['reverse_children'] = ( $args['order'] ?? 0 ) === 'desc';
|
||||
}
|
||||
|
||||
if ( ! get_option( 'fictioneer_disable_comment_callback' ) ) {
|
||||
@ -240,6 +240,8 @@ if ( ! function_exists( 'fictioneer_ajax_list_comments' ) ) {
|
||||
function fictioneer_comment_list_args( $parsed_args ) {
|
||||
// Setup
|
||||
$page = get_query_var( 'cpage', 1 );
|
||||
$order = array_intersect( [ strtolower( $_GET['comments-order'] ?? 0 ) ], ['desc', 'asc'] );
|
||||
$order = reset( $order ) ?: get_option( 'comment_order' ); // Sanitized
|
||||
|
||||
// Build arguments
|
||||
$list_args = array(
|
||||
@ -255,7 +257,7 @@ function fictioneer_comment_list_args( $parsed_args ) {
|
||||
if ( ! get_option( 'fictioneer_disable_comment_query' ) ) {
|
||||
$list_args['page'] = $page;
|
||||
$list_args['reverse_top_level'] = false;
|
||||
$list_args['reverse_children'] = true;
|
||||
$list_args['reverse_children'] = $order === 'desc';
|
||||
}
|
||||
|
||||
if ( ! get_option( 'fictioneer_disable_comment_callback' ) ) {
|
||||
|
2
js/ajax-comments.min.js
vendored
2
js/ajax-comments.min.js
vendored
@ -1 +1 @@
|
||||
const fcn_commentSection=_$("#comments[data-ajax-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(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")&&(_$$$("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[data-ajax-comments]");function fcn_getCommentSection(e=null,n=null,t=null,o=!1){if(!fcn_commentSection)return;let c,m="",a=_$$$("comment");if(a&&(m=a.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 r={action:"fictioneer_ajax_get_comment_section",post_id:e??fcn_commentSection.dataset.postId,page:parseInt(n),order:t??fcn_commentSection.dataset.order??0};fcn_urlParams.commentcode&&(r.commentcode=fcn_urlParams.commentcode),fcn_ajaxGet(r).then((e=>{if(e.success){n=e.data.page;const t=document.createElement("div");if(t.innerHTML=e.data.html,t.querySelector("#comment_post_ID")){t.querySelector("#comment_post_ID").value=e.data.postId,t.querySelector("#cancel-comment-reply-link").href="#respond";const n=t.querySelector(".logout-link");n&&(n.href=fcn_commentSection.dataset.logoutUrl)}fcn_commentSection.innerHTML=t.innerHTML,t.remove(),e.data.disabled||(a=_$$$("comment"),a.value=m,fcn_applyCommentStack(a)),fcn_addCommentMouseleaveEvents(),fcn_addCommentFormEvents(),fcn_bindAJAXCommentSubmit(),fcn_addJSTrap(),fcn_revealEditButton(),fcn_revealDeleteButton();const c=location.hash.includes("#comment")?location.hash:".respond",r=document.querySelector(c)??_$$$("respond");o&&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 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")&&(_$$$("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/complete.min.js
vendored
2
js/complete.min.js
vendored
File diff suppressed because one or more lines are too long
@ -20,9 +20,11 @@ const /** @const {HTMLElement} */ fcn_commentSection = _$('#comments[data-ajax-c
|
||||
* @since 5.0.0
|
||||
* @param {Number=} post_id - The current post ID.
|
||||
* @param {Number=} page - The requested page number.
|
||||
* @param {String=} order - Order of the comments.
|
||||
* @param {Boolean=} scroll - Whether to scroll comments into view. Default false.
|
||||
*/
|
||||
|
||||
function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
|
||||
function fcn_getCommentSection(post_id = null, page = null, order = null, scroll = false) {
|
||||
//Abort conditions...
|
||||
if (!fcn_commentSection) {
|
||||
return;
|
||||
@ -60,7 +62,8 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
|
||||
const payload = {
|
||||
'action': 'fictioneer_ajax_get_comment_section',
|
||||
'post_id': post_id ?? fcn_commentSection.dataset.postId,
|
||||
'page': parseInt(page)
|
||||
'page': parseInt(page),
|
||||
'order': order ?? fcn_commentSection.dataset.order ?? 0
|
||||
};
|
||||
|
||||
if (fcn_urlParams.commentcode) {
|
||||
@ -169,7 +172,7 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
|
||||
*/
|
||||
|
||||
function fcn_reloadCommentsPage(page = null) {
|
||||
fcn_getCommentSection(null, page, true);
|
||||
fcn_getCommentSection(null, page, null, true);
|
||||
}
|
||||
|
||||
function fcn_jumpToCommentPage() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user