diff --git a/functions.php b/functions.php index ba40ad95..23ab9f75 100644 --- a/functions.php +++ b/functions.php @@ -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'; diff --git a/includes/functions/_module-epub.php b/includes/functions/_module-epub.php index fc5d5491..4e13ebaf 100644 --- a/includes/functions/_module-epub.php +++ b/includes/functions/_module-epub.php @@ -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 // ============================================================================= diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 22de4528..933bcc3a 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -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 = ''; - - // 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 = ''; + + // 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' ); +} diff --git a/includes/functions/users/_checkmarks.php b/includes/functions/users/_checkmarks.php index 2e1b0b55..823dea71 100644 --- a/includes/functions/users/_checkmarks.php +++ b/includes/functions/users/_checkmarks.php @@ -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 // ============================================================================= diff --git a/includes/functions/users/_follows.php b/includes/functions/users/_follows.php index e9eb9977..de5df286 100644 --- a/includes/functions/users/_follows.php +++ b/includes/functions/users/_follows.php @@ -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 // ============================================================================= diff --git a/includes/functions/users/_reminders.php b/includes/functions/users/_reminders.php index ee47e458..a75cd59a 100644 --- a/includes/functions/users/_reminders.php +++ b/includes/functions/users/_reminders.php @@ -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 // =============================================================================