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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 / OrdersQuery.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
OrdersQuery.php
357 lines
1 <?php
2 /**
3 * Orders 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\Internal\Abilities\Domain\Traits\OrderAbilityTrait;
12 use Automattic\WooCommerce\Utilities\OrderUtil;
13
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Registers the WooCommerce orders query ability.
18 */
19 class OrdersQuery extends AbstractDomainAbility implements AbilityDefinition {
20
21 use OrderAbilityTrait;
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/orders-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 orders', 'woocommerce' ),
44 'description' => __(
45 'Find orders by ID or common order filters.',
46 'woocommerce'
47 ),
48 'category' => 'woocommerce',
49 'input_schema' => self::get_input_schema(),
50 'output_schema' => self::get_collection_output_schema( 'orders', self::get_order_output_schema() ),
51 'execute_callback' => array( __CLASS__, 'execute' ),
52 'permission_callback' => array( __CLASS__, 'can_query_orders' ),
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 orders.
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 $include_line_items = (bool) ( $input['include_line_items'] ?? false );
78
79 if ( ! empty( $input['id'] ) ) {
80 $order = self::get_order_from_input( $input );
81
82 if ( is_wp_error( $order ) ) {
83 return $order;
84 }
85
86 return array(
87 'orders' => array( self::format_order_for_response( $order, $include_line_items ) ),
88 'total_pages' => 1,
89 'page' => 1,
90 'per_page' => 1,
91 );
92 }
93
94 $page = (int) ( $input['page'] ?? 1 );
95 $per_page = (int) ( $input['per_page'] ?? 10 );
96 $args = array(
97 'limit' => $per_page,
98 'page' => $page,
99 'paginate' => true,
100 'return' => 'objects',
101 'type' => 'shop_order',
102 );
103
104 foreach ( array( 'status', 'billing_email', 'order' ) as $field ) {
105 if ( ! empty( $input[ $field ] ) && is_scalar( $input[ $field ] ) ) {
106 $args[ $field ] = wc_clean( (string) $input[ $field ] );
107 }
108 }
109
110 if ( empty( $args['status'] ) ) {
111 $args['status'] = self::get_allowed_order_status_slugs();
112 }
113
114 if ( ! empty( $input['orderby'] ) && is_scalar( $input['orderby'] ) ) {
115 $orderby = sanitize_text_field( (string) $input['orderby'] );
116 $args['orderby'] = self::prepare_orderby_arg( $orderby );
117 }
118
119 foreach ( array( 'customer_id', 'parent' ) as $field ) {
120 if ( isset( $input[ $field ] ) ) {
121 $args[ $field ] = (int) $input[ $field ];
122 }
123 }
124
125 if ( ! empty( $input['exclude'] ) && is_array( $input['exclude'] ) ) {
126 $args['exclude'] = array_map( 'intval', $input['exclude'] );
127 }
128
129 foreach ( array( 'date_after', 'date_before' ) as $field ) {
130 if ( ! empty( $input[ $field ] ) ) {
131 $args[ $field ] = wc_clean( $input[ $field ] );
132 }
133 }
134
135 $modified_date_query = self::build_modified_date_query_arg( $input );
136 if ( null !== $modified_date_query ) {
137 $args['date_query'][] = $modified_date_query;
138 }
139
140 $results = wc_get_orders( $args );
141 $orders = is_object( $results ) && isset( $results->orders ) ? $results->orders : array();
142 $orders = array_values(
143 array_filter(
144 $orders,
145 static function ( $order ): bool {
146 return $order instanceof \WC_Order;
147 }
148 )
149 );
150 $pages = is_object( $results ) && isset( $results->max_num_pages ) ? (int) $results->max_num_pages : ( count( $orders ) > 0 ? 1 : 0 );
151
152 return array(
153 'orders' => array_map(
154 static function ( $order ) use ( $include_line_items ) {
155 return self::format_order_for_response( $order, $include_line_items );
156 },
157 $orders
158 ),
159 'total_pages' => $pages,
160 'page' => $page,
161 'per_page' => $per_page,
162 );
163 }
164
165 /**
166 * Check order read access.
167 *
168 * @param mixed $input Ability input.
169 * @return bool
170 *
171 * @since 10.9.0
172 */
173 public static function can_query_orders( $input = array() ): bool {
174 $order_id = self::get_id_from_input( $input );
175
176 return wc_rest_check_post_permissions( 'shop_order', 'read', $order_id );
177 }
178
179 /**
180 * Get the ability input schema.
181 *
182 * @return array
183 */
184 private static function get_input_schema(): array {
185 return array(
186 'type' => 'object',
187 'properties' => array(
188 'id' => array(
189 'type' => 'integer',
190 'minimum' => 1,
191 ),
192 'status' => array(
193 'type' => 'string',
194 'description' => __( 'Filter by order status slug without the wc- prefix.', 'woocommerce' ),
195 'enum' => self::get_allowed_order_status_slugs(),
196 ),
197 'customer_id' => array(
198 'type' => 'integer',
199 'description' => __( 'Filter by customer ID. Use 0 to filter guest orders.', 'woocommerce' ),
200 'minimum' => 0,
201 ),
202 'billing_email' => array(
203 'type' => 'string',
204 'format' => 'email',
205 ),
206 'parent' => array(
207 'type' => 'integer',
208 'description' => __( 'Filter by parent order ID.', 'woocommerce' ),
209 'minimum' => 1,
210 ),
211 'exclude' => array(
212 'type' => 'array',
213 'description' => __( 'Order IDs to exclude from the results.', 'woocommerce' ),
214 'items' => array(
215 'type' => 'integer',
216 'minimum' => 1,
217 ),
218 ),
219 'date_after' => array(
220 'type' => 'string',
221 'description' => __( 'Filter orders created after this date/time.', 'woocommerce' ),
222 'format' => 'date-time',
223 ),
224 'date_before' => array(
225 'type' => 'string',
226 'description' => __( 'Filter orders created before this date/time.', 'woocommerce' ),
227 'format' => 'date-time',
228 ),
229 'modified_after' => array(
230 'type' => 'string',
231 'description' => __( 'Filter orders modified after this date/time.', 'woocommerce' ),
232 'format' => 'date-time',
233 ),
234 'modified_before' => array(
235 'type' => 'string',
236 'description' => __( 'Filter orders modified before this date/time.', 'woocommerce' ),
237 'format' => 'date-time',
238 ),
239 'orderby' => array(
240 'type' => 'string',
241 'enum' => array( 'id', 'date', 'date_modified', 'total' ),
242 ),
243 'order' => array(
244 'type' => 'string',
245 'enum' => array( 'asc', 'desc' ),
246 ),
247 'include_line_items' => array(
248 'type' => 'boolean',
249 'description' => __(
250 'Whether to include order line items in each returned order. Defaults to false.',
251 'woocommerce'
252 ),
253 'default' => false,
254 ),
255 'page' => array(
256 'type' => 'integer',
257 'default' => 1,
258 'minimum' => 1,
259 ),
260 'per_page' => array(
261 'type' => 'integer',
262 'default' => 10,
263 'minimum' => 1,
264 'maximum' => 100,
265 ),
266 ),
267 'additionalProperties' => false,
268 'default' => array(),
269 );
270 }
271
272 /**
273 * Build a modified-date query arg from modified_after/modified_before input.
274 *
275 * @param array $input Ability input.
276 * @return array|null
277 */
278 private static function build_modified_date_query_arg( array $input ): ?array {
279 $after = isset( $input['modified_after'] ) && is_string( $input['modified_after'] ) ? sanitize_text_field( $input['modified_after'] ) : '';
280 $before = isset( $input['modified_before'] ) && is_string( $input['modified_before'] ) ? sanitize_text_field( $input['modified_before'] ) : '';
281
282 if ( '' === $after && '' === $before ) {
283 return null;
284 }
285
286 $after_timestamp = '' !== $after ? self::prepare_date_time_for_query( $after ) : null;
287 $before_timestamp = '' !== $before ? self::prepare_date_time_for_query( $before ) : null;
288
289 if (
290 ( '' !== $after && null === $after_timestamp )
291 || ( '' !== $before && null === $before_timestamp )
292 ) {
293 return null;
294 }
295
296 $date_query = array(
297 'column' => 'post_modified_gmt',
298 'inclusive' => false,
299 );
300
301 if ( null !== $after_timestamp ) {
302 $date_query['after'] = self::format_timestamp_for_date_query( $after_timestamp );
303 }
304
305 if ( null !== $before_timestamp ) {
306 $date_query['before'] = self::format_timestamp_for_date_query( $before_timestamp );
307 }
308
309 return $date_query;
310 }
311
312 /**
313 * Prepare a date-time string as a timestamp for second-precision order queries.
314 *
315 * @param string $date_time Date-time string.
316 * @return int|null
317 */
318 private static function prepare_date_time_for_query( string $date_time ): ?int {
319 try {
320 return wc_string_to_datetime( $date_time )->getTimestamp();
321 } catch ( \Exception $exception ) {
322 return null;
323 }
324 }
325
326 /**
327 * Format a timestamp for a GMT date query.
328 *
329 * @param int $timestamp Timestamp.
330 * @return string
331 */
332 private static function format_timestamp_for_date_query( int $timestamp ): string {
333 return gmdate( 'Y-m-d H:i:s', $timestamp );
334 }
335
336 /**
337 * Prepare orderby values for wc_get_orders across HPOS and legacy storage.
338 *
339 * Values not present in the map are already accepted by wc_get_orders for
340 * both storage engines and pass through unchanged.
341 *
342 * @param string $orderby Input orderby value.
343 * @return string
344 */
345 private static function prepare_orderby_arg( string $orderby ): string {
346 $orderby_map = array(
347 'id' => 'ID',
348 );
349
350 if ( 'date_modified' === $orderby ) {
351 return OrderUtil::custom_orders_table_usage_is_enabled() ? 'date_modified' : 'post_modified';
352 }
353
354 return $orderby_map[ $orderby ] ?? $orderby;
355 }
356 }
357