From 12329f5ef86e4b10becb7825e0645bf9324cd70f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 17 Sep 2015 19:29:46 +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. git-svn-id: https://develop.svn.wordpress.org/trunk@34268 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-comment-query.php | 7 +- src/wp-includes/comment-functions.php | 15 ++++- tests/phpunit/tests/comment/metaCache.php | 77 ++++++++++++++++++++++ 3 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index ec35a2adac..787e769506 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/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/src/wp-includes/comment-functions.php b/src/wp-includes/comment-functions.php index 08ddfd2853..1f8f54f92a 100644 --- a/src/wp-includes/comment-functions.php +++ b/src/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/tests/phpunit/tests/comment/metaCache.php b/tests/phpunit/tests/comment/metaCache.php index a24f56fd5e..6cc0f0bd85 100644 --- a/tests/phpunit/tests/comment/metaCache.php +++ b/tests/phpunit/tests/comment/metaCache.php @@ -4,6 +4,83 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase { protected $i = 0; protected $queries = 0; + /** + * @ticket 16894 + */ + public function test_update_comment_meta_cache_should_default_to_true() { + global $wpdb; + + $p = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $comment_ids = $this->factory->comment->create_post_comments( $p, 3 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'foo', 'bar' ); + } + + $q = new WP_Comment_Query( array( + 'post_ID' => $p, + ) ); + + $num_queries = $wpdb->num_queries; + foreach ( $comment_ids as $cid ) { + get_comment_meta( $cid, 'foo', 'bar' ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + + /** + * @ticket 16894 + */ + public function test_update_comment_meta_cache_true() { + global $wpdb; + + $p = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $comment_ids = $this->factory->comment->create_post_comments( $p, 3 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'foo', 'bar' ); + } + + $q = new WP_Comment_Query( array( + 'post_ID' => $p, + 'update_comment_meta_cache' => true, + ) ); + + $num_queries = $wpdb->num_queries; + foreach ( $comment_ids as $cid ) { + get_comment_meta( $cid, 'foo', 'bar' ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + + /** + * @ticket 16894 + */ + public function test_update_comment_meta_cache_false() { + global $wpdb; + + $p = $this->factory->post->create( array( 'post_status' => 'publish' ) ); + $comment_ids = $this->factory->comment->create_post_comments( $p, 3 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'foo', 'bar' ); + } + + $q = new WP_Comment_Query( array( + 'post_ID' => $p, + 'update_comment_meta_cache' => false, + ) ); + + $num_queries = $wpdb->num_queries; + foreach ( $comment_ids as $cid ) { + get_comment_meta( $cid, 'foo', 'bar' ); + } + + $this->assertSame( $num_queries + 3, $wpdb->num_queries ); + } + public function test_comment_meta_cache() { $post_id = $this->factory->post->create( array( 'post_status' => 'publish'