Optionize comment count refresh for story data
This is an expensive query and hardly needed, but better leave it on by default for legacy reasons.
This commit is contained in:
parent
864385e370
commit
ac507476cf
@ -18,7 +18,7 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
|
||||
|
||||
function fictioneer_api_get_story_node( $story_id, $with_chapters = true ) {
|
||||
// Validation
|
||||
$data = fictioneer_get_story_data( $story_id );
|
||||
$data = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
|
||||
|
||||
// Abort if...
|
||||
if ( empty( $data ) ) return false;
|
||||
|
@ -871,7 +871,7 @@ function fictioneer_shortcode_chapter_list( $attr ) {
|
||||
if ( $story_id && empty( $chapter_ids ) ) {
|
||||
// ... via story
|
||||
$hide_icons = $hide_icons || fictioneer_get_field( 'fictioneer_story_hide_chapter_icons', $story_id );
|
||||
$story_data = fictioneer_get_story_data( $story_id );
|
||||
$story_data = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
|
||||
$chapters = $story_data['chapter_ids'];
|
||||
} elseif ( ! empty( $chapter_ids ) ) {
|
||||
// ... via chapter IDs
|
||||
|
@ -200,12 +200,14 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
|
||||
*
|
||||
* @since Fictioneer 4.3
|
||||
*
|
||||
* @param int $story_id ID of the story.
|
||||
* @param int $story_id ID of the story.
|
||||
* @param boolean $refresh_comment_count Optional. Whether to refresh comment count.
|
||||
* Default true.
|
||||
*
|
||||
* @return array|boolean $result Data of the story or false if invalid.
|
||||
*/
|
||||
|
||||
function fictioneer_get_story_data( $story_id ) {
|
||||
function fictioneer_get_story_data( $story_id, $refresh_comment_count = true ) {
|
||||
$story_id = fictioneer_validate_id( $story_id, 'fcn_story' );
|
||||
|
||||
if ( ! $story_id ) return false;
|
||||
@ -215,16 +217,18 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
|
||||
|
||||
if ( ! empty( $old_data ) && $old_data['last_modified'] >= get_the_modified_time( 'U', $story_id ) ) {
|
||||
// Refresh comment count
|
||||
$comment_count = count( $old_data['chapter_ids'] ) < 1 ? 0 : get_comments(
|
||||
array(
|
||||
'status' => 'approve',
|
||||
'post_type' => array( 'fcn_chapter' ),
|
||||
'post__in' => $old_data['chapter_ids'],
|
||||
'count' => true
|
||||
)
|
||||
);
|
||||
if ( $refresh_comment_count ) {
|
||||
$comment_count = count( $old_data['chapter_ids'] ) < 1 ? 0 : get_comments(
|
||||
array(
|
||||
'status' => 'approve',
|
||||
'post_type' => array( 'fcn_chapter' ),
|
||||
'post__in' => $old_data['chapter_ids'],
|
||||
'count' => true
|
||||
)
|
||||
);
|
||||
|
||||
$old_data['comment_count'] = $comment_count;
|
||||
$old_data['comment_count'] = $comment_count;
|
||||
}
|
||||
|
||||
// Return cached data
|
||||
return $old_data;
|
||||
|
@ -172,7 +172,7 @@ if ( ! function_exists( 'fictioneer_get_stories_total_word_count' ) ) {
|
||||
|
||||
// Sum of all word counts
|
||||
foreach( $stories as $story ) {
|
||||
$story_data = fictioneer_get_story_data( $story->ID );
|
||||
$story_data = fictioneer_get_story_data( $story->ID, false ); // Does not refresh comment count!
|
||||
$word_count += $story_data['word_count'];
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ if ( ! function_exists( 'fictioneer_request_story_comments' ) ) {
|
||||
|
||||
// Setup
|
||||
$page = isset( $_GET['page'] ) ? absint( $_GET['page'] ) : 1;
|
||||
$story = fictioneer_get_story_data( $story_id );
|
||||
$story = fictioneer_get_story_data( $story_id, true );
|
||||
$chapter_ids = $story['chapter_ids']; // Only contains publicly visible chapters
|
||||
$comments_per_page = get_option( 'comments_per_page' );
|
||||
$chapter_data = [];
|
||||
|
@ -92,7 +92,7 @@ $current_epubs = array_slice( $epubs, $offset, $epubs_per_page, true );
|
||||
$story_data = null;
|
||||
|
||||
if ( $story ) {
|
||||
$story_data = fictioneer_get_story_data( $story->ID );
|
||||
$story_data = fictioneer_get_story_data( $story->ID, false ); // Does not refresh comment count!
|
||||
}
|
||||
|
||||
$downloads = $story ? get_post_meta( $story->ID, 'fictioneer_epub_downloads', true ) : 0;
|
||||
|
@ -163,7 +163,7 @@ if ( ! function_exists( 'fictioneer_ajax_set_checkmark' ) ) {
|
||||
wp_send_json_error( ['error' => __( 'Invalid story ID.', 'fictioneer' )] );
|
||||
}
|
||||
|
||||
$story_data = fictioneer_get_story_data( $story_id );
|
||||
$story_data = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
|
||||
|
||||
// Prepare update
|
||||
$update = isset( $_POST['update'] ) ? explode( ' ', sanitize_text_field( $_POST['update'] ) ) : [];
|
||||
|
@ -19,7 +19,7 @@
|
||||
// Setup
|
||||
$title = fictioneer_get_safe_title( get_the_ID() );
|
||||
$story_id = fictioneer_get_field( 'fictioneer_chapter_story' );
|
||||
$story_data = $story_id ? fictioneer_get_story_data( $story_id ) : null;
|
||||
$story_data = $story_id ? fictioneer_get_story_data( $story_id, false ) : null; // Does not refresh comment count!
|
||||
$chapter_rating = fictioneer_get_field( 'fictioneer_chapter_rating' );
|
||||
$story_thumbnail_url_full = $story_id ? get_the_post_thumbnail_url( $story_id, 'full' ) : null;
|
||||
$text_icon = fictioneer_get_field( 'fictioneer_chapter_text_icon' );
|
||||
|
@ -75,7 +75,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
// Setup
|
||||
$title = fictioneer_get_safe_title( get_the_ID() );
|
||||
$story_id = fictioneer_get_field( 'fictioneer_chapter_story' );
|
||||
$story = $story_id ? fictioneer_get_story_data( $story_id ) : false;
|
||||
$story = $story_id ? fictioneer_get_story_data( $story_id, false ) : false; // Does not refresh comment count!
|
||||
$text_icon = fictioneer_get_field( 'fictioneer_chapter_text_icon' );
|
||||
|
||||
// Chapter images
|
||||
|
@ -77,7 +77,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
$title = fictioneer_get_safe_title( get_the_ID() );
|
||||
$chapter_rating = fictioneer_get_field( 'fictioneer_chapter_rating' );
|
||||
$story_id = fictioneer_get_field( 'fictioneer_chapter_story' );
|
||||
$story = $story_id ? fictioneer_get_story_data( $story_id ) : false;
|
||||
$story = $story_id ? fictioneer_get_story_data( $story_id, false ) : false; // Does not refresh comment count!
|
||||
$text_icon = fictioneer_get_field( 'fictioneer_chapter_text_icon' );
|
||||
|
||||
// Chapter images
|
||||
|
@ -83,7 +83,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
|
||||
<?php
|
||||
// Setup
|
||||
$story = fictioneer_get_story_data( $post->ID );
|
||||
$story = fictioneer_get_story_data( $post->ID, false ); // Does not refresh comment count!
|
||||
$tags = get_option( 'fictioneer_show_tags_on_story_cards' ) ? get_the_tags( $post ) : false;
|
||||
?>
|
||||
|
||||
|
@ -80,7 +80,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
|
||||
<?php
|
||||
// Setup
|
||||
$story = fictioneer_get_story_data( $post->ID );
|
||||
$story = fictioneer_get_story_data( $post->ID, false ); // Does not refresh comment count!
|
||||
$tags = get_option( 'fictioneer_show_tags_on_story_cards' ) ? get_the_tags( $post ) : false;
|
||||
?>
|
||||
|
||||
|
@ -87,7 +87,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
|
||||
<?php
|
||||
// Setup
|
||||
$story = fictioneer_get_story_data( $post->ID );
|
||||
$story = fictioneer_get_story_data( $post->ID, false ); // Does not refresh comment count!
|
||||
$chapter_list = [];
|
||||
$chapter_excerpt; // Set inside inner loop
|
||||
$chapter_title; // Set inside inner loop
|
||||
|
@ -88,7 +88,7 @@ $entries = fictioneer_shortcode_query( $query_args );
|
||||
|
||||
<?php
|
||||
// Setup
|
||||
$story = fictioneer_get_story_data( $post->ID );
|
||||
$story = fictioneer_get_story_data( $post->ID, false ); // Does not refresh comment count!
|
||||
$tags = get_option( 'fictioneer_show_tags_on_story_cards' ) ? get_the_tags( $post ) : false;
|
||||
$chapter_list = [];
|
||||
|
||||
|
@ -133,7 +133,7 @@ $disable_folding = fictioneer_get_field( 'fictioneer_story_disable_collapse' );
|
||||
|
||||
<section id="chapters" class="story__tab-page _current story__chapters" data-order="asc" data-view="list">
|
||||
<?php
|
||||
$chapters = fictioneer_get_field( 'fictioneer_story_chapters', $story_id );
|
||||
$chapters = fictioneer_get_field( 'fictioneer_story_chapters', $story_id ); // Already prepared!
|
||||
$chapter_groups = [];
|
||||
|
||||
// Loop and prepare groups
|
||||
|
@ -24,7 +24,7 @@ if ( ! $story_id || $is_hidden || post_password_required( $story_id ) ) {
|
||||
}
|
||||
|
||||
// Get story data
|
||||
$story = fictioneer_get_story_data( $story_id );
|
||||
$story = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
|
||||
$chapters = fictioneer_get_field( 'fictioneer_story_chapters', $story_id );
|
||||
|
||||
// Feed title
|
||||
|
@ -52,7 +52,7 @@ get_header( null, $header_args );
|
||||
|
||||
// Story data
|
||||
if ( $story_post ) {
|
||||
$story_data = fictioneer_get_story_data( $story_id );
|
||||
$story_data = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
|
||||
$chapter_ids = $story_data['chapter_ids'];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user