| 370 | // Prevent invalid date queries. |
| 371 | |
| 372 | // Array containing all min-max checks. |
| 373 | $min_max_checks = array(); |
| 374 | |
| 375 | // Months per year. |
| 376 | $min_max_checks['monthnum'] = array( |
| 377 | 'min' => 1, |
| 378 | 'max' => 12, |
| 379 | ); |
| 380 | |
| 381 | // Weeks per year. |
| 382 | if ( isset( $this->query_vars['year'] ) ) { |
| 383 | /* |
| 384 | * If we have a specific year, use it to calculate number of weeks. |
| 385 | * Note: the number of weeks in a year is the date in which Dec 28 appears. |
| 386 | */ |
| 387 | $week_count = gmdate( 'W', mktime( 0, 0, 0, 12, 28, absint($this->query_vars['year']) ) ); |
| 388 | |
| 389 | } else { |
| 390 | // Otherwise set the week-count to a maximum of 53. |
| 391 | $week_count = 53; |
| 392 | } |
| 393 | |
| 394 | $min_max_checks['w'] = array( |
| 395 | 'min' => 1, |
| 396 | 'max' => $week_count, |
| 397 | ); |
| 398 | |
| 399 | // Days per month. |
| 400 | $min_max_checks['day'] = array( |
| 401 | 'min' => 1, |
| 402 | 'max' => 31, |
| 403 | ); |
| 404 | |
| 405 | // Hours per day. |
| 406 | $min_max_checks['hour'] = array( |
| 407 | 'min' => 0, |
| 408 | 'max' => 23, |
| 409 | ); |
| 410 | |
| 411 | // Minutes per hour. |
| 412 | $min_max_checks['minute'] = array( |
| 413 | 'min' => 0, |
| 414 | 'max' => 59, |
| 415 | ); |
| 416 | |
| 417 | // Seconds per minute. |
| 418 | $min_max_checks['second'] = array( |
| 419 | 'min' => 0, |
| 420 | 'max' => 59, |
| 421 | ); |
| 422 | |
| 423 | // Concatenate and unset each invalid value. |
| 424 | |
| 425 | foreach ( $min_max_checks as $key => $check ) { |
| 426 | if ( |
| 427 | ! array_key_exists( $key, $this->query_vars ) |
| 428 | ) { |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | $is_between = $this->query_vars[ $key ] >= $check['min'] && $this->query_vars <= $check['max']; |
| 433 | |
| 434 | if ( ! is_numeric( $this->query_vars[ $key ] ) || ! $is_between ) { |
| 435 | unset($this->query_vars[ $key ]); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | $day_month_year_error_msg = ''; |
| 440 | |
| 441 | $day_exists = array_key_exists( 'day', $this->query_vars ) && is_numeric( $this->query_vars['day'] ); |
| 442 | $month_exists = array_key_exists( 'monthnum', $this->query_vars ) && is_numeric( $this->query_vars['monthnum'] ); |
| 443 | $year_exists = array_key_exists( 'year', $this->query_vars ) && is_numeric( $this->query_vars['year'] ); |
| 444 | |
| 445 | if ( $day_exists && $month_exists && $year_exists ) { |
| 446 | // 1. Checking day, month, year combination. |
| 447 | if ( ! wp_checkdate( $this->query_vars['monthnum'], $this->query_vars['day'], $this->query_vars['year'], sprintf( '%s-%s-%s', $this->query_vars['year'], $this->query_vars['monthnum'], $this->query_vars['day'] ) ) ) { |
| 448 | unset($this->query_vars['day']); |
| 449 | unset($this->query_vars['monthnum']); |
| 450 | unset($this->query_vars['year']); |
| 451 | } |
| 452 | } elseif ( $day_exists && $month_exists ) { |
| 453 | /* |
| 454 | * 2. checking day, month combination |
| 455 | * We use 2012 because, as a leap year, it's the most permissive. |
| 456 | */ |
| 457 | if ( ! wp_checkdate( $this->query_vars['monthnum'], $this->query_vars['day'], 2012, sprintf( '2012-%s-%s', $this->query_vars['monthnum'], $this->query_vars['day'] ) ) ) { |
| 458 | unset($this->query_vars['day']); |
| 459 | unset($this->query_vars['monthnum']); |
| 460 | } |
| 461 | } |
| 462 | |