2026-11-15 08:44:52 -08:00
<? php
declare ( strict_types = 1 );
namespace StreamGoals\Core ;
/** $wpdb-backed CRUD for sg_goals. Requires live WordPress. */
final class GoalRepository {
2026-11-15 12:53:07 -08:00
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' );
2026-11-15 08:44:52 -08:00
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 );
2026-11-15 12:53:07 -08:00
$row += array ( 'status' => 'active' , 'currency' => 'USD' , 'counts_fees' => 0 , 'close_at_target' => 0 );
2026-11-15 08:44:52 -08:00
$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 ;
}
2026-11-15 12:53:07 -08:00
$col = GoalTargetPolicy :: amountColumn ( $goal ); // whitelisted, not user input
2026-11-15 08:44:52 -08:00
$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 ;
}
2026-11-15 12:53:07 -08:00
/**
* "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 ;
}
2026-11-15 08:44:52 -08:00
}