Make WordPress Core

Changeset 38099


Ignore:
Timestamp:
07/19/2016 02:12:48 AM (10 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r38020 r38099  
    385385
    386386                $orderby = $this->parse_orderby( $this->query_vars['orderby'] );
     387                if ( $orderby ) {
     388                        $orderby = "ORDER BY $orderby";
     389                }
     390
    387391                $order = $this->parse_order( $this->query_vars['order'] );
    388392
     
    619623                $this->sql_clauses['select']  = "SELECT $distinct $fields";
    620624                $this->sql_clauses['from']    = "FROM $wpdb->terms AS t $join";
    621                 $this->sql_clauses['orderby'] = $orderby ? "ORDER BY $orderby $order" : '';
     625                $this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
    622626                $this->sql_clauses['limits']  = $limits;
    623627
  • trunk/tests/phpunit/tests/term/query.php

    r37860 r38099  
    8686                $this->assertSame( array( $terms[1], $terms[0], $terms[2] ), $found );
    8787        }
     88
     89        /**
     90         * @ticket 37378
     91         */
     92        public function test_order_by_keyword_should_not_be_duplicated_when_filtered() {
     93                register_taxonomy( 'wptests_tax', 'post' );
     94
     95                add_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
     96                $q = new WP_Term_Query( array(
     97                        'taxonomy' => array( 'wptests_tax' ),
     98                        'orderby' => 'name',
     99                ) );
     100                remove_filter( 'terms_clauses', array( $this, 'filter_terms_clauses' ) );
     101
     102                $this->assertContains( 'ORDER BY tt.term_id', $q->request );
     103                $this->assertNotContains( 'ORDER BY ORDER BY', $q->request );
     104        }
     105
     106        public function filter_terms_clauses( $clauses ) {
     107                $clauses['orderby'] = 'ORDER BY tt.term_id';
     108                return $clauses;
     109        }
    88110}
Note: See TracChangeset for help on using the changeset viewer.