diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 5e1c9d78..4de07c1e 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -296,7 +296,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook | `publish_to_draft` | `fictioneer_chapter_to_draft` | `rest_api_init` | `fictioneer_register_endpoint_get_story_comments` | `restrict_manage_posts` | `fictioneer_add_chapter_story_filter_dropdown` -| `save_post` | `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_transients`, `fictioneer_save_story_metaboxes`, `fictioneer_save_chapter_metaboxes`, `fictioneer_save_advanced_metabox`, `fictioneer_save_support_links_metabox`, `fictioneer_save_collection_metaboxes`, `fictioneer_save_recommendation_metaboxes`, `fictioneer_save_post_metaboxes`, `fictioneer_delete_cached_story_card_after_update` +| `save_post` | `fictioneer_refresh_chapters_schema`, `fictioneer_refresh_chapter_schema`, `fictioneer_refresh_collections_schema`, `fictioneer_refresh_post_caches`, `fictioneer_refresh_post_schema`, `fictioneer_refresh_recommendations_schema`, `fictioneer_refresh_recommendation_schema`, `fictioneer_refresh_stories_schema`, `fictioneer_refresh_story_schema`, `fictioneer_save_seo_metabox`, `fictioneer_save_word_count`, `fictioneer_track_chapter_and_story_updates`, `fictioneer_update_modified_date_on_story_for_chapter`, `fictioneer_update_shortcode_relationships`, `fictioneer_purge_transients`, `fictioneer_save_story_metaboxes`, `fictioneer_save_chapter_metaboxes`, `fictioneer_save_advanced_metabox`, `fictioneer_save_support_links_metabox`, `fictioneer_save_collection_metaboxes`, `fictioneer_save_recommendation_metaboxes`, `fictioneer_save_post_metaboxes`, `fictioneer_delete_cached_story_card_after_update`, `fictioneer_rebuild_story_data_collection` | `show_user_profile` | `fictioneer_custom_profile_fields` | `shutdown` | `fictioneer_save_story_card_cache`, `fictioneer_save_query_result_cache_registry` | `switch_theme` | `fictioneer_theme_deactivation` diff --git a/includes/functions/_service-caching.php b/includes/functions/_service-caching.php index 36e949a9..c3b1b6ca 100644 --- a/includes/functions/_service-caching.php +++ b/includes/functions/_service-caching.php @@ -763,6 +763,46 @@ function fictioneer_toggle_update_tracker_hooks( $add = true ) { fictioneer_toggle_update_tracker_hooks(); +/** + * Rebuild story data collection after purge + * + * Note: This is only triggered for one story to avoid rebuilding the + * collections of ALL stories at once in case of a complete purge. + * + * @since 5.23.1 + * + * @param int $post_id Updated post ID. + * @param WP_Post $post The post object. + */ + +function fictioneer_rebuild_story_data_collection( $post_id, $post ) { + // Prevent multi-fire and check type + if ( + fictioneer_multi_save_guard( $post_id ) || + ! in_array( $post->post_type, ['fcn_story', 'fcn_chapter'] ) + ) { + return; + } + + // Story or chapter? + if ( $post->post_type === 'fcn_chapter' ) { + $story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true ); + + if ( $story_id ) { + fictioneer_get_story_data( $story_id ); + } + } else { + fictioneer_get_story_data( $post_id ); + } + + // Prevent chain reaction + remove_action( 'save_post', 'fictioneer_rebuild_story_data_collection', 999 ); +} + +if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) { + add_action( 'save_post', 'fictioneer_rebuild_story_data_collection', 999, 2 ); +} + // ============================================================================= // PURGE CACHE TRANSIENTS // =============================================================================