2026-11-15 08:45:17 -08:00
|
|
|
<?php
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
|
|
namespace StreamGoals\Overlay;
|
|
|
|
|
|
|
|
|
|
/** $wpdb adapter reading sg_goals, sg_donations, sg_state, sg_rules directly. */
|
2026-11-15 12:36:28 -08:00
|
|
|
final class WpdbDataSource implements OverlayDataSource, RecentDonationsSource, ActionsSource {
|
2026-11-15 08:45:17 -08:00
|
|
|
|
|
|
|
|
private function t( string $name ): string {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
return $wpdb->prefix . 'sg_' . $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function activeGoal(): ?array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$row = $wpdb->get_row( "SELECT * FROM {$this->t('goals')} WHERE status = 'active' ORDER BY id DESC LIMIT 1", ARRAY_A );
|
|
|
|
|
return $row ?: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function raisedMinor( array $goal ): int {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$col = ! empty( $goal['counts_fees'] ) ? 'total_minor' : 'base_minor';
|
|
|
|
|
$sum = $wpdb->get_var( $wpdb->prepare(
|
|
|
|
|
"SELECT COALESCE(SUM($col),0) FROM {$this->t('donations')} WHERE goal_id = %d AND status = 'completed'",
|
|
|
|
|
(int) $goal['id']
|
|
|
|
|
) );
|
|
|
|
|
return (int) $sum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function stateValue( int $goalId, string $key ): ?string {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$v = $wpdb->get_var( $wpdb->prepare( "SELECT svalue FROM {$this->t('state')} WHERE goal_id = %d AND skey = %s", $goalId, $key ) );
|
|
|
|
|
return null === $v ? null : (string) $v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function counter( int $goalId ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT skey, svalue FROM {$this->t('state')} WHERE goal_id = %d AND skey LIKE %s",
|
|
|
|
|
$goalId,
|
|
|
|
|
$wpdb->esc_like( 'counter' ) . '%'
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
$total = 0;
|
|
|
|
|
foreach ( (array) $rows as $r ) {
|
|
|
|
|
if ( preg_match( '/carry|remainder/i', (string) $r['skey'] ) || ! is_numeric( $r['svalue'] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$total += (int) $r['svalue'];
|
|
|
|
|
}
|
|
|
|
|
$label = '';
|
|
|
|
|
$rules = $wpdb->get_col( $wpdb->prepare(
|
|
|
|
|
"SELECT config FROM {$this->t('rules')} WHERE goal_id = %d AND type = 'counter' AND enabled = 1 ORDER BY sort, id",
|
|
|
|
|
$goalId
|
|
|
|
|
) );
|
|
|
|
|
foreach ( (array) $rules as $cfg ) {
|
|
|
|
|
$d = json_decode( (string) $cfg, true );
|
|
|
|
|
if ( is_array( $d ) && ! empty( $d['label'] ) ) {
|
|
|
|
|
$label = (string) $d['label'];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array( 'label' => $label, 'value' => $total );
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 12:07:06 -08:00
|
|
|
public function bonusesFor( array $donationIds ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$ids = array_values( array_unique( array_filter( array_map( 'intval', $donationIds ), static fn( $i ) => $i > 0 ) ) );
|
|
|
|
|
if ( ! $ids ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
$in = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT e.donation_id, r.config FROM {$this->t('rule_events')} e
|
|
|
|
|
INNER JOIN {$this->t('rules')} r ON r.id = e.rule_id
|
|
|
|
|
WHERE r.type = 'bonus' AND e.reversed = 0 AND e.delta > 0 AND e.donation_id IN ($in)
|
|
|
|
|
ORDER BY e.id ASC",
|
|
|
|
|
$ids
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
$out = array();
|
|
|
|
|
foreach ( (array) $rows as $row ) {
|
|
|
|
|
$cfg = json_decode( (string) $row['config'], true );
|
|
|
|
|
$ev = is_array( $cfg ) && isset( $cfg['event'] ) && is_string( $cfg['event'] ) ? $cfg['event'] : '';
|
|
|
|
|
if ( '' !== $ev ) {
|
|
|
|
|
$out[ (int) $row['donation_id'] ][] = $ev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 12:36:28 -08:00
|
|
|
public function menuActions( int $goalId ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$min = \StreamGoals\Core\Settings::effectiveMin( (int) \StreamGoals\Core\Settings::get( 'min_donation_minor', 0 ) );
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT title, description, amount_minor, mode FROM {$this->t('actions')}
|
|
|
|
|
WHERE enabled = 1 AND goal_id IN (0, %d) AND amount_minor >= %d ORDER BY sort, amount_minor, id LIMIT 50",
|
|
|
|
|
$goalId,
|
|
|
|
|
$min
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
return (array) $rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function actionsFor( array $donationIds ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$ids = array_values( array_unique( array_filter( array_map( 'intval', $donationIds ), static fn( $i ) => $i > 0 ) ) );
|
|
|
|
|
if ( ! $ids ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
$in = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT donation_id, title, times FROM {$this->t('action_events')} WHERE donation_id IN ($in) ORDER BY id ASC",
|
|
|
|
|
$ids
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
$out = array();
|
|
|
|
|
foreach ( (array) $rows as $row ) {
|
|
|
|
|
$out[ (int) $row['donation_id'] ][] = array( 'title' => (string) $row['title'], 'times' => (int) $row['times'] );
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 08:45:17 -08:00
|
|
|
public function donations( ?int $sinceId, int $limit ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$cols = 'id, base_minor, currency, donor_name, is_anonymous, message, status, moderation';
|
|
|
|
|
$where = "status = 'completed' AND moderation = 'approved'";
|
|
|
|
|
if ( null === $sinceId ) {
|
|
|
|
|
$rows = $wpdb->get_results( "SELECT $cols FROM {$this->t('donations')} WHERE $where ORDER BY id DESC LIMIT 1", ARRAY_A );
|
|
|
|
|
} else {
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT $cols FROM {$this->t('donations')} WHERE $where AND id > %d ORDER BY id ASC LIMIT %d",
|
|
|
|
|
$sinceId,
|
|
|
|
|
$limit
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
}
|
|
|
|
|
return (array) $rows;
|
|
|
|
|
}
|
2026-11-15 12:26:03 -08:00
|
|
|
|
|
|
|
|
public function recentDonations( int $limit ): array {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$rows = $wpdb->get_results( $wpdb->prepare(
|
|
|
|
|
"SELECT id, base_minor, currency, donor_name, is_anonymous, message, status, moderation FROM {$this->t('donations')}
|
|
|
|
|
WHERE status = 'completed' AND moderation = 'approved' ORDER BY id DESC LIMIT %d",
|
|
|
|
|
max( 1, $limit )
|
|
|
|
|
), ARRAY_A );
|
|
|
|
|
return array_reverse( (array) $rows );
|
|
|
|
|
}
|
2026-11-15 08:45:17 -08:00
|
|
|
}
|