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.

Built from https://develop.svn.wordpress.org/trunk@27836


git-svn-id: http://core.svn.wordpress.org/trunk@27670 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2014-03-29 06:03:15 +00:00
parent bc12ccf41b
commit 88e3ebe05b
3 changed files with 56 additions and 400 deletions

View File

@ -1133,423 +1133,82 @@ function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy =
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
* @param bool $previous Optional. Whether to retrieve previous post.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
* @return mixed Post object if successful. Null if current post doesn't exist. Empty string if no corresponding adjacent post exists.
* @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
*/
function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
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();
}
global $wpdb;
$adjacent = new WP_Adjacent_Post( array(
'post' => get_post(),
'previous' => $previous,
'taxonomy' => $taxonomy,
'in_same_term' => $in_same_term,
'excluded_terms' => $excluded_terms,
) );
if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
return null;
return $adjacent->adjacent_post;
}
$current_post_date = $post->post_date;
/**
* 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.
*
* @since 3.9.0
*
* @package WordPress
* @subpackage Template
*/
class WP_Adjacent_Post {
$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";
/**
* Adjacent post object.
*
* @since 3.9.0
* @access public
* @var null|WP_Adjacent_Post
*/
public $adjacent_post = null;
/**
* Current post object.
*
* @since 3.9.0
* @access protected
* @var bool|WP_Post
*/
protected $current_post = false;
/**
* 'previous' or 'next' type of adjacent post.
*
* @since 3.9.0
* @access protected
* @var string
*/
protected $adjacent = 'previous';
/**
* Post taxonomy.
*
* @since 3.9.0
* @access protected
* @var string
*/
protected $taxonomy = 'category';
/**
* Whether the post should be in a same taxonomy term.
*
* @since 3.9.0
* @access protected
* @var string
*/
protected $in_same_term = false;
/**
* Excluded term IDs.
*
* @since 3.9.0
* @access protected
* @var string|array
*/
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;
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 );
}
$this->get_post( $args );
}
/**
* Allow direct access to adjacent post from the class instance itself.
*
* @param string $property Property to get.
* @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->taxonomy = $args['taxonomy'];
$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;
}
}
// Build our arguments for WP_Query.
$query_args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => $this->current_post->post_type,
'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(),
);
$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 );
}
}
// 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 );
$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 );
}
}
if ( ! empty( $terms ) ) {
$tax_query[] = array(
'taxonomy' => $args['taxonomy'],
'terms' => $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 );
}
}
// 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_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;
}
}
/**
* Apply the deprecated filters to WP_Query's clauses.
*
* @param array $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' ) );
$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
/**
* If no legacy filter callbacks are registered, proceed no further.
*/
if ( ! has_filter( 'get_' . $this->adjacent . '_post_join' ) && ! has_filter( 'get_' . $this->adjacent . '_post_where' ) && ! has_filter( 'get_' . $this->adjacent . '_post_sort' ) ) {
return $clauses;
}
$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" );
/**
* 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'] );
}
/*
* 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;
$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;
}
/**
* Alias posts table as `p` to match query previously built by `get_adjacent_post()`.
*
* @global $wpdb
* @param string Clause to alias
* @return string
*/
protected function alias_posts_table( $clause ) {
global $wpdb;
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = '';
return str_replace( $wpdb->posts, 'p', $clause );
}
wp_cache_set( $query_key, $result, 'counts' );
/**
* Apply the deprecated `join` clause filter to the clause built by WP_Query.
*
* @param string $join
* @return string
*/
protected function filter_join( $join ) {
/**
* @deprecated 3.9.0
*/
return apply_filters( 'get_' . $this->adjacent . '_post_join', $join, $this->in_same_term, $this->excluded_terms );
}
if ( $result )
$result = get_post( $result );
/**
* 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;
}
/**
* 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 );
if ( empty( $sort ) ) {
return compact( 'orderby', 'limits' );
}
// 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' );
}
return $result;
}
/**

View File

@ -377,7 +377,7 @@ function wp_using_ext_object_cache( $using = null ) {
$current_using = $_wp_using_ext_object_cache;
if ( null !== $using )
$_wp_using_ext_object_cache = $using;
return (bool) $current_using;
return $current_using;
}
/**

View File

@ -3221,9 +3221,6 @@ class WP_Query {
}
$split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
if ( $split_the_query && isset( $q['split_the_query'] ) && empty( $q['split_the_query'] ) ) {
$split_the_query = false;
}
/**
* Filter whether to split the query.