get_date_modified() ); } /** * Price, stock and status; "missing" for a product that is gone. * * @param ActionDescriptor $action The stored action. */ public function drift_fingerprint( ActionDescriptor $action ): string { $product = self::product( $action ); if ( null === $product ) { return 'missing'; } return implode( '|', array( $product->get_regular_price(), $product->get_sale_price(), (string) $product->get_stock_quantity(), $product->get_status() ) ); } /** * The fields an undo restores, and the stock change the agent asked * for. * * @param ActionDescriptor $action The stored action. * @return array|null */ public function snapshot( ActionDescriptor $action ): ?array { $product = self::product( $action ); if ( null === $product || ! self::undoable( $action ) ) { return null; } $before = array(); foreach ( self::FIELDS as $field ) { $value = $product->{'get_' . $field}( 'edit' ); $before[ $field ] = $value instanceof \DateTimeInterface ? $value->getTimestamp() : $value; } $stock = $action->input['stock_quantity'] ?? null; $requested = is_int( $stock ) || ( is_string( $stock ) && 1 === preg_match( '/^-?\d{1,15}$/', $stock ) ) ? (int) $stock : null; $before['stock_delta'] = null === $requested ? 0 : $requested - (int) $product->get_stock_quantity(); return $before; } /** * Undo: restore from the trash, or put the fields back and reverse the * stock change. * * @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 ) { if ( null === $action->entity_id || 'trash' !== get_post_status( $action->entity_id ) || ! wp_untrash_post( $action->entity_id ) ) { return new WP_Error( 'adafwc_product_not_in_trash', __( 'The product is no longer in the trash.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } // WordPress untrashes as a draft; the snapshot's status is put // back with the other fields below. } $product = self::product( $action ); if ( null === $product ) { return new WP_Error( 'adafwc_product_missing', __( 'The product no longer exists.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } foreach ( self::FIELDS as $field ) { if ( array_key_exists( $field, $before ) ) { $product->{'set_' . $field}( $before[ $field ] ); } } $product->save(); $delta = 'update' === $action->action_cat ? (int) ( $before['stock_delta'] ?? 0 ) : 0; if ( 0 !== $delta && $product->managing_stock() ) { wc_update_product_stock( $product, abs( $delta ), $delta > 0 ? 'decrease' : 'increase' ); } return array( 'stock_adjusted_by' => -$delta ); } /** * How far a product action can be taken back. * * @param ActionDescriptor $action The stored action. */ public function reversibility( ActionDescriptor $action ): Reversibility { if ( 'delete' === $action->action_cat ) { return self::undoable( $action ) ? new Reversibility( Reversibility::REVERSIBLE ) : new Reversibility( Reversibility::IRREVERSIBLE, array( __( 'The product is deleted permanently.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } if ( 'update' !== $action->action_cat ) { return new Reversibility( Reversibility::PARTIALLY_REVERSIBLE, array( __( 'A new product is not removed by undo; delete it if needed.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } return new Reversibility( Reversibility::REVERSIBLE, array( __( 'Stock is adjusted by the amount the agent changed it, so sales made since are kept.', '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 product. * * @param ActionDescriptor $action The stored action. */ private static function product( ActionDescriptor $action ): ?\WC_Product { $product = null === $action->entity_id ? null : wc_get_product( $action->entity_id ); return $product instanceof \WC_Product ? $product : null; } }