|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API WC Shipping Methods controller |
|
4
|
|
|
* |
|
5
|
|
|
* Handles requests to the /shipping_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
|
|
|
* @package WooCommerce/API |
|
19
|
|
|
* @extends WC_REST_Controller |
|
20
|
|
|
*/ |
|
21
|
|
|
class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Endpoint namespace. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $namespace = 'wc/v1'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Route base. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $rest_base = 'shipping_methods'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Register the route for /shipping_methods and /shipping_methods/<method> |
|
39
|
|
|
*/ |
|
40
|
|
View Code Duplication |
public function register_routes() { |
|
|
|
|
|
|
41
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
|
42
|
|
|
array( |
|
43
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
44
|
|
|
'callback' => array( $this, 'get_items' ), |
|
45
|
|
|
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
46
|
|
|
'args' => $this->get_collection_params(), |
|
47
|
|
|
), |
|
48
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
49
|
|
|
) ); |
|
50
|
|
|
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w-]+)', array( |
|
51
|
|
|
array( |
|
52
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
53
|
|
|
'callback' => array( $this, 'get_item' ), |
|
54
|
|
|
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
|
55
|
|
|
'args' => array( |
|
56
|
|
|
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
57
|
|
|
), |
|
58
|
|
|
), |
|
59
|
|
|
'schema' => array( $this, 'get_public_item_schema' ), |
|
60
|
|
|
) ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Check whether a given request has permission to view system status. |
|
65
|
|
|
* |
|
66
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
67
|
|
|
* @return WP_Error|boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get_items_permissions_check( $request ) { |
|
70
|
|
|
if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) { |
|
71
|
|
|
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
72
|
|
|
} |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check if a given request has access to read a shipping method. |
|
78
|
|
|
* |
|
79
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
80
|
|
|
* @return WP_Error|boolean |
|
81
|
|
|
*/ |
|
82
|
|
|
public function get_item_permissions_check( $request ) { |
|
83
|
|
|
if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) { |
|
84
|
|
|
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
|
85
|
|
|
} |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get shipping methods. |
|
91
|
|
|
* |
|
92
|
|
|
* @param WP_REST_Request $request Full details about the request. |
|
93
|
|
|
* @return WP_Error|WP_REST_Response |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function get_items( $request ) { |
|
|
|
|
|
|
96
|
|
|
$wc_shipping = WC_Shipping::instance(); |
|
97
|
|
|
$response = array(); |
|
98
|
|
|
foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) { |
|
|
|
|
|
|
99
|
|
|
$method = $this->prepare_item_for_response( $shipping_method, $request ); |
|
100
|
|
|
$method = $this->prepare_response_for_collection( $method ); |
|
101
|
|
|
$response[] = $method; |
|
102
|
|
|
} |
|
103
|
|
|
return rest_ensure_response( $response ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get a single Shipping Method. |
|
108
|
|
|
* |
|
109
|
|
|
* @param WP_REST_Request $request |
|
110
|
|
|
* @return WP_REST_Response|WP_Error |
|
111
|
|
|
*/ |
|
112
|
|
|
public function get_item( $request ) { |
|
113
|
|
|
$wc_shipping = WC_Shipping::instance(); |
|
114
|
|
|
$methods = $wc_shipping->get_shipping_methods(); |
|
115
|
|
|
if ( empty ( $methods[ $request['id'] ] ) ) { |
|
116
|
|
|
return new WP_Error( 'woocommerce_rest_shipping_method_invalid', __( "Resource doesn't exist.", 'woocommerce' ), array( 'status' => 404 ) ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$method = $methods[ $request['id'] ]; |
|
120
|
|
|
$response = $this->prepare_item_for_response( $method, $request ); |
|
121
|
|
|
|
|
122
|
|
|
return rest_ensure_response( $response ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Prepare a shipping method for response. |
|
127
|
|
|
* |
|
128
|
|
|
* @param WP_Comment $method Shipping method object. |
|
129
|
|
|
* @param WP_REST_Request $request Request object. |
|
130
|
|
|
* @return WP_REST_Response $response Response data. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function prepare_item_for_response( $method, $request ) { |
|
133
|
|
|
$data = array( |
|
134
|
|
|
'id' => $method->id, |
|
135
|
|
|
'title' => $method->method_title, |
|
136
|
|
|
'description' => $method->method_description, |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
140
|
|
|
$data = $this->add_additional_fields_to_object( $data, $request ); |
|
141
|
|
|
$data = $this->filter_response_by_context( $data, $context ); |
|
142
|
|
|
|
|
143
|
|
|
// Wrap the data in a response object. |
|
144
|
|
|
$response = rest_ensure_response( $data ); |
|
145
|
|
|
|
|
146
|
|
|
$response->add_links( $this->prepare_links( $method, $request ) ); |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Filter product reviews object returned from the REST API. |
|
150
|
|
|
* |
|
151
|
|
|
* @param WP_REST_Response $response The response object. |
|
152
|
|
|
* @param Object $method Product review object used to create response. |
|
153
|
|
|
* @param WP_REST_Request $request Request object. |
|
154
|
|
|
*/ |
|
155
|
|
|
return apply_filters( 'woocommerce_rest_prepare_shipping_method', $response, $method, $request ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Prepare links for the request. |
|
160
|
|
|
* |
|
161
|
|
|
* @param Object $method Shipping method object. |
|
162
|
|
|
* @param WP_REST_Request $request Request object. |
|
163
|
|
|
* @return array Links for the given product review. |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function prepare_links( $method, $request ) { |
|
|
|
|
|
|
166
|
|
|
$links = array( |
|
167
|
|
|
'self' => array( |
|
168
|
|
|
'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $method->id ) ), |
|
169
|
|
|
), |
|
170
|
|
|
'collection' => array( |
|
171
|
|
|
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
|
172
|
|
|
), |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
return $links; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get the shipping method schema, conforming to JSON Schema. |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
View Code Duplication |
public function get_item_schema() { |
|
|
|
|
|
|
184
|
|
|
$schema = array( |
|
185
|
|
|
'$schema' => 'http://json-schema.org/draft-04/schema#', |
|
186
|
|
|
'title' => 'shipping_method', |
|
187
|
|
|
'type' => 'object', |
|
188
|
|
|
'properties' => array( |
|
189
|
|
|
'id' => array( |
|
190
|
|
|
'description' => __( 'Method ID.', 'woocommerce' ), |
|
191
|
|
|
'type' => 'string', |
|
192
|
|
|
'context' => array( 'view' ), |
|
193
|
|
|
), |
|
194
|
|
|
'title' => array( |
|
195
|
|
|
'description' => __( 'Shipping method title.', 'woocommerce' ), |
|
196
|
|
|
'type' => 'string', |
|
197
|
|
|
'context' => array( 'view' ), |
|
198
|
|
|
), |
|
199
|
|
|
'description' => array( |
|
200
|
|
|
'description' => __( 'Shipping method description.', 'woocommerce' ), |
|
201
|
|
|
'type' => 'string', |
|
202
|
|
|
'context' => array( 'view' ), |
|
203
|
|
|
), |
|
204
|
|
|
), |
|
205
|
|
|
); |
|
206
|
|
|
|
|
207
|
|
|
return $this->add_additional_fields_schema( $schema ); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get any query params needed. |
|
212
|
|
|
* |
|
213
|
|
|
* @return array |
|
214
|
|
|
*/ |
|
215
|
|
|
public function get_collection_params() { |
|
216
|
|
|
return array( |
|
217
|
|
|
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|
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.