From bb43f726926e2e97a0d107383c9612d29c493850 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 17 Sep 2015 19:30:27 +0000 Subject: [PATCH] Prime comment meta caches in `WP_Comment_Query`. The new 'update_comment_meta_cache' parameter, which defaults to `true`, can be used to disable this behavior. `update_comment_cache()` has been updated to support an `$update_meta_cache` parameter, which also updates to true; this matches the pattern we use for priming post caches. See #16894. Built from https://develop.svn.wordpress.org/trunk@34268 git-svn-id: http://core.svn.wordpress.org/trunk@34232 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-comment-query.php | 7 +++++-- wp-includes/comment-functions.php | 15 +++++++++++++-- wp-includes/version.php | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index ec35a2adac..787e769506 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -94,7 +94,7 @@ class WP_Comment_Query { * * @since 4.2.0 * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. - * @since 4.4.0 Order by `comment__in` was added. + * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache` was added. * @access public * * @param string|array $query { @@ -161,6 +161,8 @@ class WP_Comment_Query { * @type array $type__in Include comments from a given array of comment types. Default empty. * @type array $type__not_in Exclude comments from a given array of comment types. Default empty. * @type int $user_id Include comments for a specific user ID. Default empty. + * @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments. + * Default true. * } */ public function __construct( $query = '' ) { @@ -201,6 +203,7 @@ class WP_Comment_Query { 'meta_value' => '', 'meta_query' => '', 'date_query' => null, // See WP_Date_Query + 'update_comment_meta_cache' => true, ); if ( ! empty( $query ) ) { @@ -698,7 +701,7 @@ class WP_Comment_Query { wp_cache_add( $cache_key, $comments, 'comment' ); if ( '*' === $fields ) { - update_comment_cache( $comments ); + update_comment_cache( $comments, $this->query_vars['update_comment_meta_cache'] ); } $this->comments = $comments; diff --git a/wp-includes/comment-functions.php b/wp-includes/comment-functions.php index 08ddfd2853..1f8f54f92a 100644 --- a/wp-includes/comment-functions.php +++ b/wp-includes/comment-functions.php @@ -2357,12 +2357,23 @@ function clean_comment_cache($ids) { * cache using the comment group with the key using the ID of the comments. * * @since 2.3.0 + * @since 4.4.0 Introduced the `$update_meta_cache` parameter. * - * @param array $comments Array of comment row objects + * @param array $comments Array of comment row objects + * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. */ -function update_comment_cache($comments) { +function update_comment_cache( $comments, $update_meta_cache = true ) { foreach ( (array) $comments as $comment ) wp_cache_add($comment->comment_ID, $comment, 'comment'); + + if ( $update_meta_cache ) { + // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. + $comment_ids = array(); + foreach ( $comments as $comment ) { + $comment_ids[] = $comment->comment_ID; + } + update_meta_cache( 'comment', $comment_ids ); + } } // diff --git a/wp-includes/version.php b/wp-includes/version.php index 1809c8b321..3d5a34a2b7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34266'; +$wp_version = '4.4-alpha-34268'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.