| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | namespace WCPOS\WooCommercePOS\API\V2\Writers; |
| 11 | 11 | |
| 12 | 12 | use WCPOS\WooCommercePOS\Interfaces\Collection_Writer_Interface; |
| 13 | 13 | use WP_REST_Request; |
| 14 | +use WP_REST_Response; | |
| 14 | 15 | |
| 15 | 16 | /** Provides the unmodified lifecycle used by ordinary wc/v3 collections. */ |
| 16 | 17 | class Null_Writer implements Collection_Writer_Interface { |
| 17 | 18 | /** Prepare an unchanged create. */ |
| @@ -43,15 +44,57 @@ | ||
| 43 | 44 | public function forward( array $prepared, callable $forward ) { |
| 44 | 45 | return $forward( $prepared['method'], $prepared['route'], $prepared['payload'] ); |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | - /** No-op for pass-through persistence. */ | |
| 48 | - public function persist( string $phase, int $id, array $payload, array $current = array(), array $response_data = array(), array $context = array() ): void { | |
| 48 | + /** No-op after create for pass-through collections. */ | |
| 49 | + public function after_create( int $id, array $payload ): void { | |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | - /** Permanently delete a generic collection record. */ | |
| 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. */ | |
| 52 | 65 | public function delete( array $meta, int $id, array $mutation, callable $dispatch, callable $can_delete ) { |
| 53 | - return $dispatch( $this->delete_request( $meta['route'] . '/' . $id, $id, true ) ); | |
| 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'] ?? '' ); | |
| 54 | 97 | } |
| 55 | 98 | |
| 56 | 99 | /** Use the shared generic document reader. */ |
| 57 | 100 | public function document( array $meta, int $id, callable $default_document ) { |