From 3f033dc73dcdf997b4d1c14e9135d4817e07a4cc Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 14 Jan 2015 16:51:24 +0000 Subject: [PATCH] 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. Fixes #31001 for trunk. Built from https://develop.svn.wordpress.org/trunk@31179 git-svn-id: http://core.svn.wordpress.org/trunk@31160 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/date.php | 46 ++++++++++++++++++++++++++--------------- wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/wp-includes/date.php b/wp-includes/date.php index c023574ed6..d2c8f6971d 100644 --- a/wp-includes/date.php +++ b/wp-includes/date.php @@ -304,8 +304,17 @@ class WP_Date_Query { // Days per year. if ( array_key_exists( 'year', $date_query ) ) { - // If a year exists in the date query, we can use it to get the days. - $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $date_query['year'] ) ) + 1; + /* + * If a year exists in the date query, we can use it to get the days. + * If multiple years are provided (as in a BETWEEN), use the first one. + */ + if ( is_array( $date_query['year'] ) ) { + $_year = reset( $date_query['year'] ); + } else { + $_year = $date_query['year']; + } + + $max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1; } else { // otherwise we use the max of 366 (leap-year) $max_days_of_year = 366; @@ -335,10 +344,10 @@ class WP_Date_Query { ); // Weeks per year. - if ( array_key_exists( 'year', $date_query ) ) { + if ( isset( $_year ) ) { // If we have a specific year, use it to calculate number of weeks. $date = new DateTime(); - $date->setISODate( $date_query['year'], 53 ); + $date->setISODate( $_year, 53 ); $week_count = $date->format( "W" ) === "53" ? 53 : 52; } else { @@ -381,22 +390,25 @@ class WP_Date_Query { continue; } - $is_between = $date_query[ $key ] >= $check['min'] && $date_query[ $key ] <= $check['max']; + // Throw a notice for each failing value. + $is_between = true; + foreach ( (array) $date_query[ $key ] as $_value ) { + $is_between = $_value >= $check['min'] && $_value <= $check['max']; - if ( ! $is_between ) { + if ( ! $is_between ) { + $error = sprintf( + /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */ + __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ), + '' . esc_html( $_value ) . '', + '' . esc_html( $key ) . '', + '' . esc_html( $check['min'] ) . '', + '' . esc_html( $check['max'] ) . '' + ); - $error = sprintf( - /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */ - __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ), - '' . esc_html( $date_query[ $key ] ) . '', - '' . esc_html( $key ) . '', - '' . esc_html( $check['min'] ) . '', - '' . esc_html( $check['max'] ) . '' - ); + _doing_it_wrong( __CLASS__, $error, '4.1.0' ); - _doing_it_wrong( __CLASS__, $error, '4.1.0' ); - - $valid = false; + $valid = false; + } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 3615ba17b1..fe352badd3 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-alpha-31178'; +$wp_version = '4.2-alpha-31179'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.