Add new functions to keep story comment count updated

This might not always be correct, but the comments get recounted in certain intervals anyway and this is cheap.
This commit is contained in:
Tetrakern 2024-08-14 23:57:14 +02:00
parent 2f494bc737
commit c82e49cf88
3 changed files with 63 additions and 14 deletions

View File

@ -277,7 +277,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `customize_controls_enqueue_scripts` | `fictioneer_enqueue_customizer_scripts`
| `customize_register` | `fictioneer_add_customizers`
| `customize_save_after` | `fictioneer_watch_for_customizer_updates`
| `delete_comment` | `fictioneer_delete_cached_story_card_by_comment`
| `delete_comment` | `fictioneer_delete_cached_story_card_by_comment`, `fictioneer_decrement_story_comment_count`
| `delete_post` | `fictioneer_refresh_post_caches`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_purge_transients`
| `do_feed_rss2` | `fictioneer_main_rss_template`
| `do_meta_boxes` | `fictioneer_remove_custom_fields_meta_boxes`
@ -312,7 +312,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `wp_enqueue_scripts` | `fictioneer_add_custom_scripts`, `fictioneer_customizer_queue`, `fictioneer_style_queue`
| `wp_footer` | `fictioneer_render_taxonomy_submenu`
| `wp_head` | `fictioneer_output_head_seo`, `fictioneer_output_rss`, `fictioneer_output_schemas`, `fictioneer_add_fiction_css`, `fictioneer_output_head_fonts`, `fictioneer_output_head_translations`, `fictioneer_remove_mu_registration_styles`, `fictioneer_output_mu_registration_style`, `fictioneer_output_head_meta`, `fictioneer_output_head_critical_scripts`. `fictioneer_output_head_anti_flicker`
| `wp_insert_comment` | `fictioneer_delete_cached_story_card_by_comment`
| `wp_insert_comment` | `fictioneer_delete_cached_story_card_by_comment`, `fictioneer_increment_story_comment_count`
| `wp_update_nav_menu` | `fictioneer_purge_nav_menu_transients`
<br>

View File

@ -1268,18 +1268,6 @@ function fictioneer_delete_cached_story_card_by_comment( $comment_id ) {
if ( $story_id ) {
fictioneer_delete_cached_story_card( $story_id );
// Update comment count in story meta cache (if story)
// if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
// $story_data = fictioneer_get_story_data( $story_id );
// if ( $story_data ) {
// $story_data['comment_count'] = fictioneer_get_story_comment_count( $story_id );
// $story_data['comment_count_timestamp'] = time();
// update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data );
// }
// }
}
}
}

View File

@ -426,3 +426,64 @@ function fictioneer_expire_post_password( $required, $post ) {
return $required;
}
add_filter( 'post_password_required', 'fictioneer_expire_post_password', 5, 2 );
// =============================================================================
// STORY COMMENT COUNT
// =============================================================================
/**
* Increments comment count of story by 1
*
* @since 5.22.3
*
* @param int $comment_id ID of the comment belonging to the post
*/
function fictioneer_increment_story_comment_count( $comment_id ) {
// Setup
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return;
}
$story_id = get_post_meta( $comment->comment_post_ID, 'fictioneer_chapter_story', true );
$story_data = $story_id ? fictioneer_get_story_data( $story_id ) : null;
// Increment comment count (will be recounted at some later point)
if ( $story_data ) {
$story_data['comment_count'] = intval( $story_data['comment_count'] ) + 1;
update_post_meta( $story_id, 'fictioneer_story_data_collection', $story_data );
}
}
/**
* Decrements comment count of story by 1
*
* @since 5.22.3
*
* @param int $comment_id ID of the comment belonging to the post
*/
function fictioneer_decrement_story_comment_count( $comment_id ) {
// Setup
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return;
}
$story_id = get_post_meta( $comment->comment_post_ID, 'fictioneer_chapter_story', true );
$story_data = $story_id ? fictioneer_get_story_data( $story_id ) : null;
// Decrement comment count (will be recounted at some later point)
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 );
}
}
if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
add_action( 'wp_insert_comment', 'fictioneer_increment_story_comment_count' );
add_action( 'delete_comment', 'fictioneer_decrement_story_comment_count' );
}