| 1 |
<?php |
| 2 |
/** |
| 3 |
* Order catalog proxy behavior. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\API\V2\Proxy |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V2\Proxy; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Sync\Collection_Rules; |
| 11 |
use WCPOS\WooCommercePOS\Sync\Order_Serializer; |
| 12 |
use WP_REST_Request; |
| 13 |
|
| 14 |
/** |
| 15 |
* Applies the order Collection Rules plan and v2 order wire shape. |
| 16 |
*/ |
| 17 |
final class Orders_Proxy_Behavior extends Scoped_Proxy_Behavior { |
| 18 |
/** |
| 19 |
* Proxy request keys claimed by the order Collection Rules plan. |
| 20 |
*/ |
| 21 |
private const PARAM_MAP = array( |
| 22 |
'orderby' => 'orderby', |
| 23 |
'order' => 'order', |
| 24 |
'search' => 'search', |
| 25 |
'pos_cashier' => 'pos_cashier', |
| 26 |
'pos_store' => 'pos_store', |
| 27 |
'created_via' => 'created_via', |
| 28 |
'include' => array( |
| 29 |
'key' => 'include', |
| 30 |
'when' => 'search', |
| 31 |
'parse' => 'id_list', |
| 32 |
), |
| 33 |
'exclude' => array( |
| 34 |
'key' => 'exclude', |
| 35 |
'when' => 'search', |
| 36 |
'parse' => 'id_list', |
| 37 |
), |
| 38 |
); |
| 39 |
|
| 40 |
/** |
| 41 |
* The Collection Rules plan for the request in flight. |
| 42 |
* |
| 43 |
* @var null|\WCPOS\WooCommercePOS\Sync\Collection_Rules_Plan |
| 44 |
*/ |
| 45 |
private $plan; |
| 46 |
|
| 47 |
/** |
| 48 |
* Claim the orders params the rules table owns, then ask wc/v3 for full precision. |
| 49 |
* |
| 50 |
* @param array $params Query parameters to forward. |
| 51 |
* @param WP_REST_Request $request Original proxy request. |
| 52 |
* |
| 53 |
* @return array |
| 54 |
*/ |
| 55 |
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 ); |
| 59 |
$params = $this->plan->forwarded_params( $params ); |
| 60 |
$params['dp'] = '6'; |
| 61 |
|
| 62 |
return $params; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Run the forward inside the Collection Rules plan for this request. |
| 67 |
* |
| 68 |
* @param callable $forward Forward operation. |
| 69 |
* |
| 70 |
* @return mixed |
| 71 |
*/ |
| 72 |
protected function run( callable $forward ) { |
| 73 |
return null === $this->plan ? $forward() : $this->plan->around( $forward ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Apply the shared v2 order augmentations to every row. |
| 78 |
* |
| 79 |
* @param mixed $data Response data. |
| 80 |
* |
| 81 |
* @return mixed |
| 82 |
*/ |
| 83 |
public function post_process( $data ) { |
| 84 |
$serializer = new Order_Serializer(); |
| 85 |
foreach ( (array) $data as $index => $payload ) { |
| 86 |
if ( is_array( $payload ) ) { |
| 87 |
$data[ $index ] = $serializer->document( $payload, Order_Serializer::V2_AUGMENTATIONS ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $data; |
| 92 |
} |
| 93 |
} |
| 94 |
|