get_date_modified() ); } /** * The discount it gives; "missing" for a coupon that is gone. * * @param ActionDescriptor $action The stored action. */ public function drift_fingerprint( ActionDescriptor $action ): string { $coupon = self::coupon( $action ); return null === $coupon ? 'missing' : implode( '|', array( $coupon->get_discount_type(), $coupon->get_amount(), (string) $coupon->get_usage_limit(), $coupon->get_status() ) ); } /** * Every property an undo restores. * * @param ActionDescriptor $action The stored action. * @return array|null */ public function snapshot( ActionDescriptor $action ): ?array { $coupon = self::coupon( $action ); if ( null === $coupon || ! self::undoable( $action ) ) { return null; } $before = array(); foreach ( self::PROPS as $prop ) { $value = $coupon->{'get_' . $prop}( 'edit' ); $before[ $prop ] = $value instanceof \DateTimeInterface ? $value->getTimestamp() : $value; } return $before; } /** * Undo: restore from the trash, then put every property 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 { if ( 'delete' === $action->action_cat && ( null === $action->entity_id || 'trash' !== get_post_status( $action->entity_id ) || ! wp_untrash_post( $action->entity_id ) ) ) { return new WP_Error( 'adafwc_coupon_not_in_trash', __( 'The coupon is no longer in the trash.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } $coupon = self::coupon( $action ); if ( null === $coupon ) { return new WP_Error( 'adafwc_coupon_missing', __( 'The coupon no longer exists.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } foreach ( self::PROPS as $prop ) { if ( array_key_exists( $prop, $before ) ) { $coupon->{'set_' . $prop}( $before[ $prop ] ); } } $coupon->save(); return array( 'restored' => $coupon->get_id() ); } /** * How far a coupon action can be taken back. * * @param ActionDescriptor $action The stored action. */ public function reversibility( ActionDescriptor $action ): Reversibility { if ( self::undoable( $action ) ) { return new Reversibility( Reversibility::REVERSIBLE, array( __( 'Orders that used the coupon since keep their discount.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } return 'delete' === $action->action_cat ? new Reversibility( Reversibility::IRREVERSIBLE, array( __( 'The coupon is deleted permanently.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ) : new Reversibility( Reversibility::PARTIALLY_REVERSIBLE, array( __( 'A new coupon is not removed by undo; delete it if needed.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } /** * Whether undo is offered: an update, or a move to the trash. * * @param ActionDescriptor $action The stored action. */ private static function undoable( ActionDescriptor $action ): bool { return 'update' === $action->action_cat || ( 'delete' === $action->action_cat && ! WcRouteClassifier::truthy( $action->input['force'] ?? null ) ); } /** * The action's coupon. * * @param ActionDescriptor $action The stored action. */ private static function coupon( ActionDescriptor $action ): ?\WC_Coupon { if ( null === $action->entity_id ) { return null; } $coupon = new \WC_Coupon( $action->entity_id ); return $coupon->get_id() > 0 ? $coupon : null; } }