Source
File: wp-includes/class-wp-date-query.php
public function validate_date_values( $date_query = array() ) {
if ( empty( $date_query ) ) {
return false;
}
$valid = true;
if ( array_key_exists( 'before', $date_query ) && is_array( $date_query['before'] ) ) {
$valid = $this->validate_date_values( $date_query['before'] );
}
if ( array_key_exists( 'after', $date_query ) && is_array( $date_query['after'] ) ) {
$valid = $this->validate_date_values( $date_query['after'] );
}
$min_max_checks = array();
if ( array_key_exists( 'year', $date_query ) ) {
if ( is_array( $date_query['year'] ) ) {
$_year = reset( $date_query['year'] );
} else {
$_year = $date_query['year'];
}
$max_days_of_year = gmdate( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1;
} else {
$max_days_of_year = 366;
}
$min_max_checks['dayofyear'] = array(
'min' => 1,
'max' => $max_days_of_year,
);
$min_max_checks['dayofweek'] = array(
'min' => 1,
'max' => 7,
);
$min_max_checks['dayofweek_iso'] = array(
'min' => 1,
'max' => 7,
);
$min_max_checks['month'] = array(
'min' => 1,
'max' => 12,
);
if ( isset( $_year ) ) {
$week_count = gmdate( 'W', mktime( 0, 0, 0, 12, 28, $_year ) );
} else {
$week_count = 53;
}
$min_max_checks['week'] = array(
'min' => 1,
'max' => $week_count,
);
$min_max_checks['day'] = array(
'min' => 1,
'max' => 31,
);
$min_max_checks['hour'] = array(
'min' => 0,
'max' => 23,
);
$min_max_checks['minute'] = array(
'min' => 0,
'max' => 59,
);
$min_max_checks['second'] = array(
'min' => 0,
'max' => 59,
);
foreach ( $min_max_checks as $key => $check ) {
if ( ! array_key_exists( $key, $date_query ) ) {
continue;
}
foreach ( (array) $date_query[ $key ] as $_value ) {
$is_between = $_value >= $check['min'] && $_value <= $check['max'];
if ( ! is_numeric( $_value ) || ! $is_between ) {
$error = sprintf(
__( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ),
'<code>' . esc_html( $_value ) . '</code>',
'<code>' . esc_html( $key ) . '</code>',
'<code>' . esc_html( $check['min'] ) . '</code>',
'<code>' . esc_html( $check['max'] ) . '</code>'
);
_doing_it_wrong( __CLASS__, $error, '4.1.0' );
$valid = false;
}
}
}
if ( ! $valid ) {
return $valid;
}
$day_month_year_error_msg = '';
$day_exists = array_key_exists( 'day', $date_query ) && is_numeric( $date_query['day'] );
$month_exists = array_key_exists( 'month', $date_query ) && is_numeric( $date_query['month'] );
$year_exists = array_key_exists( 'year', $date_query ) && is_numeric( $date_query['year'] );
if ( $day_exists && $month_exists && $year_exists ) {
if ( ! wp_checkdate( $date_query['month'], $date_query['day'], $date_query['year'], sprintf( '%s-%s-%s', $date_query['year'], $date_query['month'], $date_query['day'] ) ) ) {
$day_month_year_error_msg = sprintf(
__( 'The following values do not describe a valid date: year %1$s, month %2$s, day %3$s.' ),
'<code>' . esc_html( $date_query['year'] ) . '</code>',
'<code>' . esc_html( $date_query['month'] ) . '</code>',
'<code>' . esc_html( $date_query['day'] ) . '</code>'
);
$valid = false;
}
} elseif ( $day_exists && $month_exists ) {
if ( ! wp_checkdate( $date_query['month'], $date_query['day'], 2012, sprintf( '2012-%s-%s', $date_query['month'], $date_query['day'] ) ) ) {
$day_month_year_error_msg = sprintf(
__( 'The following values do not describe a valid date: month %1$s, day %2$s.' ),
'<code>' . esc_html( $date_query['month'] ) . '</code>',
'<code>' . esc_html( $date_query['day'] ) . '</code>'
);
$valid = false;
}
}
if ( ! empty( $day_month_year_error_msg ) ) {
_doing_it_wrong( __CLASS__, $day_month_year_error_msg, '4.1.0' );
}
return $valid;
}