Passed
Push — master ( f7c939...5bd17a )
by Mike
03:08
created

ProductReviewResponse::prepare_status_response()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 1
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Convert a review to the schema format.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Responses;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * ProductReviewResponse class.
14
 */
15
class ProductReviewResponse extends AbstractObjectResponse {
16
17
	/**
18
	 * Convert object to match data in the schema.
19
	 *
20
	 * @param \WP_Comment $object Comment data.
21
	 * @param string      $context Request context. Options: 'view' and 'edit'.
22
	 * @return array
23
	 */
24
	public function prepare_response( $object, $context ) {
25
		$data = array(
26
			'id'                   => (int) $object->comment_ID,
27
			'date_created'         => wc_rest_prepare_date_response( $object->comment_date ),
28
			'date_created_gmt'     => wc_rest_prepare_date_response( $object->comment_date_gmt ),
29
			'product_id'           => (int) $object->comment_post_ID,
30
			'status'               => $this->prepare_status_response( (string) $object->comment_approved ),
31
			'reviewer'             => $object->comment_author,
32
			'reviewer_email'       => $object->comment_author_email,
33
			'review'               => $object->comment_content,
34
			'rating'               => (int) get_comment_meta( $object->comment_ID, 'rating', true ),
35
			'verified'             => wc_review_is_from_verified_owner( $object->comment_ID ),
36
			'reviewer_avatar_urls' => rest_get_avatar_urls( $object->comment_author_email ),
37
		);
38
39
		if ( 'view' === $context ) {
40
			$data['review'] = wpautop( $data['review'] );
41
		}
42
43
		return $data;
44
	}
45
46
	/**
47
	 * Checks comment_approved to set comment status for single comment output.
48
	 *
49
	 * @param string|int $comment_approved comment status.
50
	 * @return string Comment status.
51
	 */
52
	protected function prepare_status_response( $comment_approved ) {
53
		switch ( $comment_approved ) {
54
			case 'hold':
55
			case '0':
56
				$status = 'hold';
57
				break;
58
			case 'approve':
59
			case '1':
60
				$status = 'approved';
61
				break;
62
			case 'spam':
63
			case 'trash':
64
			default:
65
				$status = $comment_approved;
66
				break;
67
		}
68
69
		return $status;
70
	}
71
}
72