<!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>[27635] trunk: Fix various issues with WP_Adjacent_Post:</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">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27635">27635</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-03-20 02:58:48 +0000 (Thu, 20 Mar 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Fix various issues with WP_Adjacent_Post:
* Performance / number of queries.
* Incorrect results caused by sticky posts.
* Back compat for filters, which had used "WHERE" while WP_Query does not; and fixing table references.
props ethitter.
fixes <a href="http://core.trac.wordpress.org/ticket/26937">#26937</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeslinktemplatephp">trunk/src/wp-includes/link-template.php</a></li>
<li><a href="#trunktestsphpunittestslinkphp">trunk/tests/phpunit/tests/link.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeslinktemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/link-template.php (27634 => 27635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/link-template.php 2014-03-20 02:54:26 UTC (rev 27634)
+++ trunk/src/wp-includes/link-template.php 2014-03-20 02:58:48 UTC (rev 27635)
</span><span class="lines">@@ -1243,7 +1243,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Allow direct access to adjacent post from the class instance itself
</del><ins>+ * Allow direct access to adjacent post from the class instance itself.
</ins><span class="cx"> *
</span><span class="cx"> * @param string $property Property to get.
</span><span class="cx"> * @return mixed String when adjacent post is found and post property exists. Null when no adjacent post is found.
</span><span class="lines">@@ -1273,7 +1273,7 @@
</span><span class="cx"> * @return mixed Post object on success. False if no adjacent post exists. Null on failure.
</span><span class="cx"> */
</span><span class="cx"> protected function get_post( $args ) {
</span><del>- $this->current_post = get_post( $args['post'] );
</del><ins>+ $this->current_post = get_post( $args['post'] );
</ins><span class="cx"> $this->excluded_terms = array_map( 'intval', $args['excluded_terms'] );
</span><span class="cx"> $this->adjacent = $args['previous'] ? 'previous' : 'next';
</span><span class="cx"> $this->taxonomy = $args['taxonomy'];
</span><span class="lines">@@ -1291,14 +1291,20 @@
</span><span class="cx">
</span><span class="cx"> // Build our arguments for WP_Query.
</span><span class="cx"> $query_args = array(
</span><del>- 'posts_per_page' => 1,
- 'post_status' => 'publish',
- 'post_type' => 'post',
- 'orderby' => 'date',
- 'order' => 'previous' === $this->adjacent ? 'DESC' : 'ASC',
- 'no_found_rows' => true,
- 'cache_results' => true,
- 'date_query' => array(),
</del><ins>+ 'posts_per_page' => 1,
+ 'post_status' => 'publish',
+ 'post_type' => 'post',
+ 'orderby' => 'date',
+ 'order' => 'previous' === $this->adjacent ? 'DESC' : 'ASC',
+ 'ignore_sticky_posts' => true,
+ 'date_query' => array(),
+
+ // Performance considerations:
+ 'no_found_rows' => true,
+ 'cache_results' => true,
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false,
+ 'split_the_query' => wp_using_ext_object_cache(),
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> $tax_query = array();
</span><span class="lines">@@ -1388,16 +1394,39 @@
</span><span class="cx"> */
</span><span class="cx"> remove_filter( 'posts_clauses', array( $this, 'filter' ) );
</span><span class="cx">
</span><del>- /*
- * The `join` and `where` filters are identical in their parameters,
- * so we can use the same approach for both.
</del><ins>+ /**
+ * If no legacy filter callbacks are registered, proceed no further.
</ins><span class="cx"> */
</span><del>- foreach ( array( 'join', 'where' ) as $clause ) {
- if ( has_filter( 'get_' . $this->adjacent . '_post_' . $clause ) ) {
- $clauses[ $clause ] = $this->filter_join_and_where( $clauses[ $clause ], $clause );
- }
</del><ins>+ if ( ! has_filter( 'get_' . $this->adjacent . '_post_join' ) && ! has_filter( 'get_' . $this->adjacent . '_post_where' ) && ! has_filter( 'get_' . $this->adjacent . '_post_sort' ) ) {
+ return $clauses;
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+ /**
+ * Posts table must be aliased as `p` for backwards compatibility with query previously generated by `get_adjacent_post()`.
+ */
+ $clauses = array_map( array( $this, 'alias_posts_table' ), $clauses );
+
+ /**
+ * Apply the legacy `join` filter.
+ */
+ if ( has_filter( 'get_' . $this->adjacent . '_post_join' ) ) {
+ $clauses['join'] = $this->filter_join( $clauses['join'] );
+ }
+
+ /**
+ * Posts table must be aliased as `p` for backwards compatibility with query previously generated by `get_adjacent_post()`.
+ * No filter on the table name exists, so we have to leverage the next applied filter, that for the `join` clause.
+ * We wait to apply this until after the legacy filter is applied so that the legacy filter doesn't remove the alias.
+ */
+ $clauses['join'] = 'AS p ' . $clauses['join'];
+
+ /**
+ * Apply the legacy `where` filter.
+ */
+ if ( has_filter( 'get_' . $this->adjacent . '_post_where' ) ) {
+ $clauses['where'] = $this->filter_where( $clauses['where'] );
+ }
+
</ins><span class="cx"> /*
</span><span class="cx"> * The legacy `sort` filter combined the ORDER BY and LIMIT clauses,
</span><span class="cx"> * while `WP_Query` does not, which requires special handling.
</span><span class="lines">@@ -1411,21 +1440,72 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Apply the deprecated `join` or `where` clause filter to the clauses built by WP_Query.
</del><ins>+ * Alias posts table as `p` to match query previously built by `get_adjacent_post()`.
</ins><span class="cx"> *
</span><del>- * @param string $value
- * @param string $clause
</del><ins>+ * @global $wpdb
+ * @param string Clause to alias
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>- protected function filter_join_and_where( $value, $clause ) {
</del><ins>+ protected function alias_posts_table( $clause ) {
+ global $wpdb;
+
+ return str_replace( $wpdb->posts, 'p', $clause );
+ }
+
+ /**
+ * Apply the deprecated `join` clause filter to the clause built by WP_Query.
+ *
+ * @param string $join
+ * @return string
+ */
+ protected function filter_join( $join ) {
</ins><span class="cx"> /**
</span><del>- * @todo Minimal hook docs
</del><span class="cx"> * @deprecated 3.9.0
</span><span class="cx"> */
</span><del>- return apply_filters( 'get_' . $this->adjacent . '_post_' . $clause, $value, $this->in_same_term, $this->excluded_terms );
</del><ins>+ return apply_filters( 'get_' . $this->adjacent . '_post_join', $join, $this->in_same_term, $this->excluded_terms );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Apply the deprecated `where` clause filter to the clause built by WP_Query.
+ *
+ * @param string $join
+ * @return string
+ */
+ protected function filter_where( $where ) {
+ $where = trim( $where );
+
+ // The legacy filter passed the entire clause, including the `WHERE`, while WP_Query's filter does not.
+ // We prepend the `WHERE` for the benefit of legacy callbacks that look for it.
+ if ( 0 !== stripos( $where, 'where' ) ) {
+ $where = 'WHERE 1=1 ' . $where;
+ }
+
+ /**
+ * @deprecated 3.9.0
+ */
+ $where = apply_filters( 'get_' . $this->adjacent . '_post_where', $where, $this->in_same_term, $this->excluded_terms );
+
+ $where = trim( $where );
+
+ // The legacy filter passed the entire clause, including the `WHERE`, while WP_Query's filter does not.
+ // Removing the `WHERE` is necessary as we've added it above, and the legacy filter could include it in the returned string.
+ if ( 0 === stripos( $where, 'where 1=1' ) ) {
+ $where = substr( $where, 9 );
+ } elseif ( 0 === stripos( $where, 'where' ) ) {
+ $where = substr( $where, 5 );
+ }
+
+ $where = trim( $where );
+
+ // WP_Query expects that the string returned begins with `AND`, as it is prepended with "1=1" when the clauses are joined
+ if ( 0 !== stripos( $where, 'and' ) ) {
+ $where = 'AND ' . $where;
+ }
+
+ return $where;
+ }
+
+ /**
</ins><span class="cx"> * Apply deprecated `sort` filter, which applies to both the ORDER BY and LIMIT clauses.
</span><span class="cx"> *
</span><span class="cx"> * @param string $orderby
</span></span></pre></div>
<a id="trunktestsphpunittestslinkphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/link.php (27634 => 27635)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/link.php 2014-03-20 02:54:26 UTC (rev 27634)
+++ trunk/tests/phpunit/tests/link.php 2014-03-20 02:58:48 UTC (rev 27635)
</span><span class="lines">@@ -208,6 +208,12 @@
</span><span class="cx"> $this->assertEquals( $post_four, get_adjacent_post( false, null, false ) );
</span><span class="cx"> remove_filter( 'get_next_post_where', array( $this, 'filter_next_post_where' ) );
</span><span class="cx">
</span><ins>+ // Test "where" filter that writes its own query
+ add_filter( 'get_previous_post_where', array( $this, 'override_previous_post_where_clause' ) );
+ $this->go_to( get_permalink( $post_four->ID ) );
+ $this->assertEquals( $post_two, get_adjacent_post( false, null, true ) );
+ remove_filter( 'get_previous_post_where', array( $this, 'override_previous_post_where_clause' ) );
+
</ins><span class="cx"> // Test "join" filter by joining the postmeta table and restricting by meta key
</span><span class="cx"> add_filter( 'get_next_post_join', array( $this, 'filter_next_post_join' ) );
</span><span class="cx"> add_filter( 'get_next_post_where', array( $this, 'filter_next_post_where_with_join' ) );
</span><span class="lines">@@ -248,10 +254,18 @@
</span><span class="cx"> /**
</span><span class="cx"> * Filter callback for `test_legacy_get_adjacent_post_filters()`
</span><span class="cx"> */
</span><ins>+ function override_previous_post_where_clause( $where ) {
+ $where = "WHERE p.post_date < '2012-02-28'";
+ return $where;
+ }
+
+ /**
+ * Filter callback for `test_legacy_get_adjacent_post_filters()`
+ */
</ins><span class="cx"> function filter_next_post_join( $join ) {
</span><span class="cx"> global $wpdb;
</span><span class="cx">
</span><del>- $join .= " INNER JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id";
</del><ins>+ $join .= " INNER JOIN {$wpdb->postmeta} ON p.ID = {$wpdb->postmeta}.post_id";
</ins><span class="cx"> return $join;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -269,10 +283,7 @@
</span><span class="cx"> * Filter callback for `test_legacy_get_adjacent_post_filters()`
</span><span class="cx"> */
</span><span class="cx"> function filter_next_post_sort( $sort ) {
</span><del>- global $wpdb;
-
- $sort = str_replace( $wpdb->posts . '.post_date', $wpdb->posts . '.post_title', $sort );
- return $sort;
</del><ins>+ return str_replace( 'p.post_date', 'p.post_title', $sort );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>