WordPress.org

Make WordPress Core

Changeset 27836


Ignore:
Timestamp:
03/29/2014 06:02:01 AM (6 years ago)
Author:
nacin
Message:

Revert the conversion of adjacent post queries to WP_Query. Explanation on the ticket.

Reverts [27285], [27286], [27287], [27288], [27291], [27292], [27293], [27296], [27633], [27634], [27635], and [27692].

see #26937.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r27802 r27836  
    11341134 * @param bool         $previous       Optional. Whether to retrieve previous post.
    11351135 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    1136  * @return mixed       Post object if successful. Null if current post doesn't exist. Empty string if no corresponding adjacent post exists.
     1136 * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    11371137 */
    11381138function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    1139     if ( is_string( $excluded_terms ) && false !== strpos( $excluded_terms, ' and ' ) ) {
    1140         // back-compat: $excluded_terms used to be IDs separated by " and "
    1141         _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
    1142         $excluded_terms = explode( ' and ', $excluded_terms );
    1143     }
    1144     if ( $excluded_terms ) {
    1145         $excluded_terms = wp_parse_id_list( $excluded_terms );
    1146     } else {
    1147         $excluded_terms = array();
    1148     }
    1149 
    1150     $adjacent = new WP_Adjacent_Post( array(
    1151         'post'           => get_post(),
    1152         'previous'       => $previous,
    1153         'taxonomy'       => $taxonomy,
    1154         'in_same_term'   => $in_same_term,
    1155         'excluded_terms' => $excluded_terms,
    1156     ) );
    1157 
    1158     return $adjacent->adjacent_post;
    1159 }
    1160 
    1161 /**
    1162  * WordPress Adjacent Post API
    1163  *
    1164  * Based on the current or specified post, determines either the previous or
    1165  * next post based on the criteria specified. Supports retrieving posts with
    1166  * the same taxonomy terms and posts that lack specific terms.
    1167  *
    1168  * @since 3.9.0
    1169  *
    1170  * @package WordPress
    1171  * @subpackage Template
    1172  */
    1173 class WP_Adjacent_Post {
    1174 
    1175     /**
    1176      * Adjacent post object.
    1177      *
    1178      * @since 3.9.0
    1179      * @access public
    1180      * @var null|WP_Adjacent_Post
    1181      */
    1182     public $adjacent_post = null;
    1183 
    1184     /**
    1185      * Current post object.
    1186      *
    1187      * @since 3.9.0
    1188      * @access protected
    1189      * @var bool|WP_Post
    1190      */
    1191     protected $current_post = false;
    1192 
    1193     /**
    1194      * 'previous' or 'next' type of adjacent post.
    1195      *
    1196      * @since 3.9.0
    1197      * @access protected
    1198      * @var string
    1199      */
    1200     protected $adjacent = 'previous';
    1201 
    1202     /**
    1203      * Post taxonomy.
    1204      *
    1205      * @since 3.9.0
    1206      * @access protected
    1207      * @var string
    1208      */
    1209     protected $taxonomy = 'category';
    1210 
    1211     /**
    1212      * Whether the post should be in a same taxonomy term.
    1213      *
    1214      * @since 3.9.0
    1215      * @access protected
    1216      * @var string
    1217      */
    1218     protected $in_same_term = false;
    1219 
    1220     /**
    1221      * Excluded term IDs.
    1222      *
    1223      * @since 3.9.0
    1224      * @access protected
    1225      * @var string|array
    1226      */
    1227     protected $excluded_terms = '';
    1228 
    1229     /**
    1230      * Class constructor.
    1231      *
    1232      * The post is queried is run if arguments are passed to the constructor.
    1233      * Otherwise, the get_post() method will need to be called.
    1234      *
    1235      * @param array $args Optional. See the get_post() method for $args.
    1236      */
    1237     public function __construct( $args = array() ) {
    1238         if ( empty( $args ) ) {
    1239             return;
     1139    global $wpdb;
     1140
     1141    if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
     1142        return null;
     1143
     1144    $current_post_date = $post->post_date;
     1145
     1146    $join = '';
     1147    $posts_in_ex_terms_sql = '';
     1148    if ( $in_same_term || ! empty( $excluded_terms ) ) {
     1149        $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";
     1150
     1151        if ( $in_same_term ) {
     1152            if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
     1153                return '';
     1154            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1155            if ( ! $term_array || is_wp_error( $term_array ) )
     1156                return '';
     1157            $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
    12401158        }
    12411159
    1242         $this->get_post( $args );
    1243     }
    1244 
    1245     /**
    1246      * Allow direct access to adjacent post from the class instance itself.
    1247      *
    1248      * @param string $property Property to get.
    1249      * @return mixed String when adjacent post is found and post property exists. Null when no adjacent post is found.
    1250      */
    1251     public function __get( $property ) {
    1252         if ( is_object( $this->adjacent_post ) && property_exists( $this->adjacent_post, $property ) ) {
    1253             return $this->adjacent_post->{$property};
    1254         } else {
    1255             return null;
    1256         }
    1257     }
    1258 
    1259     /**
    1260      * Determine adjacent post for specified post and adjacency.
    1261      *
    1262      * @since 3.9.0
    1263      *
    1264      * @param array $args {
    1265      *     Arguments for querying the adjacent post.
    1266      *
    1267      *     @type mixed  $post           Optional. Post object or ID to find adjacent post for.
    1268      *     @type bool   $previous       Optional. Whether to retrieve previous post.
    1269      *     @type string $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    1270      *     @type bool   $in_same_term   Optional. Whether post should be in a same taxonomy term.
    1271      *     @type array  $excluded_terms Optional. Array of excluded term IDs.
    1272      * }
    1273      * @return mixed Post object on success. False if no adjacent post exists. Null on failure.
    1274      */
    1275     protected function get_post( $args ) {
    1276         $this->current_post   = get_post( $args['post'] );
    1277         $this->excluded_terms = array_map( 'intval', $args['excluded_terms'] );
    1278         $this->adjacent       = $args['previous'] ? 'previous' : 'next';
    1279         $this->taxonomy       = $args['taxonomy'];
    1280         $this->in_same_term   = (bool) $args['in_same_term'];
    1281 
    1282         // Return null when either the post or taxonomy doesn't exist.
    1283         if ( ! $this->current_post ) {
    1284             return;
    1285         }
    1286         if ( $this->in_same_term || $this->excluded_terms ) {
    1287             if ( ! taxonomy_exists( $args['taxonomy'] ) ) {
    1288                 return;
     1160        $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     1161        if ( ! empty( $excluded_terms ) ) {
     1162            if ( ! is_array( $excluded_terms ) ) {
     1163                // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     1164                if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1165                    _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1166                    $excluded_terms = explode( ' and ', $excluded_terms );
     1167                } else {
     1168                    $excluded_terms = explode( ',', $excluded_terms );
     1169                }
     1170            }
     1171
     1172            $excluded_terms = array_map( 'intval', $excluded_terms );
     1173
     1174            if ( ! empty( $term_array ) ) {
     1175                $excluded_terms = array_diff( $excluded_terms, $term_array );
     1176                $posts_in_ex_terms_sql = '';
     1177            }
     1178
     1179            if ( ! empty( $excluded_terms ) ) {
     1180                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
    12891181            }
    12901182        }
    1291 
    1292         // Build our arguments for WP_Query.
    1293         $query_args = array(
    1294             'posts_per_page'      => 1,
    1295             'post_status'         => 'publish',
    1296             'post_type'           => $this->current_post->post_type,
    1297             'orderby'             => 'date',
    1298             'order'               => 'previous' === $this->adjacent ? 'DESC' : 'ASC',
    1299             'ignore_sticky_posts' => true,
    1300             'date_query'          => array(),
    1301 
    1302             // Performance considerations:
    1303             'no_found_rows'          => true,
    1304             'cache_results'          => true,
    1305             'update_post_term_cache' => false,
    1306             'update_post_meta_cache' => false,
    1307             'split_the_query'        => wp_using_ext_object_cache(),
    1308         );
    1309 
    1310         $tax_query = array();
    1311 
    1312         // Set up for requests limited to posts that share terms.
    1313         if ( $this->in_same_term ) {
    1314             $terms = get_the_terms( $this->current_post->ID, $args['taxonomy'] );
    1315 
    1316             if ( is_array( $terms ) && ! empty( $terms ) ) {
    1317                 $terms = wp_list_pluck( $terms, 'term_id' );
    1318                 $terms = array_values( $terms );
    1319                 $terms = array_map( 'intval', $terms );
    1320             } else {
    1321                 unset( $terms );
    1322             }
    1323         }
    1324 
    1325         // Handle excluded terms.
    1326         if ( $this->excluded_terms ) {
    1327             $tax_query[] = array(
    1328                 'taxonomy' => $args['taxonomy'],
    1329                 'slugs'    => $this->excluded_terms,
    1330                 'compare'  => 'NOT IN',
    1331             );
    1332         }
    1333 
    1334         // If requesting same term, ensure excluded terms don't appear in term list.
    1335         if ( isset( $terms ) ) {
    1336             if ( isset( $this->excluded_terms ) && is_array( $this->excluded_terms ) ) {
    1337                 $terms = array_diff( $terms, $this->excluded_terms );
    1338             }
    1339 
    1340             if ( ! empty( $terms ) ) {
    1341                 $tax_query[] = array(
    1342                     'taxonomy' => $args['taxonomy'],
    1343                     'terms'    => $terms,
    1344                 );
    1345             }
    1346         }
    1347 
    1348         // If we have a tax query, add it to our query args.
    1349         if ( $tax_query ) {
    1350             $query_args['tax_query'] = $tax_query;
    1351         }
    1352 
    1353         // And now, the date constraint.
    1354         $date_query_key = 'previous' === $this->adjacent ? 'before' : 'after';
    1355 
    1356         $query_args['date_query'][] = array(
    1357             $date_query_key => $this->current_post->post_date,
    1358             'inclusive'     => true,
    1359         );
    1360 
    1361         // Ensure the current post isn't returned, since we're using an inclusive date query.
    1362         $query_args['post__not_in'] = array( $this->current_post->ID );
    1363 
    1364         /**
    1365          * Filter the arguments passed to WP_Query when finding an adjacent post.
    1366          *
    1367          * @since 3.9.0
    1368          *
    1369          * @param array $query_args WP_Query arguments.
    1370          * @param array $args       Arguments passed to WP_Adjacent_Post.
    1371          */
    1372         $query_args = apply_filters( 'get_adjacent_post_query_args', $query_args, $args );
    1373 
    1374         add_filter( 'posts_clauses', array( $this, 'filter' ) );
    1375         $query = new WP_Query( $query_args );
    1376 
    1377         if ( $query->posts ) {
    1378             $this->adjacent_post = $query->post;
    1379         } else {
    1380             $this->adjacent_post = false;
    1381         }
    1382     }
    1383 
    1384     /**
    1385      * Apply the deprecated filters to WP_Query's clauses.
    1386      *
    1387      * @param array $clauses
    1388      * @return array
    1389      */
    1390     public function filter( $clauses ) {
    1391         /*
    1392          * Immediately deregister these legacy filters to avoid modifying
    1393          * any calls to WP_Query from filter callbacks hooked to WP_Query filters.
    1394          */
    1395         remove_filter( 'posts_clauses', array( $this, 'filter' ) );
    1396 
    1397         /**
    1398          * If no legacy filter callbacks are registered, proceed no further.
    1399          */
    1400         if ( ! has_filter( 'get_' . $this->adjacent . '_post_join' ) && ! has_filter( 'get_' . $this->adjacent . '_post_where' ) && ! has_filter( 'get_' . $this->adjacent . '_post_sort' ) ) {
    1401             return $clauses;
    1402         }
    1403 
    1404         /**
    1405          * Posts table must be aliased as `p` for backwards compatibility with query previously generated by `get_adjacent_post()`.
    1406          */
    1407         $clauses = array_map( array( $this, 'alias_posts_table' ), $clauses );
    1408 
    1409         /**
    1410          * Apply the legacy `join` filter.
    1411          */
    1412         if ( has_filter( 'get_' . $this->adjacent . '_post_join' ) ) {
    1413             $clauses['join'] = $this->filter_join( $clauses['join'] );
    1414         }
    1415 
    1416         /**
    1417          * Posts table must be aliased as `p` for backwards compatibility with query previously generated by `get_adjacent_post()`.
    1418          * No filter on the table name exists, so we have to leverage the next applied filter, that for the `join` clause.
    1419          * We wait to apply this until after the legacy filter is applied so that the legacy filter doesn't remove the alias.
    1420          */
    1421         $clauses['join'] = 'AS p ' . $clauses['join'];
    1422 
    1423         /**
    1424          * Apply the legacy `where` filter.
    1425          */
    1426         if ( has_filter( 'get_' . $this->adjacent . '_post_where' ) ) {
    1427             $clauses['where'] = $this->filter_where( $clauses['where'] );
    1428         }
    1429 
    1430         /*
    1431          * The legacy `sort` filter combined the ORDER BY and LIMIT clauses,
    1432          * while `WP_Query` does not, which requires special handling.
    1433          */
    1434         if ( has_filter( 'get_' . $this->adjacent . '_post_sort' ) ) {
    1435             $sort_clauses = $this->filter_sort( $clauses['orderby'], $clauses['limits'] );
    1436             $clauses      = array_merge( $clauses, $sort_clauses );
    1437         }
    1438 
    1439         return $clauses;
    1440     }
    1441 
    1442     /**
    1443      * Alias posts table as `p` to match query previously built by `get_adjacent_post()`.
    1444      *
    1445      * @global $wpdb
    1446      * @param string  Clause to alias
    1447      * @return string
    1448      */
    1449     protected function alias_posts_table( $clause ) {
    1450         global $wpdb;
    1451 
    1452         return str_replace( $wpdb->posts, 'p', $clause );
    1453     }
    1454 
    1455     /**
    1456      * Apply the deprecated `join` clause filter to the clause built by WP_Query.
    1457      *
    1458      * @param string $join
    1459      * @return string
    1460      */
    1461     protected function filter_join( $join ) {
    1462         /**
    1463          * @deprecated 3.9.0
    1464          */
    1465         return apply_filters( 'get_' . $this->adjacent . '_post_join', $join, $this->in_same_term, $this->excluded_terms );
    1466     }
    1467 
    1468     /**
    1469      * Apply the deprecated `where` clause filter to the clause built by WP_Query.
    1470      *
    1471      * @param string $join
    1472      * @return string
    1473      */
    1474     protected function filter_where( $where ) {
    1475         $where = trim( $where );
    1476 
    1477         // The legacy filter passed the entire clause, including the `WHERE`, while WP_Query's filter does not.
    1478         // We prepend the `WHERE` for the benefit of legacy callbacks that look for it.
    1479         if ( 0 !== stripos( $where, 'where' ) ) {
    1480             $where = 'WHERE 1=1 ' . $where;
    1481         }
    1482 
    1483         /**
    1484          * @deprecated 3.9.0
    1485          */
    1486         $where = apply_filters( 'get_' . $this->adjacent . '_post_where', $where, $this->in_same_term, $this->excluded_terms );
    1487 
    1488         $where = trim( $where );
    1489 
    1490         // The legacy filter passed the entire clause, including the `WHERE`, while WP_Query's filter does not.
    1491         // Removing the `WHERE` is necessary as we've added it above, and the legacy filter could include it in the returned string.
    1492         if ( 0 === stripos( $where, 'where 1=1' ) ) {
    1493             $where = substr( $where, 9 );
    1494         } elseif ( 0 === stripos( $where, 'where' ) ) {
    1495             $where = substr( $where, 5 );
    1496         }
    1497 
    1498         $where = trim( $where );
    1499 
    1500         // WP_Query expects that the string returned begins with `AND`, as it is prepended with "1=1" when the clauses are joined
    1501         if ( 0 !== stripos( $where, 'and' ) ) {
    1502             $where = 'AND ' . $where;
    1503         }
    1504 
    1505         return $where;
    1506     }
    1507 
    1508     /**
    1509      * Apply deprecated `sort` filter, which applies to both the ORDER BY and LIMIT clauses.
    1510      *
    1511      * @param string $orderby
    1512      * @param string $limits
    1513      * @return array
    1514      */
    1515     protected function filter_sort( $orderby, $limits ) {
    1516         /**
    1517          * @deprecated 3.9.0
    1518          */
    1519         $sort = apply_filters( 'get_' . $this->adjacent . '_post_sort', 'ORDER BY ' . $orderby . ' ' . $limits );
    1520 
    1521         if ( empty( $sort ) ) {
    1522             return compact( 'orderby', 'limits' );
    1523         }
    1524 
    1525         // The legacy filter could allow either clause to be removed, or their order inverted, so we need to know what we have and where.
    1526         $has_order_by = stripos( $sort, 'order by' );
    1527         $has_limit    = stripos( $sort, 'limit' );
    1528 
    1529         // Split the string of one or two clauses into their respective array keys
    1530         if ( false !== $has_order_by && false !== $has_limit ) {
    1531             /*
    1532              * The LIMIT clause cannot appear before the ORDER BY clause in a valid query
    1533              * However, since the legacy filter would allow a user to invert the order,
    1534              * we maintain that handling so the same errors are triggered.
    1535              */
    1536             if ( $has_order_by < $has_limit ) {
    1537                 $orderby = trim( str_ireplace( 'order by', '', substr( $sort, 0, $has_limit ) ) );
    1538                 $limits  = trim( substr( $sort, $has_limit ) );
    1539             } else {
    1540                 $orderby = trim( str_ireplace( 'order by', '', substr( $sort, $has_order_by ) ) );
    1541                 $limits  = trim( substr( $sort, 0, $has_order_by ) );
    1542             }
    1543         } elseif ( false !== $has_order_by ) {
    1544             $orderby = trim( str_ireplace( 'order by', '', $sort ) );
    1545             $limits  = '';
    1546         } elseif ( false !== $has_limit ) {
    1547             $orderby = '';
    1548             $limits  = trim( $sort );
    1549         }
    1550 
    1551         return compact( 'orderby', 'limits' );
    1552     }
     1183    }
     1184
     1185    $adjacent = $previous ? 'previous' : 'next';
     1186    $op = $previous ? '<' : '>';
     1187    $order = $previous ? 'DESC' : 'ASC';
     1188
     1189    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
     1190    $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 );
     1191    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
     1192
     1193    $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
     1194    $query_key = 'adjacent_post_' . md5( $query );
     1195    $result = wp_cache_get( $query_key, 'counts' );
     1196    if ( false !== $result ) {
     1197        if ( $result )
     1198            $result = get_post( $result );
     1199        return $result;
     1200    }
     1201
     1202    $result = $wpdb->get_var( $query );
     1203    if ( null === $result )
     1204        $result = '';
     1205
     1206    wp_cache_set( $query_key, $result, 'counts' );
     1207
     1208    if ( $result )
     1209        $result = get_post( $result );
     1210
     1211    return $result;
    15531212}
    15541213
  • trunk/src/wp-includes/load.php

    r27634 r27836  
    378378    if ( null !== $using )
    379379        $_wp_using_ext_object_cache = $using;
    380     return (bool) $current_using;
     380    return $current_using;
    381381}
    382382
  • trunk/src/wp-includes/query.php

    r27686 r27836  
    32223222
    32233223        $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
    3224         if ( $split_the_query && isset( $q['split_the_query'] ) && empty( $q['split_the_query'] ) ) {
    3225             $split_the_query = false;
    3226         }
    32273224
    32283225        /**
  • trunk/tests/phpunit/tests/link.php

    r27803 r27836  
    189189        $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
    190190    }
    191 
    192     /**
    193      * @ticket 26937
    194      */
    195     function test_legacy_get_adjacent_post_filters() {
    196         // Need some sample posts to test adjacency
    197         $post_one = $this->factory->post->create_and_get( array(
    198             'post_title' => 'First',
    199             'post_date'  => '2012-01-01 12:00:00',
    200         ) );
    201 
    202         $post_two = $this->factory->post->create_and_get( array(
    203             'post_title' => 'Second',
    204             'post_date'  => '2012-02-01 12:00:00',
    205         ) );
    206 
    207         $post_three = $this->factory->post->create_and_get( array(
    208             'post_title' => 'Third',
    209             'post_date'  => '2012-03-01 12:00:00',
    210         ) );
    211 
    212         $post_four = $this->factory->post->create_and_get( array(
    213             'post_title' => 'Fourth',
    214             'post_date'  => '2012-04-01 12:00:00',
    215         ) );
    216 
    217         // Use pages to test post-type adjacency
    218         $page_one = $this->factory->post->create_and_get( array(
    219             'post_title' => 'First Page',
    220             'post_date'  => '2013-01-01 12:00:00',
    221             'post_type'  => 'page',
    222         ) );
    223 
    224         $page_two = $this->factory->post->create_and_get( array(
    225             'post_title' => 'Second Page',
    226             'post_date'  => '2013-02-01 12:00:00',
    227             'post_type'  => 'page',
    228         ) );
    229 
    230         // Add some meta so we can join the postmeta table and query
    231         add_post_meta( $post_three->ID, 'unit_test_meta', 'waffle' );
    232 
    233         // Test "where" filter for a previous post
    234         add_filter( 'get_previous_post_where', array( $this, 'filter_previous_post_where' ) );
    235         $this->go_to( get_permalink( $post_three->ID ) );
    236         $this->assertEquals( $post_one, get_adjacent_post( false, null, true ) );
    237         remove_filter( 'get_previous_post_where', array( $this, 'filter_previous_post_where' ) );
    238 
    239         // Test "where" filter for a next post
    240         add_filter( 'get_next_post_where', array( $this, 'filter_next_post_where' ) );
    241         $this->go_to( get_permalink( $post_two->ID ) );
    242         $this->assertEquals( $post_four, get_adjacent_post( false, null, false ) );
    243         remove_filter( 'get_next_post_where', array( $this, 'filter_next_post_where' ) );
    244 
    245         // Test "where" filter that writes its own query
    246         add_filter( 'get_previous_post_where', array( $this, 'override_previous_post_where_clause' ) );
    247         $this->go_to( get_permalink( $post_four->ID ) );
    248         $this->assertEquals( $post_two, get_adjacent_post( false, null, true ) );
    249         remove_filter( 'get_previous_post_where', array( $this, 'override_previous_post_where_clause' ) );
    250 
    251         // Test "join" filter by joining the postmeta table and restricting by meta key
    252         add_filter( 'get_next_post_join', array( $this, 'filter_next_post_join' ) );
    253         add_filter( 'get_next_post_where', array( $this, 'filter_next_post_where_with_join' ) );
    254         $this->go_to( get_permalink( $post_one->ID ) );
    255         $this->assertEquals( $post_three, get_adjacent_post( false, null, false ) );
    256         remove_filter( 'get_next_post_join', array( $this, 'filter_next_post_join' ) );
    257         remove_filter( 'get_next_post_where', array( $this, 'filter_next_post_where_with_join' ) );
    258 
    259         // Test "sort" filter when modifying ORDER BY clause
    260         add_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort' ) );
    261         $this->go_to( get_permalink( $post_one->ID ) );
    262         $this->assertEquals( $post_four, get_adjacent_post( false, null, false ) );
    263         remove_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort' ) );
    264 
    265         // Test "sort" filter when modifying LIMIT clause
    266         add_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort_limit' ) );
    267         $this->go_to( get_permalink( $post_one->ID ) );
    268         $this->assertEquals( $post_three, get_adjacent_post( false, null, false ) );
    269         remove_filter( 'get_next_post_sort', array( $this, 'filter_next_post_sort_limit' ) );
    270 
    271         // Test post-type specificity
    272         $this->go_to( get_permalink( $page_one ) );
    273         $this->assertEquals( $page_two, get_adjacent_post( false, null, false ) );
    274 
    275         $this->go_to( get_permalink( $page_two ) );
    276         $this->assertEquals( $page_one, get_adjacent_post( false, null, true ) );
    277     }
    278 
    279     /**
    280      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    281      */
    282     function filter_previous_post_where( $where ) {
    283         $where .= " AND post_title !='Second'";
    284         return $where;
    285     }
    286 
    287     /**
    288      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    289      */
    290     function filter_next_post_where( $where ) {
    291         $where .= " AND post_title !='Third'";
    292         return $where;
    293     }
    294 
    295     /**
    296      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    297      */
    298     function override_previous_post_where_clause( $where ) {
    299         $where = "WHERE p.post_date < '2012-02-28'";
    300         return $where;
    301     }
    302 
    303     /**
    304      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    305      */
    306     function filter_next_post_join( $join ) {
    307         global $wpdb;
    308 
    309         $join .= " INNER JOIN {$wpdb->postmeta} ON p.ID = {$wpdb->postmeta}.post_id";
    310         return $join;
    311     }
    312 
    313     /**
    314      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    315      */
    316     function filter_next_post_where_with_join( $where ) {
    317         global $wpdb;
    318 
    319         $where .= " AND {$wpdb->postmeta}.meta_key = 'unit_test_meta'";
    320         return $where;
    321     }
    322 
    323     /**
    324      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    325      */
    326     function filter_next_post_sort( $sort ) {
    327         return str_replace( 'p.post_date', 'p.post_title', $sort );
    328     }
    329 
    330     /**
    331      * Filter callback for `test_legacy_get_adjacent_post_filters()`
    332      */
    333     function filter_next_post_sort_limit( $sort ) {
    334         $sort = str_replace( 'LIMIT 0, 1', 'LIMIT 1, 2', $sort );
    335         return $sort;
    336     }
    337191}
Note: See TracChangeset for help on using the changeset viewer.