db = $db ?? $GLOBALS['wpdb']; } private function t( string $name ): string { return $this->db->prefix . 'sg_' . $name; } /** Normalise numeric columns. @return array{id:int,goal_id:int,title:string,description:string,amount_minor:int,mode:string,enabled:int,sort:int,created_at:string} */ private static function cast( array $r ): array { return array( 'id' => (int) ( $r['id'] ?? 0 ), 'goal_id' => (int) ( $r['goal_id'] ?? 0 ), 'title' => (string) ( $r['title'] ?? '' ), 'description' => (string) ( $r['description'] ?? '' ), 'amount_minor' => (int) ( $r['amount_minor'] ?? 0 ), 'mode' => (string) ( $r['mode'] ?? 'exact' ), 'enabled' => (int) ( $r['enabled'] ?? 0 ), 'sort' => (int) ( $r['sort'] ?? 0 ), 'created_at' => (string) ( $r['created_at'] ?? '' ), ); } /** Enabled actions for a goal (goal_id IN (0,$goalId)) ordered by sort, amount. */ public function enabledForGoal( int $goalId ): array { $rows = $this->db->get_results( $this->db->prepare( 'SELECT * FROM ' . $this->t( 'actions' ) . ' WHERE enabled = 1 AND goal_id IN (0, %d) ORDER BY sort ASC, amount_minor ASC, id ASC', $goalId ), ARRAY_A ); return array_map( array( self::class, 'cast' ), (array) $rows ); } /** All actions (enabled or not) for the admin list. */ public function all(): array { $rows = $this->db->get_results( 'SELECT * FROM ' . $this->t( 'actions' ) . ' ORDER BY sort ASC, amount_minor ASC, id ASC', ARRAY_A ); return array_map( array( self::class, 'cast' ), (array) $rows ); } public function find( int $id ): ?array { $row = $this->db->get_row( $this->db->prepare( 'SELECT * FROM ' . $this->t( 'actions' ) . ' WHERE id = %d', $id ), ARRAY_A ); return $row ? self::cast( $row ) : null; } public function nextSort(): int { return (int) $this->db->get_var( 'SELECT COALESCE(MAX(sort),0)+10 FROM ' . $this->t( 'actions' ) ); } /** @param array{goal_id:int,title:string,description:string,amount_minor:int,mode:string,sort:int} $d @return int new id (0 on failure) */ public function create( array $d ): int { $ok = $this->db->insert( $this->t( 'actions' ), array( 'goal_id' => (int) $d['goal_id'], 'title' => (string) $d['title'], 'description' => (string) $d['description'], 'amount_minor' => (int) $d['amount_minor'], 'mode' => (string) $d['mode'], 'enabled' => 1, 'sort' => (int) $d['sort'], 'created_at' => gmdate( 'Y-m-d H:i:s' ), ), array( '%d', '%s', '%s', '%d', '%s', '%d', '%d', '%s' ) ); return $ok ? (int) $this->db->insert_id : 0; } public function update( int $id, array $d ): void { $this->db->update( $this->t( 'actions' ), array( 'goal_id' => (int) $d['goal_id'], 'title' => (string) $d['title'], 'description' => (string) $d['description'], 'amount_minor' => (int) $d['amount_minor'], 'mode' => (string) $d['mode'], 'sort' => (int) $d['sort'], ), array( 'id' => $id ), array( '%d', '%s', '%s', '%d', '%s', '%d' ), array( '%d' ) ); } public function setEnabled( int $id, bool $on ): void { $this->db->update( $this->t( 'actions' ), array( 'enabled' => $on ? 1 : 0 ), array( 'id' => $id ), array( '%d' ), array( '%d' ) ); } /** Past events keep their title snapshot, so history survives. */ public function delete( int $id ): void { $this->db->delete( $this->t( 'actions' ), array( 'id' => $id ), array( '%d' ) ); } /** * Queue triggered actions for a donation. INSERT IGNORE + UNIQUE(donation_id, action_id): replays do nothing. * * @param array[] $matches Output of Matcher::match(). */ public function recordEvents( int $donationId, array $matches ): void { $now = gmdate( 'Y-m-d H:i:s' ); foreach ( $matches as $m ) { $times = (int) ( $m['times'] ?? 0 ); $aid = (int) ( $m['action_id'] ?? 0 ); if ( $donationId <= 0 || $aid <= 0 || $times < 1 ) { continue; } $title = function_exists( 'mb_substr' ) ? mb_substr( (string) ( $m['title'] ?? '' ), 0, 120 ) : substr( (string) ( $m['title'] ?? '' ), 0, 120 ); $this->db->query( $this->db->prepare( 'INSERT IGNORE INTO ' . $this->t( 'action_events' ) . " (donation_id, action_id, times, title, status, created_at) VALUES (%d, %d, %d, %s, 'queued', %s)", $donationId, $aid, $times, $title, $now ) ); } } private function eventSelect(): string { return 'SELECT e.*, d.donor_name, d.is_anonymous, d.message, d.base_minor, d.currency FROM ' . $this->t( 'action_events' ) . ' e LEFT JOIN ' . $this->t( 'donations' ) . ' d ON d.id = e.donation_id'; } /** Oldest first. Rows include donor_name, is_anonymous, message, base_minor, currency from the donation. */ public function queued( int $limit ): array { return (array) $this->db->get_results( $this->db->prepare( $this->eventSelect() . " WHERE e.status = 'queued' ORDER BY e.id ASC LIMIT %d", $limit ), ARRAY_A ); } public function recentDone( int $limit ): array { return (array) $this->db->get_results( $this->db->prepare( $this->eventSelect() . " WHERE e.status = 'done' ORDER BY e.done_at DESC, e.id DESC LIMIT %d", $limit ), ARRAY_A ); } public function queuedCount(): int { return (int) $this->db->get_var( 'SELECT COUNT(*) FROM ' . $this->t( 'action_events' ) . " WHERE status = 'queued'" ); } public function markDone( int $eventId ): bool { $n = $this->db->query( $this->db->prepare( 'UPDATE ' . $this->t( 'action_events' ) . " SET status = 'done', done_at = %s WHERE id = %d AND status = 'queued'", gmdate( 'Y-m-d H:i:s' ), $eventId ) ); return 1 === (int) $n; } public function markAllDone(): int { return (int) $this->db->query( $this->db->prepare( 'UPDATE ' . $this->t( 'action_events' ) . " SET status = 'done', done_at = %s WHERE status = 'queued'", gmdate( 'Y-m-d H:i:s' ) ) ); } }