diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php index 07a2fc1a15e..6cc198633db 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-coupons-stats-controller.php @@ -62,7 +62,11 @@ class WC_Admin_REST_Reports_Coupons_Stats_Controller extends WC_REST_Reports_Con public function get_items( $request ) { $query_args = $this->prepare_reports_query( $request ); $coupons_query = new WC_Admin_Reports_Coupons_Stats_Query( $query_args ); - $report_data = $coupons_query->get_data(); + try { + $report_data = $coupons_query->get_data(); + } catch ( WC_Admin_Reports_Parameter_Exception $e ) { + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } $out_data = array( 'totals' => get_object_vars( $report_data->totals ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php index 87895d06e45..549badede97 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-orders-stats-controller.php @@ -70,7 +70,11 @@ class WC_Admin_REST_Reports_Orders_Stats_Controller extends WC_Admin_REST_Report public function get_items( $request ) { $query_args = $this->prepare_reports_query( $request ); $orders_query = new WC_Admin_Reports_Orders_Stats_Query( $query_args ); - $report_data = $orders_query->get_data(); + try { + $report_data = $orders_query->get_data(); + } catch ( WC_Admin_Reports_Parameter_Exception $e ) { + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } $out_data = array( 'totals' => get_object_vars( $report_data->totals ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php index d9c4073dbec..a5436bb30d4 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-products-stats-controller.php @@ -74,8 +74,12 @@ class WC_Admin_REST_Reports_Products_Stats_Controller extends WC_REST_Reports_Co } } - $query = new WC_Admin_Reports_Products_Stats_Query( $query_args ); - $report_data = $query->get_data(); + $query = new WC_Admin_Reports_Products_Stats_Query( $query_args ); + try { + $report_data = $query->get_data(); + } catch ( WC_Admin_Reports_Parameter_Exception $e ) { + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } $out_data = array( 'totals' => get_object_vars( $report_data->totals ), diff --git a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php index af71b1dd9f2..15749a93342 100644 --- a/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php +++ b/plugins/woocommerce-admin/includes/api/class-wc-admin-rest-reports-revenue-stats-controller.php @@ -60,7 +60,11 @@ class WC_Admin_REST_Reports_Revenue_Stats_Controller extends WC_REST_Reports_Con public function get_items( $request ) { $query_args = $this->prepare_reports_query( $request ); $reports_revenue = new WC_Admin_Reports_Revenue_Query( $query_args ); - $report_data = $reports_revenue->get_data(); + try { + $report_data = $reports_revenue->get_data(); + } catch ( WC_Admin_Reports_Parameter_Exception $e ) { + return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); + } $out_data = array( 'totals' => get_object_vars( $report_data->totals ), diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php index fd51232e7f3..446cea15b7a 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-api-init.php @@ -110,6 +110,9 @@ class WC_Admin_Api_Init { // Common date time code. require_once dirname( __FILE__ ) . '/class-wc-admin-reports-interval.php'; + // Exceptions. + require_once dirname( __FILE__ ) . '/class-wc-admin-reports-parameter-exception.php'; + // Segmentation. require_once dirname( __FILE__ ) . '/class-wc-admin-reports-segmenting.php'; require_once dirname( __FILE__ ) . '/class-wc-admin-reports-orders-stats-segmenting.php'; diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php index cbb898e9534..dcb82d4040e 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-coupons-stats-segmenting.php @@ -244,7 +244,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * * @return array - * @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified. + * @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. */ protected function get_segments( $type, $query_params, $table_name ) { global $wpdb; @@ -271,7 +271,7 @@ class WC_Admin_Reports_Coupons_Stats_Segmenting extends WC_Admin_Reports_Segment $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { - throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 ); + throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) ); } $segmenting_selections = array( diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php index 8d244feea52..58cd3ae6bc6 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-orders-stats-segmenting.php @@ -335,7 +335,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti * @param string $table_name Name of main SQL table for the data store (used as basis for JOINS). * * @return array - * @throws WC_REST_Exception In case of segmenting by variations, when no parent product is specified. + * @throws WC_Admin_Reports_Parameter_Exception In case of segmenting by variations, when no parent product is specified. */ protected function get_segments( $type, $query_params, $table_name ) { global $wpdb; @@ -363,7 +363,7 @@ class WC_Admin_Reports_Orders_Stats_Segmenting extends WC_Admin_Reports_Segmenti $segments = $this->get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { - throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 ); + throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) ); } $segmenting_selections = array( diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php new file mode 100644 index 00000000000..75231f7d595 --- /dev/null +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-parameter-exception.php @@ -0,0 +1,15 @@ +get_product_related_segments( $type, $segmenting_selections, $segmenting_from, $segmenting_where, $segmenting_groupby, $segmenting_dimension_name, $table_name, $query_params, $unique_orders_table ); } elseif ( 'variation' === $this->query_args['segmentby'] ) { if ( ! isset( $this->query_args['product_includes'] ) || count( $this->query_args['product_includes'] ) !== 1 ) { - throw new WC_REST_Exception( 'woocommerce_rest_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ), 400 ); + throw new WC_Admin_Reports_Parameter_Exception( 'wc_admin_reports_invalid_segmenting_variation', __( 'product_includes parameter need to specify exactly one product when segmenting by variation.', 'wc-admin' ) ); } $segmenting_selections = array(