1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* REST API Shipping Zone Methods controller |
4
|
|
|
* |
5
|
|
|
* Handles requests to the /shipping/zones/<id>/methods endpoint. |
6
|
|
|
* |
7
|
|
|
* @author WooThemes |
8
|
|
|
* @category API |
9
|
|
|
* @package WooCommerce/API |
10
|
|
|
* @since 2.7.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* REST API Shipping Zone Methods class. |
19
|
|
|
* |
20
|
|
|
* @package WooCommerce/API |
21
|
|
|
* @extends WC_REST_Shipping_Zones_Controller_Base |
22
|
|
|
*/ |
23
|
|
|
class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Controller_Base { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Register the routes for Shipping Zone Methods. |
27
|
|
|
*/ |
28
|
|
|
public function register_routes() { |
29
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<zone_id>[\d-]+)/methods', array( |
30
|
|
|
array( |
31
|
|
|
'methods' => WP_REST_Server::READABLE, |
32
|
|
|
'callback' => array( $this, 'get_items' ), |
33
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
34
|
|
|
), |
35
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
36
|
|
|
) ); |
37
|
|
|
|
38
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<zone_id>[\d-]+)/methods/(?P<instance_id>[\d-]+)', array( |
39
|
|
|
array( |
40
|
|
|
'methods' => WP_REST_Server::READABLE, |
41
|
|
|
'callback' => array( $this, 'get_item' ), |
42
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
43
|
|
|
), |
44
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
45
|
|
|
) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get a single Shipping Zone Method. |
50
|
|
|
* |
51
|
|
|
* @param WP_REST_Request $request |
52
|
|
|
* @return WP_REST_Response|WP_Error |
53
|
|
|
*/ |
54
|
|
|
public function get_item( $request ) { |
55
|
|
|
$zone = $this->get_zone( $request['zone_id'] ); |
56
|
|
|
|
57
|
|
|
if ( is_wp_error( $zone ) ) { |
58
|
|
|
return $zone; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$instance_id = (int) $request['instance_id']; |
62
|
|
|
$methods = $zone->get_shipping_methods(); |
63
|
|
|
$method = false; |
64
|
|
|
|
65
|
|
|
foreach ( $methods as $method_obj ) { |
66
|
|
|
if ( $instance_id === $method_obj->instance_id ) { |
67
|
|
|
$method = $method_obj; |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ( false === $method ) { |
73
|
|
|
return new WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$data = $this->prepare_item_for_response( $method, $request ); |
77
|
|
|
|
78
|
|
|
return rest_ensure_response( $data ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all Shipping Zone Methods. |
83
|
|
|
* |
84
|
|
|
* @param WP_REST_Request $request |
85
|
|
|
* @return WP_REST_Response|WP_Error |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function get_items( $request ) { |
|
|
|
|
88
|
|
|
$zone = $this->get_zone( $request['zone_id'] ); |
89
|
|
|
|
90
|
|
|
if ( is_wp_error( $zone ) ) { |
91
|
|
|
return $zone; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$methods = $zone->get_shipping_methods(); |
95
|
|
|
$data = array(); |
96
|
|
|
|
97
|
|
|
foreach ( $methods as $method_obj ) { |
98
|
|
|
$method = $this->prepare_item_for_response( $method_obj, $request ); |
99
|
|
|
$data[] = $method; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return rest_ensure_response( $data ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Prepare the Shipping Zone Method for the REST response. |
107
|
|
|
* |
108
|
|
|
* @param array $item Shipping Zone Method. |
109
|
|
|
* @param WP_REST_Request $request Request object. |
110
|
|
|
* @return WP_REST_Response $response |
111
|
|
|
*/ |
112
|
|
|
public function prepare_item_for_response( $item, $request ) { |
113
|
|
|
$method = array( |
114
|
|
|
'instance_id' => $item->instance_id, |
115
|
|
|
'title' => $item->instance_settings['title'], |
116
|
|
|
'order' => $item->method_order, |
117
|
|
|
'enabled' => ( 'yes' === $item->enabled ), |
118
|
|
|
'method_id' => $item->id, |
119
|
|
|
'method_title' => $item->method_title, |
120
|
|
|
'method_description' => $item->method_description, |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$context = empty( $request['context'] ) ? 'view' : $request['context']; |
124
|
|
|
$data = $this->add_additional_fields_to_object( $method, $request ); |
125
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
126
|
|
|
|
127
|
|
|
// Wrap the data in a response object. |
128
|
|
|
$response = rest_ensure_response( $data ); |
129
|
|
|
|
130
|
|
|
$response->add_links( $this->prepare_links( $request['zone_id'], $item->instance_id ) ); |
131
|
|
|
|
132
|
|
|
$response = $this->prepare_response_for_collection( $response ); |
133
|
|
|
|
134
|
|
|
return $response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Prepare links for the request. |
139
|
|
|
* |
140
|
|
|
* @param int $zone_id Given Shipping Zone ID. |
141
|
|
|
* @param int $instance_id Given Shipping Zone Method Instance ID. |
142
|
|
|
* @return array Links for the given Shipping Zone Method. |
143
|
|
|
*/ |
144
|
|
|
protected function prepare_links( $zone_id, $instance_id ) { |
145
|
|
|
$base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id; |
146
|
|
|
$links = array( |
147
|
|
|
'self' => array( |
148
|
|
|
'href' => rest_url( $base . '/methods/' . $instance_id ), |
149
|
|
|
), |
150
|
|
|
'collection' => array( |
151
|
|
|
'href' => rest_url( $base . '/methods' ), |
152
|
|
|
), |
153
|
|
|
'describes' => array( |
154
|
|
|
'href' => rest_url( $base ), |
155
|
|
|
), |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
return $links; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the Shipping Zone Methods schema, conforming to JSON Schema |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function get_item_schema() { |
167
|
|
|
$schema = array( |
168
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
169
|
|
|
'title' => 'shipping_zone_method', |
170
|
|
|
'type' => 'object', |
171
|
|
|
'properties' => array( |
172
|
|
|
'instance_id' => array( |
173
|
|
|
'description' => __( 'Shipping method instance ID.', 'woocommerce' ), |
174
|
|
|
'type' => 'integer', |
175
|
|
|
'context' => array( 'view' ), |
176
|
|
|
), |
177
|
|
|
'title' => array( |
178
|
|
|
'description' => __( 'Shipping method customer facing title.', 'woocommerce' ), |
179
|
|
|
'type' => 'string', |
180
|
|
|
'context' => array( 'view' ), |
181
|
|
|
), |
182
|
|
|
'order' => array( |
183
|
|
|
'description' => __( 'Shipping method sort order.', 'woocommerce' ), |
184
|
|
|
'type' => 'integer', |
185
|
|
|
'context' => array( 'view', 'edit' ), |
186
|
|
|
'required' => false, |
187
|
|
|
'arg_options' => array( |
188
|
|
|
'sanitize_callback' => 'absint', |
189
|
|
|
), |
190
|
|
|
), |
191
|
|
|
'enabled' => array( |
192
|
|
|
'description' => __( 'Shipping method enabled status.', 'woocommerce' ), |
193
|
|
|
'type' => 'boolean', |
194
|
|
|
'context' => array( 'view', 'edit' ), |
195
|
|
|
'required' => false, |
196
|
|
|
), |
197
|
|
|
'method_id' => array( |
198
|
|
|
'description' => __( 'Shipping method ID.', 'woocommerce' ), |
199
|
|
|
'type' => 'string', |
200
|
|
|
'context' => array( 'view' ), |
201
|
|
|
), |
202
|
|
|
'method_title' => array( |
203
|
|
|
'description' => __( 'Shipping method title.', 'woocommerce' ), |
204
|
|
|
'type' => 'string', |
205
|
|
|
'context' => array( 'view' ), |
206
|
|
|
), |
207
|
|
|
'method_description' => array( |
208
|
|
|
'description' => __( 'Shipping method description.', 'woocommerce' ), |
209
|
|
|
'type' => 'string', |
210
|
|
|
'context' => array( 'view' ), |
211
|
|
|
), |
212
|
|
|
), |
213
|
|
|
); |
214
|
|
|
|
215
|
|
|
return $this->add_additional_fields_schema( $schema ); |
216
|
|
|
} |
217
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.