Geen omschrijving

class-wc-rest-taxes-controller.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * REST API Taxes controller
  4. *
  5. * Handles requests to the /taxes endpoint.
  6. *
  7. * @package WooCommerce\RestApi
  8. * @since 2.6.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * REST API Taxes controller class.
  13. *
  14. * @package WooCommerce\RestApi
  15. * @extends WC_REST_Taxes_V2_Controller
  16. */
  17. class WC_REST_Taxes_Controller extends WC_REST_Taxes_V2_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v3';
  24. /**
  25. * Add tax rate locales to the response array.
  26. *
  27. * @param array $data Response data.
  28. * @param stdClass $tax Tax object.
  29. *
  30. * @return array
  31. */
  32. protected function add_tax_rate_locales( $data, $tax ) {
  33. global $wpdb;
  34. $data = parent::add_tax_rate_locales( $data, $tax );
  35. $data['postcodes'] = array();
  36. $data['cities'] = array();
  37. // Get locales from a tax rate.
  38. $locales = $wpdb->get_results(
  39. $wpdb->prepare(
  40. "
  41. SELECT location_code, location_type
  42. FROM {$wpdb->prefix}woocommerce_tax_rate_locations
  43. WHERE tax_rate_id = %d
  44. ",
  45. $tax->tax_rate_id
  46. )
  47. );
  48. if ( ! is_wp_error( $tax ) && ! is_null( $tax ) ) {
  49. foreach ( $locales as $locale ) {
  50. if ( 'postcode' === $locale->location_type ) {
  51. $data['postcodes'][] = $locale->location_code;
  52. } elseif ( 'city' === $locale->location_type ) {
  53. $data['cities'][] = $locale->location_code;
  54. }
  55. }
  56. }
  57. return $data;
  58. }
  59. /**
  60. * Get the taxes schema, conforming to JSON Schema.
  61. *
  62. * @return array
  63. */
  64. public function get_item_schema() {
  65. $schema = parent::get_item_schema();
  66. $schema['properties']['postcodes'] = array(
  67. 'description' => __( 'List of postcodes / ZIPs. Introduced in WooCommerce 5.3.', 'woocommerce' ),
  68. 'type' => 'array',
  69. 'items' => array(
  70. 'type' => 'string',
  71. ),
  72. 'context' => array( 'view', 'edit' ),
  73. );
  74. $schema['properties']['cities'] = array(
  75. 'description' => __( 'List of city names. Introduced in WooCommerce 5.3.', 'woocommerce' ),
  76. 'type' => 'array',
  77. 'items' => array(
  78. 'type' => 'string',
  79. ),
  80. 'context' => array( 'view', 'edit' ),
  81. );
  82. $schema['properties']['postcode']['description'] =
  83. __( "Postcode/ZIP, it doesn't support multiple values. Deprecated as of WooCommerce 5.3, 'postcodes' should be used instead.", 'woocommerce' );
  84. $schema['properties']['city']['description'] =
  85. __( "City name, it doesn't support multiple values. Deprecated as of WooCommerce 5.3, 'cities' should be used instead.", 'woocommerce' );
  86. return $schema;
  87. }
  88. /**
  89. * Create a single tax.
  90. *
  91. * @param WP_REST_Request $request Full details about the request.
  92. * @return WP_Error|WP_REST_Response The response, or an error.
  93. */
  94. public function create_item( $request ) {
  95. $this->adjust_cities_and_postcodes( $request );
  96. return parent::create_item( $request );
  97. }
  98. /**
  99. * Update a single tax.
  100. *
  101. * @param WP_REST_Request $request Full details about the request.
  102. * @return WP_Error|WP_REST_Response The response, or an error.
  103. */
  104. public function update_item( $request ) {
  105. $this->adjust_cities_and_postcodes( $request );
  106. return parent::update_item( $request );
  107. }
  108. /**
  109. * Convert array "cities" and "postcodes" parameters
  110. * into semicolon-separated strings "city" and "postcode".
  111. *
  112. * @param WP_REST_Request $request The request to adjust.
  113. */
  114. private function adjust_cities_and_postcodes( &$request ) {
  115. if ( isset( $request['cities'] ) ) {
  116. $request['city'] = join( ';', $request['cities'] );
  117. }
  118. if ( isset( $request['postcodes'] ) ) {
  119. $request['postcode'] = join( ';', $request['postcodes'] );
  120. }
  121. }
  122. }