Date queries

This commit is contained in:
Mike Jolley 2019-06-13 12:34:54 +01:00
parent a2f4f15559
commit 76ee42ff6b
2 changed files with 31 additions and 2 deletions

View File

@ -325,6 +325,7 @@ abstract class AbstractObjectsController extends AbstractController {
}
$args['date_query'] = array();
// Set before into date query. Date query must be specified as an array of an array.
if ( isset( $request['before'] ) ) {
$args['date_query'][0]['before'] = $request['before'];
@ -335,6 +336,11 @@ abstract class AbstractObjectsController extends AbstractController {
$args['date_query'][0]['after'] = $request['after'];
}
// Set date query colummn. Defaults to post_date.
if ( isset( $request['date_column'] ) && ! empty( $args['date_query'][0] ) ) {
$args['date_query'][0]['column'] = 'post_' . $request['date_column'];
}
// Force the post_type argument, since it's not a user input variable.
$args['post_type'] = $this->post_type;
@ -587,14 +593,34 @@ abstract class AbstractObjectsController extends AbstractController {
);
$params['after'] = array(
'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ),
'description' => __( 'Limit response to resources created after a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['before'] = array(
'description' => __( 'Limit response to resources published before a given ISO8601 compliant date.', 'woocommerce' ),
'description' => __( 'Limit response to resources created before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
);
$params['date_column'] = array(
'description' => __( 'When limiting response using after/before, which date column to compare against.', 'woocommerce' ),
'type' => 'string',
'default' => 'date',
'enum' => array(
'date',
'date_gmt',
'modified',
'modified_gmt',
),
'validate_callback' => 'rest_validate_request_arg',
);
$params['modified_before'] = array(
'description' => __( 'Limit response to resources modified before a given ISO8601 compliant date.', 'woocommerce' ),
'type' => 'string',
'format' => 'date-time',
'validate_callback' => 'rest_validate_request_arg',
@ -641,6 +667,7 @@ abstract class AbstractObjectsController extends AbstractController {
'default' => 'date',
'enum' => array(
'date',
'modified',
'id',
'include',
'title',

View File

@ -13,6 +13,8 @@
- Product Variations - Added `name`, `type`, `parent_id` to schema.
- Reports - Updated with updated list of available reports.
- Taxes - Added `code` and `include` params.
- Coupons/Orders/Products - Allow to sort by `modified` date.
- Coupons/Orders/Products - Added `date_column` property to use date, date_gmt, modified, modified_gmt in conjunction with before/after.
## New endpoints