CollectionQuery.php
92 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CollectionQuery class. |
| 4 | * |
| 5 | * @package WooCommerce\RestApi |
| 6 | * @internal This file is for internal use only and should not be used by external code. |
| 7 | */ |
| 8 | |
| 9 | declare( strict_types=1 ); |
| 10 | |
| 11 | namespace Automattic\WooCommerce\Internal\RestApi\Routes\V4\OrderNotes; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | use Automattic\WooCommerce\Internal\RestApi\Routes\V4\AbstractCollectionQuery; |
| 16 | use WP_REST_Request; |
| 17 | use WC_Order; |
| 18 | |
| 19 | /** |
| 20 | * CollectionQuery class. |
| 21 | * |
| 22 | * @internal This class is for internal use only and should not be used by external code. |
| 23 | */ |
| 24 | final class CollectionQuery extends AbstractCollectionQuery { |
| 25 | /** |
| 26 | * Get query schema. |
| 27 | * |
| 28 | * @return array |
| 29 | */ |
| 30 | public function get_query_schema(): array { |
| 31 | return array( |
| 32 | 'note_type' => array( |
| 33 | 'default' => 'all', |
| 34 | 'description' => __( 'Limit result to customer notes or private notes.', 'woocommerce' ), |
| 35 | 'type' => 'string', |
| 36 | 'enum' => array( 'all', 'customer', 'private' ), |
| 37 | 'sanitize_callback' => 'sanitize_key', |
| 38 | 'validate_callback' => 'rest_validate_request_arg', |
| 39 | ), |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Prepares query args. |
| 45 | * |
| 46 | * @param WP_REST_Request $request The request object. |
| 47 | * @return array |
| 48 | */ |
| 49 | public function get_query_args( WP_REST_Request $request ): array { |
| 50 | $args = array( |
| 51 | 'post_id' => $request['order_id'] ?? 0, |
| 52 | 'status' => 'approve', |
| 53 | 'type' => 'order_note', |
| 54 | ); |
| 55 | |
| 56 | // Allow filter by order note type. |
| 57 | if ( 'customer' === $request['note_type'] ) { |
| 58 | $args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 59 | array( |
| 60 | 'key' => 'is_customer_note', |
| 61 | 'value' => 1, |
| 62 | 'compare' => '=', |
| 63 | ), |
| 64 | ); |
| 65 | } elseif ( 'private' === $request['note_type'] ) { |
| 66 | $args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 67 | array( |
| 68 | 'key' => 'is_customer_note', |
| 69 | 'compare' => 'NOT EXISTS', |
| 70 | ), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | return $args; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get results of the query. |
| 79 | * |
| 80 | * @param array $query_args The query arguments. |
| 81 | * @param WP_REST_Request $request The request object. |
| 82 | * @return array |
| 83 | */ |
| 84 | public function get_query_results( array $query_args, WP_REST_Request $request ): array { |
| 85 | remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); |
| 86 | $results = get_comments( $query_args ); |
| 87 | add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 ); |
| 88 | |
| 89 | return (array) $results; |
| 90 | } |
| 91 | } |
| 92 |