From 7801bcb013e4d15ea071bef63c3b8fc9449b56d4 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 20 Jul 2017 17:08:29 -0400 Subject: [PATCH 1/4] Orders: Add support for multiple statuses in list requests --- api/class-wc-rest-dev-orders-controller.php | 47 +++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/api/class-wc-rest-dev-orders-controller.php b/api/class-wc-rest-dev-orders-controller.php index 0969e8c..6984645 100644 --- a/api/class-wc-rest-dev-orders-controller.php +++ b/api/class-wc-rest-dev-orders-controller.php @@ -28,4 +28,51 @@ class WC_REST_Dev_Orders_Controller extends WC_REST_Orders_Controller { */ protected $namespace = 'wc/v3'; + /** + * Prepare objects query. + * + * @since 3.0.0 + * @param WP_REST_Request $request Full details about the request. + * @return array + */ + protected function prepare_objects_query( $request ) { + global $wpdb; + + // This is a hack to get around WC_REST_Orders_Controller::prepare_objects_query + $status = $request['status']; + unset( $request['status'] ); + + $args = parent::prepare_objects_query( $request ); + + // Set post_status. + if ( isset( $status ) ) { + $args['post_status'] = array_map( function( $status ) { return 'wc-' . $status; }, $status ); + } else { + $args['post_status'] = 'any'; + } + + return $args; + } + + /** + * Get the query params for collections. + * + * @return array + */ + public function get_collection_params() { + $params = parent::get_collection_params(); + + $params['status'] = array( + 'default' => 'any', + 'description' => __( 'Limit result set to orders assigned a specific status.', 'woocommerce' ), + 'type' => 'array', + 'items' => array( + 'type' => 'string', + 'enum' => array_merge( array( 'any' ), $this->get_order_statuses() ), + ), + 'validate_callback' => 'rest_validate_request_arg', + ); + + return $params; + } } From 9a4e58e0cd9c62f2eefe8f88b64adc5fc9780440 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 20 Jul 2017 17:22:02 -0400 Subject: [PATCH 2/4] Bump the version --- readme.txt | 5 ++++- wc-api-dev.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/readme.txt b/readme.txt index 29f86ec..b424bdc 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: automattic, woothemes Tags: woocommerce, rest-api, api Requires at least: 4.6 Tested up to: 4.8 -Stable tag: 0.7.1 +Stable tag: 0.8.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -25,6 +25,9 @@ This section describes how to install the plugin and get it working. == Changelog == += 0.8.0 = +* Update orders endpoint to accept multiple statuses + = 0.7.1 = * Fix - add another URI to watch for when disabling sync during API requests diff --git a/wc-api-dev.php b/wc-api-dev.php index 8360be0..ef9a5c6 100644 --- a/wc-api-dev.php +++ b/wc-api-dev.php @@ -3,7 +3,7 @@ * Plugin Name: WooCommerce API Dev * Plugin URI: https://woocommerce.com/ * Description: A feature plugin providing a bleeding edge version of the WooCommerce REST API. - * Version: 0.7.1 + * Version: 0.8.0 * Author: Automattic * Author URI: https://woocommerce.com * Requires at least: 4.4 From bcbccec3b08918c7df38bd55d83c939ccdfe46b8 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Tue, 25 Jul 2017 16:47:11 -0400 Subject: [PATCH 3/4] =?UTF-8?q?PR=20feedback:=20Support=20older=20PHP=20ve?= =?UTF-8?q?rsions,=20update=20phrasing=20of=20=E2=80=9Chack=E2=80=9D=20com?= =?UTF-8?q?ment,=20remove=20unused=20global=20var?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/class-wc-rest-dev-orders-controller.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/api/class-wc-rest-dev-orders-controller.php b/api/class-wc-rest-dev-orders-controller.php index 6984645..e4828ae 100644 --- a/api/class-wc-rest-dev-orders-controller.php +++ b/api/class-wc-rest-dev-orders-controller.php @@ -36,19 +36,19 @@ class WC_REST_Dev_Orders_Controller extends WC_REST_Orders_Controller { * @return array */ protected function prepare_objects_query( $request ) { - global $wpdb; - - // This is a hack to get around WC_REST_Orders_Controller::prepare_objects_query - $status = $request['status']; + // This is needed to get around an array to string notice in WC_REST_Orders_Controller::prepare_objects_query + $statuses = $request['status']; unset( $request['status'] ); - $args = parent::prepare_objects_query( $request ); - // Set post_status. - if ( isset( $status ) ) { - $args['post_status'] = array_map( function( $status ) { return 'wc-' . $status; }, $status ); - } else { - $args['post_status'] = 'any'; + $args['post_status'] = []; + foreach ( $statuses as $status ) { + if ( 'any' === $status ) { + // Set status to "any" and short-circuit out. + $args['post_status'] = 'any'; + break; + } + $args['post_status'][] = 'wc-' . $status; } return $args; From ccf0eca12580824dae1709e4837ea20f8527a0a0 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 27 Jul 2017 11:09:36 -0400 Subject: [PATCH 4/4] Fix array syntax for PHP <5.4 --- api/class-wc-rest-dev-orders-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/class-wc-rest-dev-orders-controller.php b/api/class-wc-rest-dev-orders-controller.php index e4828ae..0e95114 100644 --- a/api/class-wc-rest-dev-orders-controller.php +++ b/api/class-wc-rest-dev-orders-controller.php @@ -41,7 +41,7 @@ protected function prepare_objects_query( $request ) { unset( $request['status'] ); $args = parent::prepare_objects_query( $request ); - $args['post_status'] = []; + $args['post_status'] = array(); foreach ( $statuses as $status ) { if ( 'any' === $status ) { // Set status to "any" and short-circuit out.