| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Revenue\Repositories; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Framework\Database\DB; |
| 7 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Revenue |
| 11 |
* @package Give\Revenue\Repositories |
| 12 |
* |
| 13 |
* Use this class to get data from "give_revenue" table. |
| 14 |
* |
| 15 |
* @since 2.9.0 |
| 16 |
* @since 2.22.1 Added the `updateRevenueAmount()` method |
| 17 |
*/ |
| 18 |
class Revenue |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Insert revenue. |
| 22 |
* |
| 23 |
* @since 2.9.0 |
| 24 |
* |
| 25 |
* @param array $revenueData |
| 26 |
* |
| 27 |
* @return bool|int |
| 28 |
*/ |
| 29 |
public function insert($revenueData) |
| 30 |
{ |
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
// Validate revenue data |
| 34 |
$this->validateNewRevenueData($revenueData); |
| 35 |
|
| 36 |
/** |
| 37 |
* Filter revenue data before inserting to revenue table. |
| 38 |
* |
| 39 |
* @since 2.9.0 |
| 40 |
*/ |
| 41 |
$revenueData = apply_filters('give_revenue_insert_data', $revenueData); |
| 42 |
|
| 43 |
return DB::insert( |
| 44 |
$wpdb->give_revenue, |
| 45 |
$revenueData, |
| 46 |
$this->getPlaceholderForPrepareQuery($revenueData) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Deletes revenue |
| 52 |
* |
| 53 |
* @param $revenueId |
| 54 |
* |
| 55 |
* @return false|int |
| 56 |
*/ |
| 57 |
public function deleteByDonationId($revenueId) |
| 58 |
{ |
| 59 |
global $wpdb; |
| 60 |
|
| 61 |
return DB::delete( |
| 62 |
$wpdb->give_revenue, |
| 63 |
['donation_id' => $revenueId], |
| 64 |
['%d'] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 2.22.1 |
| 70 |
* |
| 71 |
* @param Donation $donation |
| 72 |
* |
| 73 |
* @return false|int |
| 74 |
*/ |
| 75 |
public function updateRevenueAmount(Donation $donation) |
| 76 |
{ |
| 77 |
global $wpdb; |
| 78 |
|
| 79 |
return DB::update( |
| 80 |
$wpdb->give_revenue, |
| 81 |
['amount' => $donation->amount->formatToMinorAmount()], |
| 82 |
['donation_id' => $donation->id], |
| 83 |
['%d'], |
| 84 |
['%d'] |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @since 4.1.0 |
| 90 |
* |
| 91 |
* @return false|int |
| 92 |
*/ |
| 93 |
public function updateRevenueCampaignId(Donation $donation) |
| 94 |
{ |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
return DB::update( |
| 98 |
$wpdb->give_revenue, |
| 99 |
['campaign_id' => $donation->campaignId], |
| 100 |
['donation_id' => $donation->id], |
| 101 |
['%d'], |
| 102 |
['%d'] |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Validate new revenue data. |
| 108 |
* |
| 109 |
* @since 2.9.0 |
| 110 |
* @since 2.9.4 Mention donation id in exception message. |
| 111 |
* |
| 112 |
* @param array $array |
| 113 |
*/ |
| 114 |
protected function validateNewRevenueData($array) |
| 115 |
{ |
| 116 |
$required = ['donation_id', 'form_id', 'amount']; |
| 117 |
|
| 118 |
if (empty($array['donation_id'])) { |
| 119 |
unset($array['donation_id']); |
| 120 |
} |
| 121 |
|
| 122 |
if (empty($array['form_id'])) { |
| 123 |
unset($array['form_id']); |
| 124 |
} |
| 125 |
|
| 126 |
if (!is_numeric($array['amount']) || (int)$array['amount'] < 0) { |
| 127 |
unset($array['amount']); |
| 128 |
} |
| 129 |
|
| 130 |
if (array_diff($required, array_keys($array))) { |
| 131 |
$errorMessage = ''; |
| 132 |
if (isset($array['donation_id'])) { |
| 133 |
$errorMessage = "An error occurred when processing Donation #{$array['donation_id']}. "; |
| 134 |
} |
| 135 |
|
| 136 |
throw new InvalidArgumentException( |
| 137 |
sprintf( |
| 138 |
'%2$sTo insert revenue, please provide valid %1$s.', |
| 139 |
implode(', ', $required), |
| 140 |
$errorMessage |
| 141 |
) |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get placeholder for prepare query. |
| 148 |
* |
| 149 |
* @param array $data |
| 150 |
* |
| 151 |
* @return string[] Array of value format type |
| 152 |
*/ |
| 153 |
private function getPlaceholderForPrepareQuery($data) |
| 154 |
{ |
| 155 |
$format = []; |
| 156 |
|
| 157 |
foreach ($data as $value) { |
| 158 |
$format[] = is_numeric($value) ? '%d' : '%s'; |
| 159 |
} |
| 160 |
|
| 161 |
return $format; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Return whether or not donation id exist in give_revenue table. |
| 166 |
* |
| 167 |
* @sicne 2.9.0 |
| 168 |
* |
| 169 |
* @param int $donationId |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
public function isDonationExist($donationId) |
| 174 |
{ |
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
return (bool)DB::get_var( |
| 178 |
DB::prepare( |
| 179 |
" |
| 180 |
SELECT donation_id |
| 181 |
FROM {$wpdb->give_revenue} |
| 182 |
WHERE donation_id = %d |
| 183 |
", |
| 184 |
$donationId |
| 185 |
) |
| 186 |
); |
| 187 |
} |
| 188 |
} |
| 189 |
|