Completed
Push — master ( 31131b...6176aa )
by Mike
15:27
created

ProductVariationResponse   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 67
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare_image() 0 26 5
A prepare_response() 0 50 3
1
<?php
2
/**
3
 * Convert a product variation object to the product variation schema format.
4
 *
5
 * @package WooCommerce/RestApi
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Responses;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * ProductVariationResponse class.
14
 */
15
class ProductVariationResponse extends ProductResponse {
16
17
	/**
18
	 * Convert object to match data in the schema.
19
	 *
20
	 * @param \WC_Product_Variation $object Variation 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'                    => $object->get_id(),
27
			'name'                  => $object->get_name( $context ),
28
			'type'                  => $object->get_type(),
29
			'parent_id'             => $object->get_parent_id( $context ),
30
			'date_created'          => wc_rest_prepare_date_response( $object->get_date_created(), false ),
31
			'date_created_gmt'      => wc_rest_prepare_date_response( $object->get_date_created() ),
32
			'date_modified'         => wc_rest_prepare_date_response( $object->get_date_modified(), false ),
33
			'date_modified_gmt'     => wc_rest_prepare_date_response( $object->get_date_modified() ),
34
			'description'           => wc_format_content( $object->get_description() ),
35
			'permalink'             => $object->get_permalink(),
36
			'sku'                   => $object->get_sku(),
37
			'price'                 => $object->get_price(),
38
			'regular_price'         => $object->get_regular_price(),
39
			'sale_price'            => $object->get_sale_price(),
40
			'date_on_sale_from'     => wc_rest_prepare_date_response( $object->get_date_on_sale_from(), false ),
41
			'date_on_sale_from_gmt' => wc_rest_prepare_date_response( $object->get_date_on_sale_from() ),
42
			'date_on_sale_to'       => wc_rest_prepare_date_response( $object->get_date_on_sale_to(), false ),
43
			'date_on_sale_to_gmt'   => wc_rest_prepare_date_response( $object->get_date_on_sale_to() ),
44
			'on_sale'               => $object->is_on_sale(),
45
			'status'                => $object->get_status(),
46
			'purchasable'           => $object->is_purchasable(),
47
			'virtual'               => $object->is_virtual(),
48
			'downloadable'          => $object->is_downloadable(),
49
			'downloads'             => $this->prepare_downloads( $object ),
50
			'download_limit'        => '' !== $object->get_download_limit() ? (int) $object->get_download_limit() : -1,
0 ignored issues
show
introduced by
The condition '' !== $object->get_download_limit() is always true.
Loading history...
51
			'download_expiry'       => '' !== $object->get_download_expiry() ? (int) $object->get_download_expiry() : -1,
0 ignored issues
show
introduced by
The condition '' !== $object->get_download_expiry() is always true.
Loading history...
52
			'tax_status'            => $object->get_tax_status(),
53
			'tax_class'             => $object->get_tax_class(),
54
			'manage_stock'          => $object->managing_stock(),
55
			'stock_quantity'        => $object->get_stock_quantity(),
56
			'stock_status'          => $object->get_stock_status(),
57
			'backorders'            => $object->get_backorders(),
58
			'backorders_allowed'    => $object->backorders_allowed(),
59
			'backordered'           => $object->is_on_backorder(),
60
			'weight'                => $object->get_weight(),
61
			'dimensions'            => array(
62
				'length' => $object->get_length(),
63
				'width'  => $object->get_width(),
64
				'height' => $object->get_height(),
65
			),
66
			'shipping_class'        => $object->get_shipping_class(),
67
			'shipping_class_id'     => $object->get_shipping_class_id(),
68
			'image'                 => $this->prepare_image( $object ),
69
			'attributes'            => $this->prepare_attributes( $object ),
70
			'menu_order'            => $object->get_menu_order(),
71
			'meta_data'             => $object->get_meta_data(),
72
		);
73
		return $data;
74
	}
75
76
	/**
77
	 * Get the image for a product variation.
78
	 *
79
	 * @param \WC_Product_Variation $object Variation data.
80
	 * @return array
81
	 */
82
	protected static function prepare_image( $object ) {
83
		if ( ! $object->get_image_id() ) {
84
			return;
85
		}
86
87
		$attachment_id   = $object->get_image_id();
88
		$attachment_post = get_post( $attachment_id );
0 ignored issues
show
Bug introduced by
$attachment_id of type string is incompatible with the type WP_Post|integer|null expected by parameter $post of get_post(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$attachment_post = get_post( /** @scrutinizer ignore-type */ $attachment_id );
Loading history...
89
		if ( is_null( $attachment_post ) ) {
90
			return;
91
		}
92
93
		$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
0 ignored issues
show
Bug introduced by
$attachment_id of type string is incompatible with the type integer expected by parameter $attachment_id of wp_get_attachment_image_src(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
		$attachment = wp_get_attachment_image_src( /** @scrutinizer ignore-type */ $attachment_id, 'full' );
Loading history...
94
		if ( ! is_array( $attachment ) ) {
95
			return;
96
		}
97
98
		if ( ! isset( $image ) ) {
99
			return array(
100
				'id'                => (int) $attachment_id,
101
				'date_created'      => wc_rest_prepare_date_response( $attachment_post->post_date, false ),
102
				'date_created_gmt'  => wc_rest_prepare_date_response( strtotime( $attachment_post->post_date_gmt ) ),
103
				'date_modified'     => wc_rest_prepare_date_response( $attachment_post->post_modified, false ),
104
				'date_modified_gmt' => wc_rest_prepare_date_response( strtotime( $attachment_post->post_modified_gmt ) ),
105
				'src'               => current( $attachment ),
106
				'name'              => get_the_title( $attachment_id ),
0 ignored issues
show
Bug introduced by
$attachment_id of type string is incompatible with the type WP_Post|integer expected by parameter $post of get_the_title(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
				'name'              => get_the_title( /** @scrutinizer ignore-type */ $attachment_id ),
Loading history...
107
				'alt'               => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
0 ignored issues
show
Bug introduced by
$attachment_id of type string is incompatible with the type integer expected by parameter $post_id of get_post_meta(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
				'alt'               => get_post_meta( /** @scrutinizer ignore-type */ $attachment_id, '_wp_attachment_image_alt', true ),
Loading history...
108
			);
109
		}
110
	}
111
}
112