Completed
Push — issues/1132 ( ce98cf...c1497a )
by Ravinder
544:59 queued 534:53
created

actions.php ➔ __give_get_payment_meta()   F

Complexity

Conditions 32
Paths > 20000

Size

Total Lines 128

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
nc 67108866
nop 4
dl 0
loc 128
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Payment Actions
4
 *
5
 * @package     Give
6
 * @subpackage  Payments
7
 * @copyright   Copyright (c) 2016, WordImpress
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' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
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' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
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 );
83
	give_increase_donation_count( $form_id );
84
85
	// @todo: Refresh only range related stat cache
86
	give_delete_donation_stats();
87
88
	// Increase the donor's donation stats.
89
	$donor = new Give_Donor( $donor_id );
0 ignored issues
show
Documentation introduced by
$donor_id is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
	$donor->increase_purchase_count();
91
	$donor->increase_value( $amount );
92
93
	give_increase_total_earnings( $amount );
94
95
	// Ensure this action only runs once ever.
96
	if ( empty( $completed_date ) ) {
97
98
		// Save the completed date.
99
		$payment->completed_date = current_time( 'mysql' );
100
		$payment->save();
101
102
		/**
103
		 * Fires after a donation successfully complete.
104
		 *
105
		 * @since 1.0
106
		 *
107
		 * @param int $payment_id The ID of the payment.
108
		 */
109
		do_action( 'give_complete_donation', $payment_id );
110
	}
111
112
}
113
114
add_action( 'give_update_payment_status', 'give_complete_purchase', 100, 3 );
115
116
117
/**
118
 * Record payment status change
119
 *
120
 * @since  1.0
121
 *
122
 * @param  int    $payment_id The ID number of the payment.
123
 * @param  string $new_status The status of the payment, probably "publish".
124
 * @param  string $old_status The status of the payment prior to being marked as "complete", probably "pending".
125
 *
126
 * @return void
127
 */
128
function give_record_status_change( $payment_id, $new_status, $old_status ) {
129
130
	// Get the list of statuses so that status in the payment note can be translated.
131
	$stati      = give_get_payment_statuses();
132
	$old_status = isset( $stati[ $old_status ] ) ? $stati[ $old_status ] : $old_status;
133
	$new_status = isset( $stati[ $new_status ] ) ? $stati[ $new_status ] : $new_status;
134
135
	// translators: 1: old status 2: new status.
136
	$status_change = sprintf( esc_html__( 'Status changed from %1$s to %2$s.', 'give' ), $old_status, $new_status );
137
138
	give_insert_payment_note( $payment_id, $status_change );
139
}
140
141
add_action( 'give_update_payment_status', 'give_record_status_change', 100, 3 );
142
143
144
/**
145
 * Update Old Payments Totals
146
 *
147
 * Updates all old payments, prior to 1.2, with new meta for the total donation amount.
148
 *
149
 * It's done to query payments by their totals.
150
 *
151
 * @since  1.0
152
 *
153
 * @param  array $data Arguments passed.
154
 *
155
 * @return void
156
 */
157
function give_update_old_payments_with_totals( $data ) {
158
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give_upgrade_payments_nonce' ) ) {
159
		return;
160
	}
161
162
	if ( get_option( 'give_payment_totals_upgraded' ) ) {
163
		return;
164
	}
165
166
	$payments = give_get_payments( array(
167
		'offset' => 0,
168
		'number' => - 1,
169
		'mode'   => 'all',
170
	) );
171
172
	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...
173
		foreach ( $payments as $payment ) {
174
175
			$payment = new Give_Payment( $payment->ID );
176
			$meta    = $payment->get_meta();
177
178
			$payment->total = $meta['amount'];
179
			$payment->save();
180
181
		}
182
	}
183
184
	add_option( 'give_payment_totals_upgraded', 1 );
185
}
186
187
add_action( 'give_upgrade_payments', 'give_update_old_payments_with_totals' );
188
189
/**
190
 * Mark Abandoned Donations
191
 *
192
 * Updates over a week-old 'pending' donations to 'abandoned' status.
193
 *
194
 * @since  1.0
195
 *
196
 * @return void
197
 */
198
function give_mark_abandoned_donations() {
199
	$args = array(
200
		'status' => 'pending',
201
		'number' => - 1,
202
		'output' => 'give_payments',
203
	);
204
205
	add_filter( 'posts_where', 'give_filter_where_older_than_week' );
206
207
	$payments = give_get_payments( $args );
208
209
	remove_filter( 'posts_where', 'give_filter_where_older_than_week' );
210
211
	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...
212
		/**
213
		 * Filter payment gateways:  Used to set payment gateways which can be skip while transferring pending payment to abandon.
214
		 *
215
		 * @since 1.6
216
		 *
217
		 * @param array $skip_payment_gateways Array of payment gateways
218
		 */
219
		$skip_payment_gateways = apply_filters( 'give_mark_abandoned_donation_gateways', array( 'offline' ) );
220
221
		foreach ( $payments as $payment ) {
222
			$gateway = give_get_payment_gateway( $payment );
223
224
			// Skip payment gateways.
225
			if ( in_array( $gateway, $skip_payment_gateways ) ) {
226
				continue;
227
			}
228
229
			$payment->status = 'abandoned';
230
			$payment->save();
231
		}
232
	}
233
}
234
235
add_action( 'give_weekly_scheduled_events', 'give_mark_abandoned_donations' );
236
237
238
/**
239
 * Trigger the refresh of this month reports transients
240
 *
241
 * @since 1.7
242
 *
243
 * @param int $payment_ID Payment ID.
244
 *
245
 * @return void
246
 */
247
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...
248
	// Monthly stats.
249
	Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
250
251
	// @todo: Refresh only range related stat cache
252
	give_delete_donation_stats();
253
}
254
255
add_action( 'save_post_give_payment', 'give_refresh_thismonth_stat_transients' );
256
257
258
/**
259
 * Add support to get all payment meta.
260
 * Note: function for internal use
261
 *
262
 * @since 2.0
263
 *
264
 * @param $check
265
 * @param $object_id
266
 * @param $meta_key
267
 * @param $meta_value
268
 *
269
 * @return array
270
 */
271
function __give_get_payment_meta( $check, $object_id, $meta_key, $meta_value ) {
272
	// Bailout.
273
	if (
274
		'give_payment' !== get_post_type( $object_id ) ||
275
		 '_give_payment_meta' !== $meta_key ||
276
		! give_has_upgrade_completed( 'v20_upgrades_payment_metadata' )
277
	) {
278
		return $check;
279
	}
280
281
	// Remove filter.
282
	remove_filter( 'get_post_metadata', '__give_get_payment_meta', 0 );
283
284
	// Get all payment meta.
285
	$payment_meta = give_get_meta( $object_id );
286
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
287
288
	// Set default value to array.
289
	if ( empty( $payment_meta ) ) {
290
		return $meta_value;
291
	}
292
293
	// Donation key.
294
	$payment_meta['key'] = ! empty( $payment_meta['_give_payment_purchase_key'] ) ? current( $payment_meta['_give_payment_purchase_key'] ) : '' ;
295
296
	// Donation form.
297
	$payment_meta['form_title'] = ! empty( $payment_meta['_give_payment_form_title'] ) ? current( $payment_meta['_give_payment_form_title'] ) : '';
298
299
	// Donor email.
300
	$payment_meta['email'] = ! empty( $payment_meta['_give_payment_donor_email'] ) ? current( $payment_meta['_give_payment_donor_email'] ) : '';
301
	$payment_meta['email'] = ! empty( $payment_meta['email'] ) ?
302
		$payment_meta['email'] :
303
		Give()->donors->get_column( 'email', give_get_payment_donor_id( $object_id ) );
304
305
	// Form id.
306
	$payment_meta['form_id'] = ! empty( $payment_meta['_give_payment_form_id'] ) ? current( $payment_meta['_give_payment_form_id'] ) : '';
307
308
	// Price id.
309
	$payment_meta['price_id'] = ! empty( $payment_meta['_give_payment_price_id'] ) ? current( $payment_meta['_give_payment_price_id'] ) : '';
310
311
	// Date.
312
	$payment_meta['date'] = ! empty( $payment_meta['_give_payment_date'] ) ? current( $payment_meta['_give_payment_date'] ) : '';
313
	$payment_meta['date'] = ! empty( $payment_meta['date'] ) ?
314
		$payment_meta['date'] :
315
		get_post_field( 'post_date', $object_id );
316
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
317
318
319
	// Currency.
320
	$payment_meta['currency'] = ! empty( $payment_meta['_give_payment_currency'] ) ? current( $payment_meta['_give_payment_currency'] ) : '';
321
322
	// Decode donor data.
323
	$donor_names = give_get_donor_name_by( ( ! empty( $payment_meta['_give_payment_donor_id'] ) ? current( $payment_meta['_give_payment_donor_id'] ) : 0 ), 'donor' );
324
	$donor_names = explode( ' ', $donor_names, 2 );
325
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 3 empty lines
Loading history...
326
327
328
	// Donor first name.
329
	$donor_data['first_name'] = ! empty( $payment_meta['_give_payment_billing_first_name'] ) ? current( $payment_meta['_give_payment_billing_first_name'] ) : '';
330
	$donor_data['first_name'] = ! empty( $donor_data['first_name'] ) ?
331
		$donor_data['first_name'] :
332
		$donor_names[0];
333
334
	// Donor last name.
335
	$donor_data['last_name'] = ! empty( $payment_meta['_give_payment_billing_last_name'] ) ? current( $payment_meta['_give_payment_billing_last_name'] ) : '';
336
	$donor_data['last_name'] = ! empty( $donor_data['last_name'] ) ?
337
		$donor_data['last_name'] :
338
		( isset( $donor_names[1] ) ? $donor_names[1] : '' );
339
340
	// Donor email.
341
	$donor_data['email'] = $payment_meta['email'];
342
343
	// User ID.
344
	$donor_data['id'] = give_get_payment_user_id( $object_id );
345
346
	$donor_data['address'] = false;
347
348
	// Address1.
349
	$address1 = ! empty( $payment_meta['_give_payment_billing_address1'] ) ? $payment_meta['_give_payment_billing_address1'] : '';
350
	if ( $address1 ) {
351
		$donor_data['address']['line1'] = $address1;
352
	}
353
354
	// Address2.
355
	$address2 = ! empty( $payment_meta['_give_payment_billing_address2'] ) ? $payment_meta['_give_payment_billing_address2'] : '';
356
	if ( $address2 ) {
357
		$donor_data['address']['line2'] = $address2;
358
	}
359
360
	// City.
361
	$city = ! empty( $payment_meta['_give_payment_billing_city'] ) ? $payment_meta['_give_payment_billing_city'] : '';
362
	if ( $city ) {
363
		$donor_data['address']['city'] = $city;
364
	}
365
366
	// Zip.
367
	$zip = ! empty( $payment_meta['_give_payment_billing_zip'] ) ? $payment_meta['_give_payment_billing_zip'] : '';
368
	if ( $zip ) {
369
		$donor_data['address']['zip'] = $zip;
370
	}
371
372
	// State.
373
	$state = ! empty( $payment_meta['_give_payment_billing_state'] ) ? $payment_meta['_give_payment_billing_state'] : '';
374
	if ( $state ) {
375
		$donor_data['address']['state'] = $state;
376
	}
377
378
	// Country.
379
	$country = ! empty( $payment_meta['_give_payment_billing_country'] ) ? $payment_meta['_give_payment_billing_country'] : '';
380
	if ( $country ) {
381
		$donor_data['address']['country'] = $country;
382
	}
383
384
	$payment_meta['user_info'] = $donor_data;
385
386
	// Add filter
387
	add_filter( 'get_post_metadata', '__give_get_payment_meta', 0, 4 );
388
389
	/**
390
	 * Filter the payment meta
391
	 * Add custom meta key to payment meta
392
	 *
393
	 * @since 2.0
394
	 */
395
	$payment_meta[0] = apply_filters( 'give_get_payment_meta', $payment_meta, $object_id, $meta_key );
396
397
	return $payment_meta;
398
}
399
400
add_filter( 'get_post_metadata', '__give_get_payment_meta', 0, 4 );
401
402