From 83da0a228384c9f438f43889ea319344215fe206 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 8 Jul 2019 18:21:06 -0300 Subject: [PATCH] Reflect new changes from https://github.com/woocommerce/woocommerce/pull/23093 --- ...lass-wc-rest-tax-classes-v1-controller.php | 61 +++---------------- 1 file changed, 9 insertions(+), 52 deletions(-) diff --git a/src/Controllers/Version1/class-wc-rest-tax-classes-v1-controller.php b/src/Controllers/Version1/class-wc-rest-tax-classes-v1-controller.php index 365d0c440f8..5a7206fb6a6 100644 --- a/src/Controllers/Version1/class-wc-rest-tax-classes-v1-controller.php +++ b/src/Controllers/Version1/class-wc-rest-tax-classes-v1-controller.php @@ -158,37 +158,18 @@ class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller { } /** - * Create a single tax. + * Create a single tax class. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function create_item( $request ) { - $exists = false; - $classes = WC_Tax::get_tax_classes(); - $tax_class = array( - 'slug' => sanitize_title( $request['name'] ), - 'name' => $request['name'], - ); + $tax_class = WC_Tax::create_tax_class( $request['name'] ); - // Check if class exists. - foreach ( $classes as $key => $class ) { - if ( sanitize_title( $class ) === $tax_class['slug'] ) { - $exists = true; - break; - } + if ( is_wp_error( $tax_class ) ) { + return new WP_Error( 'woocommerce_rest_' . $tax_class->get_error_code(), $tax_class->get_error_message(), array( 'status' => 400 ) ); } - // Return error if tax class already exists. - if ( $exists ) { - return new WP_Error( 'woocommerce_rest_tax_class_exists', __( 'Cannot create existing resource.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); - } - - // Add the new class. - $classes[] = $tax_class['name']; - - update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); - $this->update_additional_fields_for_object( $tax_class, $request ); /** @@ -225,40 +206,16 @@ class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller { return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce-rest-api' ), array( 'status' => 501 ) ); } - $tax_class = array( - 'slug' => sanitize_title( $request['slug'] ), - 'name' => '', - ); - $classes = WC_Tax::get_tax_classes(); - $deleted = false; - - foreach ( $classes as $key => $class ) { - if ( sanitize_title( $class ) === $tax_class['slug'] ) { - $tax_class['name'] = $class; - unset( $classes[ $key ] ); - $deleted = true; - break; - } - } + $tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) ); + $deleted = WC_Tax::delete_tax_class_by( 'slug', sanitize_title( $request['slug'] ) ); if ( ! $deleted ) { return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); } - update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); - - // Delete tax rate locations locations from the selected class. - $wpdb->query( $wpdb->prepare( " - DELETE locations.* - FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations - INNER JOIN - {$wpdb->prefix}woocommerce_tax_rates AS rates - ON rates.tax_rate_id = locations.tax_rate_id - WHERE rates.tax_rate_class = '%s' - ", $tax_class['slug'] ) ); - - // Delete tax rates in the selected class. - $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $tax_class['slug'] ), array( '%s' ) ); + if ( is_wp_error( $deleted ) ) { + return new WP_Error( 'woocommerce_rest_' . $deleted->get_error_code(), $deleted->get_error_message(), array( 'status' => 400 ) ); + } $request->set_param( 'context', 'edit' ); $response = $this->prepare_item_for_response( $tax_class, $request );