Comments: Always lazily load comment meta.

In [34270] introduced lazy loading of comment meta. However, this was only in the context of `WP_Query`. Other parts of the codebase, like `WP_Comment_Query` did not lazily load comment meta. In this change, calls to `update_meta_cache` are now replaced with `wp_lazyload_comment_meta`, that instead of priming comment meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where comment meta is being primed unnecessarily and never used. Adding everything to the comment meta queue, also means that if comment meta is used, that is all loaded in a single database / cache call.

Follow on from [55671], [55747].

Props spacedmonkey, peterwilsoncc, flixos90, mukesh27.
Fixes #57801.

git-svn-id: https://develop.svn.wordpress.org/trunk@55749 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-05-11 12:25:51 +00:00
parent 1534d12067
commit 3114eda235
6 changed files with 124 additions and 31 deletions

View File

@@ -484,6 +484,21 @@ function get_comment_meta( $comment_id, $key = '', $single = false ) {
return get_metadata( 'comment', $comment_id, $key, $single );
}
/**
* Queue comment meta for lazy-loading.
*
* @since 6.3.0
*
* @param array $comment_ids List of comment IDs.
*/
function wp_lazyload_comment_meta( array $comment_ids ) {
if ( empty( $comment_ids ) ) {
return;
}
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'comment', $comment_ids );
}
/**
* Updates comment meta field based on comment ID.
*
@@ -514,6 +529,9 @@ function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value =
* Queues comments for metadata lazy-loading.
*
* @since 4.5.0
* @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
*
* @see wp_lazyload_comment_meta()
*
* @param WP_Comment[] $comments Array of comment objects.
*/
@@ -528,10 +546,7 @@ function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
}
}
if ( $comment_ids ) {
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'comment', $comment_ids );
}
wp_lazyload_comment_meta( $comment_ids );
}
/**
@@ -3331,6 +3346,7 @@ function update_comment_cache( $comments, $update_meta_cache = true ) {
*
* @since 4.4.0
* @since 6.1.0 This function is no longer marked as "private".
* @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
*
* @see update_comment_cache()
* @global wpdb $wpdb WordPress database abstraction object.
@@ -3345,7 +3361,11 @@ function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) {
if ( ! empty( $non_cached_ids ) ) {
$fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
update_comment_cache( $fresh_comments, $update_meta_cache );
update_comment_cache( $fresh_comments, false );
}
if ( $update_meta_cache ) {
wp_lazyload_comment_meta( $comment_ids );
}
}