diff --git src/wp-includes/date.php src/wp-includes/date.php
index 1cf4c6d..0c2a0af 100644
|
|
class WP_Date_Query { |
165 | 165 | * @return string A validated column name value. |
166 | 166 | */ |
167 | 167 | public function validate_column( $column ) { |
| 168 | global $wpdb; |
| 169 | |
168 | 170 | $valid_columns = array( |
169 | 171 | 'post_date', 'post_date_gmt', 'post_modified', |
170 | 172 | 'post_modified_gmt', 'comment_date', 'comment_date_gmt' |
171 | 173 | ); |
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'; |
182 | 174 | |
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 ); |
184 | 213 | } |
185 | 214 | |
186 | 215 | /** |
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 { |
182 | 182 | } |
183 | 183 | |
184 | 184 | public function test_validate_column_post_date() { |
| 185 | global $wpdb; |
185 | 186 | $q = new WP_Date_Query( array() ); |
186 | 187 | |
187 | | $this->assertSame( 'post_date', $q->validate_column( 'post_date' ) ); |
| 188 | $this->assertSame( $wpdb->posts . '.post_date', $q->validate_column( 'post_date' ) ); |
188 | 189 | } |
189 | 190 | |
190 | 191 | public function test_validate_column_post_date_gmt() { |
| 192 | global $wpdb; |
191 | 193 | $q = new WP_Date_Query( array() ); |
192 | 194 | |
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' ) ); |
194 | 196 | } |
195 | 197 | |
196 | 198 | public function test_validate_column_post_modified() { |
| 199 | global $wpdb; |
197 | 200 | $q = new WP_Date_Query( array() ); |
198 | 201 | |
199 | | $this->assertSame( 'post_modified', $q->validate_column( 'post_modified' ) ); |
| 202 | $this->assertSame( $wpdb->posts . '.post_modified', $q->validate_column( 'post_modified' ) ); |
200 | 203 | } |
201 | 204 | |
202 | 205 | public function test_validate_column_post_modified_gmt() { |
| 206 | global $wpdb; |
203 | 207 | $q = new WP_Date_Query( array() ); |
204 | 208 | |
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' ) ); |
206 | 210 | } |
207 | 211 | |
208 | 212 | public function test_validate_column_comment_date() { |
| 213 | global $wpdb; |
209 | 214 | $q = new WP_Date_Query( array() ); |
210 | 215 | |
211 | | $this->assertSame( 'comment_date', $q->validate_column( 'comment_date' ) ); |
| 216 | $this->assertSame( $wpdb->comments . '.comment_date', $q->validate_column( 'comment_date' ) ); |
212 | 217 | } |
213 | 218 | |
214 | 219 | public function test_validate_column_comment_date_gmt() { |
| 220 | global $wpdb; |
215 | 221 | $q = new WP_Date_Query( array() ); |
216 | 222 | |
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' ) ); |
218 | 224 | } |
219 | 225 | |
220 | 226 | public function test_validate_column_invalid() { |
| 227 | global $wpdb; |
221 | 228 | $q = new WP_Date_Query( array() ); |
222 | 229 | |
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' ) ); |
224 | 253 | } |
225 | 254 | |
226 | 255 | public function test_build_value_value_null() { |
| 256 | global $wpdb; |
227 | 257 | $q = new WP_Date_Query( array() ); |
228 | 258 | |
229 | 259 | $this->assertFalse( $q->build_value( 'foo', null ) ); |
… |
… |
class Tests_WP_Date_Query extends WP_UnitTestCase { |
571 | 601 | // varying precision on different PHP installations |
572 | 602 | $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $found ); |
573 | 603 | } |
| 604 | |
| 605 | /** Helpers **********************************************************/ |
| 606 | |
| 607 | public function date_query_valid_columns_callback( $columns ) { |
| 608 | $columns[] = 'my_custom_column'; |
| 609 | return $columns; |
| 610 | } |
574 | 611 | } |
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 { |
590 | 590 | } |
591 | 591 | |
592 | 592 | public function test_date_params_monthnum_m_duplicate() { |
| 593 | global $wpdb; |
| 594 | |
593 | 595 | $posts = $this->_get_query_result( array( |
594 | 596 | 'date_query' => array( |
595 | 597 | 'month' => 5, |
… |
… |
class Tests_Query_DateQuery extends WP_UnitTestCase { |
607 | 609 | |
608 | 610 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
609 | 611 | |
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 ); |
611 | 613 | |
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 ); |
613 | 615 | } |
614 | 616 | |
615 | 617 | public function test_date_params_week_w_duplicate() { |
| 618 | global $wpdb; |
| 619 | |
616 | 620 | $posts = $this->_get_query_result( array( |
617 | 621 | 'date_query' => array( |
618 | 622 | 'week' => 21, |
… |
… |
class Tests_Query_DateQuery extends WP_UnitTestCase { |
629 | 633 | |
630 | 634 | $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); |
631 | 635 | |
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' ); |
633 | 669 | |
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' ) ); |
635 | 671 | } |
636 | | } |
637 | | No newline at end of file |
| 672 | } |