Test Failed
Push — issues/2042 ( e8b6b5 )
by Ravinder
05:59
created

functions.php ➔ give_get_payment_status()   C

Complexity

Conditions 11
Paths 21

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 18
nc 21
nop 2
dl 0
loc 40
rs 5.2653
c 0
b 0
f 0

How to fix   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 Functions
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
 * Get Payments
19
 *
20
 * Retrieve payments from the database.
21
 *
22
 * Since 1.0, this function takes an array of arguments, instead of individual
23
 * parameters. All of the original parameters remain, but can be passed in any
24
 * order via the array.
25
 *
26
 * @since 1.0
27
 *
28
 * @param array $args     {
29
 *                        Optional. Array of arguments passed to payments query.
30
 *
31
 * @type int    $offset   The number of payments to offset before retrieval.
32
 *                            Default is 0.
33
 * @type int    $number   The number of payments to query for. Use -1 to request all
34
 *                            payments. Default is 20.
35
 * @type string $mode     Default is 'live'.
36
 * @type string $order    Designates ascending or descending order of payments.
37
 *                            Accepts 'ASC', 'DESC'. Default is 'DESC'.
38
 * @type string $orderby  Sort retrieved payments by parameter. Default is 'ID'.
39
 * @type string $status   The status of the payments. Default is 'any'.
40
 * @type string $user     User. Default is null.
41
 * @type string $meta_key Custom field key. Default is null.
42
 * }
43
 *
44
 * @return array $payments Payments retrieved from the database
45
 */
46
function give_get_payments( $args = array() ) {
47
48
	// Fallback to post objects to ensure backwards compatibility.
49
	if ( ! isset( $args['output'] ) ) {
50
		$args['output'] = 'posts';
51
	}
52
53
	$args     = apply_filters( 'give_get_payments_args', $args );
54
	$payments = new Give_Payments_Query( $args );
55
56
	return $payments->get_payments();
57
}
58
59
/**
60
 * Retrieve payment by a given field
61
 *
62
 * @since  1.0
63
 *
64
 * @param  string $field The field to retrieve the payment with.
65
 * @param  mixed  $value The value for $field.
66
 *
67
 * @return mixed
68
 */
69
function give_get_payment_by( $field = '', $value = '' ) {
70
71
	if ( empty( $field ) || empty( $value ) ) {
72
		return false;
73
	}
74
75
	switch ( strtolower( $field ) ) {
76
77
		case 'id':
78
			$payment = new Give_Payment( $value );
79
			$id      = $payment->ID;
80
81
			if ( empty( $id ) ) {
82
				return false;
83
			}
84
85
			break;
86
87 View Code Duplication
		case 'key':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
			$payment = give_get_payments( array(
89
				'meta_key'       => '_give_payment_purchase_key',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
90
				'meta_value'     => $value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
91
				'posts_per_page' => 1,
92
				'fields'         => 'ids',
93
			) );
94
95
			if ( $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment 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...
96
				$payment = new Give_Payment( $payment[0] );
97
			}
98
99
			break;
100
101 View Code Duplication
		case 'payment_number':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
			$payment = give_get_payments( array(
103
				'meta_key'       => '_give_payment_number',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
104
				'meta_value'     => $value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
105
				'posts_per_page' => 1,
106
				'fields'         => 'ids',
107
			) );
108
109
			if ( $payment ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payment 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...
110
				$payment = new Give_Payment( $payment[0] );
111
			}
112
113
			break;
114
115
		default:
116
			return false;
117
	}// End switch().
118
119
	if ( $payment ) {
120
		return $payment;
121
	}
122
123
	return false;
124
}
125
126
/**
127
 * Insert Payment
128
 *
129
 * @since  1.0
130
 *
131
 * @param  array $payment_data Arguments passed.
132
 *
133
 * @return int|bool Payment ID if payment is inserted, false otherwise.
134
 */
135
function give_insert_payment( $payment_data = array() ) {
136
137
	if ( empty( $payment_data ) ) {
138
		return false;
139
	}
140
141
	$payment    = new Give_Payment();
142
	$gateway    = ! empty( $payment_data['gateway'] ) ? $payment_data['gateway'] : '';
143
	$gateway    = empty( $gateway ) && isset( $_POST['give-gateway'] ) ? $_POST['give-gateway'] : $gateway;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
144
	$form_id    = isset( $payment_data['give_form_id'] ) ? $payment_data['give_form_id'] : 0;
145
	$price_id   = give_get_payment_meta_price_id( $payment_data );
146
	$form_title = isset( $payment_data['give_form_title'] ) ? $payment_data['give_form_title'] : get_the_title( $form_id );
147
148
	// Set properties.
149
	$payment->total          = $payment_data['price'];
150
	$payment->status         = ! empty( $payment_data['status'] ) ? $payment_data['status'] : 'pending';
151
	$payment->currency       = ! empty( $payment_data['currency'] ) ? $payment_data['currency'] : give_get_currency();
152
	$payment->user_info      = $payment_data['user_info'];
153
	$payment->gateway        = $gateway;
154
	$payment->form_title     = $form_title;
155
	$payment->form_id        = $form_id;
156
	$payment->price_id       = $price_id;
157
	$payment->user_id        = $payment_data['user_info']['id'];
158
	$payment->email          = $payment_data['user_email'];
159
	$payment->first_name     = $payment_data['user_info']['first_name'];
160
	$payment->last_name      = $payment_data['user_info']['last_name'];
161
	$payment->email          = $payment_data['user_info']['email'];
162
	$payment->ip             = give_get_ip();
163
	$payment->key            = $payment_data['purchase_key'];
164
	$payment->mode           = give_is_test_mode() ? 'test' : 'live';
165
	$payment->parent_payment = ! empty( $payment_data['parent'] ) ? absint( $payment_data['parent'] ) : '';
166
167
	// Add the donation.
168
	$args = array(
169
		'price'    => $payment->total,
170
		'price_id' => $payment->price_id,
171
	);
172
173
	$payment->add_donation( $payment->form_id, $args );
174
175
	// Set date if present.
176
	if ( isset( $payment_data['post_date'] ) ) {
177
		$payment->date = $payment_data['post_date'];
178
	}
179
180
	// Handle sequential payments.
181
	if ( give_get_option( 'enable_sequential' ) ) {
182
		$number          = give_get_next_payment_number();
183
		$payment->number = give_format_payment_number( $number );
184
		update_option( 'give_last_payment_number', $number );
185
	}
186
187
	// Save payment.
188
	$payment->save();
189
190
	/**
191
	 * Fires while inserting payments.
192
	 *
193
	 * @since 1.0
194
	 *
195
	 * @param int   $payment_id   The payment ID.
196
	 * @param array $payment_data Arguments passed.
197
	 */
198
	do_action( 'give_insert_payment', $payment->ID, $payment_data );
199
200
	// Return payment ID upon success.
201
	if ( ! empty( $payment->ID ) ) {
202
		return $payment->ID;
203
	}
204
205
	// Return false if no payment was inserted.
206
	return false;
207
208
}
209
210
/**
211
 * Create payment.
212
 *
213
 * @param $payment_data
214
 *
215
 * @return bool|int
216
 */
217
function give_create_payment( $payment_data ) {
218
219
	$form_id  = intval( $payment_data['post_data']['give-form-id'] );
220
	$price_id = isset( $payment_data['post_data']['give-price-id'] ) ? $payment_data['post_data']['give-price-id'] : '';
221
222
	// Collect payment data.
223
	$insert_payment_data = array(
224
		'price'           => $payment_data['price'],
225
		'give_form_title' => $payment_data['post_data']['give-form-title'],
226
		'give_form_id'    => $form_id,
227
		'give_price_id'   => $price_id,
228
		'date'            => $payment_data['date'],
229
		'user_email'      => $payment_data['user_email'],
230
		'purchase_key'    => $payment_data['purchase_key'],
231
		'currency'        => give_get_currency(),
232
		'user_info'       => $payment_data['user_info'],
233
		'status'          => 'pending',
234
		'gateway'         => 'paypal',
235
	);
236
237
	/**
238
	 * Filter the payment params.
239
	 *
240
	 * @since 1.8
241
	 *
242
	 * @param array $insert_payment_data
243
	 */
244
	$insert_payment_data = apply_filters( 'give_create_payment', $insert_payment_data );
245
246
	// Record the pending payment.
247
	return give_insert_payment( $insert_payment_data );
248
}
249
250
/**
251
 * Updates a payment status.
252
 *
253
 * @since  1.0
254
 *
255
 * @param  int    $payment_id Payment ID.
256
 * @param  string $new_status New Payment Status. Default is 'publish'.
257
 *
258
 * @return bool
259
 */
260
function give_update_payment_status( $payment_id, $new_status = 'publish' ) {
261
262
	$updated = false;
263
	$payment = new Give_Payment( $payment_id );
264
265
	if( $payment && $payment->ID > 0 ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
266
267
		$payment->status = $new_status;
268
		$updated = $payment->save();
269
270
	}
271
272
	return $updated;
273
}
274
275
276
/**
277
 * Deletes a Donation
278
 *
279
 * @since  1.0
280
 * @global      $give_logs
281
 *
282
 * @param  int  $payment_id   Payment ID (default: 0).
283
 * @param  bool $update_donor If we should update the donor stats (default:true).
284
 *
285
 * @return void
286
 */
287
function give_delete_donation( $payment_id = 0, $update_donor = true ) {
288
	global $give_logs;
289
290
	$payment  = new Give_Payment( $payment_id );
291
	$amount   = give_get_payment_amount( $payment_id );
292
	$status   = $payment->post_status;
293
	$donor_id = give_get_payment_donor_id( $payment_id );
294
	$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...
295
296
	// Only undo donations that aren't these statuses.
297
	$dont_undo_statuses = apply_filters( 'give_undo_donation_statuses', array(
298
		'pending',
299
		'cancelled',
300
	) );
301
302
	if ( ! in_array( $status, $dont_undo_statuses ) ) {
303
		give_undo_donation( $payment_id );
304
	}
305
306
	if ( $status == 'publish' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
307
308
		// Only decrease earnings if they haven't already been decreased (or were never increased for this payment).
309
		give_decrease_total_earnings( $amount );
310
311
		// @todo: Refresh only range related stat cache
312
		give_delete_donation_stats();
313
314
		if ( $donor->id && $update_donor ) {
315
316
			// Decrement the stats for the donor.
317
			$donor->decrease_donation_count();
318
			$donor->decrease_value( $amount );
319
320
		}
321
	}
322
323
	/**
324
	 * Fires before deleting payment.
325
	 *
326
	 * @since 1.0
327
	 *
328
	 * @param int $payment_id Payment ID.
329
	 */
330
	do_action( 'give_payment_delete', $payment_id );
331
332
	if ( $donor->id && $update_donor ) {
333
334
		// Remove the payment ID from the donor.
335
		$donor->remove_payment( $payment_id );
336
337
	}
338
339
	// Remove the payment.
340
	wp_delete_post( $payment_id, true );
341
342
	// Remove related sale log entries.
343
	$give_logs->delete_logs( null, 'sale', array(
344
		array(
345
			'key'   => '_give_log_payment_id',
346
			'value' => $payment_id,
347
		),
348
	) );
349
350
	/**
351
	 * Fires after payment deleted.
352
	 *
353
	 * @since 1.0
354
	 *
355
	 * @param int $payment_id Payment ID.
356
	 */
357
	do_action( 'give_payment_deleted', $payment_id );
358
}
359
360
/**
361
 * Undo Donation
362
 *
363
 * Undoes a donation, including the decrease of donations and earning stats.
364
 * Used for when refunding or deleting a donation.
365
 *
366
 * @since  1.0
367
 *
368
 * @param  int $payment_id Payment ID.
369
 *
370
 * @return void
371
 */
372
function give_undo_donation( $payment_id ) {
373
374
	$payment = new Give_Payment( $payment_id );
375
376
	$maybe_decrease_earnings = apply_filters( 'give_decrease_earnings_on_undo', true, $payment, $payment->form_id );
377
	if ( true === $maybe_decrease_earnings ) {
378
		// Decrease earnings.
379
		give_decrease_earnings( $payment->form_id, $payment->total );
380
	}
381
382
	$maybe_decrease_donations = apply_filters( 'give_decrease_donations_on_undo', true, $payment, $payment->form_id );
383
	if ( true === $maybe_decrease_donations ) {
384
		// Decrease donation count.
385
		give_decrease_donation_count( $payment->form_id );
386
	}
387
388
}
389
390
391
/**
392
 * Count Payments
393
 *
394
 * Returns the total number of payments recorded.
395
 *
396
 * @since  1.0
397
 *
398
 * @param  array $args Arguments passed.
399
 *
400
 * @return object $stats Contains the number of payments per payment status.
401
 */
402
function give_count_payments( $args = array() ) {
403
404
	global $wpdb;
405
406
	$defaults = array(
407
		'user'       => null,
408
		's'          => null,
409
		'start-date' => null,
410
		'end-date'   => null,
411
		'form_id'    => null,
412
	);
413
414
	$args = wp_parse_args( $args, $defaults );
415
416
	$select = 'SELECT p.post_status,count( * ) AS num_posts';
417
	$join   = '';
418
	$where  = "WHERE p.post_type = 'give_payment' AND p.post_status IN ('". implode( "','", give_get_payment_status_keys() ). "')";
419
420
	// Count payments for a specific user.
421
	if ( ! empty( $args['user'] ) ) {
422
423
		if ( is_email( $args['user'] ) ) {
424
			$field = 'email';
425
		} elseif ( is_numeric( $args['user'] ) ) {
426
			$field = 'id';
427
		} else {
428
			$field = '';
429
		}
430
431
		$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
432
433
		if ( ! empty( $field ) ) {
434
			$where .= "
435
				AND m.meta_key = '_give_payment_user_{$field}'
436
				AND m.meta_value = '{$args['user']}'";
437
		}
438
	} elseif ( ! empty( $args['donor'] ) ) {
439
440
		$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
441
		$where .= "
442
			AND m.meta_key = '_give_payment_customer_id'
443
			AND m.meta_value = '{$args['donor']}'";
444
445
		// Count payments for a search.
446
	} elseif ( ! empty( $args['s'] ) ) {
447
448
		if ( is_email( $args['s'] ) || strlen( $args['s'] ) == 32 ) {
449
450
			if ( is_email( $args['s'] ) ) {
451
				$field = '_give_payment_user_email';
452
			} else {
453
				$field = '_give_payment_purchase_key';
454
			}
455
456
			$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
457
			$where .= $wpdb->prepare( '
458
                AND m.meta_key = %s
459
                AND m.meta_value = %s', $field, $args['s'] );
460
461
		} elseif ( '#' == substr( $args['s'], 0, 1 ) ) {
462
463
			$search = str_replace( '#:', '', $args['s'] );
464
			$search = str_replace( '#', '', $search );
465
466
			$select = 'SELECT p.post_status,count( * ) AS num_posts ';
467
			$join   = '';
468
			$where  = $wpdb->prepare( 'WHERE p.post_type=%s  AND p.ID = %d ', 'give_payment', $search );
469
470
		} elseif ( is_numeric( $args['s'] ) ) {
471
472
			$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
473
			$where .= $wpdb->prepare( "
474
				AND m.meta_key = '_give_payment_user_id'
475
				AND m.meta_value = %d", $args['s'] );
476
477
		} else {
478
			$search = $wpdb->esc_like( $args['s'] );
479
			$search = '%' . $search . '%';
480
481
			$where .= $wpdb->prepare( 'AND ((p.post_title LIKE %s) OR (p.post_content LIKE %s))', $search, $search );
482
		}// End if().
483
	}// End if().
484
485
	if ( ! empty( $args['form_id'] ) && is_numeric( $args['form_id'] ) ) {
486
487
		$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
488
		$where .= $wpdb->prepare( '
489
                AND m.meta_key = %s
490
                AND m.meta_value = %s', '_give_payment_form_id', $args['form_id'] );
491
	}
492
493
	// Limit payments count by date.
494
	if ( ! empty( $args['start-date'] ) && false !== strpos( $args['start-date'], '/' ) ) {
495
496
		$date_parts = explode( '/', $args['start-date'] );
497
		$month      = ! empty( $date_parts[0] ) && is_numeric( $date_parts[0] ) ? $date_parts[0] : 0;
498
		$day        = ! empty( $date_parts[1] ) && is_numeric( $date_parts[1] ) ? $date_parts[1] : 0;
499
		$year       = ! empty( $date_parts[2] ) && is_numeric( $date_parts[2] ) ? $date_parts[2] : 0;
500
501
		$is_date = checkdate( $month, $day, $year );
502 View Code Duplication
		if ( false !== $is_date ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
503
504
			$date  = new DateTime( $args['start-date'] );
505
			$where .= $wpdb->prepare( " AND p.post_date >= '%s'", $date->format( 'Y-m-d' ) );
506
507
		}
508
509
		// Fixes an issue with the payments list table counts when no end date is specified (with stats class).
510
		if ( empty( $args['end-date'] ) ) {
511
			$args['end-date'] = $args['start-date'];
512
		}
513
	}
514
515
	if ( ! empty( $args['end-date'] ) && false !== strpos( $args['end-date'], '/' ) ) {
516
517
		$date_parts = explode( '/', $args['end-date'] );
518
519
		$month = ! empty( $date_parts[0] ) ? $date_parts[0] : 0;
520
		$day   = ! empty( $date_parts[1] ) ? $date_parts[1] : 0;
521
		$year  = ! empty( $date_parts[2] ) ? $date_parts[2] : 0;
522
523
		$is_date = checkdate( $month, $day, $year );
524 View Code Duplication
		if ( false !== $is_date ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
525
526
			$date  = new DateTime( $args['end-date'] );
527
			$where .= $wpdb->prepare( " AND p.post_date <= '%s'", $date->format( 'Y-m-d' ) );
528
529
		}
530
	}
531
532
	$where = apply_filters( 'give_count_payments_where', $where );
533
	$join  = apply_filters( 'give_count_payments_join', $join );
534
535
	$query = "$select
536
		FROM $wpdb->posts p
537
		$join
538
		$where
539
		GROUP BY p.post_status
540
	";
541
542
	$cache_key = md5( $query );
543
544
	$count = wp_cache_get( $cache_key, 'counts' );
545
	if ( false !== $count ) {
546
		return $count;
547
	}
548
549
	$count = $wpdb->get_results( $query, ARRAY_A );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
550
551
	$stats    = array();
552
	$statuses = get_post_stati();
553
	if ( isset( $statuses['private'] ) && empty( $args['s'] ) ) {
554
		unset( $statuses['private'] );
555
	}
556
557
	foreach ( $statuses as $state ) {
558
		$stats[ $state ] = 0;
559
	}
560
561
	foreach ( (array) $count as $row ) {
562
563
		if ( 'private' == $row['post_status'] && empty( $args['s'] ) ) {
564
			continue;
565
		}
566
567
		$stats[ $row['post_status'] ] = $row['num_posts'];
568
	}
569
570
	$stats = (object) $stats;
571
	wp_cache_set( $cache_key, $stats, 'counts' );
572
573
	return $stats;
574
}
575
576
577
/**
578
 * Check For Existing Payment
579
 *
580
 * @since  1.0
581
 *
582
 * @param  int $payment_id Payment ID
583
 *
584
 * @return bool $exists True if payment exists, false otherwise.
585
 */
586
function give_check_for_existing_payment( $payment_id ) {
587
	$exists  = false;
588
	$payment = new Give_Payment( $payment_id );
589
590
	if ( $payment_id === $payment->ID && 'publish' === $payment->status ) {
591
		$exists = true;
592
	}
593
594
	return $exists;
595
}
596
597
/**
598
 * Get Payment Status
599
 *
600
 * @since 1.0
601
 *
602
 * @param WP_Post|Give_Payment|int $payment      Payment object or payment ID.
603
 * @param bool                 $return_label Whether to return the translated status label
604
 *                                           instead of status value. Default false.
605
 *
606
 * @return bool|mixed True if payment status exists, false otherwise.
607
 */
608
function give_get_payment_status( $payment, $return_label = false ) {
609
610
	if( is_numeric( $payment ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
611
612
		$payment = new Give_Payment( $payment );
613
614
		if( ! $payment->ID > 0 ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
615
			return false;
616
		}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
617
618
	}
619
620
	if ( ! is_object( $payment ) || ! isset( $payment->post_status ) ) {
621
		return false;
622
	}
623
624
	$statuses = give_get_payment_statuses();
625
626
	if ( ! is_array( $statuses ) || empty( $statuses ) ) {
627
		return false;
628
	}
629
630
	// Get payment object if not already given.
631
	$payment = $payment instanceof Give_Payment ? $payment : new Give_Payment( $payment->ID );
632
633
	if ( array_key_exists( $payment->status, $statuses ) ) {
634
		if ( true === $return_label ) {
635
			// Return translated status label.
636
			return $statuses[ $payment->status ];
637
		} else {
638
			// Account that our 'publish' status is labeled 'Complete'
639
			$post_status = 'publish' == $payment->status ? 'Complete' : $payment->post_status;
640
641
			// Make sure we're matching cases, since they matter
642
			return array_search( strtolower( $post_status ), array_map( 'strtolower', $statuses ) );
643
		}
644
	}
645
646
	return false;
647
}
648
649
/**
650
 * Retrieves all available statuses for payments.
651
 *
652
 * @since  1.0
653
 *
654
 * @return array $payment_status All the available payment statuses.
655
 */
656
function give_get_payment_statuses() {
657
	$payment_statuses = array(
658
		'pending'     => __( 'Pending', 'give' ),
659
		'publish'     => __( 'Complete', 'give' ),
660
		'refunded'    => __( 'Refunded', 'give' ),
661
		'failed'      => __( 'Failed', 'give' ),
662
		'cancelled'   => __( 'Cancelled', 'give' ),
663
		'abandoned'   => __( 'Abandoned', 'give' ),
664
		'preapproval' => __( 'Pre-Approved', 'give' ),
665
		'processing'  => __( 'Processing', 'give' ),
666
		'revoked'     => __( 'Revoked', 'give' ),
667
	);
668
669
	return apply_filters( 'give_payment_statuses', $payment_statuses );
670
}
671
672
/**
673
 * Get Payment Status Keys
674
 *
675
 * Retrieves keys for all available statuses for payments
676
 *
677
 * @since  1.0
678
 *
679
 * @return array $payment_status All the available payment statuses.
680
 */
681
function give_get_payment_status_keys() {
682
	$statuses = array_keys( give_get_payment_statuses() );
683
	asort( $statuses );
684
685
	return array_values( $statuses );
686
}
687
688
/**
689
 * Get Earnings By Date
690
 *
691
 * @since  1.0
692
 *
693
 * @param  int $day       Day number. Default is null.
694
 * @param  int $month_num Month number. Default is null.
695
 * @param  int $year      Year number. Default is null.
696
 * @param  int $hour      Hour number. Default is null.
697
 *
698
 * @return int $earnings  Earnings
699
 */
700
function give_get_earnings_by_date( $day = null, $month_num, $year = null, $hour = null ) {
701
702
	// This is getting deprecated soon. Use Give_Payment_Stats with the get_earnings() method instead.
703
	global $wpdb;
704
705
	$args = array(
706
		'post_type'              => 'give_payment',
707
		'nopaging'               => true,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
708
		'year'                   => $year,
709
		'monthnum'               => $month_num,
710
		'post_status'            => array( 'publish' ),
711
		'fields'                 => 'ids',
712
		'update_post_term_cache' => false,
713
	);
714
	if ( ! empty( $day ) ) {
715
		$args['day'] = $day;
716
	}
717
718
	if ( ! empty( $hour ) ) {
719
		$args['hour'] = $hour;
720
	}
721
722
	$args = apply_filters( 'give_get_earnings_by_date_args', $args );
723
	$key  = Give_Cache::get_key( 'give_stats', $args );
724
725 View Code Duplication
	if ( ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'give-refresh-reports' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
726
		$earnings = false;
727
	} else {
728
		$earnings = Give_Cache::get( $key );
729
	}
730
731
	if ( false === $earnings ) {
732
		$donations = get_posts( $args );
733
		$earnings  = 0;
734
		if ( $donations ) {
735
			$donations = implode( ',', $donations );
736
737
			$earnings = $wpdb->get_var( "SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_give_payment_total' AND post_id IN ({$donations})" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
738
739
		}
740
		// Cache the results for one hour.
741
		Give_Cache::set( $key, $earnings, HOUR_IN_SECONDS );
742
	}
743
744
	return round( $earnings, 2 );
745
}
746
747
/**
748
 * Get Donations (sales) By Date
749
 *
750
 * @since  1.0
751
 *
752
 * @param  int $day       Day number. Default is null.
753
 * @param  int $month_num Month number. Default is null.
754
 * @param  int $year      Year number. Default is null.
755
 * @param  int $hour      Hour number. Default is null.
756
 *
757
 * @return int $count     Sales
758
 */
759
function give_get_sales_by_date( $day = null, $month_num = null, $year = null, $hour = null ) {
760
761
	// This is getting deprecated soon. Use Give_Payment_Stats with the get_sales() method instead.
762
	$args = array(
763
		'post_type'              => 'give_payment',
764
		'nopaging'               => true,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
765
		'year'                   => $year,
766
		'fields'                 => 'ids',
767
		'post_status'            => array( 'publish' ),
768
		'update_post_meta_cache' => false,
769
		'update_post_term_cache' => false,
770
	);
771
772
	$show_free = apply_filters( 'give_sales_by_date_show_free', true, $args );
773
774
	if ( false === $show_free ) {
775
		$args['meta_query'] = array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
776
			array(
777
				'key'     => '_give_payment_total',
778
				'value'   => 0,
779
				'compare' => '>',
780
				'type'    => 'NUMERIC',
781
			),
782
		);
783
	}
784
785
	if ( ! empty( $month_num ) ) {
786
		$args['monthnum'] = $month_num;
787
	}
788
789
	if ( ! empty( $day ) ) {
790
		$args['day'] = $day;
791
	}
792
793
	if ( ! empty( $hour ) ) {
794
		$args['hour'] = $hour;
795
	}
796
797
	$args = apply_filters( 'give_get_sales_by_date_args', $args );
798
799
	$key = Give_Cache::get_key( 'give_stats', $args );
800
801 View Code Duplication
	if ( ! empty( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'give-refresh-reports' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
802
		$count = false;
803
	} else {
804
		$count = Give_Cache::get( $key );
805
	}
806
807
	if ( false === $count ) {
808
		$donations = new WP_Query( $args );
809
		$count     = (int) $donations->post_count;
810
		// Cache the results for one hour.
811
		Give_Cache::set( $key, $count, HOUR_IN_SECONDS );
812
	}
813
814
	return $count;
815
}
816
817
/**
818
 * Checks whether a payment has been marked as complete.
819
 *
820
 * @since  1.0
821
 *
822
 * @param  int $payment_id Payment ID to check against.
823
 *
824
 * @return bool $ret True if complete, false otherwise.
825
 */
826
function give_is_payment_complete( $payment_id ) {
827
	$payment = new Give_Payment( $payment_id );
828
829
	$ret = false;
830
831
	if ( $payment->ID > 0 ) {
832
833
		if ( (int) $payment_id === (int) $payment->ID && 'publish' == $payment->status ) {
834
			$ret = true;
835
		}
836
	}
837
838
	return apply_filters( 'give_is_payment_complete', $ret, $payment_id, $payment->post_status );
839
}
840
841
/**
842
 * Get Total Donations.
843
 *
844
 * @since  1.0
845
 *
846
 * @return int $count Total number of donations.
847
 */
848
function give_get_total_donations() {
849
850
	$payments = give_count_payments();
851
852
	return $payments->publish;
853
}
854
855
/**
856
 * Get Total Earnings
857
 *
858
 * @since  1.0
859
 *
860
 * @param bool $recalculate Recalculate earnings forcefully.
861
 *
862
 * @return float $total Total earnings.
863
 */
864
function give_get_total_earnings( $recalculate = false ) {
865
866
	$total = get_option( 'give_earnings_total', 0 );
867
868
	// Calculate total earnings.
869
	if ( ! $total || $recalculate ) {
870
		global $wpdb;
871
872
		$total = (float) 0;
873
874
		$args = apply_filters( 'give_get_total_earnings_args', array(
875
			'offset' => 0,
876
			'number' => - 1,
877
			'status' => array( 'publish' ),
878
			'fields' => 'ids',
879
		) );
880
881
		$payments = give_get_payments( $args );
882
		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...
883
884
			/**
885
			 * If performing a donation, we need to skip the very last payment in the database,
886
			 * since it calls give_increase_total_earnings() on completion,
887
			 * which results in duplicated earnings for the very first donation.
888
			 */
889
			if ( did_action( 'give_update_payment_status' ) ) {
890
				array_pop( $payments );
891
			}
892
893
			if ( ! empty( $payments ) ) {
894
				$payments = implode( ',', $payments );
895
				$total    += $wpdb->get_var( "SELECT SUM(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_give_payment_total' AND post_id IN({$payments})" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
896
			}
897
		}
898
899
		update_option( 'give_earnings_total', $total, 'no' );
900
	}
901
902
	if ( $total < 0 ) {
903
		$total = 0; // Don't ever show negative earnings.
904
	}
905
906
	return apply_filters( 'give_total_earnings', round( $total, give_currency_decimal_filter() ) );
907
}
908
909
/**
910
 * Increase the Total Earnings
911
 *
912
 * @since  1.0
913
 *
914
 * @param  int $amount   The amount you would like to increase the total earnings by.
915
 *                       Default is 0.
916
 *
917
 * @return float $total  Total earnings.
918
 */
919
function give_increase_total_earnings( $amount = 0 ) {
920
	$total = give_get_total_earnings();
921
	$total += $amount;
922
	update_option( 'give_earnings_total', $total );
923
924
	return $total;
925
}
926
927
/**
928
 * Decrease the Total Earnings
929
 *
930
 * @since 1.0
931
 *
932
 * @param int $amount The amount you would like to decrease the total earnings by.
933
 *
934
 * @return float $total Total earnings.
935
 */
936
function give_decrease_total_earnings( $amount = 0 ) {
937
	$total = give_get_total_earnings();
938
	$total -= $amount;
939
	if ( $total < 0 ) {
940
		$total = 0;
941
	}
942
	update_option( 'give_earnings_total', $total );
943
944
	return $total;
945
}
946
947
/**
948
 * Get Payment Meta for a specific Payment
949
 *
950
 * @since 1.0
951
 *
952
 * @param int    $payment_id Payment ID.
953
 * @param string $meta_key   The meta key to pull.
954
 * @param bool   $single     Pull single meta entry or as an object.
955
 *
956
 * @return mixed $meta Payment Meta.
957
 */
958
function give_get_payment_meta( $payment_id = 0, $meta_key = '_give_payment_meta', $single = true ) {
959
	$payment = new Give_Payment( $payment_id );
960
961
	return $payment->get_meta( $meta_key, $single );
962
}
963
964
/**
965
 * Update the meta for a payment
966
 *
967
 * @param  int    $payment_id Payment ID.
968
 * @param  string $meta_key   Meta key to update.
969
 * @param  string $meta_value Value to update to.
970
 * @param  string $prev_value Previous value.
971
 *
972
 * @return mixed Meta ID if successful, false if unsuccessful.
973
 */
974
function give_update_payment_meta( $payment_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) {
975
	$payment = new Give_Payment( $payment_id );
976
977
	return $payment->update_meta( $meta_key, $meta_value, $prev_value );
978
}
979
980
/**
981
 * Get the user_info Key from Payment Meta
982
 *
983
 * @since 1.0
984
 *
985
 * @param int $payment_id Payment ID.
986
 *
987
 * @return array $user_info User Info Meta Values.
988
 */
989
function give_get_payment_meta_user_info( $payment_id ) {
990
	$payment = new Give_Payment( $payment_id );
991
992
	return $payment->user_info;
993
}
994
995
/**
996
 * Get the donations Key from Payment Meta
997
 *
998
 * Retrieves the form_id from a (Previously titled give_get_payment_meta_donations)
999
 *
1000
 * @since 1.0
1001
 *
1002
 * @param int $payment_id Payment ID.
1003
 *
1004
 * @return int $form_id Form ID.
1005
 */
1006
function give_get_payment_form_id( $payment_id ) {
1007
	$payment = new Give_Payment( $payment_id );
1008
1009
	return $payment->form_id;
1010
}
1011
1012
/**
1013
 * Get the user email associated with a payment
1014
 *
1015
 * @since 1.0
1016
 *
1017
 * @param int $payment_id Payment ID.
1018
 *
1019
 * @return string $email User email.
1020
 */
1021
function give_get_payment_user_email( $payment_id ) {
1022
	$payment = new Give_Payment( $payment_id );
1023
1024
	return $payment->email;
1025
}
1026
1027
/**
1028
 * Is the payment provided associated with a user account
1029
 *
1030
 * @since  1.3
1031
 *
1032
 * @param  int $payment_id The payment ID.
1033
 *
1034
 * @return bool $is_guest_payment If the payment is associated with a user (false) or not (true)
1035
 */
1036
function give_is_guest_payment( $payment_id ) {
1037
	$payment_user_id  = give_get_payment_user_id( $payment_id );
1038
	$is_guest_payment = ! empty( $payment_user_id ) && $payment_user_id > 0 ? false : true;
1039
1040
	return (bool) apply_filters( 'give_is_guest_payment', $is_guest_payment, $payment_id );
1041
}
1042
1043
/**
1044
 * Get the user ID associated with a payment
1045
 *
1046
 * @since 1.3
1047
 *
1048
 * @param int $payment_id Payment ID.
1049
 *
1050
 * @return int $user_id User ID.
1051
 */
1052
function give_get_payment_user_id( $payment_id ) {
1053
	$payment = new Give_Payment( $payment_id );
1054
1055
	return $payment->user_id;
1056
}
1057
1058
/**
1059
 * Get the donor ID associated with a payment.
1060
 *
1061
 * @since 1.0
1062
 *
1063
 * @param int $payment_id Payment ID.
1064
 *
1065
 * @return int $payment->customer_id Donor ID.
1066
 */
1067
function give_get_payment_donor_id( $payment_id ) {
1068
	$payment = new Give_Payment( $payment_id );
1069
1070
	return $payment->customer_id;
1071
}
1072
1073
/**
1074
 * Get the IP address used to make a donation
1075
 *
1076
 * @since 1.0
1077
 *
1078
 * @param int $payment_id Payment ID.
1079
 *
1080
 * @return string $ip User IP.
1081
 */
1082
function give_get_payment_user_ip( $payment_id ) {
1083
	$payment = new Give_Payment( $payment_id );
1084
1085
	return $payment->ip;
1086
}
1087
1088
/**
1089
 * Get the date a payment was completed
1090
 *
1091
 * @since 1.0
1092
 *
1093
 * @param int $payment_id Payment ID.
1094
 *
1095
 * @return string $date The date the payment was completed.
1096
 */
1097
function give_get_payment_completed_date( $payment_id = 0 ) {
1098
	$payment = new Give_Payment( $payment_id );
1099
1100
	return $payment->completed_date;
1101
}
1102
1103
/**
1104
 * Get the gateway associated with a payment
1105
 *
1106
 * @since 1.0
1107
 *
1108
 * @param int $payment_id Payment ID.
1109
 *
1110
 * @return string $gateway Gateway.
1111
 */
1112
function give_get_payment_gateway( $payment_id ) {
1113
	$payment = new Give_Payment( $payment_id );
1114
1115
	return $payment->gateway;
1116
}
1117
1118
/**
1119
 * Get the currency code a payment was made in
1120
 *
1121
 * @since 1.0
1122
 *
1123
 * @param int $payment_id Payment ID.
1124
 *
1125
 * @return string $currency The currency code.
1126
 */
1127
function give_get_payment_currency_code( $payment_id = 0 ) {
1128
	$payment = new Give_Payment( $payment_id );
1129
1130
	return $payment->currency;
1131
}
1132
1133
/**
1134
 * Get the currency name a payment was made in
1135
 *
1136
 * @since 1.0
1137
 *
1138
 * @param int $payment_id Payment ID.
1139
 *
1140
 * @return string $currency The currency name.
1141
 */
1142
function give_get_payment_currency( $payment_id = 0 ) {
1143
	$currency = give_get_payment_currency_code( $payment_id );
1144
1145
	return apply_filters( 'give_payment_currency', give_get_currency_name( $currency ), $payment_id );
1146
}
1147
1148
/**
1149
 * Get the key for a donation
1150
 *
1151
 * @since 1.0
1152
 *
1153
 * @param int $payment_id Payment ID.
1154
 *
1155
 * @return string $key Donation key.
1156
 */
1157
function give_get_payment_key( $payment_id = 0 ) {
1158
	$payment = new Give_Payment( $payment_id );
1159
1160
	return $payment->key;
1161
}
1162
1163
/**
1164
 * Get the payment order number
1165
 *
1166
 * This will return the payment ID if sequential order numbers are not enabled or the order number does not exist
1167
 *
1168
 * @since 1.0
1169
 *
1170
 * @param int $payment_id Payment ID.
1171
 *
1172
 * @return string $number Payment order number.
1173
 */
1174
function give_get_payment_number( $payment_id = 0 ) {
1175
	$payment = new Give_Payment( $payment_id );
1176
1177
	return $payment->number;
1178
}
1179
1180
/**
1181
 * Formats the payment number with the prefix and postfix
1182
 *
1183
 * @since  1.3
1184
 *
1185
 * @param  int $number The payment number to format.
1186
 *
1187
 * @return string      The formatted payment number.
1188
 */
1189
function give_format_payment_number( $number ) {
1190
1191
	if ( ! give_get_option( 'enable_sequential' ) ) {
1192
		return $number;
1193
	}
1194
1195
	if ( ! is_numeric( $number ) ) {
1196
		return $number;
1197
	}
1198
1199
	$prefix  = give_get_option( 'sequential_prefix' );
1200
	$number  = absint( $number );
1201
	$postfix = give_get_option( 'sequential_postfix' );
1202
1203
	$formatted_number = $prefix . $number . $postfix;
1204
1205
	return apply_filters( 'give_format_payment_number', $formatted_number, $prefix, $number, $postfix );
1206
}
1207
1208
/**
1209
 * Gets the next available order number
1210
 *
1211
 * This is used when inserting a new payment
1212
 *
1213
 * @since 1.0
1214
 * @return string $number The next available payment number.
1215
 */
1216
function give_get_next_payment_number() {
1217
1218
	if ( ! give_get_option( 'enable_sequential' ) ) {
1219
		return false;
1220
	}
1221
1222
	$number           = get_option( 'give_last_payment_number' );
1223
	$start            = give_get_option( 'sequential_start', 1 );
1224
	$increment_number = true;
1225
1226
	if ( false !== $number ) {
1227
1228
		if ( empty( $number ) ) {
1229
1230
			$number           = $start;
1231
			$increment_number = false;
1232
1233
		}
1234
	} else {
1235
1236
		// This case handles the first addition of the new option, as well as if it get's deleted for any reason.
1237
		$payments     = new Give_Payments_Query( array(
1238
			'number'  => 1,
1239
			'order'   => 'DESC',
1240
			'orderby' => 'ID',
1241
			'output'  => 'posts',
1242
			'fields'  => 'ids',
1243
		) );
1244
		$last_payment = $payments->get_payments();
1245
1246
		if ( ! empty( $last_payment ) ) {
1247
1248
			$number = give_get_payment_number( $last_payment[0] );
1249
1250
		}
1251
1252
		if ( ! empty( $number ) && $number !== (int) $last_payment[0] ) {
1253
1254
			$number = give_remove_payment_prefix_postfix( $number );
1255
1256
		} else {
1257
1258
			$number           = $start;
1259
			$increment_number = false;
1260
		}
1261
	}// End if().
1262
1263
	$increment_number = apply_filters( 'give_increment_payment_number', $increment_number, $number );
1264
1265
	if ( $increment_number ) {
1266
		$number ++;
1267
	}
1268
1269
	return apply_filters( 'give_get_next_payment_number', $number );
1270
}
1271
1272
/**
1273
 * Given a given a number, remove the pre/postfix
1274
 *
1275
 * @since  1.3
1276
 *
1277
 * @param  string $number The formatted Current Number to increment.
1278
 *
1279
 * @return string The new Payment number without prefix and postfix.
1280
 */
1281
function give_remove_payment_prefix_postfix( $number ) {
1282
1283
	$prefix  = give_get_option( 'sequential_prefix' );
1284
	$postfix = give_get_option( 'sequential_postfix' );
1285
1286
	// Remove prefix.
1287
	$number = preg_replace( '/' . $prefix . '/', '', $number, 1 );
1288
1289
	// Remove the postfix.
1290
	$length      = strlen( $number );
1291
	$postfix_pos = strrpos( $number, $postfix );
1292
	if ( false !== $postfix_pos ) {
1293
		$number = substr_replace( $number, '', $postfix_pos, $length );
1294
	}
1295
1296
	// Ensure it's a whole number.
1297
	$number = intval( $number );
1298
1299
	return apply_filters( 'give_remove_payment_prefix_postfix', $number, $prefix, $postfix );
1300
1301
}
1302
1303
1304
/**
1305
 * Get Payment Amount
1306
 *
1307
 * Get the fully formatted payment amount. The payment amount is retrieved using give_get_payment_amount() and is then
1308
 * sent through give_currency_filter() and  give_format_amount() to format the amount correctly.
1309
 *
1310
 * @since       1.0
1311
 *
1312
 * @param int $payment_id Payment ID.
1313
 *
1314
 * @return string $amount Fully formatted payment amount.
1315
 */
1316
function give_payment_amount( $payment_id = 0 ) {
1317
	$amount = give_get_payment_amount( $payment_id );
1318
1319
	return give_currency_filter( give_format_amount( $amount, array( 'sanitize' => false ) ), give_get_payment_currency_code( $payment_id ) );
1320
}
1321
1322
/**
1323
 * Get the amount associated with a payment
1324
 *
1325
 * @access public
1326
 * @since  1.0
1327
 *
1328
 * @param int $payment_id Payment ID.
1329
 *
1330
 * @return mixed
1331
 */
1332
function give_get_payment_amount( $payment_id ) {
1333
1334
	$payment = new Give_Payment( $payment_id );
1335
1336
	return apply_filters( 'give_payment_amount', floatval( $payment->total ), $payment_id );
1337
}
1338
1339
/**
1340
 * Payment Subtotal
1341
 *
1342
 * Retrieves subtotal for payment and then returns a full formatted amount. This
1343
 * function essentially calls give_get_payment_subtotal()
1344
 *
1345
 * @since 1.5
1346
 *
1347
 * @param int $payment_id Payment ID.
1348
 *
1349
 * @see   give_get_payment_subtotal()
1350
 *
1351
 * @return array Fully formatted payment subtotal.
1352
 */
1353
function give_payment_subtotal( $payment_id = 0 ) {
1354
	$subtotal = give_get_payment_subtotal( $payment_id );
1355
1356
	return give_currency_filter( give_format_amount( $subtotal , array( 'sanitize' => false ) ), give_get_payment_currency_code( $payment_id ) );
1357
}
1358
1359
/**
1360
 * Get Payment Subtotal
1361
 *
1362
 * Retrieves subtotal for payment and then returns a non formatted amount.
1363
 *
1364
 * @since 1.5
1365
 *
1366
 * @param int $payment_id Payment ID.
1367
 *
1368
 * @return float $subtotal Subtotal for payment (non formatted).
1369
 */
1370
function give_get_payment_subtotal( $payment_id = 0 ) {
1371
	$payment = new Give_Payment( $payment_id );
1372
1373
	return $payment->subtotal;
1374
}
1375
1376
/**
1377
 * Retrieves the donation ID
1378
 *
1379
 * @since  1.0
1380
 *
1381
 * @param int $payment_id Payment ID.
1382
 *
1383
 * @return string The donation ID.
1384
 */
1385
function give_get_payment_transaction_id( $payment_id = 0 ) {
1386
	$payment = new Give_Payment( $payment_id );
1387
1388
	return $payment->transaction_id;
1389
}
1390
1391
/**
1392
 * Sets a Transaction ID in post meta for the given Payment ID.
1393
 *
1394
 * @since  1.0
1395
 *
1396
 * @param int    $payment_id     Payment ID.
1397
 * @param string $transaction_id The transaction ID from the gateway.
1398
 *
1399
 * @return bool|mixed
1400
 */
1401
function give_set_payment_transaction_id( $payment_id = 0, $transaction_id = '' ) {
1402
1403
	if ( empty( $payment_id ) || empty( $transaction_id ) ) {
1404
		return false;
1405
	}
1406
1407
	$transaction_id = apply_filters( 'give_set_payment_transaction_id', $transaction_id, $payment_id );
1408
1409
	return give_update_payment_meta( $payment_id, '_give_payment_transaction_id', $transaction_id );
1410
}
1411
1412
/**
1413
 * Retrieve the donation ID based on the key
1414
 *
1415
 * @since 1.0
1416
 * @global object $wpdb Used to query the database using the WordPress Database API.
1417
 *
1418
 * @param string $key  the key to search for.
1419
 *
1420
 * @return int $purchase Donation ID.
1421
 */
1422 View Code Duplication
function give_get_purchase_id_by_key( $key ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1423
	global $wpdb;
1424
1425
	$purchase = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_give_payment_purchase_key' AND meta_value = %s LIMIT 1", $key ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1426
1427
	if ( $purchase != null ) {
1428
		return $purchase;
1429
	}
1430
1431
	return 0;
1432
}
1433
1434
1435
/**
1436
 * Retrieve the donation ID based on the transaction ID
1437
 *
1438
 * @since 1.3
1439
 * @global object $wpdb Used to query the database using the WordPress Database API.
1440
 *
1441
 * @param string $key  The transaction ID to search for.
1442
 *
1443
 * @return int $purchase Donation ID.
1444
 */
1445 View Code Duplication
function give_get_purchase_id_by_transaction_id( $key ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1446
	global $wpdb;
1447
1448
	$purchase = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_give_payment_transaction_id' AND meta_value = %s LIMIT 1", $key ) );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
1449
1450
	if ( $purchase != null ) {
1451
		return $purchase;
1452
	}
1453
1454
	return 0;
1455
}
1456
1457
/**
1458
 * Retrieve all notes attached to a donation
1459
 *
1460
 * @since 1.0
1461
 *
1462
 * @param int    $payment_id The donation ID to retrieve notes for.
1463
 * @param string $search     Search for notes that contain a search term.
1464
 *
1465
 * @return array $notes Donation Notes
1466
 */
1467
function give_get_payment_notes( $payment_id = 0, $search = '' ) {
1468
1469
	if ( empty( $payment_id ) && empty( $search ) ) {
1470
		return false;
1471
	}
1472
1473
	remove_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1474
	remove_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10 );
1475
1476
	$notes = get_comments( array(
1477
		'post_id' => $payment_id,
1478
		'order' => 'ASC',
1479
		'search' => $search,
1480
	) );
1481
1482
	add_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1483
	add_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10, 2 );
1484
1485
	return $notes;
1486
}
1487
1488
1489
/**
1490
 * Add a note to a payment
1491
 *
1492
 * @since 1.0
1493
 *
1494
 * @param int    $payment_id The payment ID to store a note for.
1495
 * @param string $note       The note to store.
1496
 *
1497
 * @return int The new note ID
1498
 */
1499
function give_insert_payment_note( $payment_id = 0, $note = '' ) {
1500
	if ( empty( $payment_id ) ) {
1501
		return false;
1502
	}
1503
1504
	/**
1505
	 * Fires before inserting payment note.
1506
	 *
1507
	 * @since 1.0
1508
	 *
1509
	 * @param int    $payment_id Payment ID.
1510
	 * @param string $note       The note.
1511
	 */
1512
	do_action( 'give_pre_insert_payment_note', $payment_id, $note );
1513
1514
	$note_id = wp_insert_comment( wp_filter_comment( array(
1515
		'comment_post_ID'      => $payment_id,
1516
		'comment_content'      => $note,
1517
		'user_id'              => is_admin() ? get_current_user_id() : 0,
1518
		'comment_date'         => current_time( 'mysql' ),
1519
		'comment_date_gmt'     => current_time( 'mysql', 1 ),
1520
		'comment_approved'     => 1,
1521
		'comment_parent'       => 0,
1522
		'comment_author'       => '',
1523
		'comment_author_IP'    => '',
1524
		'comment_author_url'   => '',
1525
		'comment_author_email' => '',
1526
		'comment_type'         => 'give_payment_note',
1527
1528
	) ) );
1529
1530
	/**
1531
	 * Fires after payment note inserted.
1532
	 *
1533
	 * @since 1.0
1534
	 *
1535
	 * @param int    $note_id    Note ID.
1536
	 * @param int    $payment_id Payment ID.
1537
	 * @param string $note       The note.
1538
	 */
1539
	do_action( 'give_insert_payment_note', $note_id, $payment_id, $note );
1540
1541
	return $note_id;
1542
}
1543
1544
/**
1545
 * Deletes a payment note
1546
 *
1547
 * @since 1.0
1548
 *
1549
 * @param int $comment_id The comment ID to delete.
1550
 * @param int $payment_id The payment ID the note is connected to.
1551
 *
1552
 * @return bool True on success, false otherwise.
1553
 */
1554
function give_delete_payment_note( $comment_id = 0, $payment_id = 0 ) {
1555
	if ( empty( $comment_id ) ) {
1556
		return false;
1557
	}
1558
1559
	/**
1560
	 * Fires before deleting donation note.
1561
	 *
1562
	 * @since 1.0
1563
	 *
1564
	 * @param int $comment_id Note ID.
1565
	 * @param int $payment_id Payment ID.
1566
	 */
1567
	do_action( 'give_pre_delete_payment_note', $comment_id, $payment_id );
1568
1569
	$ret = wp_delete_comment( $comment_id, true );
1570
1571
	/**
1572
	 * Fires after donation note deleted.
1573
	 *
1574
	 * @since 1.0
1575
	 *
1576
	 * @param int $comment_id Note ID.
1577
	 * @param int $payment_id Payment ID.
1578
	 */
1579
	do_action( 'give_post_delete_payment_note', $comment_id, $payment_id );
1580
1581
	return $ret;
1582
}
1583
1584
/**
1585
 * Gets the payment note HTML
1586
 *
1587
 * @since 1.0
1588
 *
1589
 * @param object|int $note       The comment object or ID.
1590
 * @param int        $payment_id The payment ID the note is connected to.
1591
 *
1592
 * @return string
1593
 */
1594
function give_get_payment_note_html( $note, $payment_id = 0 ) {
1595
1596
	if ( is_numeric( $note ) ) {
1597
		$note = get_comment( $note );
1598
	}
1599
1600
	if ( ! empty( $note->user_id ) ) {
1601
		$user = get_userdata( $note->user_id );
1602
		$user = $user->display_name;
1603
	} else {
1604
		$user = esc_html__( 'System', 'give' );
1605
	}
1606
1607
	$date_format = give_date_format() . ', ' . get_option( 'time_format' );
1608
1609
	$delete_note_url = wp_nonce_url( add_query_arg( array(
1610
		'give-action' => 'delete_payment_note',
1611
		'note_id'     => $note->comment_ID,
1612
		'payment_id'  => $payment_id,
1613
	) ), 'give_delete_payment_note_' . $note->comment_ID );
1614
1615
	$note_html = '<div class="give-payment-note" id="give-payment-note-' . $note->comment_ID . '">';
1616
	$note_html .= '<p>';
1617
	$note_html .= '<strong>' . $user . '</strong>&nbsp;&ndash;&nbsp;<span style="color:#aaa;font-style:italic;">' . date_i18n( $date_format, strtotime( $note->comment_date ) ) . '</span><br/>';
1618
	$note_html .= $note->comment_content;
1619
	$note_html .= '&nbsp;&ndash;&nbsp;<a href="' . esc_url( $delete_note_url ) . '" class="give-delete-payment-note" data-note-id="' . absint( $note->comment_ID ) . '" data-payment-id="' . absint( $payment_id ) . '" aria-label="' . esc_attr__( 'Delete this donation note.', 'give' ) . '">' . esc_html__( 'Delete', 'give' ) . '</a>';
1620
	$note_html .= '</p>';
1621
	$note_html .= '</div>';
1622
1623
	return $note_html;
1624
1625
}
1626
1627
/**
1628
 * Exclude notes (comments) on give_payment post type from showing in Recent
1629
 * Comments widgets
1630
 *
1631
 * @since 1.0
1632
 *
1633
 * @param object $query WordPress Comment Query Object.
1634
 *
1635
 * @return void
1636
 */
1637
function give_hide_payment_notes( $query ) {
1638
	if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '>=' ) ) {
1639
		$types = isset( $query->query_vars['type__not_in'] ) ? $query->query_vars['type__not_in'] : array();
1640
		if ( ! is_array( $types ) ) {
1641
			$types = array( $types );
1642
		}
1643
		$types[]                           = 'give_payment_note';
1644
		$query->query_vars['type__not_in'] = $types;
1645
	}
1646
}
1647
1648
add_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1649
1650
/**
1651
 * Exclude notes (comments) on give_payment post type from showing in Recent Comments widgets
1652
 *
1653
 * @since 1.0
1654
 *
1655
 * @param array  $clauses          Comment clauses for comment query.
1656
 * @param object $wp_comment_query WordPress Comment Query Object.
1657
 *
1658
 * @return array $clauses Updated comment clauses.
1659
 */
1660
function give_hide_payment_notes_pre_41( $clauses, $wp_comment_query ) {
0 ignored issues
show
Unused Code introduced by
The parameter $wp_comment_query 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...
1661
	if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '<' ) ) {
1662
		$clauses['where'] .= ' AND comment_type != "give_payment_note"';
1663
	}
1664
1665
	return $clauses;
1666
}
1667
1668
add_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10, 2 );
1669
1670
1671
/**
1672
 * Exclude notes (comments) on give_payment post type from showing in comment feeds
1673
 *
1674
 * @since 1.0
1675
 *
1676
 * @param string $where
1677
 * @param object $wp_comment_query WordPress Comment Query Object.
1678
 *
1679
 * @return string $where
1680
 */
1681
function give_hide_payment_notes_from_feeds( $where, $wp_comment_query ) {
0 ignored issues
show
Unused Code introduced by
The parameter $wp_comment_query 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...
1682
	global $wpdb;
1683
1684
	$where .= $wpdb->prepare( ' AND comment_type != %s', 'give_payment_note' );
1685
1686
	return $where;
1687
}
1688
1689
add_filter( 'comment_feed_where', 'give_hide_payment_notes_from_feeds', 10, 2 );
1690
1691
1692
/**
1693
 * Remove Give Comments from the wp_count_comments function
1694
 *
1695
 * @access public
1696
 * @since  1.0
1697
 *
1698
 * @param array $stats   (empty from core filter).
1699
 * @param int   $post_id Post ID.
1700
 *
1701
 * @return array Array of comment counts.
1702
 */
1703
function give_remove_payment_notes_in_comment_counts( $stats, $post_id ) {
1704
	global $wpdb, $pagenow;
1705
1706
	if ( 'index.php' != $pagenow ) {
1707
		return $stats;
1708
	}
1709
1710
	$post_id = (int) $post_id;
1711
1712
	if ( apply_filters( 'give_count_payment_notes_in_comments', false ) ) {
1713
		return $stats;
1714
	}
1715
1716
	$stats = wp_cache_get( "comments-{$post_id}", 'counts' );
1717
1718
	if ( false !== $stats ) {
1719
		return $stats;
1720
	}
1721
1722
	$where = 'WHERE comment_type != "give_payment_note"';
1723
1724
	if ( $post_id > 0 ) {
1725
		$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
1726
	}
1727
1728
	$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
1729
1730
	$total    = 0;
1731
	$approved = array(
1732
		'0'            => 'moderated',
0 ignored issues
show
introduced by
Detected usage of 0, possible slow query.
Loading history...
1733
		'1'            => 'approved',
1734
		'spam'         => 'spam',
1735
		'trash'        => 'trash',
1736
		'post-trashed' => 'post-trashed',
1737
	);
1738
	foreach ( (array) $count as $row ) {
1739
		// Don't count post-trashed toward totals.
1740
		if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
1741
			$total += $row['num_comments'];
1742
		}
1743
		if ( isset( $approved[ $row['comment_approved'] ] ) ) {
1744
			$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
1745
		}
1746
	}
1747
1748
	$stats['total_comments'] = $total;
1749
	foreach ( $approved as $key ) {
1750
		if ( empty( $stats[ $key ] ) ) {
1751
			$stats[ $key ] = 0;
1752
		}
1753
	}
1754
1755
	$stats = (object) $stats;
1756
	wp_cache_set( "comments-{$post_id}", $stats, 'counts' );
1757
1758
	return $stats;
1759
}
1760
1761
add_filter( 'wp_count_comments', 'give_remove_payment_notes_in_comment_counts', 10, 2 );
1762
1763
1764
/**
1765
 * Filter where older than one week
1766
 *
1767
 * @access public
1768
 * @since  1.0
1769
 *
1770
 * @param string $where Where clause.
1771
 *
1772
 * @return string $where Modified where clause.
1773
 */
1774
function give_filter_where_older_than_week( $where = '' ) {
1775
	// Payments older than one week.
1776
	$start = date( 'Y-m-d', strtotime( '-7 days' ) );
1777
	$where .= " AND post_date <= '{$start}'";
1778
1779
	return $where;
1780
}
1781
1782
1783
/**
1784
 * Get Payment Form ID.
1785
 *
1786
 * Retrieves the form title and appends the level name if present.
1787
 *
1788
 * @since 1.5
1789
 *
1790
 * @param array  $payment_meta Payment meta data.
1791
 * @param bool   $only_level   If set to true will only return the level name if multi-level enabled.
1792
 * @param string $separator    The separator between the .
1793
 *
1794
 * @return string $form_title Returns the full title if $only_level is false, otherwise returns the levels title.
1795
 */
1796
function give_get_payment_form_title( $payment_meta, $only_level = false, $separator = '' ) {
1797
1798
	$form_id    = isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : 0;
1799
	$price_id   = isset( $payment_meta['price_id'] ) ? $payment_meta['price_id'] : null;
1800
	$form_title = isset( $payment_meta['form_title'] ) ? $payment_meta['form_title'] : '';
1801
1802
	if ( $only_level == true ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
Found "== true". Use Yoda Condition checks, you must
Loading history...
1803
		$form_title = '';
1804
	}
1805
1806
	// If multi-level, append to the form title.
1807
	if ( give_has_variable_prices( $form_id ) ) {
1808
1809
		// Only add separator if there is a form title.
1810
		if ( ! empty( $form_title ) ) {
1811
			$form_title .= ' ' . $separator . ' ';
1812
		}
1813
1814
		$form_title .= '<span class="donation-level-text-wrap">';
1815
1816
		if ( $price_id == 'custom' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
1817
			$custom_amount_text = give_get_meta( $form_id, '_give_custom_amount_text', true );
1818
			$form_title         .= ! empty( $custom_amount_text ) ? $custom_amount_text : __( 'Custom Amount', 'give' );
1819
		} else {
1820
			$form_title .= give_get_price_option_name( $form_id, $price_id );
1821
		}
1822
1823
		$form_title .= '</span>';
1824
1825
	}
1826
1827
	return apply_filters( 'give_get_payment_form_title', $form_title, $payment_meta );
1828
1829
}
1830
1831
/**
1832
 * Get Price ID
1833
 *
1834
 * Retrieves the Price ID when provided a proper form ID and price (donation) total
1835
 *
1836
 * @param int    $form_id Form ID.
1837
 * @param string $price   Price ID.
1838
 *
1839
 * @return string $price_id
1840
 */
1841
function give_get_price_id( $form_id, $price ) {
1842
	$price_id = null;
1843
1844
	if ( give_has_variable_prices( $form_id ) ) {
1845
1846
		$levels = maybe_unserialize( give_get_meta( $form_id, '_give_donation_levels', true ) );
1847
1848
		foreach ( $levels as $level ) {
1849
1850
			$level_amount = give_maybe_sanitize_amount( $level['_give_amount'] );
1851
1852
			// Check that this indeed the recurring price.
1853
			if ( $level_amount == $price ) {
1854
1855
				$price_id = $level['_give_id']['level_id'];
1856
				break;
1857
1858
			}
1859
		}
1860
1861
		if( is_null( $price_id ) && give_is_custom_price_mode( $form_id ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1862
			$price_id = 'custom';
1863
		}
1864
	}
1865
1866
	// Price ID must be numeric or string.
1867
	$price_id = ! is_numeric( $price_id ) && ! is_string( $price_id ) ? 0 : $price_id;
1868
1869
	return $price_id;
1870
}
1871
1872
/**
1873
 * Get/Print give form dropdown html
1874
 *
1875
 * This function is wrapper to public method forms_dropdown of Give_HTML_Elements class to get/print form dropdown html.
1876
 * Give_HTML_Elements is defined in includes/class-give-html-elements.php.
1877
 *
1878
 * @since 1.6
1879
 *
1880
 * @param array $args Arguments for form dropdown.
1881
 * @param bool  $echo This parameter decides if print form dropdown html output or not.
1882
 *
1883
 * @return string
1884
 */
1885
function give_get_form_dropdown( $args = array(), $echo = false ) {
1886
	$form_dropdown_html = Give()->html->forms_dropdown( $args );
1887
1888
	if ( ! $echo ) {
1889
		return $form_dropdown_html;
1890
	}
1891
1892
	echo $form_dropdown_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_dropdown_html'
Loading history...
1893
}
1894
1895
/**
1896
 * Get/Print give form variable price dropdown html
1897
 *
1898
 * @since 1.6
1899
 *
1900
 * @param array $args Arguments for form dropdown.
1901
 * @param bool  $echo This parameter decide if print form dropdown html output or not.
1902
 *
1903
 * @return string|bool
1904
 */
1905
function give_get_form_variable_price_dropdown( $args = array(), $echo = false ) {
1906
1907
	// Check for give form id.
1908
	if ( empty( $args['id'] ) ) {
1909
		return false;
1910
	}
1911
1912
	$form = new Give_Donate_Form( $args['id'] );
1913
1914
	// Check if form has variable prices or not.
1915
	if ( ! $form->ID || ! $form->has_variable_prices() ) {
1916
		return false;
1917
	}
1918
1919
	$variable_prices        = $form->get_prices();
1920
	$variable_price_options = array();
1921
1922
	// Check if multi donation form support custom donation or not.
1923
	if ( $form->is_custom_price_mode() ) {
1924
		$variable_price_options['custom'] = _x( 'Custom', 'custom donation dropdown item', 'give' );
1925
	}
1926
1927
	// Get variable price and ID from variable price array.
1928
	foreach ( $variable_prices as $variable_price ) {
1929
		$variable_price_options[ $variable_price['_give_id']['level_id'] ] = ! empty( $variable_price['_give_text'] ) ? $variable_price['_give_text'] : give_currency_filter( give_format_amount( $variable_price['_give_amount'], array( 'sanitize' => false ) ) );
1930
	}
1931
1932
	// Update options.
1933
	$args = array_merge( $args, array(
1934
		'options' => $variable_price_options,
1935
	) );
1936
1937
	// Generate select html.
1938
	$form_dropdown_html = Give()->html->select( $args );
1939
1940
	if ( ! $echo ) {
1941
		return $form_dropdown_html;
1942
	}
1943
1944
	echo $form_dropdown_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_dropdown_html'
Loading history...
1945
}
1946
1947
/**
1948
 * Get the price_id from the payment meta.
1949
 *
1950
 * Some gateways use `give_price_id` and others were using just `price_id`;
1951
 * This checks for the difference and falls back to retrieving it from the form as a last resort.
1952
 *
1953
 * @since 1.8.6
1954
 *
1955
 * @param $payment_meta
1956
 *
1957
 * @return string
1958
 */
1959
function give_get_payment_meta_price_id( $payment_meta ) {
1960
1961
	if ( isset( $payment_meta['give_price_id'] ) ) {
1962
		$price_id = $payment_meta['give_price_id'];
1963
	} elseif ( isset( $payment_meta['price_id'] ) ) {
1964
		$price_id = $payment_meta['price_id'];
1965
	} else {
1966
		$price_id = give_get_price_id( $payment_meta['give_form_id'], $payment_meta['price'] );
1967
	}
1968
1969
	return apply_filters( 'give_get_payment_meta_price_id', $price_id );
1970
1971
}
1972