Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WC_API_Taxes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_API_Taxes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WC_API_Taxes extends WC_API_Resource { |
||
| 18 | |||
| 19 | /** @var string $base the route base */ |
||
| 20 | protected $base = '/taxes'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Register the routes for this class |
||
| 24 | * |
||
| 25 | * GET /taxes |
||
| 26 | * GET /taxes/count |
||
| 27 | * GET /taxes/<id> |
||
| 28 | * |
||
| 29 | * @since 2.1 |
||
| 30 | * @param array $routes |
||
| 31 | * @return array |
||
| 32 | */ |
||
| 33 | public function register_routes( $routes ) { |
||
| 34 | |||
| 35 | # GET/POST /taxes |
||
| 36 | $routes[ $this->base ] = array( |
||
| 37 | array( array( $this, 'get_taxes' ), WC_API_Server::READABLE ), |
||
| 38 | array( array( $this, 'create_tax' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ), |
||
| 39 | ); |
||
| 40 | |||
| 41 | # GET /taxes/count |
||
| 42 | $routes[ $this->base . '/count'] = array( |
||
| 43 | array( array( $this, 'get_taxes_count' ), WC_API_Server::READABLE ), |
||
| 44 | ); |
||
| 45 | |||
| 46 | # GET/PUT/DELETE /taxes/<id> |
||
| 47 | $routes[ $this->base . '/(?P<id>\d+)' ] = array( |
||
| 48 | array( array( $this, 'get_tax' ), WC_API_Server::READABLE ), |
||
| 49 | array( array( $this, 'edit_tax' ), WC_API_SERVER::EDITABLE | WC_API_SERVER::ACCEPT_DATA ), |
||
| 50 | array( array( $this, 'delete_tax' ), WC_API_SERVER::DELETABLE ), |
||
| 51 | ); |
||
| 52 | |||
| 53 | # GET/POST /taxes/classes |
||
| 54 | $routes[ $this->base . '/classes' ] = array( |
||
| 55 | array( array( $this, 'get_tax_classes' ), WC_API_Server::READABLE ), |
||
| 56 | array( array( $this, 'create_tax_class' ), WC_API_Server::CREATABLE | WC_API_Server::ACCEPT_DATA ), |
||
| 57 | ); |
||
| 58 | |||
| 59 | # GET /taxes/classes/count |
||
| 60 | $routes[ $this->base . '/classes/count'] = array( |
||
| 61 | array( array( $this, 'get_tax_classes_count' ), WC_API_Server::READABLE ), |
||
| 62 | ); |
||
| 63 | |||
| 64 | # GET /taxes/classes/<slug> |
||
| 65 | $routes[ $this->base . '/classes/(?P<slug>\w[\w\s\-]*)' ] = array( |
||
| 66 | array( array( $this, 'delete_tax_class' ), WC_API_SERVER::DELETABLE ), |
||
| 67 | ); |
||
| 68 | |||
| 69 | # POST|PUT /taxes/bulk |
||
| 70 | $routes[ $this->base . '/bulk' ] = array( |
||
| 71 | array( array( $this, 'bulk' ), WC_API_Server::EDITABLE | WC_API_Server::ACCEPT_DATA ), |
||
| 72 | ); |
||
| 73 | |||
| 74 | return $routes; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get all taxes |
||
| 79 | * |
||
| 80 | * @since 2.5.0 |
||
| 81 | * |
||
| 82 | * @param string $fields |
||
| 83 | * @param array $filter |
||
| 84 | * @param string $class |
||
| 85 | * @param int $page |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function get_taxes( $fields = null, $filter = array(), $class = null, $page = 1 ) { |
||
| 90 | if ( ! empty( $class ) ) { |
||
| 91 | $filter['tax_rate_class'] = $class; |
||
| 92 | } |
||
| 93 | |||
| 94 | $filter['page'] = $page; |
||
| 95 | |||
| 96 | $query = $this->query_tax_rates( $filter ); |
||
| 97 | |||
| 98 | $taxes = array(); |
||
| 99 | |||
| 100 | foreach ( $query['results'] as $tax ) { |
||
| 101 | $taxes[] = current( $this->get_tax( $tax->tax_rate_id, $fields ) ); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Set pagination headers |
||
| 105 | $this->server->add_pagination_headers( $query['headers'] ); |
||
| 106 | |||
| 107 | return array( 'taxes' => $taxes ); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the tax for the given ID |
||
| 112 | * |
||
| 113 | * @since 2.5.0 |
||
| 114 | * |
||
| 115 | * @param int $id The tax ID |
||
| 116 | * @param string $fields fields to include in response |
||
| 117 | * |
||
| 118 | * @return array|WP_Error |
||
| 119 | */ |
||
| 120 | public function get_tax( $id, $fields = null ) { |
||
| 121 | global $wpdb; |
||
| 122 | |||
| 123 | try { |
||
| 124 | $id = absint( $id ); |
||
| 125 | |||
| 126 | // Permissions check |
||
| 127 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 128 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax', __( 'You do not have permission to read tax rate', 'woocommerce' ), 401 ); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Get tax rate details |
||
| 132 | $tax = WC_Tax::_get_tax_rate( $id ); |
||
| 133 | |||
| 134 | if ( is_wp_error( $tax ) || empty( $tax ) ) { |
||
| 135 | throw new WC_API_Exception( 'woocommerce_api_invalid_tax_id', __( 'A tax rate with the provided ID could not be found', 'woocommerce' ), 404 ); |
||
| 136 | } |
||
| 137 | |||
| 138 | $tax_data = array( |
||
| 139 | 'id' => (int) $tax['tax_rate_id'], |
||
| 140 | 'country' => $tax['tax_rate_country'], |
||
| 141 | 'state' => $tax['tax_rate_state'], |
||
| 142 | 'postcode' => '', |
||
| 143 | 'city' => '', |
||
| 144 | 'rate' => $tax['tax_rate'], |
||
| 145 | 'name' => $tax['tax_rate_name'], |
||
| 146 | 'priority' => (int) $tax['tax_rate_priority'], |
||
| 147 | 'compound' => (bool) $tax['tax_rate_compound'], |
||
| 148 | 'shipping' => (bool) $tax['tax_rate_shipping'], |
||
| 149 | 'order' => (int) $tax['tax_rate_order'], |
||
| 150 | 'class' => $tax['tax_rate_class'] ? $tax['tax_rate_class'] : 'standard' |
||
| 151 | ); |
||
| 152 | |||
| 153 | // Get locales from a tax rate |
||
| 154 | $locales = $wpdb->get_results( $wpdb->prepare( " |
||
| 155 | SELECT location_code, location_type |
||
| 156 | FROM {$wpdb->prefix}woocommerce_tax_rate_locations |
||
| 157 | WHERE tax_rate_id = %d |
||
| 158 | ", $id ) ); |
||
| 159 | |||
| 160 | if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) { |
||
| 161 | foreach ( $locales as $locale ) { |
||
| 162 | $tax_data[ $locale->location_type ] = $locale->location_code; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | return array( 'tax' => apply_filters( 'woocommerce_api_tax_response', $tax_data, $tax, $fields, $this ) ); |
||
| 167 | } catch ( WC_API_Exception $e ) { |
||
| 168 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Create a tax |
||
| 174 | * |
||
| 175 | * @since 2.5.0 |
||
| 176 | * |
||
| 177 | * @param array $data |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function create_tax( $data ) { |
||
| 182 | try { |
||
| 183 | if ( ! isset( $data['tax'] ) ) { |
||
| 184 | throw new WC_API_Exception( 'woocommerce_api_missing_tax_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'tax' ), 400 ); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Check permissions |
||
| 188 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 189 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_tax', __( 'You do not have permission to create tax rates', 'woocommerce' ), 401 ); |
||
| 190 | } |
||
| 191 | |||
| 192 | $data = apply_filters( 'woocommerce_api_create_tax_data', $data['tax'], $this ); |
||
| 193 | |||
| 194 | $tax_data = array( |
||
| 195 | 'tax_rate_country' => '', |
||
| 196 | 'tax_rate_state' => '', |
||
| 197 | 'tax_rate' => '', |
||
| 198 | 'tax_rate_name' => '', |
||
| 199 | 'tax_rate_priority' => 1, |
||
| 200 | 'tax_rate_compound' => 0, |
||
| 201 | 'tax_rate_shipping' => 1, |
||
| 202 | 'tax_rate_order' => 0, |
||
| 203 | 'tax_rate_class' => '', |
||
| 204 | ); |
||
| 205 | |||
| 206 | foreach ( $tax_data as $key => $value ) { |
||
| 207 | $new_key = str_replace( 'tax_rate_', '', $key ); |
||
| 208 | $new_key = 'tax_rate' === $new_key ? 'rate' : $new_key; |
||
| 209 | |||
| 210 | if ( isset( $data[ $new_key ] ) ) { |
||
| 211 | if ( in_array( $new_key, array( 'compound', 'shipping' ) ) ) { |
||
| 212 | $tax_data[ $key ] = $data[ $new_key ] ? 1 : 0; |
||
| 213 | } else { |
||
| 214 | $tax_data[ $key ] = $data[ $new_key ]; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | // Create tax rate |
||
| 220 | $id = WC_Tax::_insert_tax_rate( $tax_data ); |
||
| 221 | |||
| 222 | // Add locales |
||
| 223 | if ( ! empty( $data['postcode'] ) ) { |
||
| 224 | WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $data['postcode'] ) ); |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( ! empty( $data['city'] ) ) { |
||
| 228 | WC_Tax::_update_tax_rate_cities( $id, wc_clean( $data['city'] ) ); |
||
| 229 | } |
||
| 230 | |||
| 231 | do_action( 'woocommerce_api_create_tax', $id, $data ); |
||
| 232 | |||
| 233 | $this->server->send_status( 201 ); |
||
| 234 | |||
| 235 | return $this->get_tax( $id ); |
||
| 236 | } catch ( WC_API_Exception $e ) { |
||
| 237 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Edit a tax |
||
| 243 | * |
||
| 244 | * @since 2.5.0 |
||
| 245 | * |
||
| 246 | * @param int $id The tax ID |
||
| 247 | * @param array $data |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function edit_tax( $id, $data ) { |
||
| 252 | try { |
||
| 253 | if ( ! isset( $data['tax'] ) ) { |
||
| 254 | throw new WC_API_Exception( 'woocommerce_api_missing_tax_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'tax' ), 400 ); |
||
| 255 | } |
||
| 256 | |||
| 257 | // Check permissions |
||
| 258 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 259 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_tax', __( 'You do not have permission to edit tax rates', 'woocommerce' ), 401 ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $data = $data['tax']; |
||
| 263 | |||
| 264 | // Get current tax rate data |
||
| 265 | $tax = $this->get_tax( $id ); |
||
| 266 | |||
| 267 | if ( is_wp_error( $tax ) ) { |
||
| 268 | $error_data = $tax->get_error_data(); |
||
| 269 | throw new WC_API_Exception( $tax->get_error_code(), $tax->get_error_message(), $error_data['status'] ); |
||
| 270 | } |
||
| 271 | |||
| 272 | $current_data = $tax['tax']; |
||
| 273 | $data = apply_filters( 'woocommerce_api_edit_tax_data', $data, $this ); |
||
| 274 | $tax_data = array(); |
||
| 275 | $default_fields = array( |
||
| 276 | 'tax_rate_country', |
||
| 277 | 'tax_rate_state', |
||
| 278 | 'tax_rate', |
||
| 279 | 'tax_rate_name', |
||
| 280 | 'tax_rate_priority', |
||
| 281 | 'tax_rate_compound', |
||
| 282 | 'tax_rate_shipping', |
||
| 283 | 'tax_rate_order', |
||
| 284 | 'tax_rate_class' |
||
| 285 | ); |
||
| 286 | |||
| 287 | foreach ( $data as $key => $value ) { |
||
| 288 | $new_key = 'rate' === $key ? 'tax_rate' : 'tax_rate_' . $key; |
||
| 289 | |||
| 290 | // Check if the key is valid |
||
| 291 | if ( ! in_array( $new_key, $default_fields ) ) { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | |||
| 295 | // Test new data against current data |
||
| 296 | if ( $value === $current_data[ $key ] ) { |
||
| 297 | continue; |
||
| 298 | } |
||
| 299 | |||
| 300 | // Fix compund and shipping values |
||
| 301 | if ( in_array( $key, array( 'compound', 'shipping' ) ) ) { |
||
| 302 | $value = $value ? 1 : 0; |
||
| 303 | } |
||
| 304 | |||
| 305 | $tax_data[ $new_key ] = $value; |
||
| 306 | } |
||
| 307 | |||
| 308 | // Update tax rate |
||
| 309 | WC_Tax::_update_tax_rate( $id, $tax_data ); |
||
| 310 | |||
| 311 | // Update locales |
||
| 312 | if ( ! empty( $data['postcode'] ) && $current_data['postcode'] != $data['postcode'] ) { |
||
| 313 | WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $data['postcode'] ) ); |
||
| 314 | } |
||
| 315 | |||
| 316 | if ( ! empty( $data['city'] ) && $current_data['city'] != $data['city'] ) { |
||
| 317 | WC_Tax::_update_tax_rate_cities( $id, wc_clean( $data['city'] ) ); |
||
| 318 | } |
||
| 319 | |||
| 320 | do_action( 'woocommerce_api_edit_tax_rate', $id, $data ); |
||
| 321 | |||
| 322 | return $this->get_tax( $id ); |
||
| 323 | } catch ( WC_API_Exception $e ) { |
||
| 324 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Delete a tax |
||
| 330 | * |
||
| 331 | * @since 2.5.0 |
||
| 332 | * |
||
| 333 | * @param int $id The tax ID |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function delete_tax( $id ) { |
||
| 338 | global $wpdb; |
||
| 339 | |||
| 340 | try { |
||
| 341 | // Check permissions |
||
| 342 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 343 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_tax', __( 'You do not have permission to delete tax rates', 'woocommerce' ), 401 ); |
||
| 344 | } |
||
| 345 | |||
| 346 | $id = absint( $id ); |
||
| 347 | |||
| 348 | WC_Tax::_delete_tax_rate( $id ); |
||
| 349 | |||
| 350 | if ( 0 === $wpdb->rows_affected ) { |
||
| 351 | throw new WC_API_Exception( 'woocommerce_api_cannot_delete_tax', __( 'Could not delete the tax rate', 'woocommerce' ), 401 ); |
||
| 352 | } |
||
| 353 | |||
| 354 | return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'tax' ) ); |
||
| 355 | } catch ( WC_API_Exception $e ) { |
||
| 356 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get the total number of taxes |
||
| 362 | * |
||
| 363 | * @since 2.5.0 |
||
| 364 | * |
||
| 365 | * @param string $class |
||
| 366 | * @param array $filter |
||
| 367 | * |
||
| 368 | * @return array |
||
| 369 | */ |
||
| 370 | public function get_taxes_count( $class = null, $filter = array() ) { |
||
| 371 | try { |
||
| 372 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 373 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_taxes_count', __( 'You do not have permission to read the taxes count', 'woocommerce' ), 401 ); |
||
| 374 | } |
||
| 375 | |||
| 376 | if ( ! empty( $class ) ) { |
||
| 377 | $filter['tax_rate_class'] = $class; |
||
| 378 | } |
||
| 379 | |||
| 380 | $query = $this->query_tax_rates( $filter, true ); |
||
| 381 | |||
| 382 | return array( 'count' => (int) $query['headers']->total ); |
||
| 383 | } catch ( WC_API_Exception $e ) { |
||
| 384 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Helper method to get tax rates objects |
||
| 390 | * |
||
| 391 | * @since 2.5.0 |
||
| 392 | * |
||
| 393 | * @param array $args |
||
| 394 | * @param bool $count_only |
||
| 395 | * |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | protected function query_tax_rates( $args, $count_only = false ) { |
||
| 399 | global $wpdb; |
||
| 400 | |||
| 401 | $results = ''; |
||
| 402 | |||
| 403 | // Set args |
||
| 404 | $args = $this->merge_query_args( $args, array() ); |
||
| 405 | |||
| 406 | $query = " |
||
| 407 | SELECT tax_rate_id |
||
| 408 | FROM {$wpdb->prefix}woocommerce_tax_rates |
||
| 409 | WHERE 1 = 1 |
||
| 410 | "; |
||
| 411 | |||
| 412 | // Filter by tax class |
||
| 413 | if ( ! empty( $args['tax_rate_class'] ) ) { |
||
| 414 | $tax_rate_class = 'standard' !== $args['tax_rate_class'] ? sanitize_title( $args['tax_rate_class'] ) : ''; |
||
| 415 | $query .= " AND tax_rate_class = '$tax_rate_class'"; |
||
| 416 | } |
||
| 417 | |||
| 418 | // Order tax rates |
||
| 419 | $order_by = ' ORDER BY tax_rate_order'; |
||
| 420 | |||
| 421 | // Pagination |
||
| 422 | $per_page = isset( $args['posts_per_page'] ) ? $args['posts_per_page'] : get_option( 'posts_per_page' ); |
||
| 423 | $offset = 1 < $args['paged'] ? ( $args['paged'] - 1 ) * $per_page : 0; |
||
| 424 | $pagination = sprintf( ' LIMIT %d, %d', $offset, $per_page ); |
||
| 425 | |||
| 426 | if ( ! $count_only ) { |
||
| 427 | $results = $wpdb->get_results( $query . $order_by . $pagination ); |
||
| 428 | } |
||
| 429 | |||
| 430 | $wpdb->get_results( $query ); |
||
| 431 | $headers = new stdClass; |
||
| 432 | $headers->page = $args['paged']; |
||
| 433 | $headers->total = (int) $wpdb->num_rows; |
||
| 434 | $headers->is_single = $per_page > $headers->total; |
||
| 435 | $headers->total_pages = ceil( $headers->total / $per_page ); |
||
| 436 | |||
| 437 | return array( |
||
| 438 | 'results' => $results, |
||
| 439 | 'headers' => $headers |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Bulk update or insert taxes |
||
| 445 | * Accepts an array with taxes in the formats supported by |
||
| 446 | * WC_API_Taxes->create_tax() and WC_API_Taxes->edit_tax() |
||
| 447 | * |
||
| 448 | * @since 2.5.0 |
||
| 449 | * |
||
| 450 | * @param array $data |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | public function bulk( $data ) { |
||
| 455 | try { |
||
| 456 | if ( ! isset( $data['taxes'] ) ) { |
||
| 457 | throw new WC_API_Exception( 'woocommerce_api_missing_taxes_data', sprintf( __( 'No %1$s data specified to create/edit %1$s', 'woocommerce' ), 'taxes' ), 400 ); |
||
| 458 | } |
||
| 459 | |||
| 460 | $data = $data['taxes']; |
||
| 461 | $limit = apply_filters( 'woocommerce_api_bulk_limit', 100, 'taxes' ); |
||
| 462 | |||
| 463 | // Limit bulk operation |
||
| 464 | if ( count( $data ) > $limit ) { |
||
| 465 | throw new WC_API_Exception( 'woocommerce_api_taxes_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request', 'woocommerce' ), $limit ), 413 ); |
||
| 466 | } |
||
| 467 | |||
| 468 | $taxes = array(); |
||
| 469 | |||
| 470 | foreach ( $data as $_tax ) { |
||
| 471 | $tax_id = 0; |
||
| 472 | |||
| 473 | // Try to get the tax rate ID |
||
| 474 | if ( isset( $_tax['id'] ) ) { |
||
| 475 | $tax_id = intval( $_tax['id'] ); |
||
| 476 | } |
||
| 477 | |||
| 478 | // Tax rate exists / edit tax rate |
||
| 479 | if ( $tax_id ) { |
||
| 480 | $edit = $this->edit_tax( $tax_id, array( 'tax' => $_tax ) ); |
||
| 481 | |||
| 482 | if ( is_wp_error( $edit ) ) { |
||
| 483 | $taxes[] = array( |
||
| 484 | 'id' => $tax_id, |
||
| 485 | 'error' => array( 'code' => $edit->get_error_code(), 'message' => $edit->get_error_message() ) |
||
| 486 | ); |
||
| 487 | } else { |
||
| 488 | $taxes[] = $edit['tax']; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | |||
| 492 | // Tax rate don't exists / create tax rate |
||
| 493 | else { |
||
| 494 | $new = $this->create_tax( array( 'tax' => $_tax ) ); |
||
| 495 | |||
| 496 | if ( is_wp_error( $new ) ) { |
||
| 497 | $taxes[] = array( |
||
| 498 | 'id' => $tax_id, |
||
| 499 | 'error' => array( 'code' => $new->get_error_code(), 'message' => $new->get_error_message() ) |
||
| 500 | ); |
||
| 501 | } else { |
||
| 502 | $taxes[] = $new['tax']; |
||
| 503 | } |
||
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | return array( 'taxes' => apply_filters( 'woocommerce_api_taxes_bulk_response', $taxes, $this ) ); |
||
| 508 | } catch ( WC_API_Exception $e ) { |
||
| 509 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Get all tax classes |
||
| 515 | * |
||
| 516 | * @since 2.5.0 |
||
| 517 | * |
||
| 518 | * @param string $fields |
||
| 519 | * |
||
| 520 | * @return array |
||
| 521 | */ |
||
| 522 | public function get_tax_classes( $fields = null ) { |
||
| 523 | try { |
||
| 524 | // Permissions check |
||
| 525 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 526 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax_classes', __( 'You do not have permission to read tax classes', 'woocommerce' ), 401 ); |
||
| 527 | } |
||
| 528 | |||
| 529 | $tax_classes = array(); |
||
| 530 | |||
| 531 | // Add standard class |
||
| 532 | $tax_classes[] = array( |
||
| 533 | 'slug' => 'standard', |
||
| 534 | 'name' => __( 'Standard Rate', 'woocommerce' ) |
||
| 535 | ); |
||
| 536 | |||
| 537 | $classes = WC_Tax::get_tax_classes(); |
||
| 538 | |||
| 539 | foreach ( $classes as $class ) { |
||
| 540 | $tax_classes[] = apply_filters( 'woocommerce_api_tax_class_response', array( |
||
| 541 | 'slug' => sanitize_title( $class ), |
||
| 542 | 'name' => $class |
||
| 543 | ), $class, $fields, $this ); |
||
| 544 | } |
||
| 545 | |||
| 546 | return array( 'tax_classes' => apply_filters( 'woocommerce_api_tax_classes_response', $tax_classes, $classes, $fields, $this ) ); |
||
| 547 | } catch ( WC_API_Exception $e ) { |
||
| 548 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Create a tax class. |
||
| 554 | * |
||
| 555 | * @since 2.5.0 |
||
| 556 | * |
||
| 557 | * @param array $data |
||
| 558 | * |
||
| 559 | * @return array |
||
| 560 | */ |
||
| 561 | public function create_tax_class( $data ) { |
||
| 562 | try { |
||
| 563 | if ( ! isset( $data['tax_class'] ) ) { |
||
| 564 | throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'tax_class' ), 400 ); |
||
| 565 | } |
||
| 566 | |||
| 567 | // Check permissions |
||
| 568 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 569 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_tax_class', __( 'You do not have permission to create tax classes', 'woocommerce' ), 401 ); |
||
| 570 | } |
||
| 571 | |||
| 572 | $data = $data['tax_class']; |
||
| 573 | |||
| 574 | if ( empty( $data['name'] ) ) { |
||
| 575 | throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 ); |
||
| 576 | } |
||
| 577 | |||
| 578 | $name = sanitize_text_field( $data['name'] ); |
||
| 579 | $slug = sanitize_title( $name ); |
||
| 580 | $classes = WC_Tax::get_tax_classes(); |
||
| 581 | $exists = false; |
||
| 582 | |||
| 583 | // Check if class exists. |
||
| 584 | foreach ( $classes as $key => $class ) { |
||
| 585 | if ( sanitize_title( $class ) === $slug ) { |
||
| 586 | $exists = true; |
||
| 587 | break; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | // Return error if tax class already exists. |
||
| 592 | if ( $exists ) { |
||
| 593 | throw new WC_API_Exception( 'woocommerce_api_cannot_create_tax_class', __( 'Tax class already exists', 'woocommerce' ), 401 ); |
||
| 594 | } |
||
| 595 | |||
| 596 | // Add the new class. |
||
| 597 | $classes[] = $name; |
||
| 598 | |||
| 599 | update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); |
||
| 600 | |||
| 601 | do_action( 'woocommerce_api_create_tax_class', $slug, $data ); |
||
| 602 | |||
| 603 | $this->server->send_status( 201 ); |
||
| 604 | |||
| 605 | return array( |
||
| 606 | 'tax_class' => array( |
||
| 607 | 'slug' => $slug, |
||
| 608 | 'name' => $name |
||
| 609 | ) |
||
| 610 | ); |
||
| 611 | } catch ( WC_API_Exception $e ) { |
||
| 612 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Delete a tax class |
||
| 618 | * |
||
| 619 | * @since 2.5.0 |
||
| 620 | * |
||
| 621 | * @param int $slug The tax class slug |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | public function delete_tax_class( $slug ) { |
||
| 626 | global $wpdb; |
||
| 627 | |||
| 628 | try { |
||
| 629 | // Check permissions |
||
| 630 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 631 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_delete_tax_class', __( 'You do not have permission to delete tax classes', 'woocommerce' ), 401 ); |
||
| 632 | } |
||
| 633 | |||
| 634 | $slug = sanitize_title( $slug ); |
||
| 635 | $classes = WC_Tax::get_tax_classes(); |
||
| 636 | $deleted = false; |
||
| 637 | |||
| 638 | foreach ( $classes as $key => $class ) { |
||
| 639 | if ( sanitize_title( $class ) === $slug ) { |
||
| 640 | unset( $classes[ $key ] ); |
||
| 641 | $deleted = true; |
||
| 642 | break; |
||
| 643 | } |
||
| 644 | } |
||
| 645 | |||
| 646 | if ( ! $deleted ) { |
||
| 647 | throw new WC_API_Exception( 'woocommerce_api_cannot_delete_tax_class', __( 'Could not delete the tax class', 'woocommerce' ), 401 ); |
||
| 648 | } |
||
| 649 | |||
| 650 | update_option( 'woocommerce_tax_classes', implode( "\n", $classes ) ); |
||
| 651 | |||
| 652 | // Delete tax rate locations locations from the selected class. |
||
| 653 | $wpdb->query( $wpdb->prepare( " |
||
| 654 | DELETE locations.* |
||
| 655 | FROM {$wpdb->prefix}woocommerce_tax_rate_locations AS locations |
||
| 656 | INNER JOIN |
||
| 657 | {$wpdb->prefix}woocommerce_tax_rates AS rates |
||
| 658 | ON rates.tax_rate_id = locations.tax_rate_id |
||
| 659 | WHERE rates.tax_rate_class = '%s' |
||
| 660 | ", $slug ) ); |
||
| 661 | |||
| 662 | // Delete tax rates in the selected class. |
||
| 663 | $wpdb->delete( $wpdb->prefix . 'woocommerce_tax_rates', array( 'tax_rate_class' => $slug ), array( '%s' ) ); |
||
| 664 | |||
| 665 | return array( 'message' => sprintf( __( 'Deleted %s', 'woocommerce' ), 'tax_class' ) ); |
||
| 666 | } catch ( WC_API_Exception $e ) { |
||
| 667 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Get the total number of tax classes |
||
| 673 | * |
||
| 674 | * @since 2.5.0 |
||
| 675 | * |
||
| 676 | * @return array |
||
| 677 | */ |
||
| 678 | public function get_tax_classes_count() { |
||
| 679 | try { |
||
| 680 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 681 | throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_tax_classes_count', __( 'You do not have permission to read the tax classes count', 'woocommerce' ), 401 ); |
||
| 682 | } |
||
| 683 | |||
| 684 | $total = count( WC_Tax::get_tax_classes() ) + 1; // +1 for Standard Rate |
||
| 685 | |||
| 686 | return array( 'count' => $total ); |
||
| 687 | } catch ( WC_API_Exception $e ) { |
||
| 688 | return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | } |
||
| 692 |