Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 325c7f6

Browse files
author
Justin Shreve
committed
Completely remove position argument and add updated schema.
1 parent 41ee2fc commit 325c7f6

File tree

2 files changed

+973
-13
lines changed

2 files changed

+973
-13
lines changed

api/class-wc-rest-dev-product-variations-controller.php

Lines changed: 356 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,7 @@ class WC_REST_Dev_Product_Variations_Controller extends WC_REST_Product_Variatio
3535
* @return array
3636
*/
3737
protected function get_images( $variation ) {
38-
if ( has_post_thumbnail( $variation->get_id() ) ) {
39-
$attachment_id = $variation->get_image_id();
40-
} else {
41-
$attachment_id = current( $variation->get_gallery_image_ids() );
42-
}
43-
38+
$attachment_id = $variation->get_image_id();
4439
$attachment_post = get_post( $attachment_id );
4540
if ( is_null( $attachment_post ) ) {
4641
$image = array();
@@ -61,11 +56,365 @@ protected function get_images( $variation ) {
6156
'src' => current( $attachment ),
6257
'name' => get_the_title( $attachment_id ),
6358
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
64-
'position' => (int) $position,
6559
);
6660
}
6761

6862
return array( $image );
6963
}
7064

65+
/**
66+
* Get the Variation's schema, conforming to JSON Schema.
67+
*
68+
* @return array
69+
*/
70+
public function get_item_schema() {
71+
$weight_unit = get_option( 'woocommerce_weight_unit' );
72+
$dimension_unit = get_option( 'woocommerce_dimension_unit' );
73+
$schema = array(
74+
'$schema' => 'http://json-schema.org/draft-04/schema#',
75+
'title' => $this->post_type,
76+
'type' => 'object',
77+
'properties' => array(
78+
'id' => array(
79+
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
80+
'type' => 'integer',
81+
'context' => array( 'view', 'edit' ),
82+
'readonly' => true,
83+
),
84+
'date_created' => array(
85+
'description' => __( "The date the variation was created, in the site's timezone.", 'woocommerce' ),
86+
'type' => 'date-time',
87+
'context' => array( 'view', 'edit' ),
88+
'readonly' => true,
89+
),
90+
'date_modified' => array(
91+
'description' => __( "The date the variation was last modified, in the site's timezone.", 'woocommerce' ),
92+
'type' => 'date-time',
93+
'context' => array( 'view', 'edit' ),
94+
'readonly' => true,
95+
),
96+
'description' => array(
97+
'description' => __( 'Variation description.', 'woocommerce' ),
98+
'type' => 'string',
99+
'context' => array( 'view', 'edit' ),
100+
),
101+
'permalink' => array(
102+
'description' => __( 'Variation URL.', 'woocommerce' ),
103+
'type' => 'string',
104+
'format' => 'uri',
105+
'context' => array( 'view', 'edit' ),
106+
'readonly' => true,
107+
),
108+
'sku' => array(
109+
'description' => __( 'Unique identifier.', 'woocommerce' ),
110+
'type' => 'string',
111+
'context' => array( 'view', 'edit' ),
112+
),
113+
'price' => array(
114+
'description' => __( 'Current variation price.', 'woocommerce' ),
115+
'type' => 'string',
116+
'context' => array( 'view', 'edit' ),
117+
'readonly' => true,
118+
),
119+
'regular_price' => array(
120+
'description' => __( 'Variation regular price.', 'woocommerce' ),
121+
'type' => 'string',
122+
'context' => array( 'view', 'edit' ),
123+
),
124+
'sale_price' => array(
125+
'description' => __( 'Variation sale price.', 'woocommerce' ),
126+
'type' => 'string',
127+
'context' => array( 'view', 'edit' ),
128+
),
129+
'date_on_sale_from' => array(
130+
'description' => __( "Start date of sale price, in the site's timezone.", 'woocommerce' ),
131+
'type' => 'date-time',
132+
'context' => array( 'view', 'edit' ),
133+
),
134+
'date_on_sale_from_gmt' => array(
135+
'description' => __( 'Start date of sale price, as GMT.', 'woocommerce' ),
136+
'type' => 'date-time',
137+
'context' => array( 'view', 'edit' ),
138+
),
139+
'date_on_sale_to' => array(
140+
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
141+
'type' => 'date-time',
142+
'context' => array( 'view', 'edit' ),
143+
),
144+
'date_on_sale_to_gmt' => array(
145+
'description' => __( "End date of sale price, in the site's timezone.", 'woocommerce' ),
146+
'type' => 'date-time',
147+
'context' => array( 'view', 'edit' ),
148+
),
149+
'on_sale' => array(
150+
'description' => __( 'Shows if the variation is on sale.', 'woocommerce' ),
151+
'type' => 'boolean',
152+
'context' => array( 'view', 'edit' ),
153+
'readonly' => true,
154+
),
155+
'visible' => array(
156+
'description' => __( "Define if the attribute is visible on the \"Additional information\" tab in the product's page.", 'woocommerce' ),
157+
'type' => 'boolean',
158+
'default' => true,
159+
'context' => array( 'view', 'edit' ),
160+
),
161+
'purchasable' => array(
162+
'description' => __( 'Shows if the variation can be bought.', 'woocommerce' ),
163+
'type' => 'boolean',
164+
'context' => array( 'view', 'edit' ),
165+
'readonly' => true,
166+
),
167+
'virtual' => array(
168+
'description' => __( 'If the variation is virtual.', 'woocommerce' ),
169+
'type' => 'boolean',
170+
'default' => false,
171+
'context' => array( 'view', 'edit' ),
172+
),
173+
'downloadable' => array(
174+
'description' => __( 'If the variation is downloadable.', 'woocommerce' ),
175+
'type' => 'boolean',
176+
'default' => false,
177+
'context' => array( 'view', 'edit' ),
178+
),
179+
'downloads' => array(
180+
'description' => __( 'List of downloadable files.', 'woocommerce' ),
181+
'type' => 'array',
182+
'context' => array( 'view', 'edit' ),
183+
'items' => array(
184+
'type' => 'object',
185+
'properties' => array(
186+
'id' => array(
187+
'description' => __( 'File MD5 hash.', 'woocommerce' ),
188+
'type' => 'string',
189+
'context' => array( 'view', 'edit' ),
190+
'readonly' => true,
191+
),
192+
'name' => array(
193+
'description' => __( 'File name.', 'woocommerce' ),
194+
'type' => 'string',
195+
'context' => array( 'view', 'edit' ),
196+
),
197+
'file' => array(
198+
'description' => __( 'File URL.', 'woocommerce' ),
199+
'type' => 'string',
200+
'context' => array( 'view', 'edit' ),
201+
),
202+
),
203+
),
204+
),
205+
'download_limit' => array(
206+
'description' => __( 'Number of times downloadable files can be downloaded after purchase.', 'woocommerce' ),
207+
'type' => 'integer',
208+
'default' => -1,
209+
'context' => array( 'view', 'edit' ),
210+
),
211+
'download_expiry' => array(
212+
'description' => __( 'Number of days until access to downloadable files expires.', 'woocommerce' ),
213+
'type' => 'integer',
214+
'default' => -1,
215+
'context' => array( 'view', 'edit' ),
216+
),
217+
'tax_status' => array(
218+
'description' => __( 'Tax status.', 'woocommerce' ),
219+
'type' => 'string',
220+
'default' => 'taxable',
221+
'enum' => array( 'taxable', 'shipping', 'none' ),
222+
'context' => array( 'view', 'edit' ),
223+
),
224+
'tax_class' => array(
225+
'description' => __( 'Tax class.', 'woocommerce' ),
226+
'type' => 'string',
227+
'context' => array( 'view', 'edit' ),
228+
),
229+
'manage_stock' => array(
230+
'description' => __( 'Stock management at variation level.', 'woocommerce' ),
231+
'type' => 'boolean',
232+
'default' => false,
233+
'context' => array( 'view', 'edit' ),
234+
),
235+
'stock_quantity' => array(
236+
'description' => __( 'Stock quantity.', 'woocommerce' ),
237+
'type' => 'integer',
238+
'context' => array( 'view', 'edit' ),
239+
),
240+
'in_stock' => array(
241+
'description' => __( 'Controls whether or not the variation is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ),
242+
'type' => 'boolean',
243+
'default' => true,
244+
'context' => array( 'view', 'edit' ),
245+
),
246+
'backorders' => array(
247+
'description' => __( 'If managing stock, this controls if backorders are allowed.', 'woocommerce' ),
248+
'type' => 'string',
249+
'default' => 'no',
250+
'enum' => array( 'no', 'notify', 'yes' ),
251+
'context' => array( 'view', 'edit' ),
252+
),
253+
'backorders_allowed' => array(
254+
'description' => __( 'Shows if backorders are allowed.', 'woocommerce' ),
255+
'type' => 'boolean',
256+
'context' => array( 'view', 'edit' ),
257+
'readonly' => true,
258+
),
259+
'backordered' => array(
260+
'description' => __( 'Shows if the variation is on backordered.', 'woocommerce' ),
261+
'type' => 'boolean',
262+
'context' => array( 'view', 'edit' ),
263+
'readonly' => true,
264+
),
265+
'weight' => array(
266+
/* translators: %s: weight unit */
267+
'description' => sprintf( __( 'Variation weight (%s).', 'woocommerce' ), $weight_unit ),
268+
'type' => 'string',
269+
'context' => array( 'view', 'edit' ),
270+
),
271+
'dimensions' => array(
272+
'description' => __( 'Variation dimensions.', 'woocommerce' ),
273+
'type' => 'object',
274+
'context' => array( 'view', 'edit' ),
275+
'properties' => array(
276+
'length' => array(
277+
/* translators: %s: dimension unit */
278+
'description' => sprintf( __( 'Variation length (%s).', 'woocommerce' ), $dimension_unit ),
279+
'type' => 'string',
280+
'context' => array( 'view', 'edit' ),
281+
),
282+
'width' => array(
283+
/* translators: %s: dimension unit */
284+
'description' => sprintf( __( 'Variation width (%s).', 'woocommerce' ), $dimension_unit ),
285+
'type' => 'string',
286+
'context' => array( 'view', 'edit' ),
287+
),
288+
'height' => array(
289+
/* translators: %s: dimension unit */
290+
'description' => sprintf( __( 'Variation height (%s).', 'woocommerce' ), $dimension_unit ),
291+
'type' => 'string',
292+
'context' => array( 'view', 'edit' ),
293+
),
294+
),
295+
),
296+
'shipping_class' => array(
297+
'description' => __( 'Shipping class slug.', 'woocommerce' ),
298+
'type' => 'string',
299+
'context' => array( 'view', 'edit' ),
300+
),
301+
'shipping_class_id' => array(
302+
'description' => __( 'Shipping class ID.', 'woocommerce' ),
303+
'type' => 'string',
304+
'context' => array( 'view', 'edit' ),
305+
'readonly' => true,
306+
),
307+
'image' => array(
308+
'description' => __( 'Variation image data.', 'woocommerce' ),
309+
'type' => 'object',
310+
'context' => array( 'view', 'edit' ),
311+
'properties' => array(
312+
'id' => array(
313+
'description' => __( 'Image ID.', 'woocommerce' ),
314+
'type' => 'integer',
315+
'context' => array( 'view', 'edit' ),
316+
),
317+
'date_created' => array(
318+
'description' => __( "The date the image was created, in the site's timezone.", 'woocommerce' ),
319+
'type' => 'date-time',
320+
'context' => array( 'view', 'edit' ),
321+
'readonly' => true,
322+
),
323+
'date_created_gmt' => array(
324+
'description' => __( 'The date the image was created, as GMT.', 'woocommerce' ),
325+
'type' => 'date-time',
326+
'context' => array( 'view', 'edit' ),
327+
'readonly' => true,
328+
),
329+
'date_modified' => array(
330+
'description' => __( "The date the image was last modified, in the site's timezone.", 'woocommerce' ),
331+
'type' => 'date-time',
332+
'context' => array( 'view', 'edit' ),
333+
'readonly' => true,
334+
),
335+
'date_modified_gmt' => array(
336+
'description' => __( 'The date the image was last modified, as GMT.', 'woocommerce' ),
337+
'type' => 'date-time',
338+
'context' => array( 'view', 'edit' ),
339+
'readonly' => true,
340+
),
341+
'src' => array(
342+
'description' => __( 'Image URL.', 'woocommerce' ),
343+
'type' => 'string',
344+
'format' => 'uri',
345+
'context' => array( 'view', 'edit' ),
346+
),
347+
'name' => array(
348+
'description' => __( 'Image name.', 'woocommerce' ),
349+
'type' => 'string',
350+
'context' => array( 'view', 'edit' ),
351+
),
352+
'alt' => array(
353+
'description' => __( 'Image alternative text.', 'woocommerce' ),
354+
'type' => 'string',
355+
'context' => array( 'view', 'edit' ),
356+
),
357+
),
358+
),
359+
'attributes' => array(
360+
'description' => __( 'List of attributes.', 'woocommerce' ),
361+
'type' => 'array',
362+
'context' => array( 'view', 'edit' ),
363+
'items' => array(
364+
'type' => 'object',
365+
'properties' => array(
366+
'id' => array(
367+
'description' => __( 'Attribute ID.', 'woocommerce' ),
368+
'type' => 'integer',
369+
'context' => array( 'view', 'edit' ),
370+
),
371+
'name' => array(
372+
'description' => __( 'Attribute name.', 'woocommerce' ),
373+
'type' => 'string',
374+
'context' => array( 'view', 'edit' ),
375+
),
376+
'option' => array(
377+
'description' => __( 'Selected attribute term name.', 'woocommerce' ),
378+
'type' => 'string',
379+
'context' => array( 'view', 'edit' ),
380+
),
381+
),
382+
),
383+
),
384+
'menu_order' => array(
385+
'description' => __( 'Menu order, used to custom sort products.', 'woocommerce' ),
386+
'type' => 'integer',
387+
'context' => array( 'view', 'edit' ),
388+
),
389+
'meta_data' => array(
390+
'description' => __( 'Meta data.', 'woocommerce' ),
391+
'type' => 'array',
392+
'context' => array( 'view', 'edit' ),
393+
'items' => array(
394+
'type' => 'object',
395+
'properties' => array(
396+
'id' => array(
397+
'description' => __( 'Meta ID.', 'woocommerce' ),
398+
'type' => 'integer',
399+
'context' => array( 'view', 'edit' ),
400+
'readonly' => true,
401+
),
402+
'key' => array(
403+
'description' => __( 'Meta key.', 'woocommerce' ),
404+
'type' => 'string',
405+
'context' => array( 'view', 'edit' ),
406+
),
407+
'value' => array(
408+
'description' => __( 'Meta value.', 'woocommerce' ),
409+
'type' => 'string',
410+
'context' => array( 'view', 'edit' ),
411+
),
412+
),
413+
),
414+
),
415+
),
416+
);
417+
return $this->add_additional_fields_schema( $schema );
418+
}
419+
71420
}

0 commit comments

Comments
 (0)