<!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>[29248] trunk: In `get_adjacent_post()`, make `$excluded_terms` work as expected.</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/29248">29248</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-07-19 22:26:43 +0000 (Sat, 19 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>In `get_adjacent_post()`, make `$excluded_terms` work as expected.

Adds unit tests.
Props jessepollak, kovshenin.
Fixes <a href="http://core.trac.wordpress.org/ticket/22112">#22112</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 (29247 => 29248)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/link-template.php  2014-07-19 18:54:50 UTC (rev 29247)
+++ trunk/src/wp-includes/link-template.php     2014-07-19 22:26:43 UTC (rev 29248)
</span><span class="lines">@@ -1477,41 +1477,41 @@
</span><span class="cx">  $current_post_date = $post->post_date;
</span><span class="cx"> 
</span><span class="cx">  $join = '';
</span><del>-       $posts_in_ex_terms_sql = '';
</del><ins>+        $where = '';
+
</ins><span class="cx">   if ( $in_same_term || ! empty( $excluded_terms ) ) {
</span><span class="cx">          $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
</span><ins>+               $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
</ins><span class="cx"> 
</span><ins>+               if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
+                       // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
+                       if ( false !== strpos( $excluded_terms, ' and ' ) ) {
+                               _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
+                               $excluded_terms = explode( ' and ', $excluded_terms );
+                       } else {
+                               $excluded_terms = explode( ',', $excluded_terms );
+                       }
+
+                       $excluded_terms = array_map( 'intval', $excluded_terms );
+               }
+
</ins><span class="cx">           if ( $in_same_term ) {
</span><span class="cx">                  if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
</span><span class="cx">                          return '';
</span><span class="cx">                  $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
</span><ins>+
+                       // Remove any exclusions from the term array to include.
+                       $term_array = array_diff( $term_array, (array) $excluded_terms );
+                       $term_array = array_map( 'intval', $term_array );
+
</ins><span class="cx">                   if ( ! $term_array || is_wp_error( $term_array ) )
</span><span class="cx">                          return '';
</span><del>-                       $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
</del><ins>+
+                       $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
</ins><span class="cx">           }
</span><span class="cx"> 
</span><del>-               $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
</del><span class="cx">           if ( ! empty( $excluded_terms ) ) {
</span><del>-                       if ( ! is_array( $excluded_terms ) ) {
-                               // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
-                               if ( false !== strpos( $excluded_terms, ' and ' ) ) {
-                                       _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
-                                       $excluded_terms = explode( ' and ', $excluded_terms );
-                               } else {
-                                       $excluded_terms = explode( ',', $excluded_terms );
-                               }
-                       }
-
-                       $excluded_terms = array_map( 'intval', $excluded_terms );
-
-                       if ( ! empty( $term_array ) ) {
-                               $excluded_terms = array_diff( $excluded_terms, $term_array );
-                               $posts_in_ex_terms_sql = '';
-                       }
-
-                       if ( ! empty( $excluded_terms ) ) {
-                               $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
-                       }
</del><ins>+                        $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )';
</ins><span class="cx">           }
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -1545,7 +1545,7 @@
</span><span class="cx">   * @param bool   $in_same_term   Whether post should be in a same taxonomy term.
</span><span class="cx">   * @param array  $excluded_terms Array of excluded term IDs.
</span><span class="cx">   */
</span><del>-       $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
</del><ins>+        $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
</ins><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * Filter the ORDER BY clause in the SQL for an adjacent post query.
</span></span></pre></div>
<a id="trunktestsphpunittestslinkphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/link.php (29247 => 29248)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/link.php       2014-07-19 18:54:50 UTC (rev 29247)
+++ trunk/tests/phpunit/tests/link.php  2014-07-19 22:26:43 UTC (rev 29248)
</span><span class="lines">@@ -188,4 +188,65 @@
</span><span class="cx">          $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
</span><span class="cx">          $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
</span><span class="cx">  }
</span><ins>+
+       /**
+       * @ticket 22112
+       */
+       function test_get_adjacent_post_exclude_self_term() {
+               $include = $this->factory->category->create();
+               $exclude = $this->factory->category->create();
+
+               $one = $this->factory->post->create_and_get( array(
+                       'post_date' => '2012-01-01 12:00:00',
+                       'post_category' => array( $include, $exclude ),
+               ) );
+
+               $two = $this->factory->post->create_and_get( array(
+                       'post_date' => '2012-01-02 12:00:00',
+                       'post_category' => array(),
+               ) );
+
+               $three = $this->factory->post->create_and_get( array(
+                       'post_date' => '2012-01-03 12:00:00',
+                       'post_category' => array( $include, $exclude ),
+               ) );
+
+               $four = $this->factory->post->create_and_get( array(
+                       'post_date' => '2012-01-04 12:00:00',
+                       'post_category' => array( $include ),
+               ) );
+
+               $five = $this->factory->post->create_and_get( array(
+                       'post_date' => '2012-01-05 12:00:00',
+                       'post_category' => array( $include, $exclude ),
+               ) );
+
+               // First post
+               $this->go_to( get_permalink( $one ) );
+               $this->assertEquals( $two, get_adjacent_post( false, array(), false ) );
+               $this->assertEquals( $three, get_adjacent_post( true, array(), false ) );
+               $this->assertEquals( $two, get_adjacent_post( false, array( $exclude ), false ) );
+               $this->assertEquals( $four, get_adjacent_post( true, array( $exclude ), false ) );
+               $this->assertEmpty( get_adjacent_post( false, array(), true ) );
+
+               // Fourth post
+               $this->go_to( get_permalink( $four ) );
+               $this->assertEquals( $five, get_adjacent_post( false, array(), false ) );
+               $this->assertEquals( $five, get_adjacent_post( true, array(), false ) );
+               $this->assertEmpty( get_adjacent_post( false, array( $exclude ), false ) );
+               $this->assertEmpty( get_adjacent_post( true, array( $exclude ), false ) );
+
+               $this->assertEquals( $three, get_adjacent_post( false, array(), true ) );
+               $this->assertEquals( $three, get_adjacent_post( true, array(), true ) );
+               $this->assertEquals( $two, get_adjacent_post( false, array( $exclude ), true ) );
+               $this->assertEmpty( get_adjacent_post( true, array( $exclude ), true ) );
+
+               // Last post
+               $this->go_to( get_permalink( $five ) );
+               $this->assertEquals( $four, get_adjacent_post( false, array(), true ) );
+               $this->assertEquals( $four, get_adjacent_post( true, array(), true ) );
+               $this->assertEquals( $four, get_adjacent_post( false, array( $exclude ), true ) );
+               $this->assertEquals( $four, get_adjacent_post( true, array( $exclude ), true ) );
+               $this->assertEmpty( get_adjacent_post( false, array(), false ) );
+       }
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>