Make WordPress Core

Ticket #31083: 31083.patch

File 31083.patch, 3.4 KB (added by ChriCo, 10 years ago)

added new method for validating before/after, added time-string validation, unit tests

  • tests/phpunit/tests/date/query.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    645645                                'day'  => 8,
    646646                                'year' => 2014,
    647647                        ),
     648                        '21 June 1987',
    648649                );
    649650
    650651                foreach ( $valid_args as $args ) {
     
    669670                        array(
    670671                                'week' => 54,
    671672                        ),
     673                        'I am a valid date string!',
     674                        false,
     675                        true,
     676                        3.14159265359
     677
    672678                );
    673679
    674680                foreach ( $invalid_args as $args ) {
  • src/wp-includes/date.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    291291                 * validation routine continue to be sure that all invalid
    292292                 * values generate errors too.
    293293                 */
    294                 if ( array_key_exists( 'before', $date_query ) && is_array( $date_query['before'] ) ){
    295                         $valid = $this->validate_date_values( $date_query['before'] );
     294                if ( array_key_exists( 'before', $date_query ) ) {
     295                        $valid = $this->validate_before_after_args( $date_query[ 'before' ], 'before' );
    296296                }
    297297
    298                 if ( array_key_exists( 'after', $date_query ) && is_array( $date_query['after'] ) ){
    299                         $valid = $this->validate_date_values( $date_query['after'] );
     298                if ( array_key_exists( 'after', $date_query ) ) {
     299                        $valid = $this->validate_before_after_args( $date_query[ 'after' ], 'after' );
    300300                }
    301301
    302302                // Array containing all min-max checks.
     
    394394                        foreach ( (array) $date_query[ $key ] as $_value ) {
    395395                                $is_between = $_value >= $check['min'] && $_value <= $check['max'];
    396396
    397                                 if ( ! is_numeric( $_value ) || ! $is_between ) {
     397                                if ( ! $is_between ) {
    398398                                        $error = sprintf(
    399                                                 /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */
     399                                        /* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */
    400400                                                __( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ),
    401401                                                '<code>' . esc_html( $_value ) . '</code>',
    402402                                                '<code>' . esc_html( $key ) . '</code>',
     
    458458                }
    459459
    460460                return $valid;
     461        }
     462
     463        /**
     464         * Validates the before/after date-query args.
     465         *
     466         * @since 4.1.1
     467         *
     468         * @param string|array $args    the date-query args for before/after
     469         * @param string $type          the type "before" or "after"
     470         *
     471         * @return bool true|false
     472         */
     473        public function validate_before_after_args( $args, $type ) {
     474
     475                if ( is_array( $args ) ) {
     476                        return $this->validate_date_values( $args );
     477                }
     478
     479                if ( is_string( $args ) ) {
     480                        $valid = ! ! strtotime( $args );
     481
     482                        if ( ! $valid ) {
     483                                $error = sprintf(
     484                                        _x( 'Invalid time string %1$s for %2$s.', '1: time-string, 2: "before" or "after"' ),
     485                                        '<code>' . $args . '</code>',
     486                                        $type
     487                                );
     488                                _doing_it_wrong( __CLASS__, $error, '4.1.1' );
     489                        }
     490
     491                        return $valid;
     492                }
     493
     494                $error = sprintf(
     495                        _x( 'Invalid type %1$s for %2$s. Excepted string or array', '1: argument type, 2: "before" or "after"' ),
     496                        '<code>' . gettype( $args ) . '</code>',
     497                        '<strong>' . $type . '</strong>'
     498                );
     499
     500                _doing_it_wrong( __CLASS__, $error, '4.1.1' );
     501
     502                return false;
    461503        }
    462504