Make WordPress Core

Changeset 34268


Ignore:
Timestamp:
09/17/2015 07:29:46 PM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r34212 r34268  
    9595     * @since 4.2.0
    9696     * @since 4.4.0 `$parent__in` and `$parent__not_in` were added.
    97      * @since 4.4.0 Order by `comment__in` was added.
     97     * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache` was added.
    9898     * @access public
    9999     *
     
    162162     *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
    163163     *     @type int          $user_id             Include comments for a specific user ID. Default empty.
     164     *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
     165     *                                                   Default true.
    164166     * }
    165167     */
     
    202204            'meta_query' => '',
    203205            'date_query' => null, // See WP_Date_Query
     206            'update_comment_meta_cache' => true,
    204207        );
    205208
     
    699702        wp_cache_add( $cache_key, $comments, 'comment' );
    700703        if ( '*' === $fields ) {
    701             update_comment_cache( $comments );
     704            update_comment_cache( $comments, $this->query_vars['update_comment_meta_cache'] );
    702705        }
    703706
  • trunk/src/wp-includes/comment-functions.php

    r34253 r34268  
    23582358 *
    23592359 * @since 2.3.0
    2360  *
    2361  * @param array $comments Array of comment row objects
    2362  */
    2363 function update_comment_cache($comments) {
     2360 * @since 4.4.0 Introduced the `$update_meta_cache` parameter.
     2361 *
     2362 * @param array $comments          Array of comment row objects
     2363 * @param bool  $update_meta_cache Whether to update commentmeta cache. Default true.
     2364 */
     2365function update_comment_cache( $comments, $update_meta_cache = true ) {
    23642366    foreach ( (array) $comments as $comment )
    23652367        wp_cache_add($comment->comment_ID, $comment, 'comment');
     2368
     2369    if ( $update_meta_cache ) {
     2370        // Avoid `wp_list_pluck()` in case `$comments` is passed by reference.
     2371        $comment_ids = array();
     2372        foreach ( $comments as $comment ) {
     2373            $comment_ids[] = $comment->comment_ID;
     2374        }
     2375        update_meta_cache( 'comment', $comment_ids );
     2376    }
    23662377}
    23672378
  • trunk/tests/phpunit/tests/comment/metaCache.php

    r33926 r34268  
    44    protected $i = 0;
    55    protected $queries = 0;
     6
     7    /**
     8     * @ticket 16894
     9     */
     10    public function test_update_comment_meta_cache_should_default_to_true() {
     11        global $wpdb;
     12
     13        $p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     14        $comment_ids = $this->factory->comment->create_post_comments( $p, 3 );
     15
     16        foreach ( $comment_ids as $cid ) {
     17            update_comment_meta( $cid, 'foo', 'bar' );
     18        }
     19
     20        $q = new WP_Comment_Query( array(
     21            'post_ID' => $p,
     22        ) );
     23
     24        $num_queries = $wpdb->num_queries;
     25        foreach ( $comment_ids as $cid ) {
     26            get_comment_meta( $cid, 'foo', 'bar' );
     27        }
     28
     29        $this->assertSame( $num_queries, $wpdb->num_queries );
     30    }
     31
     32    /**
     33     * @ticket 16894
     34     */
     35    public function test_update_comment_meta_cache_true() {
     36        global $wpdb;
     37
     38        $p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     39        $comment_ids = $this->factory->comment->create_post_comments( $p, 3 );
     40
     41        foreach ( $comment_ids as $cid ) {
     42            update_comment_meta( $cid, 'foo', 'bar' );
     43        }
     44
     45        $q = new WP_Comment_Query( array(
     46            'post_ID' => $p,
     47            'update_comment_meta_cache' => true,
     48        ) );
     49
     50        $num_queries = $wpdb->num_queries;
     51        foreach ( $comment_ids as $cid ) {
     52            get_comment_meta( $cid, 'foo', 'bar' );
     53        }
     54
     55        $this->assertSame( $num_queries, $wpdb->num_queries );
     56    }
     57
     58    /**
     59     * @ticket 16894
     60     */
     61    public function test_update_comment_meta_cache_false() {
     62        global $wpdb;
     63
     64        $p = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     65        $comment_ids = $this->factory->comment->create_post_comments( $p, 3 );
     66
     67        foreach ( $comment_ids as $cid ) {
     68            update_comment_meta( $cid, 'foo', 'bar' );
     69        }
     70
     71        $q = new WP_Comment_Query( array(
     72            'post_ID' => $p,
     73            'update_comment_meta_cache' => false,
     74        ) );
     75
     76        $num_queries = $wpdb->num_queries;
     77        foreach ( $comment_ids as $cid ) {
     78            get_comment_meta( $cid, 'foo', 'bar' );
     79        }
     80
     81        $this->assertSame( $num_queries + 3, $wpdb->num_queries );
     82    }
    683
    784    public function test_comment_meta_cache() {
Note: See TracChangeset for help on using the changeset viewer.