, string, bool): EffectOutcome */ private Closure $decide; /** * Constructor. * * @param (Closure(ActionDescriptor, array, string, bool): EffectOutcome)|null $decide Effect decision; defaults to the parent's API. */ public function __construct( ?Closure $decide = null ) { $this->decide = $decide ?? static fn( ActionDescriptor $action, array $envelope, string $provider, bool $independent ): EffectOutcome => Api::hold_at_effect( $action, $envelope, $provider, $independent ); } /** * Hook every guarded write. */ public function register(): void { foreach ( self::HOOKED as $type ) { add_action( 'woocommerce_before_' . $type . '_object_save', array( $this, 'guard' ), 10, 1 ); add_action( 'woocommerce_after_' . $type . '_object_save', array( $this, 'finished' ), 10, 1 ); } add_action( 'woocommerce_pre_payment_complete', array( $this, 'guard_payment' ), 10, 2 ); add_action( 'woocommerce_payment_complete', array( $this, 'payment_done' ), 10, 1 ); foreach ( self::PAYMENT_ENDS as $status ) { add_action( 'woocommerce_payment_complete_order_status_' . $status, array( $this, 'payment_done' ), 10, 1 ); } add_action( 'woocommerce_product_before_set_stock', array( $this, 'guard_stock' ), 10, 1 ); add_action( 'woocommerce_variation_before_set_stock', array( $this, 'guard_stock' ), 10, 1 ); } /** * Decide a save about to be written; veto it unless it may proceed. * * @param mixed $data The object being saved. * @throws HeldException When the firewall holds or denies the write. */ public function guard( $data ): void { // Every store save passes here: anything not an agent's leaves at // once, before the object is even read. if ( null === Api::current_agent() ) { return; } if ( $data instanceof \WC_Order_Item ) { $this->guard_item( $data ); return; } if ( $data instanceof \WC_Order_Refund ) { $this->guard_refund( $data ); return; } $data = self::guarded( $data ); if ( null === $data ) { return; } if ( InFlight::was_vetoed( $data ) ) { self::veto( $data, new EffectOutcome( 'DENY', 'vetoed_earlier' ) ); } InFlight::restart( $data ); $type = (string) self::type_of( $data ); $id = (int) $data->get_id(); if ( $id > 0 && 'order' === $type && InFlight::covers( 'order', $id ) ) { return; } $changes = $data->get_changes(); $meta = Changes::meta( $data ); $items = $data instanceof \WC_Order ? Changes::items( $data ) : array(); if ( $id > 0 && array() === $changes && array() === $meta && array() === $items ) { return; } $input = Changes::input( $changes ) + array( 'hook' => (string) current_action() ); if ( array() !== $meta ) { $input['meta'] = Changes::input( $meta ); } if ( array() !== $items ) { $input['items_changed'] = count( $items ); } $envelope = array( 'type' => $type, 'id' => $id, 'class' => get_class( $data ), 'changes' => Changes::envelope( $changes ), 'meta' => Changes::envelope( $meta ), ); if ( array() !== $items ) { // Line items are objects: a held item change cannot be stored // whole, so the parent denies it rather than replay half. $envelope['items'] = $items; } $outcome = $this->decide( new ActionDescriptor( 'unclassified', $id > 0 ? 'update' : 'create', $type, $id > 0 ? $id : null, null, $input ), $envelope, self::PROVIDER_ID, $data instanceof \WC_Order && array_key_exists( 'status', $changes ) ); if ( $outcome->vetoes() ) { self::veto( $data, $outcome ); } if ( null !== $outcome->decision ) { InFlight::saving( $data ); } } /** * A save the backstop allowed has finished: its own consequences are * over. Only a cover this class started is ended. * * @param mixed $data The object saved. */ public function finished( $data ): void { if ( is_object( $data ) ) { InFlight::saved( $data ); } } /** * Decide payment_complete() before it saves the order and fires * `woocommerce_payment_complete`, which reduces stock: vetoed there, * WooCommerce catches the exception and returns false with nothing done. * * @param mixed $order_id The order. * @param mixed $transaction_id The gateway's transaction id. * @throws HeldException When the firewall holds or denies it. */ public function guard_payment( $order_id, $transaction_id = '' ): void { $order_id = absint( $order_id ); if ( null === Api::current_agent() || 0 === $order_id || InFlight::covers( 'order', $order_id ) ) { return; } $outcome = $this->decide( new ActionDescriptor( 'unclassified', 'update', 'order', $order_id, null, array( 'set_paid' => true, 'hook' => 'woocommerce_pre_payment_complete', ) ), array( 'order_id' => $order_id, 'transaction_id' => is_scalar( $transaction_id ) ? (string) $transaction_id : '', ), self::PAYMENT_PROVIDER_ID, true ); if ( $outcome->vetoes() ) { throw new HeldException( $outcome->held() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- A bool; HeldException escapes its message. } if ( null !== $outcome->decision ) { InFlight::order( $order_id ); } } /** * payment_complete() finished, whichever branch it took. * * @param mixed $order_id The order. */ public function payment_done( $order_id ): void { InFlight::order_done( absint( $order_id ) ); } /** * Decide wc_update_product_stock() before its SQL moves the stock. The * new quantity is not passed to this hook, so the stock facts are * unknown and a stock rule fires; a held change cannot be replayed, so * the parent denies it. Stock an allowed refund, payment or save moves * is that write's consequence. * * @param mixed $product The product. * @throws HeldException When the firewall holds or denies it. */ public function guard_stock( $product ): void { if ( null === Api::current_agent() || ! $product instanceof \WC_Product || InFlight::any_order() ) { return; } $type = (string) self::type_of( $product ); $id = (int) $product->get_id(); if ( InFlight::covers( $type, $id ) ) { return; } $outcome = $this->decide( new ActionDescriptor( 'unclassified', 'update', $type, $id, null, array( 'stock_quantity' => null, 'hook' => (string) current_action(), ) ), array( 'product' => $product ), self::PROVIDER_ID ); if ( $outcome->vetoes() ) { throw new HeldException( $outcome->held() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- A bool; HeldException escapes its message. } } /** * A line item saved on its own changes its order. Items saved by an * allowed save of their order are that save's. * * @param \WC_Order_Item $item The item. * @throws HeldException When the firewall holds or denies it. */ private function guard_item( \WC_Order_Item $item ): void { $order_id = (int) $item->get_order_id(); if ( $order_id > 0 && InFlight::covers( 'order', $order_id ) ) { return; } $changes = $item->get_changes(); if ( $item->get_id() > 0 && array() === $changes && array() === Changes::meta( $item ) ) { return; } $outcome = $this->decide( new ActionDescriptor( 'unclassified', 'update', 'order', $order_id > 0 ? $order_id : null, null, array( 'item_type' => $item->get_type(), 'item' => Changes::input( $changes ), 'hook' => (string) current_action(), ) ), array( 'item' => $item ), self::PROVIDER_ID ); if ( $outcome->vetoes() ) { self::veto( $item, $outcome ); } } /** * A refund saved outside wc_create_refund() is a refund record the * gateway never sees, changing the order's refunded total and reports. * One inside wc_create_refund() is the refund guard's (WC-E4). * * @param \WC_Order_Refund $refund The refund. * @throws HeldException When the firewall holds or denies it. */ private function guard_refund( \WC_Order_Refund $refund ): void { if ( in_array( 'wc_create_refund', array_column( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), 'function' ), true ) ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- The one way to tell wc_create_refund()'s own saves, which have no hook before the first. return; } $order_id = (int) $refund->get_parent_id(); $outcome = $this->decide( new ActionDescriptor( 'unclassified', $refund->get_id() > 0 ? 'update' : 'create', 'refund', $order_id > 0 ? $order_id : null, null, array( 'amount' => (string) $refund->get_amount(), 'api_refund' => false, 'hook' => (string) current_action(), ) ), array( 'refund' => $refund ), self::PROVIDER_ID, true ); if ( $outcome->vetoes() ) { self::veto( $refund, $outcome ); } } /** * Ask the firewall, failing closed. * * The firewall decides an agent request once (one decision per request, * decided 2026-10-22): a dependent effect inside a request it already * allowed goes undecided. What cannot be undone is decided on its own * all the same - an order's status change (emails, stock, download * access), marking an order paid, a refund record - as the refund guard * decides refunds. What undo covers - prices, stock, meta, line items - * stays dependent. * * @param ActionDescriptor $action The write. * @param array $envelope What a replay needs. * @param string $provider The replay provider. * @param bool $independent Whether it cannot be undone. */ private function decide( ActionDescriptor $action, array $envelope, string $provider, bool $independent = false ): EffectOutcome { try { return ( $this->decide )( $action, $envelope, $provider, $independent ); } catch ( \Throwable $failure ) { return new EffectOutcome( 'DENY', 'unavailable' ); } } /** * Veto a save: leave the object with nothing pending, remember the * veto, and throw. * * @param \WC_Order|\WC_Product|\WC_Coupon|\WC_Order_Item|\WC_Order_Refund $data The object. * @param EffectOutcome $outcome The decision. * @throws HeldException Always. */ private static function veto( \WC_Order|\WC_Product|\WC_Coupon|\WC_Order_Item|\WC_Order_Refund $data, EffectOutcome $outcome ): never { self::neutralize( $data ); InFlight::veto( $data ); throw new HeldException( $outcome->held() ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- A bool; HeldException escapes its message. } /** * Drop a vetoed object's pending properties, meta, line items and * status transition. All are protected; this is the only way to make * the save write nothing, and to keep WC_Order::save() - which catches * the exception - from firing status hooks for a status it never * stored, or a retried save from writing what was vetoed. * * @param \WC_Order|\WC_Product|\WC_Coupon|\WC_Order_Item|\WC_Order_Refund $data The object. */ public static function neutralize( \WC_Order|\WC_Product|\WC_Coupon|\WC_Order_Item|\WC_Order_Refund $data ): void { ( function (): void { $this->changes = array(); if ( property_exists( $this, 'status_transition' ) ) { $this->status_transition = false; } if ( property_exists( $this, 'meta_data' ) ) { $this->meta_data = null; } if ( property_exists( $this, 'items' ) ) { $this->items = array(); } if ( property_exists( $this, 'items_to_delete' ) ) { $this->items_to_delete = array(); } } )->call( $data ); } /** * The object, when it is one whose saves the backstop decides. * * @param mixed $data What the hook passed. */ public static function guarded( mixed $data ): \WC_Order|\WC_Product|\WC_Coupon|null { return $data instanceof \WC_Order || $data instanceof \WC_Product || $data instanceof \WC_Coupon ? $data : null; } /** * The entity type of a WooCommerce object, if the backstop guards it. * * @param object $data The object. */ public static function type_of( object $data ): ?string { return match ( true ) { $data instanceof \WC_Product_Variation => 'product_variation', $data instanceof \WC_Product => 'product', $data instanceof \WC_Order => 'order', $data instanceof \WC_Coupon => 'coupon', default => null, }; } }