Use SQL to check for valid new chapters in story
This commit is contained in:
parent
d1894d3459
commit
f387230e1c
@ -381,7 +381,7 @@ if ( ! function_exists( 'fictioneer_sql_filter_valid_chapter_ids' ) ) {
|
|||||||
/**
|
/**
|
||||||
* Filters out non-valid chapters for ID array
|
* Filters out non-valid chapters for ID array
|
||||||
*
|
*
|
||||||
* Note: This is about 3 times faster than using WP_Query.
|
* Note: This is up to 3 times faster than using WP_Query.
|
||||||
*
|
*
|
||||||
* @since 5.26.0
|
* @since 5.26.0
|
||||||
*
|
*
|
||||||
@ -436,54 +436,56 @@ if ( ! function_exists( 'fictioneer_sql_filter_valid_chapter_ids' ) ) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ( ! function_exists( 'fictioneer_sql_has_new_story_chapters' ) ) {
|
||||||
* Filters out non-valid chapters for ID array
|
/**
|
||||||
*
|
* Checks whether there any added chapters are to be considered "new".
|
||||||
* Note: This is about 3 times faster than using WP_Query.
|
*
|
||||||
*
|
* @since 5.26.0
|
||||||
* @since 5.26.0
|
*
|
||||||
*
|
* @param int $story_id Story ID.
|
||||||
* @global wpdb $wpdb WordPress database object.
|
* @param int[] $chapter_ids Current array of chapter IDs.
|
||||||
*
|
* @param int[] $previous_chapter_ids Previous array of chapter IDs.
|
||||||
* @param int $story_id Story ID.
|
*
|
||||||
* @param int[] $chapter_ids Array of chapter IDs.
|
* @return bool True or false.
|
||||||
*
|
*/
|
||||||
* @return int[] Filtered array of chapter IDs.
|
function fictioneer_sql_has_new_story_chapters( $story_id, $chapter_ids, $previous_chapter_ids ) {
|
||||||
*/
|
global $wpdb;
|
||||||
|
|
||||||
function fictioneer_sql_filter_valid_chapter_ids( $story_id, $chapter_ids ) {
|
// Any chapters added?
|
||||||
global $wpdb;
|
$chapter_diff = array_diff( $chapter_ids, $previous_chapter_ids );
|
||||||
|
|
||||||
// Prepare
|
if ( empty( $chapter_diff ) ) {
|
||||||
$chapter_ids = is_array( $chapter_ids ) ? $chapter_ids : [ $chapter_ids ];
|
return;
|
||||||
$chapter_ids = array_map( 'intval', $chapter_ids );
|
}
|
||||||
$chapter_ids = array_filter( $chapter_ids, function( $value ) { return $value > 0; } );
|
|
||||||
$chapter_ids = array_unique( $chapter_ids );
|
|
||||||
|
|
||||||
// Empty?
|
// Filter allowed statuses
|
||||||
if ( empty( $chapter_ids ) ) {
|
$allowed_statuses = apply_filters(
|
||||||
return [];
|
'fictioneer_filter_chapters_added_statuses',
|
||||||
|
['publish'],
|
||||||
|
$story_id
|
||||||
|
);
|
||||||
|
|
||||||
|
// Prepare placeholders for IN clauses
|
||||||
|
$chapter_placeholders = implode( ',', array_fill( 0, count( $chapter_diff ), '%d' ) );
|
||||||
|
$status_placeholders = implode( ',', array_fill( 0, count( $allowed_statuses ), '%s' ) );
|
||||||
|
|
||||||
|
// Prepare SQL
|
||||||
|
$sql =
|
||||||
|
"SELECT p.ID
|
||||||
|
FROM {$wpdb->posts} p
|
||||||
|
LEFT JOIN {$wpdb->postmeta} pm_hidden ON p.ID = pm_hidden.post_id
|
||||||
|
WHERE p.post_type = 'fcn_chapter'
|
||||||
|
AND p.ID IN ($chapter_placeholders)
|
||||||
|
AND p.post_status IN ($status_placeholders)
|
||||||
|
AND (pm_hidden.meta_key != 'fictioneer_chapter_hidden' OR pm_hidden.meta_value IS NULL)
|
||||||
|
LIMIT 1";
|
||||||
|
|
||||||
|
$query = $wpdb->prepare( $sql, array_merge( $chapter_diff, $allowed_statuses ) );
|
||||||
|
|
||||||
|
// Execute the query to check for new valid chapters
|
||||||
|
$new_chapters = $wpdb->get_col( $query );
|
||||||
|
|
||||||
|
// Report
|
||||||
|
return ! empty( $new_chapters );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup
|
|
||||||
$chapter_ids_placeholder = implode( ',', $chapter_ids ?: [0] );
|
|
||||||
|
|
||||||
$sql =
|
|
||||||
"SELECT p.ID
|
|
||||||
FROM {$wpdb->posts} p
|
|
||||||
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
|
|
||||||
WHERE p.post_type = 'fcn_chapter'
|
|
||||||
AND p.ID IN ($chapter_ids_placeholder)
|
|
||||||
AND p.post_status NOT IN ('trash', 'draft', 'auto-draft', 'inherit')";
|
|
||||||
|
|
||||||
// Chapters need to be assigned to a story, which can be turned off (but really should not)
|
|
||||||
if ( defined('FICTIONEER_FILTER_STORY_CHAPTERS') && FICTIONEER_FILTER_STORY_CHAPTERS ) {
|
|
||||||
$sql .= $wpdb->prepare( " AND pm.meta_key = %s AND pm.meta_value = %d", 'fictioneer_chapter_story', $story_id );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute
|
|
||||||
$filtered_ids = $wpdb->get_col( $sql );
|
|
||||||
|
|
||||||
// Restore order and return
|
|
||||||
return array_values( array_intersect( $chapter_ids, $filtered_ids ) );
|
|
||||||
}
|
}
|
||||||
|
@ -2369,42 +2369,8 @@ function fictioneer_save_story_metaboxes( $post_id ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remember when chapters have been added
|
// Remember when chapters have been added
|
||||||
$chapter_diff = array_diff( $chapter_ids, $previous_chapter_ids );
|
if ( fictioneer_sql_has_new_story_chapters( $post_id, $chapter_ids, $previous_chapter_ids ) ) {
|
||||||
|
update_post_meta( $post_id, 'fictioneer_chapters_added', current_time( 'mysql', true ) );
|
||||||
if ( ! empty( $chapter_diff ) ) {
|
|
||||||
$allowed_statuses = apply_filters(
|
|
||||||
'fictioneer_filter_chapters_added_statuses',
|
|
||||||
['publish'],
|
|
||||||
$post_id
|
|
||||||
);
|
|
||||||
|
|
||||||
$new_chapters = new WP_Query(
|
|
||||||
array(
|
|
||||||
'post_type' => 'fcn_chapter',
|
|
||||||
'post_status' => $allowed_statuses,
|
|
||||||
'post__in' => $chapter_diff,
|
|
||||||
'fields' => 'ids',
|
|
||||||
'posts_per_page' => 10, // Sensible limit in case of bulks
|
|
||||||
'update_post_meta_cache' => true,
|
|
||||||
'update_post_term_cache' => false, // Improve performance
|
|
||||||
'no_found_rows' => true // Improve performance
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( $new_chapters->have_posts() ) {
|
|
||||||
$valid_new_chapters = false;
|
|
||||||
|
|
||||||
foreach ( $new_chapters->posts as $chapter_id ) {
|
|
||||||
if ( ! get_post_meta( $chapter_id, 'fictioneer_chapter_hidden', true ) ) {
|
|
||||||
$valid_new_chapters = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $valid_new_chapters ) {
|
|
||||||
update_post_meta( $post_id, 'fictioneer_chapters_added', current_time( 'mysql', true ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log changes
|
// Log changes
|
||||||
|
Loading…
x
Reference in New Issue
Block a user