Make WordPress Core

Ticket #42883: 42883.diff

File 42883.diff, 2.2 KB (added by Mte90, 7 years ago)

first implementation

  • src/wp-includes/date.php

    diff --git src/wp-includes/date.php src/wp-includes/date.php
    index ef7cb2e214..1ef6217c00 100644
    class WP_Date_Query { 
    738738                        $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
    739739                }
    740740                // Specific value queries.
    741 
    742                 if ( isset( $query['year'] ) && $value = $this->build_value( $compare, $query['year'] ) ) {
    743                         $where_parts[] = "YEAR( $column ) $compare $value";
     741   
     742                if ( isset( $query['year'] ) && $value = $this->build_year( $compare, $column, $query['year'] ) ) {
     743                        $where_parts[] = $value;
    744744                }
    745745
    746746                if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) ) {
    class WP_Date_Query { 
    848848                }
    849849        }
    850850
     851        /**
     852         * Builds and validates a year string based on the comparison operator.
     853         *
     854         * @since 5.0.0
     855         *
     856         * @param string $compare The compare operator to use
     857         * @param string|array $value The value
     858         * @return string|false|int The value to be used in SQL or false on error.
     859         */
     860        public function build_year( $compare, $column, $value ) {
     861                if ( ! isset( $value ) ) {
     862                        return false;
     863                }
     864
     865                switch ( $compare ) {
     866                        case 'IN':
     867                        case 'NOT IN':
     868                                $value = (array) $value;
     869
     870                                // Remove non-numeric values.
     871                                $value = array_filter( $value, 'is_numeric' );
     872
     873                                if ( empty( $value ) ) {
     874                                        return false;
     875                                }
     876
     877                                return $column . $compare . 'DATE(' . implode( ',', array_map( 'intval', $value ) ) . '-01-01)';
     878
     879                        case 'BETWEEN':
     880                        case 'NOT BETWEEN':
     881                                if ( ! is_array( $value ) || 2 != count( $value ) ) {
     882                                        $value = array( $value, $value );
     883                                } else {
     884                                        $value = array_values( $value );
     885                                }
     886
     887                                // If either value is non-numeric, bail.
     888                                foreach ( $value as $v ) {
     889                                        if ( ! is_numeric( $v ) ) {
     890                                                return false;
     891                                        }
     892                                }
     893
     894                                $value = array_map( 'intval', $value );
     895
     896                                return $column . ' >= DATE("' . $value[0] . '-01-01") AND ' . $column . ' < DATE("' . $value[1] . '-01-01")';
     897
     898                        default:
     899                                if ( ! is_numeric( $value ) ) {
     900                                        return false;
     901                                }
     902
     903                                return (int) $value;
     904                }
     905        }
     906 
    851907        /**
    852908         * Builds a MySQL format date/time based on some query parameters.
    853909         *