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 |