Completed
Push — issue/2250 ( d83be4 )
by Ravinder
253:30 queued 246:15
created

functions.php ➔ give_admin_form_goal_stats()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 2
nop 1
dl 0
loc 33
rs 8.4586
c 0
b 0
f 0
1
<?php
2
/**
3
 * Give Form Functions
4
 *
5
 * @package     WordImpress
6
 * @subpackage  Includes/Forms
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.1
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Filter: Do not show the Give shortcut button on Give Forms CPT
19
 *
20
 * @return bool
21
 */
22
function give_shortcode_button_condition() {
23
24
	global $typenow;
25
26
	if ( $typenow != 'give_forms' ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
27
		return true;
28
	}
29
30
	return false;
31
}
32
33
add_filter( 'give_shortcode_button_condition', 'give_shortcode_button_condition' );
34
35
36
/**
37
 * Get the form ID from the form $args
38
 *
39
 * @param array $args
40
 *
41
 * @return int|false
42
 */
43
function get_form_id_from_args( $args ) {
44
45
	if ( isset( $args['form_id'] ) && $args['form_id'] != 0 ) {
0 ignored issues
show
introduced by
Found "!= 0". Use Yoda Condition checks, you must
Loading history...
46
47
		return intval( $args['form_id'] );
48
	}
49
50
	return false;
51
}
52
53
/**
54
 * Checks whether floating labels is enabled for the form ID in $args
55
 *
56
 * @since 1.1
57
 *
58
 * @param array $args
59
 *
60
 * @return bool
61
 */
62
function give_is_float_labels_enabled( $args ) {
63
64
	$float_labels = '';
65
66
	if ( ! empty( $args['float_labels'] ) ) {
67
		$float_labels = $args['float_labels'];
68
	}
69
70
	if ( empty( $float_labels ) ) {
71
		$float_labels = give_get_meta( $args['form_id'], '_give_form_floating_labels', true );
72
	}
73
74
	if ( empty( $float_labels ) || ( 'global' === $float_labels ) ) {
75
		$float_labels = give_get_option( 'floatlabels', 'disabled' );
76
	}
77
78
	return give_is_setting_enabled( $float_labels );
79
}
80
81
/**
82
 * Determines if a user can checkout or not
83
 *
84
 * Allows themes and plugins to set donation checkout conditions
85
 *
86
 * @since 1.0
87
 *
88
 * @return bool Can user checkout?
89
 */
90
function give_can_checkout() {
91
92
	$can_checkout = true;
93
94
	return (bool) apply_filters( 'give_can_checkout', $can_checkout );
95
}
96
97
/**
98
 * Retrieve the Success page URI
99
 *
100
 * @access      public
101
 * @since       1.0
102
 *
103
 * @return      string
104
 */
105 View Code Duplication
function give_get_success_page_uri() {
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...
106
	$give_options = give_get_settings();
107
108
	$success_page = isset( $give_options['success_page'] ) ? get_permalink( absint( $give_options['success_page'] ) ) : get_bloginfo( 'url' );
109
110
	return apply_filters( 'give_get_success_page_uri', $success_page );
111
}
112
113
/**
114
 * Determines if we're currently on the Success page.
115
 *
116
 * @since 1.0
117
 *
118
 * @return bool True if on the Success page, false otherwise.
119
 */
120
function give_is_success_page() {
121
	$give_options    = give_get_settings();
122
	$is_success_page = isset( $give_options['success_page'] ) ? is_page( $give_options['success_page'] ) : false;
123
124
	return apply_filters( 'give_is_success_page', $is_success_page );
125
}
126
127
/**
128
 * Send To Success Page
129
 *
130
 * Sends the user to the success page.
131
 *
132
 * @param string $query_string
133
 *
134
 * @access      public
135
 * @since       1.0
136
 * @return      void
137
 */
138
function give_send_to_success_page( $query_string = null ) {
139
140
	$redirect = give_get_success_page_uri();
141
142
	if ( $query_string ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query_string of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
143
		$redirect .= $query_string;
144
	}
145
146
	$gateway = isset( $_REQUEST['give-gateway'] ) ? $_REQUEST['give-gateway'] : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
147
148
	wp_redirect( apply_filters( 'give_success_page_redirect', $redirect, $gateway, $query_string ) );
149
	give_die();
150
}
151
152
153
/**
154
 * Send back to donation form.
155
 *
156
 * Used to redirect a user back to the donation form if there are errors present.
157
 *
158
 * @param array|string $args
159
 *
160
 * @access public
161
 * @since  1.0
162
 * @return Void
163
 */
164
function give_send_back_to_checkout( $args = array() ) {
165
166
	$url     = isset( $_POST['give-current-url'] ) ? sanitize_text_field( $_POST['give-current-url'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
167
	$form_id = 0;
168
169
	// Set the form_id.
170
	if ( isset( $_POST['give-form-id'] ) ) {
171
		$form_id = sanitize_text_field( $_POST['give-form-id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
172
	}
173
174
	// Need a URL to continue. If none, redirect back to single form.
175
	if ( empty( $url ) ) {
176
		wp_safe_redirect( get_permalink( $form_id ) );
177
		give_die();
178
	}
179
180
	$defaults = array(
181
		'form-id' => (int) $form_id,
182
	);
183
184
	// Set the $level_id.
185
	if ( isset( $_POST['give-price-id'] ) ) {
186
		$defaults['level-id'] = sanitize_text_field( $_POST['give-price-id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
187
	}
188
189
	// Check for backward compatibility.
190
	if ( is_string( $args ) ) {
191
		$args = str_replace( '?', '', $args );
192
	}
193
194
	$args = wp_parse_args( $args, $defaults );
195
196
	// Merge URL query with $args to maintain third-party URL parameters after redirect.
197
	$url_data = wp_parse_url( $url );
198
199
	// Check if an array to prevent notices before parsing.
200
	if ( isset( $url_data['query'] ) && ! empty( $url_data['query'] ) ) {
201
		parse_str( $url_data['query'], $query );
202
203
		// Precaution: don't allow any CC info.
204
		unset( $query['card_number'] );
205
		unset( $query['card_cvc'] );
206
207
	} else {
208
		// No $url_data so pass empty array.
209
		$query = array();
210
	}
211
212
	$new_query        = array_merge( $args, $query );
213
	$new_query_string = http_build_query( $new_query );
214
215
	// Assemble URL parts.
216
	$redirect = home_url( '/' . $url_data['path'] . '?' . $new_query_string . '#give-form-' . $form_id . '-wrap' );
217
218
	// Redirect them.
219
	wp_safe_redirect( apply_filters( 'give_send_back_to_checkout', $redirect, $args ) );
220
	give_die();
221
222
}
223
224
/**
225
 * Get Success Page URL
226
 *
227
 * Gets the success page URL.
228
 *
229
 * @param string $query_string
230
 *
231
 * @access      public
232
 * @since       1.0
233
 * @return      string
234
 */
235
function give_get_success_page_url( $query_string = null ) {
236
237
	$success_page = give_get_option( 'success_page', 0 );
238
	$success_page = get_permalink( $success_page );
239
240
	if ( $query_string ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query_string of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
241
		$success_page .= $query_string;
242
	}
243
244
	return apply_filters( 'give_success_page_url', $success_page );
245
246
}
247
248
/**
249
 * Get the URL of the Failed Donation Page.
250
 *
251
 * @since 1.0
252
 *
253
 * @param bool $extras Extras to append to the URL.
254
 *
255
 * @return mixed Full URL to the Failed Donation Page, if present, home page if it doesn't exist.
256
 */
257
function give_get_failed_transaction_uri( $extras = false ) {
258
	$give_options = give_get_settings();
259
260
	// Remove question mark.
261
	if ( 0 === strpos( $extras, '?' ) ) {
262
		$extras = substr( $extras, 1 );
263
	}
264
265
	$extras_args = wp_parse_args( $extras );
266
267
	// Set nonce if payment id exist in extra params.
268
	if ( array_key_exists( 'payment-id', $extras_args ) ) {
269
		$extras_args['_wpnonce'] = wp_create_nonce( "give-failed-donation-{$extras_args['payment-id']}" );
270
		$extras                  = http_build_query( $extras_args );
271
	}
272
273
	$uri = ! empty( $give_options['failure_page'] ) ?
274
		trailingslashit( get_permalink( $give_options['failure_page'] ) ) :
275
		home_url();
276
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
277
278
	if ( $extras ) {
279
		$uri .= "?{$extras}";
280
	}
281
282
	return apply_filters( 'give_get_failed_transaction_uri', $uri );
283
}
284
285
/**
286
 * Determines if we're currently on the Failed Donation Page.
287
 *
288
 * @since 1.0
289
 * @return bool True if on the Failed Donation Page, false otherwise.
290
 */
291
function give_is_failed_transaction_page() {
292
	$give_options = give_get_settings();
293
	$ret          = isset( $give_options['failure_page'] ) ? is_page( $give_options['failure_page'] ) : false;
294
295
	return apply_filters( 'give_is_failure_page', $ret );
296
}
297
298
/**
299
 * Mark payments as Failed when returning to the Failed Donation Page
300
 *
301
 * @since  1.0
302
 * @since  1.8.16 Add security check
303
 *
304
 * @return bool
305
 */
306
function give_listen_for_failed_payments() {
307
308
	$failed_page = give_get_option( 'failure_page', 0 );
309
	$payment_id  = ! empty( $_GET['payment-id'] ) ? absint( $_GET['payment-id'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
310
	$nonce       = ! empty( $_GET['_wpnonce'] ) ? give_clean( $_GET['_wpnonce'] ) : false;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
311
312
	// Bailout.
313
	if ( ! $failed_page || ! is_page( $failed_page ) || ! $payment_id || ! $nonce ) {
314
		return false;
315
	}
316
317
	// Security check.
318
	if ( ! wp_verify_nonce( $nonce, "give-failed-donation-{$payment_id}" ) ) {
319
		wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ) );
320
	}
321
322
	// Set payment status to failure
323
	give_update_payment_status( $payment_id, 'failed' );
324
}
325
326
add_action( 'template_redirect', 'give_listen_for_failed_payments' );
327
328
/**
329
 * Retrieve the Donation History page URI
330
 *
331
 * @access      public
332
 * @since       1.7
333
 *
334
 * @return      string
335
 */
336 View Code Duplication
function give_get_history_page_uri() {
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...
337
	$give_options = give_get_settings();
338
339
	$history_page = isset( $give_options['history_page'] ) ? get_permalink( absint( $give_options['history_page'] ) ) : get_bloginfo( 'url' );
340
341
	return apply_filters( 'give_get_history_page_uri', $history_page );
342
}
343
344
/**
345
 * Check if a field is required
346
 *
347
 * @param string $field
348
 * @param int    $form_id
349
 *
350
 * @access      public
351
 * @since       1.0
352
 * @return      bool
353
 */
354
function give_field_is_required( $field = '', $form_id ) {
355
356
	$required_fields = give_get_required_fields( $form_id );
357
358
	return array_key_exists( $field, $required_fields );
359
}
360
361
/**
362
 * Record Donation In Log
363
 *
364
 * Stores log information for a donation.
365
 *
366
 * @since 1.0
367
 *
368
 * @param int         $give_form_id  Give Form ID.
369
 * @param int         $payment_id    Payment ID.
370
 * @param bool|int    $price_id      Price ID, if any.
371
 * @param string|null $donation_date The date of the donation.
372
 *
373
 * @return void
374
 */
375
function give_record_donation_in_log( $give_form_id = 0, $payment_id, $price_id = false, $donation_date = null ) {
376
	$log_data = array(
377
		'log_parent'   => $payment_id,
378
		'log_type'     => 'sale',
379
		'log_date'     => isset( $donation_date ) ? $donation_date : null,
380
		'log_date_gmt' => isset( $donation_date ) ? $donation_date : null,
381
	);
382
383
	$log_meta = array(
384
		'form_id'  => $give_form_id,
385
		'price_id' => (int) $price_id,
386
	);
387
388
	Give()->logs->insert_log( $log_data, $log_meta );
389
}
390
391
392
/**
393
 * Increases the donation total count of a donation form.
394
 *
395
 * @since 1.0
396
 *
397
 * @param int $form_id  Give Form ID
398
 * @param int $quantity Quantity to increase donation count by
399
 *
400
 * @return bool|int
401
 */
402
function give_increase_donation_count( $form_id = 0, $quantity = 1 ) {
403
	$quantity = (int) $quantity;
404
	$form     = new Give_Donate_Form( $form_id );
405
406
	return $form->increase_sales( $quantity );
407
}
408
409
/**
410
 * Decreases the sale count of a form. Primarily for when a donation is refunded.
411
 *
412
 * @since 1.0
413
 *
414
 * @param int $form_id  Give Form ID
415
 * @param int $quantity Quantity to increase donation count by
416
 *
417
 * @return bool|int
418
 */
419
function give_decrease_donation_count( $form_id = 0, $quantity = 1 ) {
420
	$quantity = (int) $quantity;
421
	$form     = new Give_Donate_Form( $form_id );
422
423
	return $form->decrease_sales( $quantity );
424
}
425
426
/**
427
 * Increases the total earnings of a form.
428
 *
429
 * @since 1.0
430
 *
431
 * @param int $give_form_id Give Form ID
432
 * @param int $amount       Earnings
433
 *
434
 * @return bool|int
435
 */
436
function give_increase_earnings( $give_form_id = 0, $amount ) {
437
	$form = new Give_Donate_Form( $give_form_id );
438
439
	return $form->increase_earnings( $amount );
440
}
441
442
/**
443
 * Decreases the total earnings of a form.
444
 *
445
 * Primarily for when a donation is refunded.
446
 *
447
 * @since 1.0
448
 *
449
 * @param int $form_id Give Form ID
450
 * @param int $amount  Earnings
451
 *
452
 * @return bool|int
453
 */
454
function give_decrease_form_earnings( $form_id = 0, $amount ) {
455
456
	$form = new Give_Donate_Form( $form_id );
457
458
	return $form->decrease_earnings( $amount );
459
}
460
461
462
/**
463
 * Returns the total earnings for a form.
464
 *
465
 * @since 1.0
466
 *
467
 * @param int $form_id Give Form ID
468
 *
469
 * @return int $earnings Earnings for a certain form
470
 */
471
function give_get_form_earnings_stats( $form_id = 0 ) {
472
	$give_form = new Give_Donate_Form( $form_id );
473
474
	/**
475
	 * Filter the form earnings
476
	 *
477
	 * @since 1.8.17
478
	 */
479
	return apply_filters( 'give_get_form_earnings_stats', $give_form->earnings, $form_id, $give_form );
480
}
481
482
483
/**
484
 * Return the sales number for a form.
485
 *
486
 * @since 1.0
487
 *
488
 * @param int $give_form_id Give Form ID
489
 *
490
 * @return int $sales Amount of sales for a certain form
491
 */
492
function give_get_form_sales_stats( $give_form_id = 0 ) {
493
	$give_form = new Give_Donate_Form( $give_form_id );
494
495
	return $give_form->sales;
496
}
497
498
499
/**
500
 * Retrieves the average monthly sales for a specific donation form
501
 *
502
 * @since 1.0
503
 *
504
 * @param int $form_id Form ID
505
 *
506
 * @return float $sales Average monthly sales
507
 */
508 View Code Duplication
function give_get_average_monthly_form_sales( $form_id = 0 ) {
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...
509
	$sales        = give_get_form_sales_stats( $form_id );
510
	$release_date = get_post_field( 'post_date', $form_id );
511
512
	$diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
513
514
	$months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
515
516
	if ( $months > 0 ) {
517
		$sales = ( $sales / $months );
518
	}
519
520
	return $sales;
521
}
522
523
524
/**
525
 * Retrieves the average monthly earnings for a specific form
526
 *
527
 * @since 1.0
528
 *
529
 * @param int $form_id Form ID
530
 *
531
 * @return float $earnings Average monthly earnings
532
 */
533 View Code Duplication
function give_get_average_monthly_form_earnings( $form_id = 0 ) {
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...
534
	$earnings     = give_get_form_earnings_stats( $form_id );
535
	$release_date = get_post_field( 'post_date', $form_id );
536
537
	$diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
538
539
	$months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
540
541
	if ( $months > 0 ) {
542
		$earnings = ( $earnings / $months );
543
	}
544
545
	return $earnings < 0 ? 0 : $earnings;
546
}
547
548
549
/**
550
 * Get Price Option Name (Text)
551
 *
552
 * Retrieves the name of a variable price option.
553
 *
554
 * @since       1.0
555
 *
556
 * @param int  $form_id      ID of the donation form.
557
 * @param int  $price_id     ID of the price option.
558
 * @param int  $payment_id   payment ID for use in filters ( optional ).
559
 * @param bool $use_fallback Outputs the level amount if no level text is provided.
560
 *
561
 * @return string $price_name Name of the price option
562
 */
563
function give_get_price_option_name( $form_id = 0, $price_id = 0, $payment_id = 0, $use_fallback = true ) {
564
565
	$prices     = give_get_variable_prices( $form_id );
566
	$price_name = '';
567
568
	if ( false === $prices ) {
569
		return $price_name;
570
	}
571
572
	foreach ( $prices as $price ) {
573
574
		if ( intval( $price['_give_id']['level_id'] ) === intval( $price_id ) ) {
575
576
			$price_text     = isset( $price['_give_text'] ) ? $price['_give_text'] : '';
577
			$price_fallback = $use_fallback ?
578
				give_currency_filter(
579
					give_format_amount(
580
						$price['_give_amount'],
581
						array( 'sanitize' => false )
582
					),
583
					array( 'decode_currency' => true )
584
				) : '';
585
			$price_name     = ! empty( $price_text ) ? $price_text : $price_fallback;
586
587
		}
588
	}
589
590
	return apply_filters( 'give_get_price_option_name', $price_name, $form_id, $payment_id, $price_id );
591
}
592
593
594
/**
595
 * Retrieves a price from from low to high of a variable priced form
596
 *
597
 * @since 1.0
598
 *
599
 * @param int  $form_id   ID of the form
600
 * @param bool $formatted Flag to decide which type of price range string return
601
 *
602
 * @return string $range A fully formatted price range
603
 */
604
function give_price_range( $form_id = 0, $formatted = true ) {
605
	$low        = give_get_lowest_price_option( $form_id );
606
	$high       = give_get_highest_price_option( $form_id );
607
	$order_type = ! empty( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'asc';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
608
609
	$range = sprintf(
610
		'<span class="give_price_range_%1$s">%2$s</span><span class="give_price_range_sep">&nbsp;&ndash;&nbsp;</span><span class="give_price_range_%3$s">%4$s</span>',
611
		'asc' === $order_type ? 'low' : 'high',
612
		'asc' === $order_type ? give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ),
613
		'asc' === $order_type ? 'high' : 'low',
614
		'asc' === $order_type ? give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) )
615
0 ignored issues
show
Coding Style introduced by
There should be no empty lines in a multi-line function call.
Loading history...
616
	);
617
618
	if ( ! $formatted ) {
619
		$range = wp_strip_all_tags( $range );
620
	}
621
622
	return apply_filters( 'give_price_range', $range, $form_id, $low, $high );
623
}
624
625
626
/**
627
 * Get Lowest Price ID
628
 *
629
 * Retrieves the ID for the cheapest price option of a variable donation form
630
 *
631
 * @since 1.5
632
 *
633
 * @param int $form_id ID of the donation
634
 *
635
 * @return int ID of the lowest price
636
 */
637
function give_get_lowest_price_id( $form_id = 0 ) {
638
639
	if ( empty( $form_id ) ) {
640
		$form_id = get_the_ID();
641
	}
642
643
	if ( ! give_has_variable_prices( $form_id ) ) {
644
		return give_get_form_price( $form_id );
645
	}
646
647
	$prices = give_get_variable_prices( $form_id );
648
649
	$min = $min_id = 0;
650
651
	if ( ! empty( $prices ) ) {
652
653
		foreach ( $prices as $key => $price ) {
654
655
			if ( empty( $price['_give_amount'] ) ) {
656
				continue;
657
			}
658
659
			if ( ! isset( $min ) ) {
660
				$min = $price['_give_amount'];
661
			} else {
662
				$min = min( $min, $price['_give_amount'] );
663
			}
664
665
			if ( $price['_give_amount'] == $min ) {
666
				$min_id = $price['_give_id']['level_id'];
667
			}
668
		}
669
	}
670
671
	return (int) $min_id;
672
}
673
674
/**
675
 * Retrieves cheapest price option of a variable priced form
676
 *
677
 * @since 1.0
678
 *
679
 * @param int $form_id ID of the form
680
 *
681
 * @return float Amount of the lowest price
682
 */
683 View Code Duplication
function give_get_lowest_price_option( $form_id = 0 ) {
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...
684
	if ( empty( $form_id ) ) {
685
		$form_id = get_the_ID();
686
	}
687
688
	if ( ! give_has_variable_prices( $form_id ) ) {
689
		return give_get_form_price( $form_id );
690
	}
691
692
	if ( ! ( $low = get_post_meta( $form_id, '_give_levels_minimum_amount', true ) ) ) {
693
		// Backward compatibility.
694
		$prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
695
		$low    = ! empty( $prices ) ? min( $prices ) : 0;
696
	}
697
698
	return give_maybe_sanitize_amount( $low );
699
}
700
701
/**
702
 * Retrieves most expensive price option of a variable priced form
703
 *
704
 * @since 1.0
705
 *
706
 * @param int $form_id ID of the form
707
 *
708
 * @return float Amount of the highest price
709
 */
710 View Code Duplication
function give_get_highest_price_option( $form_id = 0 ) {
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...
711
712
	if ( empty( $form_id ) ) {
713
		$form_id = get_the_ID();
714
	}
715
716
	if ( ! give_has_variable_prices( $form_id ) ) {
717
		return give_get_form_price( $form_id );
718
	}
719
720
	if ( ! ( $high = get_post_meta( $form_id, '_give_levels_maximum_amount', true ) ) ) {
721
		// Backward compatibility.
722
		$prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
723
		$high   = ! empty( $prices ) ? max( $prices ) : 0;
724
	}
725
726
	return give_maybe_sanitize_amount( $high );
727
}
728
729
/**
730
 * Returns the price of a form, but only for non-variable priced forms.
731
 *
732
 * @since 1.0
733
 *
734
 * @param int $form_id ID number of the form to retrieve a price for
735
 *
736
 * @return mixed string|int Price of the form
737
 */
738
function give_get_form_price( $form_id = 0 ) {
739
740
	if ( empty( $form_id ) ) {
741
		return false;
742
	}
743
744
	$form = new Give_Donate_Form( $form_id );
745
746
	return $form->__get( 'price' );
747
}
748
749
/**
750
 * Returns the minimum price amount of a form, only enforced for the custom amount input.
751
 *
752
 * @since 1.3.6
753
 *
754
 * @param int $form_id ID number of the form to retrieve the minimum price for
755
 *
756
 * @return mixed string|int Minimum price of the form
757
 */
758
function give_get_form_minimum_price( $form_id = 0 ) {
759
760
	if ( empty( $form_id ) ) {
761
		return false;
762
	}
763
764
	$form = new Give_Donate_Form( $form_id );
765
766
	return $form->get_minimum_price();
767
768
}
769
770
/**
771
 * Return the maximum price amount of form.
772
 *
773
 * @since 2.1
774
 *
775
 * @param int $form_id Donate Form ID
776
 *
777
 * @return bool|float
778
 */
779
function give_get_form_maximum_price( $form_id = 0 ) {
780
781
	if ( empty( $form_id ) ) {
782
		return false;
783
	}
784
785
	$form = new Give_Donate_Form( $form_id );
786
787
	return $form->get_maximum_price();
788
}
789
790
/**
791
 * Displays a formatted price for a donation form
792
 *
793
 * @since 1.0
794
 *
795
 * @param int      $form_id  ID of the form price to show
796
 * @param bool     $echo     Whether to echo or return the results
797
 * @param bool|int $price_id Optional price id for variable pricing
798
 *
799
 * @return int $formatted_price
800
 */
801
function give_price( $form_id = 0, $echo = true, $price_id = false ) {
802
	$price = 0;
803
804
	if ( empty( $form_id ) ) {
805
		$form_id = get_the_ID();
806
	}
807
808
	if ( give_has_variable_prices( $form_id ) ) {
809
810
		$prices = give_get_variable_prices( $form_id );
811
812
		if ( false !== $price_id ) {
813
814
			// loop through multi-prices to see which is default
815 View Code Duplication
			foreach ( $prices as $price ) {
0 ignored issues
show
Bug introduced by
The expression $prices of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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...
816
				// this is the default price
817
				if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
818
					$price = (float) $price['_give_amount'];
819
				};
820
			}
821
		} else {
822
823
			$price = give_get_lowest_price_option( $form_id );
824
		}
825
	} else {
826
827
		$price = give_get_form_price( $form_id );
828
	}
829
830
	$price           = apply_filters( 'give_form_price', give_maybe_sanitize_amount( $price ), $form_id );
831
	$formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>';
832
	$formatted_price = apply_filters( 'give_form_price_after_html', $formatted_price, $form_id, $price );
833
834
	if ( $echo ) {
835
		echo $formatted_price;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$formatted_price'
Loading history...
836
	} else {
837
		return $formatted_price;
838
	}
839
}
840
841
add_filter( 'give_form_price', 'give_format_amount', 10 );
842
add_filter( 'give_form_price', 'give_currency_filter', 20 );
843
844
845
/**
846
 * Retrieves the amount of a variable price option
847
 *
848
 * @since 1.0
849
 *
850
 * @param int $form_id  ID of the form
851
 * @param int $price_id ID of the price option
852
 *
853
 * @return float $amount Amount of the price option
854
 */
855
function give_get_price_option_amount( $form_id = 0, $price_id = 0 ) {
856
	$prices = give_get_variable_prices( $form_id );
857
858
	$amount = 0.00;
859
860 View Code Duplication
	foreach ( $prices as $price ) {
0 ignored issues
show
Bug introduced by
The expression $prices of type false|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
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...
861
		if ( isset( $price['_give_id']['level_id'] ) && $price['_give_id']['level_id'] == $price_id ) {
862
			$amount = isset( $price['_give_amount'] ) ? $price['_give_amount'] : 0.00;
863
			break;
864
		};
865
	}
866
867
	return apply_filters( 'give_get_price_option_amount', give_maybe_sanitize_amount( $amount ), $form_id, $price_id );
868
}
869
870
/**
871
 * Returns the goal of a form
872
 *
873
 * @since 1.0
874
 *
875
 * @param int $form_id ID number of the form to retrieve a goal for
876
 *
877
 * @return mixed string|int Goal of the form
878
 */
879
function give_get_form_goal( $form_id = 0 ) {
880
881
	if ( empty( $form_id ) ) {
882
		return false;
883
	}
884
885
	$form = new Give_Donate_Form( $form_id );
886
887
	return $form->goal;
888
889
}
890
891
/**
892
 * Returns the goal format of a form
893
 *
894
 * @since 2.0
895
 *
896
 * @param int $form_id ID number of the form to retrieve a goal for
897
 *
898
 * @return mixed string|int Goal of the form
899
 */
900
function give_get_form_goal_format( $form_id = 0 ) {
901
902
	if ( empty( $form_id ) ) {
903
		return false;
904
	}
905
906
	return give_get_meta( $form_id, '_give_goal_format', true );
907
908
}
909
910
/**
911
 * Display/Return a formatted goal for a donation form
912
 *
913
 * @param int|Give_Donate_Form $form Form ID or Form Object.
914
 *
915
 * @since 2.1
916
 *
917
 * @return array
918
 */
919
function give_goal_progress_stats( $form ) {
920
921
	if ( ! $form instanceof Give_Donate_Form ) {
922
		$form = new Give_Donate_Form( $form );
923
	}
924
925
	$goal_format = give_get_form_goal_format( $form->ID );
926
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
927
928
	/**
929
	 * Filter the form donations.
930
	 *
931
	 * @since 2.1
932
	 */
933
	$donations = apply_filters( 'give_goal_donations_raised_output', $form->sales, $form->ID, $form );
934
935
	/**
936
	 * Filter the form income.
937
	 *
938
	 * @since 1.8.8
939
	 */
940
	$income = apply_filters( 'give_goal_amount_raised_output', $form->earnings, $form->ID, $form );
941
942
	/**
943
	 * Filter the form.
944
	 *
945
	 * @since 1.8.8
946
	 */
947
	$total_goal = apply_filters( 'give_goal_amount_target_output', round( give_maybe_sanitize_amount( $form->goal ) ), $form->ID, $form );
948
949
	switch ( $goal_format ) {
950
		case  'donation':
951
			$actual = $donations;
952
			break;
953
		case 'donors':
954
			$actual = give_get_form_donor_count( $form->ID );
955
			break;
956
		default :
957
			$actual = $income;
958
			break;
959
	}
960
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
961
962
	$progress = round( ( $actual / $total_goal ) * 100, 2 );
963
964
	$stats_array = array(
965
		'raw_actual' => $actual,
966
		'raw_goal'   => $total_goal
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
967
	);
968
969
	/**
970
	 * Filter the goal progress output
971
	 *
972
	 * @since 1.8.8
973
	 */
974
	$progress = apply_filters( 'give_goal_amount_funded_percentage_output', $progress, $form->ID, $form );
975
976
	// Define Actual Goal based on the goal format.
977 View Code Duplication
	if ( 'percentage' === $goal_format ) {
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...
978
		$actual = "{$actual}%";
979
	} else if ( 'amount' === $goal_format ) {
980
		$actual = give_currency_filter( give_format_amount( $actual ) );
981
	}
982
983
	// Define Total Goal based on the goal format.
984 View Code Duplication
	if ( 'percentage' === $goal_format ) {
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...
985
		$total_goal = '';
986
	} else if ( 'amount' === $goal_format ) {
987
		$total_goal = give_currency_filter( give_format_amount( $total_goal ) );
988
	}
989
990
	$stats_array = array_merge(
991
		array(
992
			'progress' => $progress,
993
			'actual'   => $actual,
994
			'goal'     => $total_goal,
995
			'format'   => $goal_format,
996
		),
997
		$stats_array
998
	);
999
1000
	/**
1001
	 * Filter the goal stats
1002
	 *
1003
	 * @since 2.1
1004
	 */
1005
	return apply_filters( 'give_goal_progress_stats', $stats_array );
1006
1007
}
1008
1009
/**
1010
 * Display/Return a formatted goal for a donation form
1011
 *
1012
 * @since 1.0
1013
 *
1014
 * @param int  $form_id ID of the form price to show
1015
 * @param bool $echo    Whether to echo or return the results
1016
 *
1017
 * @return string $formatted_goal
1018
 */
1019
function give_goal( $form_id = 0, $echo = true ) {
1020
1021
	if ( empty( $form_id ) ) {
1022
		$form_id = get_the_ID();
1023
	}
1024
1025
	$goal        = give_get_form_goal( $form_id );
1026
	$goal_format = give_get_form_goal_format( $form_id );
1027
1028
	if ( 'donation' === $goal_format ) {
1029
		$goal = "{$goal} donations";
1030
	} else {
1031
		$goal = apply_filters( 'give_form_goal', give_maybe_sanitize_amount( $goal ), $form_id );
1032
	}
1033
1034
	$formatted_goal = sprintf(
1035
		'<span class="give_price" id="give_price_%1$s">%2$s</span>',
1036
		$form_id,
1037
		$goal
1038
	);
1039
	$formatted_goal = apply_filters( 'give_form_price_after_html', $formatted_goal, $form_id, $goal );
1040
1041
	if ( $echo ) {
1042
		echo $formatted_goal;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$formatted_goal'
Loading history...
1043
	} else {
1044
		return $formatted_goal;
1045
	}
1046
}
1047
1048
add_filter( 'give_form_goal', 'give_format_amount', 10 );
1049
add_filter( 'give_form_goal', 'give_currency_filter', 20 );
1050
1051
1052
/**
1053
 * Checks if users can only donate when logged in
1054
 *
1055
 * @since  1.0
1056
 *
1057
 * @param  int $form_id Give form ID
1058
 *
1059
 * @return bool  $ret Whether or not the logged_in_only setting is set
1060
 */
1061
function give_logged_in_only( $form_id ) {
1062
	// If _give_logged_in_only is set to enable then guest can donate from that specific form.
1063
	// Otherwise it is member only donation form.
1064
	$val = give_get_meta( $form_id, '_give_logged_in_only', true );
1065
	$val = ! empty( $val ) ? $val : 'enabled';
1066
1067
	$ret = ! give_is_setting_enabled( $val );
1068
1069
	return (bool) apply_filters( 'give_logged_in_only', $ret, $form_id );
1070
}
1071
1072
1073
/**
1074
 * Checks the option for the "Register / Login Option"
1075
 *
1076
 * @since 1.4.1
1077
 *
1078
 * @param int $form_id
1079
 *
1080
 * @return string
1081
 */
1082
function give_show_login_register_option( $form_id ) {
1083
1084
	$show_register_form = give_get_meta( $form_id, '_give_show_register_form', true );
1085
1086
	return apply_filters( 'give_show_register_form', $show_register_form, $form_id );
1087
1088
}
1089
1090
1091
/**
1092
 * Get pre fill form field values.
1093
 *
1094
 * Note: this function will extract form field values from give_purchase session data.
1095
 *
1096
 * @since  1.8
1097
 *
1098
 * @param  int $form_id Form ID.
1099
 *
1100
 * @return array
1101
 */
1102
function _give_get_prefill_form_field_values( $form_id ) {
1103
	$logged_in_donor_info = array();
1104
1105
	if ( is_user_logged_in() ) :
1106
		$donor_data    = get_userdata( get_current_user_id() );
1107
		$donor         = new Give_Donor( get_current_user_id(), true );
1108
		$donor_address = give_get_donor_address( get_current_user_id() );
1109
		$company_name  = $donor->get_company_name();
1110
1111
		$logged_in_donor_info = array(
1112
			// First name.
1113
			'give_first'      => $donor_data->first_name,
1114
1115
			// Last name.
1116
			'give_last'       => $donor_data->last_name,
1117
1118
			// Company name.
1119
			'company_name'    => $company_name,
1120
1121
			// Email.
1122
			'give_email'      => $donor_data->user_email,
1123
1124
			// Street address 1.
1125
			'card_address'    => $donor_address['line1'],
1126
1127
			// Street address 2.
1128
			'card_address_2'  => $donor_address['line2'],
1129
1130
			// Country.
1131
			'billing_country' => $donor_address['country'],
1132
1133
			// State.
1134
			'card_state'      => $donor_address['state'],
1135
1136
			// City.
1137
			'card_city'       => $donor_address['city'],
1138
1139
			// Zipcode
1140
			'card_zip'        => $donor_address['zip'],
1141
		);
1142
	endif;
1143
1144
	// Bailout: Auto fill form field values only form form which donor is donating.
1145
	if (
1146
		empty( $_GET['form-id'] )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
1147
		|| ! $form_id
1148
		|| ( $form_id !== absint( $_GET['form-id'] ) )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
1149
	) {
1150
		return $logged_in_donor_info;
1151
	}
1152
1153
	// Get purchase data.
1154
	$give_purchase_data = Give()->session->get( 'give_purchase' );
1155
1156
	// Get donor info from form data.
1157
	$give_donor_info_in_session = empty( $give_purchase_data['post_data'] )
1158
		? array()
1159
		: $give_purchase_data['post_data'];
1160
1161
	// Output.
1162
	return wp_parse_args( $give_donor_info_in_session, $logged_in_donor_info );
1163
}
1164
1165
/**
1166
 * Get donor count of form
1167
 *
1168
 * @since 2.1.0
1169
 *
1170
 * @param int   $form_id
1171
 * @param array $args
1172
 *
1173
 * @return int
1174
 */
1175
function give_get_form_donor_count( $form_id, $args = array() ) {
1176
	global $wpdb;
1177
1178
	$cache_key   = Give_Cache::get_key( "form_donor_count_{$form_id}", $args, false );
1179
	$donor_count = absint( Give_Cache::get_db_query( $cache_key ) );
1180
1181
	if ( $form_id && ! $donor_count ) {
1182
		// Set arguments.
1183
		$args = wp_parse_args(
1184
			$args,
1185
			array(
1186
				'unique' => true,
1187
			)
1188
		);
1189
1190
		$donation_meta_table = Give()->payment_meta->table_name;
1191
1192
		$distinct = $args['unique'] ? 'DISTINCT meta_value' : 'meta_value';
1193
1194
		$query = $wpdb->prepare(
1195
			"
1196
			SELECT COUNT({$distinct})
1197
			FROM {$donation_meta_table}
1198
			WHERE meta_key=%s
1199
			AND payment_id IN(
1200
				SELECT payment_id
1201
				FROM {$donation_meta_table}
1202
				WHERE meta_key=%s
1203
				AND meta_value=%s
1204
			)
1205
			",
1206
			'_give_payment_donor_id',
1207
			'_give_payment_form_id',
1208
			$form_id
1209
		);
1210
1211
		$donor_count = absint( $wpdb->get_var( $query ) );
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...
1212
	}
1213
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1214
1215
	/**
1216
	 * Filter the donor count
1217
	 *
1218
	 * @since 2.1.0
1219
	 */
1220
	$donor_count = apply_filters( 'give_get_form_donor_count', $donor_count, $form_id, $args );
1221
1222
	Give_Cache::set_db_query( $cache_key, $donor_count );
1223
1224
	return $donor_count;
1225
}
1226
1227
/**
1228
 * Verify the form status.
1229
 *
1230
 * @param int $form_id Donation Form ID.
1231
 *
1232
 * @since 2.1
1233
 *
1234
 * @return void
1235
 */
1236
function give_set_form_closed_status( $form_id ) {
1237
1238
	// Bailout.
1239
	if ( empty( $form_id ) ) {
1240
		return;
1241
	}
1242
1243
	$open_form       = false;
1244
	$is_goal_enabled = give_is_setting_enabled( give_get_meta( $form_id, '_give_goal_option', true, 'disabled' ) );
0 ignored issues
show
Documentation introduced by
'disabled' is of type string, 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...
1245
1246
	// Proceed, if the form goal is enabled.
1247
	if ( $is_goal_enabled ) {
1248
1249
		$close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $form_id, '_give_close_form_when_goal_achieved', true, 'disabled' ) );
0 ignored issues
show
Documentation introduced by
'disabled' is of type string, 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...
1250
1251
		// Proceed, if close form when goal achieved option is enabled.
1252
		if ( $close_form_when_goal_achieved ) {
1253
1254
			$form        = new Give_Donate_Form( $form_id );
1255
			$goal_format = give_get_form_goal_format( $form_id );
1256
1257
			// Verify whether the form is closed or not after processing data based on goal format.
1258 View Code Duplication
			switch ( $goal_format ) {
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...
1259
				case 'donation':
1260
					$closed = $form->get_goal() <= $form->get_sales();
1261
					break;
1262
				case 'donors':
1263
					$closed = $form->get_goal() <= give_get_form_donor_count( $form->ID );
1264
					break;
1265
				default :
1266
					$closed = $form->get_goal() <= $form->get_earnings();
1267
					break;
1268
			}
1269
1270
			// Update form meta if verified that the form is closed.
1271
			if ( $closed ) {
1272
				give_update_meta( $form_id, '_give_form_status', 'closed' );
1273
			} else {
1274
				$open_form = true;
1275
			}
1276
		} else {
1277
			$open_form = true;
1278
		}
1279
	} else {
1280
		$open_form = true;
1281
	}
1282
1283
	// If $open_form is true, then update form status to open.
1284
	if ( $open_form ) {
1285
		give_update_meta( $form_id, '_give_form_status', 'open' );
1286
	}
1287
}
1288
1289
/**
1290
 * Show Form Goal Stats in Admin ( Listing and Detail page )
1291
 *
1292
 * @param int $form_id Form ID.
1293
 *
1294
 * @since 2.1.0
1295
 *
1296
 * @return string
1297
 */
1298
function give_admin_form_goal_stats( $form_id ) {
1299
1300
	$html             = '';
1301
	$goal_stats       = give_goal_progress_stats( $form_id );
1302
	$percent_complete = round( ( $goal_stats['raw_actual'] / $goal_stats['raw_goal'] ), 3 ) * 100;
1303
1304
	$html .= sprintf(
1305
		'<div class="give-admin-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="%1$s">
1306
<span style="width:%1$s%%;"></span>
1307
</div>',
1308
		esc_attr( $goal_stats['progress'] )
1309
	);
1310
1311
	$html .= sprintf(
1312
		( 'percentage' !== $goal_stats['format'] ) ?
1313
			'<div class="give-goal-text"><span>%1$s</span> %2$s <a href="%3$s">%4$s</a> %5$s ' :
1314
			'<div class="give-goal-text"><a href="%3$s">%1$s </a>',
1315
		( 'percentage' !== $goal_stats['format'] ) ? $goal_stats['actual'] : $percent_complete . '%',
1316
		( 'percentage' !== $goal_stats['format'] ) ? __( 'of', 'give' ) : '',
1317
		esc_url( admin_url( "post.php?post={$form_id}&action=edit&give_tab=donation_goal_options" ) ),
1318
		$goal_stats['goal'],
1319
		( 'donors' === $goal_stats['format'] ? __( 'Donors', 'give' ) : ( 'donation' === $goal_stats['format'] ? __( 'Donations', 'give' ) : '' ) )
1320
	);
1321
1322
	if ( $goal_stats['raw_actual'] >= $goal_stats['raw_goal'] ) {
1323
		$html .= sprintf( '<span class="give-admin-goal-achieved"><span class="dashicons dashicons-star-filled"></span> %s</span>', __( 'Goal achieved', 'give' ) );
1324
	}
1325
1326
	$html .= '</div>';
1327
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
1328
1329
	return $html;
1330
}