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 / Writers / Null_Writer.php

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

105 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Pass-through collection writer.
4 *
5 * @package WCPOS\WooCommercePOS\API\V2\Writers
6 */
7
8 // phpcs:disable Squiz.Commenting, Generic.Commenting -- Lifecycle docblocks are intentionally concise.
9
10 namespace WCPOS\WooCommercePOS\API\V2\Writers;
11
12 use WCPOS\WooCommercePOS\Interfaces\Collection_Writer_Interface;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 /** Provides the unmodified lifecycle used by ordinary wc/v3 collections. */
17 class Null_Writer implements Collection_Writer_Interface {
18 /** Prepare an unchanged create. */
19 public function prepare_create( array $meta, array $payload, callable $validate_tax_ids ) {
20 return array(
21 'method' => 'POST',
22 'route' => $meta['route'],
23 'payload' => $payload,
24 'context' => array(),
25 );
26 }
27
28 /** Prepare an unchanged update. */
29 public function prepare_update( array $meta, int $id, array $payload, callable $validate_tax_ids ) {
30 return array(
31 'method' => 'PUT',
32 'route' => $meta['route'] . '/' . $id,
33 'payload' => $payload,
34 'context' => array(),
35 );
36 }
37
38 /** Accept an existing target unchanged. */
39 public function validate_existing_create( int $id, array $payload, array $prepared ) {
40 return null;
41 }
42
43 /** Forward without collection hooks. */
44 public function forward( array $prepared, callable $forward ) {
45 return $forward( $prepared['method'], $prepared['route'], $prepared['payload'] );
46 }
47
48 /** No-op for pass-through persistence. */
49 public function persist( string $phase, int $id, array $payload, array $current = array(), array $response_data = array(), array $context = array() ): void {
50 }
51
52 /** Delete a generic collection record, honouring the envelope's `force` flag. */
53 public function delete( array $meta, int $id, array $mutation, callable $dispatch, callable $can_delete ) {
54 $route = $meta['route'] . '/' . $id;
55 if ( isset( $mutation['force'] ) ) {
56 return $dispatch( $this->delete_request( $route, $id, (bool) $mutation['force'] ) );
57 }
58
59 $response = $dispatch( $this->delete_request( $route, $id, false ) );
60
61 return $this->trash_not_supported( $response ) ? $dispatch( $this->delete_request( $route, $id, true ) ) : $response;
62 }
63
64 /**
65 * Whether wc/v3 refused a trash (`force=false`) because the record cannot be trashed.
66 *
67 * A delete without `force` asks WooCommerce to trash and lets WooCommerce decide whether it
68 * can: products and coupons trash unless the site disabled it (`EMPTY_TRASH_DAYS`) or an
69 * extension opted the type out (`woocommerce_rest_{type}_object_trashable`); terms and users
70 * have no trash at all. Re-deriving that predicate here would drift from WooCommerce's, so the
71 * writer reads the answer off the 501 and only then deletes permanently. The refused attempt
72 * has no side effects — every wc/v3 controller checks `force` before touching the record. An
73 * explicit envelope value is never retried: `force:false` on a term returns the 501 as-is.
74 * Variations never reach this path — {@see Variation_Writer::delete()} forces, as they cannot trash.
75 *
76 * @param mixed $response The forwarded delete's response.
77 */
78 protected function trash_not_supported( $response ): bool {
79 if ( ! $response instanceof WP_REST_Response || 501 !== $response->get_status() ) {
80 return false;
81 }
82 $data = $response->get_data();
83
84 return \is_array( $data ) && 'woocommerce_rest_trash_not_supported' === ( $data['code'] ?? '' );
85 }
86
87 /** Use the shared generic document reader. */
88 public function document( array $meta, int $id, callable $default_document ) {
89 return $default_document( $meta, $id, array() );
90 }
91
92 /** Use the shared generic response builder. */
93 public function build_response_document( array $bare, string $record_id, array $meta, int $id, callable $default_builder ): array {
94 return $default_builder( $bare, $record_id, $meta, $id );
95 }
96
97 /** Build a delete request with authoritative route parameters. */
98 protected function delete_request( string $route, int $id, bool $force ): WP_REST_Request {
99 $request = new WP_REST_Request( 'DELETE', $route );
100 $request->set_param( 'id', $id );
101 $request->set_param( 'force', $force );
102 return $request;
103 }
104 }
105