Make WordPress Core

Changeset 30142


Ignore:
Timestamp:
11/01/2014 03:23:15 AM (10 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/date.php

    r29938 r30142  
    6161     * @var array
    6262     */
    63     public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'hour', 'minute', 'second' );
     63    public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' );
    6464
    6565    /**
     
    6868     * @since 3.7.0
    6969     * @since 4.0.0 The $inclusive logic was updated to include all times within the date range.
     70     * @since 4.1.0 Introduced 'dayofweek_iso' time type parameter.
    7071     * @access public
    7172     *
     
    117118     *             @type int          $dayofyear Optional. The day number of the year. Default empty. Accepts numbers 1-366.
    118119     *             @type int          $day       Optional. The day of the month. Default empty. Accepts numbers 1-31.
    119      *             @type int          $dayofweek Optional. The day number of the week. Default empty. Accepts numbers 1-7.
     120     *             @type int          $dayofweek Optional. The day number of the week. Default empty. Accepts numbers 1-7 (1 is Sunday).
     121     *             @type int          $dayofweek_iso Optional. The day number of the week (ISO). Default empty.
     122     *                       Accepts numbers 1-7 (1 is Monday).
    120123     *             @type int          $hour      Optional. The hour of the day. Default empty. Accepts numbers 0-23.
    121124     *             @type int          $minute    Optional. The minute of the hour. Default empty. Accepts numbers 0-60.
     
    310313        // Days per week.
    311314        $min_max_checks['dayofweek'] = array(
     315            'min' => 1,
     316            'max' => 7
     317        );
     318
     319        // Days per week.
     320        $min_max_checks['dayofweek_iso'] = array(
    312321            'min' => 1,
    313322            'max' => 7
     
    728737            $where_parts[] = "DAYOFWEEK( $column ) $compare $value";
    729738
     739        if ( isset( $query['dayofweek_iso'] ) && $value = $this->build_value( $compare, $query['dayofweek_iso'] ) )
     740            $where_parts[] = "WEEKDAY( $column ) + 1 $compare $value";
     741
    730742        if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) {
    731743            // Avoid notices.
  • trunk/tests/phpunit/tests/date/query.php

    r29933 r30142  
    926926
    927927    /**
     928     * @ticket 28063
     929     * @expectedIncorrectUsage WP_Date_Query
     930     */
     931    public function test_validate_date_values_day_of_week_iso() {
     932        // Valid values.
     933        $days_of_week = range( 1, 7 );
     934        foreach ( $days_of_week as $day_of_week ) {
     935            $this->assertTrue( $this->q->validate_date_values( array( 'dayofweek_iso' => $day_of_week ) ) );
     936        }
     937
     938        // Invalid values.
     939        $days_of_week = array( -1, 0, 8 );
     940        foreach ( $days_of_week as $day_of_week ) {
     941            $this->assertFalse( $this->q->validate_date_values( array( 'dayofweek_iso' => $day_of_week ) ) );
     942        }
     943    }
     944
     945    /**
    928946     * @ticket 25834
    929947     * @expectedIncorrectUsage WP_Date_Query
  • trunk/tests/phpunit/tests/query/dateQuery.php

    r29993 r30142  
    629629                array(
    630630                    'dayofweek' => 3,
     631                ),
     632            ),
     633        ) );
     634
     635        $this->assertEquals( array( $p1 ), wp_list_pluck( $posts, 'ID' ) );
     636    }
     637
     638    /**
     639     * @ticket 28063
     640     */
     641    public function test_date_query_dayofweek_iso() {
     642        $p1 = $this->factory->post->create( array( 'post_date' => '2014-10-31 10:42:29', ) );
     643        $p2 = $this->factory->post->create( array( 'post_date' => '2014-10-30 10:42:29', ) );
     644
     645        $posts = $this->_get_query_result( array(
     646            'date_query' => array(
     647                array(
     648                    'dayofweek_iso' => 5,
    631649                ),
    632650            ),
Note: See TracChangeset for help on using the changeset viewer.