Fix auto appending of chapters (again)

This commit is contained in:
Tetrakern 2023-08-18 09:23:06 +02:00
parent 0ad0900f73
commit b3279359fb
3 changed files with 86 additions and 81 deletions

View File

@ -235,7 +235,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `manage_comments_custom_column` | `fictioneer_add_comments_report_column_content`
| `personal_options_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`
| `pre_get_posts` | `fictioneer_extend_search_query`, `fictioneer_remove_unlisted_from_search`, `fictioneer_read_others_files`, `fictioneer_read_others_files_list_view`
| `save_post` | `fictioneer_create_sitemap`, `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_cache_transients`, `fictioneer_restrict_page_templates`, `fictioneer_flush_object_cache`, `fictioneer_append_chapter_to_story`, `fictioneer_post_story_to_discord`, `fictioneer_post_chapter_to_discord`
| `save_post` | `fictioneer_create_sitemap`, `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_cache_transients`, `fictioneer_restrict_page_templates`, `fictioneer_flush_object_cache`, `fictioneer_post_story_to_discord`, `fictioneer_post_chapter_to_discord`
| `show_user_profile` | `fictioneer_custom_profile_fields`
| `switch_theme` | `fictioneer_theme_deactivation`
| `template_redirect` | `fictioneer_disable_date_archives`, `fictioneer_generate_epub`, `fictioneer_handle_oauth`, `fictioneer_logout`, `fictioneer_disable_attachment_pages`, `fictioneer_gate_unpublished_content`
@ -316,7 +316,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `acf/fields/relationship/query/name=fictioneer_story_chapters` | `fictioneer_acf_filter_chapters`
| `acf/fields/wysiwyg/toolbars` | `fictioneer_acf_reduce_wysiwyg`
| `acf/pre_render_fields` | `fictioneer_remove_filter_search_id_input`, `fictioneer_remove_custom_page_css_inputs`, `fictioneer_remove_custom_epub_css_input`, `fictioneer_remove_custom_story_pages`
| `acf/save_post` | `fictioneer_update_post_relationships`
| `acf/save_post` | `fictioneer_update_post_relationships`, `fictioneer_append_chapter_to_story`
| `acf/settings/load_json` | `fictioneer_acf_loading_point`
| `acf/settings/show_admin` | `fictioneer_acf_settings_show_admin`
| `acf/update_value/name=fictioneer_custom_css` | `fictioneer_acf_prevent_value_update`

View File

@ -297,4 +297,88 @@ function fictioneer_acf_reduce_wysiwyg( $toolbars ) {
}
add_filter( 'acf/fields/wysiwyg/toolbars', 'fictioneer_acf_reduce_wysiwyg' );
// =============================================================================
// APPEND NEW CHAPTERS TO STORY
// =============================================================================
/**
* Append new chapters to story list
*
* @since Fictioneer 5.4.9
*
* @param int $post_id The post ID.
*/
function fictioneer_append_chapter_to_story( $post_id ) {
// Prevent miss-fire (REST_REQUEST undefined!)
if (
wp_is_post_autosave( $post_id ) ||
wp_is_post_revision( $post_id ) ||
get_post_status( $post_id ) !== 'publish' ||
get_post_type( $post_id ) !== 'fcn_chapter'
) {
return;
}
// Only do this for newly published chapters (not more than 5 seconds ago),
// which means this can be triggered again by resetting the publish date.
$publishing_time = get_post_time( 'U', true, $post_id, true );
$current_time = current_time( 'timestamp', true );
if ( $current_time - $publishing_time > 5 ) {
return;
}
// Setup
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story = get_post( $story_id );
// Abort if story not found
if ( empty( $story ) ) {
return;
}
// Setup, continued
$chapter_author_id = get_post_field( 'post_author', $post_id );
$story_author_id = get_post_field( 'post_author', $story_id );
// Abort if the author IDs do not match unless it's an administrator
if ( ! current_user_can( 'manage_options' ) && $chapter_author_id != $story_author_id ) {
return;
}
// Get current story chapters
$story_chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true );
// Prepare chapter array if null (or broken)
if ( ! is_array( $story_chapters ) ) {
$story_chapters = [];
}
// Append chapter (if not already included) and save to database
if ( ! in_array( $post_id, $story_chapters ) ) {
$story_chapters[] = $post_id;
// Append chapter to field
update_post_meta( $story_id, 'fictioneer_story_chapters', array_unique( $story_chapters ) );
// Remember when chapter list has been last updated
update_post_meta( $story_id, 'fictioneer_chapters_modified', current_time( 'mysql' ) );
update_post_meta( $story_id, 'fictioneer_chapters_added', current_time( 'mysql' ) );
// Clear story data cache to ensure it gets refreshed
update_post_meta( $story_id, 'fictioneer_story_data_collection', false );
} else {
return;
}
// Update story post to fire associated actions
wp_update_post( array( 'ID' => $story_id ) );
}
if ( get_option( 'fictioneer_enable_chapter_appending' ) ) {
// WP save_post may not have the field yet!
add_action( 'acf/save_post', 'fictioneer_append_chapter_to_story', 99 );
}
?>

View File

@ -1024,85 +1024,6 @@ function fictioneer_extend_allowed_upload_types( $mimes ) {
return $mimes;
}
add_filter( 'upload_mimes', 'fictioneer_extend_allowed_upload_types' );
// =============================================================================
// APPEND NEW CHAPTERS TO STORY
// =============================================================================
/**
* Append new chapters to story list
*
* @since Fictioneer 5.4.9
*
* @param int $post_id The post ID.
*/
function fictioneer_append_chapter_to_story( $post_id ) {
// Prevent miss-fire
if ( fictioneer_multi_save_guard( $post_id ) || get_post_type( $post_id ) != 'fcn_chapter' ) {
return;
}
// Only do this for newly published chapters (not more than 5 seconds ago),
// which means this can be triggered again by resetting the publish date.
$publishing_time = get_post_time( 'U', true, $post_id, true );
$current_time = current_time( 'timestamp', true );
if ( $current_time - $publishing_time > 5 ) {
return;
}
// Setup
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story = get_post( $story_id );
// Abort if story not found
if ( empty( $story ) ) {
return;
}
// Setup, continued
$chapter_author_id = get_post_field( 'post_author', $post_id );
$story_author_id = get_post_field( 'post_author', $story_id );
// Abort if the author IDs do not match unless it's an administrator
if ( ! current_user_can( 'manage_options' ) && $chapter_author_id != $story_author_id ) {
return;
}
// Get current story chapters
$story_chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true );
// Prepare chapter array if null (or broken)
if ( ! is_array( $story_chapters ) ) {
$story_chapters = [];
}
// Append chapter (if not already included) and save to database
if ( ! in_array( $post_id, $story_chapters ) ) {
$story_chapters[] = $post_id;
// Append chapter to field
update_post_meta( $story_id, 'fictioneer_story_chapters', array_unique( $story_chapters ) );
// Remember when chapter list has been last updated
update_post_meta( $story_id, 'fictioneer_chapters_modified', current_time( 'mysql' ) );
update_post_meta( $story_id, 'fictioneer_chapters_added', current_time( 'mysql' ) );
// Clear story data cache to ensure it gets refreshed
update_post_meta( $story_id, 'fictioneer_story_data_collection', false );
} else {
return;
}
// Update story post to fire associated actions
wp_update_post( array( 'ID' => $story_id ) );
}
if ( get_option( 'fictioneer_enable_chapter_appending' ) ) {
add_action( 'save_post', 'fictioneer_append_chapter_to_story', 99 );
}
// =============================================================================
// SEE SOME 3V1L
// =============================================================================