Make WordPress Core

Ticket #31001: 31001.diff

File 31001.diff, 4.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/date.php

    diff --git src/wp-includes/date.php src/wp-includes/date.php
    index c023574..3428bc2 100644
    class WP_Date_Query { 
    304304
    305305                // Days per year.
    306306                if ( array_key_exists( 'year', $date_query ) ) {
    307                         // If a year exists in the date query, we can use it to get the days.
    308                         $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $date_query['year'] ) ) + 1;
     307                        /*
     308                         * If a year exists in the date query, we can use it to get the days.
     309                         * If multiple years are provided (as in a BETWEEN), use the first one.
     310                         */
     311                        if ( is_array( $date_query['year'] ) ) {
     312                                $_year = reset( $date_query['year'] );
     313                        } else {
     314                                $_year = $date_query['year'];
     315                        }
     316
     317                        $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1;
    309318                } else {
    310319                        // otherwise we use the max of 366 (leap-year)
    311320                        $max_days_of_year = 366;
    class WP_Date_Query { 
    335344                );
    336345
    337346                // Weeks per year.
    338                 if ( array_key_exists( 'year', $date_query ) ) {
     347                if ( isset( $_year ) ) {
    339348                        // If we have a specific year, use it to calculate number of weeks.
    340349                        $date = new DateTime();
    341                         $date->setISODate( $date_query['year'], 53 );
     350                        $date->setISODate( $_year, 53 );
    342351                        $week_count = $date->format( "W" ) === "53" ? 53 : 52;
    343352
    344353                } else {
    class WP_Date_Query { 
    381390                                continue;
    382391                        }
    383392
    384                         $is_between = $date_query[ $key ] >= $check['min'] && $date_query[ $key ] <= $check['max'];
     393                        // If multiple values are passed, and one fails, throw a notice.
     394                        $is_between = true;
     395                        foreach ( (array) $date_query[ $key ] as $_value ) {
     396                                $is_between = $_value >= $check['min'] && $_value <= $check['max'];
     397
     398                                if ( ! $is_between ) {
     399                                        break;
     400                                }
     401                        }
     402
    385403
    386404                        if ( ! $is_between ) {
    387405
    388406                                $error = sprintf(
    389407                                        /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */
    390408                                        __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ),
    391                                         '<code>' . esc_html( $date_query[ $key ] ) . '</code>',
     409                                        '<code>' . esc_html( $_value ) . '</code>',
    392410                                        '<code>' . esc_html( $key ) . '</code>',
    393411                                        '<code>' . esc_html( $check['min'] ) . '</code>',
    394412                                        '<code>' . esc_html( $check['max'] ) . '</code>'
  • tests/phpunit/tests/date/query.php

    diff --git tests/phpunit/tests/date/query.php tests/phpunit/tests/date/query.php
    index 82b6ffa..2169d88 100644
    class Tests_WP_Date_Query extends WP_UnitTestCase { 
    960960                }
    961961        }
    962962
     963        /**
     964         * @ticket 31001
     965         */
     966        public function test_validate_date_values_should_process_array_value_for_year() {
     967                $p1 = $this->factory->post->create( array( 'post_date' => '2015-01-12' ) );
     968                $p2 = $this->factory->post->create( array( 'post_date' => '2013-01-12' ) );
     969
     970                $q = new WP_Query( array(
     971                        'date_query' => array(
     972                                array(
     973                                        'compare' => 'BETWEEN',
     974                                        'year' => array( 2012, 2014 ),
     975                                ),
     976                        ),
     977                        'fields' => 'ids',
     978                ) );
     979
     980                $this->assertEquals( array( $p2 ), $q->posts );
     981        }
     982
     983        /**
     984         * @ticket 31001
     985         */
     986        public function test_validate_date_values_should_process_array_value_for_day() {
     987                $p1 = $this->factory->post->create( array( 'post_date' => '2015-01-12' ) );
     988                $p2 = $this->factory->post->create( array( 'post_date' => '2015-01-10' ) );
     989
     990                $q = new WP_Query( array(
     991                        'date_query' => array(
     992                                array(
     993                                        'compare' => 'BETWEEN',
     994                                        'day' => array( 9, 11 ),
     995                                ),
     996                        ),
     997                        'fields' => 'ids',
     998                ) );
     999
     1000                $this->assertEquals( array( $p2 ), $q->posts );
     1001        }
     1002
     1003        /**
     1004         * @ticket 31001
     1005         * @expectedIncorrectUsage WP_Date_Query
     1006         */
     1007        public function test_validate_date_values_should_process_array_value_for_day_when_values_are_invalid() {
     1008                $p1 = $this->factory->post->create( array( 'post_date' => '2015-01-12' ) );
     1009                $p2 = $this->factory->post->create( array( 'post_date' => '2015-01-10' ) );
     1010
     1011                $q = new WP_Query( array(
     1012                        'date_query' => array(
     1013                                array(
     1014                                        'compare' => 'BETWEEN',
     1015                                        'day' => array( 9, 32 ),
     1016                                ),
     1017                        ),
     1018                        'fields' => 'ids',
     1019                ) );
     1020
     1021                // MySQL ignores the invalid clause.
     1022                $this->assertEquals( array( $p1, $p2 ), $q->posts );
     1023        }
     1024
    9631025        /** Helpers **********************************************************/
    9641026
    9651027        public function date_query_valid_columns_callback( $columns ) {