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