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
ShopperListItemsByKey.php
103 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperList; |
| 7 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 8 | |
| 9 | /** |
| 10 | * DELETE /shopper-lists/{slug}/items/{key}. |
| 11 | */ |
| 12 | class ShopperListItemsByKey extends AbstractRoute { |
| 13 | // Stopgap CSRF guard, replaced once the upstream trait lands on trunk. |
| 14 | use ShopperListsNonceCheck; |
| 15 | |
| 16 | /** |
| 17 | * Route identifier. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | const IDENTIFIER = 'shopper-list-items-by-key'; |
| 22 | |
| 23 | /** |
| 24 | * Schema identifier this route uses. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | const SCHEMA_TYPE = 'shopper-list-item'; |
| 29 | |
| 30 | /** |
| 31 | * Get the path of this REST route. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | public function get_path() { |
| 36 | return self::get_path_regex(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the path regex for this REST route. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public static function get_path_regex() { |
| 45 | return '/shopper-lists/(?P<slug>[a-z0-9-]+)/items/(?P<key>[\w-]{32})'; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get method arguments for this REST route. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public function get_args() { |
| 54 | return array( |
| 55 | 'args' => array( |
| 56 | 'slug' => array( |
| 57 | 'description' => __( 'Stable slug for the list.', 'woocommerce' ), |
| 58 | 'type' => 'string', |
| 59 | ), |
| 60 | 'key' => array( |
| 61 | 'description' => __( 'Item key.', 'woocommerce' ), |
| 62 | 'type' => 'string', |
| 63 | ), |
| 64 | ), |
| 65 | array( |
| 66 | 'methods' => \WP_REST_Server::DELETABLE, |
| 67 | 'callback' => array( $this, 'get_response' ), |
| 68 | 'permission_callback' => function () { |
| 69 | return is_user_logged_in(); |
| 70 | }, |
| 71 | ), |
| 72 | 'schema' => array( $this->schema, 'get_public_item_schema' ), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delete a single item from a list. |
| 78 | * |
| 79 | * @throws RouteException When the list or item doesn't exist. |
| 80 | * |
| 81 | * @param \WP_REST_Request $request Request object. |
| 82 | * |
| 83 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 84 | * |
| 85 | * @return \WP_REST_Response |
| 86 | */ |
| 87 | protected function get_route_delete_response( \WP_REST_Request $request ) { |
| 88 | $list = ShopperList::get_by_slug( (string) $request['slug'] ); |
| 89 | |
| 90 | if ( ! $list ) { |
| 91 | throw new RouteException( 'woocommerce_rest_shopper_list_not_found', esc_html__( 'Your saved list isn\'t available right now.', 'woocommerce' ), 404 ); |
| 92 | } |
| 93 | |
| 94 | if ( ! $list->remove_item( (string) $request['key'] ) ) { |
| 95 | throw new RouteException( 'woocommerce_rest_shopper_list_item_not_found', esc_html__( 'That item isn\'t in your saved list anymore.', 'woocommerce' ), 404 ); |
| 96 | } |
| 97 | |
| 98 | $list->save(); |
| 99 | |
| 100 | return new \WP_REST_Response( null, 204 ); |
| 101 | } |
| 102 | } |
| 103 |