PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.2
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.2
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 / Sync / Product_Serializer.php

Product_Serializer.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.2, at includes/Sync/Product_Serializer.php

224 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WCPOS sync product serializer.
4 *
5 * @package WCPOS\WooCommercePOS\Sync
6 */
7
8 namespace WCPOS\WooCommercePOS\Sync;
9
10 use WC_Product;
11 use WC_Product_Variation;
12 use WC_REST_Product_Variations_Controller;
13 use WC_REST_Products_Controller;
14 use WP_REST_Request;
15
16 /**
17 * THE product-record assembly line.
18 *
19 * Every sync surface that needs a single product (or variation) document builds
20 * it here: the changes revision-hash walk, the barcode resolver, the targeted
21 * variations read and the write acknowledgement. Before this class each of those
22 * pasted the same four lines — controller instantiation, `prepare_object_for_response`,
23 * `rest_get_server()->response_to_data()`, then the augmentation filter — and the
24 * copies drifted (see the namespace-resolution comment the variations controller
25 * used to carry).
26 *
27 * Two rules the pasted blocks encoded and this class keeps:
28 *
29 * 1. ADR 0003 — values come from the FILTERED WC REST representation, never a raw
30 * projection. `prepare_object_for_response` runs WooCommerce's own
31 * `woocommerce_rest_prepare_product_object` filter; `response_to_data` resolves
32 * embedded links exactly as a real request would.
33 * 2. Variations are serialized through the SAME products controller as products —
34 * `wc_get_product()` hands back a `WC_Product_Variation` and the controller
35 * handles it, so the two lanes cannot drift apart.
36 */
37 final class Product_Serializer {
38 /**
39 * The WooCommerce products controller, created on first use.
40 *
41 * Memoized because the callers serialize in LOOPS (a revision-hash page, an
42 * include-set of variations); instantiating a controller per record was never
43 * the intent of the pasted blocks — each of them hoisted it out of the loop.
44 *
45 * @var null|WC_REST_Products_Controller
46 */
47 private $controller = null;
48
49 /**
50 * The WooCommerce product VARIATIONS controller, created on first use.
51 *
52 * A variation is not a product. WooCommerce serves it from its own controller, whose
53 * response carries `image` (singular), `wc_get_formatted_variation()` as the name, and
54 * none of the ~25 product-only fields (`categories`, `related_ids`, `price_html`, …)
55 * that mean nothing on a variation. 1.9.x served exactly that shape from
56 * `API\V1\Product_Variations_Controller`; hydrating variations through the PRODUCTS
57 * controller instead is what dropped `image` and blanked every variation thumbnail in
58 * the POS on 1.10.0 (#1710).
59 *
60 * @var null|WC_REST_Product_Variations_Controller
61 */
62 private $variations_controller = null;
63
64 /**
65 * The request used when a caller does not supply one.
66 *
67 * @var null|WP_REST_Request
68 */
69 private $default_request = null;
70
71 /**
72 * Serialize one product or variation into its augmented REST representation.
73 *
74 * @param int|WC_Product $product Product id, or an already-loaded product/variation object.
75 * @param null|WP_REST_Request $request Serialization context. A bare `GET /` request is used when omitted.
76 *
77 * @return array The augmented payload, or an empty array when the id does not resolve to a product.
78 */
79 public function serialize( $product, ?WP_REST_Request $request = null ): array {
80 $object = $product instanceof WC_Product
81 ? $product
82 : ( \function_exists( 'wc_get_product' ) ? wc_get_product( (int) $product ) : false );
83
84 if ( ! $object instanceof WC_Product ) {
85 return array();
86 }
87
88 $request = $request instanceof WP_REST_Request ? $request : $this->default_request();
89 // Every lane that hydrates a product — changes, resolve, targeted
90 // variations, the write ack — builds a bare `GET /` and hands it here, so
91 // this is the ONE place that has to carry the till's store scope into
92 // `woocommerce_rest_prepare_product_object`. Without it the assembly line
93 // serializes the global price and the till redisplays it moments after the
94 // cashier changed the store's (pro#425). Stamping is idempotent and never
95 // overrides a scope the caller set deliberately.
96 Store_Scope::stamp( $request );
97 // Ours for the duration of the serialization — this runs outside any
98 // dispatch, so the lane marker is the only signal a response filter has.
99 $is_variation = $object instanceof WC_Product_Variation;
100 if ( $is_variation ) {
101 // `prepare_links()` reads `$request['product_id']` to build the nested
102 // `products/<parent>/variations/<id>` route. The lanes that hydrate here build a
103 // bare `GET /`, so without this the links would claim parent 0.
104 $request->set_param( 'product_id', $object->get_parent_id() );
105 }
106 // Store scope is carried by the request + lane marker above, both controller-agnostic,
107 // and Pro registers `bake_store_prices` on the product AND variation prepare filters —
108 // so store-scoped prices ride either controller (pro#425).
109 $controller = $is_variation ? $this->variations_controller() : $this->controller();
110 $response = Store_Scope::in_v2_lane(
111 function () use ( $controller, $object, $request ) {
112 return rest_ensure_response( $controller->prepare_object_for_response( $object, $request ) );
113 }
114 );
115 /**
116 * WordPress response data is not guaranteed to be an array at runtime.
117 *
118 * @var mixed $payload
119 */
120 $payload = rest_get_server()->response_to_data( $response, false );
121 if ( $is_variation && \is_array( $payload ) ) {
122 $payload = self::backfill_pre_wc83_variation_fields( $payload, $object );
123 }
124
125 return self::augment( \is_array( $payload ) ? $payload : array(), $object, $request );
126 }
127
128 /**
129 * `name` and `parent_id` on WooCommerce older than 8.3.
130 *
131 * WooCommerce added both to the VARIATIONS controller's response in 8.3; the products
132 * controller has always emitted them. So moving variations onto their own controller would
133 * silently drop two client-required fields on WooCommerce 5.3–8.2 — and this plugin still
134 * declares `WC requires at least: 5.3`. The client reads `payload.name` for the variation row
135 * title and `parent_id` to resolve the parent after a scan.
136 *
137 * The same backfill the v1 lane has always carried
138 * (`API\V1\Product_Variations_Controller::wcpos_variation_response`), for the same reason.
139 *
140 * @param array $payload Serialized variation payload.
141 * @param WC_Product_Variation $object The variation backing it.
142 */
143 private static function backfill_pre_wc83_variation_fields( array $payload, $object ): array {
144 if ( ! isset( $payload['parent_id'] ) ) {
145 $payload['parent_id'] = $object->get_parent_id();
146 }
147 if ( ! isset( $payload['name'] ) ) {
148 $payload['name'] = \function_exists( 'wc_get_formatted_variation' )
149 ? wc_get_formatted_variation( $object, true, false, false )
150 : '';
151 }
152
153 return $payload;
154 }
155
156 /**
157 * Run the public augmentation filter over an already-serialized payload.
158 *
159 * Exposed separately for the write acknowledgement, which already holds the
160 * bare wc/v3 data (it must hash the bare bytes for the conflict check) and only
161 * needs the stamps applied on top.
162 *
163 * @param array $payload Serialized product payload.
164 * @param mixed $object The product/variation backing the payload.
165 * @param null|WP_REST_Request $request Serialization context.
166 */
167 public static function augment( array $payload, $object = null, ?WP_REST_Request $request = null ): array {
168 if ( $request instanceof WP_REST_Request ) {
169 Store_Scope::stamp( $request );
170 }
171
172 /**
173 * Filters a serialized WCPOS product record.
174 *
175 * Additive only: it must never remove WooCommerce REST fields.
176 *
177 * @param array $payload Serialized product payload.
178 * @param mixed $object The product or variation backing the payload.
179 * @param null|WP_REST_Request $request Serialization context.
180 */
181 /**
182 * Public filters can return values outside the documented contract.
183 *
184 * @var mixed $augmented
185 */
186 $augmented = apply_filters( 'woocommerce_pos_sync_serialized_product', $payload, $object, $request );
187
188 return \is_array( $augmented ) ? $augmented : $payload;
189 }
190
191 /**
192 * The memoized WooCommerce products controller.
193 */
194 private function controller(): WC_REST_Products_Controller {
195 if ( null === $this->controller ) {
196 $this->controller = new WC_REST_Products_Controller();
197 }
198
199 return $this->controller;
200 }
201
202 /**
203 * The memoized WooCommerce product variations controller.
204 */
205 private function variations_controller(): WC_REST_Product_Variations_Controller {
206 if ( null === $this->variations_controller ) {
207 $this->variations_controller = new WC_REST_Product_Variations_Controller();
208 }
209
210 return $this->variations_controller;
211 }
212
213 /**
214 * The memoized fallback serialization request.
215 */
216 private function default_request(): WP_REST_Request {
217 if ( null === $this->default_request ) {
218 $this->default_request = new WP_REST_Request( 'GET', '/' );
219 }
220
221 return $this->default_request;
222 }
223 }
224