← All changes
|
includes/API/V2/Proxy/Orders_Proxy_Behavior.php
+51
-4
1.10.19
→
1.10.17
View file →
| @@ -6,8 +6,10 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WCPOS\WooCommercePOS\API\V2\Proxy; |
| 9 | 9 | |
| 10 | +use Automattic\WooCommerce\Utilities\OrderUtil; | |
| 11 | +use WCPOS\WooCommercePOS\API\Order_Search; | |
| 10 | 12 | use WCPOS\WooCommercePOS\Sync\Collection_Rules; |
| 11 | 13 | use WCPOS\WooCommercePOS\Sync\Order_Serializer; |
| 12 | 14 | use WP_REST_Request; |
| 13 | 15 | |
| @@ -15,14 +17,20 @@ | ||
| 15 | 17 | * Applies the order Collection Rules plan and v2 order wire shape. |
| 16 | 18 | */ |
| 17 | 19 | final class Orders_Proxy_Behavior extends Scoped_Proxy_Behavior { |
| 18 | 20 | /** |
| 21 | + * Search text removed from the forwarded request. | |
| 22 | + * | |
| 23 | + * @var string | |
| 24 | + */ | |
| 25 | + private $search = ''; | |
| 26 | + | |
| 27 | + /** | |
| 19 | 28 | * Proxy request keys claimed by the order Collection Rules plan. |
| 20 | 29 | */ |
| 21 | 30 | private const PARAM_MAP = array( |
| 22 | 31 | 'orderby' => 'orderby', |
| 23 | 32 | 'order' => 'order', |
| 24 | - 'search' => 'search', | |
| 25 | 33 | 'pos_cashier' => 'pos_cashier', |
| 26 | 34 | 'pos_store' => 'pos_store', |
| 27 | 35 | 'created_via' => 'created_via', |
| 28 | 36 | 'include' => array( |
| @@ -52,15 +60,54 @@ | ||
| 52 | 60 | * |
| 53 | 61 | * @return array |
| 54 | 62 | */ |
| 55 | 63 | public function forwarded_params( array $params, WP_REST_Request $request ): array { |
| 56 | - $plan_request = clone $request; | |
| 57 | - $plan_request->set_param( 'search', $params['search'] ?? null ); | |
| 58 | - $this->plan = Collection_Rules::for_request( 'orders', $plan_request, self::PARAM_MAP ); | |
| 64 | + $this->plan = Collection_Rules::for_request( 'orders', $request, self::PARAM_MAP ); | |
| 59 | 65 | $params = $this->plan->forwarded_params( $params ); |
| 66 | + // Claim only a string with at least one term. Anything else (an array from | |
| 67 | + // `search[]=`, whitespace only) stays on the forward so wc/v3's own schema | |
| 68 | + // validation answers it, as it did before. | |
| 69 | + $search = $params['search'] ?? null; | |
| 70 | + if ( is_string( $search ) && array() !== Order_Search::terms( $search ) ) { | |
| 71 | + $this->search = $search; | |
| 72 | + unset( $params['search'] ); | |
| 73 | + } | |
| 60 | 74 | $params['dp'] = '6'; |
| 61 | 75 | |
| 62 | 76 | return $params; |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Install the storage-specific POS order search filter. | |
| 81 | + * | |
| 82 | + * @return array<int, array{0: string, 1: callable, 2: int}> | |
| 83 | + */ | |
| 84 | + protected function install(): array { | |
| 85 | + if ( '' === $this->search ) { | |
| 86 | + return array(); | |
| 87 | + } | |
| 88 | + | |
| 89 | + $search = $this->search; | |
| 90 | + // The class arrived after the declared WooCommerce minimum (5.3); without it the | |
| 91 | + // store is on post storage. Same guard as Collection_Rules::detect_storage(). | |
| 92 | + if ( class_exists( OrderUtil::class ) && OrderUtil::custom_orders_table_usage_is_enabled() ) { | |
| 93 | + $filter = static function ( $clauses, $query ) use ( $search ) { | |
| 94 | + $clauses['where'] .= ' AND ' . Order_Search::hpos_where( $search, $query ); | |
| 95 | + return $clauses; | |
| 96 | + }; | |
| 97 | + add_filter( 'woocommerce_orders_table_query_clauses', $filter, 10, 2 ); | |
| 98 | + return array( array( 'woocommerce_orders_table_query_clauses', $filter, 10 ) ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + $filter = static function ( $where, $query ) use ( $search ) { | |
| 102 | + $post_type = is_object( $query ) ? ( $query->query_vars['post_type'] ?? null ) : null; | |
| 103 | + if ( 'shop_order' !== $post_type && ( ! is_array( $post_type ) || ! in_array( 'shop_order', $post_type, true ) ) ) { | |
| 104 | + return $where; | |
| 105 | + } | |
| 106 | + return $where . ' AND ' . Order_Search::posts_where( $search ); | |
| 107 | + }; | |
| 108 | + add_filter( 'posts_where', $filter, 10, 2 ); | |
| 109 | + return array( array( 'posts_where', $filter, 10 ) ); | |
| 63 | 110 | } |
| 64 | 111 | |
| 65 | 112 | /** |
| 66 | 113 | * Run the forward inside the Collection Rules plan for this request. |