Improve conditional loading for AJAX functions

This commit is contained in:
Tetrakern 2024-06-24 15:33:48 +02:00
parent c489a896dd
commit 2ee8744901
6 changed files with 148 additions and 102 deletions

View File

@ -523,7 +523,7 @@ if ( strpos( $_SERVER['REQUEST_URI'], 'wp-json/fictioneer/' ) !== false ) {
require_once __DIR__ . '/includes/functions/comments/_comments_threads.php';
}
if ( is_admin() ) {
if ( wp_doing_ajax() ) {
// Required for AJAX
require_once __DIR__ . '/includes/functions/comments/_comments_ajax.php';
require_once __DIR__ . '/includes/functions/comments/_comments_form.php';

View File

@ -1178,6 +1178,15 @@ function fictioneer_generate_epub() {
}
add_action( 'template_redirect', 'fictioneer_generate_epub', 10 );
// =============================================================================
// AJAX REQUESTS
// > Return early if no AJAX functions are required.
// =============================================================================
if ( ! wp_doing_ajax() ) {
return;
}
// =============================================================================
// AJAX - CHECK IF READY TO DOWNLOAD
// =============================================================================

View File

@ -1691,49 +1691,6 @@ function fictioneer_show_auth_content() {
get_option( 'fictioneer_enable_ajax_authentication' );
}
// =============================================================================
// AJAX AUTHENTICATION
// =============================================================================
/**
* Send user authentication status via AJAX
*
* @since 5.7.0
*/
function fictioneer_ajax_get_auth() {
// Enabled?
if ( ! get_option( 'fictioneer_enable_ajax_authentication' ) ) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Setup
$user = wp_get_current_user();
$nonce = wp_create_nonce( 'fictioneer_nonce' );
$nonce_html = '<input id="fictioneer-ajax-nonce" name="fictioneer-ajax-nonce" type="hidden" value="' . $nonce . '">';
// Response
wp_send_json_success(
array(
'loggedIn' => is_user_logged_in(),
'isAdmin' => fictioneer_is_admin( $user->ID ),
'isModerator' => fictioneer_is_moderator( $user->ID ),
'isAuthor' => fictioneer_is_author( $user->ID ),
'isEditor' => fictioneer_is_editor( $user->ID ),
'nonce' => $nonce,
'nonceHtml' => $nonce_html
)
);
}
if ( get_option( 'fictioneer_enable_ajax_authentication' ) ) {
add_action( 'wp_ajax_fictioneer_ajax_get_auth', 'fictioneer_ajax_get_auth' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_get_auth', 'fictioneer_ajax_get_auth' );
}
// =============================================================================
// FICTIONEER TRANSLATIONS
// =============================================================================
@ -3163,3 +3120,56 @@ function fictioneer_get_random_username( $unique = true ) {
// Return username
return $username;
}
// =============================================================================
// AJAX REQUESTS
// > Return early if no AJAX functions are required.
// =============================================================================
if ( ! wp_doing_ajax() ) {
return;
}
// =============================================================================
// AJAX AUTHENTICATION
// =============================================================================
/**
* Send user authentication status via AJAX
*
* @since 5.7.0
*/
function fictioneer_ajax_get_auth() {
// Enabled?
if ( ! get_option( 'fictioneer_enable_ajax_authentication' ) ) {
wp_send_json_error(
array( 'error' => __( 'Not allowed.', 'fictioneer' ) ),
403
);
}
// Setup
$user = wp_get_current_user();
$nonce = wp_create_nonce( 'fictioneer_nonce' );
$nonce_html = '<input id="fictioneer-ajax-nonce" name="fictioneer-ajax-nonce" type="hidden" value="' . $nonce . '">';
// Response
wp_send_json_success(
array(
'loggedIn' => is_user_logged_in(),
'isAdmin' => fictioneer_is_admin( $user->ID ),
'isModerator' => fictioneer_is_moderator( $user->ID ),
'isAuthor' => fictioneer_is_author( $user->ID ),
'isEditor' => fictioneer_is_editor( $user->ID ),
'nonce' => $nonce,
'nonceHtml' => $nonce_html
)
);
}
if ( get_option( 'fictioneer_enable_ajax_authentication' ) ) {
add_action( 'wp_ajax_fictioneer_ajax_get_auth', 'fictioneer_ajax_get_auth' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_get_auth', 'fictioneer_ajax_get_auth' );
}

View File

@ -103,6 +103,15 @@ if ( ! function_exists( 'fictioneer_count_chapter_checkmarks' ) ) {
}
}
// =============================================================================
// AJAX REQUESTS
// > Return early if no AJAX functions are required.
// =============================================================================
if ( ! wp_doing_ajax() ) {
return;
}
// =============================================================================
// SET CHECKMARKS - AJAX
// =============================================================================

View File

@ -44,6 +44,73 @@ if ( ! function_exists( 'fictioneer_load_follows' ) ) {
}
}
// =============================================================================
// QUERY CHAPTERS OF FOLLOWED STORIES
// =============================================================================
if ( ! function_exists( 'fictioneer_query_followed_chapters' ) ) {
/**
* Query chapters of followed stories
*
* @since 4.3.0
*
* @param array $story_ids IDs of the followed stories.
* @param string|false $after_date Optional. Only return chapters after this date, e.g. wp_date( 'c', $timestamp ).
* @param int $count Optional. Maximum number of chapters to be returned. Default 20.
*
* @return array Collection of chapters.
*/
function fictioneer_query_followed_chapters( $story_ids, $after_date = false, $count = 20 ) {
// Setup
$query_args = array (
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'fictioneer_chapter_story',
'value' => $story_ids,
'compare' => 'IN',
)
),
'numberposts' => $count,
'orderby' => 'date',
'order' => 'DESC',
'update_post_meta_cache' => true,
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
);
if ( $after_date ) {
$query_args['date_query'] = array( 'after' => $after_date );
}
// Query
$all_chapters = get_posts( $query_args );
// Filter out hidden chapters (faster than meta query, highly unlikely to eliminate all)
$chapters = array_filter( $all_chapters, function ( $candidate ) {
// Chapter hidden?
$chapter_hidden = get_post_meta( $candidate->ID, 'fictioneer_chapter_hidden', true );
// Only keep if not hidden
return empty( $chapter_hidden ) || $chapter_hidden === '0';
});
// Return final results
return $chapters;
}
}
// =============================================================================
// AJAX REQUESTS
// > Return early if no AJAX functions are required.
// =============================================================================
if ( ! wp_doing_ajax() ) {
return;
}
// =============================================================================
// TOGGLE FOLLOW - AJAX
// =============================================================================
@ -205,64 +272,6 @@ if ( get_option( 'fictioneer_enable_follows' ) ) {
add_action( 'wp_ajax_fictioneer_ajax_mark_follows_read', 'fictioneer_ajax_mark_follows_read' );
}
// =============================================================================
// QUERY CHAPTERS OF FOLLOWED STORIES
// =============================================================================
if ( ! function_exists( 'fictioneer_query_followed_chapters' ) ) {
/**
* Query chapters of followed stories
*
* @since 4.3.0
*
* @param array $story_ids IDs of the followed stories.
* @param string|false $after_date Optional. Only return chapters after this date, e.g. wp_date( 'c', $timestamp ).
* @param int $count Optional. Maximum number of chapters to be returned. Default 20.
*
* @return array Collection of chapters.
*/
function fictioneer_query_followed_chapters( $story_ids, $after_date = false, $count = 20 ) {
// Setup
$query_args = array (
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'fictioneer_chapter_story',
'value' => $story_ids,
'compare' => 'IN',
)
),
'numberposts' => $count,
'orderby' => 'date',
'order' => 'DESC',
'update_post_meta_cache' => true,
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
);
if ( $after_date ) {
$query_args['date_query'] = array( 'after' => $after_date );
}
// Query
$all_chapters = get_posts( $query_args );
// Filter out hidden chapters (faster than meta query, highly unlikely to eliminate all)
$chapters = array_filter( $all_chapters, function ( $candidate ) {
// Chapter hidden?
$chapter_hidden = get_post_meta( $candidate->ID, 'fictioneer_chapter_hidden', true );
// Only keep if not hidden
return empty( $chapter_hidden ) || $chapter_hidden === '0';
});
// Return final results
return $chapters;
}
}
// =============================================================================
// GET FOLLOWS NOTIFICATIONS - AJAX
// =============================================================================

View File

@ -39,6 +39,15 @@ if ( ! function_exists( 'fictioneer_load_reminders' ) ) {
}
}
// =============================================================================
// AJAX REQUESTS
// > Return early if no AJAX functions are required.
// =============================================================================
if ( ! wp_doing_ajax() ) {
return;
}
// =============================================================================
// TOGGLE REMINDER - AJAX
// =============================================================================