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
This commit is contained in:
Boone Gorges 2015-09-17 19:30:27 +00:00
parent cdc6bc8ad5
commit bb43f72692
3 changed files with 19 additions and 5 deletions

View File

@ -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;

View File

@ -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 );
}
}
//

View File

@ -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.