woocommerce
/
includes
/
rest-api
/
Controllers
/
Version2
/
class-wc-rest-tax-classes-v2-controller.php
class-wc-rest-coupons-v2-controller.php
2 months ago
class-wc-rest-customer-downloads-v2-controller.php
5 years ago
class-wc-rest-customers-v2-controller.php
2 months ago
class-wc-rest-network-orders-v2-controller.php
1 year ago
class-wc-rest-order-notes-v2-controller.php
5 years ago
class-wc-rest-order-refunds-v2-controller.php
2 months ago
class-wc-rest-orders-v2-controller.php
1 month ago
class-wc-rest-payment-gateways-v2-controller.php
11 months ago
class-wc-rest-product-attribute-terms-v2-controller.php
5 years ago
class-wc-rest-product-attributes-v2-controller.php
5 years ago
class-wc-rest-product-brands-v2-controller.php
1 year ago
class-wc-rest-product-categories-v2-controller.php
3 months ago
class-wc-rest-product-reviews-v2-controller.php
4 years ago
class-wc-rest-product-shipping-classes-v2-controller.php
5 years ago
class-wc-rest-product-tags-v2-controller.php
5 years ago
class-wc-rest-product-variations-v2-controller.php
2 months ago
class-wc-rest-products-v2-controller.php
2 months ago
class-wc-rest-report-sales-v2-controller.php
5 years ago
class-wc-rest-report-top-sellers-v2-controller.php
5 years ago
class-wc-rest-reports-v2-controller.php
5 years ago
class-wc-rest-setting-options-v2-controller.php
2 months ago
class-wc-rest-settings-v2-controller.php
5 years ago
class-wc-rest-shipping-methods-v2-controller.php
1 year ago
class-wc-rest-shipping-zone-locations-v2-controller.php
1 year ago
class-wc-rest-shipping-zone-methods-v2-controller.php
1 year ago
class-wc-rest-shipping-zones-v2-controller.php
1 month ago
class-wc-rest-system-status-tools-v2-controller.php
1 month ago
class-wc-rest-system-status-v2-controller.php
1 month ago
class-wc-rest-tax-classes-v2-controller.php
1 year ago
class-wc-rest-taxes-v2-controller.php
5 years ago
class-wc-rest-webhook-deliveries-v2-controller.php
5 years ago
class-wc-rest-webhooks-v2-controller.php
1 year ago
class-wc-rest-tax-classes-v2-controller.php
112 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Tax Classes controller |
| 4 | * |
| 5 | * Handles requests to the /taxes/classes endpoint. |
| 6 | * |
| 7 | * @package WooCommerce\RestApi |
| 8 | * @since 2.6.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * REST API Tax Classes controller class. |
| 15 | * |
| 16 | * @package WooCommerce\RestApi |
| 17 | * @extends WC_REST_Tax_Classes_V1_Controller |
| 18 | */ |
| 19 | class WC_REST_Tax_Classes_V2_Controller extends WC_REST_Tax_Classes_V1_Controller { |
| 20 | |
| 21 | /** |
| 22 | * Endpoint namespace. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $namespace = 'wc/v2'; |
| 27 | |
| 28 | /** |
| 29 | * Register the routes for tax classes. |
| 30 | */ |
| 31 | public function register_routes() { |
| 32 | register_rest_route( |
| 33 | $this->namespace, |
| 34 | '/' . $this->rest_base, |
| 35 | array( |
| 36 | array( |
| 37 | 'methods' => WP_REST_Server::READABLE, |
| 38 | 'callback' => array( $this, 'get_items' ), |
| 39 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 40 | 'args' => $this->get_collection_params(), |
| 41 | ), |
| 42 | array( |
| 43 | 'methods' => WP_REST_Server::CREATABLE, |
| 44 | 'callback' => array( $this, 'create_item' ), |
| 45 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
| 46 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
| 47 | ), |
| 48 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | register_rest_route( |
| 53 | $this->namespace, |
| 54 | '/' . $this->rest_base . '/(?P<slug>\w[\w\s\-]*)', |
| 55 | array( |
| 56 | 'args' => array( |
| 57 | 'slug' => array( |
| 58 | 'description' => __( 'Unique slug for the resource.', 'woocommerce' ), |
| 59 | 'type' => 'string', |
| 60 | ), |
| 61 | ), |
| 62 | array( |
| 63 | 'methods' => WP_REST_Server::READABLE, |
| 64 | 'callback' => array( $this, 'get_item' ), |
| 65 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 66 | ), |
| 67 | array( |
| 68 | 'methods' => WP_REST_Server::DELETABLE, |
| 69 | 'callback' => array( $this, 'delete_item' ), |
| 70 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
| 71 | 'args' => array( |
| 72 | 'force' => array( |
| 73 | 'default' => false, |
| 74 | 'type' => 'boolean', |
| 75 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
| 76 | ), |
| 77 | ), |
| 78 | ), |
| 79 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get one tax class. |
| 86 | * |
| 87 | * @param WP_REST_Request $request Request object. |
| 88 | * @return array |
| 89 | */ |
| 90 | public function get_item( $request ) { |
| 91 | if ( 'standard' === $request['slug'] ) { |
| 92 | $tax_class = array( |
| 93 | 'slug' => 'standard', |
| 94 | 'name' => __( 'Standard rate', 'woocommerce' ), |
| 95 | ); |
| 96 | } else { |
| 97 | $tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) ); |
| 98 | } |
| 99 | |
| 100 | if ( ! $tax_class ) { |
| 101 | return new WP_Error( 'woocommerce_rest_tax_class_invalid_slug', __( 'Invalid slug.', 'woocommerce' ), array( 'status' => 404 ) ); |
| 102 | } |
| 103 | |
| 104 | $data = array(); |
| 105 | $class = $this->prepare_item_for_response( $tax_class, $request ); |
| 106 | $class = $this->prepare_response_for_collection( $class ); |
| 107 | $data[] = $class; |
| 108 | |
| 109 | return rest_ensure_response( $data ); |
| 110 | } |
| 111 | } |
| 112 |