Query.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\MultiFormGoals\ProgressBar; |
| 4 | |
| 5 | /** |
| 6 | * Get the Total, Count, and Average of the payment totals for published donations of a given set of forms. |
| 7 | */ |
| 8 | class Query |
| 9 | { |
| 10 | |
| 11 | /** @var array */ |
| 12 | protected $formIDs; |
| 13 | |
| 14 | /** |
| 15 | * @var array $formIDs |
| 16 | */ |
| 17 | public function __construct($formIDs) |
| 18 | { |
| 19 | global $wpdb; |
| 20 | $this->wpdb = $wpdb; |
| 21 | $this->formIDs = $formIDs; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return string |
| 26 | */ |
| 27 | public function getSQL() |
| 28 | { |
| 29 | global $wpdb; |
| 30 | |
| 31 | $sql = " |
| 32 | SELECT |
| 33 | sum( revenue.amount ) as total, |
| 34 | count( payment.ID ) as count |
| 35 | FROM {$wpdb->posts} as payment |
| 36 | JOIN {$wpdb->give_revenue} as revenue |
| 37 | ON revenue.donation_id = payment.ID |
| 38 | WHERE |
| 39 | payment.post_type = 'give_payment' |
| 40 | AND |
| 41 | payment.post_status IN ( 'publish', 'give_subscription' ) |
| 42 | "; |
| 43 | |
| 44 | if ( ! empty($this->formIDs)) { |
| 45 | $sql .= ' |
| 46 | AND |
| 47 | revenue.form_id IN ( ' . $this->getFormsString() . ' ) |
| 48 | '; |
| 49 | } |
| 50 | |
| 51 | return $sql; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return string |
| 56 | */ |
| 57 | protected function getFormsString() |
| 58 | { |
| 59 | return implode(',', $this->formIDs); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return stdClass |
| 64 | */ |
| 65 | public function getResults() |
| 66 | { |
| 67 | $sql = $this->getSQL(); |
| 68 | |
| 69 | return $this->wpdb->get_row($sql); |
| 70 | } |
| 71 | } |
| 72 |