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.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / StoreApi / Routes / V1 / AbstractRoute.php
woocommerce / src / StoreApi / Routes / V1 Last commit date
AI 1 year ago Agentic 4 months ago AbstractCartRoute.php 4 months ago AbstractRoute.php 3 months ago AbstractTermsRoute.php 4 months ago Batch.php 4 months ago Cart.php 1 year ago CartAddItem.php 4 months ago CartApplyCoupon.php 1 year ago CartCoupons.php 3 months ago CartCouponsByCode.php 1 year ago CartExtensions.php 2 years ago CartItems.php 2 years ago CartItemsByKey.php 2 years ago CartRemoveCoupon.php 1 year ago CartRemoveItem.php 4 months ago CartSelectShippingRate.php 5 months ago CartUpdateCustomer.php 4 months ago CartUpdateItem.php 4 months ago Checkout.php 1 month ago CheckoutOrder.php 1 year ago Order.php 7 months ago Patterns.php 1 year ago ProductAttributeTerms.php 1 month ago ProductAttributes.php 1 year ago ProductAttributesById.php 1 year ago ProductBrands.php 1 year ago ProductBrandsById.php 1 year ago ProductCategories.php 1 year ago ProductCategoriesById.php 1 year ago ProductCollectionData.php 11 months ago ProductReviews.php 4 months ago ProductTags.php 1 year ago Products.php 3 months ago ProductsById.php 3 months ago ProductsBySlug.php 3 months ago ShopperListItems.php 1 month ago ShopperListItemsByKey.php 1 month ago ShopperLists.php 1 month ago ShopperListsBySlug.php 1 month ago ShopperListsNonceCheck.php 1 month ago
AbstractRoute.php
336 lines
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Routes\V1;
3
4 use Automattic\WooCommerce\StoreApi\SchemaController;
5 use Automattic\WooCommerce\StoreApi\Routes\RouteInterface;
6 use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
7 use Automattic\WooCommerce\StoreApi\Exceptions\InvalidCartException;
8 use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema;
9 use WP_Error;
10
11 /**
12 * AbstractRoute class.
13 */
14 abstract class AbstractRoute implements RouteInterface {
15 /**
16 * Schema class instance.
17 *
18 * @var AbstractSchema
19 */
20 protected $schema;
21
22 /**
23 * Route namespace.
24 *
25 * @var string
26 */
27 protected $namespace = 'wc/store/v1';
28
29 /**
30 * Schema Controller instance.
31 *
32 * @var SchemaController
33 */
34 protected $schema_controller;
35
36 /**
37 * The routes schema.
38 *
39 * @var string
40 */
41 const SCHEMA_TYPE = '';
42
43 /**
44 * The routes schema version.
45 *
46 * @var integer
47 */
48 const SCHEMA_VERSION = 1;
49
50 /**
51 * Constructor.
52 *
53 * @param SchemaController $schema_controller Schema Controller instance.
54 * @param AbstractSchema $schema Schema class for this route.
55 */
56 public function __construct( SchemaController $schema_controller, AbstractSchema $schema ) {
57 $this->schema_controller = $schema_controller;
58 $this->schema = $schema;
59 }
60
61 /**
62 * Get the namespace for this route.
63 *
64 * @return string
65 */
66 public function get_namespace() {
67 return $this->namespace;
68 }
69
70 /**
71 * Set the namespace for this route.
72 *
73 * @param string $namespace Given namespace.
74 */
75 public function set_namespace( $namespace ) {
76 $this->namespace = $namespace;
77 }
78
79 /**
80 * Get item schema properties.
81 *
82 * @return array
83 */
84 public function get_item_schema() {
85 return $this->schema->get_item_schema();
86 }
87
88 /**
89 * Get the route response based on the type of request.
90 *
91 * @param \WP_REST_Request $request Request object.
92 * @return \WP_REST_Response
93 */
94 public function get_response( \WP_REST_Request $request ) {
95 $response = null;
96
97 try {
98 $response = $this->get_response_by_request_method( $request );
99 } catch ( RouteException $error ) {
100 $response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() );
101 } catch ( InvalidCartException $error ) {
102 $response = $this->get_route_error_response_from_object( $error->getError(), $error->getCode(), $error->getAdditionalData() );
103 } catch ( \Exception $error ) {
104 $response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 );
105 }
106
107 return is_wp_error( $response ) ? $this->error_to_response( $response ) : $response;
108 }
109
110 /**
111 * Get the route response based on the type of request.
112 *
113 * @param \WP_REST_Request $request Request object.
114 * @return \WP_REST_Response
115 */
116 protected function get_response_by_request_method( \WP_REST_Request $request ) {
117 switch ( $request->get_method() ) {
118 case 'POST':
119 return $this->get_route_post_response( $request );
120 case 'PUT':
121 case 'PATCH':
122 return $this->get_route_update_response( $request );
123 case 'DELETE':
124 return $this->get_route_delete_response( $request );
125 }
126 return $this->get_route_response( $request );
127 }
128
129 /**
130 * Converts an error to a response object. Based on \WP_REST_Server.
131 *
132 * @param \WP_Error $error WP_Error instance.
133 * @return \WP_REST_Response List of associative arrays with code and message keys.
134 */
135 protected function error_to_response( $error ) {
136 $error_data = $error->get_error_data();
137 $status = isset( $error_data, $error_data['status'] ) ? $error_data['status'] : 500;
138 $errors = [];
139
140 foreach ( (array) $error->errors as $code => $messages ) {
141 foreach ( (array) $messages as $message ) {
142 $errors[] = array(
143 'code' => $code,
144 'message' => $message,
145 'data' => $error->get_error_data( $code ),
146 );
147 }
148 }
149
150 $data = array_shift( $errors );
151
152 if ( count( $errors ) ) {
153 $data['additional_errors'] = $errors;
154 }
155
156 return new \WP_REST_Response( $data, $status );
157 }
158
159 /**
160 * Get route response for GET requests.
161 *
162 * When implemented, should return a \WP_REST_Response.
163 *
164 * @throws RouteException On error.
165 * @param \WP_REST_Request $request Request object.
166 * @return \WP_REST_Response
167 */
168 protected function get_route_response( \WP_REST_Request $request ) {
169 return $this->get_route_error_response( 'woocommerce_rest_invalid_endpoint', __( 'Method not implemented', 'woocommerce' ), 404 );
170 }
171
172 /**
173 * Get route response for POST requests.
174 *
175 * When implemented, should return a \WP_REST_Response.
176 *
177 * @throws RouteException On error.
178 * @param \WP_REST_Request $request Request object.
179 * @return \WP_REST_Response
180 */
181 protected function get_route_post_response( \WP_REST_Request $request ) {
182 return $this->get_route_error_response( 'woocommerce_rest_invalid_endpoint', __( 'Method not implemented', 'woocommerce' ), 404 );
183 }
184
185 /**
186 * Get route response for PUT requests.
187 *
188 * When implemented, should return a \WP_REST_Response.
189 *
190 * @throws RouteException On error.
191 * @param \WP_REST_Request $request Request object.
192 * @return \WP_REST_Response
193 */
194 protected function get_route_update_response( \WP_REST_Request $request ) {
195 return $this->get_route_error_response( 'woocommerce_rest_invalid_endpoint', __( 'Method not implemented', 'woocommerce' ), 404 );
196 }
197
198 /**
199 * Get route response for DELETE requests.
200 *
201 * When implemented, should return a \WP_REST_Response.
202 *
203 * @throws RouteException On error.
204 * @param \WP_REST_Request $request Request object.
205 * @return \WP_REST_Response
206 */
207 protected function get_route_delete_response( \WP_REST_Request $request ) {
208 return $this->get_route_error_response( 'woocommerce_rest_invalid_endpoint', __( 'Method not implemented', 'woocommerce' ), 404 );
209 }
210
211 /**
212 * Get route response when something went wrong.
213 *
214 * @param string $error_code String based error code.
215 * @param string $error_message User facing error message.
216 * @param int $http_status_code HTTP status. Defaults to 500.
217 * @param array $additional_data Extra data (key value pairs) to expose in the error response.
218 * @return \WP_Error WP Error object.
219 */
220 protected function get_route_error_response( $error_code, $error_message, $http_status_code = 500, $additional_data = [] ) {
221 return new \WP_Error( $error_code, $error_message, array_merge( $additional_data, [ 'status' => $http_status_code ] ) );
222 }
223
224 /**
225 * Get route response when something went wrong and the supplied error is a WP_Error. This currently only happens
226 * when an item in the cart is out of stock, partially out of stock, can only be bought individually, or when the
227 * item is not purchasable.
228 *
229 * @param WP_Error $error_object The WP_Error object containing the error.
230 * @param int $http_status_code HTTP status. Defaults to 500.
231 * @param array $additional_data Extra data (key value pairs) to expose in the error response.
232 * @return WP_Error WP Error object.
233 */
234 protected function get_route_error_response_from_object( $error_object, $http_status_code = 500, $additional_data = [] ) {
235 $error_object->add_data( array_merge( $additional_data, [ 'status' => $http_status_code ] ) );
236 return $error_object;
237 }
238
239 /**
240 * Prepare a single item for response.
241 *
242 * @param mixed $item Item to format to schema.
243 * @param \WP_REST_Request $request Request object.
244 * @return \WP_REST_Response $response Response data.
245 */
246 public function prepare_item_for_response( $item, \WP_REST_Request $request ) {
247 $response = rest_ensure_response( $this->schema->get_item_response( $item ) );
248
249 if ( is_wp_error( $response ) ) {
250 return rest_convert_error_to_response( $response );
251 }
252
253 $response->add_links( $this->prepare_links( $item, $request ) );
254
255 return $response;
256 }
257
258 /**
259 * Retrieves the context param.
260 *
261 * Ensures consistent descriptions between endpoints, and populates enum from schema.
262 *
263 * @param array $args Optional. Additional arguments for context parameter. Default empty array.
264 * @return array Context parameter details.
265 */
266 protected function get_context_param( $args = array() ) {
267 $param_details = array(
268 'description' => __( 'Scope under which the request is made; determines fields present in response.', 'woocommerce' ),
269 'type' => 'string',
270 'sanitize_callback' => 'sanitize_key',
271 'validate_callback' => 'rest_validate_request_arg',
272 );
273
274 $schema = $this->get_item_schema();
275
276 if ( empty( $schema['properties'] ) ) {
277 return array_merge( $param_details, $args );
278 }
279
280 $contexts = array();
281
282 foreach ( $schema['properties'] as $attributes ) {
283 if ( ! empty( $attributes['context'] ) ) {
284 $contexts = array_merge( $contexts, $attributes['context'] );
285 }
286 }
287
288 if ( ! empty( $contexts ) ) {
289 $param_details['enum'] = array_unique( $contexts );
290 rsort( $param_details['enum'] );
291 }
292
293 return array_merge( $param_details, $args );
294 }
295
296 /**
297 * Prepares a response for insertion into a collection.
298 *
299 * @param \WP_REST_Response $response Response object.
300 * @return array|mixed Response data, ready for insertion into collection data.
301 */
302 protected function prepare_response_for_collection( \WP_REST_Response $response ) {
303 $data = (array) $response->get_data();
304 $server = rest_get_server();
305 $links = $server::get_compact_response_links( $response );
306
307 if ( ! empty( $links ) ) {
308 $data['_links'] = $links;
309 }
310
311 return $data;
312 }
313
314 /**
315 * Prepare links for the request.
316 *
317 * @param mixed $item Item to prepare.
318 * @param \WP_REST_Request $request Request object.
319 * @return array
320 */
321 protected function prepare_links( $item, $request ) {
322 return [];
323 }
324
325 /**
326 * Retrieves the query params for the collections.
327 *
328 * @return array Query parameters for the collection.
329 */
330 public function get_collection_params() {
331 return array(
332 'context' => $this->get_context_param(),
333 );
334 }
335 }
336