Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

functions.php ➔ give_insert_payment()   F

Complexity

Conditions 14
Paths 4097

Size

Total Lines 77
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 14.0826

Importance

Changes 0
Metric Value
cc 14
eloc 42
nc 4097
nop 1
dl 0
loc 77
ccs 37
cts 40
cp 0.925
crap 14.0826
rs 2.1716
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 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 42
 * @type string $orderby  Sort retrieved payments by parameter. Default is 'ID'.
39 42
 * @type string $status   The status of the payments. Default is 'any'.
40 42
 * @type string $user     User. Default is null.
41
 * @type string $meta_key Custom field key. Default is null.
42 42
 * }
43 42
 *
44
 * @return array $payments Payments retrieved from the database
45 42
 */
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 42
 * Retrieve payment by a given field
61
 *
62
 * @since  1.0
63
 *
64 42
 * @param  string $field The field to retrieve the payment with.
65
 * @param  mixed  $value The value for $field.
66 42
 *
67 42
 * @return mixed
68 42
 */
69
function give_get_payment_by( $field = '', $value = '' ) {
70 42
71
	if ( empty( $field ) || empty( $value ) ) {
72
		return false;
73
	}
74 42
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 42
				'fields'         => 'ids',
107
			) );
108 42
109 42
			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 52
/**
127
 * Insert Payment
128
 *
129
 * @since  1.0
130 52
 *
131 52
 * @param  array $payment_data Arguments passed.
132 52
 *
133 52
 * @return int|bool Payment ID if payment is inserted, false otherwise.
134 52
 */
135 52
function give_insert_payment( $payment_data = array() ) {
136
137
	if ( empty( $payment_data ) ) {
138 52
		return false;
139 52
	}
140 52
141 52
	$payment    = new Give_Payment();
142 52
	$gateway    = ! empty( $payment_data['gateway'] ) ? $payment_data['gateway'] : '';
143 52
	$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 52
	$form_id    = isset( $payment_data['give_form_id'] ) ? $payment_data['give_form_id'] : 0;
145 52
	$price_id   = give_get_payment_meta_price_id( $payment_data );
146 52
	$form_title = isset( $payment_data['give_form_title'] ) ? $payment_data['give_form_title'] : get_the_title( $form_id );
147 52
148 52
	// Set properties.
149 52
	$payment->total          = $payment_data['price'];
150 52
	$payment->status         = ! empty( $payment_data['status'] ) ? $payment_data['status'] : 'pending';
151 52
	$payment->currency       = ! empty( $payment_data['currency'] ) ? $payment_data['currency'] : give_get_currency();
152 52
	$payment->user_info      = $payment_data['user_info'];
153 52
	$payment->gateway        = $gateway;
154 52
	$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 52
	$payment->email          = $payment_data['user_email'];
159 52
	$payment->first_name     = $payment_data['user_info']['first_name'];
160 52
	$payment->last_name      = $payment_data['user_info']['last_name'];
161 52
	$payment->email          = $payment_data['user_info']['email'];
162
	$payment->ip             = give_get_ip();
163 52
	$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 52
167
	// Add the donation.
168
	$args = array(
169
		'price'    => $payment->total,
170
		'price_id' => $payment->price_id,
171 52
	);
172 20
173 20
	$payment->add_donation( $payment->form_id, $args );
174 20
175 20
	// Set date if present.
176
	if ( isset( $payment_data['post_date'] ) ) {
177
		$payment->date = $payment_data['post_date'];
178 52
	}
179
180
	// Handle sequential payments.
181 52
	if ( give_get_option( 'enable_sequential' ) ) {
182
		$number          = give_get_next_payment_number();
183
		$payment->number = give_format_payment_number( $number );
184 52
		update_option( 'give_last_payment_number', $number );
185
	}
186
187 52
	// Clear the user's donation cache.
188 52
	delete_transient( 'give_user_' . $payment_data['user_info']['id'] . '_purchases' );
189
190
	// Save payment.
191
	$payment->save();
192
193
	/**
194
	 * Fires while inserting payments.
195
	 *
196
	 * @since 1.0
197
	 *
198
	 * @param int   $payment_id   The payment ID.
199
	 * @param array $payment_data Arguments passed.
200
	 */
201
	do_action( 'give_insert_payment', $payment->ID, $payment_data );
202
203
	// Return payment ID upon success.
204
	if ( ! empty( $payment->ID ) ) {
205
		return $payment->ID;
206
	}
207
208 35
	// Return false if no payment was inserted.
209 35
	return false;
210 35
211
}
212 35
213
/**
214
 * Create payment.
215
 *
216
 * @param $payment_data
217
 *
218
 * @return bool|int
219
 */
220
function give_create_payment( $payment_data ) {
221
222
	$form_id  = intval( $payment_data['post_data']['give-form-id'] );
223
	$price_id = isset( $payment_data['post_data']['give-price-id'] ) ? $payment_data['post_data']['give-price-id'] : '';
224
225
	// Collect payment data.
226
	$insert_payment_data = array(
227
		'price'           => $payment_data['price'],
228
		'give_form_title' => $payment_data['post_data']['give-form-title'],
229 31
		'give_form_id'    => $form_id,
230
		'give_price_id'   => $price_id,
231 31
		'date'            => $payment_data['date'],
232 31
		'user_email'      => $payment_data['user_email'],
233 31
		'purchase_key'    => $payment_data['purchase_key'],
234 31
		'currency'        => give_get_currency(),
235 31
		'user_info'       => $payment_data['user_info'],
236
		'status'          => 'pending',
237
		'gateway'         => 'paypal',
238 31
	);
239 31
240
	/**
241 31
	 * Filter the payment params.
242
	 *
243 31
	 * @since 1.8
244 16
	 *
245 16
	 * @param array $insert_payment_data
246
	 */
247 31
	$insert_payment_data = apply_filters( 'give_create_payment', $insert_payment_data );
248
249
	// Record the pending payment.
250 14
	return give_insert_payment( $insert_payment_data );
251
}
252 14
253
/**
254 14
 * Updates a payment status.
255
 *
256
 * @since  1.0
257
 *
258
 * @param  int    $payment_id Payment ID.
259
 * @param  string $new_status New Payment Status. Default is 'publish'.
260
 *
261 14
 * @return bool
262
 */
263 31
function give_update_payment_status( $payment_id, $new_status = 'publish' ) {
264
265 31
	$payment         = new Give_Payment( $payment_id );
266
	$payment->status = $new_status;
267
	$updated         = $payment->save();
268 1
269
	return $updated;
270 1
}
271
272
273 31
/**
274
 * Deletes a Donation
275
 *
276 31
 * @since  1.0
277 31
 * @global      $give_logs
278 31
 *
279
 * @param  int  $payment_id   Payment ID (default: 0).
280
 * @param  bool $update_donor If we should update the donor stats (default:true).
281 31
 *
282
 * @return void
283 31
 */
284 31
function give_delete_donation( $payment_id = 0, $update_donor = true ) {
285 31
	global $give_logs;
286
287 31
	$payment  = new Give_Payment( $payment_id );
288 31
	$amount   = give_get_payment_amount( $payment_id );
289
	$status   = $payment->post_status;
290
	$donor_id = give_get_payment_donor_id( $payment_id );
291
	$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...
292
293
	// Only undo donations that aren't these statuses.
294
	$dont_undo_statuses = apply_filters( 'give_undo_donation_statuses', array(
295
		'pending',
296
		'cancelled',
297
	) );
298
299
	if ( ! in_array( $status, $dont_undo_statuses ) ) {
300
		give_undo_donation( $payment_id );
301
	}
302 20
303
	if ( $status == 'publish' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
304
305
		// Only decrease earnings if they haven't already been decreased (or were never increased for this payment).
306
		give_decrease_total_earnings( $amount );
307 20
308
		// @todo: Refresh only range related stat cache
309 20
		give_delete_donation_stats();
310 20
311
		if ( $donor->id && $update_donor ) {
312 18
313 18
			// Decrement the stats for the donor.
314
			$donor->decrease_donation_count();
315 20
			$donor->decrease_value( $amount );
316 20
317
		}
318 18
	}
319 18
320
	/**
321 20
	 * Fires before deleting payment.
322
	 *
323
	 * @since 1.0
324
	 *
325
	 * @param int $payment_id Payment ID.
326
	 */
327
	do_action( 'give_payment_delete', $payment_id );
328
329
	if ( $donor->id && $update_donor ) {
330
331
		// Remove the payment ID from the donor.
332
		$donor->remove_payment( $payment_id );
333
334
	}
335
336
	// Remove the payment.
337 6
	wp_delete_post( $payment_id, true );
338
339
	// Remove related sale log entries.
340 6
	$give_logs->delete_logs( null, 'sale', array(
341 6
		array(
342 6
			'key'   => '_give_log_payment_id',
343 6
			'value' => $payment_id,
344 6
		),
345 6
	) );
346
347 6
	/**
348
	 * Fires after payment deleted.
349 6
	 *
350 6
	 * @since 1.0
351 6
	 *
352
	 * @param int $payment_id Payment ID.
353
	 */
354 6
	do_action( 'give_payment_deleted', $payment_id );
355
}
356
357
/**
358
 * Undo Donation
359
 *
360
 * Undoes a donation, including the decrease of donations and earning stats.
361
 * Used for when refunding or deleting a donation.
362
 *
363
 * @since  1.0
364
 *
365
 * @param  int $payment_id Payment ID.
366
 *
367
 * @return void
368
 */
369
function give_undo_donation( $payment_id ) {
370
371
	$payment = new Give_Payment( $payment_id );
372
373 6
	$maybe_decrease_earnings = apply_filters( 'give_decrease_earnings_on_undo', true, $payment, $payment->form_id );
374
	if ( true === $maybe_decrease_earnings ) {
375
		// Decrease earnings.
376
		give_decrease_earnings( $payment->form_id, $payment->total );
377
	}
378
379
	$maybe_decrease_donations = apply_filters( 'give_decrease_donations_on_undo', true, $payment, $payment->form_id );
380
	if ( true === $maybe_decrease_donations ) {
381
		// Decrease donation count.
382
		give_decrease_donation_count( $payment->form_id );
383
	}
384
385
}
386
387
388
/**
389
 * Count Payments
390
 *
391
 * Returns the total number of payments recorded.
392
 *
393
 * @since  1.0
394
 *
395
 * @param  array $args Arguments passed.
396
 *
397
 * @return object $stats Contains the number of payments per payment status.
398
 */
399
function give_count_payments( $args = array() ) {
400
401
	global $wpdb;
402
403
	$defaults = array(
404
		'user'       => null,
405
		's'          => null,
406
		'start-date' => null,
407
		'end-date'   => null,
408
		'form_id'    => null,
409
	);
410
411
	$args = wp_parse_args( $args, $defaults );
412
413
	$select = 'SELECT p.post_status,count( * ) AS num_posts';
414
	$join   = '';
415
	$where  = "WHERE p.post_type = 'give_payment'";
416
417
	// Count payments for a specific user.
418
	if ( ! empty( $args['user'] ) ) {
419
420 6
		if ( is_email( $args['user'] ) ) {
421
			$field = 'email';
422
		} elseif ( is_numeric( $args['user'] ) ) {
423
			$field = 'id';
424
		} else {
425
			$field = '';
426 6
		}
427
428
		$join = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
429
430
		if ( ! empty( $field ) ) {
431
			$where .= "
432
				AND m.meta_key = '_give_payment_user_{$field}'
433
				AND m.meta_value = '{$args['user']}'";
434
		}
435
	} elseif ( ! empty( $args['donor'] ) ) {
436
437
		$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
438
		$where .= "
439
			AND m.meta_key = '_give_payment_customer_id'
440
			AND m.meta_value = '{$args['donor']}'";
441
442
		// Count payments for a search.
443
	} elseif ( ! empty( $args['s'] ) ) {
444
445
		if ( is_email( $args['s'] ) || strlen( $args['s'] ) == 32 ) {
446
447
			if ( is_email( $args['s'] ) ) {
448 6
				$field = '_give_payment_user_email';
449
			} else {
450
				$field = '_give_payment_purchase_key';
451
			}
452
453
			$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
454
			$where .= $wpdb->prepare( '
455
                AND m.meta_key = %s
456
                AND m.meta_value = %s', $field, $args['s'] );
457
458
		} elseif ( '#' == substr( $args['s'], 0, 1 ) ) {
459
460
			$search = str_replace( '#:', '', $args['s'] );
461
			$search = str_replace( '#', '', $search );
462
463
			$select = 'SELECT p.post_status,count( * ) AS num_posts ';
464
			$join   = '';
465
			$where  = $wpdb->prepare( 'WHERE p.post_type=%s  AND p.ID = %d ', 'give_payment', $search );
466 6
467 6
		} elseif ( is_numeric( $args['s'] ) ) {
468
469
			$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
470 6
			$where .= $wpdb->prepare( "
471 6
				AND m.meta_key = '_give_payment_user_id'
472 6
				AND m.meta_value = %d", $args['s'] );
473
474 6
		} else {
475
			$search = $wpdb->esc_like( $args['s'] );
476 6
			$search = '%' . $search . '%';
477
478 6
			$where .= $wpdb->prepare( 'AND ((p.post_title LIKE %s) OR (p.post_content LIKE %s))', $search, $search );
479 6
		}// End if().
480
	}// End if().
481
482
	if ( ! empty( $args['form_id'] ) && is_numeric( $args['form_id'] ) ) {
483 6
484
		$join  = "LEFT JOIN $wpdb->postmeta m ON (p.ID = m.post_id)";
485 6
		$where .= $wpdb->prepare( '
486 6
                AND m.meta_key = %s
487 6
                AND m.meta_value = %s', '_give_payment_form_id', $args['form_id'] );
488 6
	}
489 6
490
	// Limit payments count by date.
491 6
	if ( ! empty( $args['start-date'] ) && false !== strpos( $args['start-date'], '/' ) ) {
492 6
493 6
		$date_parts = explode( '/', $args['start-date'] );
494
		$month      = ! empty( $date_parts[0] ) && is_numeric( $date_parts[0] ) ? $date_parts[0] : 0;
495 6
		$day        = ! empty( $date_parts[1] ) && is_numeric( $date_parts[1] ) ? $date_parts[1] : 0;
496
		$year       = ! empty( $date_parts[2] ) && is_numeric( $date_parts[2] ) ? $date_parts[2] : 0;
497 6
498
		$is_date = checkdate( $month, $day, $year );
499 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...
500
501 6
			$date  = new DateTime( $args['start-date'] );
502 6
			$where .= $wpdb->prepare( " AND p.post_date >= '%s'", $date->format( 'Y-m-d' ) );
503
504 6
		}
505 6
506
		// Fixes an issue with the payments list table counts when no end date is specified (with stats class).
507 6
		if ( empty( $args['end-date'] ) ) {
508
			$args['end-date'] = $args['start-date'];
509
		}
510
	}
511
512
	if ( ! empty( $args['end-date'] ) && false !== strpos( $args['end-date'], '/' ) ) {
513
514
		$date_parts = explode( '/', $args['end-date'] );
515
516
		$month = ! empty( $date_parts[0] ) ? $date_parts[0] : 0;
517
		$day   = ! empty( $date_parts[1] ) ? $date_parts[1] : 0;
518
		$year  = ! empty( $date_parts[2] ) ? $date_parts[2] : 0;
519
520
		$is_date = checkdate( $month, $day, $year );
521 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...
522
523
			$date  = new DateTime( $args['end-date'] );
524
			$where .= $wpdb->prepare( " AND p.post_date <= '%s'", $date->format( 'Y-m-d' ) );
525
526
		}
527
	}
528
529
	$where = apply_filters( 'give_count_payments_where', $where );
530
	$join  = apply_filters( 'give_count_payments_join', $join );
531
532
	$query = "$select
533
		FROM $wpdb->posts p
534
		$join
535
		$where
536
		GROUP BY p.post_status
537
	";
538
539
	$cache_key = md5( $query );
540
541
	$count = wp_cache_get( $cache_key, 'counts' );
542
	if ( false !== $count ) {
543
		return $count;
544
	}
545
546
	$count = $wpdb->get_results( $query, ARRAY_A );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
547
548
	$stats    = array();
549
	$statuses = get_post_stati();
550
	if ( isset( $statuses['private'] ) && empty( $args['s'] ) ) {
551
		unset( $statuses['private'] );
552
	}
553
554
	foreach ( $statuses as $state ) {
555
		$stats[ $state ] = 0;
556
	}
557
558
	foreach ( (array) $count as $row ) {
559
560
		if ( 'private' == $row['post_status'] && empty( $args['s'] ) ) {
561
			continue;
562
		}
563
564
		$stats[ $row['post_status'] ] = $row['num_posts'];
565
	}
566
567
	$stats = (object) $stats;
568
	wp_cache_set( $cache_key, $stats, 'counts' );
569
570
	return $stats;
571
}
572
573
574
/**
575
 * Check For Existing Payment
576
 *
577
 * @since  1.0
578
 *
579 52
 * @param  int $payment_id Payment ID
580 52
 *
581 52
 * @return bool $exists True if payment exists, false otherwise.
582 52
 */
583 52
function give_check_for_existing_payment( $payment_id ) {
584 52
	$exists  = false;
585 52
	$payment = new Give_Payment( $payment_id );
586 52
587 52
	if ( $payment_id === $payment->ID && 'publish' === $payment->status ) {
588
		$exists = true;
589 52
	}
590
591
	return $exists;
592
}
593
594
/**
595
 * Get Payment Status
596
 *
597
 * @since 1.0
598
 *
599
 * @param WP_Post|Give_Payment $payment      Payment object.
600
 * @param bool                 $return_label Whether to return the translated status label
601 52
 *                                           instead of status value. Default false.
602 52
 *
603
 * @return bool|mixed True if payment status exists, false otherwise.
604 52
 */
605
function give_get_payment_status( $payment, $return_label = false ) {
606
607
	if ( ! is_object( $payment ) || ! isset( $payment->post_status ) ) {
608
		return false;
609
	}
610
611
	$statuses = give_get_payment_statuses();
612
613
	if ( ! is_array( $statuses ) || empty( $statuses ) ) {
614
		return false;
615
	}
616
617
	// Get payment object if no already given.
618
	$payment = $payment instanceof Give_Payment ? $payment : new Give_Payment( $payment->ID );
619
620
	if ( array_key_exists( $payment->status, $statuses ) ) {
621
		if ( true === $return_label ) {
622
			// Return translated status label.
623
			return $statuses[ $payment->status ];
624
		} else {
625
			// Account that our 'publish' status is labeled 'Complete'
626
			$post_status = 'publish' == $payment->status ? 'Complete' : $payment->post_status;
627
628
			// Make sure we're matching cases, since they matter
629
			return array_search( strtolower( $post_status ), array_map( 'strtolower', $statuses ) );
630
		}
631
	}
632
633
	return false;
634
}
635
636
/**
637
 * Retrieves all available statuses for payments.
638
 *
639
 * @since  1.0
640
 *
641
 * @return array $payment_status All the available payment statuses.
642
 */
643
function give_get_payment_statuses() {
644
	$payment_statuses = array(
645
		'pending'     => __( 'Pending', 'give' ),
646
		'publish'     => __( 'Complete', 'give' ),
647
		'refunded'    => __( 'Refunded', 'give' ),
648
		'failed'      => __( 'Failed', 'give' ),
649
		'cancelled'   => __( 'Cancelled', 'give' ),
650
		'abandoned'   => __( 'Abandoned', 'give' ),
651
		'preapproval' => __( 'Pre-Approved', 'give' ),
652
		'processing'  => __( 'Processing', 'give' ),
653
		'revoked'     => __( 'Revoked', 'give' ),
654
	);
655
656
	return apply_filters( 'give_payment_statuses', $payment_statuses );
657
}
658
659
/**
660
 * Get Payment Status Keys
661
 *
662
 * Retrieves keys for all available statuses for payments
663
 *
664
 * @since  1.0
665
 *
666
 * @return array $payment_status All the available payment statuses.
667
 */
668
function give_get_payment_status_keys() {
669
	$statuses = array_keys( give_get_payment_statuses() );
670
	asort( $statuses );
671
672
	return array_values( $statuses );
673
}
674
675
/**
676
 * Get Earnings By Date
677
 *
678
 * @since  1.0
679
 *
680
 * @param  int $day       Day number. Default is null.
681
 * @param  int $month_num Month number. Default is null.
682
 * @param  int $year      Year number. Default is null.
683
 * @param  int $hour      Hour number. Default is null.
684
 *
685
 * @return int $earnings  Earnings
686
 */
687
function give_get_earnings_by_date( $day = null, $month_num, $year = null, $hour = null ) {
688
689
	// This is getting deprecated soon. Use Give_Payment_Stats with the get_earnings() method instead.
690
	global $wpdb;
691
692
	$args = array(
693
		'post_type'              => 'give_payment',
694
		'nopaging'               => true,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
695
		'year'                   => $year,
696
		'monthnum'               => $month_num,
697
		'post_status'            => array( 'publish' ),
698
		'fields'                 => 'ids',
699
		'update_post_term_cache' => false,
700
	);
701
	if ( ! empty( $day ) ) {
702
		$args['day'] = $day;
703
	}
704
705
	if ( ! empty( $hour ) ) {
706
		$args['hour'] = $hour;
707
	}
708
709
	$args = apply_filters( 'give_get_earnings_by_date_args', $args );
710
	$key  = Give_Cache::get_key( 'give_stats', $args );
711
712 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...
713
		$earnings = false;
714
	} else {
715
		$earnings = Give_Cache::get( $key );
716
	}
717
718
	if ( false === $earnings ) {
719
		$donations = get_posts( $args );
720
		$earnings  = 0;
721
		if ( $donations ) {
722
			$donations = implode( ',', $donations );
723
724
			$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...
725
726
		}
727
		// Cache the results for one hour.
728
		Give_Cache::set( $key, $earnings, HOUR_IN_SECONDS );
729
	}
730
731
	return round( $earnings, 2 );
732
}
733
734
/**
735
 * Get Donations (sales) By Date
736
 *
737
 * @since  1.0
738
 *
739
 * @param  int $day       Day number. Default is null.
740
 * @param  int $month_num Month number. Default is null.
741
 * @param  int $year      Year number. Default is null.
742
 * @param  int $hour      Hour number. Default is null.
743
 *
744
 * @return int $count     Sales
745
 */
746
function give_get_sales_by_date( $day = null, $month_num = null, $year = null, $hour = null ) {
747
748
	// This is getting deprecated soon. Use Give_Payment_Stats with the get_sales() method instead.
749
	$args = array(
750
		'post_type'              => 'give_payment',
751
		'nopaging'               => true,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
752
		'year'                   => $year,
753
		'fields'                 => 'ids',
754
		'post_status'            => array( 'publish' ),
755
		'update_post_meta_cache' => false,
756
		'update_post_term_cache' => false,
757
	);
758
759
	$show_free = apply_filters( 'give_sales_by_date_show_free', true, $args );
760
761
	if ( false === $show_free ) {
762
		$args['meta_query'] = array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
763
			array(
764
				'key'     => '_give_payment_total',
765
				'value'   => 0,
766
				'compare' => '>',
767
				'type'    => 'NUMERIC',
768
			),
769
		);
770 5
	}
771
772 5
	if ( ! empty( $month_num ) ) {
773
		$args['monthnum'] = $month_num;
774
	}
775
776
	if ( ! empty( $day ) ) {
777
		$args['day'] = $day;
778
	}
779
780
	if ( ! empty( $hour ) ) {
781
		$args['hour'] = $hour;
782
	}
783 42
784
	$args = apply_filters( 'give_get_sales_by_date_args', $args );
785
786 42
	$key = Give_Cache::get_key( 'give_stats', $args );
787
788 42 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...
789
		$count = false;
790 42
	} else {
791
		$count = Give_Cache::get( $key );
792 42
	}
793
794 42
	if ( false === $count ) {
795
		$donations = new WP_Query( $args );
796 42
		$count     = (int) $donations->post_count;
797 42
		// Cache the results for one hour.
798 42
		Give_Cache::set( $key, $count, HOUR_IN_SECONDS );
799 42
	}
800
801 42
	return $count;
802
}
803
804 42
/**
805 42
 * Checks whether a payment has been marked as complete.
806
 *
807
 * @since  1.0
808
 *
809
 * @param  int $payment_id Payment ID to check against.
810
 *
811
 * @return bool $ret True if complete, false otherwise.
812
 */
813 42
function give_is_payment_complete( $payment_id ) {
814 42
	$payment = new Give_Payment( $payment_id );
815 42
816
	$ret = false;
817 42
818
	if ( $payment->ID > 0 ) {
819
820
		if ( (int) $payment_id === (int) $payment->ID && 'publish' == $payment->status ) {
821
			$ret = true;
822 42
		}
823
	}
824
825 42
	return apply_filters( 'give_is_payment_complete', $ret, $payment_id, $payment->post_status );
826
}
827
828 42
/**
829 42
 * Get Total Donations.
830 42
 *
831
 * @since  1.0
832 42
 *
833
 * @return int $count Total number of donations.
834
 */
835
function give_get_total_donations() {
836 42
837
	$payments = give_count_payments();
838
839
	return $payments->publish;
840
}
841
842
/**
843
 * Get Total Earnings
844
 *
845
 * @since  1.0
846
 *
847
 * @param bool $recalculate Recalculate earnings forcefully.
848
 *
849 42
 * @return float $total Total earnings.
850 42
 */
851 42
function give_get_total_earnings( $recalculate = false ) {
852
853 42
	$total = get_option( 'give_earnings_total', 0 );
854
855
	// Calculate total earnings.
856
	if ( ! $total || $recalculate ) {
857
		global $wpdb;
858
859
		$total = (float) 0;
860
861
		$args = apply_filters( 'give_get_total_earnings_args', array(
862
			'offset' => 0,
863
			'number' => - 1,
864
			'status' => array( 'publish' ),
865
			'fields' => 'ids',
866 19
		) );
867 19
868 19
		$payments = give_get_payments( $args );
869
		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...
870
871 19
			/**
872
			 * If performing a donation, we need to skip the very last payment in the database,
873 19
			 * since it calls give_increase_total_earnings() on completion,
874
			 * which results in duplicated earnings for the very first donation.
875
			 */
876
			if ( did_action( 'give_update_payment_status' ) ) {
877
				array_pop( $payments );
878
			}
879
880
			if ( ! empty( $payments ) ) {
881
				$payments = implode( ',', $payments );
882
				$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...
883
			}
884
		}
885
886
		update_option( 'give_earnings_total', $total, 'no' );
887
	}
888 42
889
	if ( $total < 0 ) {
890 42
		$total = 0; // Don't ever show negative earnings.
891
	}
892
893
	return apply_filters( 'give_total_earnings', round( $total, give_currency_decimal_filter() ) );
894
}
895
896
/**
897
 * Increase the Total Earnings
898
 *
899
 * @since  1.0
900
 *
901
 * @param  int $amount   The amount you would like to increase the total earnings by.
902
 *                       Default is 0.
903
 *
904 18
 * @return float $total  Total earnings.
905
 */
906 18
function give_increase_total_earnings( $amount = 0 ) {
907
	$total = give_get_total_earnings();
908
	$total += $amount;
909
	update_option( 'give_earnings_total', $total );
910
911
	return $total;
912
}
913
914
/**
915
 * Decrease the Total Earnings
916
 *
917
 * @since 1.0
918
 *
919
 * @param int $amount The amount you would like to decrease the total earnings by.
920
 *
921
 * @return float $total Total earnings.
922
 */
923
function give_decrease_total_earnings( $amount = 0 ) {
924
	$total = give_get_total_earnings();
925
	$total -= $amount;
926
	if ( $total < 0 ) {
927
		$total = 0;
928
	}
929
	update_option( 'give_earnings_total', $total );
930
931
	return $total;
932
}
933
934
/**
935
 * Get Payment Meta for a specific Payment
936
 *
937
 * @since 1.0
938
 *
939
 * @param int    $payment_id Payment ID.
940
 * @param string $meta_key   The meta key to pull.
941
 * @param bool   $single     Pull single meta entry or as an object.
942
 *
943
 * @return mixed $meta Payment Meta.
944
 */
945
function give_get_payment_meta( $payment_id = 0, $meta_key = '_give_payment_meta', $single = true ) {
946
	$payment = new Give_Payment( $payment_id );
947
948
	return $payment->get_meta( $meta_key, $single );
949
}
950 42
951
/**
952 42
 * Update the meta for a payment
953
 *
954
 * @param  int    $payment_id Payment ID.
955
 * @param  string $meta_key   Meta key to update.
956
 * @param  string $meta_value Value to update to.
957
 * @param  string $prev_value Previous value.
958
 *
959
 * @return mixed Meta ID if successful, false if unsuccessful.
960
 */
961
function give_update_payment_meta( $payment_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) {
962
	$payment = new Give_Payment( $payment_id );
963
964
	return $payment->update_meta( $meta_key, $meta_value, $prev_value );
965
}
966
967
/**
968
 * Get the user_info Key from Payment Meta
969
 *
970
 * @since 1.0
971
 *
972
 * @param int $payment_id Payment ID.
973
 *
974
 * @return array $user_info User Info Meta Values.
975
 */
976
function give_get_payment_meta_user_info( $payment_id ) {
977
	$payment = new Give_Payment( $payment_id );
978
979
	return $payment->user_info;
980
}
981
982
/**
983
 * Get the donations Key from Payment Meta
984
 *
985
 * Retrieves the form_id from a (Previously titled give_get_payment_meta_donations)
986
 *
987
 * @since 1.0
988
 *
989
 * @param int $payment_id Payment ID.
990
 *
991
 * @return int $form_id Form ID.
992
 */
993
function give_get_payment_form_id( $payment_id ) {
994
	$payment = new Give_Payment( $payment_id );
995
996 31
	return $payment->form_id;
997
}
998 31
999
/**
1000
 * Get the user email associated with a payment
1001
 *
1002
 * @since 1.0
1003
 *
1004
 * @param int $payment_id Payment ID.
1005
 *
1006
 * @return string $email User email.
1007
 */
1008
function give_get_payment_user_email( $payment_id ) {
1009
	$payment = new Give_Payment( $payment_id );
1010
1011
	return $payment->email;
1012
}
1013
1014
/**
1015
 * Is the payment provided associated with a user account
1016
 *
1017
 * @since  1.3
1018
 *
1019
 * @param  int $payment_id The payment ID.
1020
 *
1021
 * @return bool $is_guest_payment If the payment is associated with a user (false) or not (true)
1022
 */
1023
function give_is_guest_payment( $payment_id ) {
1024
	$payment_user_id  = give_get_payment_user_id( $payment_id );
1025
	$is_guest_payment = ! empty( $payment_user_id ) && $payment_user_id > 0 ? false : true;
1026
1027
	return (bool) apply_filters( 'give_is_guest_payment', $is_guest_payment, $payment_id );
1028
}
1029
1030
/**
1031
 * Get the user ID associated with a payment
1032
 *
1033
 * @since 1.3
1034
 *
1035
 * @param int $payment_id Payment ID.
1036
 *
1037
 * @return int $user_id User ID.
1038
 */
1039
function give_get_payment_user_id( $payment_id ) {
1040
	$payment = new Give_Payment( $payment_id );
1041
1042
	return $payment->user_id;
1043
}
1044
1045
/**
1046
 * Get the donor ID associated with a payment.
1047
 *
1048
 * @since 1.0
1049
 *
1050
 * @param int $payment_id Payment ID.
1051
 *
1052
 * @return int $payment->customer_id Donor ID.
1053
 */
1054
function give_get_payment_donor_id( $payment_id ) {
1055
	$payment = new Give_Payment( $payment_id );
1056
1057
	return $payment->customer_id;
1058
}
1059
1060
/**
1061
 * Get the IP address used to make a donation
1062
 *
1063
 * @since 1.0
1064
 *
1065
 * @param int $payment_id Payment ID.
1066
 *
1067
 * @return string $ip User IP.
1068
 */
1069
function give_get_payment_user_ip( $payment_id ) {
1070
	$payment = new Give_Payment( $payment_id );
1071
1072
	return $payment->ip;
1073
}
1074
1075
/**
1076
 * Get the date a payment was completed
1077
 *
1078
 * @since 1.0
1079
 *
1080
 * @param int $payment_id Payment ID.
1081
 *
1082
 * @return string $date The date the payment was completed.
1083
 */
1084
function give_get_payment_completed_date( $payment_id = 0 ) {
1085
	$payment = new Give_Payment( $payment_id );
1086 52
1087
	return $payment->completed_date;
1088 52
}
1089
1090
/**
1091
 * Get the gateway associated with a payment
1092
 *
1093
 * @since 1.0
1094
 *
1095
 * @param int $payment_id Payment ID.
1096
 *
1097
 * @return string $gateway Gateway.
1098
 */
1099
function give_get_payment_gateway( $payment_id ) {
1100
	$payment = new Give_Payment( $payment_id );
1101
1102
	return $payment->gateway;
1103
}
1104
1105
/**
1106
 * Get the currency code a payment was made in
1107
 *
1108
 * @since 1.0
1109
 *
1110
 * @param int $payment_id Payment ID.
1111
 *
1112
 * @return string $currency The currency code.
1113
 */
1114
function give_get_payment_currency_code( $payment_id = 0 ) {
1115
	$payment = new Give_Payment( $payment_id );
1116
1117
	return $payment->currency;
1118
}
1119 20
1120
/**
1121
 * Get the currency name a payment was made in
1122
 *
1123 20
 * @since 1.0
1124
 *
1125
 * @param int $payment_id Payment ID.
1126
 *
1127 20
 * @return string $currency The currency name.
1128 20
 */
1129 20
function give_get_payment_currency( $payment_id = 0 ) {
1130
	$currency = give_get_payment_currency_code( $payment_id );
1131 20
1132
	return apply_filters( 'give_payment_currency', give_get_currency_name( $currency ), $payment_id );
1133 20
}
1134
1135
/**
1136
 * Get the key for a donation
1137
 *
1138
 * @since 1.0
1139
 *
1140
 * @param int $payment_id Payment ID.
1141
 *
1142
 * @return string $key Donation key.
1143
 */
1144
function give_get_payment_key( $payment_id = 0 ) {
1145
	$payment = new Give_Payment( $payment_id );
1146 20
1147
	return $payment->key;
1148
}
1149
1150 20
/**
1151 20
 * Get the payment order number
1152 20
 *
1153
 * This will return the payment ID if sequential order numbers are not enabled or the order number does not exist
1154 20
 *
1155
 * @since 1.0
1156 2
 *
1157
 * @param int $payment_id Payment ID.
1158
 *
1159
 * @return string $number Payment order number.
1160
 */
1161
function give_get_payment_number( $payment_id = 0 ) {
1162
	$payment = new Give_Payment( $payment_id );
1163 2
1164
	return $payment->number;
1165
}
1166 20
1167 20
/**
1168 20
 * Formats the payment number with the prefix and postfix
1169 20
 *
1170 20
 * @since  1.3
1171
 *
1172 20
 * @param  int $number The payment number to format.
1173 20
 *
1174
 * @return string      The formatted payment number.
1175 20
 */
1176
function give_format_payment_number( $number ) {
1177
1178
	if ( ! give_get_option( 'enable_sequential' ) ) {
1179
		return $number;
1180
	}
1181 20
1182
	if ( ! is_numeric( $number ) ) {
1183
		return $number;
1184
	}
1185
1186
	$prefix  = give_get_option( 'sequential_prefix' );
1187 20
	$number  = absint( $number );
1188 20
	$postfix = give_get_option( 'sequential_postfix' );
1189
1190
	$formatted_number = $prefix . $number . $postfix;
1191
1192
	return apply_filters( 'give_format_payment_number', $formatted_number, $prefix, $number, $postfix );
1193 20
}
1194
1195 20
/**
1196 2
 * Gets the next available order number
1197 2
 *
1198
 * This is used when inserting a new payment
1199 20
 *
1200
 * @since 1.0
1201
 * @return string $number The next available payment number.
1202
 */
1203
function give_get_next_payment_number() {
1204
1205
	if ( ! give_get_option( 'enable_sequential' ) ) {
1206
		return false;
1207
	}
1208
1209
	$number           = get_option( 'give_last_payment_number' );
1210
	$start            = give_get_option( 'sequential_start', 1 );
1211
	$increment_number = true;
1212
1213
	if ( false !== $number ) {
1214
1215
		if ( empty( $number ) ) {
1216
1217
			$number           = $start;
1218
			$increment_number = false;
1219
1220
		}
1221
	} else {
1222
1223
		// This case handles the first addition of the new option, as well as if it get's deleted for any reason.
1224
		$payments     = new Give_Payments_Query( array(
1225
			'number'  => 1,
1226
			'order'   => 'DESC',
1227
			'orderby' => 'ID',
1228
			'output'  => 'posts',
1229
			'fields'  => 'ids',
1230
		) );
1231
		$last_payment = $payments->get_payments();
1232
1233
		if ( ! empty( $last_payment ) ) {
1234
1235
			$number = give_get_payment_number( $last_payment[0] );
1236
1237
		}
1238
1239
		if ( ! empty( $number ) && $number !== (int) $last_payment[0] ) {
1240
1241
			$number = give_remove_payment_prefix_postfix( $number );
1242
1243
		} else {
1244
1245
			$number           = $start;
1246
			$increment_number = false;
1247
		}
1248
	}// End if().
1249
1250
	$increment_number = apply_filters( 'give_increment_payment_number', $increment_number, $number );
1251
1252
	if ( $increment_number ) {
1253
		$number ++;
1254
	}
1255
1256
	return apply_filters( 'give_get_next_payment_number', $number );
1257
}
1258
1259
/**
1260
 * Given a given a number, remove the pre/postfix
1261
 *
1262
 * @since  1.3
1263 52
 *
1264
 * @param  string $number The formatted Current Number to increment.
1265 52
 *
1266
 * @return string The new Payment number without prefix and postfix.
1267
 */
1268
function give_remove_payment_prefix_postfix( $number ) {
1269
1270
	$prefix  = give_get_option( 'sequential_prefix' );
1271
	$postfix = give_get_option( 'sequential_postfix' );
1272
1273
	// Remove prefix.
1274
	$number = preg_replace( '/' . $prefix . '/', '', $number, 1 );
1275
1276
	// Remove the postfix.
1277
	$length      = strlen( $number );
1278
	$postfix_pos = strrpos( $number, $postfix );
1279
	if ( false !== $postfix_pos ) {
1280
		$number = substr_replace( $number, '', $postfix_pos, $length );
1281
	}
1282
1283
	// Ensure it's a whole number.
1284
	$number = intval( $number );
1285
1286
	return apply_filters( 'give_remove_payment_prefix_postfix', $number, $prefix, $postfix );
1287
1288
}
1289
1290
1291
/**
1292
 * Get Payment Amount
1293
 *
1294
 * Get the fully formatted payment amount. The payment amount is retrieved using give_get_payment_amount() and is then
1295
 * sent through give_currency_filter() and  give_format_amount() to format the amount correctly.
1296
 *
1297
 * @since       1.0
1298
 *
1299
 * @param int $payment_id Payment ID.
1300
 *
1301
 * @return string $amount Fully formatted payment amount.
1302
 */
1303
function give_payment_amount( $payment_id = 0 ) {
1304
	$amount = give_get_payment_amount( $payment_id );
1305
1306
	return give_currency_filter( give_format_amount( $amount, array( 'sanitize' => false ) ), give_get_payment_currency_code( $payment_id ) );
1307
}
1308
1309
/**
1310
 * Get the amount associated with a payment
1311
 *
1312
 * @access public
1313
 * @since  1.0
1314
 *
1315
 * @param int $payment_id Payment ID.
1316
 *
1317
 * @return mixed
1318
 */
1319
function give_get_payment_amount( $payment_id ) {
1320
1321
	$payment = new Give_Payment( $payment_id );
1322
1323
	return apply_filters( 'give_payment_amount', floatval( $payment->total ), $payment_id );
1324
}
1325
1326
/**
1327
 * Payment Subtotal
1328
 *
1329
 * Retrieves subtotal for payment and then returns a full formatted amount. This
1330
 * function essentially calls give_get_payment_subtotal()
1331
 *
1332
 * @since 1.5
1333
 *
1334
 * @param int $payment_id Payment ID.
1335
 *
1336
 * @see   give_get_payment_subtotal()
1337
 *
1338
 * @return array Fully formatted payment subtotal.
1339
 */
1340
function give_payment_subtotal( $payment_id = 0 ) {
1341
	$subtotal = give_get_payment_subtotal( $payment_id );
1342
1343
	return give_currency_filter( give_format_amount( $subtotal , array( 'sanitize' => false ) ), give_get_payment_currency_code( $payment_id ) );
1344
}
1345
1346
/**
1347 18
 * Get Payment Subtotal
1348
 *
1349
 * Retrieves subtotal for payment and then returns a non formatted amount.
1350
 *
1351 18
 * @since 1.5
1352
 *
1353 18
 * @param int $payment_id Payment ID.
1354
 *
1355
 * @return float $subtotal Subtotal for payment (non formatted).
1356
 */
1357
function give_get_payment_subtotal( $payment_id = 0 ) {
1358
	$payment = new Give_Payment( $payment_id );
1359
1360
	return $payment->subtotal;
1361
}
1362
1363
/**
1364
 * Retrieves the donation ID
1365
 *
1366
 * @since  1.0
1367
 *
1368
 * @param int $payment_id Payment ID.
1369
 *
1370
 * @return string The donation ID.
1371
 */
1372
function give_get_payment_transaction_id( $payment_id = 0 ) {
1373
	$payment = new Give_Payment( $payment_id );
1374
1375
	return $payment->transaction_id;
1376
}
1377
1378
/**
1379
 * Sets a Transaction ID in post meta for the given Payment ID.
1380
 *
1381
 * @since  1.0
1382
 *
1383
 * @param int    $payment_id     Payment ID.
1384
 * @param string $transaction_id The transaction ID from the gateway.
1385
 *
1386
 * @return bool|mixed
1387
 */
1388
function give_set_payment_transaction_id( $payment_id = 0, $transaction_id = '' ) {
1389
1390
	if ( empty( $payment_id ) || empty( $transaction_id ) ) {
1391
		return false;
1392
	}
1393
1394
	$transaction_id = apply_filters( 'give_set_payment_transaction_id', $transaction_id, $payment_id );
1395
1396
	return give_update_payment_meta( $payment_id, '_give_payment_transaction_id', $transaction_id );
1397
}
1398
1399
/**
1400
 * Retrieve the donation ID based on the key
1401
 *
1402
 * @since 1.0
1403
 * @global object $wpdb Used to query the database using the WordPress Database API.
1404
 *
1405
 * @param string $key  the key to search for.
1406
 *
1407
 * @return int $purchase Donation ID.
1408
 */
1409 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...
1410
	global $wpdb;
1411
1412
	$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...
1413
1414
	if ( $purchase != null ) {
1415
		return $purchase;
1416
	}
1417
1418
	return 0;
1419
}
1420
1421
1422
/**
1423
 * Retrieve the donation ID based on the transaction ID
1424
 *
1425
 * @since 1.3
1426
 * @global object $wpdb Used to query the database using the WordPress Database API.
1427
 *
1428
 * @param string $key  The transaction ID to search for.
1429
 *
1430
 * @return int $purchase Donation ID.
1431
 */
1432 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...
1433
	global $wpdb;
1434
1435
	$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...
1436
1437
	if ( $purchase != null ) {
1438
		return $purchase;
1439
	}
1440
1441
	return 0;
1442 52
}
1443
1444
/**
1445
 * Retrieve all notes attached to a donation
1446 52
 *
1447
 * @since 1.0
1448 52
 *
1449 52
 * @param int    $payment_id The donation ID to retrieve notes for.
1450 52
 * @param string $search     Search for notes that contain a search term.
1451 52
 *
1452 52
 * @return array $notes Donation Notes
1453 52
 */
1454 52
function give_get_payment_notes( $payment_id = 0, $search = '' ) {
1455 52
1456 52
	if ( empty( $payment_id ) && empty( $search ) ) {
1457 52
		return false;
1458 52
	}
1459 52
1460
	remove_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1461
	remove_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10 );
1462 52
1463
	$notes = get_comments( array(
1464 52
		'post_id' => $payment_id,
1465
		'order' => 'ASC',
1466 52
		'search' => $search,
1467
	) );
1468
1469
	add_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1470
	add_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10, 2 );
1471
1472
	return $notes;
1473
}
1474
1475
1476
/**
1477
 * Add a note to a payment
1478
 *
1479
 * @since 1.0
1480
 *
1481
 * @param int    $payment_id The payment ID to store a note for.
1482
 * @param string $note       The note to store.
1483
 *
1484
 * @return int The new note ID
1485
 */
1486
function give_insert_payment_note( $payment_id = 0, $note = '' ) {
1487
	if ( empty( $payment_id ) ) {
1488
		return false;
1489
	}
1490
1491
	/**
1492
	 * Fires before inserting payment note.
1493
	 *
1494
	 * @since 1.0
1495
	 *
1496
	 * @param int    $payment_id Payment ID.
1497
	 * @param string $note       The note.
1498
	 */
1499
	do_action( 'give_pre_insert_payment_note', $payment_id, $note );
1500
1501
	$note_id = wp_insert_comment( wp_filter_comment( array(
1502
		'comment_post_ID'      => $payment_id,
1503
		'comment_content'      => $note,
1504
		'user_id'              => is_admin() ? get_current_user_id() : 0,
1505
		'comment_date'         => current_time( 'mysql' ),
1506
		'comment_date_gmt'     => current_time( 'mysql', 1 ),
1507
		'comment_approved'     => 1,
1508
		'comment_parent'       => 0,
1509
		'comment_author'       => '',
1510
		'comment_author_IP'    => '',
1511
		'comment_author_url'   => '',
1512
		'comment_author_email' => '',
1513
		'comment_type'         => 'give_payment_note',
1514
1515
	) ) );
1516
1517
	/**
1518
	 * Fires after payment note inserted.
1519
	 *
1520
	 * @since 1.0
1521
	 *
1522
	 * @param int    $note_id    Note ID.
1523
	 * @param int    $payment_id Payment ID.
1524
	 * @param string $note       The note.
1525
	 */
1526
	do_action( 'give_insert_payment_note', $note_id, $payment_id, $note );
1527
1528
	return $note_id;
1529
}
1530
1531
/**
1532
 * Deletes a payment note
1533
 *
1534
 * @since 1.0
1535
 *
1536
 * @param int $comment_id The comment ID to delete.
1537
 * @param int $payment_id The payment ID the note is connected to.
1538
 *
1539
 * @return bool True on success, false otherwise.
1540
 */
1541
function give_delete_payment_note( $comment_id = 0, $payment_id = 0 ) {
1542
	if ( empty( $comment_id ) ) {
1543
		return false;
1544
	}
1545
1546
	/**
1547
	 * Fires before deleting donation note.
1548
	 *
1549
	 * @since 1.0
1550
	 *
1551
	 * @param int $comment_id Note ID.
1552
	 * @param int $payment_id Payment ID.
1553
	 */
1554
	do_action( 'give_pre_delete_payment_note', $comment_id, $payment_id );
1555
1556
	$ret = wp_delete_comment( $comment_id, true );
1557
1558
	/**
1559
	 * Fires after donation note deleted.
1560
	 *
1561
	 * @since 1.0
1562
	 *
1563
	 * @param int $comment_id Note ID.
1564
	 * @param int $payment_id Payment ID.
1565
	 */
1566
	do_action( 'give_post_delete_payment_note', $comment_id, $payment_id );
1567
1568
	return $ret;
1569
}
1570
1571
/**
1572
 * Gets the payment note HTML
1573
 *
1574
 * @since 1.0
1575
 *
1576
 * @param object|int $note       The comment object or ID.
1577
 * @param int        $payment_id The payment ID the note is connected to.
1578
 *
1579
 * @return string
1580
 */
1581
function give_get_payment_note_html( $note, $payment_id = 0 ) {
1582
1583
	if ( is_numeric( $note ) ) {
1584
		$note = get_comment( $note );
1585
	}
1586
1587
	if ( ! empty( $note->user_id ) ) {
1588
		$user = get_userdata( $note->user_id );
1589
		$user = $user->display_name;
1590
	} else {
1591
		$user = esc_html__( 'System', 'give' );
1592
	}
1593
1594
	$date_format = give_date_format() . ', ' . get_option( 'time_format' );
1595
1596
	$delete_note_url = wp_nonce_url( add_query_arg( array(
1597
		'give-action' => 'delete_payment_note',
1598
		'note_id'     => $note->comment_ID,
1599
		'payment_id'  => $payment_id,
1600
	) ), 'give_delete_payment_note_' . $note->comment_ID );
1601
1602
	$note_html = '<div class="give-payment-note" id="give-payment-note-' . $note->comment_ID . '">';
1603
	$note_html .= '<p>';
1604
	$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/>';
1605
	$note_html .= $note->comment_content;
1606
	$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>';
1607
	$note_html .= '</p>';
1608
	$note_html .= '</div>';
1609
1610
	return $note_html;
1611
1612
}
1613
1614
/**
1615
 * Exclude notes (comments) on give_payment post type from showing in Recent
1616
 * Comments widgets
1617
 *
1618
 * @since 1.0
1619
 *
1620
 * @param object $query WordPress Comment Query Object.
1621
 *
1622
 * @return void
1623
 */
1624
function give_hide_payment_notes( $query ) {
1625
	if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '>=' ) ) {
1626
		$types = isset( $query->query_vars['type__not_in'] ) ? $query->query_vars['type__not_in'] : array();
1627
		if ( ! is_array( $types ) ) {
1628
			$types = array( $types );
1629
		}
1630
		$types[]                           = 'give_payment_note';
1631
		$query->query_vars['type__not_in'] = $types;
1632
	}
1633
}
1634
1635
add_action( 'pre_get_comments', 'give_hide_payment_notes', 10 );
1636
1637
/**
1638
 * Exclude notes (comments) on give_payment post type from showing in Recent Comments widgets
1639
 *
1640
 * @since 1.0
1641
 *
1642
 * @param array  $clauses          Comment clauses for comment query.
1643
 * @param object $wp_comment_query WordPress Comment Query Object.
1644
 *
1645
 * @return array $clauses Updated comment clauses.
1646
 */
1647
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...
1648
	if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.1', '<' ) ) {
1649
		$clauses['where'] .= ' AND comment_type != "give_payment_note"';
1650
	}
1651
1652
	return $clauses;
1653
}
1654
1655
add_filter( 'comments_clauses', 'give_hide_payment_notes_pre_41', 10, 2 );
1656
1657
1658
/**
1659
 * Exclude notes (comments) on give_payment post type from showing in comment feeds
1660
 *
1661
 * @since 1.0
1662
 *
1663
 * @param string $where
1664
 * @param object $wp_comment_query WordPress Comment Query Object.
1665
 *
1666
 * @return string $where
1667
 */
1668
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...
1669
	global $wpdb;
1670
1671
	$where .= $wpdb->prepare( ' AND comment_type != %s', 'give_payment_note' );
1672
1673
	return $where;
1674
}
1675
1676
add_filter( 'comment_feed_where', 'give_hide_payment_notes_from_feeds', 10, 2 );
1677
1678
1679
/**
1680
 * Remove Give Comments from the wp_count_comments function
1681
 *
1682
 * @access public
1683
 * @since  1.0
1684
 *
1685
 * @param array $stats   (empty from core filter).
1686
 * @param int   $post_id Post ID.
1687
 *
1688
 * @return array Array of comment counts.
1689
 */
1690
function give_remove_payment_notes_in_comment_counts( $stats, $post_id ) {
1691
	global $wpdb, $pagenow;
1692
1693
	if ( 'index.php' != $pagenow ) {
1694
		return $stats;
1695
	}
1696
1697
	$post_id = (int) $post_id;
1698
1699
	if ( apply_filters( 'give_count_payment_notes_in_comments', false ) ) {
1700
		return $stats;
1701
	}
1702
1703
	$stats = wp_cache_get( "comments-{$post_id}", 'counts' );
1704
1705
	if ( false !== $stats ) {
1706
		return $stats;
1707
	}
1708 42
1709 42
	$where = 'WHERE comment_type != "give_payment_note"';
1710 42
1711
	if ( $post_id > 0 ) {
1712 42
		$where .= $wpdb->prepare( ' AND comment_post_ID = %d', $post_id );
1713
	}
1714
1715
	$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...
1716 42
1717
	$total    = 0;
1718 34
	$approved = array(
1719 34
		'0'            => 'moderated',
0 ignored issues
show
introduced by
Detected usage of 0, possible slow query.
Loading history...
1720 34
		'1'            => 'approved',
1721 34
		'spam'         => 'spam',
1722
		'trash'        => 'trash',
1723 34
		'post-trashed' => 'post-trashed',
1724
	);
1725
	foreach ( (array) $count as $row ) {
1726
		// Don't count post-trashed toward totals.
1727
		if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] ) {
1728
			$total += $row['num_comments'];
1729 34
		}
1730
		if ( isset( $approved[ $row['comment_approved'] ] ) ) {
1731
			$stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments'];
1732 34
		}
1733
	}
1734 34
1735
	$stats['total_comments'] = $total;
1736 42
	foreach ( $approved as $key ) {
1737
		if ( empty( $stats[ $key ] ) ) {
1738
			$stats[ $key ] = 0;
1739
		}
1740
	}
1741
1742
	$stats = (object) $stats;
1743
	wp_cache_set( "comments-{$post_id}", $stats, 'counts' );
1744
1745
	return $stats;
1746
}
1747
1748
add_filter( 'wp_count_comments', 'give_remove_payment_notes_in_comment_counts', 10, 2 );
1749
1750
1751
/**
1752 52
 * Filter where older than one week
1753
 *
1754 52
 * @access public
1755
 * @since  1.0
1756 32
 *
1757
 * @param string $where Where clause.
1758 32
 *
1759
 * @return string $where Modified where clause.
1760 32
 */
1761
function give_filter_where_older_than_week( $where = '' ) {
1762
	// Payments older than one week.
1763 32
	$start = date( 'Y-m-d', strtotime( '-7 days' ) );
1764
	$where .= " AND post_date <= '{$start}'";
1765 32
1766
	return $where;
1767 32
}
1768
1769 32
1770
/**
1771 32
 * Get Payment Form ID.
1772
 *
1773 52
 * Retrieves the form title and appends the level name if present.
1774
 *
1775
 * @since 1.5
1776
 *
1777
 * @param array  $payment_meta Payment meta data.
1778
 * @param bool   $only_level   If set to true will only return the level name if multi-level enabled.
1779
 * @param string $separator    The separator between the .
1780
 *
1781
 * @return string $form_title Returns the full title if $only_level is false, otherwise returns the levels title.
1782
 */
1783
function give_get_payment_form_title( $payment_meta, $only_level = false, $separator = '' ) {
1784
1785
	$form_id    = isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : 0;
1786
	$price_id   = isset( $payment_meta['price_id'] ) ? $payment_meta['price_id'] : null;
1787
	$form_title = isset( $payment_meta['form_title'] ) ? $payment_meta['form_title'] : '';
1788
1789
	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...
1790
		$form_title = '';
1791
	}
1792
1793
	// If multi-level, append to the form title.
1794
	if ( give_has_variable_prices( $form_id ) ) {
1795
1796
		// Only add separator if there is a form title.
1797
		if ( ! empty( $form_title ) ) {
1798
			$form_title .= ' ' . $separator . ' ';
1799
		}
1800
1801
		$form_title .= '<span class="donation-level-text-wrap">';
1802
1803
		if ( $price_id == 'custom' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
1804
			$custom_amount_text = give_get_meta( $form_id, '_give_custom_amount_text', true );
1805
			$form_title         .= ! empty( $custom_amount_text ) ? $custom_amount_text : __( 'Custom Amount', 'give' );
1806
		} else {
1807
			$form_title .= give_get_price_option_name( $form_id, $price_id );
1808
		}
1809
1810
		$form_title .= '</span>';
1811
1812
	}
1813
1814
	return apply_filters( 'give_get_payment_form_title', $form_title, $payment_meta );
1815
1816
}
1817
1818
/**
1819
 * Get Price ID
1820
 *
1821
 * Retrieves the Price ID when provided a proper form ID and price (donation) total
1822
 *
1823
 * @param int    $form_id Form ID.
1824
 * @param string $price   Price ID.
1825
 *
1826
 * @return string $price_id
1827
 */
1828
function give_get_price_id( $form_id, $price ) {
1829
1830
	$price_id = 0;
1831
1832
	if ( give_has_variable_prices( $form_id ) ) {
1833
1834
		$levels = maybe_unserialize( give_get_meta( $form_id, '_give_donation_levels', true ) );
1835
1836
		foreach ( $levels as $level ) {
1837
1838
			$level_amount = (float) give_maybe_sanitize_amount( $level['_give_amount'] );
1839
1840
			// Check that this indeed the recurring price.
1841
			if ( $level_amount == $price ) {
1842
1843
				$price_id = $level['_give_id']['level_id'];
1844
1845
			}
1846
		}
1847
	}
1848
1849
	return $price_id;
1850
1851
}
1852
1853
/**
1854
 * Get/Print give form dropdown html
1855
 *
1856
 * This function is wrapper to public method forms_dropdown of Give_HTML_Elements class to get/print form dropdown html.
1857
 * Give_HTML_Elements is defined in includes/class-give-html-elements.php.
1858
 *
1859
 * @since 1.6
1860
 *
1861
 * @param array $args Arguments for form dropdown.
1862
 * @param bool  $echo This parameter decides if print form dropdown html output or not.
1863
 *
1864
 * @return string
1865
 */
1866
function give_get_form_dropdown( $args = array(), $echo = false ) {
1867
	$form_dropdown_html = Give()->html->forms_dropdown( $args );
1868
1869
	if ( ! $echo ) {
1870
		return $form_dropdown_html;
1871
	}
1872
1873
	echo $form_dropdown_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_dropdown_html'
Loading history...
1874
}
1875
1876
/**
1877
 * Get/Print give form variable price dropdown html
1878
 *
1879
 * @since 1.6
1880
 *
1881
 * @param array $args Arguments for form dropdown.
1882
 * @param bool  $echo This parameter decide if print form dropdown html output or not.
1883
 *
1884
 * @return string|bool
1885
 */
1886
function give_get_form_variable_price_dropdown( $args = array(), $echo = false ) {
1887
1888
	// Check for give form id.
1889
	if ( empty( $args['id'] ) ) {
1890
		return false;
1891
	}
1892
1893
	$form = new Give_Donate_Form( $args['id'] );
1894
1895
	// Check if form has variable prices or not.
1896
	if ( ! $form->ID || ! $form->has_variable_prices() ) {
1897
		return false;
1898
	}
1899
1900
	$variable_prices        = $form->get_prices();
1901
	$variable_price_options = array();
1902
1903
	// Check if multi donation form support custom donation or not.
1904
	if ( $form->is_custom_price_mode() ) {
1905
		$variable_price_options['custom'] = _x( 'Custom', 'custom donation dropdown item', 'give' );
1906
	}
1907
1908
	// Get variable price and ID from variable price array.
1909
	foreach ( $variable_prices as $variable_price ) {
1910
		$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 ) ) );
1911
	}
1912
1913
	// Update options.
1914
	$args = array_merge( $args, array(
1915
		'options' => $variable_price_options,
1916
	) );
1917
1918
	// Generate select html.
1919
	$form_dropdown_html = Give()->html->select( $args );
1920
1921
	if ( ! $echo ) {
1922
		return $form_dropdown_html;
1923
	}
1924
1925
	echo $form_dropdown_html;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form_dropdown_html'
Loading history...
1926
}
1927
1928
/**
1929
 * Get the price_id from the payment meta.
1930
 *
1931
 * Some gateways use `give_price_id` and others were using just `price_id`;
1932
 * This checks for the difference and falls back to retrieving it from the form as a last resort.
1933
 *
1934
 * @since 1.8.6
1935
 *
1936
 * @param $payment_meta
1937
 *
1938
 * @return string
1939
 */
1940
function give_get_payment_meta_price_id( $payment_meta ) {
1941
1942
	if ( isset( $payment_meta['give_price_id'] ) ) {
1943
		$price_id = $payment_meta['give_price_id'];
1944
	} elseif ( isset( $payment_meta['price_id'] ) ) {
1945
		$price_id = $payment_meta['price_id'];
1946
	} else {
1947
		$price_id = give_get_price_id( $payment_meta['give_form_id'], $payment_meta['price'] );
1948
	}
1949
1950
	return apply_filters( 'give_get_payment_meta_price_id', $price_id );
1951
1952
}
1953