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