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
ProductAttributeTerms.php
137 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\Internal\ProductAttributes\VisualAttributeTermMeta; |
| 5 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 6 | use Automattic\WooCommerce\StoreApi\Schemas\V1\ProductAttributeTermSchema; |
| 7 | |
| 8 | /** |
| 9 | * ProductAttributeTerms class. |
| 10 | */ |
| 11 | class ProductAttributeTerms extends AbstractTermsRoute { |
| 12 | /** |
| 13 | * The route identifier. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | const IDENTIFIER = 'product-attribute-terms'; |
| 18 | |
| 19 | /** |
| 20 | * The routes schema. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | const SCHEMA_TYPE = ProductAttributeTermSchema::IDENTIFIER; |
| 25 | |
| 26 | /** |
| 27 | * Get the path of this REST route. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public function get_path() { |
| 32 | return self::get_path_regex(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the path of this rest route. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public static function get_path_regex() { |
| 41 | return '/products/attributes/(?P<attribute_id>[\d]+)/terms'; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get method arguments for this REST route. |
| 46 | * |
| 47 | * @return array An array of endpoints. |
| 48 | */ |
| 49 | public function get_args() { |
| 50 | return [ |
| 51 | 'args' => array( |
| 52 | 'attribute_id' => array( |
| 53 | 'description' => __( 'Unique identifier for the attribute.', 'woocommerce' ), |
| 54 | 'type' => 'integer', |
| 55 | ), |
| 56 | ), |
| 57 | [ |
| 58 | 'methods' => \WP_REST_Server::READABLE, |
| 59 | 'callback' => [ $this, 'get_response' ], |
| 60 | 'permission_callback' => '__return_true', |
| 61 | 'args' => $this->get_collection_params(), |
| 62 | 'allow_batch' => [ 'v1' => true ], |
| 63 | ], |
| 64 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the query params for collections of attributes. |
| 70 | * |
| 71 | * @return array |
| 72 | */ |
| 73 | public function get_collection_params() { |
| 74 | $params = parent::get_collection_params(); |
| 75 | $params['orderby']['enum'][] = 'menu_order'; |
| 76 | $params['orderby']['enum'][] = 'name_num'; |
| 77 | $params['orderby']['enum'][] = 'id'; |
| 78 | $params['__experimental_visual'] = array( |
| 79 | 'description' => __( 'If true, include experimental visual swatch data for wc-visual attribute terms.', 'woocommerce' ), |
| 80 | 'type' => 'boolean', |
| 81 | 'default' => false, |
| 82 | 'sanitize_callback' => 'wc_string_to_bool', |
| 83 | 'validate_callback' => 'rest_validate_request_arg', |
| 84 | ); |
| 85 | return $params; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Prepare a single item for response. |
| 90 | * |
| 91 | * @param mixed $item Item to format to schema. |
| 92 | * @param \WP_REST_Request $request Request object. |
| 93 | * |
| 94 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 95 | * |
| 96 | * @return \WP_REST_Response $response Response data. |
| 97 | */ |
| 98 | public function prepare_item_for_response( $item, \WP_REST_Request $request ) { |
| 99 | $response = parent::prepare_item_for_response( $item, $request ); |
| 100 | |
| 101 | if ( |
| 102 | ! wc_string_to_bool( $request['__experimental_visual'] ) || |
| 103 | ! ( $item instanceof \WP_Term ) || |
| 104 | ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $item->taxonomy ) |
| 105 | ) { |
| 106 | return $response; |
| 107 | } |
| 108 | |
| 109 | $data = $response->get_data(); |
| 110 | |
| 111 | $data[ ProductAttributeTermSchema::VISUAL_PROPERTY_NAME ] = VisualAttributeTermMeta::get_term_visual( |
| 112 | (int) $item->term_id |
| 113 | ); |
| 114 | |
| 115 | $response->set_data( $data ); |
| 116 | |
| 117 | return $response; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get a collection of attribute terms. |
| 122 | * |
| 123 | * @throws RouteException On error. |
| 124 | * @param \WP_REST_Request $request Request object. |
| 125 | * @return \WP_REST_Response |
| 126 | */ |
| 127 | protected function get_route_response( \WP_REST_Request $request ) { |
| 128 | $attribute = wc_get_attribute( $request['attribute_id'] ); |
| 129 | |
| 130 | if ( ! $attribute || ! taxonomy_exists( $attribute->slug ) ) { |
| 131 | throw new RouteException( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woocommerce' ), 404 ); |
| 132 | } |
| 133 | |
| 134 | return $this->get_terms_response( $attribute->slug, $request ); |
| 135 | } |
| 136 | } |
| 137 |