Skip to content

Commit

Permalink
New working date query format
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed May 9, 2017
1 parent c89e389 commit 93835f6
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 145 deletions.
145 changes: 76 additions & 69 deletions includes/data-stores/class-wc-data-store-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,19 @@ protected function get_wp_query_args( $query_vars ) {
}

/**
*
* Valid date formats: YYYY-MM-DD or timestamp, possibly combined with an operator in $valid_operators.
* Map a valid date query var to WP_Query arguments.
* Valid date formats: YYYY-MM-DD or timestamp, possibly combined with an operator from $valid_operators.
* Also accepts a WC_DateTime object.
*
* @param mixed $query_var A valid date format
* @param string $key meta or db column key
* @param array $wp_query_args WP_Query args
* @return array Modified $wp_query_args
*/
protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = array() ) {
$query_parse_regex = '/([^.<>]*)(<|>|\.\.\.=?)([^.<>]+)/';
$query_parse_regex = '/([^.<>]*)(>=|<=|>|<|\.\.\.)([^.<>]+)/';
$valid_operators = array( '>', '>=', '=', '<=', '<', '...' );

// YYYY-MM-DD queries have 'day' precision. Timestamp/WC_DateTime queries have 'second' precision.
$precision = 'second';

$start_date = null;
Expand All @@ -268,13 +273,12 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
// Query with operators and possible range of dates.
} elseif ( preg_match( $query_parse_regex, $query_var, $sections ) ) {
if ( ! empty( $sections[1] ) ) {
$start_date = is_numeric( $sections[1] ) ? new WC_DateTime( "@{$query_var}", new DateTimeZone( 'UTC' ) ) : wc_string_to_datetime( $sections[1] );
$start_date = is_numeric( $sections[1] ) ? new WC_DateTime( "@{$sections[1]}", new DateTimeZone( 'UTC' ) ) : wc_string_to_datetime( $sections[1] );
}

$operator = in_array( $sections[2], $valid_operators ) ? $sections[2] : '';
$end_date = is_numeric( $sections[3] ) ? new WC_DateTime( "@{$query_var}", new DateTimeZone( 'UTC' ) ) : wc_string_to_datetime( $sections[3] );
$end_date = is_numeric( $sections[3] ) ? new WC_DateTime( "@{$sections[3]}", new DateTimeZone( 'UTC' ) ) : wc_string_to_datetime( $sections[3] );

// YYYY-MM-DD strings shouldn't match down to the second.
if ( ! is_numeric( $sections[1] ) && ! is_numeric( $sections[3] ) ) {
$precision = 'day';
}
Expand Down Expand Up @@ -308,7 +312,8 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
'day' => $end_date->date( 'j' ),
);
if ( 'second' === $precision ) {

$query_arg['after']['minute'] = $end_date->date( 'i' );
$query_arg['after']['second'] = $end_date->date( 's' );
}
} elseif( '<' == $operator || '<=' == $operator ) {
$query_arg['before'] = array(
Expand All @@ -317,28 +322,33 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
'day' => $end_date->date( 'j' ),
);
if ( 'second' === $precision ) {

$query_arg['before']['minute'] = $end_date->date( 'i' );
$query_arg['before']['second'] = $end_date->date( 's' );
}
} elseif( '...' == $operator ) {
$query_arg['before'] = array(
$query_arg['after'] = array(
'year' => $start_date->date( 'Y' ),
'month' => $start_date->date( 'n' ),
'day' => $start_date->date( 'j' ),
);
$query_arg['after'] = array(
$query_arg['before'] = array(
'year' => $end_date->date( 'Y' ),
'month' => $end_date->date( 'n' ),
'day' => $end_date->date( 'j' ),
);
if ( 'second' === $precision ) {

$query_arg['after']['minute'] = $start_date->date( 'i' );
$query_arg['after']['second'] = $start_date->date( 's' );
$query_arg['before']['minute'] = $end_date->date( 'i' );
$query_arg['before']['second'] = $end_date->date( 's' );
}
} else {
$query_arg['year'] = $end_date->date( 'Y' );
$query_arg['month'] = $end_date->date( 'n' );
$query_arg['day'] = $end_date->date( 'j' );
if ( 'second' === $precision ) {

$query_arg['minute'] = $end_date->date( 'i' );
$query_arg['second'] = $end_date->date( 's' );
}
}

Expand All @@ -351,66 +361,63 @@ protected function parse_date_for_wp_query( $query_var, $key, $wp_query_args = a
$wp_query_args['meta_query'] = array();
}

if ( '...' !== $operator ) {
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $end_date->getTimestamp(),
'compare' => $operator,
);
} else {
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $start_date->getTimestamp(),
'compare' => '>=',
);
$wp_query_args['meta_query'][] = array(
'key' => '$key',
'value' => $end_date->getTimestamp(),
'compare' => '<=',
);
}

return $wp_query_args;
}

protected function build_wp_date_query( $start, $end, $operator, $precision ) {

}

/**
* Get a valid date for use in WP_Query date queries.
*
* @since 3.1.0
* @param mixed $query_var Value from a WC_Object_Query's query variable.
*/
protected function get_date_for_wp_query( $query_var, $as_timestamp = false ) {
if ( ! $query_var ) {
return '';
}

if ( is_a( $query_var, 'WC_DateTime' ) ) {
$datetime = $query_var;
} elseif ( is_numeric( $query_var ) ) {
// Timestamps are handled as UTC timestamps in all cases.
$datetime = new WC_DateTime( "@{$value}", new DateTimeZone( 'UTC' ) );
// Meta dates are stored as timestamps in the db.
// Check against begining/end-of-day timestamps when using 'day' precision.
if ( 'day' === $precision ) {
$start_timestamp = '...' !== $operator ? strtotime( $end_date->date( 'm/d/Y 00:00:00' ) ) : strtotime( $start_date->date( 'm/d/Y 00:00:00' ) );
$end_timestamp = '...' !== $operator ? ( $start_timestamp + DAY_IN_SECONDS ) : strtotime( $end_date->date( 'm/d/Y 00:00:00' ) );
switch ( $operator ) {
case '>':
case '<=':
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $end_timestamp,
'compare' => $operator,
);
break;

case '<':
case '>=':
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $start_timestamp,
'compare' => $operator,
);
break;

default:
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $start_timestamp,
'compare' => '>=',
);
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $end_timestamp,
'compare' => '<=',
);
}
} else {
// Strings are defined in local WP timezone. Convert to UTC.
if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $query_var, $date_bits ) ) {
$offset = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
$timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
if ( '...' !== $operator ) {
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $end_date->getTimestamp(),
'compare' => $operator,
);
} else {
$timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $query_var ) ) ) );
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $start_date->getTimestamp(),
'compare' => '>=',
);
$wp_query_args['meta_query'][] = array(
'key' => $key,
'value' => $end_date->getTimestamp(),
'compare' => '<=',
);
}
$datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );
}

// Set local timezone or offset.
if ( get_option( 'timezone_string' ) ) {
$datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
} else {
$datetime->set_utc_offset( wc_timezone_offset() );
}

return $as_timestamp ? $datetime->getTimestamp() : (string) $datetime;
return $wp_query_args;
}
}
68 changes: 7 additions & 61 deletions includes/data-stores/class-wc-order-data-store-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,71 +673,17 @@ protected function get_wp_query_args( $query_vars ) {
);
foreach ( $date_queries as $query_var_key => $db_key ) {
if ( isset( $query_vars[ $query_var_key ] ) && '' !== $query_vars[ $query_var_key ] ) {
$wp_query_args = $this->parse_date_for_wp_query( $query_vars[ $query_var_key ], $db_key, $wp_query_args );
}
}

/*
if ( isset( $query_vars[ 'date_created_before'] ) && '' !== $query_vars['date_created_before'] ) {
$wp_query_args['date_query'][] = array(
'column' => 'post_date',
'before' => $this->get_date_for_wp_query( $query_vars['date_created_before'] )
);
}
// Remove any existing meta queries for the same keys to prevent conflicts.
$existing_queries = wp_list_pluck( $wp_query_args['meta_query'], 'key', true );
foreach ( $existing_queries as $query_index => $query_contents ) {
unset( $wp_query_args['meta_query'][ $query_index ] );
}

if ( isset( $query_vars[ 'date_created_after'] ) && '' !== $query_vars[ 'date_created_after' ] ) {
$wp_query_args['date_query'][] = array(
'column' => 'post_date',
'after' => $this->get_date_for_wp_query( $query_vars['date_created_after'] )
);
}
if ( isset( $query_vars[ 'date_modified_before'] ) && '' !== $query_vars[ 'date_modified_before' ] ) {
$wp_query_args['date_query'][] = array(
'column' => 'post_modified',
'after' => $this->get_date_for_wp_query( $query_vars['date_modified_before'] )
);
}
if ( isset( $query_vars[ 'date_modified_after'] ) && '' !== $query_vars[ 'date_modified_after' ] ) {
$wp_query_args['date_query'][] = array(
'column' => 'post_modified',
'after' => $this->get_date_for_wp_query( $query_vars['date_modified_after'] )
);
}
if ( isset( $query_vars[ 'date_completed_before' ] ) && '' !== $query_vars[ 'date_completed_before' ] ) {
$wp_query_args['meta_query'][] = array(
'key' => '_date_completed',
'value' => $this->get_date_for_wp_query( $query_vars[ 'date_completed_before' ], true ),
'compare' => '<',
);
}
if ( isset( $query_vars[ 'date_completed_after' ] ) && '' !== $query_vars[ 'date_completed_after' ] ) {
$wp_query_args['meta_query'][] = array(
'key' => '_date_completed',
'value' => $this->get_date_for_wp_query( $query_vars[ 'date_completed_after' ], true ),
'compare' => '>',
);
}
if ( isset( $query_vars[ 'date_paid_before' ] ) && '' !== $query_vars[ 'date_paid_before' ] ) {
$wp_query_args['meta_query'][] = array(
'key' => '_date_paid',
'value' => $this->get_date_for_wp_query( $query_vars[ 'date_paid_before' ], true ),
'compare' => '<',
);
$wp_query_args = $this->parse_date_for_wp_query( $query_vars[ $query_var_key ], $db_key, $wp_query_args );
}
}

if ( isset( $query_vars[ 'date_paid_after' ] ) && '' !== $query_vars[ 'date_paid_after' ] ) {
$wp_query_args['meta_query'][] = array(
'key' => '_date_paid',
'value' => $this->get_date_for_wp_query( $query_vars[ 'date_paid_after' ], true ),
'compare' => '>',
);
}*/

if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) {
$wp_query_args['no_found_rows'] = true;
}
Expand Down
Loading

0 comments on commit 93835f6

Please sign in to comment.