actions.php ➔ give_refresh_thismonth_stat_transients()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Unused Code introduced by
$user_info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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( array(
170
		'offset' => 0,
171
		'number' => - 1,
172
		'mode'   => 'all',
173
	) );
174
175
	if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
176
		foreach ( $payments as $payment ) {
177
178
			$payment = new Give_Payment( $payment->ID );
179
			$meta    = $payment->get_meta();
180
181
			$payment->total = $meta['amount'];
182
			$payment->save();
183
184
		}
185
	}
186
187
	add_option( 'give_payment_totals_upgraded', 1 );
188
}
189
190
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' );
191
192
/**
193
 * Mark Abandoned Donations
194
 *
195
 * Updates over a week-old 'pending' donations to 'abandoned' status.
196
 *
197
 * @since  1.0
198
 *
199
 * @return void
200
 */
201
function give_mark_abandoned_donations() {
202
	$args = array(
203
		'status' => 'pending',
204
		'number' => - 1,
205
		'output' => 'give_payments',
206
	);
207
208
	add_filter( 'posts_where', 'give_filter_where_older_than_week' );
209
210
	$payments = give_get_payments( $args );
211
212
	remove_filter( 'posts_where', 'give_filter_where_older_than_week' );
213
214
	if ( $payments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
215
		/**
216
		 * Filter payment gateways:  Used to set payment gateways which can be skip while transferring pending payment to abandon.
217
		 *
218
		 * @since 1.6
219
		 *
220
		 * @param array $skip_payment_gateways Array of payment gateways
221
		 */
222
		$skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) );
223
224
		/* @var Give_Payment $payment */
225
		foreach ( $payments as $payment ) {
226
			$gateway = give_get_payment_gateway( $payment->ID );
227
228
			// Skip payment gateways.
229
			if ( in_array( $gateway, $skip_payment_gateways ) ) {
230
				continue;
231
			}
232
233
			$payment->status = 'abandoned';
234
			$payment->save();
235
		}
236
	}
237
}
238
239
Give_Cron::add_weekly_event( 'give_mark_abandoned_donations' );
240
241
242
/**
243
 * Trigger the refresh of this month reports transients
244
 *
245
 * @since 1.7
246
 *
247
 * @param int $payment_ID Payment ID.
248
 *
249
 * @return void
250
 */
251
function give_refresh_thismonth_stat_transients( $payment_ID ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment_ID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
	// Monthly stats.
253
	Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
254
255
	// @todo: Refresh only range related stat cache
256
	give_delete_donation_stats();
257
}
258
259
add_action( 'save_post_give_payment', 'give_refresh_thismonth_stat_transients' );
260
261
262
/**
263
 * Add support to get all payment meta.
264
 * Note: only use for internal purpose
265
 *
266
 * @since 2.0
267
 *
268
 * @param $check
269
 * @param $object_id
270
 * @param $meta_key
271
 * @param $single
272
 *
273
 * @return array
274
 */
275
function give_bc_v20_get_payment_meta( $check, $object_id, $meta_key, $single ) {
276
	// Bailout.
277
	if (
278
		'give_payment' !== get_post_type( $object_id )
279
		|| '_give_payment_meta' !== $meta_key
280
	) {
281
		return $check;
282
	}
283
284
	$cache_key = "_give_payment_meta_{$object_id}";
285
286
	// Get already calculate payment meta from cache.
287
	$payment_meta = Give_Cache::get_db_query( $cache_key );
288
289
	if ( is_null( $payment_meta ) ) {
290
		// Remove filter.
291
		remove_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999 );
292
293
		$donation = new Give_Payment( $object_id );
294
295
		// Get all payment meta.
296
		$payment_meta = give_get_meta( $object_id );
297
298
		// Set default value to array.
299
		if ( empty( $payment_meta ) ) {
300
			return $check;
301
		}
302
303
		// Convert all meta key value to string instead of array
304
		array_walk( $payment_meta, function ( &$meta, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
305
			$meta = current( $meta );
306
		} );
307
308
		/**
309
		 * Add backward compatibility to old meta keys.
310
		 */
311
		// Donation key.
312
		$payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? $payment_meta['_give_payment_purchase_key'] : '';
313
314
		// Donation form.
315
		$payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? $payment_meta['_give_payment_form_title'] : '';
316
317
		// Donor email.
318
		$payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? $payment_meta['_give_payment_donor_email'] : '';
319
		$payment_meta['email'] = ! empty( $payment_meta['email'] ) ?
320
			$payment_meta['email'] :
321
			Give()->donors->get_column( 'email', $donation->donor_id );
322
323
		// Form id.
324
		$payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? $payment_meta['_give_payment_form_id'] : '';
325
326
		// Price id.
327
		$payment_meta['price_id'] = isset( $payment_meta['_give_payment_price_id'] ) ? $payment_meta['_give_payment_price_id'] : '';
328
329
		// Date.
330
		$payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? $payment_meta['_give_payment_date'] : '';
331
		$payment_meta['date'] = ! empty( $payment_meta['date'] ) ?
332
			$payment_meta['date'] :
333
			get_post_field( 'post_date', $object_id );
334
335
336
		// Currency.
337
		$payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? $payment_meta['_give_payment_currency'] : '';
338
339
		// Decode donor data.
340
		$donor_id = ! empty( $payment_meta['_give_payment_donor_id'] ) ? $payment_meta['_give_payment_donor_id'] : 0;
341
		$donor    = new Give_Donor( $donor_id );
342
343
		// Donor first name.
344
		$donor_data['first_name'] = ! empty( $payment_meta['_give_donor_billing_first_name'] ) ? $payment_meta['_give_donor_billing_first_name'] : '';
345
		$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
346
			$donor_data['first_name'] :
347
			$donor->get_first_name();
348
349
		// Donor last name.
350
		$donor_data['last_name'] = ! empty( $payment_meta['_give_donor_billing_last_name'] ) ? $payment_meta['_give_donor_billing_last_name'] : '';
351
		$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
352
			$donor_data['last_name'] :
353
			$donor->get_last_name();
354
355
		// Donor email.
356
		$donor_data['email'] = $payment_meta['email'];
357
358
		// User ID.
359
		$donor_data['id'] = $donation->user_id;
360
361
		$donor_data['address'] = false;
362
363
		// Address1.
364
		$address1 = ! empty( $payment_meta['_give_donor_billing_address1'] ) ? $payment_meta['_give_donor_billing_address1'] : '';
365
		if ( $address1 ) {
366
			$donor_data['address']['line1'] = $address1;
367
		}
368
369
		// Address2.
370
		$address2 = ! empty( $payment_meta['_give_donor_billing_address2'] ) ? $payment_meta['_give_donor_billing_address2'] : '';
371
		if ( $address2 ) {
372
			$donor_data['address']['line2'] = $address2;
373
		}
374
375
		// City.
376
		$city = ! empty( $payment_meta['_give_donor_billing_city'] ) ? $payment_meta['_give_donor_billing_city'] : '';
377
		if ( $city ) {
378
			$donor_data['address']['city'] = $city;
379
		}
380
381
		// Zip.
382
		$zip = ! empty( $payment_meta['_give_donor_billing_zip'] ) ? $payment_meta['_give_donor_billing_zip'] : '';
383
		if ( $zip ) {
384
			$donor_data['address']['zip'] = $zip;
385
		}
386
387
		// State.
388
		$state = ! empty( $payment_meta['_give_donor_billing_state'] ) ? $payment_meta['_give_donor_billing_state'] : '';
389
		if ( $state ) {
390
			$donor_data['address']['state'] = $state;
391
		}
392
393
		// Country.
394
		$country = ! empty( $payment_meta['_give_donor_billing_country'] ) ? $payment_meta['_give_donor_billing_country'] : '';
395
		if ( $country ) {
396
			$donor_data['address']['country'] = $country;
397
		}
398
399
		$payment_meta['user_info'] = $donor_data;
400
401
		// Add filter
402
		add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
403
404
		// Set custom meta key into payment meta.
405
		if ( ! empty( $payment_meta['_give_payment_meta'] ) ) {
406
			$payment_meta['_give_payment_meta'] = is_array( $payment_meta['_give_payment_meta'] ) ? $payment_meta['_give_payment_meta'] : array();
407
408
			$payment_meta = array_merge( maybe_unserialize( $payment_meta['_give_payment_meta'] ), $payment_meta );
409
		}
410
411
		// Set cache.
412
		Give_Cache::set_db_query( $cache_key, $payment_meta );
413
	}
414
415
	if ( $single ) {
416
		/**
417
		 * Filter the payment meta
418
		 * Add custom meta key to payment meta
419
		 *
420
		 * @since 2.0
421
		 */
422
		$new_payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key );
423
424
		$payment_meta = $new_payment_meta;
425
	}
426
427
	return $payment_meta;
428
}
429
430
if( give_has_upgrade_completed( 'v20_upgrades_payment_metadata' ) ) {
431
	add_filter( 'get_post_metadata', 'give_bc_v20_get_payment_meta', 999, 4 );
432
}
433
434
/**
435
 * Add meta in payment that store page id and page url.
436
 *
437
 * Will add/update when user add click on the checkout page.
438
 * The status of the donation doest not matter as it get change when user had made the payment successfully.
439
 *
440
 * @since 1.8.13
441
 *
442
 * @param int $payment_id Payment id for which the meta value should be updated.
443
 */
444
function give_payment_save_page_data( $payment_id ) {
445
	$page_url = ( ! empty( $_REQUEST['give-current-url'] ) ? esc_url( $_REQUEST['give-current-url'] ) : false );
446
447
	// Check $page_url is not empty.
448
	if ( $page_url ) {
449
		update_post_meta( $payment_id, '_give_current_url', $page_url );
450
		$page_id = url_to_postid( $page_url );
451
		// Check $page_id is not empty.
452
		if ( $page_id ) {
453
			update_post_meta( $payment_id, '_give_current_page_id', $page_id );
454
		}
455
	}
456
}
457
458
// Fire when payment is save.
459
add_action( 'give_insert_payment', 'give_payment_save_page_data' );
460