From 542ac152fc8250a44f6ae6f1817ad45a985565e7 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 5 Sep 2015 22:24:16 +0000 Subject: [PATCH] In `wp_list_comments()`, update the comment meta cache when the comments derive from `WP_Query` and the new `->comment_meta_cached` prop is `false`. There are no uses of `wp_list_comments()` in Core where `$comments` are passed as the 2nd argument. Adds unit tests. Props wonderboymusic, bradt. Fixes #16894. git-svn-id: https://develop.svn.wordpress.org/trunk@33925 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 6 +++ src/wp-includes/query.php | 9 ++++ tests/phpunit/tests/comment/metaCache.php | 64 +++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/phpunit/tests/comment/metaCache.php diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index 8c4c60e520..5c59348fc5 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -2116,6 +2116,12 @@ function wp_list_comments( $args = array(), $comments = null ) { } else { $_comments = $wp_query->comments; } + + if ( ! $wp_query->comment_meta_cached ) { + $comment_ids = wp_list_pluck( $_comments, 'comment_ID' ); + update_meta_cache( 'comment', $comment_ids ); + $wp_query->comment_meta_cached = true; + } } if ( '' === $r['per_page'] && get_option('page_comments') ) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 4abfa2da11..0d45bcbbf8 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1292,6 +1292,15 @@ class WP_Query { */ public $thumbnails_cached = false; + /** + * Set if comment meta has already been cached + * + * @since 4.4.0 + * @access public + * @var bool + */ + public $comment_meta_cached = false; + /** * Cached list of search stopwords. * diff --git a/tests/phpunit/tests/comment/metaCache.php b/tests/phpunit/tests/comment/metaCache.php new file mode 100644 index 0000000000..a4d5ffadc7 --- /dev/null +++ b/tests/phpunit/tests/comment/metaCache.php @@ -0,0 +1,64 @@ +factory->post->create( array( + 'post_status' => 'publish' + ) ); + + $comment_ids = $this->factory->comment->create_post_comments( $post_id, 10 ); + + foreach ( $comment_ids as $cid ) { + update_comment_meta( $cid, 'sauce', 'fire' ); + } + + $post = get_post( $post_id ); + + $this->assertEquals( $post->comment_count, count( $comment_ids ) ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertTrue( is_single() ); + $this->assertTrue( have_posts() ); + + global $wp_query; + + while ( have_posts() ) { + the_post(); + + $comment_args = array( + 'order' => 'ASC', + 'orderby' => 'comment_date_gmt', + 'status' => 'approve', + 'post_id' => get_the_ID(), + ); + + $comments = get_comments( $comment_args ); + + // This is beyond awful + $wp_query->comments = $comments; + + wp_list_comments( array( + 'echo' => false, + 'callback' => array( $this, '_comment_callback' ) + ) ); + } + } + + public function _comment_callback( $comment ) { + global $wpdb; + + get_comment_meta( $comment->comment_ID, 'sauce' ); + + if ( 0 === $this->i ) { + $this->queries = $wpdb->num_queries; + } else { + $this->assertEquals( $this->queries, $wpdb->num_queries ); + } + + $this->i++; + } +} \ No newline at end of file