<!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>[27285] trunk: Make get_adjacent_post() wrap a new WP_Get_Adjacent_Post object that uses WP_Query.</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/27285">27285</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-02-26 17:09:54 +0000 (Wed, 26 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make get_adjacent_post() wrap a new WP_Get_Adjacent_Post object that uses WP_Query.</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 (27284 => 27285)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/link-template.php  2014-02-26 16:49:24 UTC (rev 27284)
+++ trunk/src/wp-includes/link-template.php     2014-02-26 17:09:54 UTC (rev 27285)
</span><span class="lines">@@ -1,4 +1,4 @@
</span><del>-<?php
</del><ins>+ <?php
</ins><span class="cx"> /**
</span><span class="cx">  * WordPress Link Template Functions
</span><span class="cx">  *
</span><span class="lines">@@ -1119,82 +1119,283 @@
</span><span class="cx">  * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
</span><span class="cx">  * @param bool         $previous       Optional. Whether to retrieve previous post.
</span><span class="cx">  * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
</span><del>- * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
</del><ins>+ * @return mixed       Post object if successful. Null if current post doesn't exist. Empty string if no corresponding adjacent post exists.
</ins><span class="cx">  */
</span><span class="cx"> function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
</span><del>-       global $wpdb;
</del><ins>+        if ( is_string( $excluded_terms ) && false !== strpos( $excluded_terms, ' and ' ) ) {
+               // back-compat: $excluded_terms used to be IDs separated by " and "
+               _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
+               $excluded_terms = explode( ' and ', $excluded_terms );
+       }
+       if ( $excluded_terms ) {
+               $excluded_terms = wp_parse_id_list( $excluded_terms );
+       } else {
+               $excluded_terms = array();
+       }
</ins><span class="cx"> 
</span><del>-       if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
-               return null;
</del><ins>+        $adjacent = new WP_Get_Adjacent_Post( array(
+               'post'           => get_post(),
+               'previous'       => $previous,
+               'taxonomy'       => $taxonomy,
+               'in_same_term'   => $in_same_term,
+               'excluded_terms' => $excluded_terms,
+       ) );
</ins><span class="cx"> 
</span><del>-       $current_post_date = $post->post_date;
</del><ins>+        return $adjacent->adjacent_post;
+}
</ins><span class="cx"> 
</span><del>-       $join = '';
-       $posts_in_ex_terms_sql = '';
-       if ( $in_same_term || ! empty( $excluded_terms ) ) {
-               $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";
</del><ins>+/**
+ * WordPress Adjacent Post API
+ *
+ * Based on the current or specified post, determines either the previous or
+ * next post based on the criteria specified. Supports retrieving posts with the
+ * same taxonomy terms and posts that lack specific terms.
+ */
+class WP_Get_Adjacent_Post {
+       public $adjacent_post = null;
</ins><span class="cx"> 
</span><del>-               if ( $in_same_term ) {
-                       if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
-                               return '';
-                       $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
-                       if ( ! $term_array || is_wp_error( $term_array ) )
-                               return '';
-                       $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
</del><ins>+        protected $current_post   = false;
+       protected $adjacent       = 'previous';
+       protected $taxonomy       = 'category';
+       protected $in_same_term   = false;
+       protected $excluded_terms = '';
+
+       /**
+        * Class constructor.
+        *
+        * The post is queried is run if arguments are passed to the constructor.
+        * Otherwise, the get_post() method will need to be called.
+        *
+        * @param array $args Optional. See the get_post() method for $args.
+        */
+       public function __construct( $args = array() ) {
+               if ( empty( $args ) ) {
+                       return;
</ins><span class="cx">           }
</span><span class="cx"> 
</span><del>-               $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
-               if ( ! empty( $excluded_terms ) ) {
-                       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 );
-                               }
</del><ins>+                $this->get_post( $args );
+       }
+
+       /**
+        * Allow direct access to adjacent post from the class instance itself
+        *
+        * @param string $property
+        * @return mixed String when adjacent post is found and post property exists. Null when no adjacent post is found.
+        */
+       public function __get( $property ) {
+               if ( is_object( $this->adjacent_post ) && property_exists( $this->adjacent_post, $property ) ) {
+                       return $this->adjacent_post->{$property};
+               } else {
+                       return null;
+               }
+       }
+
+       /**
+        * Determine adjacent post for specified post and adjacency.
+        *
+        * @since 3.9.0
+        *
+        * @param array $args {
+        *     Arguments for querying the adjacent post.
+        *
+        *     @type mixed  $post           Optional. Post object or ID to find adjacent post for.
+        *     @type bool   $previous       Optional. Whether to retrieve previous post.
+        *     @type string $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+        *     @type bool   $in_same_term   Optional. Whether post should be in a same taxonomy term.
+        *     @type array  $excluded_terms Optional. Array of excluded term IDs.
+        * }
+        * @return mixed Post object on success. False if no adjacent post exists. Null on failure.
+        */
+       protected function get_post( $args ) {
+               $this->current_post = get_post( $args['post'] );
+               $this->excluded_terms = array_map( 'intval', $args['excluded_terms'] );
+               $this->adjacent       = $args['previous'] ? 'previous' : 'next';
+               $this->in_same_term   = (bool) $args['in_same_term'];
+
+               // Return null when either the post or taxonomy doesn't exist.
+               if ( ! $this->current_post ) {
+                       return;
+               }
+               if ( $this->in_same_term || $this->excluded_terms ) {
+                       if ( ! taxonomy_exists( $args['taxonomy'] ) ) {
+                               return;
</ins><span class="cx">                   }
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-                       $excluded_terms = array_map( 'intval', $excluded_terms );
</del><ins>+                // Build our arguments for WP_Query.
+               $query_args = array(
+                       '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(),
+               );
</ins><span class="cx"> 
</span><del>-                       if ( ! empty( $term_array ) ) {
-                               $excluded_terms = array_diff( $excluded_terms, $term_array );
-                               $posts_in_ex_terms_sql = '';
</del><ins>+                $tax_query = array();
+
+               // Set up for requests limited to posts that share terms.
+               if ( $this->in_same_term ) {
+                       $terms = get_the_terms( $this->current_post->ID, $args['taxonomy'] );
+
+                       if ( is_array( $terms ) && ! empty( $terms ) ) {
+                               $terms = wp_list_pluck( $terms, 'term_id' );
+                               $terms = array_values( $terms );
+                               $terms = array_map( 'intval', $terms );
+                       } else {
+                               unset( $terms );
</ins><span class="cx">                   }
</span><ins>+               }
</ins><span class="cx"> 
</span><del>-                       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>+                // Handle excluded terms.
+               if ( $this->excluded_terms ) {
+                       $tax_query[] = array(
+                               'taxonomy' => $args['taxonomy'],
+                               'slugs'    => $this->excluded_terms,
+                               'compare'  => 'NOT IN',
+                       );
+               }
+
+               // If requesting same term, ensure excluded terms don't appear in term list.
+               if ( isset( $terms ) ) {
+                       if ( isset( $this->excluded_terms ) && is_array( $this->excluded_terms ) ) {
+                               $terms = array_diff( $terms, $this->excluded_terms );
</ins><span class="cx">                   }
</span><ins>+
+                       if ( ! empty( $terms ) ) {
+                               $tax_query[] = array(
+                                       'taxonomy' => $args['taxonomy'],
+                                       'terms'    => $terms,
+                               );
+                       }
</ins><span class="cx">           }
</span><ins>+
+               // If we have a tax query, add it to our query args.
+               if ( $tax_query ) {
+                       $query_args['tax_query'] = $tax_query;
+               }
+
+               // And now, the date constraint.
+               $date_query_key = 'previous' === $this->adjacent ? 'before' : 'after';
+
+               $query_args['date_query'][] = array(
+                       $date_query_key => $this->current_post->post_date,
+                       'inclusive'     => true,
+               );
+
+               // Ensure the current post isn't returned, since we're using an inclusive date query.
+               $query_args['post__not_in'] = array( $this->current_post->ID );
+
+               /**
+                * Filter the arguments passed to WP_Query when finding an adjacent post.
+                *
+                * @since 3.9.0
+                *
+                * @param array $query_args WP_Query arguments.
+                * @param array $args       Arguments passed to WP_Get_Adjacent_Post.
+                */
+               $query_args = apply_filters( 'get_adjacent_post_query_args', $query_args, $args );
+
+               add_filter( 'posts_clauses', array( $this, 'filter' ) );
+               $query = new WP_Query( $query_args );
+
+               if ( $query->posts ) {
+                       $this->adjacent_post = $query->post;
+               } else {
+                       $this->adjacent_post = false;
+               }
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       $adjacent = $previous ? 'previous' : 'next';
-       $op = $previous ? '<' : '>';
-       $order = $previous ? 'DESC' : 'ASC';
</del><ins>+        /**
+        * Apply the deprecated filters to WP_Query's clauses.
+        *
+        * @param array $clauses
+        * @uses $this->filter_join_and_where()
+        * @uses $this->filter_sort()
+        * @filter post_clauses
+        * @return array
+        */
+       public function filter( $clauses ) {
+               // Immediately deregister these legacy filters to avoid modifying
+               // any calls to WP_Query from filter callbacks hooked to WP_Query filters.
+               remove_filter( 'posts_clauses', array( $this, 'filter' ) );
</ins><span class="cx"> 
</span><del>-       $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
-       $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 );
-       $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
</del><ins>+                // The `join` and `where` filters are identical in their parameters,
+               // so we can use the same approach for both.
+               foreach ( array( 'join', 'where' ) as $clause ) {
+                       if ( has_filter( 'get_' . $this->adjacent . '_post_' . $clause ) ) {
+                               $clauses[ $clause ] = $this->filter_join_and_where( $clauses[ $clause ], $clause );
+                       }
+               }
</ins><span class="cx"> 
</span><del>-       $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
-       $query_key = 'adjacent_post_' . md5( $query );
-       $result = wp_cache_get( $query_key, 'counts' );
-       if ( false !== $result ) {
-               if ( $result )
-                       $result = get_post( $result );
-               return $result;
</del><ins>+                // The legacy `sort` filter combined the ORDER BY and LIMIT clauses,
+               // while `WP_Query` does not, which requires special handling.
+               if ( has_filter( 'get_' . $this->adjacent . '_post_sort' ) ) {
+                       $sort_clauses = $this->filter_sort( $clauses['orderby'], $clauses['limits'] );
+                       $clauses      = array_merge( $clauses, $sort_clauses );
+               }
+
+               return $clauses;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       $result = $wpdb->get_var( $query );
-       if ( null === $result )
-               $result = '';
</del><ins>+        /**
+        * Apply the deprecated `join` or `where` clause filter to the clauses built by WP_Query.
+        *
+        * @param string $value
+        * @param string $clause
+        * @return string
+        */
+       protected function filter_join_and_where( $value, $clause ) {
+               /**
+                * @deprecated 3.9.0
+                */
+               return apply_filters( 'get_' . $this->adjacent . '_post_' . $clause, $value, $this->in_same_term, $this->excluded_terms );
+       }
</ins><span class="cx"> 
</span><del>-       wp_cache_set( $query_key, $result, 'counts' );
</del><ins>+        /**
+        * Apply deprecated `sort` filter, which applies to both the ORDER BY and LIMIT clauses.
+        *
+        * @param string $orderby
+        * @param string $limits
+        * @return array
+        */
+       protected function filter_sort( $orderby, $limits ) {
+               /**
+                * @deprecated 3.9.0
+                */
+               $sort = apply_filters( 'get_' . $this->adjacent . '_post_sort', 'ORDER BY ' . $orderby . ' ' . $limits );
</ins><span class="cx"> 
</span><del>-       if ( $result )
-               $result = get_post( $result );
</del><ins>+                if ( empty( $sort ) ) {
+                       return compact( 'orderby', 'limits' );
+               }
</ins><span class="cx"> 
</span><del>-       return $result;
</del><ins>+                // The legacy filter could allow either clause to be removed, or their order inverted, so we need to know what we have and where.
+               $has_order_by = stripos( $sort, 'order by' );
+               $has_limit    = stripos( $sort, 'limit' );
+
+               // Split the string of one or two clauses into their respective array keys
+               if ( false !== $has_order_by && false !== $has_limit ) {
+                       // The LIMIT clause cannot appear before the ORDER BY clause in a valid query
+                       // However, since the legacy filter would allow a user to invert the order, we maintain that handling so the same errors are triggered.
+                       if ( $has_order_by < $has_limit ) {
+                               $orderby = trim( str_ireplace( 'order by', '', substr( $sort, 0, $has_limit ) ) );
+                               $limits  = trim( substr( $sort, $has_limit ) );
+                       } else {
+                               $orderby = trim( str_ireplace( 'order by', '', substr( $sort, $has_order_by ) ) );
+                               $limits  = trim( substr( $sort, 0, $has_order_by ) );
+                       }
+               } elseif ( false !== $has_order_by ) {
+                       $orderby = trim( str_ireplace( 'order by', '', $sort ) );
+                       $limits  = '';
+               } elseif ( false !== $has_limit ) {
+                       $orderby = '';
+                       $limits  = trim( $sort );
+               }
+
+               return compact( 'orderby', 'limits' );
+       }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestslinkphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/link.php (27284 => 27285)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/link.php       2014-02-26 16:49:24 UTC (rev 27284)
+++ trunk/tests/phpunit/tests/link.php  2014-02-26 17:09:54 UTC (rev 27285)
</span><span class="lines">@@ -167,4 +167,119 @@
</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 26937
+        */
+       function test_legacy_get_adjacent_post_filters() {
+               // Need some sample posts to test adjacency
+               $post_one = $this->factory->post->create_and_get( array(
+                       'post_title' => 'First',
+                       'post_date' => '2012-01-01 12:00:00'
+               ) );
+
+               $post_two = $this->factory->post->create_and_get( array(
+                       'post_title' => 'Second',
+                       'post_date' => '2012-02-01 12:00:00'
+               ) );
+
+               $post_three = $this->factory->post->create_and_get( array(
+                       'post_title' => 'Third',
+                       'post_date' => '2012-03-01 12:00:00'
+               ) );
+
+               $post_four = $this->factory->post->create_and_get( array(
+                       'post_title' => 'Fourth',
+                       'post_date' => '2012-04-01 12:00:00'
+               ) );
+
+               // Add some meta so we can join the postmeta table and query
+               add_post_meta( $post_three->ID, 'unit_test_meta', 'waffle' );
+
+               // Test "where" filter for a previous post
+               add_filter( 'get_previous_post_where', array( $this, 'filter_previous_post_where' ) );
+               $this->go_to( get_permalink( $post_three->ID ) );
+               $this->assertEquals( $post_one, get_adjacent_post( false, null, true ) );
+               remove_filter( 'get_previous_post_where', array( $this, 'filter_previous_post_where' ) );
+
+               // Test "where" filter for a next post
+               add_filter( 'get_next_post_where', array( $this, 'filter_next_post_where' ) );
+               $this->go_to( get_permalink( $post_two->ID ) );
+               $this->assertEquals( $post_four, get_adjacent_post( false, null, false ) );
+               remove_filter( 'get_next_post_where', array( $this, 'filter_next_post_where' ) );
+
+               // Test "join" filter by joining the postmeta table and restricting by meta key
+               add_filter( 'get_next_post_join', array( $this, 'filter_next_post_join' ) );
+               add_filter( 'get_next_post_where', array( $this, 'filter_next_post_where_with_join' ) );
+               $this->go_to( get_permalink( $post_one->ID ) );
+               $this->assertEquals( $post_three, get_adjacent_post( false, null, false ) );
+               remove_filter( 'get_next_post_join', array( $this, 'filter_next_post_join' ) );
+               remove_filter( 'get_next_post_where', array( $this, 'filter_next_post_where_with_join' ) );
+
+               // Test "sort" filter when modifying ORDER BY clause
+               add_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort' ) );
+               $this->go_to( get_permalink( $post_one->ID ) );
+               $this->assertEquals( $post_four, get_adjacent_post( false, null, false ) );
+               remove_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort' ) );
+
+               // Test "sort" filter when modifying LIMIT clause
+               add_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort_limit' ) );
+               $this->go_to( get_permalink( $post_one->ID ) );
+               $this->assertEquals( $post_three, get_adjacent_post( false, null, false ) );
+               remove_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort_limit' ) );
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_previous_post_where( $where ) {
+               $where .= " AND post_title !='Second'";
+               return $where;
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_next_post_where( $where ) {
+               $where .= " AND post_title !='Third'";
+               return $where;
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_next_post_join( $join ) {
+               global $wpdb;
+
+               $join .= " INNER JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id";
+               return $join;
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_next_post_where_with_join( $where ) {
+               global $wpdb;
+
+               $where .= " AND {$wpdb->postmeta}.meta_key = 'unit_test_meta'";
+               return $where;
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_next_post_sort( $sort ) {
+               global $wpdb;
+
+               $sort = str_replace( $wpdb->posts . '.post_date', $wpdb->posts . '.post_title', $sort );
+               return $sort;
+       }
+
+       /**
+        * Filter callback for `test_legacy_get_adjacent_post_filters()`
+        */
+       function filter_next_post_sort_limit( $sort ) {
+               $sort = str_replace( 'LIMIT 0, 1', 'LIMIT 1, 2', $sort );
+               return $sort;
+       }
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>