<!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>[55749] trunk: Comments: Always lazily load comment meta.</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 { white-space: pre-line; 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/55749">55749</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/55749","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-05-11 12:25:51 +0000 (Thu, 11 May 2023)</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'>Comments: Always lazily load comment meta.

In <a href="https://core.trac.wordpress.org/changeset/34270">[34270]</a> introduced lazy loading of comment meta. However, this was only in the context of `WP_Query`. Other parts of the codebase, like `WP_Comment_Query` did not lazily load comment meta. In this change, calls to `update_meta_cache` are now replaced with `wp_lazyload_comment_meta`, that instead of priming comment meta caches, just adds them to the queue to be primed it ever called. This results in far less database queries, as there a number of places where comment meta is being primed unnecessarily and never used. Adding everything to the comment meta queue, also means that if comment meta is used, that is all loaded in a single database / cache call.

Follow on from <a href="https://core.trac.wordpress.org/changeset/55671">[55671]</a>, <a href="https://core.trac.wordpress.org/changeset/55747">[55747]</a>.

Props spacedmonkey, peterwilsoncc, flixos90, mukesh27.
Fixes <a href="https://core.trac.wordpress.org/ticket/57801">#57801</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpcommentslisttablephp">trunk/src/wp-admin/includes/class-wp-comments-list-table.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcommentqueryphp">trunk/src/wp-includes/class-wp-comment-query.php</a></li>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunksrcwpincludescommenttemplatephp">trunk/src/wp-includes/comment-template.php</a></li>
<li><a href="#trunksrcwpincludescommentphp">trunk/src/wp-includes/comment.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="trunksrcwpadminincludesclasswpcommentslisttablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-comments-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-comments-list-table.php      2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/src/wp-admin/includes/class-wp-comments-list-table.php        2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -165,8 +165,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $_comments = get_comments( $args );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_array( $_comments ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        update_comment_cache( $_comments );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         $this->items       = array_slice( $_comments, 0, $comments_per_page );
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->extra_items = array_slice( $_comments, $comments_per_page );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcommentqueryphp"></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/class-wp-comment-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-comment-query.php  2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/src/wp-includes/class-wp-comment-query.php    2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,12 +481,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $comment_ids = array_map( 'intval', $comment_ids );
</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 ( $this->query_vars['update_comment_meta_cache'] ) {
+                       wp_lazyload_comment_meta( $comment_ids );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'ids' === $this->query_vars['fields'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->comments = $comment_ids;
</span><span class="cx" style="display: block; padding: 0 10px">                        return $this->comments;
</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">-                _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         _prime_comment_caches( $comment_ids, false );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Fetch full comment objects from the primed cache.
</span><span class="cx" style="display: block; padding: 0 10px">                $_comments = array();
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpqueryphp"></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/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/src/wp-includes/class-wp-query.php    2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2813,7 +2813,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $comment_ids = $wpdb->get_col( $comments_request );
</span><span class="cx" style="display: block; padding: 0 10px">                                wp_cache_add( $cache_key, $comment_ids, 'comment-queries' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        _prime_comment_caches( $comment_ids, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 _prime_comment_caches( $comment_ids );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Convert to WP_Comment.
</span><span class="cx" style="display: block; padding: 0 10px">                        /** @var WP_Comment[] */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3372,7 +3372,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $comment_ids = $wpdb->get_col( $comments_request );
</span><span class="cx" style="display: block; padding: 0 10px">                                wp_cache_add( $comment_cache_key, $comment_ids, 'comment-queries' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        _prime_comment_caches( $comment_ids, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 _prime_comment_caches( $comment_ids );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Convert to WP_Comment.
</span><span class="cx" style="display: block; padding: 0 10px">                        /** @var WP_Comment[] */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3487,11 +3487,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">-                // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
-               if ( ! empty( $this->comments ) ) {
-                       wp_queue_comments_for_comment_meta_lazyload( $this->comments );
-               }
-
</del><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">                         * Filters the array of retrieved posts after they've been fetched and
</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        2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/src/wp-includes/comment-template.php  2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1440,12 +1440,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $comment_author_url = esc_url( $commenter['comment_author_url'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $comment_args = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'orderby'                   => 'comment_date_gmt',
-               'order'                     => 'ASC',
-               'status'                    => 'approve',
-               'post_id'                   => $post->ID,
-               'no_found_rows'             => false,
-               'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'orderby'       => 'comment_date_gmt',
+               'order'         => 'ASC',
+               'status'        => 'approve',
+               'post_id'       => $post->ID,
+               'no_found_rows' => false,
</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">        if ( get_option( 'thread_comments' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2380,8 +2379,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $parsed_args['reverse_top_level'] = ( 'desc' === get_option( 'comment_order' ) );
</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">-        wp_queue_comments_for_comment_meta_lazyload( $_comments );
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( empty( $parsed_args['walker'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $walker = new Walker_Comment();
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span></span></pre></div>
<a id="trunksrcwpincludescommentphp"></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.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/comment.php 2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/src/wp-includes/comment.php   2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -485,6 +485,21 @@
</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">+ * Queue comment meta for lazy-loading.
+ *
+ * @since 6.3.0
+ *
+ * @param array $comment_ids List of comment IDs.
+ */
+function wp_lazyload_comment_meta( array $comment_ids ) {
+       if ( empty( $comment_ids ) ) {
+               return;
+       }
+       $lazyloader = wp_metadata_lazyloader();
+       $lazyloader->queue_objects( 'comment', $comment_ids );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Updates comment meta field based on comment ID.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Use the $prev_value parameter to differentiate between meta fields with the
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,7 +529,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Queues comments for metadata lazy-loading.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @see wp_lazyload_comment_meta()
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @param WP_Comment[] $comments Array of comment objects.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_queue_comments_for_comment_meta_lazyload( $comments ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -528,10 +546,7 @@
</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">-        if ( $comment_ids ) {
-               $lazyloader = wp_metadata_lazyloader();
-               $lazyloader->queue_objects( 'comment', $comment_ids );
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_lazyload_comment_meta( $comment_ids );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3331,6 +3346,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 6.1.0 This function is no longer marked as "private".
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 Use wp_lazyload_comment_meta() for lazy-loading of comment meta.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @see update_comment_cache()
</span><span class="cx" style="display: block; padding: 0 10px">  * @global wpdb $wpdb WordPress database abstraction object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3345,8 +3361,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! empty( $non_cached_ids ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", implode( ',', array_map( 'intval', $non_cached_ids ) ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                update_comment_cache( $fresh_comments, $update_meta_cache );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         update_comment_cache( $fresh_comments, false );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       if ( $update_meta_cache ) {
+               wp_lazyload_comment_meta( $comment_ids );
+       }
</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   2023-05-11 11:42:00 UTC (rev 55748)
+++ trunk/tests/phpunit/tests/comment/metaCache.php     2023-05-11 12:25:51 UTC (rev 55749)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,7 +11,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::update_comment_meta
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_update_comment_meta_cache_should_default_to_true() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_comment_meta_cache_should_default_to_lazy_loading() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,21 +22,58 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Clear comment cache, just in case.
</span><span class="cx" style="display: block; padding: 0 10px">                clean_comment_cache( $comment_ids );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $q = new WP_Comment_Query(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $num_queries = get_num_queries();
+               $q           = new WP_Comment_Query(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_ID' => $p,
</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">+                $this->assertSame( 2, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $num_queries = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $comment_ids as $cid ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        get_comment_meta( $cid, 'foo', 'bar' );
</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">-                $this->assertSame( $num_queries, get_num_queries() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 57801
+        *
+        * @covers ::wp_lazyload_comment_meta
+        */
+       public function test_update_comment_meta_cache_should_default_to_lazy_loading_fields_id() {
+               $p           = self::factory()->post->create( array( 'post_status' => 'publish' ) );
+               $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
+
+               foreach ( $comment_ids as $cid ) {
+                       update_comment_meta( $cid, 'foo', 'bar' );
+               }
+
+               // Clear comment cache, just in case.
+               clean_comment_cache( $comment_ids );
+
+               $num_queries = get_num_queries();
+               $q           = new WP_Comment_Query(
+                       array(
+                               'post_ID' => $p,
+                               'fields'  => 'ids',
+                       )
+               );
+
+               $this->assertSame( 1, get_num_queries() - $num_queries, 'Querying comments is expected to make two queries' );
+
+               $num_queries = get_num_queries();
+               foreach ( $comment_ids as $cid ) {
+                       get_comment_meta( $cid, 'foo', 'bar' );
+               }
+
+               $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta is expected to be lazy loaded' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 16894
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::update_comment_meta
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,12 +89,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Clear comment cache, just in case.
</span><span class="cx" style="display: block; padding: 0 10px">                clean_comment_cache( $comment_ids );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $q = new WP_Comment_Query(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $num_queries = get_num_queries();
+               $q           = new WP_Comment_Query(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_ID'                   => $p,
</span><span class="cx" style="display: block; padding: 0 10px">                                'update_comment_meta_cache' => true,
</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">+                $this->assertSame( 2, get_num_queries() - $num_queries, 'Comments should be queries and primed in two database queries' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $num_queries = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $comment_ids as $cid ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -64,10 +103,48 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        get_comment_meta( $cid, 'foo', 'bar' );
</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">-                $this->assertSame( $num_queries, get_num_queries() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 57801
+        *
+        * @covers ::update_comment_meta
+        */
+       public function test_update_comment_meta_cache_true_multiple() {
+               $posts           = self::factory()->post->create_many( 3 );
+               $all_comment_ids = array();
+               foreach ( $posts as $p ) {
+                       $comment_ids = self::factory()->comment->create_post_comments( $p, 3 );
+
+                       foreach ( $comment_ids as $cid ) {
+                               update_comment_meta( $cid, 'foo', 'bar' );
+                               $all_comment_ids[] = $cid;
+                       }
+
+                       $num_queries = get_num_queries();
+                       $q           = new WP_Comment_Query(
+                               array(
+                                       'post_ID'                   => $p,
+                                       'update_comment_meta_cache' => true,
+                               )
+                       );
+                       $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment query should only add one query' );
+               }
+
+               $filter = new MockAction();
+               add_filter( 'update_comment_metadata_cache', array( $filter, 'filter' ), 10, 2 );
+               $num_queries = get_num_queries();
+               get_comment_meta( $comment_ids[0], 'foo', 'bar' );
+
+               $this->assertSame( 1, get_num_queries() - $num_queries, 'Comment meta should be loaded in one database query' );
+               $args              = $filter->get_args();
+               $first             = reset( $args );
+               $prime_comment_ids = end( $first );
+               $this->assertSameSets( $prime_comment_ids, $all_comment_ids, 'All comment meta should be loaded all at once' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 16894
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::update_comment_meta
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -92,7 +169,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        get_comment_meta( $cid, 'foo', 'bar' );
</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">-                $this->assertSame( $num_queries + 3, get_num_queries() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 3, get_num_queries() - $num_queries );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -120,12 +197,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                // First request will hit the database.
</span><span class="cx" style="display: block; padding: 0 10px">                                $num_queries = get_num_queries();
</span><span class="cx" style="display: block; padding: 0 10px">                                get_comment_meta( $comment_ids[0], 'sauce' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->assertSame( $num_queries + 1, get_num_queries() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->assertSame( 1, get_num_queries() - $num_queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Second and third requests should be in cache.
</span><span class="cx" style="display: block; padding: 0 10px">                                get_comment_meta( $comment_ids[1], 'sauce' );
</span><span class="cx" style="display: block; padding: 0 10px">                                get_comment_meta( $comment_ids[2], 'sauce' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->assertSame( $num_queries + 1, get_num_queries() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->assertSame( 1, get_num_queries() - $num_queries );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,6 +211,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 34047
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::get_comment_meta
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @covers ::wp_lazyload_comment_meta
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_comment_meta_should_be_lazy_loaded_in_comment_feed_queries() {
</span><span class="cx" style="display: block; padding: 0 10px">                $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -182,6 +260,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 34047
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::get_comment_meta
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @covers ::wp_lazyload_comment_meta
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_comment_meta_should_be_lazy_loaded_in_single_post_comment_feed_queries() {
</span><span class="cx" style="display: block; padding: 0 10px">                $posts = self::factory()->post->create_many( 2, array( 'post_status' => 'publish' ) );
</span></span></pre>
</div>
</div>

</body>
</html>