From fe0dea6373dbf75495022c9b5111c2f5edec65a5 Mon Sep 17 00:00:00 2001 From: Joshua Flowers Date: Mon, 15 Apr 2019 15:18:24 +0800 Subject: [PATCH] Add delete imports endpoint --- ...c-admin-rest-reports-import-controller.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php index e24c4c518ce..6539ba879e0 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-import-controller.php @@ -59,6 +59,18 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont 'schema' => array( $this, 'get_import_public_schema' ), ) ); + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/delete', + array( + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'delete_imported_items' ), + 'permission_callback' => array( $this, 'import_permissions_check' ), + ), + 'schema' => array( $this, 'get_import_public_schema' ), + ) + ); } /** @@ -204,6 +216,35 @@ class WC_Admin_REST_Reports_Import_Controller extends WC_Admin_REST_Reports_Cont 'status' => 'success', 'message' => __( 'All pending and in-progress import actions have been cancelled.', 'woocommerce-admin' ), ); + + $response = $this->prepare_item_for_response( $result, $request ); + $data = $this->prepare_response_for_collection( $response ); + + return rest_ensure_response( $data ); + } + + /** + * Delete all imported items. + * + * @param WP_REST_Request $request Request data. + * @return WP_Error|WP_REST_Response + */ + + public function delete_imported_items( $request ) { + $delete = WC_Admin_Reports_Sync::delete_report_data(); + + if ( is_wp_error( $delete ) ) { + $result = array( + 'status' => 'error', + 'message' => $delete->get_error_message(), + ); + } else { + $result = array( + 'status' => 'success', + 'message' => $delete, + ); + } + $response = $this->prepare_item_for_response( $result, $request ); $data = $this->prepare_response_for_collection( $response );