Option to use fast AJAX for comments

I cannot believe this just works...
This commit is contained in:
Tetrakern 2023-08-23 21:43:04 +02:00
parent 22d84ec21a
commit bcee6014d7
5 changed files with 74 additions and 12 deletions

View File

@ -460,7 +460,10 @@ if ( ! defined( 'FICTIONEER_FAST_AJAX_COMMENT_FUNCTIONS' ) ) {
'fictioneer_ajax_delete_my_comment',
'fictioneer_ajax_moderate_comment',
'fictioneer_ajax_report_comment',
'fictioneer_ajax_get_comment_form'
'fictioneer_ajax_get_comment_form',
'fictioneer_ajax_get_comment_section',
'fictioneer_ajax_submit_comment',
'fictioneer_ajax_edit_comment'
)
);
}

View File

@ -13,6 +13,17 @@
*/
function fictioneer_ajax_get_comment_form() {
// Enabled?
if (
! get_option( 'fictioneer_enable_ajax_comment_form' ) &&
! get_option( 'fictioneer_enable_ajax_comments' )
) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Nonce (disabled for now because public content, might conflict with caching)
// if ( ! check_ajax_referer( 'fictioneer_nonce', 'nonce', false ) ) {
// wp_send_json_error( array( 'error' => __( 'Security token expired. Please reload.', 'fictioneer' ) ) );
@ -60,6 +71,14 @@ if ( get_option( 'fictioneer_enable_ajax_comment_form' ) ) {
*/
function fictioneer_ajax_get_comment_section() {
// Enabled?
if ( ! get_option( 'fictioneer_enable_ajax_comments' ) ) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Nonce (disabled for now because public content, might conflict with caching)
// if ( ! check_ajax_referer( 'fictioneer_nonce', 'nonce', false ) ) {
// wp_send_json_error( array( 'error' => __( 'Security token expired. Please reload.', 'fictioneer' ) ) );
@ -193,6 +212,14 @@ if ( get_option( 'fictioneer_enable_ajax_comments' ) ) {
*/
function fictioneer_ajax_submit_comment() {
// Enabled?
if ( ! get_option( 'fictioneer_enable_ajax_comment_submit' ) ) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Nonce
if ( ! check_ajax_referer( 'fictioneer_nonce', 'nonce', false ) ) {
wp_send_json_error(
@ -399,6 +426,14 @@ if ( get_option( 'fictioneer_enable_ajax_comment_submit' ) ) {
*/
function fictioneer_ajax_edit_comment() {
// Enabled?
if ( ! get_option( 'fictioneer_enable_user_comment_editing' ) ) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Setup
$comment_id = isset( $_POST['comment_id'] ) ? fictioneer_validate_id( $_POST['comment_id'] ) : false;
$user = fictioneer_get_validated_ajax_user();

View File

@ -571,7 +571,10 @@ if ( get_option( 'fictioneer_enable_ajax_comment_moderation' ) ) {
function fictioneer_ajax_report_comment() {
// Enabled?
if ( get_option( 'fictioneer_disable_comment_callback' ) || ! get_option( 'fictioneer_enable_comment_reporting' ) ) {
if (
get_option( 'fictioneer_disable_comment_callback' ) ||
! get_option( 'fictioneer_enable_comment_reporting' )
) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403

View File

@ -131,7 +131,7 @@ function fcn_getCommentSection(post_id = null, page = null, scroll = false) {
scrollTarget = document.querySelector(scrollTargetSelector) ?? _$$$('respond');
if (scroll) {
scrollTarget.scrollIntoView({behavior: 'smooth'});
scrollTarget.scrollIntoView({ behavior: 'smooth' });
}
// Add page to URL and preserve params/anchor
@ -187,7 +187,9 @@ function fcn_jumpToCommentPage() {
const input = parseInt(window.prompt(_x('Enter page number:', 'Pagination jump prompt.', 'fictioneer')));
// Reload comments on entered page
if (input > 0) fcn_reloadCommentsPage(input);
if (input > 0) {
fcn_reloadCommentsPage(input);
}
}
// =============================================================================

View File

@ -196,7 +196,9 @@ function fcn_flagComment(source) {
reportButton = comment.querySelector('.fictioneer-report-comment-button');
// Abort if another AJAX action is in progress
if (comment.classList.contains('ajax-in-progress')) return;
if (comment.classList.contains('ajax-in-progress')) {
return;
}
// Lock comment until AJAX action is complete
comment.classList.add('ajax-in-progress');
@ -214,10 +216,14 @@ function fcn_flagComment(source) {
reportButton.classList.remove('_dubious');
// If report was dubious, it will be resynchronized
if (response.data.resync) fcn_showNotification(response.data.resync);
if (response.data.resync) {
fcn_showNotification(response.data.resync);
}
} else {
// Show error notice
if (response.data?.error) fcn_showNotification(response.data.error, 5, 'warning');
if (response.data?.error) {
fcn_showNotification(response.data.error, 5, 'warning');
}
}
})
.catch((error) => {
@ -463,9 +469,17 @@ function fcn_bindAJAXCommentSubmit() {
}
// Optional payload
if (parentId) payload['parent_id'] = parentId;
if (email?.value) payload['email'] = email?.value;
if (author?.value) payload['author'] = author?.value;
if (parentId) {
payload['parent_id'] = parentId;
}
if (email?.value) {
payload['email'] = email?.value;
}
if (author?.value) {
payload['author'] = author?.value;
}
// Request
fcn_ajaxPost(payload)
@ -554,7 +568,10 @@ function fcn_bindAJAXCommentSubmit() {
fcn_addCommentMouseleaveEvents();
// Clean-up form
if (_$$$('comment_parent').value != '0') _$$$('cancel-comment-reply-link').click();
if (_$$$('comment_parent').value != '0') {
_$$$('cancel-comment-reply-link').click();
}
content.value = '';
content.style.height = '';
@ -562,7 +579,9 @@ function fcn_bindAJAXCommentSubmit() {
const refresh = window.location.protocol + '//' + window.location.host + window.location.pathname;
let urlPart = '';
if (response.data.commentcode) urlPart += `?commentcode=${response.data.commentcode}`;
if (response.data.commentcode) {
urlPart += `?commentcode=${response.data.commentcode}`;
}
history.pushState({path: refresh}, '', refresh + urlPart + `#comment-${response.data.comment_id}`);