Improve multi-fire guard for hooks

This commit is contained in:
Tetrakern 2023-08-05 12:12:07 +02:00
parent da2ff1bc14
commit 46696bc4f2
13 changed files with 81 additions and 38 deletions

View File

@ -266,13 +266,7 @@ if ( get_option( 'fictioneer_limit_chapter_stories_by_author' ) ) {
function fictioneer_acf_append_chapter_to_story( $post_id ) {
// Prevent miss-fire
if (
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
wp_is_post_autosave( $post_id ) ||
wp_is_post_revision( $post_id ) ||
! in_array( get_post_status( $post_id ), ['publish', 'future'] ) ||
get_post_type( $post_id ) != 'fcn_chapter'
) {
if ( fictioneer_multi_save_guard( $post_id ) || get_post_type( $post_id ) != 'fcn_chapter' ) {
return;
}

View File

@ -293,8 +293,9 @@ if ( ! function_exists( 'fictioneer_refresh_post_caches' ) ) {
function fictioneer_refresh_post_caches( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Purge all?
if ( get_option( 'fictioneer_purge_all_caches' ) ) {
@ -508,14 +509,15 @@ if ( ! function_exists( 'fictioneer_track_chapter_and_story_updates' ) ) {
function fictioneer_track_chapter_and_story_updates( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Get story ID from post or parent story (if any)
$post_type = get_post_type( $post_id );
$story_id = $post_type == 'fcn_story' ? $post_id : fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
// Since every chapter needs to be attached to a story...
// If there is a story...
if ( ! empty( $story_id ) ) {
// Decides when cached story/chapter data need to be refreshed
// Beware: This is an option, not a Transient!
@ -585,8 +587,9 @@ if ( ! function_exists( 'fictioneer_get_last_story_or_chapter_update' ) ) {
function fictioneer_purge_cache_transients( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Shortcodes
if ( FICTIONEER_SHORTCODE_TRANSIENT_EXPIRATION > 0 ) {

View File

@ -49,8 +49,9 @@ if ( ! function_exists( 'fictioneer_shortcode_query' ) ) {
function fictioneer_update_shortcode_relationships( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Setup
$registry = fictioneer_get_relationship_registry();

View File

@ -1722,4 +1722,35 @@ if ( ! function_exists( 'fictioneer_get_assigned_page_link' ) ) {
}
}
// =============================================================================
// MULTI SAVE GUARD
// =============================================================================
if ( ! function_exists( 'fictioneer_multi_save_guard' ) ) {
/**
* Prevents multi-fire in update hooks
*
* @since 5.5.2
*
* @param int $post_id The ID of the updated post.
*
* @return boolean True if not saved by user, false otherwise.
*/
function fictioneer_multi_save_guard( $post_id ) {
// Automatic save?
if (
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
wp_is_post_autosave( $post_id ) ||
wp_is_post_revision( $post_id ) ||
in_array( get_post_status( $post_id ), ['auto-draft'] )
) {
return true;
}
// User-initiated save
return false;
}
}
?>

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_chapter_schema' ) ) {
function fictioneer_refresh_chapter_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
if ( $post->post_type !== 'fcn_chapter' ) return;

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_chapters_schema' ) ) {
function fictioneer_refresh_chapters_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
$sub_update = in_array( $post->post_type, ['fcn_chapter', 'fcn_collection'] );

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_collections_schema' ) ) {
function fictioneer_refresh_collections_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
$sub_update = in_array( $post->post_type, ['fcn_collection', 'fcn_story', 'fcn_chapter', 'fcn_recommendation', 'post'] );

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_post_schema' ) ) {
function fictioneer_refresh_post_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
if ( $post->post_type !== 'post' ) return;

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_recommendation_schema' ) ) {
function fictioneer_refresh_recommendation_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
if ( $post->post_type !== 'fcn_recommendation' ) return;

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_recommendations_schema' ) ) {
function fictioneer_refresh_recommendations_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
$sub_update = in_array( $post->post_type, ['fcn_recommendation', 'fcn_collection'] );

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_stories_schema' ) ) {
function fictioneer_refresh_stories_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
$sub_update = in_array( $post->post_type, ['fcn_story', 'fcn_collection'] );

View File

@ -19,8 +19,9 @@ if ( ! function_exists( 'fictioneer_refresh_story_schema' ) ) {
function fictioneer_refresh_story_schema( $post_id, $post ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Check what was updated
if ( $post->post_type !== 'fcn_story' ) return;

View File

@ -500,8 +500,9 @@ add_action( 'untrashed_post', function( $post_id ) {
function fictioneer_log_published_posts( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Updated or first published?
if ( strtotime( '-5 seconds' ) < strtotime( get_the_date( 'c', $post_id ) ) ) {
@ -535,8 +536,9 @@ add_action( 'publish_fcn_recommendation', 'fictioneer_log_published_posts' );
function fictioneer_log_pending_posts( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) return;
if ( fictioneer_multi_save_guard( $post_id ) ) {
return;
}
// Relay
fictioneer_log_post_update( $post_id, __( 'submitted for review', 'fictioneer' ) );
@ -564,8 +566,12 @@ add_action( 'pending_fcn_recommendation', 'fictioneer_log_published_posts' );
function fictioneer_log_deleted_posts( $post_id ) {
// Prevent multi-fire
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) return;
if ( did_action( 'before_delete_post' ) != 1 ) return;
if (
( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
did_action( 'before_delete_post' ) != 1
) {
return;
}
// Setup
$post_type = get_post_type( $post_id );