End-at-target goals (auto-close when target reached) and fit-to-window rewards menu overlay
- Core\GoalTargetPolicy; atomic close in the capture path; friendly 'goal reached' message - In-flight checkouts still capture; new orders for a closed goal are refused - Rewards menu overlay: card hugs content, text scales to the window, resize-aware, scroll only as a last resort Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1c6d93ad5f
commit
5ea1e1d273
9 files changed
+216
-19
No files matched your search
@@ -6,7 +6,7 @@ namespace StreamGoals\Core;
|
||||
/** $wpdb-backed CRUD for sg_goals. Requires live WordPress. */
|
||||
final class GoalRepository {
|
||||
|
||||
private const FIELDS = array( 'title' => '%s', 'target_minor' => '%d', 'currency' => '%s', 'status' => '%s', 'starts_at' => '%s', 'ends_at' => '%s', 'counts_fees' => '%d' );
|
||||
private const FIELDS = array( 'title' => '%s', 'target_minor' => '%d', 'currency' => '%s', 'status' => '%s', 'starts_at' => '%s', 'ends_at' => '%s', 'counts_fees' => '%d', 'close_at_target' => '%d' );
|
||||
|
||||
private \wpdb $db;
|
||||
|
||||
@@ -21,7 +21,7 @@ final class GoalRepository {
|
||||
/** @return int new goal id */
|
||||
public function create( array $data ): int {
|
||||
$row = array_intersect_key( $data, self::FIELDS );
|
||||
$row += array( 'status' => 'active', 'currency' => 'USD', 'counts_fees' => 0 );
|
||||
$row += array( 'status' => 'active', 'currency' => 'USD', 'counts_fees' => 0, 'close_at_target' => 0 );
|
||||
$row['created_at'] = gmdate( 'Y-m-d H:i:s' );
|
||||
$this->db->insert( $this->table(), $row );
|
||||
return (int) $this->db->insert_id;
|
||||
@@ -62,9 +62,26 @@ final class GoalRepository {
|
||||
if ( ! $goal ) {
|
||||
return 0;
|
||||
}
|
||||
$col = ! empty( $goal['counts_fees'] ) ? 'total_minor' : 'base_minor'; // whitelisted, not user input
|
||||
$col = GoalTargetPolicy::amountColumn( $goal ); // whitelisted, not user input
|
||||
$d = $this->db->prefix . 'sg_donations';
|
||||
$sum = $this->db->get_var( $this->db->prepare( "SELECT COALESCE(SUM({$col}), 0) FROM {$d} WHERE goal_id = %d AND status = 'completed'", $goalId ) );
|
||||
return (int) $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* "End at target": if the goal is active, flagged close_at_target and its raised total has reached the
|
||||
* target, set it to 'complete'. The UPDATE is conditional on status = 'active', so concurrent callers
|
||||
* change it once. Idempotent and cheap (one SELECT for non-flagged goals). Returns true if THIS call closed it.
|
||||
*/
|
||||
public function closeIfTargetReached( int $goalId ): bool {
|
||||
$goal = $this->find( $goalId );
|
||||
if ( ! $goal || empty( $goal['close_at_target'] ) || 'active' !== $goal['status'] ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! GoalTargetPolicy::shouldClose( $goal, $this->raised_minor( $goalId ) ) ) {
|
||||
return false;
|
||||
}
|
||||
$n = $this->db->query( $this->db->prepare( "UPDATE {$this->table()} SET status = 'complete' WHERE id = %d AND status = 'active' AND close_at_target = 1", $goalId ) );
|
||||
return 1 === $n;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user