woocommerce /
woocommerce-rest-api
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * REST API Tax Classes controller |
||||||
| 4 | * |
||||||
| 5 | * Handles requests to the /taxes/classes endpoint. |
||||||
| 6 | * |
||||||
| 7 | * @package Automattic/WooCommerce/RestApi |
||||||
| 8 | */ |
||||||
| 9 | |||||||
| 10 | namespace Automattic\WooCommerce\RestApi\Controllers\Version4; |
||||||
| 11 | |||||||
| 12 | defined( 'ABSPATH' ) || exit; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * REST API Tax Class controller class. |
||||||
| 16 | */ |
||||||
| 17 | class TaxClasses extends AbstractController { |
||||||
| 18 | |||||||
| 19 | /** |
||||||
| 20 | * Route base. |
||||||
| 21 | * |
||||||
| 22 | * @var string |
||||||
| 23 | */ |
||||||
| 24 | protected $rest_base = 'taxes/classes'; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * Permission to check. |
||||||
| 28 | * |
||||||
| 29 | * @var string |
||||||
| 30 | */ |
||||||
| 31 | protected $resource_type = 'settings'; |
||||||
| 32 | |||||||
| 33 | /** |
||||||
| 34 | * Register the routes for tax classes. |
||||||
| 35 | */ |
||||||
| 36 | public function register_routes() { |
||||||
| 37 | register_rest_route( |
||||||
| 38 | $this->namespace, |
||||||
| 39 | '/' . $this->rest_base, |
||||||
| 40 | array( |
||||||
| 41 | array( |
||||||
| 42 | 'methods' => \WP_REST_Server::READABLE, |
||||||
| 43 | 'callback' => array( $this, 'get_items' ), |
||||||
| 44 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||||||
| 45 | 'args' => $this->get_collection_params(), |
||||||
| 46 | ), |
||||||
| 47 | array( |
||||||
| 48 | 'methods' => \WP_REST_Server::CREATABLE, |
||||||
| 49 | 'callback' => array( $this, 'create_item' ), |
||||||
| 50 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||||||
| 51 | 'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ), |
||||||
| 52 | ), |
||||||
| 53 | 'schema' => array( $this, 'get_public_item_schema' ), |
||||||
| 54 | ), |
||||||
| 55 | true |
||||||
| 56 | ); |
||||||
| 57 | |||||||
| 58 | register_rest_route( |
||||||
| 59 | $this->namespace, |
||||||
| 60 | '/' . $this->rest_base . '/(?P<slug>\w[\w\s\-]*)', |
||||||
| 61 | array( |
||||||
| 62 | 'args' => array( |
||||||
| 63 | 'slug' => array( |
||||||
| 64 | 'description' => __( 'Unique slug for the resource.', 'woocommerce-rest-api' ), |
||||||
| 65 | 'type' => 'string', |
||||||
| 66 | ), |
||||||
| 67 | ), |
||||||
| 68 | array( |
||||||
| 69 | 'methods' => \WP_REST_Server::DELETABLE, |
||||||
| 70 | 'callback' => array( $this, 'delete_item' ), |
||||||
| 71 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||||||
| 72 | 'args' => array( |
||||||
| 73 | 'force' => array( |
||||||
| 74 | 'default' => false, |
||||||
| 75 | 'type' => 'boolean', |
||||||
| 76 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce-rest-api' ), |
||||||
| 77 | ), |
||||||
| 78 | ), |
||||||
| 79 | ), |
||||||
| 80 | 'schema' => array( $this, 'get_public_item_schema' ), |
||||||
| 81 | ), |
||||||
| 82 | true |
||||||
| 83 | ); |
||||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | /** |
||||||
| 87 | * Get all tax classes. |
||||||
| 88 | * |
||||||
| 89 | * @param \WP_REST_Request $request Request params. |
||||||
| 90 | * @return array |
||||||
| 91 | */ |
||||||
| 92 | public function get_items( $request ) { |
||||||
| 93 | $tax_classes = array(); |
||||||
| 94 | |||||||
| 95 | // Add standard class. |
||||||
| 96 | $tax_classes[] = array( |
||||||
| 97 | 'slug' => 'standard', |
||||||
| 98 | 'name' => __( 'Standard rate', 'woocommerce-rest-api' ), |
||||||
| 99 | ); |
||||||
| 100 | |||||||
| 101 | $classes = \WC_Tax::get_tax_classes(); |
||||||
| 102 | |||||||
| 103 | foreach ( $classes as $class ) { |
||||||
| 104 | $tax_classes[] = array( |
||||||
| 105 | 'slug' => sanitize_title( $class ), |
||||||
| 106 | 'name' => $class, |
||||||
| 107 | ); |
||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | $data = array(); |
||||||
| 111 | foreach ( $tax_classes as $tax_class ) { |
||||||
| 112 | $class = $this->prepare_item_for_response( $tax_class, $request ); |
||||||
| 113 | $class = $this->prepare_response_for_collection( $class ); |
||||||
| 114 | $data[] = $class; |
||||||
| 115 | } |
||||||
| 116 | |||||||
| 117 | return rest_ensure_response( $data ); |
||||||
| 118 | } |
||||||
| 119 | |||||||
| 120 | /** |
||||||
| 121 | * Create a single tax class. |
||||||
| 122 | * |
||||||
| 123 | * @param \WP_REST_Request $request Full details about the request. |
||||||
| 124 | * @return \WP_Error|\WP_REST_Response |
||||||
| 125 | */ |
||||||
| 126 | public function create_item( $request ) { |
||||||
| 127 | $tax_class = \WC_Tax::create_tax_class( $request['name'] ); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 128 | |||||||
| 129 | if ( is_wp_error( $tax_class ) ) { |
||||||
| 130 | return new \WP_Error( 'woocommerce_rest_' . $tax_class->get_error_code(), $tax_class->get_error_message(), array( 'status' => 400 ) ); |
||||||
| 131 | } |
||||||
| 132 | |||||||
| 133 | $this->update_additional_fields_for_object( $tax_class, $request ); |
||||||
| 134 | |||||||
| 135 | /** |
||||||
| 136 | * Fires after a tax class is created or updated via the REST API. |
||||||
| 137 | * |
||||||
| 138 | * @param \stdClass $tax_class Data used to create the tax class. |
||||||
| 139 | * @param \WP_REST_Request $request Request object. |
||||||
| 140 | * @param boolean $creating True when creating tax class, false when updating tax class. |
||||||
| 141 | */ |
||||||
| 142 | do_action( 'woocommerce_rest_insert_tax_class', (object) $tax_class, $request, true ); |
||||||
| 143 | |||||||
| 144 | $request->set_param( 'context', 'edit' ); |
||||||
| 145 | $response = $this->prepare_item_for_response( $tax_class, $request ); |
||||||
| 146 | $response = rest_ensure_response( $response ); |
||||||
| 147 | $response->set_status( 201 ); |
||||||
| 148 | $response->header( 'Location', rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $tax_class['slug'] ) ) ); |
||||||
| 149 | |||||||
| 150 | return $response; |
||||||
| 151 | } |
||||||
| 152 | |||||||
| 153 | /** |
||||||
| 154 | * Delete a single tax class. |
||||||
| 155 | * |
||||||
| 156 | * @param \WP_REST_Request $request Full details about the request. |
||||||
| 157 | * @return \WP_Error|\WP_REST_Response |
||||||
| 158 | */ |
||||||
| 159 | public function delete_item( $request ) { |
||||||
| 160 | global $wpdb; |
||||||
| 161 | |||||||
| 162 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
||||||
| 163 | |||||||
| 164 | // We don't support trashing for this type, error out. |
||||||
| 165 | if ( ! $force ) { |
||||||
| 166 | return new \WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Taxes do not support trashing.', 'woocommerce-rest-api' ), array( 'status' => 501 ) ); |
||||||
| 167 | } |
||||||
| 168 | |||||||
| 169 | $request->set_param( 'context', 'edit' ); |
||||||
| 170 | |||||||
| 171 | $tax_class = \WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) ); |
||||||
|
0 ignored issues
–
show
The method
get_tax_class_by() does not exist on WC_Tax. Did you maybe mean get_tax_classes()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 172 | $previous = $this->prepare_item_for_response( $tax_class, $request ); |
||||||
| 173 | $deleted = \WC_Tax::delete_tax_class_by( 'slug', sanitize_title( $request['slug'] ) ); |
||||||
|
0 ignored issues
–
show
The method
delete_tax_class_by() does not exist on WC_Tax.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 174 | |||||||
| 175 | if ( ! $deleted ) { |
||||||
| 176 | return new \WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce-rest-api' ), array( 'status' => 400 ) ); |
||||||
| 177 | } |
||||||
| 178 | |||||||
| 179 | if ( is_wp_error( $deleted ) ) { |
||||||
| 180 | return new \WP_Error( 'woocommerce_rest_' . $deleted->get_error_code(), $deleted->get_error_message(), array( 'status' => 400 ) ); |
||||||
| 181 | } |
||||||
| 182 | |||||||
| 183 | $response = new \WP_REST_Response(); |
||||||
| 184 | $response->set_data( |
||||||
| 185 | array( |
||||||
| 186 | 'deleted' => true, |
||||||
| 187 | 'previous' => $previous->get_data(), |
||||||
| 188 | ) |
||||||
| 189 | ); |
||||||
| 190 | |||||||
| 191 | /** |
||||||
| 192 | * Fires after a tax class is deleted via the REST API. |
||||||
| 193 | * |
||||||
| 194 | * @param \stdClass $tax_class The tax data. |
||||||
| 195 | * @param \WP_REST_Response $response The response returned from the API. |
||||||
| 196 | * @param \WP_REST_Request $request The request sent to the API. |
||||||
| 197 | */ |
||||||
| 198 | do_action( 'woocommerce_rest_delete_tax_class', (object) $tax_class, $response, $request ); |
||||||
| 199 | |||||||
| 200 | return $response; |
||||||
| 201 | } |
||||||
| 202 | |||||||
| 203 | /** |
||||||
| 204 | * Prepare links for the request. |
||||||
| 205 | * |
||||||
| 206 | * @param mixed $item Object to prepare. |
||||||
| 207 | * @param \WP_REST_Request $request Request object. |
||||||
| 208 | * @return array |
||||||
| 209 | */ |
||||||
| 210 | protected function prepare_links( $item, $request ) { |
||||||
| 211 | $links = array( |
||||||
| 212 | 'collection' => array( |
||||||
| 213 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||||||
| 214 | ), |
||||||
| 215 | ); |
||||||
| 216 | |||||||
| 217 | return $links; |
||||||
| 218 | } |
||||||
| 219 | |||||||
| 220 | /** |
||||||
| 221 | * Get the Tax Classes schema, conforming to JSON Schema |
||||||
| 222 | * |
||||||
| 223 | * @return array |
||||||
| 224 | */ |
||||||
| 225 | public function get_item_schema() { |
||||||
| 226 | $schema = array( |
||||||
| 227 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||||||
| 228 | 'title' => 'tax_class', |
||||||
| 229 | 'type' => 'object', |
||||||
| 230 | 'properties' => array( |
||||||
| 231 | 'slug' => array( |
||||||
| 232 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce-rest-api' ), |
||||||
| 233 | 'type' => 'string', |
||||||
| 234 | 'context' => array( 'view', 'edit' ), |
||||||
| 235 | 'readonly' => true, |
||||||
| 236 | ), |
||||||
| 237 | 'name' => array( |
||||||
| 238 | 'description' => __( 'Tax class name.', 'woocommerce-rest-api' ), |
||||||
| 239 | 'type' => 'string', |
||||||
| 240 | 'context' => array( 'view', 'edit' ), |
||||||
| 241 | 'required' => true, |
||||||
| 242 | 'arg_options' => array( |
||||||
| 243 | 'sanitize_callback' => 'sanitize_text_field', |
||||||
| 244 | ), |
||||||
| 245 | ), |
||||||
| 246 | ), |
||||||
| 247 | ); |
||||||
| 248 | |||||||
| 249 | return $this->add_additional_fields_schema( $schema ); |
||||||
| 250 | } |
||||||
| 251 | |||||||
| 252 | /** |
||||||
| 253 | * Get the query params for collections. |
||||||
| 254 | * |
||||||
| 255 | * @return array |
||||||
| 256 | */ |
||||||
| 257 | public function get_collection_params() { |
||||||
| 258 | return array( |
||||||
| 259 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||||||
| 260 | ); |
||||||
| 261 | } |
||||||
| 262 | } |
||||||
| 263 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.