Lazy-load comment meta on single post pages.

[34268] introduced cache priming for commentmeta, enabled by default. To
ensure performance on single post pages - where commentmeta is most likely
to cause performance issues - we disable up-front cache-priming. Instead, we
prime commentmeta caches for all comments in the loop the first time
`get_comment_meta()` is called on the page.

Props bradt, dd32, wonderboymusic, boonebgorges.
Fixes #16894.

git-svn-id: https://develop.svn.wordpress.org/trunk@34270 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-09-17 20:00:31 +00:00
parent 5ea07785b3
commit 504deb52fc
4 changed files with 49 additions and 46 deletions

View File

@@ -2376,6 +2376,30 @@ function update_comment_cache( $comments, $update_meta_cache = true ) {
}
}
/**
* Lazy load comment meta when inside of a `WP_Query` loop.
*
* @since 4.4.0
*
* @param null $check The `$check` param passed from the 'pre_comment_metadata' hook.
* @param int $comment_id ID of the comment whose metadata is being cached.
* @return null In order not to short-circuit `get_metadata()`.
*/
function wp_lazyload_comment_meta( $check, $comment_id ) {
global $wp_query;
if ( ! empty( $wp_query->comments ) ) {
// Don't use `wp_list_pluck()` to avoid by-reference manipulation.
$comment_ids = array();
foreach ( $wp_query->comments as $comment ) {
$comment_ids[] = $comment->comment_ID;
}
update_meta_cache( 'comment', $comment_ids );
}
return $check;
}
//
// Internal
//