PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.18
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.18
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / API / V2 / Proxy / Orders_Proxy_Behavior.php

Orders_Proxy_Behavior.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.18, at includes/API/V2/Proxy/Orders_Proxy_Behavior.php

94 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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