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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 / ProductReviews.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
ProductReviews.php
242 lines
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Routes\V1;
3
4 use WP_Comment_Query;
5 use Automattic\WooCommerce\StoreApi\Utilities\Pagination;
6
7 /**
8 * ProductReviews class.
9 */
10 class ProductReviews extends AbstractRoute {
11 /**
12 * The route identifier.
13 *
14 * @var string
15 */
16 const IDENTIFIER = 'product-reviews';
17
18 /**
19 * The routes schema.
20 *
21 * @var string
22 */
23 const SCHEMA_TYPE = 'product-review';
24
25 /**
26 * Get the path of this REST route.
27 *
28 * @return string
29 */
30 public function get_path() {
31 return self::get_path_regex();
32 }
33
34 /**
35 * Get the path of this rest route.
36 *
37 * @return string
38 */
39 public static function get_path_regex() {
40 return '/products/reviews';
41 }
42
43 /**
44 * Get method arguments for this REST route.
45 *
46 * @return array An array of endpoints.
47 */
48 public function get_args() {
49 return [
50 [
51 'methods' => \WP_REST_Server::READABLE,
52 'callback' => [ $this, 'get_response' ],
53 'permission_callback' => '__return_true',
54 'args' => $this->get_collection_params(),
55 'allow_batch' => [ 'v1' => true ],
56 ],
57 'schema' => [ $this->schema, 'get_public_item_schema' ],
58 ];
59 }
60
61 /**
62 * Get a collection of reviews.
63 *
64 * @param \WP_REST_Request $request Request object.
65 * @return \WP_REST_Response
66 */
67 protected function get_route_response( \WP_REST_Request $request ) {
68 $prepared_args = array(
69 'type' => 'review',
70 'status' => 'approve',
71 'no_found_rows' => false,
72 'offset' => $request['offset'],
73 'order' => $request['order'],
74 'number' => $request['per_page'],
75 'post__in' => $request['product_id'],
76 );
77
78 /**
79 * Map category id to list of product ids.
80 */
81 if ( ! empty( $request['category_id'] ) ) {
82 $category_ids = $request['category_id'];
83 $child_ids = [];
84 foreach ( $category_ids as $category_id ) {
85 $child_ids = array_merge( $child_ids, get_term_children( $category_id, 'product_cat' ) );
86 }
87 $category_ids = array_unique( array_merge( $category_ids, $child_ids ) );
88 $product_ids = get_objects_in_term( $category_ids, 'product_cat' );
89 $prepared_args['post__in'] = isset( $prepared_args['post__in'] ) ? array_merge( $prepared_args['post__in'], $product_ids ) : $product_ids;
90 }
91
92 if ( 'rating' === $request['orderby'] ) {
93 $prepared_args['meta_query'] = array( // phpcs:ignore
94 'relation' => 'OR',
95 array(
96 'key' => 'rating',
97 'compare' => 'EXISTS',
98 ),
99 array(
100 'key' => 'rating',
101 'compare' => 'NOT EXISTS',
102 ),
103 );
104 }
105 $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] );
106
107 if ( empty( $request['offset'] ) ) {
108 $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
109 }
110
111 $query = new WP_Comment_Query();
112 $query_result = $query->query( $prepared_args );
113 $response_objects = array();
114
115 foreach ( $query_result as $review ) {
116 $data = $this->prepare_item_for_response( $review, $request );
117 $response_objects[] = $this->prepare_response_for_collection( $data );
118 }
119
120 $total_reviews = (int) $query->found_comments;
121 $max_pages = (int) $query->max_num_pages;
122
123 if ( $total_reviews < 1 ) {
124 // Out-of-bounds, run the query again without LIMIT for total count.
125 unset( $prepared_args['number'], $prepared_args['offset'] );
126
127 $query = new WP_Comment_Query();
128 $prepared_args['count'] = true;
129
130 $total_reviews = $query->query( $prepared_args );
131 $max_pages = $request['per_page'] ? ceil( $total_reviews / $request['per_page'] ) : 1;
132 }
133
134 $response = rest_ensure_response( $response_objects );
135 $response = ( new Pagination() )->add_headers( $response, $request, $total_reviews, $max_pages );
136
137 return $response;
138 }
139
140 /**
141 * Prepends internal property prefix to query parameters to match our response fields.
142 *
143 * @param string $query_param Query parameter.
144 * @return string
145 */
146 protected function normalize_query_param( $query_param ) {
147 $prefix = 'comment_';
148
149 switch ( $query_param ) {
150 case 'id':
151 $normalized = $prefix . 'ID';
152 break;
153 case 'product':
154 $normalized = $prefix . 'post_ID';
155 break;
156 case 'rating':
157 $normalized = 'meta_value_num';
158 break;
159 default:
160 $normalized = $prefix . $query_param;
161 break;
162 }
163
164 return $normalized;
165 }
166
167 /**
168 * Get the query params for collections of products.
169 *
170 * @return array
171 */
172 public function get_collection_params() {
173 $params = array();
174 $params['context'] = $this->get_context_param();
175 $params['context']['default'] = 'view';
176
177 $params['page'] = array(
178 'description' => __( 'Current page of the collection.', 'woocommerce' ),
179 'type' => 'integer',
180 'default' => 1,
181 'sanitize_callback' => 'absint',
182 'validate_callback' => 'rest_validate_request_arg',
183 'minimum' => 1,
184 );
185
186 $params['per_page'] = array(
187 'description' => __( 'Maximum number of items to be returned in result set.', 'woocommerce' ),
188 'type' => 'integer',
189 'default' => 10,
190 'minimum' => 1,
191 'maximum' => 100,
192 'sanitize_callback' => 'absint',
193 'validate_callback' => 'rest_validate_request_arg',
194 );
195
196 $params['offset'] = array(
197 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ),
198 'type' => 'integer',
199 'sanitize_callback' => 'absint',
200 'validate_callback' => 'rest_validate_request_arg',
201 );
202
203 $params['order'] = array(
204 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ),
205 'type' => 'string',
206 'default' => 'desc',
207 'enum' => array( 'asc', 'desc' ),
208 'validate_callback' => 'rest_validate_request_arg',
209 );
210
211 $params['orderby'] = array(
212 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ),
213 'type' => 'string',
214 'default' => 'date',
215 'enum' => array(
216 'date',
217 'date_gmt',
218 'id',
219 'rating',
220 'product',
221 ),
222 'validate_callback' => 'rest_validate_request_arg',
223 );
224
225 $params['category_id'] = array(
226 'description' => __( 'Limit result set to reviews from specific category IDs.', 'woocommerce' ),
227 'type' => 'string',
228 'sanitize_callback' => 'wp_parse_id_list',
229 'validate_callback' => 'rest_validate_request_arg',
230 );
231
232 $params['product_id'] = array(
233 'description' => __( 'Limit result set to reviews from specific product IDs.', 'woocommerce' ),
234 'type' => 'string',
235 'sanitize_callback' => 'wp_parse_id_list',
236 'validate_callback' => 'rest_validate_request_arg',
237 );
238
239 return $params;
240 }
241 }
242