4
4
*
5
5
* Handles requests to the /products endpoint.
6
6
*
7
- * @author WooThemes
7
+ * @author Automattic
8
8
* @category API
9
9
* @package WooCommerce/API
10
10
* @since 2.6.0
@@ -28,4 +28,113 @@ class WC_REST_Dev_Products_Controller extends WC_REST_Products_Controller {
28
28
*/
29
29
protected $ namespace = 'wc/v3 ' ;
30
30
31
+ /**
32
+ * Get the images for a product or product variation.
33
+ *
34
+ * @param WC_Product|WC_Product_Variation $product Product instance.
35
+ * @return array
36
+ */
37
+ protected function get_images ( $ product ) {
38
+ $ images = array ();
39
+ $ attachment_ids = array ();
40
+
41
+ // Add featured image.
42
+ if ( has_post_thumbnail ( $ product ->get_id () ) ) {
43
+ $ attachment_ids [] = $ product ->get_image_id ();
44
+ }
45
+
46
+ // Add gallery images.
47
+ $ attachment_ids = array_merge ( $ attachment_ids , $ product ->get_gallery_image_ids () );
48
+
49
+ // Build image data.
50
+ foreach ( $ attachment_ids as $ position => $ attachment_id ) {
51
+ $ attachment_post = get_post ( $ attachment_id );
52
+ if ( is_null ( $ attachment_post ) ) {
53
+ continue ;
54
+ }
55
+
56
+ $ attachment = wp_get_attachment_image_src ( $ attachment_id , 'full ' );
57
+ if ( ! is_array ( $ attachment ) ) {
58
+ continue ;
59
+ }
60
+
61
+ $ images [] = array (
62
+ 'id ' => (int ) $ attachment_id ,
63
+ 'date_created ' => wc_rest_prepare_date_response ( $ attachment_post ->post_date , false ),
64
+ 'date_created_gmt ' => wc_rest_prepare_date_response ( strtotime ( $ attachment_post ->post_date_gmt ) ),
65
+ 'date_modified ' => wc_rest_prepare_date_response ( $ attachment_post ->post_modified , false ),
66
+ 'date_modified_gmt ' => wc_rest_prepare_date_response ( strtotime ( $ attachment_post ->post_modified_gmt ) ),
67
+ 'src ' => current ( $ attachment ),
68
+ 'name ' => get_the_title ( $ attachment_id ),
69
+ 'alt ' => get_post_meta ( $ attachment_id , '_wp_attachment_image_alt ' , true ),
70
+ 'position ' => (int ) $ position ,
71
+ );
72
+ }
73
+
74
+ return $ images ;
75
+ }
76
+
77
+ /**
78
+ * Set product images.
79
+ *
80
+ * @throws WC_REST_Exception REST API exceptions.
81
+ * @param WC_Product $product Product instance.
82
+ * @param array $images Images data.
83
+ * @return WC_Product
84
+ */
85
+ protected function set_product_images ( $ product , $ images ) {
86
+ if ( is_array ( $ images ) ) {
87
+ $ gallery = array ();
88
+
89
+ foreach ( $ images as $ image ) {
90
+ $ attachment_id = isset ( $ image ['id ' ] ) ? absint ( $ image ['id ' ] ) : 0 ;
91
+
92
+ if ( 0 === $ attachment_id && isset ( $ image ['src ' ] ) ) {
93
+ $ upload = wc_rest_upload_image_from_url ( esc_url_raw ( $ image ['src ' ] ) );
94
+
95
+ if ( is_wp_error ( $ upload ) ) {
96
+ if ( ! apply_filters ( 'woocommerce_rest_suppress_image_upload_error ' , false , $ upload , $ product ->get_id (), $ images ) ) {
97
+ throw new WC_REST_Exception ( 'woocommerce_product_image_upload_error ' , $ upload ->get_error_message (), 400 );
98
+ } else {
99
+ continue ;
100
+ }
101
+ }
102
+
103
+ $ attachment_id = wc_rest_set_uploaded_image_as_attachment ( $ upload , $ product ->get_id () );
104
+ }
105
+
106
+ if ( ! wp_attachment_is_image ( $ attachment_id ) ) {
107
+ throw new WC_REST_Exception ( 'woocommerce_product_invalid_image_id ' , sprintf ( __ ( '#%s is an invalid image ID. ' , 'woocommerce ' ), $ attachment_id ), 400 );
108
+ }
109
+
110
+ $ featured_image = $ product ->get_image_id ();
111
+
112
+ if ( isset ( $ image ['position ' ] ) && 0 === absint ( $ image ['position ' ] ) ) {
113
+ $ product ->set_image_id ( $ attachment_id );
114
+ } elseif ( empty ( $ image ['position ' ] ) && empty ( $ featured_image ) ) {
115
+ $ product ->set_image_id ( $ attachment_id );
116
+ } else {
117
+ $ gallery [] = $ attachment_id ;
118
+ }
119
+
120
+ // Set the image alt if present.
121
+ if ( ! empty ( $ image ['alt ' ] ) ) {
122
+ update_post_meta ( $ attachment_id , '_wp_attachment_image_alt ' , wc_clean ( $ image ['alt ' ] ) );
123
+ }
124
+
125
+ // Set the image name if present.
126
+ if ( ! empty ( $ image ['name ' ] ) ) {
127
+ wp_update_post ( array ( 'ID ' => $ attachment_id , 'post_title ' => $ image ['name ' ] ) );
128
+ }
129
+ }
130
+
131
+ $ product ->set_gallery_image_ids ( $ gallery );
132
+ } else {
133
+ $ product ->set_image_id ( '' );
134
+ $ product ->set_gallery_image_ids ( array () );
135
+ }
136
+
137
+ return $ product ;
138
+ }
139
+
31
140
}
0 commit comments