From 0f9ff3d70b59349ccc85ccddac82ebfa7dea6489 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 17 Apr 2019 00:36:20 -0300 Subject: [PATCH] [REST API] Stops order creating if returns any error Prevents orders of been created if a WC_Data_Exception or a WC_REST_Exception was throwed. --- .../api/class-wc-rest-orders-controller.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index d2bb6f06663..904954f4fc7 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -144,6 +144,8 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller { * @return WC_Data|WP_Error */ protected function save_object( $request, $creating = false ) { + $object = null; + try { $object = $this->prepare_object_for_database( $request, $creating ); @@ -196,12 +198,29 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller { return $this->get_object( $object->get_id() ); } catch ( WC_Data_Exception $e ) { + $this->purge( $object, $creating ); return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() ); } catch ( WC_REST_Exception $e ) { + $this->purge( $object, $creating ); return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } } + /** + * Purge object when creating. + * + * @param WC_Data $object Object data. + * @param bool $creating If is creating a new object. + * @return bool + */ + protected function purge( $object, $creating ) { + if ( $object instanceof WC_Data && $creating ) { + return $object->delete( true ); + } + + return false; + } + /** * Prepare objects query. *