<!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>[34535] trunk: Use `WP_Comment_Query` in `get_page_of_comment()`.</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/34535">34535</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/34535","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-25 05:10:40 +0000 (Fri, 25 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'>Use `WP_Comment_Query` in `get_page_of_comment()`.
This change allows `get_page_of_comment()` to use `WP_Comment_Query`'s native
caching mechanisms.
Props boonebgorges, Viper007Bond, wmertens, jeremyfelt.
Fixes <a href="https://core.trac.wordpress.org/ticket/11334">#11334</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescommentfunctionsphp">trunk/src/wp-includes/comment-functions.php</a></li>
<li><a href="#trunktestsphpunittestscommentgetPageOfCommentphp">trunk/tests/phpunit/tests/comment/getPageOfComment.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-25 04:43:57 UTC (rev 34534)
+++ trunk/src/wp-includes/comment-functions.php 2015-09-25 05:10:40 UTC (rev 34535)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -821,9 +821,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.7.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global wpdb $wpdb
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
- * @param int $comment_ID Comment ID.
- * @param array $args Optional args.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $comment_ID Comment ID.
+ * @param array $args {
+ * Array of optional arguments.
+ * @type string $type Limit paginated comments to those matching a given type. Accepts 'comment',
+ * 'trackback', 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
+ * Default is 'all'.
+ * @type int $per_page Per-page count to use when calculating pagination. Defaults to the value of the
+ * 'comments_per_page' option.
+ * @type int|string $max_depth If greater than 1, comment page will be determined for the top-level parent of
+ * `$comment_ID`. Defaults to the value of the 'thread_comments_depth' option.
+ * } *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return int|null Comment page number or null on error.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_page_of_comment( $comment_ID, $args = array() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -855,23 +863,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent )
</span><span class="cx" style="display: block; padding: 0 10px"> return get_page_of_comment( $comment->comment_parent, $args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $allowedtypes = array(
- 'comment' => '',
- 'pingback' => 'pingback',
- 'trackback' => 'trackback',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $comment_args = array(
+ 'type' => $args['type'],
+ 'post_ID' => $comment->comment_post_ID,
+ 'fields' => 'ids',
+ 'status' => 'approve',
+ 'date_query' => array(
+ array(
+ 'column' => "$wpdb->comments.comment_date_gmt",
+ 'before' => $comment->comment_date_gmt,
+ )
+ ),
</ins><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">- $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $older_comment_ids = get_comments( $comment_args );
+ $older_comment_count = count( $older_comment_ids );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Count comments older than this one
- $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // No older comments? Then it's page #1.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 0 == $oldercoms )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 0 == $older_comment_count )
</ins><span class="cx" style="display: block; padding: 0 10px"> return 1;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Divide comments older than this one by comments per page to get this comment's page number
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return ceil( ( $oldercoms + 1 ) / $args['per_page'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return ceil( ( $older_comment_count + 1 ) / $args['per_page'] );
</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="trunktestsphpunittestscommentgetPageOfCommentphp"></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/getPageOfComment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment/getPageOfComment.php 2015-09-25 04:43:57 UTC (rev 34534)
+++ trunk/tests/phpunit/tests/comment/getPageOfComment.php 2015-09-25 05:10:40 UTC (rev 34535)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,10 +6,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Comment_GetPageOfComment extends WP_UnitTestCase {
</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 setUp() {
- parent::setUp();
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> public function test_last_comment() {
</span><span class="cx" style="display: block; padding: 0 10px"> $p = $this->factory->post->create();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,4 +34,136 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 3 ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ public function test_type_pings() {
+ $p = $this->factory->post->create();
+ $now = time();
+
+ $trackbacks = array();
+ for ( $i = 0; $i <= 3; $i++ ) {
+ $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
+ $now -= 10 * $i;
+ }
+
+ $pingbacks = array();
+ for ( $i = 0; $i <= 6; $i++ ) {
+ $pingbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'pingback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
+ $now -= 10 * $i;
+ }
+
+ $this->assertEquals( 2, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'trackback' ) ) );
+ $this->assertEquals( 3, get_page_of_comment( $pingbacks[0], array( 'per_page' => 2, 'type' => 'pingback' ) ) );
+ $this->assertEquals( 5, get_page_of_comment( $trackbacks[0], array( 'per_page' => 2, 'type' => 'pings' ) ) );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_subsequent_calls_should_hit_cache() {
+ global $wpdb;
+
+ $p = $this->factory->post->create();
+ $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
+
+ // Prime cache.
+ $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
+
+ $num_queries = $wpdb->num_queries;
+ $page_2 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
+
+ $this->assertSame( $page_1, $page_2 );
+ $this->assertSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_cache_hits_should_be_sensitive_to_comment_type() {
+ global $wpdb;
+
+ $p = $this->factory->post->create();
+ $comment = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'comment' ) );
+
+ $now = time();
+ $trackbacks = array();
+ for ( $i = 0; $i <= 5; $i++ ) {
+ $trackbacks[ $i ] = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_type' => 'trackback', 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - ( 10 * $i ) ) ) );
+ }
+
+ // Prime cache for trackbacks.
+ $page_trackbacks = get_page_of_comment( $trackbacks[1], array( 'per_page' => 3, 'type' => 'trackback' ) );
+ $this->assertEquals( 2, $page_trackbacks );
+
+ $num_queries = $wpdb->num_queries;
+ $page_comments = get_page_of_comment( $comment, array( 'per_page' => 3, 'type' => 'comment' ) );
+ $this->assertEquals( 1, $page_comments );
+
+ $this->assertNotEquals( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_cache_should_be_invalidated_when_comment_is_approved() {
+ $p = $this->factory->post->create();
+ $c = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0 ) );
+
+ // Prime cache.
+ $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
+
+ // Approve comment.
+ wp_set_comment_status( $c, 'approve' );
+
+ $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_cache_should_be_invalidated_when_comment_is_deleted() {
+ $p = $this->factory->post->create();
+ $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
+
+ // Prime cache.
+ $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
+
+ // Trash comment.
+ wp_trash_comment( $c );
+
+ $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_cache_should_be_invalidated_when_comment_is_spammed() {
+ $p = $this->factory->post->create();
+ $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
+
+ // Prime cache.
+ $page_1 = get_page_of_comment( $c, array( 'per_page' => 3 ) );
+
+ // Spam comment.
+ wp_spam_comment( $c );
+
+ $this->assertFalse( wp_cache_get( $c, 'comment_pages' ) );
+ }
+
+ /**
+ * @ticket 11334
+ */
+ public function test_cache_should_be_invalidated_when_older_comment_is_published() {
+ $now = time();
+
+ $p = $this->factory->post->create();
+ $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now ) ) );
+ $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 20 ) ) );
+ $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $p, 'comment_approved' => 0, 'comment_date_gmt' => date( 'Y-m-d H:i:s', $now - 30 ) ) );
+
+ $this->assertEquals( 1, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
+
+ wp_set_comment_status( $c3, '1' );
+
+ $this->assertEquals( 2, get_page_of_comment( $c1, array( 'per_page' => 2 ) ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>