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 |