Merge pull request #5914 from maxrice/feature-issue-5720

Add orderby filtering to REST API resource endpoints
This commit is contained in:
Mike Jolley 2014-07-29 13:15:53 +01:00
commit 2201a93981
2 changed files with 40 additions and 9 deletions

View File

@ -539,6 +539,21 @@ class WC_API_Customers extends WC_API_Resource {
$this->created_at_max = $this->server->parse_datetime( $args['created_at_max'] );
}
// order (ASC or DESC, ASC by default)
if ( ! empty( $args['order'] ) ) {
$query_args['order'] = $args['order'];
}
// orderby
if ( ! empty( $args['orderby'] ) ) {
$query_args['orderby'] = $args['orderby'];
// allow sorting by meta value
if ( ! empty( $args['orderby_meta_key'] ) ) {
$query_args['meta_key'] = $args['orderby_meta_key'];
}
}
$query = new WP_User_Query( $query_args );
// helper members for pagination headers

View File

@ -123,38 +123,54 @@ class WC_API_Resource {
$args['date_query'] = array();
// resources created after specified date
if ( ! empty( $request_args['created_at_min'] ) )
if ( ! empty( $request_args['created_at_min'] ) ) {
$args['date_query'][] = array( 'column' => 'post_date_gmt', 'after' => $this->server->parse_datetime( $request_args['created_at_min'] ), 'inclusive' => true );
}
// resources created before specified date
if ( ! empty( $request_args['created_at_max'] ) )
if ( ! empty( $request_args['created_at_max'] ) ) {
$args['date_query'][] = array( 'column' => 'post_date_gmt', 'before' => $this->server->parse_datetime( $request_args['created_at_max'] ), 'inclusive' => true );
}
// resources updated after specified date
if ( ! empty( $request_args['updated_at_min'] ) )
if ( ! empty( $request_args['updated_at_min'] ) ) {
$args['date_query'][] = array( 'column' => 'post_modified_gmt', 'after' => $this->server->parse_datetime( $request_args['updated_at_min'] ), 'inclusive' => true );
}
// resources updated before specified date
if ( ! empty( $request_args['updated_at_max'] ) )
if ( ! empty( $request_args['updated_at_max'] ) ) {
$args['date_query'][] = array( 'column' => 'post_modified_gmt', 'before' => $this->server->parse_datetime( $request_args['updated_at_max'] ), 'inclusive' => true );
}
}
// search
if ( ! empty( $request_args['q'] ) )
if ( ! empty( $request_args['q'] ) ) {
$args['s'] = $request_args['q'];
}
// resources per response
if ( ! empty( $request_args['limit'] ) )
if ( ! empty( $request_args['limit'] ) ) {
$args['posts_per_page'] = $request_args['limit'];
}
// resource offset
if ( ! empty( $request_args['offset'] ) )
if ( ! empty( $request_args['offset'] ) ) {
$args['offset'] = $request_args['offset'];
}
// allow order change (ASC or DESC)
// order (ASC or DESC, ASC by default)
if ( ! empty( $request_args['order'] ) ) {
$args['order'] = $request_args['order'];
unset( $request_args['order'] );
}
// orderby
if ( ! empty( $request_args['orderby'] ) ) {
$args['orderby'] = $request_args['orderby'];
// allow sorting by meta value
if ( ! empty( $request_args['orderby_meta_key'] ) ) {
$args['meta_key'] = $request_args['orderby_meta_key'];
}
}
// allow post status change