PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.19
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.19
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.19, at includes/API/V2/Writers/Null_Writer.php

117 lines 4.4 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 after create for pass-through collections. */
49 public function after_create( int $id, array $payload ): void {
50 }
51
52 /** No-op after identity for pass-through collections. */
53 public function after_identity( int $id, array $payload ): void {
54 }
55
56 /** No-op after recovery for pass-through collections. */
57 public function after_recovery( int $id, array $payload ): void {
58 }
59
60 /** No-op after update for pass-through collections. */
61 public function after_update( int $id, array $payload, array $current, array $response_data, array $context ): void {
62 }
63
64 /** Delete a generic collection record, honouring the envelope's `force` flag. */
65 public function delete( array $meta, int $id, array $mutation, callable $dispatch, callable $can_delete ) {
66 $route = $meta['route'] . '/' . $id;
67 if ( isset( $mutation['force'] ) ) {
68 return $dispatch( $this->delete_request( $route, $id, (bool) $mutation['force'] ) );
69 }
70
71 $response = $dispatch( $this->delete_request( $route, $id, false ) );
72
73 return $this->trash_not_supported( $response ) ? $dispatch( $this->delete_request( $route, $id, true ) ) : $response;
74 }
75
76 /**
77 * Whether wc/v3 refused a trash (`force=false`) because the record cannot be trashed.
78 *
79 * A delete without `force` asks WooCommerce to trash and lets WooCommerce decide whether it
80 * can: products and coupons trash unless the site disabled it (`EMPTY_TRASH_DAYS`) or an
81 * extension opted the type out (`woocommerce_rest_{type}_object_trashable`); terms and users
82 * have no trash at all. Re-deriving that predicate here would drift from WooCommerce's, so the
83 * writer reads the answer off the 501 and only then deletes permanently. The refused attempt
84 * has no side effects — every wc/v3 controller checks `force` before touching the record. An
85 * explicit envelope value is never retried: `force:false` on a term returns the 501 as-is.
86 * Variations never reach this path — {@see Variation_Writer::delete()} forces, as they cannot trash.
87 *
88 * @param mixed $response The forwarded delete's response.
89 */
90 protected function trash_not_supported( $response ): bool {
91 if ( ! $response instanceof WP_REST_Response || 501 !== $response->get_status() ) {
92 return false;
93 }
94 $data = $response->get_data();
95
96 return \is_array( $data ) && 'woocommerce_rest_trash_not_supported' === ( $data['code'] ?? '' );
97 }
98
99 /** Use the shared generic document reader. */
100 public function document( array $meta, int $id, callable $default_document ) {
101 return $default_document( $meta, $id, array() );
102 }
103
104 /** Use the shared generic response builder. */
105 public function build_response_document( array $bare, string $record_id, array $meta, int $id, callable $default_builder ): array {
106 return $default_builder( $bare, $record_id, $meta, $id );
107 }
108
109 /** Build a delete request with authoritative route parameters. */
110 protected function delete_request( string $route, int $id, bool $force ): WP_REST_Request {
111 $request = new WP_REST_Request( 'DELETE', $route );
112 $request->set_param( 'id', $id );
113 $request->set_param( 'force', $force );
114 return $request;
115 }
116 }
117