Completed
Pull Request — master (#11561)
by
unknown
16:38
created

WC_REST_Product_Reviews_Controller   C

Complexity

Total Complexity 61

Size/Duplication

Total Lines 532
Duplicated Lines 34.4 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
dl 183
loc 532
rs 6.018
c 4
b 0
f 1
wmc 61
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 66 66 1
A get_items_permissions_check() 7 7 2
A get_item_permissions_check() 9 9 3
A create_item_permissions_check() 0 7 3
A update_item_permissions_check() 0 7 3
A delete_item_permissions_check() 0 7 3
A batch_items_permissions_check() 0 6 2
A get_items() 17 17 4
B get_item() 19 19 6
C create_item() 0 51 7
C update_item() 0 48 10
C delete_item() 0 34 8
A batch_items() 0 21 4
B prepare_item_for_response() 0 29 2
A prepare_links() 17 17 1
A get_item_schema() 48 48 1
A get_collection_params() 0 5 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WC_REST_Product_Reviews_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WC_REST_Product_Reviews_Controller, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * REST API Product Reviews Controller.
4
 *
5
 * Handles requests to /products/<product_id>/reviews.
6
 *
7
 * @author   WooThemes
8
 * @category API
9
 * @package  WooCommerce/API
10
 * @since    2.6.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * REST API Product Reviews Controller Class.
19
 *
20
 * @package WooCommerce/API
21
 * @extends WC_REST_Controller
22
 */
23
class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
24
25
	/**
26
	 * Endpoint namespace.
27
	 *
28
	 * @var string
29
	 */
30
	protected $namespace = 'wc/v1';
31
32
	/**
33
	 * Route base.
34
	 *
35
	 * @var string
36
	 */
37
	protected $rest_base = 'products/(?P<product_id>[\d]+)/reviews';
38
39
	/**
40
	 * Register the routes for product reviews.
41
	 */
42 View Code Duplication
	public function register_routes() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
		register_rest_route( $this->namespace, '/' . $this->rest_base, array(
44
			array(
45
				'methods'             => WP_REST_Server::READABLE,
46
				'callback'            => array( $this, 'get_items' ),
47
				'permission_callback' => array( $this, 'get_items_permissions_check' ),
48
				'args'                => $this->get_collection_params(),
49
			),
50
			array(
51
				'methods'             => WP_REST_Server::CREATABLE,
52
				'callback'            => array( $this, 'create_item' ),
53
				'permission_callback' => array( $this, 'create_item_permissions_check' ),
54
				'args'                => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array(
55
					'review' => array(
56
						'required' => true,
57
					),
58
					'name' => array(
59
						'required' => true,
60
					),
61
					'email' => array(
62
						'required' => true,
63
					),
64
				) ),
65
			),
66
			'schema' => array( $this, 'get_public_item_schema' ),
67
		) );
68
69
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
70
			array(
71
				'methods'             => WP_REST_Server::READABLE,
72
				'callback'            => array( $this, 'get_item' ),
73
				'permission_callback' => array( $this, 'get_item_permissions_check' ),
74
				'args'                => array(
75
					'context' => $this->get_context_param( array( 'default' => 'view' ) ),
76
				),
77
			),
78
			array(
79
				'methods'             => WP_REST_Server::EDITABLE,
80
				'callback'            => array( $this, 'update_item' ),
81
				'permission_callback' => array( $this, 'update_item_permissions_check' ),
82
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
83
			),
84
			array(
85
				'methods'             => WP_REST_Server::DELETABLE,
86
				'callback'            => array( $this, 'delete_item' ),
87
				'permission_callback' => array( $this, 'delete_item_permissions_check' ),
88
				'args'                => array(
89
					'force' => array(
90
						'default'     => false,
91
						'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ),
92
					),
93
				),
94
			),
95
			'schema' => array( $this, 'get_public_item_schema' ),
96
		) );
97
98
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
99
			array(
100
				'methods'             => WP_REST_Server::EDITABLE,
101
				'callback'            => array( $this, 'batch_items' ),
102
				'permission_callback' => array( $this, 'batch_items_permissions_check' ),
103
				'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
104
			),
105
			'schema' => array( $this, 'get_public_batch_schema' ),
106
		) );
107
	}
108
109
	/**
110
	 * Check whether a given request has permission to read webhook deliveries.
111
	 *
112
	 * @param  WP_REST_Request $request Full details about the request.
113
	 * @return WP_Error|boolean
114
	 */
115 View Code Duplication
	public function get_items_permissions_check( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
		if ( ! wc_rest_check_post_permissions( 'product', 'read' ) ) {
117
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
118
		}
119
120
		return true;
121
	}
122
123
	/**
124
	 * Check if a given request has access to read a  product review.
125
	 *
126
	 * @param  WP_REST_Request $request Full details about the request.
127
	 * @return WP_Error|boolean
128
	 */
129 View Code Duplication
	public function get_item_permissions_check( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
		$post = get_post( (int) $request['product_id'] );
131
132
		if ( $post && ! wc_rest_check_post_permissions( 'product', 'read', $post->ID ) ) {
133
			return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
134
		}
135
136
		return true;
137
	}
138
139
	/**
140
	 * Check if a given request has access to create a new product review.
141
	 *
142
	 * @param  WP_REST_Request $request Full details about the request.
143
	 * @return WP_Error|boolean
144
	 */
145
	public function create_item_permissions_check( $request ) {
146
		$post = get_post( (int) $request['product_id'] );
147
		if ( $post && ! wc_rest_check_post_permissions( 'product', 'create', $post->ID ) ) {
148
			return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you cannot create new resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
149
		}
150
		return true;
151
	}
152
153
	/**
154
	 * Check if a given request has access to update a product review.
155
	 *
156
	 * @param  WP_REST_Request $request Full details about the request.
157
	 * @return WP_Error|boolean
158
	 */
159
	public function update_item_permissions_check( $request ) {
160
		$post = get_post( (int) $request['product_id'] );
161
		if ( $post && ! wc_rest_check_post_permissions( 'product', 'edit', $post->ID ) ) {
162
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
163
		}
164
		return true;
165
	}
166
167
	/**
168
	 * Check if a given request has access to delete a product review.
169
	 *
170
	 * @param  WP_REST_Request $request Full details about the request.
171
	 * @return WP_Error|boolean
172
	 */
173
	public function delete_item_permissions_check( $request ) {
174
		$post = get_post( (int) $request['product_id'] );
175
		if ( $post && ! wc_rest_check_post_permissions( 'product', 'delete', $post->ID ) ) {
176
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete product reviews.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
177
		}
178
		return true;
179
	}
180
181
	/**
182
	 * Check if a given request has access to batch manage product reviews.
183
	 *
184
	 * @param  WP_REST_Request $request Full details about the request.
185
	 * @return WP_Error|boolean
186
	 */
187
	public function batch_items_permissions_check( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
		if ( ! wc_rest_check_post_permissions( 'product', 'batch' ) ) {
189
			return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot batch manipulate product reviews.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
190
		}
191
		return true;
192
	}
193
194
	/**
195
	 * Get all reviews from a product.
196
	 *
197
	 * @param WP_REST_Request $request
198
	 * @return array
199
	 */
200 View Code Duplication
	public function get_items( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
		$product = get_post( (int) $request['product_id'] );
202
203
		if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
204
			return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
205
		}
206
207
		$reviews = get_approved_comments( $product->ID );
208
		$data    = array();
209
		foreach ( $reviews as $review_data ) {
210
			$review = $this->prepare_item_for_response( $review_data, $request );
211
			$review = $this->prepare_response_for_collection( $review );
212
			$data[] = $review;
213
		}
214
215
		return rest_ensure_response( $data );
216
	}
217
218
	/**
219
	 * Get a single product review.
220
	 *
221
	 * @param WP_REST_Request $request Full details about the request.
222
	 * @return WP_Error|WP_REST_Response
223
	 */
224 View Code Duplication
	public function get_item( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
		$id      = (int) $request['id'];
226
		$product = get_post( (int) $request['product_id'] );
227
228
		if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
229
			return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
230
		}
231
232
		$review = get_comment( $id );
233
234
		if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== intval( $product->ID ) ) {
235
			return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
236
		}
237
238
		$delivery = $this->prepare_item_for_response( $review, $request );
239
		$response = rest_ensure_response( $delivery );
240
241
		return $response;
242
	}
243
244
245
	/**
246
	 * Create a product review.
247
	 *
248
	 * @param WP_REST_Request $request Full details about the request.
249
	 * @return WP_Error|WP_REST_Response
250
	 */
251
	public function create_item( $request ) {
252
		$product = get_post( (int) $request['product_id'] );
253
254
		if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
255
			return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
256
		}
257
258
		if ( empty( $request['review'] ) ) {
259
			return new WP_Error( 'woocommerce_rest_product_review_invalid_review', __( 'Product review content is required.', 'woocommerce' ), array( 'status' => 400 ) );
260
		}
261
262
		if ( empty( $request['name'] ) ) {
263
			return new WP_Error( 'woocommerce_rest_product_review_invalid_name', __( 'Product review author name is required.', 'woocommerce' ), array( 'status' => 400 ) );
264
		}
265
266
		if ( empty( $request['email'] ) ) {
267
			return new WP_Error( 'woocommerce_rest_product_review_invalid_email', __( 'Product review author email is required.', 'woocommerce' ), array( 'status' => 400 ) );
268
		}
269
270
		$data = array(
271
			'comment_post_ID'      => $product->ID,
272
			'comment_author'       => $request['name'],
273
			'comment_author_email' => $request['email'],
274
			'comment_content'      => $request['review'],
275
			'comment_approved'     => 1,
276
			'comment_type'         => 'review',
277
		);
278
		$product_review_id = wp_insert_comment( $data );
279
		update_comment_meta( $product_review_id, 'rating', ( ! empty( $request['rating'] ) ? $request['rating'] : '0' ) );
280
281
		$comment = get_comment( $product_review_id );
282
		$this->update_additional_fields_for_object( $comment, $request );
283
284
		/**
285
		 * Fires after a single item is created or updated via the REST API.
286
		 *
287
		 * @param WP_Comment         $comment      Inserted object.
288
		 * @param WP_REST_Request $request   Request object.
289
		 * @param boolean         $creating  True when creating item, false when updating.
290
		 */
291
		do_action( "woocommerce_rest_insert_product_review", $comment, $request, true );
292
293
		$request->set_param( 'context', 'edit' );
294
		$response = $this->prepare_item_for_response( $comment, $request );
295
		$response = rest_ensure_response( $response );
296
		$response->set_status( 201 );
297
		$base = str_replace( '(?P<product_id>[\d]+)', $product->id, $this->rest_base );
298
		$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $product_review_id ) ) );
299
300
		return $response;
301
	}
302
303
	/**
304
	 * Update a single product review.
305
	 *
306
	 * @param WP_REST_Request $request Full details about the request.
307
	 * @return WP_Error|WP_REST_Response
308
	 */
309
	public function update_item( $request ) {
310
		$id      = (int) $request['id'];
311
		$product = get_post( (int) $request['product_id'] );
312
313
		if ( empty( $product->post_type ) || 'product' !== $product->post_type ) {
314
			return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
315
		}
316
317
		$review = get_comment( $id );
318
319
		if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== intval( $product->ID ) ) {
320
			return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
321
		}
322
323
		// Update fields
324
		$commentdata = array( 'comment_ID' => $id );
325
		if ( ! empty( $request['name'] ) ) {
326
			$commentdata['comment_author'] = $request['name' ];
327
		}
328
		if ( ! empty( $request['email'] ) ) {
329
			$commentdata['comment_author_email'] = $request['email' ];
330
		}
331
		if ( ! empty( $request['review'] ) ) {
332
			$commentdata['comment_content'] = $request['review' ];
333
		}
334
335
		wp_update_comment( $commentdata );
336
		if ( ! empty( $request['rating'] ) ) {
337
			update_comment_meta( $id, 'rating', $request['rating'] );
338
		}
339
340
		$comment = get_comment( $id );
341
		$this->update_additional_fields_for_object( $comment, $request );
342
343
		/**
344
		 * Fires after a single item is created or updated via the REST API.
345
		 *
346
		 * @param WP_Comment         $comment      Inserted object.
347
		 * @param WP_REST_Request $request   Request object.
348
		 * @param boolean         $creating  True when creating item, false when updating.
349
		 */
350
		do_action( "woocommerce_rest_insert_product_review", $comment, $request, true );
351
352
		$request->set_param( 'context', 'edit' );
353
		$response = $this->prepare_item_for_response( $comment, $request );
354
355
		return rest_ensure_response( $response );
356
	}
357
358
	/**
359
	 * Delete a product review.
360
	 *
361
	 * @param WP_REST_Request $request Full details about the request
362
	 * @return WP_Error|boolean
363
	 */
364
	public function delete_item( $request ) {
365
		$id    = is_array( $request['id'] ) ? $request['id']['id'] : $request['id'];
366
		$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
367
368
		if ( ! $force ) {
369
			return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Product reviews do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) );
370
		}
371
372
		$comment = get_comment( $id );
373
374
		if ( empty( $id ) || empty( $comment->comment_ID ) || empty( $comment->comment_post_ID ) ) {
375
			return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
376
		}
377
378
		$request->set_param( 'context', 'edit' );
379
		$response = $this->prepare_item_for_response( $comment, $request );
380
381
		$result  = wp_delete_comment( $id, true );
382
383
		if ( ! $result ) {
384
			return new WP_Error( 'rest_cannot_delete', __( 'The product review cannot be deleted.' ), array( 'status' => 500 ) );
385
		}
386
387
		/**
388
		 * Fires after a product review is deleted via the REST API.
389
		 *
390
		 * @param object           $comment  The deleted item.
391
		 * @param WP_REST_Response $response The response data.
392
		 * @param WP_REST_Request  $request  The request sent to the API.
393
		 */
394
		do_action( 'rest_delete_product_review', $comment, $response, $request );
395
396
		return $response;
397
	}
398
399
	/**
400
	 * Bulk create, update and delete items.
401
	 *
402
	 * @since  2.7.0
403
	 * @param WP_REST_Request $request Full details about the request.
404
	 * @return array Of WP_Error or WP_REST_Response.
405
	 */
406
	public function batch_items( $request ) {
407
		$items       = array_filter( $request->get_params() );
408
		$params      = $request->get_url_params();
409
		$product_id  = $params['product_id'];
410
		$body_params = array();
411
412
		foreach ( array( 'update', 'create', 'delete' ) as $batch_type ) {
413
			if ( ! empty( $items[ $batch_type ] ) ) {
414
				$injected_items = array();
415
				foreach ( $items[ $batch_type ] as $item ) {
416
					$injected_items[] = array_merge( array( 'product_id' => $product_id ), $item );
417
				}
418
				$body_params[ $batch_type ] = $injected_items;
419
			}
420
		}
421
422
		$request = new WP_REST_Request( $request->get_method() );
423
		$request->set_body_params( $body_params );
424
425
		return parent::batch_items( $request );
426
	}
427
428
	/**
429
	 * Prepare a single product review output for response.
430
	 *
431
	 * @param WP_Comment $review Product review object.
432
	 * @param WP_REST_Request $request Request object.
433
	 * @return WP_REST_Response $response Response data.
434
	 */
435
	public function prepare_item_for_response( $review, $request ) {
436
		$data = array(
437
			'id'           => (int) $review->comment_ID,
438
			'date_created' => wc_rest_prepare_date_response( $review->comment_date_gmt ),
439
			'review'       => $review->comment_content,
440
			'rating'       => (int) get_comment_meta( $review->comment_ID, 'rating', true ),
441
			'name'         => $review->comment_author,
442
			'email'        => $review->comment_author_email,
443
			'verified'     => wc_review_is_from_verified_owner( $review->comment_ID ),
444
		);
445
446
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
447
		$data    = $this->add_additional_fields_to_object( $data, $request );
448
		$data    = $this->filter_response_by_context( $data, $context );
449
450
		// Wrap the data in a response object.
451
		$response = rest_ensure_response( $data );
452
453
		$response->add_links( $this->prepare_links( $review, $request ) );
454
455
		/**
456
		 * Filter product reviews object returned from the REST API.
457
		 *
458
		 * @param WP_REST_Response $response The response object.
459
		 * @param WP_Comment       $review   Product review object used to create response.
460
		 * @param WP_REST_Request  $request  Request object.
461
		 */
462
		return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request );
463
	}
464
465
	/**
466
	 * Prepare links for the request.
467
	 *
468
	 * @param WP_Comment $review Product review object.
469
	 * @param WP_REST_Request $request Request object.
470
	 * @return array Links for the given product review.
471
	 */
472 View Code Duplication
	protected function prepare_links( $review, $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
473
		$product_id = (int) $request['product_id'];
474
		$base       = str_replace( '(?P<product_id>[\d]+)', $product_id, $this->rest_base );
475
		$links      = array(
476
			'self' => array(
477
				'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $base, $review->comment_ID ) ),
478
			),
479
			'collection' => array(
480
				'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ),
481
			),
482
			'up' => array(
483
				'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $product_id ) ),
484
			),
485
		);
486
487
		return $links;
488
	}
489
490
	/**
491
	 * Get the Product Review's schema, conforming to JSON Schema.
492
	 *
493
	 * @return array
494
	 */
495 View Code Duplication
	public function get_item_schema() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
496
		$schema = array(
497
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
498
			'title'      => 'product_review',
499
			'type'       => 'object',
500
			'properties' => array(
501
				'id' => array(
502
					'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
503
					'type'        => 'integer',
504
					'context'     => array( 'view', 'edit' ),
505
					'readonly'    => true,
506
				),
507
				'review' => array(
508
					'description' => __( 'The content of the review.', 'woocommerce' ),
509
					'type'        => 'string',
510
					'context'     => array( 'view', 'edit' ),
511
				),
512
				'date_created' => array(
513
					'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ),
514
					'type'        => 'date-time',
515
					'context'     => array( 'view', 'edit' ),
516
				),
517
				'rating' => array(
518
					'description' => __( 'Review rating (0 to 5).', 'woocommerce' ),
519
					'type'        => 'integer',
520
					'context'     => array( 'view', 'edit' ),
521
				),
522
				'name' => array(
523
					'description' => __( 'Reviewer name.', 'woocommerce' ),
524
					'type'        => 'string',
525
					'context'     => array( 'view', 'edit' ),
526
				),
527
				'email' => array(
528
					'description' => __( 'Reviewer email.', 'woocommerce' ),
529
					'type'        => 'string',
530
					'context'     => array( 'view', 'edit' ),
531
				),
532
				'verified' => array(
533
					'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ),
534
					'type'        => 'boolean',
535
					'context'     => array( 'view', 'edit' ),
536
					'readonly'    => true,
537
				),
538
			),
539
		);
540
541
		return $this->add_additional_fields_schema( $schema );
542
	}
543
544
	/**
545
	 * Get the query params for collections.
546
	 *
547
	 * @return array
548
	 */
549
	public function get_collection_params() {
550
		return array(
551
			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
552
		);
553
	}
554
}
555