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
This commit is contained in:
parent
b08a176488
commit
12329f5ef8
@ -94,7 +94,7 @@ class WP_Comment_Query {
|
|||||||
*
|
*
|
||||||
* @since 4.2.0
|
* @since 4.2.0
|
||||||
* @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
|
* @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
|
* @access public
|
||||||
*
|
*
|
||||||
* @param string|array $query {
|
* @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__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 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 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 = '' ) {
|
public function __construct( $query = '' ) {
|
||||||
@ -201,6 +203,7 @@ class WP_Comment_Query {
|
|||||||
'meta_value' => '',
|
'meta_value' => '',
|
||||||
'meta_query' => '',
|
'meta_query' => '',
|
||||||
'date_query' => null, // See WP_Date_Query
|
'date_query' => null, // See WP_Date_Query
|
||||||
|
'update_comment_meta_cache' => true,
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( ! empty( $query ) ) {
|
if ( ! empty( $query ) ) {
|
||||||
@ -698,7 +701,7 @@ class WP_Comment_Query {
|
|||||||
|
|
||||||
wp_cache_add( $cache_key, $comments, 'comment' );
|
wp_cache_add( $cache_key, $comments, 'comment' );
|
||||||
if ( '*' === $fields ) {
|
if ( '*' === $fields ) {
|
||||||
update_comment_cache( $comments );
|
update_comment_cache( $comments, $this->query_vars['update_comment_meta_cache'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->comments = $comments;
|
$this->comments = $comments;
|
||||||
|
@ -2357,12 +2357,23 @@ function clean_comment_cache($ids) {
|
|||||||
* cache using the comment group with the key using the ID of the comments.
|
* cache using the comment group with the key using the ID of the comments.
|
||||||
*
|
*
|
||||||
* @since 2.3.0
|
* @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 )
|
foreach ( (array) $comments as $comment )
|
||||||
wp_cache_add($comment->comment_ID, $comment, '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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -4,6 +4,83 @@ class Tests_Comment_Meta_Cache extends WP_UnitTestCase {
|
|||||||
protected $i = 0;
|
protected $i = 0;
|
||||||
protected $queries = 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() {
|
public function test_comment_meta_cache() {
|
||||||
$post_id = $this->factory->post->create( array(
|
$post_id = $this->factory->post->create( array(
|
||||||
'post_status' => 'publish'
|
'post_status' => 'publish'
|
||||||
|
Loading…
Reference in New Issue
Block a user