Files
wp-donations/includes/Tiers/Recorder.php
T
shadowdaoandClaude Sonnet 5.5 353f7ca5be
Test and Release / test (push) Successful in 1m28s
Test and Release / release (push) Successful in 7s
Hold donations with flagged donor names for moderation; reconcile lost side effects (schema v6)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 17:07:32 -08:00

39 lines
1.5 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tiers;
/** Unlocks the tiers a completed donation reaches. Runs BEFORE the "end at target" check. */
final class Recorder {
/**
* Never throws. Idempotent (atomic UPDATE ... WHERE reached_at IS NULL per tier). Test donations (goal_id 0) are skipped.
*
* @return int[] ids of the tiers this call unlocked.
*/
public static function forDonation( int $donationId ): array {
return self::forDonationChecked( $donationId ) ?? array();
}
/** Same as forDonation() but null (instead of an empty list) when something failed. Never throws. @return int[]|null */
public static function forDonationChecked( int $donationId ): ?array {
try {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT id, goal_id, status FROM ' . $wpdb->prefix . 'sg_donations WHERE id = %d', $donationId ), ARRAY_A );
if ( ! is_array( $row ) || 'completed' !== ( $row['status'] ?? '' ) || (int) $row['goal_id'] <= 0 ) {
return array();
}
$goalId = (int) $row['goal_id'];
$repo = new TierRepository( $wpdb );
if ( ! $repo->forGoal( $goalId ) ) {
return array();
}
$raised = ( new \StreamGoals\Core\GoalRepository( $wpdb ) )->raised_minor( $goalId );
return $repo->markReached( $goalId, $raised, $donationId );
} catch ( \Throwable $e ) {
error_log( 'StreamGoals: tier unlock failed for donation ' . $donationId . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
return null;
}
}
}