WP_Date_Query: The inclusive logic should include all times within the date range.

props mboynes, oso96_2000, DrewAPicture.
fixes #26653.

git-svn-id: https://develop.svn.wordpress.org/trunk@28935 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2014-07-01 01:17:39 +00:00
parent 416e5bcb96
commit 8b0ef058f3
2 changed files with 41 additions and 3 deletions

View File

@ -48,6 +48,9 @@ class WP_Date_Query {
/**
* Constructor.
*
* @since 3.7.0
* @since 4.0.0 The $inclusive logic was updated to include all times within the date range.
*
* @param array $date_query {
* One or more associative arrays of date query parameters.
*
@ -235,19 +238,23 @@ class WP_Date_Query {
$compare = $this->get_compare( $query );
$inclusive = ! empty( $query['inclusive'] );
// Assign greater- and less-than values.
$lt = '<';
$gt = '>';
if ( ! empty( $query['inclusive'] ) ) {
if ( $inclusive ) {
$lt .= '=';
$gt .= '=';
}
// Range queries
if ( ! empty( $query['after'] ) )
$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], true ) );
$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
if ( ! empty( $query['before'] ) )
$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], false ) );
$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
// Specific value queries

View File

@ -254,6 +254,37 @@ class Tests_Query_DateQuery extends WP_UnitTestCase {
$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
}
/**
* @ticket 26653
*/
public function test_date_query_inclusive_between_dates() {
$posts = $this->_get_query_result( array(
'date_query' => array(
'after' => array(
'year' => 2007,
'month' => 1
),
'before' => array(
'year' => 2008,
'month' => 12
),
'inclusive' => true
),
) );
$expected_dates = array(
'2007-01-22 03:49:21',
'2007-05-16 17:32:22',
'2007-09-24 07:17:23',
'2008-03-29 09:04:25',
'2008-07-15 11:32:26',
'2008-12-10 13:06:27',
);
$this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) );
}
public function test_date_query_year_expecting_results() {
$posts = $this->_get_query_result( array(
'date_query' => array(