PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.19.4
GiveWP – Donation Plugin and Fundraising Platform v3.19.4
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / payments / actions.php
give / includes / payments Last commit date
actions.php 1 year ago backward-compatibility.php 6 years ago class-give-payment.php 5 years ago class-give-sequential-donation-number.php 2 years ago class-payment-stats.php 4 years ago class-payments-query.php 5 years ago functions.php 1 year ago
actions.php
465 lines
1 <?php
2 /**
3 * Payment Actions
4 *
5 * @package Give
6 * @subpackage Payments
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Complete a donation
19 *
20 * Performs all necessary actions to complete a donation.
21 * Triggered by the give_update_payment_status() function.
22 *
23 * @since 1.0
24 *
25 * @param int $payment_id The ID number of the payment.
26 * @param string $new_status The status of the payment, probably "publish".
27 * @param string $old_status The status of the payment prior to being marked as "complete", probably "pending".
28 *
29 * @return void
30 */
31 function give_complete_purchase( $payment_id, $new_status, $old_status ) {
32
33 // Make sure that payments are only completed once.
34 if ( $old_status == 'publish' || $old_status == 'complete' ) {
35 return;
36 }
37
38 // Make sure the payment completion is only processed when new status is complete.
39 if ( $new_status != 'publish' && $new_status != 'complete' ) {
40 return;
41 }
42
43 $payment = new Give_Payment( $payment_id );
44
45 $creation_date = get_post_field( 'post_date', $payment_id, 'raw' );
46 $payment_meta = $payment->payment_meta;
47 $completed_date = $payment->completed_date;
48 $user_info = $payment->user_info;
49 $donor_id = $payment->customer_id;
50 $amount = $payment->total;
51 $price_id = $payment->price_id;
52 $form_id = $payment->form_id;
53
54 /**
55 * Fires before completing donation.
56 *
57 * @since 1.0
58 *
59 * @param int $payment_id The ID of the payment.
60 */
61 do_action( 'give_pre_complete_donation', $payment_id );
62
63 // Ensure these actions only run once, ever.
64 if ( empty( $completed_date ) ) {
65
66 give_record_donation_in_log( $form_id, $payment_id, $price_id, $creation_date );
67
68 /**
69 * Fires after logging donation record.
70 *
71 * @since 1.0
72 *
73 * @param int $form_id The ID number of the form.
74 * @param int $payment_id The ID number of the payment.
75 * @param array $payment_meta The payment meta.
76 */
77 do_action( 'give_complete_form_donation', $form_id, $payment_id, $payment_meta );
78
79 }
80
81 // Increase the earnings for this form ID.
82 give_increase_earnings( $form_id, $amount, $payment_id );
83 give_increase_donation_count( $form_id );
84
85 // Update the goal progress for this form ID.
86 give_update_goal_progress( $form_id );
87
88 // @todo: Refresh only range related stat cache
89 give_delete_donation_stats();
90
91 // Increase the donor's donation stats.
92 $donor = new Give_Donor( $donor_id );
93 $donor->increase_purchase_count();
94 $donor->increase_value( $amount );
95
96 give_increase_total_earnings( $amount );
97
98 // Ensure this action only runs once ever.
99 if ( empty( $completed_date ) ) {
100
101 // Save the completed date.
102 $payment->completed_date = current_time( 'mysql' );
103 $payment->save();
104
105 /**
106 * Fires after a donation successfully complete.
107 *
108 * @since 1.0
109 *
110 * @param int $payment_id The ID of the payment.
111 */
112 do_action( 'give_complete_donation', $payment_id );
113 }
114
115 }
116
117 add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 );
118
119
120 /**
121 * Record payment status change
122 *
123 * @since 1.0
124 *
125 * @param int $payment_id The ID number of the payment.
126 * @param string $new_status The status of the payment, probably "publish".
127 * @param string $old_status The status of the payment prior to being marked as "complete", probably "pending".
128 *
129 * @return void
130 */
131 function give_record_status_change( $payment_id, $new_status, $old_status ) {
132
133 // Get the list of statuses so that status in the payment note can be translated.
134 $stati = give_get_payment_statuses();
135 $old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status;
136 $new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status;
137
138 // translators: 1: old status 2: new status.
139 $status_change = sprintf( esc_html__( 'Status changed from %1$s to %2$s.', 'give' ), $old_status, $new_status );
140
141 give_insert_payment_note( $payment_id, $status_change );
142 }
143
144 add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 );
145
146
147 /**
148 * Update Old Payments Totals
149 *
150 * Updates all old payments, prior to 1.2, with new meta for the total donation amount.
151 *
152 * It's done to query payments by their totals.
153 *
154 * @since 1.0
155 *
156 * @param array $data Arguments passed.
157 *
158 * @return void
159 */
160 function give_update_old_payments_with_totals( $data ) {
161 if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) {
162 return;
163 }
164
165 if ( get_option( 'give_payment_totals_upgraded' ) ) {
166 return;
167 }
168
169 $payments = give_get_payments(
170 array(
171 'offset' => 0,
172 'number' => - 1,
173 'mode' => 'all',
174 )
175 );
176
177 if ( $payments ) {
178 foreach ( $payments as $payment ) {
179
180 $payment = new Give_Payment( $payment->ID );
181 $meta = $payment->get_meta();
182
183 $payment->total = $meta['amount'];
184 $payment->save();
185
186 }
187 }
188
189 add_option( 'give_payment_totals_upgraded', 1 );
190 }
191
192 add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' );
193
194 /**
195 * Mark Abandoned Donations
196 *
197 * Updates over a week-old 'pending' donations to 'abandoned' status.
198 *
199 * @since 1.0
200 *
201 * @return void
202 */
203 function give_mark_abandoned_donations() {
204 $args = array(
205 'status' => 'pending',
206 'number' => - 1,
207 'output' => 'give_payments',
208 );
209
210 add_filter( 'posts_where', 'give_filter_where_older_than_week' );
211
212 $payments = give_get_payments( $args );
213
214 remove_filter( 'posts_where', 'give_filter_where_older_than_week' );
215
216 if ( $payments ) {
217 /**
218 * Filter payment gateways: Used to set payment gateways that can be skipped while updating the donation status from pending to abandoned.
219 *
220 * @since 1.6
221 *
222 * @param array $skip_payment_gateways Array of payment gateways
223 */
224 $skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) );
225
226 /* @var Give_Payment $payment */
227 foreach ( $payments as $payment ) {
228 $gateway = give_get_payment_gateway( $payment->ID );
229
230 // Skip payment gateways.
231 if ( in_array( $gateway, $skip_payment_gateways ) ) {
232 continue;
233 }
234
235 $payment->status = 'abandoned';
236 $payment->save();
237 }
238 }
239 }
240
241 Give_Cron::add_weekly_event( 'give_mark_abandoned_donations' );
242
243
244 /**
245 * Trigger the refresh of this month reports transients
246 *
247 * @since 1.7
248 *
249 * @param int $payment_ID Payment ID.
250 *
251 * @return void
252 */
253 function give_refresh_thismonth_stat_transients( $payment_ID ) {
254 // Monthly stats.
255 Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
256
257 // @todo: Refresh only range related stat cache
258 give_delete_donation_stats();
259 }
260
261 add_action( 'save_post_give_payment', 'give_refresh_thismonth_stat_transients' );
262
263
264 /**
265 * Add support to get all payment meta.
266 * Note: only use for internal purpose
267 *
268 * @since 3.19.0 change $donor_data['address'] to array instead of false
269 * @since 2.0
270 *
271 * @param $check
272 * @param $object_id
273 * @param $meta_key
274 * @param $single
275 *
276 * @return array
277 */
278 function give_bc_v20_get_payment_meta( $check, $object_id, $meta_key, $single ) {
279 // Bailout.
280 if (
281 'give_payment' !== get_post_type( $object_id )
282 || '_give_payment_meta' !== $meta_key
283 ) {
284 return $check;
285 }
286
287 $cache_key = "_give_payment_meta_{$object_id}";
288
289 // Get already calculate payment meta from cache.
290 $payment_meta = Give_Cache::get_db_query( $cache_key );
291
292 if ( is_null( $payment_meta ) ) {
293 // Remove filter.
294 remove_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999 );
295
296 $donation = new Give_Payment( $object_id );
297
298 // Get all payment meta.
299 $payment_meta = give_get_meta( $object_id );
300
301 // Set default value to array.
302 if ( empty( $payment_meta ) ) {
303 return $check;
304 }
305
306 // Convert all meta key value to string instead of array
307 array_walk(
308 $payment_meta,
309 function ( &$meta, $key ) {
310 $meta = current( $meta );
311 }
312 );
313
314 /**
315 * Add backward compatibility to old meta keys.
316 */
317 // Donation key.
318 $payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? $payment_meta['_give_payment_purchase_key'] : '';
319
320 // Donation form.
321 $payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? $payment_meta['_give_payment_form_title'] : '';
322
323 // Donor email.
324 $payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? $payment_meta['_give_payment_donor_email'] : '';
325 $payment_meta['email'] = ! empty( $payment_meta['email'] ) ?
326 $payment_meta['email'] :
327 Give()->donors->get_column( 'email', $donation->donor_id );
328
329 // Form id.
330 $payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? $payment_meta['_give_payment_form_id'] : '';
331
332 // Price id.
333 $payment_meta['price_id'] = isset( $payment_meta['_give_payment_price_id'] ) ? $payment_meta['_give_payment_price_id'] : '';
334
335 // Date.
336 $payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? $payment_meta['_give_payment_date'] : '';
337 $payment_meta['date'] = ! empty( $payment_meta['date'] ) ?
338 $payment_meta['date'] :
339 get_post_field( 'post_date', $object_id );
340
341 // Currency.
342 $payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? $payment_meta['_give_payment_currency'] : '';
343
344 // Decode donor data.
345 $donor_id = ! empty( $payment_meta['_give_payment_donor_id'] ) ? $payment_meta['_give_payment_donor_id'] : 0;
346 $donor = new Give_Donor( $donor_id );
347
348 // Donor first name.
349 $donor_data['first_name'] = ! empty( $payment_meta['_give_donor_billing_first_name'] ) ? $payment_meta['_give_donor_billing_first_name'] : '';
350 $donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
351 $donor_data['first_name'] :
352 $donor->get_first_name();
353
354 // Donor last name.
355 $donor_data['last_name'] = ! empty( $payment_meta['_give_donor_billing_last_name'] ) ? $payment_meta['_give_donor_billing_last_name'] : '';
356 $donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
357 $donor_data['last_name'] :
358 $donor->get_last_name();
359
360 // Donor email.
361 $donor_data['email'] = $payment_meta['email'];
362
363 // User ID.
364 $donor_data['id'] = $donation->user_id;
365
366 $donor_data['address'] = [];
367
368 // Address1.
369 $address1 = ! empty( $payment_meta['_give_donor_billing_address1'] ) ? $payment_meta['_give_donor_billing_address1'] : '';
370 if ( $address1 ) {
371 $donor_data['address']['line1'] = $address1;
372 }
373
374 // Address2.
375 $address2 = ! empty( $payment_meta['_give_donor_billing_address2'] ) ? $payment_meta['_give_donor_billing_address2'] : '';
376 if ( $address2 ) {
377 $donor_data['address']['line2'] = $address2;
378 }
379
380 // City.
381 $city = ! empty( $payment_meta['_give_donor_billing_city'] ) ? $payment_meta['_give_donor_billing_city'] : '';
382 if ( $city ) {
383 $donor_data['address']['city'] = $city;
384 }
385
386 // Zip.
387 $zip = ! empty( $payment_meta['_give_donor_billing_zip'] ) ? $payment_meta['_give_donor_billing_zip'] : '';
388 if ( $zip ) {
389 $donor_data['address']['zip'] = $zip;
390 }
391
392 // State.
393 $state = ! empty( $payment_meta['_give_donor_billing_state'] ) ? $payment_meta['_give_donor_billing_state'] : '';
394 if ( $state ) {
395 $donor_data['address']['state'] = $state;
396 }
397
398 // Country.
399 $country = ! empty( $payment_meta['_give_donor_billing_country'] ) ? $payment_meta['_give_donor_billing_country'] : '';
400 if ( $country ) {
401 $donor_data['address']['country'] = $country;
402 }
403
404 $payment_meta['user_info'] = $donor_data;
405
406 // Add filter
407 add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
408
409 // Set custom meta key into payment meta.
410 if ( ! empty( $payment_meta['_give_payment_meta'] ) ) {
411 $payment_meta['_give_payment_meta'] = is_array( $payment_meta['_give_payment_meta'] ) ? $payment_meta['_give_payment_meta'] : array();
412
413 $payment_meta = array_merge( maybe_unserialize( $payment_meta['_give_payment_meta'] ), $payment_meta );
414 }
415
416 // Set cache.
417 Give_Cache::set_db_query( $cache_key, $payment_meta );
418 }
419
420 if ( $single ) {
421 /**
422 * Filter the payment meta
423 * Add custom meta key to payment meta
424 *
425 * @since 2.0
426 */
427 $new_payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key );
428
429 $payment_meta = $new_payment_meta;
430 }
431
432 return $payment_meta;
433 }
434
435 if ( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
436 add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
437 }
438
439 /**
440 * Add meta in payment that store page id and page url.
441 *
442 * Will add/update when user add click on the checkout page.
443 * The status of the donation doest not matter as it get change when user had made the payment successfully.
444 *
445 * @since 1.8.13
446 *
447 * @param int $payment_id Payment id for which the meta value should be updated.
448 */
449 function give_payment_save_page_data( $payment_id ) {
450 $page_url = ( ! empty( $_REQUEST['give-current-url'] ) ? esc_url( $_REQUEST['give-current-url'] ) : false );
451
452 // Check $page_url is not empty.
453 if ( $page_url ) {
454 update_post_meta( $payment_id, '_give_current_url', $page_url );
455 $page_id = url_to_postid( $page_url );
456 // Check $page_id is not empty.
457 if ( $page_id ) {
458 update_post_meta( $payment_id, '_give_current_page_id', $page_id );
459 }
460 }
461 }
462
463 // Fire when payment is save.
464 add_action( 'give_insert_payment', 'give_payment_save_page_data' );
465