|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API Shipping Zone Locations controller |
|
4
|
|
|
* |
|
5
|
|
|
* Handles requests to the /shipping/zones/<id>/locations 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 Shipping Zone Locations class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ShippingZoneLocations extends AbstractShippingZonesController { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Register the routes for Shipping Zone Locations. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function register_routes() { |
|
23
|
|
|
register_rest_route( |
|
24
|
|
|
$this->namespace, |
|
25
|
|
|
'/' . $this->rest_base . '/(?P<id>[\d]+)/locations', |
|
26
|
|
|
array( |
|
27
|
|
|
'args' => array( |
|
28
|
|
|
'id' => array( |
|
29
|
|
|
'description' => __( 'Unique ID for the resource.', 'woocommerce-rest-api' ), |
|
30
|
|
|
'type' => 'integer', |
|
31
|
|
|
), |
|
32
|
|
|
), |
|
33
|
|
|
array( |
|
34
|
|
|
'methods' => \WP_REST_Server::READABLE, |
|
35
|
|
|
'callback' => array( $this, 'get_items' ), |
|
36
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
37
|
|
|
), |
|
38
|
|
|
array( |
|
39
|
|
|
'methods' => \WP_REST_Server::EDITABLE, |
|
40
|
|
|
'callback' => array( $this, 'update_items' ), |
|
41
|
|
|
'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
42
|
|
|
'args' => $this->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
|
43
|
|
|
), |
|
44
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
45
|
|
|
), |
|
46
|
|
|
true |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get all Shipping Zone Locations. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \WP_REST_Request $request Request data. |
|
54
|
|
|
* @return \WP_REST_Response|\WP_Error |
|
55
|
|
|
*/ |
|
56
|
|
|
public function get_items( $request ) { |
|
57
|
|
|
$zone = $this->get_zone( (int) $request['id'] ); |
|
58
|
|
|
|
|
59
|
|
|
if ( is_wp_error( $zone ) ) { |
|
60
|
|
|
return $zone; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$locations = $zone->get_zone_locations(); |
|
|
|
|
|
|
64
|
|
|
$data = array(); |
|
65
|
|
|
|
|
66
|
|
|
foreach ( $locations as $location_obj ) { |
|
67
|
|
|
$location = $this->prepare_item_for_response( (array) $location_obj, $request ); |
|
68
|
|
|
$location = $this->prepare_response_for_collection( $location ); |
|
69
|
|
|
$data[] = $location; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return rest_ensure_response( $data ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Update all Shipping Zone Locations. |
|
77
|
|
|
* |
|
78
|
|
|
* @param \WP_REST_Request $request Request data. |
|
79
|
|
|
* @return \WP_REST_Response|\WP_Error |
|
80
|
|
|
*/ |
|
81
|
|
|
public function update_items( $request ) { |
|
82
|
|
|
$zone = $this->get_zone( (int) $request['id'] ); |
|
83
|
|
|
|
|
84
|
|
|
if ( is_wp_error( $zone ) ) { |
|
85
|
|
|
return $zone; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ( 0 === $zone->get_id() ) { |
|
|
|
|
|
|
89
|
|
|
return new \WP_Error( 'woocommerce_rest_shipping_zone_locations_invalid_zone', __( 'The "locations not covered by your other zones" zone cannot be updated.', 'woocommerce-rest-api' ), array( 'status' => 403 ) ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$raw_locations = $request->get_json_params(); |
|
93
|
|
|
$locations = array(); |
|
94
|
|
|
|
|
95
|
|
|
foreach ( (array) $raw_locations as $raw_location ) { |
|
96
|
|
|
if ( empty( $raw_location['code'] ) ) { |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$type = ! empty( $raw_location['type'] ) ? sanitize_text_field( $raw_location['type'] ) : 'country'; |
|
101
|
|
|
|
|
102
|
|
|
if ( ! in_array( $type, array( 'postcode', 'state', 'country', 'continent' ), true ) ) { |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$locations[] = array( |
|
107
|
|
|
'code' => sanitize_text_field( $raw_location['code'] ), |
|
108
|
|
|
'type' => sanitize_text_field( $type ), |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$zone->set_locations( $locations ); |
|
|
|
|
|
|
113
|
|
|
$zone->save(); |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
return $this->get_items( $request ); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Prepare links for the request. |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $item Object to prepare. |
|
122
|
|
|
* @param \WP_REST_Request $request Request object. |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function prepare_links( $item, $request ) { |
|
126
|
|
|
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['id']; |
|
127
|
|
|
$links = array( |
|
128
|
|
|
'collection' => array( |
|
129
|
|
|
'href' => rest_url( $base . '/locations' ), |
|
130
|
|
|
), |
|
131
|
|
|
'describes' => array( |
|
132
|
|
|
'href' => rest_url( $base ), |
|
133
|
|
|
), |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
return $links; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the Shipping Zone Locations schema, conforming to JSON Schema |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function get_item_schema() { |
|
145
|
|
|
$schema = array( |
|
146
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
147
|
|
|
'title' => 'shipping_zone_location', |
|
148
|
|
|
'type' => 'object', |
|
149
|
|
|
'properties' => array( |
|
150
|
|
|
'code' => array( |
|
151
|
|
|
'description' => __( 'Shipping zone location code.', 'woocommerce-rest-api' ), |
|
152
|
|
|
'type' => 'string', |
|
153
|
|
|
'context' => array( 'view', 'edit' ), |
|
154
|
|
|
), |
|
155
|
|
|
'type' => array( |
|
156
|
|
|
'description' => __( 'Shipping zone location type.', 'woocommerce-rest-api' ), |
|
157
|
|
|
'type' => 'string', |
|
158
|
|
|
'default' => 'country', |
|
159
|
|
|
'enum' => array( |
|
160
|
|
|
'postcode', |
|
161
|
|
|
'state', |
|
162
|
|
|
'country', |
|
163
|
|
|
'continent', |
|
164
|
|
|
), |
|
165
|
|
|
'context' => array( 'view', 'edit' ), |
|
166
|
|
|
), |
|
167
|
|
|
), |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
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.