| 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 |
/** @var array */ |
| 11 |
protected $formIDs; |
| 12 |
|
| 13 |
/** |
| 14 |
* @var array $formIDs |
| 15 |
*/ |
| 16 |
public function __construct( $formIDs ) { |
| 17 |
global $wpdb; |
| 18 |
$this->wpdb = $wpdb; |
| 19 |
$this->formIDs = $formIDs; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function getSQL() { |
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
$sql = " |
| 29 |
SELECT |
| 30 |
sum( revenue.amount ) as total, |
| 31 |
count( payment.ID ) as count |
| 32 |
FROM {$wpdb->posts} as payment |
| 33 |
JOIN {$wpdb->give_revenue} as revenue |
| 34 |
ON revenue.donation_id = payment.ID |
| 35 |
WHERE |
| 36 |
payment.post_type = 'give_payment' |
| 37 |
AND |
| 38 |
payment.post_status IN ( 'publish', 'give_subscription' ) |
| 39 |
"; |
| 40 |
|
| 41 |
if ( ! empty( $this->formIDs ) ) { |
| 42 |
$sql .= ' |
| 43 |
AND |
| 44 |
revenue.form_id IN ( ' . $this->getFormsString() . ' ) |
| 45 |
'; |
| 46 |
} |
| 47 |
|
| 48 |
return $sql; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
protected function getFormsString() { |
| 55 |
return implode( ',', $this->formIDs ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @return stdClass |
| 60 |
*/ |
| 61 |
public function getResults() { |
| 62 |
$sql = $this->getSQL(); |
| 63 |
return $this->wpdb->get_row( $sql ); |
| 64 |
} |
| 65 |
} |
| 66 |
|