get_date_modified() ); } /** * Status and total; "missing" for an order that is gone. * * @param ActionDescriptor $action The stored action. */ public function drift_fingerprint( ActionDescriptor $action ): string { $order = self::order( $action ); return null === $order ? 'missing' : $order->get_status() . '|' . wc_format_decimal( $order->get_total() ); } /** * Before a move to the trash, the status it had. * * @param ActionDescriptor $action The stored action. * @return array|null */ public function snapshot( ActionDescriptor $action ): ?array { $order = self::order( $action ); if ( null === $order || ! self::trashes( $action ) ) { return null; } return array( 'status' => $order->get_status() ); } /** * Take a move to the trash back. * * @param ActionDescriptor $action The stored action. * @param array $before The snapshot. * @return array|WP_Error */ public function restore( ActionDescriptor $action, array $before ): array|WP_Error { $order = self::order( $action ); if ( null === $order || 'trash' !== $order->get_status() ) { return new WP_Error( 'adafwc_order_not_in_trash', __( 'The order is no longer in the trash.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } if ( ! $order->untrash() ) { return new WP_Error( 'adafwc_order_untrash_failed', __( 'WooCommerce could not restore the order from the trash.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } $order->add_order_note( __( 'Restored from the trash by Agent Firewall (undo of an AI agent\'s action).', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); return array( 'status' => $order->get_status() ); } /** * How far an order action can be taken back. * * @param ActionDescriptor $action The stored action. */ public function reversibility( ActionDescriptor $action ): Reversibility { if ( 'delete' === $action->action_cat ) { return self::trashes( $action ) ? new Reversibility( Reversibility::REVERSIBLE ) : new Reversibility( Reversibility::IRREVERSIBLE, array( __( 'The order is deleted permanently.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } // What a status change does beyond the status. return new Reversibility( Reversibility::PARTIALLY_REVERSIBLE, array( __( 'Emails sent to the customer and the store are not unsent.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), __( 'Stock reduced or restored is not moved back.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), __( 'Download permissions granted or revoked are not changed back.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), ) ); } /** * Whether a delete moves the order to the trash rather than deleting it. * * @param ActionDescriptor $action The stored action. */ private static function trashes( ActionDescriptor $action ): bool { return 'delete' === $action->action_cat && ! WcRouteClassifier::truthy( $action->input['force'] ?? null ); } /** * The action's order. * * @param ActionDescriptor $action The stored action. */ private static function order( ActionDescriptor $action ): ?\WC_Order { $order = null === $action->entity_id ? null : wc_get_order( $action->entity_id ); return $order instanceof \WC_Order ? $order : null; } }