Ei kuvausta

class-wc-rest-tax-classes-v2-controller.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Tax Classes controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Tax_Classes_V1_Controller
  16. */
  17. class WC_REST_Tax_Classes_V2_Controller extends WC_REST_Tax_Classes_V1_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Register the routes for tax classes.
  26. */
  27. public function register_routes() {
  28. register_rest_route(
  29. $this->namespace,
  30. '/' . $this->rest_base,
  31. array(
  32. array(
  33. 'methods' => WP_REST_Server::READABLE,
  34. 'callback' => array( $this, 'get_items' ),
  35. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  36. 'args' => $this->get_collection_params(),
  37. ),
  38. array(
  39. 'methods' => WP_REST_Server::CREATABLE,
  40. 'callback' => array( $this, 'create_item' ),
  41. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  42. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
  43. ),
  44. 'schema' => array( $this, 'get_public_item_schema' ),
  45. )
  46. );
  47. register_rest_route(
  48. $this->namespace,
  49. '/' . $this->rest_base . '/(?P<slug>\w[\w\s\-]*)',
  50. array(
  51. 'args' => array(
  52. 'slug' => array(
  53. 'description' => __( 'Unique slug for the resource.', 'woocommerce' ),
  54. 'type' => 'string',
  55. ),
  56. ),
  57. array(
  58. 'methods' => WP_REST_Server::READABLE,
  59. 'callback' => array( $this, 'get_item' ),
  60. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  61. ),
  62. array(
  63. 'methods' => WP_REST_Server::DELETABLE,
  64. 'callback' => array( $this, 'delete_item' ),
  65. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  66. 'args' => array(
  67. 'force' => array(
  68. 'default' => false,
  69. 'type' => 'boolean',
  70. 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
  71. ),
  72. ),
  73. ),
  74. 'schema' => array( $this, 'get_public_item_schema' ),
  75. )
  76. );
  77. }
  78. /**
  79. * Get one tax class.
  80. *
  81. * @param WP_REST_Request $request Request object.
  82. * @return array
  83. */
  84. public function get_item( $request ) {
  85. if ( 'standard' === $request['slug'] ) {
  86. $tax_class = array(
  87. 'slug' => 'standard',
  88. 'name' => __( 'Standard rate', 'woocommerce' ),
  89. );
  90. } else {
  91. $tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
  92. }
  93. $data = array();
  94. if ( $tax_class ) {
  95. $class = $this->prepare_item_for_response( $tax_class, $request );
  96. $class = $this->prepare_response_for_collection( $class );
  97. $data[] = $class;
  98. }
  99. return rest_ensure_response( $data );
  100. }
  101. }