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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Abilities / Domain / ProductsQuery.php
woocommerce / src / Internal / Abilities / Domain Last commit date
Traits 4 weeks ago AbstractDomainAbility.php 4 weeks ago OrderAddNote.php 4 weeks ago OrderUpdateStatus.php 4 weeks ago OrdersQuery.php 4 weeks ago ProductCreate.php 4 weeks ago ProductDelete.php 4 weeks ago ProductUpdate.php 4 weeks ago ProductsQuery.php 4 weeks ago
ProductsQuery.php
212 lines
1 <?php
2 /**
3 * Products query ability definition file.
4 */
5
6 declare( strict_types=1 );
7
8 namespace Automattic\WooCommerce\Internal\Abilities\Domain;
9
10 use Automattic\WooCommerce\Abilities\AbilityDefinition;
11 use Automattic\WooCommerce\Enums\ProductType;
12 use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\ProductAbilityTrait;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Registers the WooCommerce products query ability.
18 */
19 class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
20
21 use ProductAbilityTrait;
22
23 /**
24 * Get the ability name.
25 *
26 * @return string
27 *
28 * @since 10.9.0
29 */
30 public static function get_name(): string {
31 return 'woocommerce/products-query';
32 }
33
34 /**
35 * Get the ability registration arguments.
36 *
37 * @return array
38 *
39 * @since 10.9.0
40 */
41 public static function get_registration_args(): array {
42 return array(
43 'label' => __( 'Query products', 'woocommerce' ),
44 'description' => __(
45 'Find products by ID or common catalog filters.',
46 'woocommerce'
47 ),
48 'category' => 'woocommerce',
49 'input_schema' => self::get_input_schema(),
50 'output_schema' => self::get_collection_output_schema( 'products', self::get_product_output_schema() ),
51 'execute_callback' => array( __CLASS__, 'execute' ),
52 'permission_callback' => array( __CLASS__, 'can_query_products' ),
53 'meta' => array(
54 'show_in_rest' => true,
55 'mcp' => array(
56 'public' => true,
57 'type' => 'tool',
58 ),
59 'annotations' => array(
60 'readonly' => true,
61 'idempotent' => true,
62 'destructive' => false,
63 ),
64 ),
65 );
66 }
67
68 /**
69 * Query products.
70 *
71 * @param array $input Ability input.
72 * @return array|\WP_Error
73 *
74 * @since 10.9.0
75 */
76 public static function execute( array $input ) {
77 if ( ! empty( $input['id'] ) ) {
78 $product = self::get_product_from_input( $input );
79
80 if ( is_wp_error( $product ) ) {
81 return $product;
82 }
83
84 if ( $product->is_type( ProductType::VARIATION ) ) {
85 return new \WP_Error(
86 'woocommerce_product_type_unsupported',
87 __( 'Product type is not supported by this ability.', 'woocommerce' ),
88 array( 'status' => 400 )
89 );
90 }
91
92 return array(
93 'products' => array( self::format_product_for_response( $product ) ),
94 'total_pages' => 1,
95 'page' => 1,
96 'per_page' => 1,
97 );
98 }
99
100 $page = (int) ( $input['page'] ?? 1 );
101 $per_page = (int) ( $input['per_page'] ?? 10 );
102 $args = array(
103 'limit' => $per_page,
104 'page' => $page,
105 'paginate' => true,
106 'return' => 'objects',
107 );
108
109 foreach ( array( 'status', 'sku', 'stock_status' ) as $field ) {
110 if ( ! empty( $input[ $field ] ) ) {
111 $args[ $field ] = wc_clean( $input[ $field ] );
112 }
113 }
114
115 if ( ! empty( $input['product_type_alias'] ) && is_scalar( $input['product_type_alias'] ) ) {
116 $product_type_alias = sanitize_text_field( (string) $input['product_type_alias'] );
117 $type_args = self::get_product_query_args_for_alias( $product_type_alias );
118
119 if ( is_wp_error( $type_args ) ) {
120 return $type_args;
121 }
122
123 $args = array_merge( $args, $type_args );
124 }
125
126 if ( ! empty( $input['search'] ) ) {
127 $args['s'] = wc_clean( $input['search'] );
128 }
129
130 $results = wc_get_products( $args );
131 $products = is_object( $results ) && isset( $results->products ) ? $results->products : array();
132 $pages = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : ( count( $products ) > 0 ? 1 : 0 );
133
134 return array(
135 'products' => array_map(
136 static function ( $product ) {
137 return self::format_product_for_response( $product );
138 },
139 $products
140 ),
141 'total_pages' => $pages,
142 'page' => $page,
143 'per_page' => $per_page,
144 );
145 }
146
147 /**
148 * Check product read access.
149 *
150 * @param mixed $input Ability input.
151 * @return bool
152 *
153 * @since 10.9.0
154 */
155 public static function can_query_products( $input = array() ): bool {
156 $product_id = self::get_id_from_input( $input );
157
158 return wc_rest_check_post_permissions( 'product', 'read', $product_id );
159 }
160
161 /**
162 * Get the ability input schema.
163 *
164 * @return array
165 */
166 private static function get_input_schema(): array {
167 return array(
168 'type' => 'object',
169 'properties' => array(
170 'id' => array(
171 'type' => 'integer',
172 'minimum' => 1,
173 ),
174 'search' => array( 'type' => 'string' ),
175 'sku' => array(
176 'type' => 'string',
177 'description' => __( 'Limit results to products with SKUs that partially match this string. Use * to match products with any non-empty SKU.', 'woocommerce' ),
178 ),
179 'status' => array(
180 'type' => 'string',
181 'enum' => self::get_product_query_status_slugs(),
182 ),
183 'product_type_alias' => array(
184 'type' => 'string',
185 'description' => __(
186 'Filter by supported agent-facing product type alias. physical maps to simple shippable, non-downloadable products; virtual maps to simple non-shipping, non-downloadable products; digital maps to simple virtual/downloadable products; affiliate maps to the external product type; grouped maps to grouped.',
187 'woocommerce'
188 ),
189 'enum' => self::get_supported_product_type_aliases(),
190 ),
191 'stock_status' => array(
192 'type' => 'string',
193 'enum' => array_keys( wc_get_product_stock_status_options() ),
194 ),
195 'page' => array(
196 'type' => 'integer',
197 'default' => 1,
198 'minimum' => 1,
199 ),
200 'per_page' => array(
201 'type' => 'integer',
202 'default' => 10,
203 'minimum' => 1,
204 'maximum' => 100,
205 ),
206 ),
207 'additionalProperties' => false,
208 'default' => array(),
209 );
210 }
211 }
212