Taxonomy: Improve back compat of values passed to 'terms_clauses' filter.

Prior to the introduction of `WP_Term_Query`, the 'orderby' clause
passed to the 'terms_clauses' filter was prefixed by `ORDER BY`. After
`WP_Term_Query`, this was not the case; `ORDER BY` was added after the
filter. As such, plugins filtering 'terms_clauses' and returning an
'orderby' clause beginning with `ORDER BY` resulted in invalid syntax
when `WP_Term_Query` prepended a second `ORDER BY` keyword to
the clause.

This changeset rearranges the way the 'orderby' clause is built so that
it will be passed to 'terms_clauses' in the previous format.

Fixes #37378.

git-svn-id: https://develop.svn.wordpress.org/trunk@38099 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-07-19 02:12:48 +00:00
parent 456d5b4880
commit 103da159d5
2 changed files with 27 additions and 1 deletions

View File

@ -384,6 +384,10 @@ class WP_Term_Query {
}
$orderby = $this->parse_orderby( $this->query_vars['orderby'] );
if ( $orderby ) {
$orderby = "ORDER BY $orderby";
}
$order = $this->parse_order( $this->query_vars['order'] );
if ( $taxonomies ) {
@ -618,7 +622,7 @@ class WP_Term_Query {
$this->sql_clauses['select'] = "SELECT $distinct $fields";
$this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join";
$this->sql_clauses['orderby'] = $orderby ? "ORDER BY $orderby $order" : '';
$this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits;
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";

View File

@ -85,4 +85,26 @@ class Tests_Term_Query extends WP_UnitTestCase {
$found = array_map( 'intval', $q->terms );
$this->assertSame( array( $terms[1], $terms[0], $terms[2] ), $found );
}
/**
* @ticket 37378
*/
public function test_order_by_keyword_should_not_be_duplicated_when_filtered() {
register_taxonomy( 'wptests_tax', 'post' );
add_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
$q = new WP_Term_Query( array(
'taxonomy' => array( 'wptests_tax' ),
'orderby' => 'name',
) );
remove_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
$this->assertContains( 'ORDER BY tt.term_id', $q->request );
$this->assertNotContains( 'ORDER BY ORDER BY', $q->request );
}
public function filter_terms_clauses( $clauses ) {
$clauses['orderby'] = 'ORDER BY tt.term_id';
return $clauses;
}
}