ActiveDonationFormsData.php
4 years ago
DonationData.php
4 years ago
DonationFormsData.php
4 years ago
DonationMetricsData.php
4 years ago
EditedDonationFormsData.php
4 years ago
GivePluginSettingsData.php
4 years ago
PluginsData.php
4 years ago
ServerData.php
4 years ago
ThemeData.php
4 years ago
WebsiteData.php
4 years ago
WebsiteInfoData.php
4 years ago
DonationFormsData.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Tracking\TrackingData; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Helpers\ArrayDataSet; |
| 7 | use Give\Helpers\Form\Template; |
| 8 | use Give\Tracking\Contracts\TrackData; |
| 9 | use Give\Tracking\Helpers\DonationStatuses; |
| 10 | use Give\Tracking\Repositories\TrackEvents; |
| 11 | |
| 12 | /** |
| 13 | * Class DonationFormsData |
| 14 | * |
| 15 | * Represents donation forms data |
| 16 | * |
| 17 | * @package Give\Tracking\TrackingData |
| 18 | * @since 2.10.0 |
| 19 | */ |
| 20 | class DonationFormsData implements TrackData |
| 21 | { |
| 22 | protected $formIds = []; |
| 23 | protected $formRevenues = []; |
| 24 | protected $formDonorCounts = []; |
| 25 | |
| 26 | /** |
| 27 | * @var TrackEvents |
| 28 | */ |
| 29 | protected $trackEvents; |
| 30 | |
| 31 | /** |
| 32 | * DonationFormsData constructor. |
| 33 | * |
| 34 | * @param TrackEvents $trackEvents |
| 35 | */ |
| 36 | public function __construct(TrackEvents $trackEvents) |
| 37 | { |
| 38 | $this->trackEvents = $trackEvents; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @inheritdoc |
| 43 | */ |
| 44 | public function get() |
| 45 | { |
| 46 | $this->setFormIds(); |
| 47 | |
| 48 | if ( ! $this->formIds) { |
| 49 | return []; |
| 50 | } |
| 51 | |
| 52 | $this->setRevenues() |
| 53 | ->setDonorCounts(); |
| 54 | |
| 55 | return $this->getData(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get forms data. |
| 60 | * |
| 61 | * @since 2.10.0 |
| 62 | * @return array |
| 63 | */ |
| 64 | protected function getData() |
| 65 | { |
| 66 | if ( ! $this->formIds) { |
| 67 | return []; |
| 68 | } |
| 69 | |
| 70 | $data = []; |
| 71 | |
| 72 | foreach ($this->formIds as $formId) { |
| 73 | $formTemplate = Template::getActiveID($formId); |
| 74 | |
| 75 | $temp = [ |
| 76 | 'form_id' => (int)$formId, |
| 77 | 'form_url' => untrailingslashit(get_permalink($formId)), |
| 78 | 'form_name' => get_post_field('post_name', $formId, 'db'), |
| 79 | 'form_type' => give()->form_meta->get_meta($formId, '_give_price_option', true), |
| 80 | 'form_template' => ! $formTemplate || 'legacy' === $formTemplate ? 'legacy' : $formTemplate, |
| 81 | 'donor_count' => $this->formDonorCounts[$formId], |
| 82 | 'revenue' => $this->formRevenues[$formId], |
| 83 | ]; |
| 84 | |
| 85 | $this->addAddonsInformation($temp, $formId); |
| 86 | $data[] = $temp; |
| 87 | } |
| 88 | |
| 89 | return $data; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set form ids. |
| 94 | * |
| 95 | * @since 2.10.0 |
| 96 | * @return self |
| 97 | */ |
| 98 | protected function setFormIds() |
| 99 | { |
| 100 | global $wpdb; |
| 101 | |
| 102 | $statues = DonationStatuses::getCompletedDonationsStatues(true); |
| 103 | $time = $this->trackEvents->getRequestTime(); |
| 104 | |
| 105 | $this->formIds = DB::get_col( |
| 106 | " |
| 107 | SELECT DISTINCT dm.meta_value |
| 108 | FROM {$wpdb->donationmeta} as dm |
| 109 | INNER JOIN {$wpdb->posts} as p ON dm.donation_id = p.ID |
| 110 | INNER JOIN {$wpdb->donationmeta} as dm2 ON dm.donation_id = dm2.donation_id |
| 111 | WHERE p.post_status IN ({$statues}) |
| 112 | AND p.post_date>='{$time}' |
| 113 | AND p.post_type='give_payment' |
| 114 | AND dm2.meta_key='_give_payment_mode' |
| 115 | AND dm2.meta_value='live' |
| 116 | AND dm.meta_key='_give_payment_form_id' |
| 117 | " |
| 118 | ); |
| 119 | |
| 120 | return $this; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set forms revenues. |
| 125 | * |
| 126 | * @since 2.10.0 |
| 127 | * @return self |
| 128 | */ |
| 129 | protected function setRevenues() |
| 130 | { |
| 131 | global $wpdb; |
| 132 | |
| 133 | $formIds = ArrayDataSet::getStringSeparatedByCommaEnclosedWithSingleQuote($this->formIds); |
| 134 | $defaultResult = array_combine( |
| 135 | $this->formIds, |
| 136 | array_fill(0, count($this->formIds), 0) // Set default revenue to 0 |
| 137 | ); |
| 138 | |
| 139 | $result = DB::get_results( |
| 140 | " |
| 141 | SELECT SUM(r.amount) as amount, r.form_id |
| 142 | FROM {$wpdb->give_revenue} as r |
| 143 | INNER JOIN {$wpdb->donationmeta} as dm ON r.donation_id = dm.donation_id |
| 144 | WHERE dm.meta_key='_give_payment_mode' |
| 145 | AND dm.meta_value='live' |
| 146 | AND r.form_id IN ({$formIds}) |
| 147 | GROUP BY form_id |
| 148 | ", |
| 149 | ARRAY_A |
| 150 | ); |
| 151 | |
| 152 | if ($result) { |
| 153 | $result = array_map( |
| 154 | 'absint', |
| 155 | array_combine( |
| 156 | wp_list_pluck($result, 'form_id'), |
| 157 | wp_list_pluck($result, 'amount') |
| 158 | ) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | $this->formRevenues = array_replace($defaultResult, $result); |
| 163 | |
| 164 | return $this; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set forms revenues till current date. |
| 169 | * |
| 170 | * @since 2.10.0 |
| 171 | * |
| 172 | * @return self |
| 173 | */ |
| 174 | protected function setDonorCounts() |
| 175 | { |
| 176 | global $wpdb; |
| 177 | |
| 178 | $formIds = ArrayDataSet::getStringSeparatedByCommaEnclosedWithSingleQuote($this->formIds); |
| 179 | $statues = DonationStatuses::getCompletedDonationsStatues(true); |
| 180 | $defaultResult = array_combine( |
| 181 | $this->formIds, |
| 182 | array_fill(0, count($this->formIds), 0) // Set default donor count to 0 |
| 183 | ); |
| 184 | |
| 185 | $result = DB::get_results( |
| 186 | " |
| 187 | SELECT COUNT(DISTINCT dm2.meta_value) as donor_count, dm.meta_value as form_id |
| 188 | FROM {$wpdb->donationmeta} as dm |
| 189 | INNER JOIN {$wpdb->posts} as p ON donation_id = p.ID |
| 190 | INNER JOIN {$wpdb->donationmeta} as dm2 ON dm.donation_id = dm2.donation_id |
| 191 | INNER JOIN {$wpdb->donors} as donor ON dm2.meta_value = donor.id |
| 192 | INNER JOIN {$wpdb->donationmeta} as dm3 ON dm.donation_id = dm3.donation_id |
| 193 | WHERE p.post_status IN ({$statues}) |
| 194 | AND dm3.meta_key='_give_payment_mode' |
| 195 | AND dm3.meta_value='live' |
| 196 | AND dm.meta_key='_give_payment_form_id' |
| 197 | AND dm.meta_value IN ({$formIds}) |
| 198 | AND dm2.meta_key='_give_payment_donor_id' |
| 199 | AND donor.purchase_value > 0 |
| 200 | GROUP BY dm.meta_value |
| 201 | ", |
| 202 | ARRAY_A |
| 203 | ); |
| 204 | |
| 205 | if ($result) { |
| 206 | $result = array_map( |
| 207 | 'absint', |
| 208 | array_combine( |
| 209 | wp_list_pluck($result, 'form_id'), |
| 210 | wp_list_pluck($result, 'donor_count') |
| 211 | ) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | $this->formDonorCounts = array_replace($defaultResult, $result); |
| 216 | |
| 217 | return $this; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Add addon information whether or not they active for donation form. |
| 222 | * |
| 223 | * @since 2.10.0 |
| 224 | * |
| 225 | * @param array $array |
| 226 | * @param int $formId |
| 227 | */ |
| 228 | private function addAddonsInformation(&$array, $formId) |
| 229 | { |
| 230 | $array = array_merge( |
| 231 | $array, |
| 232 | [ |
| 233 | 'recurring_donations' => (int)apply_filters('give_telemetry_form_uses_addon_recurring', false, $formId), |
| 234 | 'fee_recovery' => (int)apply_filters('give_telemetry_form_uses_addon_fee_recovery', false, $formId), |
| 235 | 'form_field_manager' => (int)apply_filters( |
| 236 | 'give_telemetry_form_uses_addon_form_field_manager', |
| 237 | false, |
| 238 | $formId |
| 239 | ), |
| 240 | ] |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 |