diff --git a/includes/api/class-wc-rest-report-orders-count-controller.php b/includes/api/class-wc-rest-report-orders-count-controller.php new file mode 100644 index 00000000000..40eb672175e --- /dev/null +++ b/includes/api/class-wc-rest-report-orders-count-controller.php @@ -0,0 +1,127 @@ + $name ) { + if ( ! isset( $counts->$slug ) ) { + continue; + } + + $data[] = array( + 'slug' => $slug, + 'name' => $name, + 'total' => (int) $counts->$slug, + ); + } + + return $data; + } + + /** + * Prepare a report object for serialization. + * + * @param stdClass $report Report data. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response $response Response data. + */ + public function prepare_item_for_response( $report, $request ) { + $data = array( + 'slug' => $report->slug, + 'name' => $report->name, + 'total' => $report->total, + ); + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + /** + * Filter a report returned from the API. + * + * Allows modification of the report data right before it is returned. + * + * @param WP_REST_Response $response The response object. + * @param object $report The original report object. + * @param WP_REST_Request $request Request used to generate the response. + */ + return apply_filters( 'woocommerce_rest_prepare_report_orders_count', $response, $report, $request ); + } + + /** + * Get the Report's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'report', + 'type' => 'object', + 'properties' => array( + 'slug' => array( + 'description' => __( 'An alphanumeric identifier for the resource.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view' ), + 'readonly' => true, + ), + 'name' => array( + 'description' => __( 'Order status name.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view' ), + 'readonly' => true, + ), + 'total' => array( + 'description' => __( 'Amount of orders.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view' ), + 'readonly' => true, + ), + ), + ); + + return $this->add_additional_fields_schema( $schema ); + } +} diff --git a/includes/class-wc-api.php b/includes/class-wc-api.php index a7bb9b27e71..4448a219f80 100644 --- a/includes/class-wc-api.php +++ b/includes/class-wc-api.php @@ -218,6 +218,7 @@ class WC_API extends WC_Legacy_API { include_once dirname( __FILE__ ) . '/api/class-wc-rest-reports-controller.php'; include_once dirname( __FILE__ ) . '/api/class-wc-rest-report-sales-controller.php'; include_once dirname( __FILE__ ) . '/api/class-wc-rest-report-top-sellers-controller.php'; + include_once dirname( __FILE__ ) . '/api/class-wc-rest-report-orders-count-controller.php'; include_once dirname( __FILE__ ) . '/api/class-wc-rest-settings-controller.php'; include_once dirname( __FILE__ ) . '/api/class-wc-rest-setting-options-controller.php'; include_once dirname( __FILE__ ) . '/api/class-wc-rest-shipping-zones-controller.php'; @@ -319,6 +320,7 @@ class WC_API extends WC_Legacy_API { 'WC_REST_Product_Variations_Controller', 'WC_REST_Report_Sales_Controller', 'WC_REST_Report_Top_Sellers_Controller', + 'WC_REST_Report_Orders_Count_Controller', 'WC_REST_Reports_Controller', 'WC_REST_Settings_Controller', 'WC_REST_Setting_Options_Controller',