AI
1 year ago
Agentic
7 months ago
AbstractAddressSchema.php
1 year ago
AbstractSchema.php
1 month ago
BatchSchema.php
2 years ago
BillingAddressSchema.php
2 years ago
CartCouponSchema.php
1 year ago
CartExtensionsSchema.php
1 year ago
CartFeeSchema.php
2 years ago
CartItemSchema.php
1 month ago
CartSchema.php
2 months ago
CartShippingRateSchema.php
1 month ago
CheckoutOrderSchema.php
2 years ago
CheckoutSchema.php
1 month ago
ErrorSchema.php
2 years ago
ImageAttachmentSchema.php
3 months ago
ItemSchema.php
11 months ago
OrderCouponSchema.php
2 years ago
OrderFeeSchema.php
2 years ago
OrderItemSchema.php
1 month ago
OrderSchema.php
2 months ago
PatternsSchema.php
1 year ago
ProductAttributeSchema.php
2 years ago
ProductAttributeTermSchema.php
1 month ago
ProductBrandSchema.php
1 year ago
ProductCategorySchema.php
2 years ago
ProductCollectionDataSchema.php
11 months ago
ProductReviewSchema.php
2 years ago
ProductSchema.php
1 month ago
ShippingAddressSchema.php
2 years ago
ShopperListItemSchema.php
1 month ago
ShopperListSchema.php
1 month ago
TermSchema.php
2 years ago
ImageAttachmentSchema.php
112 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | /** |
| 5 | * ImageAttachmentSchema class. |
| 6 | */ |
| 7 | class ImageAttachmentSchema extends AbstractSchema { |
| 8 | /** |
| 9 | * The schema item name. |
| 10 | * |
| 11 | * @var string |
| 12 | */ |
| 13 | protected $title = 'image'; |
| 14 | |
| 15 | /** |
| 16 | * The schema item identifier. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const IDENTIFIER = 'image'; |
| 21 | |
| 22 | /** |
| 23 | * Product schema properties. |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | public function get_properties() { |
| 28 | return [ |
| 29 | 'id' => [ |
| 30 | 'description' => __( 'Image ID.', 'woocommerce' ), |
| 31 | 'type' => 'integer', |
| 32 | 'context' => [ 'view', 'edit', 'embed' ], |
| 33 | ], |
| 34 | 'src' => [ |
| 35 | 'description' => __( 'Full size image URL.', 'woocommerce' ), |
| 36 | 'type' => 'string', |
| 37 | 'format' => 'uri', |
| 38 | 'context' => [ 'view', 'edit', 'embed' ], |
| 39 | ], |
| 40 | 'thumbnail' => [ |
| 41 | 'description' => __( 'Thumbnail URL.', 'woocommerce' ), |
| 42 | 'type' => 'string', |
| 43 | 'format' => 'uri', |
| 44 | 'context' => [ 'view', 'edit', 'embed' ], |
| 45 | ], |
| 46 | 'srcset' => [ |
| 47 | 'description' => __( 'Full size image srcset for responsive images.', 'woocommerce' ), |
| 48 | 'type' => 'string', |
| 49 | 'context' => [ 'view', 'edit', 'embed' ], |
| 50 | ], |
| 51 | 'sizes' => [ |
| 52 | 'description' => __( 'Full size image sizes for responsive images.', 'woocommerce' ), |
| 53 | 'type' => 'string', |
| 54 | 'context' => [ 'view', 'edit', 'embed' ], |
| 55 | ], |
| 56 | 'thumbnail_srcset' => [ |
| 57 | 'description' => __( 'Thumbnail srcset for responsive images.', 'woocommerce' ), |
| 58 | 'type' => 'string', |
| 59 | 'context' => [ 'view', 'edit', 'embed' ], |
| 60 | ], |
| 61 | 'thumbnail_sizes' => [ |
| 62 | 'description' => __( 'Thumbnail sizes for responsive images.', 'woocommerce' ), |
| 63 | 'type' => 'string', |
| 64 | 'context' => [ 'view', 'edit', 'embed' ], |
| 65 | ], |
| 66 | 'name' => [ |
| 67 | 'description' => __( 'Image name.', 'woocommerce' ), |
| 68 | 'type' => 'string', |
| 69 | 'context' => [ 'view', 'edit', 'embed' ], |
| 70 | ], |
| 71 | 'alt' => [ |
| 72 | 'description' => __( 'Image alternative text.', 'woocommerce' ), |
| 73 | 'type' => 'string', |
| 74 | 'context' => [ 'view', 'edit', 'embed' ], |
| 75 | ], |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert a WooCommerce product into an object suitable for the response. |
| 81 | * |
| 82 | * @param int $attachment_id Image attachment ID. |
| 83 | * @return object|null |
| 84 | */ |
| 85 | public function get_item_response( $attachment_id ) { |
| 86 | if ( ! $attachment_id ) { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | $attachment = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 91 | |
| 92 | if ( ! is_array( $attachment ) ) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | $thumbnail = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' ); |
| 97 | |
| 98 | return (object) [ |
| 99 | 'id' => (int) $attachment_id, |
| 100 | 'src' => current( $attachment ), |
| 101 | 'thumbnail' => current( $thumbnail ), |
| 102 | 'srcset' => (string) wp_get_attachment_image_srcset( $attachment_id, 'full' ), |
| 103 | 'sizes' => (string) wp_get_attachment_image_sizes( $attachment_id, 'full' ), |
| 104 | 'thumbnail_srcset' => (string) wp_get_attachment_image_srcset( $attachment_id, 'woocommerce_thumbnail' ), |
| 105 | 'thumbnail_sizes' => (string) wp_get_attachment_image_sizes( $attachment_id, 'woocommerce_thumbnail' ), |
| 106 | 'name' => get_the_title( $attachment_id ), |
| 107 | 'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ), |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 |