Introduced dayofweek_iso time param for WP_Date_Query.

The initial `dayofweek` param sets day 1 to Sunday. This is out of step with
ISO standards, which calls Monday day 1. To maintain backward compatibility
with the existing parameter, we introduce the new `dayofweek_iso` for the
new, more compliant param.

Props mboynes.
Fixes #28063.

git-svn-id: https://develop.svn.wordpress.org/trunk@30142 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-01 03:23:15 +00:00
parent d6b7b7aa41
commit 7c64b3b3dd
3 changed files with 50 additions and 2 deletions

View File

@ -60,13 +60,14 @@ class WP_Date_Query {
* @access public
* @var array
*/
public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'hour', 'minute', 'second' );
public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' );
/**
* Constructor.
*
* @since 3.7.0
* @since 4.0.0 The $inclusive logic was updated to include all times within the date range.
* @since 4.1.0 Introduced 'dayofweek_iso' time type parameter.
* @access public
*
* @param array $date_query {
@ -116,7 +117,9 @@ class WP_Date_Query {
* @type int $week Optional. The week number of the year. Default empty. Accepts numbers 0-53.
* @type int $dayofyear Optional. The day number of the year. Default empty. Accepts numbers 1-366.
* @type int $day Optional. The day of the month. Default empty. Accepts numbers 1-31.
* @type int $dayofweek Optional. The day number of the week. Default empty. Accepts numbers 1-7.
* @type int $dayofweek Optional. The day number of the week. Default empty. Accepts numbers 1-7 (1 is Sunday).
* @type int $dayofweek_iso Optional. The day number of the week (ISO). Default empty.
* Accepts numbers 1-7 (1 is Monday).
* @type int $hour Optional. The hour of the day. Default empty. Accepts numbers 0-23.
* @type int $minute Optional. The minute of the hour. Default empty. Accepts numbers 0-60.
* @type int $second Optional. The second of the minute. Default empty. Accepts numbers 0-60.
@ -313,6 +316,12 @@ class WP_Date_Query {
'max' => 7
);
// Days per week.
$min_max_checks['dayofweek_iso'] = array(
'min' => 1,
'max' => 7
);
// Months per year.
$min_max_checks['month'] = array(
'min' => 1,
@ -727,6 +736,9 @@ class WP_Date_Query {
if ( isset( $query['dayofweek'] ) && $value = $this->build_value( $compare, $query['dayofweek'] ) )
$where_parts[] = "DAYOFWEEK( $column ) $compare $value";
if ( isset( $query['dayofweek_iso'] ) && $value = $this->build_value( $compare, $query['dayofweek_iso'] ) )
$where_parts[] = "WEEKDAY( $column ) + 1 $compare $value";
if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) {
// Avoid notices.
foreach ( array( 'hour', 'minute', 'second' ) as $unit ) {

View File

@ -924,6 +924,24 @@ class Tests_WP_Date_Query extends WP_UnitTestCase {
}
}
/**
* @ticket 28063
* @expectedIncorrectUsage WP_Date_Query
*/
public function test_validate_date_values_day_of_week_iso() {
// Valid values.
$days_of_week = range( 1, 7 );
foreach ( $days_of_week as $day_of_week ) {
$this->assertTrue( $this->q->validate_date_values( array( 'dayofweek_iso' => $day_of_week ) ) );
}
// Invalid values.
$days_of_week = array( -1, 0, 8 );
foreach ( $days_of_week as $day_of_week ) {
$this->assertFalse( $this->q->validate_date_values( array( 'dayofweek_iso' => $day_of_week ) ) );
}
}
/**
* @ticket 25834
* @expectedIncorrectUsage WP_Date_Query

View File

@ -635,6 +635,24 @@ class Tests_Query_DateQuery extends WP_UnitTestCase {
$this->assertEquals( array( $p1 ), wp_list_pluck( $posts, 'ID' ) );
}
/**
* @ticket 28063
*/
public function test_date_query_dayofweek_iso() {
$p1 = $this->factory->post->create( array( 'post_date' => '2014-10-31 10:42:29', ) );
$p2 = $this->factory->post->create( array( 'post_date' => '2014-10-30 10:42:29', ) );
$posts = $this->_get_query_result( array(
'date_query' => array(
array(
'dayofweek_iso' => 5,
),
),
) );
$this->assertEquals( array( $p1 ), wp_list_pluck( $posts, 'ID' ) );
}
public function test_date_query_hour() {
$p1 = $this->factory->post->create( array( 'post_date' => '2014-10-21 13:42:29', ) );
$p2 = $this->factory->post->create( array( 'post_date' => '2014-10-21 12:42:29', ) );