Make WordPress Core


Ignore:
Timestamp:
10/17/2014 01:19:03 AM (10 years ago)
Author:
boonebgorges
Message:

Use table aliases for columns in SQL generated by WP_Date_Query.

The use of non-aliased column names (eg 'post_date' instead of 'wp_posts.post_date')
in WP_Date_Query causes SQL notices and other failures when queries involve
table joins, such as date_query combined with tax_query or meta_query. This
changeset modifies WP_Date_Query::validate_column() to add the table alias when
it can be detected from the column name (ie, in the case of core columns).

A side effect of this change is that it is now possible to use WP_Date_Query
to build WHERE clauses across multiple tables, though there is currently no
core support for the automatic generation of the necessary JOIN clauses. See

Props ew_holmes, wonderboymusic, neoxx, Viper007Bond, boonebgorges.
Fixes #25775.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/date/query.php

    r29925 r29933  
    202202
    203203    public function test_validate_column_post_date() {
    204         $q = new WP_Date_Query( array() );
    205 
    206         $this->assertSame( 'post_date', $q->validate_column( 'post_date' ) );
     204        global $wpdb;
     205        $q = new WP_Date_Query( array() );
     206
     207        $this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'post_date' ) );
    207208    }
    208209
    209210    public function test_validate_column_post_date_gmt() {
    210         $q = new WP_Date_Query( array() );
    211 
    212         $this->assertSame( 'post_date_gmt', $q->validate_column( 'post_date_gmt' ) );
     211        global $wpdb;
     212        $q = new WP_Date_Query( array() );
     213
     214        $this->assertSame( $wpdb->posts . '.post_date_gmt', $q->validate_column( 'post_date_gmt' ) );
    213215    }
    214216
    215217    public function test_validate_column_post_modified() {
    216         $q = new WP_Date_Query( array() );
    217 
    218         $this->assertSame( 'post_modified', $q->validate_column( 'post_modified' ) );
     218        global $wpdb;
     219        $q = new WP_Date_Query( array() );
     220
     221        $this->assertSame( $wpdb->posts . '.post_modified', $q->validate_column( 'post_modified' ) );
    219222    }
    220223
    221224    public function test_validate_column_post_modified_gmt() {
    222         $q = new WP_Date_Query( array() );
    223 
    224         $this->assertSame( 'post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) );
     225        global $wpdb;
     226        $q = new WP_Date_Query( array() );
     227
     228        $this->assertSame( $wpdb->posts . '.post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) );
    225229    }
    226230
    227231    public function test_validate_column_comment_date() {
    228         $q = new WP_Date_Query( array() );
    229 
    230         $this->assertSame( 'comment_date', $q->validate_column( 'comment_date' ) );
     232        global $wpdb;
     233        $q = new WP_Date_Query( array() );
     234
     235        $this->assertSame( $wpdb->comments . '.comment_date', $q->validate_column( 'comment_date' ) );
    231236    }
    232237
    233238    public function test_validate_column_comment_date_gmt() {
    234         $q = new WP_Date_Query( array() );
    235 
    236         $this->assertSame( 'comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) );
     239        global $wpdb;
     240        $q = new WP_Date_Query( array() );
     241
     242        $this->assertSame( $wpdb->comments . '.comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) );
    237243    }
    238244
    239245    public function test_validate_column_invalid() {
    240         $q = new WP_Date_Query( array() );
    241 
    242         $this->assertSame( 'post_date', $q->validate_column( 'foo' ) );
     246        global $wpdb;
     247        $q = new WP_Date_Query( array() );
     248
     249        $this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'foo' ) );
     250    }
     251
     252    /**
     253     * @ticket 25775
     254     */
     255    public function test_validate_column_with_date_query_valid_columns_filter() {
     256        $q = new WP_Date_Query( array() );
     257
     258        add_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
     259
     260        $this->assertSame( 'my_custom_column', $q->validate_column( 'my_custom_column' ) );
     261
     262        remove_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
     263    }
     264
     265    /**
     266     * @ticket 25775
     267     */
     268    public function test_validate_column_prefixed_column_name() {
     269        $q = new WP_Date_Query( array() );
     270
     271        $this->assertSame( 'foo.bar', $q->validate_column( 'foo.bar' ) );
     272    }
     273
     274    /**
     275     * @ticket 25775
     276     */
     277    public function test_validate_column_prefixed_column_name_with_illegal_characters() {
     278        $q = new WP_Date_Query( array() );
     279
     280        $this->assertSame( 'foo.bar', $q->validate_column( 'f"\'oo\/.b;:()ar' ) );
    243281    }
    244282
    245283    public function test_build_value_value_null() {
     284        global $wpdb;
    246285        $q = new WP_Date_Query( array() );
    247286
     
    903942        }
    904943    }
     944
     945    /** Helpers **********************************************************/
     946
     947    public function date_query_valid_columns_callback( $columns ) {
     948        $columns[] = 'my_custom_column';
     949        return $columns;
     950    }
    905951}
Note: See TracChangeset for help on using the changeset viewer.