Optimize blog story IDs validation

This commit is contained in:
Tetrakern 2024-10-27 22:44:09 +01:00
parent 5f99927441
commit 7c6783f52f
2 changed files with 50 additions and 25 deletions

View File

@ -512,12 +512,12 @@ if ( ! function_exists( 'fictioneer_sql_filter_valid_collection_ids' ) ) {
// Prepare // Prepare
$item_ids = is_array( $item_ids ) ? $item_ids : [ $item_ids ]; $item_ids = is_array( $item_ids ) ? $item_ids : [ $item_ids ];
$item_ids = array_map( 'intval', $item_ids ); $item_ids = array_map( 'intval', $item_ids );
$item_ids = array_filter( $item_ids, function( $value ) { return $value > 0; } );
if ( empty( $item_ids ) ) { if ( empty( $item_ids ) ) {
return []; return [];
} }
$item_ids = array_filter( $item_ids, function( $value ) { return $value > 0; } );
$item_ids = array_unique( $item_ids ); $item_ids = array_unique( $item_ids );
// Exclude forbidden IDs // Exclude forbidden IDs
@ -559,6 +559,53 @@ if ( ! function_exists( 'fictioneer_sql_filter_valid_collection_ids' ) ) {
} }
} }
if ( ! function_exists( 'fictioneer_sql_filter_valid_blog_story_ids' ) ) {
/**
* Filters out non-valid blog story array IDs
*
* Note: This is a lot faster than using WP_Query().
*
* @since 5.26.0
*
* @global wpdb $wpdb WordPress database object.
*
* @param int $story_author_id Author ID of the story.
* @param int[] $story_blogs Array of story blog IDs.
*
* @return int[] Filtered and validated array of item IDs.
*/
function fictioneer_sql_filter_valid_blog_story_ids( $story_author_id, $story_blogs ) {
global $wpdb;
// Prepare
$story_blogs = is_array( $story_blogs ) ? $story_blogs : [ $story_blogs ];
$story_blogs = array_map( 'intval', $story_blogs );
$story_blogs = array_filter( $story_blogs, function( $value ) { return $value > 0; } );
if ( empty( $story_blogs ) ) {
return [];
}
$story_blogs = array_unique( $story_blogs );
// Prepare placeholders
$placeholders = implode( ',', array_fill( 0, count( $story_blogs ), '%d' ) );
// Prepare SQL query
$sql =
"SELECT p.ID
FROM {$wpdb->posts} p
WHERE p.post_author = %d
AND p.ID IN ($placeholders)
AND p.post_type = 'fcn_story'
AND p.post_status IN ('publish', 'private', 'future')";
// Execute and return
return $wpdb->get_col( $wpdb->prepare( $sql, $story_author_id, ...$story_blogs ) );
}
}
if ( ! function_exists( 'fictioneer_sql_has_new_story_chapters' ) ) { if ( ! function_exists( 'fictioneer_sql_has_new_story_chapters' ) ) {
/** /**
* Checks whether there any added chapters are to be considered "new". * Checks whether there any added chapters are to be considered "new".

View File

@ -3359,31 +3359,9 @@ function fictioneer_save_extra_metabox( $post_id ) {
// Story blogs // Story blogs
if ( isset( $_POST['fictioneer_post_story_blogs'] ) && $post_type === 'post' ) { if ( isset( $_POST['fictioneer_post_story_blogs'] ) && $post_type === 'post' ) {
$story_blogs = fictioneer_explode_list( $_POST['fictioneer_post_story_blogs'] ); $story_blogs = fictioneer_explode_list( $_POST['fictioneer_post_story_blogs'] );
$story_blogs = fictioneer_sql_filter_valid_blog_story_ids( $post_author_id, $story_blogs );
if ( ! empty( $story_blogs ) ) { $fields['fictioneer_post_story_blogs'] = array_map( 'strval', $story_blogs ); // Safer to match with LIKE in SQL
$story_blogs = array_map( 'absint', $story_blogs );
$story_blogs = array_unique( $story_blogs );
// Ensure the stories belong to the post author
$blog_story_query = new WP_Query(
array(
'author' => $post_author_id,
'post_type' => 'fcn_story',
'post_status' => ['publish', 'private'],
'post__in' => $story_blogs ?: [0], // Must not be empty!
'fields' => 'ids',
'posts_per_page'=> -1,
'update_post_meta_cache' => false, // Improve performance
'update_post_term_cache' => false, // Improve performance
'no_found_rows' => true // Improve performance
)
);
$story_blogs = $blog_story_query->posts;
$story_blogs = array_map( 'strval', $story_blogs ); // Safer to match with LIKE in SQL
}
$fields['fictioneer_post_story_blogs'] = $story_blogs;
} }
// Patreon // Patreon