Add goals repository and configurable rules engine
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
939193f258
commit
7155396312
19 files changed
+882
No files matched your search
@@ -0,0 +1,70 @@
|
||||
<?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' );
|
||||
|
||||
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 );
|
||||
$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 = ! empty( $goal['counts_fees'] ) ? 'total_minor' : 'base_minor'; // 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user