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,99 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Core;
|
||||
|
||||
use StreamGoals\Rules\Donation;
|
||||
|
||||
/** $wpdb-backed access to sg_donations. Requires live WordPress. */
|
||||
final class DonationRepository {
|
||||
|
||||
private const STATUSES = array( 'pending', 'completed', 'refunded', 'reversed' );
|
||||
|
||||
private \wpdb $db;
|
||||
|
||||
public function __construct( ?\wpdb $db = null ) {
|
||||
$this->db = $db ?? $GLOBALS['wpdb'];
|
||||
}
|
||||
|
||||
private function table(): string {
|
||||
return $this->db->prefix . 'sg_donations';
|
||||
}
|
||||
|
||||
/** @return int|null new id, or null on failure (e.g. duplicate capture id). */
|
||||
public function insert( array $d ): ?int {
|
||||
$allowed = array( 'goal_id', 'paypal_order_id', 'paypal_capture_id', 'base_minor', 'fee_minor', 'total_minor', 'currency', 'donor_name', 'is_anonymous', 'message', 'status', 'moderation' );
|
||||
$row = array_intersect_key( $d, array_flip( $allowed ) );
|
||||
$row += array( 'status' => 'pending', 'moderation' => 'approved', 'fee_minor' => 0 );
|
||||
$row['created_at'] = gmdate( 'Y-m-d H:i:s' );
|
||||
$ok = $this->db->insert( $this->table(), $row );
|
||||
return $ok ? (int) $this->db->insert_id : null;
|
||||
}
|
||||
|
||||
public function find( int $id ): ?array {
|
||||
return $this->one( $this->db->prepare( "SELECT * FROM {$this->table()} WHERE id = %d", $id ) );
|
||||
}
|
||||
|
||||
public function findByCaptureId( string $captureId ): ?array {
|
||||
return $this->one( $this->db->prepare( "SELECT * FROM {$this->table()} WHERE paypal_capture_id = %s", $captureId ) );
|
||||
}
|
||||
|
||||
public function findByOrderId( string $orderId ): ?array {
|
||||
return $this->one( $this->db->prepare( "SELECT * FROM {$this->table()} WHERE paypal_order_id = %s ORDER BY id DESC LIMIT 1", $orderId ) );
|
||||
}
|
||||
|
||||
public function updateStatus( int $id, string $status, ?string $captureId = null ): bool {
|
||||
if ( ! in_array( $status, self::STATUSES, true ) ) {
|
||||
return false;
|
||||
}
|
||||
$data = array( 'status' => $status );
|
||||
if ( null !== $captureId ) {
|
||||
$data['paypal_capture_id'] = $captureId;
|
||||
}
|
||||
return false !== $this->db->update( $this->table(), $data, array( 'id' => $id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Public-safe feed of completed + approved donations. Real name is never returned for
|
||||
* anonymous donors.
|
||||
*
|
||||
* @return array<int,array{id:int,name:string,amount_minor:int,currency:string,message:string,created_at:string}>
|
||||
*/
|
||||
public function recentApproved( ?int $goalId = null, int $limit = 10 ): array {
|
||||
$limit = max( 1, min( 100, $limit ) );
|
||||
$where = "status = 'completed' AND moderation = 'approved'";
|
||||
$args = array();
|
||||
if ( null !== $goalId ) {
|
||||
$where .= ' AND goal_id = %d';
|
||||
$args[] = $goalId;
|
||||
}
|
||||
$args[] = $limit;
|
||||
$rows = $this->db->get_results(
|
||||
$this->db->prepare( "SELECT id, base_minor, currency, donor_name, is_anonymous, message, created_at FROM {$this->table()} WHERE {$where} ORDER BY id DESC LIMIT %d", ...$args ),
|
||||
ARRAY_A
|
||||
);
|
||||
$out = array();
|
||||
foreach ( (array) $rows as $r ) {
|
||||
$anon = ! empty( $r['is_anonymous'] ) || '' === trim( (string) $r['donor_name'] );
|
||||
$out[] = array(
|
||||
'id' => (int) $r['id'],
|
||||
'name' => $anon ? 'Anonymous' : (string) $r['donor_name'],
|
||||
'amount_minor' => (int) $r['base_minor'],
|
||||
'currency' => (string) $r['currency'],
|
||||
'message' => (string) $r['message'],
|
||||
'created_at' => (string) $r['created_at'],
|
||||
);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/** Convert a DB row to the rules engine value object. */
|
||||
public static function toDonation( array $r ): Donation {
|
||||
return new Donation( (int) $r['id'], (int) $r['goal_id'], (int) $r['base_minor'], (int) $r['fee_minor'], (int) $r['total_minor'], (string) $r['currency'], (string) $r['status'] );
|
||||
}
|
||||
|
||||
private function one( string $sql ): ?array {
|
||||
$row = $this->db->get_row( $sql, ARRAY_A );
|
||||
return $row ?: null;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Core;
|
||||
|
||||
use StreamGoals\Rules\StateRepository;
|
||||
|
||||
/** $wpdb-backed sg_state access (goal_id, skey, svalue). Requires live WordPress. */
|
||||
final class StateStore implements StateRepository {
|
||||
|
||||
private \wpdb $db;
|
||||
|
||||
public function __construct( ?\wpdb $db = null ) {
|
||||
$this->db = $db ?? $GLOBALS['wpdb'];
|
||||
}
|
||||
|
||||
public function getInt( int $goalId, string $key, int $default = 0 ): int {
|
||||
$t = $this->db->prefix . 'sg_state';
|
||||
$v = $this->db->get_var( $this->db->prepare( "SELECT svalue FROM {$t} WHERE goal_id = %d AND skey = %s", $goalId, $key ) );
|
||||
return null === $v ? $default : (int) $v;
|
||||
}
|
||||
|
||||
public function setInt( int $goalId, string $key, int $value ): void {
|
||||
$t = $this->db->prefix . 'sg_state';
|
||||
$this->db->query(
|
||||
$this->db->prepare(
|
||||
"INSERT INTO {$t} (goal_id, skey, svalue, updated_at) VALUES (%d, %s, %s, %s)
|
||||
ON DUPLICATE KEY UPDATE svalue = VALUES(svalue), updated_at = VALUES(updated_at)",
|
||||
$goalId,
|
||||
$key,
|
||||
(string) $value,
|
||||
gmdate( 'Y-m-d H:i:s' )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user