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
Batch.php
145 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Routes\RouteInterface; |
| 5 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 6 | use WP_REST_Request; |
| 7 | use WP_REST_Response; |
| 8 | |
| 9 | /** |
| 10 | * Batch Route class. |
| 11 | */ |
| 12 | class Batch extends AbstractRoute implements RouteInterface { |
| 13 | /** |
| 14 | * The route identifier. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | const IDENTIFIER = 'batch'; |
| 19 | |
| 20 | /** |
| 21 | * The schema item identifier. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | const SCHEMA_TYPE = 'batch'; |
| 26 | |
| 27 | /** |
| 28 | * Get the path of this REST route. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function get_path() { |
| 33 | return self::get_path_regex(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the path of this rest route. |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public static function get_path_regex() { |
| 42 | return '/batch'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get arguments for this REST route. |
| 47 | * |
| 48 | * @return array An array of endpoints. |
| 49 | */ |
| 50 | public function get_args() { |
| 51 | return array( |
| 52 | 'callback' => [ $this, 'get_response' ], |
| 53 | 'methods' => 'POST', |
| 54 | 'permission_callback' => '__return_true', |
| 55 | 'args' => array( |
| 56 | 'validation' => array( |
| 57 | 'type' => 'string', |
| 58 | 'enum' => array( 'require-all-validate', 'normal' ), |
| 59 | 'default' => 'normal', |
| 60 | ), |
| 61 | 'requests' => array( |
| 62 | 'required' => true, |
| 63 | 'type' => 'array', |
| 64 | 'maxItems' => 25, |
| 65 | 'items' => array( |
| 66 | 'type' => 'object', |
| 67 | 'properties' => array( |
| 68 | 'method' => array( |
| 69 | 'type' => 'string', |
| 70 | /** |
| 71 | * Filters the allowed methods for store API batch requests. |
| 72 | * |
| 73 | * @since 9.8.0 |
| 74 | * |
| 75 | * @param string[] $methods Allowed methods. |
| 76 | */ |
| 77 | 'enum' => apply_filters( '__experimental_woocommerce_store_api_batch_request_methods', array( 'POST', 'PUT', 'PATCH', 'DELETE' ) ), |
| 78 | 'default' => 'POST', |
| 79 | ), |
| 80 | 'path' => array( |
| 81 | 'type' => 'string', |
| 82 | 'required' => true, |
| 83 | ), |
| 84 | 'body' => array( |
| 85 | 'type' => 'object', |
| 86 | 'properties' => array(), |
| 87 | 'additionalProperties' => true, |
| 88 | ), |
| 89 | 'headers' => array( |
| 90 | 'type' => 'object', |
| 91 | 'properties' => array(), |
| 92 | 'additionalProperties' => array( |
| 93 | 'type' => array( 'string', 'array' ), |
| 94 | 'items' => array( |
| 95 | 'type' => 'string', |
| 96 | ), |
| 97 | ), |
| 98 | ), |
| 99 | ), |
| 100 | ), |
| 101 | ), |
| 102 | ), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the route response. |
| 108 | * |
| 109 | * @see WP_REST_Server::serve_batch_request_v1 |
| 110 | * https://developer.wordpress.org/reference/classes/wp_rest_server/serve_batch_request_v1/ |
| 111 | * |
| 112 | * @throws RouteException On error. |
| 113 | * |
| 114 | * @param WP_REST_Request $request Request object. |
| 115 | * @return WP_REST_Response |
| 116 | */ |
| 117 | public function get_response( WP_REST_Request $request ) { |
| 118 | try { |
| 119 | foreach ( $request['requests'] as $args ) { |
| 120 | $parsed_path = wp_parse_url( $args['path'], PHP_URL_PATH ); |
| 121 | if ( ! $parsed_path || strpos( $parsed_path, '/wc/store' ) !== 0 ) { |
| 122 | throw new RouteException( 'woocommerce_rest_invalid_path', __( 'Invalid path provided.', 'woocommerce' ), 400 ); |
| 123 | } |
| 124 | } |
| 125 | $response = rest_get_server()->serve_batch_request_v1( $request ); |
| 126 | } catch ( RouteException $error ) { |
| 127 | $response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() ); |
| 128 | } catch ( \Exception $error ) { |
| 129 | $response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 ); |
| 130 | } |
| 131 | |
| 132 | if ( is_wp_error( $response ) ) { |
| 133 | $response = $this->error_to_response( $response ); |
| 134 | } |
| 135 | |
| 136 | $nonce = wp_create_nonce( 'wc_store_api' ); |
| 137 | |
| 138 | $response->header( 'Nonce', $nonce ); |
| 139 | $response->header( 'Nonce-Timestamp', time() ); |
| 140 | $response->header( 'User-ID', get_current_user_id() ); |
| 141 | |
| 142 | return $response; |
| 143 | } |
| 144 | } |
| 145 |