- 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>
88 lines
3.1 KiB
PHP
88 lines
3.1 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
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', 'close_at_target' => '%d' );
|
|
|
|
private \wpdb $db;
|
|
|
|
public function __construct( ?\wpdb $db = null ) {
|
|
$this->db = $db ?? $GLOBALS['wpdb'];
|
|
}
|
|
|
|
private function table(): string {
|
|
return $this->db->prefix . 'sg_goals';
|
|
}
|
|
|
|
/** @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, '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;
|
|
}
|
|
|
|
public function find( int $id ): ?array {
|
|
$row = $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table()} WHERE id = %d", $id ), ARRAY_A );
|
|
return $row ?: null;
|
|
}
|
|
|
|
/** @return array[] */
|
|
public function all( ?string $status = null ): array {
|
|
$t = $this->table();
|
|
$sql = null === $status
|
|
? "SELECT * FROM {$t} ORDER BY id DESC"
|
|
: $this->db->prepare( "SELECT * FROM {$t} WHERE status = %s ORDER BY id DESC", $status );
|
|
return (array) $this->db->get_results( $sql, ARRAY_A );
|
|
}
|
|
|
|
public function update( int $id, array $data ): bool {
|
|
$row = array_intersect_key( $data, self::FIELDS );
|
|
if ( ! $row ) {
|
|
return false;
|
|
}
|
|
return false !== $this->db->update( $this->table(), $row, array( 'id' => $id ) );
|
|
}
|
|
|
|
public function delete( int $id ): bool {
|
|
return false !== $this->db->delete( $this->table(), array( 'id' => $id ), array( '%d' ) );
|
|
}
|
|
|
|
/**
|
|
* Sum of completed donations (refunded/reversed/pending excluded), using total_minor when the
|
|
* goal counts fees, else base_minor.
|
|
*/
|
|
public function raised_minor( int $goalId ): int {
|
|
$goal = $this->find( $goalId );
|
|
if ( ! $goal ) {
|
|
return 0;
|
|
}
|
|
$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;
|
|
}
|
|
}
|