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
ShopperListSchema.php
129 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperList; |
| 7 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperListItem; |
| 8 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 9 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 10 | |
| 11 | /** |
| 12 | * ShopperListSchema class. |
| 13 | * |
| 14 | * Represents a single shopper list, including its saved items. |
| 15 | */ |
| 16 | class ShopperListSchema extends AbstractSchema { |
| 17 | /** |
| 18 | * The schema item name. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $title = 'shopper_list'; |
| 23 | |
| 24 | /** |
| 25 | * The schema item identifier. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const IDENTIFIER = 'shopper-list'; |
| 30 | |
| 31 | /** |
| 32 | * Item schema instance. |
| 33 | * |
| 34 | * @var ShopperListItemSchema |
| 35 | */ |
| 36 | protected $item_schema; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @throws \RuntimeException When the ShopperListItemSchema is not registered. |
| 42 | * |
| 43 | * @param ExtendSchema $extend Rest Extending instance. |
| 44 | * @param SchemaController $controller Schema Controller instance. |
| 45 | */ |
| 46 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 47 | parent::__construct( $extend, $controller ); |
| 48 | $schema = $this->controller->get( ShopperListItemSchema::IDENTIFIER ); |
| 49 | if ( ! $schema instanceof ShopperListItemSchema ) { |
| 50 | throw new \RuntimeException( 'ShopperListItemSchema is not registered in SchemaController.' ); |
| 51 | } |
| 52 | $this->item_schema = $schema; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Schema properties. |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function get_properties() { |
| 61 | return array( |
| 62 | 'slug' => array( |
| 63 | 'description' => __( 'Stable slug for the list.', 'woocommerce' ), |
| 64 | 'type' => 'string', |
| 65 | 'context' => array( 'view', 'edit' ), |
| 66 | 'readonly' => true, |
| 67 | ), |
| 68 | 'date_created_gmt' => array( |
| 69 | 'description' => __( 'The date the list was created, as GMT.', 'woocommerce' ), |
| 70 | 'type' => 'string', |
| 71 | 'format' => 'date-time', |
| 72 | 'context' => array( 'view', 'edit' ), |
| 73 | 'readonly' => true, |
| 74 | ), |
| 75 | 'item_count' => array( |
| 76 | 'description' => __( 'Number of items currently in the list.', 'woocommerce' ), |
| 77 | 'type' => 'integer', |
| 78 | 'context' => array( 'view', 'edit' ), |
| 79 | 'readonly' => true, |
| 80 | ), |
| 81 | 'items' => array( |
| 82 | 'description' => __( 'List of saved items.', 'woocommerce' ), |
| 83 | 'type' => 'array', |
| 84 | 'context' => array( 'view', 'edit' ), |
| 85 | 'readonly' => true, |
| 86 | 'items' => array( |
| 87 | 'type' => 'object', |
| 88 | 'properties' => $this->force_schema_readonly( $this->item_schema->get_properties() ), |
| 89 | ), |
| 90 | ), |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Serialize the shopper list. |
| 96 | * |
| 97 | * @param ShopperList $shopper_list The list. |
| 98 | * @return array |
| 99 | */ |
| 100 | public function get_item_response( $shopper_list ) { |
| 101 | $items = array_values( $shopper_list->get_items() ); |
| 102 | |
| 103 | $product_ids = array_filter( |
| 104 | array_map( |
| 105 | static function ( ShopperListItem $item ): int { |
| 106 | $variation_id = $item->get_variation_id(); |
| 107 | return $variation_id > 0 ? $variation_id : $item->get_product_id(); |
| 108 | }, |
| 109 | $items |
| 110 | ) |
| 111 | ); |
| 112 | if ( ! empty( $product_ids ) ) { |
| 113 | _prime_post_caches( array_unique( $product_ids ) ); |
| 114 | } |
| 115 | |
| 116 | return array( |
| 117 | 'slug' => $shopper_list->get_slug(), |
| 118 | 'date_created_gmt' => wc_rest_prepare_date_response( $shopper_list->get_date_created_gmt() ), |
| 119 | 'item_count' => count( $items ), |
| 120 | 'items' => array_values( |
| 121 | array_map( |
| 122 | fn( ShopperListItem $item ) => $this->item_schema->get_item_response( $item ), |
| 123 | $items |
| 124 | ) |
| 125 | ), |
| 126 | ); |
| 127 | } |
| 128 | } |
| 129 |