Make WordPress Core


Ignore:
Timestamp:
02/10/2015 01:13:38 AM (10 years ago)
Author:
dd32
Message:

Support array values in WP_Date_Query::validate_date_values().

Introduced in [29925], validate_date_values() throws _doing_it_wrong()
notices when values passed as part of a WP_Date_Query do not reflect actual
dates. However, the validation did not account properly for the case where an
array of multiple values is passed, as when doing IN or BETWEEN queries.

Props dlh.
Merges [31179] to the 4.1 branch.
Fixes #31001.

Location:
branches/4.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/src/wp-includes/date.php

    r31377 r31396  
    306306        // Days per year.
    307307        if ( array_key_exists( 'year', $date_query ) ) {
    308             // If a year exists in the date query, we can use it to get the days.
    309             $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $date_query['year'] ) ) + 1;
     308            /*
     309             * If a year exists in the date query, we can use it to get the days.
     310             * If multiple years are provided (as in a BETWEEN), use the first one.
     311             */
     312            if ( is_array( $date_query['year'] ) ) {
     313                $_year = reset( $date_query['year'] );
     314            } else {
     315                $_year = $date_query['year'];
     316            }
     317
     318            $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1;
    310319        } else {
    311320            // otherwise we use the max of 366 (leap-year)
     
    337346
    338347        // Weeks per year.
    339         if ( array_key_exists( 'year', $date_query ) ) {
     348        if ( isset( $_year ) ) {
    340349            // If we have a specific year, use it to calculate number of weeks.
    341350            $date = new DateTime();
    342             $date->setISODate( $date_query['year'], 53 );
     351            $date->setISODate( $_year, 53 );
    343352            $week_count = $date->format( "W" ) === "53" ? 53 : 52;
    344353
     
    383392            }
    384393
    385             $is_between = $date_query[ $key ] >= $check['min'] && $date_query[ $key ] <= $check['max'];
    386 
    387             if ( ! is_numeric( $date_query[ $key ] ) || ! $is_between ) {
    388 
    389                 $error = sprintf(
    390                     /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */
    391                     __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ),
    392                     '<code>' . esc_html( $date_query[ $key ] ) . '</code>',
    393                     '<code>' . esc_html( $key ) . '</code>',
    394                     '<code>' . esc_html( $check['min'] ) . '</code>',
    395                     '<code>' . esc_html( $check['max'] ) . '</code>'
    396                 );
    397 
    398                 _doing_it_wrong( __CLASS__, $error, '4.1.0' );
    399 
    400                 $valid = false;
     394            // Throw a notice for each failing value.
     395            $is_between = true;
     396            foreach ( (array) $date_query[ $key ] as $_value ) {
     397                $is_between = $_value >= $check['min'] && $_value <= $check['max'];
     398
     399                if ( ! is_numeric( $_value ) || ! $is_between ) {
     400                    $error = sprintf(
     401                        /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */
     402                        __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ),
     403                        '<code>' . esc_html( $_value ) . '</code>',
     404                        '<code>' . esc_html( $key ) . '</code>',
     405                        '<code>' . esc_html( $check['min'] ) . '</code>',
     406                        '<code>' . esc_html( $check['max'] ) . '</code>'
     407                    );
     408
     409                    _doing_it_wrong( __CLASS__, $error, '4.1.0' );
     410
     411                    $valid = false;
     412                }
    401413            }
    402414        }
Note: See TracChangeset for help on using the changeset viewer.