From 7aec3c55af6f543afc4ad1a25be3fa182bc0de51 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:46:36 +0100 Subject: [PATCH] Store story comment count in post row Primarily for query purposes. --- includes/functions/_helpers-query.php | 48 +++++++++++++++++++++++++++ includes/functions/_service-posts.php | 4 +++ includes/functions/_utility.php | 6 ++++ 3 files changed, 58 insertions(+) diff --git a/includes/functions/_helpers-query.php b/includes/functions/_helpers-query.php index 06e41209..a32159c5 100644 --- a/includes/functions/_helpers-query.php +++ b/includes/functions/_helpers-query.php @@ -937,3 +937,51 @@ if ( ! function_exists( 'fictioneer_sql_get_story_chapter_relationship_data' ) ) return $results; } } + +/** + * Returns comment count of a story + * + * @since 5.26.0 + * + * @global wpdb $wpdb WordPress database object. + * + * @param int $post_id Post ID. + * + * @return int Comment count. + */ + +function fictioneer_sql_get_comment_count( $post_id ) { + global $wpdb; + + $query = $wpdb->prepare( + "SELECT comment_count + FROM {$wpdb->posts} + WHERE ID = %d", + $post_id + ); + + return $wpdb->get_var( $query ); +} + +/** + * Updates the comment count of a post + * + * @since 5.26.0 + * + * @global wpdb $wpdb WordPress database object. + * + * @param int $post_id Post ID. + * @param int $count Comment count. + */ + +function fictioneer_sql_update_comment_count( $post_id, $count ) { + global $wpdb; + + $wpdb->update( + $wpdb->posts, + array( 'comment_count' => $count ), + array( 'ID' => $post_id ), + ['%d'], + ['%d'] + ); +} diff --git a/includes/functions/_service-posts.php b/includes/functions/_service-posts.php index 89dd662f..daf762af 100644 --- a/includes/functions/_service-posts.php +++ b/includes/functions/_service-posts.php @@ -425,6 +425,8 @@ function fictioneer_increment_story_comment_count( $comment_id ) { if ( $story_data ) { $story_data['comment_count'] = intval( $story_data['comment_count'] ) + 1; update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data ); + + fictioneer_sql_update_comment_count( $story_id, $story_data['comment_count'] ); } } @@ -451,6 +453,8 @@ function fictioneer_decrement_story_comment_count( $comment_id ) { if ( $story_data ) { $story_data['comment_count'] = max( 0, intval( $story_data['comment_count'] ) - 1 ); update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data ); + + fictioneer_sql_update_comment_count( $story_id, $story_data['comment_count'] ); } } diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 1b11cef7..a587ac09 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -429,6 +429,9 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { $meta_cache['comment_count'] = $comment_count; $meta_cache['comment_count_timestamp'] = time(); + // Update post database comment count + fictioneer_sql_update_comment_count( $story_id, $comment_count ); + // Update meta cache and purge update_post_meta( $story_id, 'fictioneer_story_data_collection', $meta_cache ); @@ -579,6 +582,9 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) { // Update story total word count update_post_meta( $story_id, 'fictioneer_story_total_word_count', $word_count ); + // Update post database comment count + fictioneer_sql_update_comment_count( $story_id, $comment_count ); + // Done return $result; }