Make WordPress Core

Ticket #25775: 25775.2.patch

File 25775.2.patch, 8.0 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/date.php

    diff --git src/wp-includes/date.php src/wp-includes/date.php
    index 1cf4c6d..0c2a0af 100644
    class WP_Date_Query { 
    165165         * @return string A validated column name value.
    166166         */
    167167        public function validate_column( $column ) {
     168                global $wpdb;
     169
    168170                $valid_columns = array(
    169171                        'post_date', 'post_date_gmt', 'post_modified',
    170172                        'post_modified_gmt', 'comment_date', 'comment_date_gmt'
    171173                );
    172                 /**
    173                  * Filter the list of valid date query columns.
    174                  *
    175                  * @since 3.7.0
    176                  *
    177                  * @param array $valid_columns An array of valid date query columns. Defaults are 'post_date', 'post_date_gmt',
    178                  *                             'post_modified', 'post_modified_gmt', 'comment_date', 'comment_date_gmt'
    179                  */
    180                 if ( ! in_array( $column, apply_filters( 'date_query_valid_columns', $valid_columns ) ) )
    181                         $column = 'post_date';
    182174
    183                 return $column;
     175                // Attempt to detect a table prefix.
     176                if ( false === strpos( $column, '.' ) ) {
     177                        /**
     178                         * Filter the list of valid date query columns.
     179                         *
     180                         * @since 3.7.0
     181                         *
     182                         * @param array $valid_columns An array of valid date query columns. Defaults
     183                         *                             are 'post_date', 'post_date_gmt', 'post_modified',
     184                         *                             'post_modified_gmt', 'comment_date', 'comment_date_gmt'
     185                         */
     186                        if ( ! in_array( $column, apply_filters( 'date_query_valid_columns', $valid_columns ) ) ) {
     187                                $column = 'post_date';
     188                        }
     189
     190                        $known_columns = array(
     191                                $wpdb->posts => array(
     192                                        'post_date',
     193                                        'post_date_gmt',
     194                                        'post_modified',
     195                                        'post_modified_gmt',
     196                                ),
     197                                $wpdb->comments => array(
     198                                        'comment_date',
     199                                        'comment_date_gmt',
     200                                ),
     201                        );
     202
     203                        foreach ( $known_columns as $table_name => $table_columns ) {
     204                                if ( in_array( $column, $table_columns ) ) {
     205                                        $column = $table_name . '.' . $column;
     206                                        break;
     207                                }
     208                        }
     209
     210                }
     211
     212                return esc_sql( $column );
    184213        }
    185214
    186215        /**
  • tests/phpunit/tests/date/query.php

    diff --git tests/phpunit/tests/date/query.php tests/phpunit/tests/date/query.php
    index e9dd191..1acbff2 100644
    class Tests_WP_Date_Query extends WP_UnitTestCase { 
    182182        }
    183183
    184184        public function test_validate_column_post_date() {
     185                global $wpdb;
    185186                $q = new WP_Date_Query( array() );
    186187
    187                 $this->assertSame( 'post_date', $q->validate_column( 'post_date' ) );
     188                $this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'post_date' ) );
    188189        }
    189190
    190191        public function test_validate_column_post_date_gmt() {
     192                global $wpdb;
    191193                $q = new WP_Date_Query( array() );
    192194
    193                 $this->assertSame( 'post_date_gmt', $q->validate_column( 'post_date_gmt' ) );
     195                $this->assertSame( $wpdb->posts . '.post_date_gmt', $q->validate_column( 'post_date_gmt' ) );
    194196        }
    195197
    196198        public function test_validate_column_post_modified() {
     199                global $wpdb;
    197200                $q = new WP_Date_Query( array() );
    198201
    199                 $this->assertSame( 'post_modified', $q->validate_column( 'post_modified' ) );
     202                $this->assertSame( $wpdb->posts . '.post_modified', $q->validate_column( 'post_modified' ) );
    200203        }
    201204
    202205        public function test_validate_column_post_modified_gmt() {
     206                global $wpdb;
    203207                $q = new WP_Date_Query( array() );
    204208
    205                 $this->assertSame( 'post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) );
     209                $this->assertSame( $wpdb->posts . '.post_modified_gmt', $q->validate_column( 'post_modified_gmt' ) );
    206210        }
    207211
    208212        public function test_validate_column_comment_date() {
     213                global $wpdb;
    209214                $q = new WP_Date_Query( array() );
    210215
    211                 $this->assertSame( 'comment_date', $q->validate_column( 'comment_date' ) );
     216                $this->assertSame( $wpdb->comments . '.comment_date', $q->validate_column( 'comment_date' ) );
    212217        }
    213218
    214219        public function test_validate_column_comment_date_gmt() {
     220                global $wpdb;
    215221                $q = new WP_Date_Query( array() );
    216222
    217                 $this->assertSame( 'comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) );
     223                $this->assertSame( $wpdb->comments . '.comment_date_gmt', $q->validate_column( 'comment_date_gmt' ) );
    218224        }
    219225
    220226        public function test_validate_column_invalid() {
     227                global $wpdb;
    221228                $q = new WP_Date_Query( array() );
    222229
    223                 $this->assertSame( 'post_date', $q->validate_column( 'foo' ) );
     230                $this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'foo' ) );
     231        }
     232
     233        /**
     234         * @ticket 25775
     235         */
     236        public function test_validate_column_with_date_query_valid_columns_filter() {
     237                $q = new WP_Date_Query( array() );
     238
     239                add_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
     240
     241                $this->assertSame( 'my_custom_column', $q->validate_column( 'my_custom_column' ) );
     242
     243                remove_filter( 'date_query_valid_columns', array( $this, 'date_query_valid_columns_callback' ) );
     244        }
     245
     246        /**
     247         * @ticket 25775
     248         */
     249        public function test_validate_column_prefixed_column_name() {
     250                $q = new WP_Date_Query( array() );
     251
     252                $this->assertSame( 'foo.bar', $q->validate_column( 'foo.bar' ) );
    224253        }
    225254
    226255        public function test_build_value_value_null() {
     256                global $wpdb;
    227257                $q = new WP_Date_Query( array() );
    228258
    229259                $this->assertFalse( $q->build_value( 'foo', null ) );
    class Tests_WP_Date_Query extends WP_UnitTestCase { 
    571601                // varying precision on different PHP installations
    572602                $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $found );
    573603        }
     604
     605        /** Helpers **********************************************************/
     606
     607        public function date_query_valid_columns_callback( $columns ) {
     608                $columns[] = 'my_custom_column';
     609                return $columns;
     610        }
    574611}
  • tests/phpunit/tests/query/dateQuery.php

    diff --git tests/phpunit/tests/query/dateQuery.php tests/phpunit/tests/query/dateQuery.php
    index 2f94c4a..cadffc1 100644
    class Tests_Query_DateQuery extends WP_UnitTestCase { 
    590590        }
    591591
    592592        public function test_date_params_monthnum_m_duplicate() {
     593                global $wpdb;
     594
    593595                $posts = $this->_get_query_result( array(
    594596                        'date_query' => array(
    595597                                'month' => 5,
    class Tests_Query_DateQuery extends WP_UnitTestCase { 
    607609
    608610                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    609611
    610                 $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request );
     612                $this->assertContains( "AND ( ( MONTH( $wpdb->posts.post_date ) = 5 ) ) AND", $this->q->request );
    611613
    612                 $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request );
     614                $this->assertNotContains( "AND ( ( MONTH( $wpdb->posts.post_date ) = 5 AND MONTH( $wpdb->posts.post_date ) = 9 ) ) AND", $this->q->request );
    613615        }
    614616
    615617        public function test_date_params_week_w_duplicate() {
     618                global $wpdb;
     619
    616620                $posts = $this->_get_query_result( array(
    617621                        'date_query' => array(
    618622                                'week' => 21,
    class Tests_Query_DateQuery extends WP_UnitTestCase { 
    629633
    630634                $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
    631635
    632                 $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request );
     636                $this->assertContains( "AND ( ( WEEK( $wpdb->posts.post_date, 1 ) = 21 ) ) AND", $this->q->request );
     637
     638                $this->assertNotContains( "AND ( ( WEEK( $wpdb->posts.post_date, 1 ) = 21 AND WEEK( $wpdb->posts.post_date, 1 ) = 22 ) ) AND", $this->q->request );
     639        }
     640
     641        /**
     642         * @ticket 25775
     643         */
     644        public function test_date_query_with_taxonomy_join() {
     645                $p1 = $this->factory->post->create( array(
     646                        'post_date' => '2013-04-27 01:01:01',
     647                ) );
     648                $p2 = $this->factory->post->create( array(
     649                        'post_date' => '2013-03-21 01:01:01',
     650                ) );
     651
     652                register_taxonomy( 'foo', 'post' );
     653                wp_set_object_terms( $p1, 'bar', 'foo' );
     654
     655                $posts = $this->_get_query_result( array(
     656                        'date_query' => array(
     657                                'year' => 2013,
     658                        ),
     659                        'tax_query' => array(
     660                                array(
     661                                        'taxonomy' => 'foo',
     662                                        'terms' => array( 'bar' ),
     663                                        'field' => 'name',
     664                                ),
     665                        ),
     666                ) );
     667
     668                _unregister_taxonomy( 'foo' );
    633669
    634                 $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request );
     670                $this->assertEquals( array( $p1 ), wp_list_pluck( $posts, 'ID' ) );
    635671        }
    636 }
    637  No newline at end of file
     672}