diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index 218fea26a0..5fade26fe6 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -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 ) { diff --git a/tests/phpunit/tests/date/query.php b/tests/phpunit/tests/date/query.php index aa9197d3ac..82b6ffaffa 100644 --- a/tests/phpunit/tests/date/query.php +++ b/tests/phpunit/tests/date/query.php @@ -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 diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php index afbd38907c..32cc18f8dc 100644 --- a/tests/phpunit/tests/query/dateQuery.php +++ b/tests/phpunit/tests/query/dateQuery.php @@ -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', ) );