<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[34711] trunk: Improve lazyloading of comment meta in `WP_Query` loops.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/34711">34711</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/34711","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-09-30 01:34:54 +0000 (Wed, 30 Sep 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Improve lazyloading of comment meta in `WP_Query` loops.

Lazy-loading logic is moved to a method on `WP_Query`. This makes it possible
for comment feeds to take advantage of metadata lazyloading, in addition to
comments loaded via `comments_template()`.

This new technique parallels the termmeta lazyloading technique introduced in
<a href="https://core.trac.wordpress.org/changeset/34704">[34704]</a>.

Fixes <a href="https://core.trac.wordpress.org/ticket/34047">#34047</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommentfunctionsphp">trunk/src/wp-includes/comment-functions.php</a></li>
<li><a href="#trunksrcwpincludescommenttemplatephp">trunk/src/wp-includes/comment-template.php</a></li>
<li><a href="#trunksrcwpincludesdefaultfiltersphp">trunk/src/wp-includes/default-filters.php</a></li>
<li><a href="#trunksrcwpincludesqueryphp">trunk/src/wp-includes/query.php</a></li>
<li><a href="#trunktestsphpunittestscommentmetaCachephp">trunk/tests/phpunit/tests/comment/metaCache.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescommentfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment-functions.php       2015-09-30 01:24:33 UTC (rev 34710)
+++ trunk/src/wp-includes/comment-functions.php 2015-09-30 01:34:54 UTC (rev 34711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2452,30 +2452,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/**
- * Lazy load comment meta when inside of a `WP_Query` loop.
- *
- * @since 4.4.0
- *
- * @param null $check      The `$check` param passed from the 'pre_comment_metadata' hook.
- * @param int  $comment_id ID of the comment whose metadata is being cached.
- * @return null In order not to short-circuit `get_metadata()`.
- */
-function wp_lazyload_comment_meta( $check, $comment_id ) {
-       global $wp_query;
-
-       if ( ! empty( $wp_query->comments ) ) {
-               // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
-               $comment_ids = array();
-               foreach ( $wp_query->comments as $comment ) {
-                       $comment_ids[] = $comment->comment_ID;
-               }
-               update_meta_cache( 'comment', $comment_ids );
-       }
-
-       return $check;
-}
-
</del><span class="cx" style="display: block; padding: 0 10px"> //
</span><span class="cx" style="display: block; padding: 0 10px"> // Internal
</span><span class="cx" style="display: block; padding: 0 10px"> //
</span></span></pre></div>
<a id="trunksrcwpincludescommenttemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/comment-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment-template.php        2015-09-30 01:24:33 UTC (rev 34710)
+++ trunk/src/wp-includes/comment-template.php  2015-09-30 01:34:54 UTC (rev 34711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1320,6 +1320,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param int   $post_ID  Post ID.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        $wp_query->comments = apply_filters( 'comments_array', $comments_flat, $post->ID );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       // Set up lazy-loading for comment metadata.
+       add_action( 'get_comment_metadata', array( $wp_query, 'lazyload_comment_meta' ), 10, 2 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $comments = &$wp_query->comments;
</span><span class="cx" style="display: block; padding: 0 10px">        $wp_query->comment_count = count($wp_query->comments);
</span><span class="cx" style="display: block; padding: 0 10px">        $wp_query->max_num_comment_pages = $comment_query->max_num_pages;
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-filters.php 2015-09-30 01:24:33 UTC (rev 34710)
+++ trunk/src/wp-includes/default-filters.php   2015-09-30 01:34:54 UTC (rev 34711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -200,7 +200,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'pingback_ping_source_uri', 'pingback_ping_source_uri'            );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'xmlrpc_pingback_error',    'xmlrpc_pingback_error'               );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'title_save_pre',           'trim'                                );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'get_comment_metadata',     'wp_lazyload_comment_meta',     10, 2 );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/query.php   2015-09-30 01:24:33 UTC (rev 34710)
+++ trunk/src/wp-includes/query.php     2015-09-30 01:34:54 UTC (rev 34711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1312,6 +1312,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $updated_term_meta_cache = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Whether the comment meta cache for matched posts has been primed.
+        *
+        * @since 4.4.0
+        * @access protected
+        * @var bool
+        */
+       public $updated_comment_meta_cache = false;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Cached list of search stopwords.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3682,6 +3691,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
+               if ( ! empty( $this->comments ) ) {
+                       add_action( 'get_comment_metadata', array( $this, 'lazyload_comment_meta' ), 10, 2 );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $q['suppress_filters'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Filter the array of retrieved posts after they've been fetched and
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4816,6 +4830,50 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $check;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Lazy-load comment meta when inside of a `WP_Query` loop.
+        *
+        * This method is public so that it can be used as a filter callback. As a rule, there is no need to invoke it
+        * directly, from either inside or outside the `WP_Query` object.
+        *
+        * @since 4.4.0
+        *
+        * @param null $check      The `$check` param passed from the 'pre_comment_metadata' hook.
+        * @param int  $comment_id ID of the comment whose metadata is being cached.
+        * @return null In order not to short-circuit `get_metadata()`.
+        */
+       public function lazyload_comment_meta( $check, $comment_id ) {
+               /*
+                * We only do this once per `WP_Query` instance.
+                * Can't use `remove_action()` because of non-unique object hashes.
+                */
+               if ( $this->updated_comment_meta_cache ) {
+                       return $check;
+               }
+
+               // Don't use `wp_list_pluck()` to avoid by-reference manipulation.
+               $comment_ids = array();
+               if ( is_array( $this->comments ) ) {
+                       foreach ( $this->comments as $comment ) {
+                               $comment_ids[] = $comment->comment_ID;
+                       }
+               }
+
+               /*
+                * Only update the metadata cache for comments belonging to these posts if the comment_id passed
+                * to `get_comment_meta()` matches one of those comments. This prevents a single call to
+                * `get_comment_meta()` from priming metadata for all `WP_Query` objects.
+                */
+               if ( in_array( $comment_id, $comment_ids ) ) {
+                       update_meta_cache( 'comment', $comment_ids );
+                       $this->updated_comment_meta_cache = true;
+               } elseif ( empty( $comment_ids ) ) {
+                       $this->updated_comment_meta_cache = true;
+               }
+
+               return $check;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestscommentmetaCachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment/metaCache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/metaCache.php   2015-09-30 01:24:33 UTC (rev 34710)
+++ trunk/tests/phpunit/tests/comment/metaCache.php     2015-09-30 01:34:54 UTC (rev 34711)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -121,4 +121,93 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @group 34047
+        */
+       public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
+               global $wpdb;
+
+               $posts = $this->factory->post->create_many( 2, array( 'post_status' => 'publish' ) );
+
+               $now = time();
+               $comments = array();
+               for ( $i = 0; $i < 5; $i++ ) {
+                       $comments[] = $this->factory->comment->create( array(
+                               'comment_post_ID' => $posts[0],
+                               'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
+                       ) );
+               }
+
+               foreach ( $comments as $c ) {
+                       add_comment_meta( $c, 'foo', 'bar' );
+               }
+
+               update_option( 'posts_per_rss', 3 );
+
+               $q = new WP_Query( array(
+                       'feed' => true,
+                       'withcomments' => true,
+               ) );
+
+               // First comment will cause the cache to be primed.
+               $num_queries = $wpdb->num_queries;
+               $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
+               $num_queries++;
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+
+               // Second comment from the results should not cause more queries.
+               $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+
+               // A comment from outside the results will not be primed.
+               $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
+               $num_queries++;
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+       }
+
+       /**
+        * @group 34047
+        */
+       public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
+               global $wpdb;
+
+               $posts = $this->factory->post->create_many( 2, array( 'post_status' => 'publish' ) );
+
+               $now = time();
+               $comments = array();
+               for ( $i = 0; $i < 5; $i++ ) {
+                       $comments[] = $this->factory->comment->create( array(
+                               'comment_post_ID' => $posts[0],
+                               'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 60 * $i ) ),
+                       ) );
+               }
+
+               foreach ( $comments as $c ) {
+                       add_comment_meta( $c, 'foo', 'bar' );
+               }
+
+               update_option( 'posts_per_rss', 3 );
+
+               $q = new WP_Query( array(
+                       'feed' => true,
+                       'withcomments' => true,
+                       'p' => $posts[0],
+               ) );
+
+               // First comment will cause the cache to be primed.
+               $num_queries = $wpdb->num_queries;
+               $this->assertSame( 'bar', get_comment_meta( $comments[0], 'foo', 'bar' ) );
+               $num_queries++;
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+
+               // Second comment from the results should not cause more queries.
+               $this->assertSame( 'bar', get_comment_meta( $comments[1], 'foo', 'bar' ) );
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+
+               // A comment from outside the results will not be primed.
+               $this->assertSame( 'bar', get_comment_meta( $comments[4], 'foo', 'bar' ) );
+               $num_queries++;
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>