Hold donations with flagged donor names for moderation; reconcile lost side effects (schema v6)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
473fe192cd
commit
353f7ca5be
16 files changed
+681
-41
No files matched your search
@@ -60,9 +60,10 @@ final class DonationStore {
|
||||
'message' => $v['message'],
|
||||
'status' => 'pending',
|
||||
'moderation' => $v['moderation'],
|
||||
'moderation_flags' => (string) ( $v['moderation_flags'] ?? '' ),
|
||||
'created_at' => gmdate( 'Y-m-d H:i:s' ),
|
||||
),
|
||||
array( '%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s' )
|
||||
array( '%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%s' )
|
||||
);
|
||||
return $ok ? (int) $wpdb->insert_id : 0;
|
||||
}
|
||||
@@ -98,37 +99,71 @@ final class DonationStore {
|
||||
return 1 === $n;
|
||||
}
|
||||
|
||||
/** Hand a freshly completed donation to the rules engine (if present). Never throws. */
|
||||
public static function notify_applied( int $id ): void {
|
||||
self::call_engine( 'applyDonation', $id );
|
||||
/**
|
||||
* Run every side effect of a completed donation (rules/timer/counter, rewards, tiers, end-at-target) and, only if ALL of
|
||||
* them finished without error, stamp sg_donations.effects_applied_at. Never throws. Every step is idempotent, so this is
|
||||
* also what Maintenance::reconcile_effects() replays for a donation whose request died after mark_completed().
|
||||
*
|
||||
* @return bool True when the donation is (now) marked as fully applied.
|
||||
*/
|
||||
public static function notify_applied( int $id ): bool {
|
||||
$ok = self::call_engine( 'applyDonation', $id );
|
||||
// Donation actions (rewards). Not under the goal lock: events are idempotent via a UNIQUE key. Never throws.
|
||||
try {
|
||||
\StreamGoals\Actions\Recorder::forDonation( $id );
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: Recorder failed for donation ' . $id . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
if ( ! \StreamGoals\Actions\Recorder::forDonation( $id ) ) {
|
||||
$ok = false;
|
||||
}
|
||||
// Goal tiers: unlock every tier this donation reaches. MUST run before the close-at-target check below
|
||||
// (the final tier's target is the goal target). Own try/catch; never throws, never delays a capture.
|
||||
try {
|
||||
\StreamGoals\Tiers\Recorder::forDonation( $id );
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: tier unlock failed for donation ' . $id . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
if ( null === \StreamGoals\Tiers\Recorder::forDonationChecked( $id ) ) {
|
||||
$ok = false;
|
||||
}
|
||||
self::close_goal_if_target_reached( $id );
|
||||
if ( ! self::close_goal_if_target_reached( $id ) ) {
|
||||
$ok = false;
|
||||
}
|
||||
return $ok && self::mark_effects_applied( $id );
|
||||
}
|
||||
|
||||
/** Stamp "side effects applied" once. Only a completed row with a NULL stamp is touched (first stamp wins). */
|
||||
public static function mark_effects_applied( int $id ): bool {
|
||||
global $wpdb;
|
||||
$n = $wpdb->query( $wpdb->prepare( 'UPDATE ' . self::t( 'donations' ) . " SET effects_applied_at = %s WHERE id = %d AND status = 'completed' AND effects_applied_at IS NULL", gmdate( 'Y-m-d H:i:s' ), $id ) );
|
||||
return false !== $n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Completed donations whose side effects were never confirmed: completed, effects_applied_at NULL, a real goal
|
||||
* (test rows use goal_id 0), created between $notBefore and $notAfter (UTC 'Y-m-d H:i:s'; the lower bound keeps a
|
||||
* permanently failing row from being retried forever, the upper bound leaves in-flight requests alone). Oldest first.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public static function unapplied_completed_ids( string $notAfter, string $notBefore, int $limit ): array {
|
||||
global $wpdb;
|
||||
$ids = $wpdb->get_col( $wpdb->prepare(
|
||||
'SELECT id FROM ' . self::t( 'donations' ) . " WHERE status = 'completed' AND effects_applied_at IS NULL AND goal_id > 0 AND created_at < %s AND created_at >= %s ORDER BY id ASC LIMIT %d",
|
||||
$notAfter,
|
||||
$notBefore,
|
||||
max( 1, $limit )
|
||||
) );
|
||||
return array_map( 'intval', (array) $ids );
|
||||
}
|
||||
|
||||
/**
|
||||
* "End at target": runs after the donation is already completed and counted, so it can never refuse or
|
||||
* undo a capture. Failures are logged and swallowed. Idempotent (atomic UPDATE ... WHERE status='active').
|
||||
*
|
||||
* @return bool False when the check failed.
|
||||
*/
|
||||
public static function close_goal_if_target_reached( int $donationId ): void {
|
||||
public static function close_goal_if_target_reached( int $donationId ): bool {
|
||||
try {
|
||||
$row = self::find( $donationId );
|
||||
if ( $row && (int) $row->goal_id > 0 ) {
|
||||
( new \StreamGoals\Core\GoalRepository() )->closeIfTargetReached( (int) $row->goal_id );
|
||||
}
|
||||
return true;
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: end-at-target check failed for donation ' . $donationId . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,10 +173,10 @@ final class DonationStore {
|
||||
return (array) $wpdb->get_results( 'SELECT * FROM ' . self::t( 'goals' ) . " WHERE status = 'complete' AND close_at_target = 1 ORDER BY id DESC LIMIT 20", ARRAY_A );
|
||||
}
|
||||
|
||||
private static function call_engine( string $method, int $id ): void {
|
||||
private static function call_engine( string $method, int $id ): bool {
|
||||
$row = self::find( $id );
|
||||
if ( ! $row ) {
|
||||
return;
|
||||
return false; // vanished row: nothing to apply, and never claim success
|
||||
}
|
||||
try {
|
||||
$donation = \StreamGoals\Core\DonationRepository::toDonation( (array) $row );
|
||||
@@ -151,8 +186,10 @@ final class DonationStore {
|
||||
\StreamGoals\Rules\Engine::default()->$method( $donation );
|
||||
}
|
||||
);
|
||||
return true;
|
||||
} catch ( \Throwable $e ) {
|
||||
error_log( 'StreamGoals: Engine::' . $method . ' failed for donation ' . $id . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user