[wp-trac] [WordPress Trac] #25834: WP_Date_Query not allowed values possible

WordPress Trac noreply at wordpress.org
Sun Feb 23 10:49:44 UTC 2014


#25834: WP_Date_Query not allowed values possible
------------------------------------------+------------------
 Reporter:  ChriCo                        |       Owner:
     Type:  defect (bug)                  |      Status:  new
 Priority:  normal                        |   Milestone:  3.9
Component:  Query                         |     Version:  3.7
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+------------------

Comment (by ChriCo):

 >What should happen when you pass an out of range date value to a WP_Query
 call?

 As Toscho ([https://core.trac.wordpress.org/ticket/25834#comment:9])
 already said, returning or giving at least an Error-Message (could also
 trigger_error() to show a warning when WP_DEBUG is true).

 I'm a little bit disappointed of your recommendation. It's harder to debug
 an unexpected result and not validated parameters, than debugging a
 meaningful error-message thrown by WP_Error or trigger_error().

 Basic validation could be something like:

 {{{
 // checking the days of year
 if( array_key_exists( 'dayofyear', $date_query ) ){
         $year               = array_key_exists( 'year', $date_query ) ?
 $date_query[ 'year' ] : date( 'Y' );
         $max_days_in_year   = date( "z", mktime( 0, 0, 0, 12, 31, $year )
 ) + 1;
         $filter_args = array(
                 'options' => array(
                         'min_range' => 1,
                         'max_range' => $max_days_in_year
                 )
         );
         $valid = filter_var(
                 $date_query[ 'dayofyear' ],
                 FILTER_VALIDATE_INT,
                 $filter_args
         );
 }

 // checking day of week
 if( array_key_exists( 'dayofweek', $date_query ) ){
         $filter_args = array(
                 'options' => array(
                         'min_range' => 1,
                         'max_range' => 6
                 )
         );
         $valid = filter_var(
                 $date_query[ 'dayofweek' ],
                 FILTER_VALIDATE_INT,
                 $filter_args
         );
 }
 }}}

 And when switching min-Support of PHP to 5.3 you can also change the whole
 logic to:

 {{{
 function check_date_value_by_format( $date_value, $format ) {
         $date_time = DateTime::createFromFormat( $format, $date_value);
         $is_valid = $date_time && $date_time->format( $format ) ==
 $date_value;
         if( !$is_valid && WP_DEBUG ) {
                 $msg = sprintf(
                         'The entered date_value <code>%s</code> is not
 valid. Excepted <code>%s</code> by validating with format
 <code>%s</code>.'
                         $date_value,
                         $date_time->( $format ),
                         $format
                 );
                 trigger_error( $msg, E_USER_ERROR );
         }
         return $is_valid;
 }

 // week
 $format = 'w';
 check_date_value_by_format( 53, $format ); // invalid
 check_date_value_by_format( 52, $format ); // valid

 // second
 $format = 's';
 check_date_value_by_format( 60, $format ); // invalid
 check_date_value_by_format( 59, $format ); // valid

 // and so on..
 }}}


 The only problem i see is, validating the day-month-year-combinations
 (maybe with the PHP check_date()-Function). There you have following
 scenarios:

 1. validating day + month + year ( month: min 1 to max 12 && day: min (of
 month of year) + max (of month of year) ).
 2. validating day + month ( month: min 1 to max 12 && day: min (of month)
 + max (of month) )
 3. validating month + year ( month: min (of year) to max (of year) )
 3. validating month ( min 1 to max 12 )
 4. validating day ( min 1 to max 31 )

 But what happens on 2. when we have following values:

 {{{
 $day = 29;
 $month = 2
 }}}

 In which year? Or should we just check through cal_days_in_month(); of a
 leap-year to verify a valid max ( 29 or 30 or 31 )?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/25834#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list