*/ private array $attachments = array(); 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 ); } 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; } 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; } public function tiersFor( int $goalId ): array { return ( new \StreamGoals\Tiers\TierRepository( $GLOBALS['wpdb'] ) )->forGoal( $goalId ); } public function tiersReachedFor( array $donationIds ): array { return ( new \StreamGoals\Tiers\TierRepository( $GLOBALS['wpdb'] ) )->reachedByDonation( $donationIds ); } public function alertDefs(): array { return ( new \StreamGoals\Alerts\DefRepository( $GLOBALS['wpdb'] ) )->enabled(); } public function triggeredActionsFor( 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, e.action_id, e.title, COALESCE(a.amount_minor,0) AS amount_minor FROM {$this->t('action_events')} e LEFT JOIN {$this->t('actions')} a ON a.id = e.action_id WHERE e.donation_id IN ($in) ORDER BY e.id ASC", $ids ), ARRAY_A ); $out = array(); foreach ( (array) $rows as $row ) { $out[ (int) $row['donation_id'] ][] = array( 'action_id' => (int) $row['action_id'], 'amount_minor' => (int) $row['amount_minor'], 'title' => (string) $row['title'] ); } return $out; } public function attachment( int $id ): ?array { if ( array_key_exists( $id, $this->attachments ) ) { return $this->attachments[ $id ]; } $out = null; if ( $id > 0 && 'attachment' === get_post_type( $id ) ) { $mime = (string) get_post_mime_type( $id ); $url = wp_get_attachment_url( $id ); if ( \StreamGoals\Alerts\Media::isAllowed( $mime ) && is_string( $url ) && '' !== $url ) { $path = get_attached_file( $id ); $mtime = ( is_string( $path ) && is_file( $path ) ) ? (int) @filemtime( $path ) : 0; $out = array( 'url' => $url, 'mime' => $mime, 'v' => $mtime, 'modified' => (string) get_post_field( 'post_modified_gmt', $id ) ); } } return $this->attachments[ $id ] = $out; } public function donations( ?int $sinceId, int $limit ): array { global $wpdb; $cols = 'id, goal_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; } public function recentDonations( int $limit ): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, goal_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 ); } }