PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.2.3
GiveWP – Donation Plugin and Fundraising Platform v2.2.3
4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / forms / functions.php
give / includes / forms Last commit date
functions.php 7 years ago template.php 7 years ago widget.php 7 years ago
functions.php
1493 lines
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' ) {
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 ) {
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 function give_get_success_page_uri() {
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 ) {
143 $redirect .= $query_string;
144 }
145
146 $gateway = isset( $_REQUEST['give-gateway'] ) ? $_REQUEST['give-gateway'] : '';
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'] ) : '';
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'] );
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'] );
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 ) {
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
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;
310 $nonce = ! empty( $_GET['_wpnonce'] ) ? give_clean( $_GET['_wpnonce'] ) : false;
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 function give_get_history_page_uri() {
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
405 /** @var \Give_Donate_Form $form */
406 $form = new Give_Donate_Form( $form_id );
407
408 return $form->increase_sales( $quantity );
409 }
410
411 /**
412 * Decreases the sale count of a form. Primarily for when a donation is refunded.
413 *
414 * @since 1.0
415 *
416 * @param int $form_id Give Form ID
417 * @param int $quantity Quantity to increase donation count by
418 *
419 * @return bool|int
420 */
421 function give_decrease_donation_count( $form_id = 0, $quantity = 1 ) {
422 $quantity = (int) $quantity;
423
424 /** @var \Give_Donate_Form $form */
425 $form = new Give_Donate_Form( $form_id );
426
427 return $form->decrease_sales( $quantity );
428 }
429
430 /**
431 * Increases the total earnings of a form.
432 *
433 * @since 1.0
434 *
435 * @since 2.1 Pass donation id.
436 *
437 * @param int $give_form_id Give Form ID
438 * @param int $amount Earnings
439 * @param int $payment_id Donation ID.
440 *
441 * @return bool|int
442 */
443 function give_increase_earnings( $give_form_id = 0, $amount, $payment_id = 0 ) {
444 /** @var \Give_Donate_Form $form */
445 $form = new Give_Donate_Form( $give_form_id );
446
447 return $form->increase_earnings( $amount, $payment_id );
448 }
449
450 /**
451 * Decreases the total earnings of a form.
452 *
453 * Primarily for when a donation is refunded.
454 *
455 * @since 1.0
456 *
457 * @since 2.1 Pass donation id.
458 *
459 * @param int $form_id Give Form ID
460 * @param int $amount Earnings
461 * @param int $payment_id Donation ID.
462 *
463 * @return bool|int
464 */
465 function give_decrease_form_earnings( $form_id = 0, $amount, $payment_id = 0 ) {
466 /** @var \Give_Donate_Form $form */
467 $form = new Give_Donate_Form( $form_id );
468
469 return $form->decrease_earnings( $amount, $payment_id );
470 }
471
472
473 /**
474 * Returns the total earnings for a form.
475 *
476 * @since 1.0
477 *
478 * @param int $form_id Give Form ID
479 *
480 * @return int $earnings Earnings for a certain form
481 */
482 function give_get_form_earnings_stats( $form_id = 0 ) {
483 $give_form = new Give_Donate_Form( $form_id );
484
485 /**
486 * Filter the form earnings
487 *
488 * @since 1.8.17
489 */
490 return apply_filters( 'give_get_form_earnings_stats', $give_form->earnings, $form_id, $give_form );
491 }
492
493
494 /**
495 * Return the sales number for a form.
496 *
497 * @since 1.0
498 *
499 * @param int $give_form_id Give Form ID
500 *
501 * @return int $sales Amount of sales for a certain form
502 */
503 function give_get_form_sales_stats( $give_form_id = 0 ) {
504 $give_form = new Give_Donate_Form( $give_form_id );
505
506 return $give_form->sales;
507 }
508
509
510 /**
511 * Retrieves the average monthly sales for a specific donation form
512 *
513 * @since 1.0
514 *
515 * @param int $form_id Form ID
516 *
517 * @return float $sales Average monthly sales
518 */
519 function give_get_average_monthly_form_sales( $form_id = 0 ) {
520 $sales = give_get_form_sales_stats( $form_id );
521 $release_date = get_post_field( 'post_date', $form_id );
522
523 $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
524
525 $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
526
527 if ( $months > 0 ) {
528 $sales = ( $sales / $months );
529 }
530
531 return $sales;
532 }
533
534
535 /**
536 * Retrieves the average monthly earnings for a specific form
537 *
538 * @since 1.0
539 *
540 * @param int $form_id Form ID
541 *
542 * @return float $earnings Average monthly earnings
543 */
544 function give_get_average_monthly_form_earnings( $form_id = 0 ) {
545 $earnings = give_get_form_earnings_stats( $form_id );
546 $release_date = get_post_field( 'post_date', $form_id );
547
548 $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
549
550 $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
551
552 if ( $months > 0 ) {
553 $earnings = ( $earnings / $months );
554 }
555
556 return $earnings < 0 ? 0 : $earnings;
557 }
558
559
560 /**
561 * Get Price Option Name (Text)
562 *
563 * Retrieves the name of a variable price option.
564 *
565 * @since 1.0
566 *
567 * @param int $form_id ID of the donation form.
568 * @param int $price_id ID of the price option.
569 * @param int $payment_id payment ID for use in filters ( optional ).
570 * @param bool $use_fallback Outputs the level amount if no level text is provided.
571 *
572 * @return string $price_name Name of the price option
573 */
574 function give_get_price_option_name( $form_id = 0, $price_id = 0, $payment_id = 0, $use_fallback = true ) {
575
576 $prices = give_get_variable_prices( $form_id );
577 $price_name = '';
578
579 if ( false === $prices ) {
580 return $price_name;
581 }
582
583 foreach ( $prices as $price ) {
584
585 if ( intval( $price['_give_id']['level_id'] ) === intval( $price_id ) ) {
586
587 $price_text = isset( $price['_give_text'] ) ? $price['_give_text'] : '';
588 $price_fallback = $use_fallback ?
589 give_currency_filter(
590 give_format_amount(
591 $price['_give_amount'],
592 array( 'sanitize' => false )
593 ),
594 array( 'decode_currency' => true )
595 ) : '';
596 $price_name = ! empty( $price_text ) ? $price_text : $price_fallback;
597
598 }
599 }
600
601 return apply_filters( 'give_get_price_option_name', $price_name, $form_id, $payment_id, $price_id );
602 }
603
604
605 /**
606 * Retrieves a price from from low to high of a variable priced form
607 *
608 * @since 1.0
609 *
610 * @param int $form_id ID of the form
611 * @param bool $formatted Flag to decide which type of price range string return
612 *
613 * @return string $range A fully formatted price range
614 */
615 function give_price_range( $form_id = 0, $formatted = true ) {
616 $low = give_get_lowest_price_option( $form_id );
617 $high = give_get_highest_price_option( $form_id );
618 $order_type = ! empty( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'asc';
619
620 $range = sprintf(
621 '<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>',
622 'asc' === $order_type ? 'low' : 'high',
623 'asc' === $order_type ? give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ),
624 'asc' === $order_type ? 'high' : 'low',
625 'asc' === $order_type ? give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) )
626
627 );
628
629 if ( ! $formatted ) {
630 $range = wp_strip_all_tags( $range );
631 }
632
633 return apply_filters( 'give_price_range', $range, $form_id, $low, $high );
634 }
635
636
637 /**
638 * Get Lowest Price ID
639 *
640 * Retrieves the ID for the cheapest price option of a variable donation form
641 *
642 * @since 1.5
643 *
644 * @param int $form_id ID of the donation
645 *
646 * @return int ID of the lowest price
647 */
648 function give_get_lowest_price_id( $form_id = 0 ) {
649
650 if ( empty( $form_id ) ) {
651 $form_id = get_the_ID();
652 }
653
654 if ( ! give_has_variable_prices( $form_id ) ) {
655 return give_get_form_price( $form_id );
656 }
657
658 $prices = give_get_variable_prices( $form_id );
659
660 $min = $min_id = 0;
661
662 if ( ! empty( $prices ) ) {
663
664 foreach ( $prices as $key => $price ) {
665
666 if ( empty( $price['_give_amount'] ) ) {
667 continue;
668 }
669
670 if ( ! isset( $min ) ) {
671 $min = $price['_give_amount'];
672 } else {
673 $min = min( $min, $price['_give_amount'] );
674 }
675
676 if ( $price['_give_amount'] == $min ) {
677 $min_id = $price['_give_id']['level_id'];
678 }
679 }
680 }
681
682 return (int) $min_id;
683 }
684
685 /**
686 * Retrieves cheapest price option of a variable priced form
687 *
688 * @since 1.0
689 *
690 * @param int $form_id ID of the form
691 *
692 * @return float Amount of the lowest price
693 */
694 function give_get_lowest_price_option( $form_id = 0 ) {
695 if ( empty( $form_id ) ) {
696 $form_id = get_the_ID();
697 }
698
699 if ( ! give_has_variable_prices( $form_id ) ) {
700 return give_get_form_price( $form_id );
701 }
702
703 if ( ! ( $low = get_post_meta( $form_id, '_give_levels_minimum_amount', true ) ) ) {
704 // Backward compatibility.
705 $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
706 $low = ! empty( $prices ) ? min( $prices ) : 0;
707 }
708
709 return give_maybe_sanitize_amount( $low );
710 }
711
712 /**
713 * Retrieves most expensive price option of a variable priced form
714 *
715 * @since 1.0
716 *
717 * @param int $form_id ID of the form
718 *
719 * @return float Amount of the highest price
720 */
721 function give_get_highest_price_option( $form_id = 0 ) {
722
723 if ( empty( $form_id ) ) {
724 $form_id = get_the_ID();
725 }
726
727 if ( ! give_has_variable_prices( $form_id ) ) {
728 return give_get_form_price( $form_id );
729 }
730
731 if ( ! ( $high = get_post_meta( $form_id, '_give_levels_maximum_amount', true ) ) ) {
732 // Backward compatibility.
733 $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
734 $high = ! empty( $prices ) ? max( $prices ) : 0;
735 }
736
737 return give_maybe_sanitize_amount( $high );
738 }
739
740 /**
741 * Returns the price of a form, but only for non-variable priced forms.
742 *
743 * @since 1.0
744 *
745 * @param int $form_id ID number of the form to retrieve a price for
746 *
747 * @return mixed string|int Price of the form
748 */
749 function give_get_form_price( $form_id = 0 ) {
750
751 if ( empty( $form_id ) ) {
752 return false;
753 }
754
755 $form = new Give_Donate_Form( $form_id );
756
757 return $form->__get( 'price' );
758 }
759
760 /**
761 * Returns the minimum price amount of a form, only enforced for the custom amount input.
762 *
763 * @since 1.3.6
764 *
765 * @param int $form_id ID number of the form to retrieve the minimum price for
766 *
767 * @return mixed string|int Minimum price of the form
768 */
769 function give_get_form_minimum_price( $form_id = 0 ) {
770
771 if ( empty( $form_id ) ) {
772 return false;
773 }
774
775 $form = new Give_Donate_Form( $form_id );
776
777 return $form->get_minimum_price();
778
779 }
780
781 /**
782 * Return the maximum price amount of form.
783 *
784 * @since 2.1
785 *
786 * @param int $form_id Donate Form ID
787 *
788 * @return bool|float
789 */
790 function give_get_form_maximum_price( $form_id = 0 ) {
791
792 if ( empty( $form_id ) ) {
793 return false;
794 }
795
796 $form = new Give_Donate_Form( $form_id );
797
798 return $form->get_maximum_price();
799 }
800
801 /**
802 * Displays a formatted price for a donation form
803 *
804 * @since 1.0
805 *
806 * @param int $form_id ID of the form price to show
807 * @param bool $echo Whether to echo or return the results
808 * @param bool|int $price_id Optional price id for variable pricing
809 *
810 * @return int $formatted_price
811 */
812 function give_price( $form_id = 0, $echo = true, $price_id = false ) {
813 $price = 0;
814
815 if ( empty( $form_id ) ) {
816 $form_id = get_the_ID();
817 }
818
819 if ( give_has_variable_prices( $form_id ) ) {
820
821 $prices = give_get_variable_prices( $form_id );
822
823 if ( false !== $price_id ) {
824
825 // loop through multi-prices to see which is default
826 foreach ( $prices as $price ) {
827 // this is the default price
828 if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
829 $price = (float) $price['_give_amount'];
830 };
831 }
832 } else {
833
834 $price = give_get_lowest_price_option( $form_id );
835 }
836 } else {
837
838 $price = give_get_form_price( $form_id );
839 }
840
841 $price = apply_filters( 'give_form_price', give_maybe_sanitize_amount( $price ), $form_id );
842 $formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>';
843 $formatted_price = apply_filters( 'give_form_price_after_html', $formatted_price, $form_id, $price );
844
845 if ( $echo ) {
846 echo $formatted_price;
847 } else {
848 return $formatted_price;
849 }
850 }
851
852 add_filter( 'give_form_price', 'give_format_amount', 10 );
853 add_filter( 'give_form_price', 'give_currency_filter', 20 );
854
855
856 /**
857 * Retrieves the amount of a variable price option
858 *
859 * @since 1.0
860 *
861 * @param int $form_id ID of the form
862 * @param int $price_id ID of the price option
863 *
864 * @return float $amount Amount of the price option
865 */
866 function give_get_price_option_amount( $form_id = 0, $price_id = 0 ) {
867 $prices = give_get_variable_prices( $form_id );
868
869 $amount = 0.00;
870
871 foreach ( $prices as $price ) {
872 if ( isset( $price['_give_id']['level_id'] ) && $price['_give_id']['level_id'] == $price_id ) {
873 $amount = isset( $price['_give_amount'] ) ? $price['_give_amount'] : 0.00;
874 break;
875 };
876 }
877
878 return apply_filters( 'give_get_price_option_amount', give_maybe_sanitize_amount( $amount ), $form_id, $price_id );
879 }
880
881 /**
882 * Returns the goal of a form
883 *
884 * @since 1.0
885 *
886 * @param int $form_id ID number of the form to retrieve a goal for
887 *
888 * @return mixed string|int Goal of the form
889 */
890 function give_get_form_goal( $form_id = 0 ) {
891
892 if ( empty( $form_id ) ) {
893 return false;
894 }
895
896 $form = new Give_Donate_Form( $form_id );
897
898 return $form->goal;
899
900 }
901
902 /**
903 * Returns the goal format of a form
904 *
905 * @since 2.0
906 *
907 * @param int $form_id ID number of the form to retrieve a goal for
908 *
909 * @return mixed string|int Goal of the form
910 */
911 function give_get_form_goal_format( $form_id = 0 ) {
912
913 if ( empty( $form_id ) ) {
914 return false;
915 }
916
917 return give_get_meta( $form_id, '_give_goal_format', true );
918
919 }
920
921 /**
922 * Display/Return a formatted goal for a donation form
923 *
924 * @since 1.0
925 *
926 * @param int $form_id ID of the form price to show
927 * @param bool $echo Whether to echo or return the results
928 *
929 * @return string $formatted_goal
930 */
931 function give_goal( $form_id = 0, $echo = true ) {
932
933 if ( empty( $form_id ) ) {
934 $form_id = get_the_ID();
935 }
936
937 $goal = give_get_form_goal( $form_id );
938 $goal_format = give_get_form_goal_format( $form_id );
939
940 if ( 'donation' === $goal_format ) {
941 $goal = "{$goal} donations";
942 } else {
943 $goal = apply_filters( 'give_form_goal', give_maybe_sanitize_amount( $goal ), $form_id );
944 }
945
946 $formatted_goal = sprintf(
947 '<span class="give_price" id="give_price_%1$s">%2$s</span>',
948 $form_id,
949 $goal
950 );
951 $formatted_goal = apply_filters( 'give_form_price_after_html', $formatted_goal, $form_id, $goal );
952
953 if ( $echo ) {
954 echo $formatted_goal;
955 } else {
956 return $formatted_goal;
957 }
958 }
959
960 add_filter( 'give_form_goal', 'give_format_amount', 10 );
961 add_filter( 'give_form_goal', 'give_currency_filter', 20 );
962
963
964 /**
965 * Checks if users can only donate when logged in
966 *
967 * @since 1.0
968 *
969 * @param int $form_id Give form ID
970 *
971 * @return bool $ret Whether or not the logged_in_only setting is set
972 */
973 function give_logged_in_only( $form_id ) {
974 // If _give_logged_in_only is set to enable then guest can donate from that specific form.
975 // Otherwise it is member only donation form.
976 $val = give_get_meta( $form_id, '_give_logged_in_only', true );
977 $val = ! empty( $val ) ? $val : 'enabled';
978
979 $ret = ! give_is_setting_enabled( $val );
980
981 return (bool) apply_filters( 'give_logged_in_only', $ret, $form_id );
982 }
983
984
985 /**
986 * Checks the option for the "Register / Login Option"
987 *
988 * @since 1.4.1
989 *
990 * @param int $form_id
991 *
992 * @return string
993 */
994 function give_show_login_register_option( $form_id ) {
995
996 $show_register_form = give_get_meta( $form_id, '_give_show_register_form', true );
997
998 return apply_filters( 'give_show_register_form', $show_register_form, $form_id );
999
1000 }
1001
1002
1003 /**
1004 * Get pre fill form field values.
1005 *
1006 * Note: this function will extract form field values from give_purchase session data.
1007 *
1008 * @since 1.8
1009 *
1010 * @param int $form_id Form ID.
1011 *
1012 * @return array
1013 */
1014 function _give_get_prefill_form_field_values( $form_id ) {
1015 $logged_in_donor_info = array();
1016
1017 if ( is_user_logged_in() ) :
1018 $donor_data = get_userdata( get_current_user_id() );
1019 $donor = new Give_Donor( get_current_user_id(), true );
1020 $donor_address = $donor->get_donor_address();
1021 $company_name = $donor->get_company_name();
1022
1023 $logged_in_donor_info = array(
1024 // First name.
1025 'give_first' => $donor_data->first_name,
1026
1027 // Last name.
1028 'give_last' => $donor_data->last_name,
1029
1030 // Title Prefix.
1031 'give_title' => $donor->get_meta( '_give_donor_title_prefix', true ),
1032
1033 // Company name.
1034 'company_name' => $company_name,
1035
1036 // Email.
1037 'give_email' => $donor_data->user_email,
1038
1039 // Street address 1.
1040 'card_address' => $donor_address['line1'],
1041
1042 // Street address 2.
1043 'card_address_2' => $donor_address['line2'],
1044
1045 // Country.
1046 'billing_country' => $donor_address['country'],
1047
1048 // State.
1049 'card_state' => $donor_address['state'],
1050
1051 // City.
1052 'card_city' => $donor_address['city'],
1053
1054 // Zipcode
1055 'card_zip' => $donor_address['zip'],
1056 );
1057 endif;
1058
1059 // Bailout: Auto fill form field values only form form which donor is donating.
1060 if (
1061 empty( $_GET['form-id'] )
1062 || ! $form_id
1063 || ( $form_id !== absint( $_GET['form-id'] ) )
1064 ) {
1065 return $logged_in_donor_info;
1066 }
1067
1068 // Get purchase data.
1069 $give_purchase_data = Give()->session->get( 'give_purchase' );
1070
1071 // Get donor info from form data.
1072 $give_donor_info_in_session = empty( $give_purchase_data['post_data'] )
1073 ? array()
1074 : $give_purchase_data['post_data'];
1075
1076 // Output.
1077 return wp_parse_args( $give_donor_info_in_session, $logged_in_donor_info );
1078 }
1079
1080 /**
1081 * Get donor count of form
1082 *
1083 * @since 2.1.0
1084 *
1085 * @param int $form_id
1086 * @param array $args
1087 *
1088 * @return int
1089 */
1090 function give_get_form_donor_count( $form_id, $args = array() ) {
1091 global $wpdb;
1092
1093 $cache_key = Give_Cache::get_key( "form_donor_count_{$form_id}", $args, false );
1094 $donor_count = absint( Give_Cache::get_db_query( $cache_key ) );
1095
1096 if ( $form_id && ! $donor_count ) {
1097 // Set arguments.
1098 $args = wp_parse_args(
1099 $args,
1100 array(
1101 'unique' => true,
1102 )
1103 );
1104
1105 $donation_meta_table = Give()->payment_meta->table_name;
1106 $donation_id_col_name = Give()->payment_meta->get_meta_type() . '_id';
1107
1108 $distinct = $args['unique'] ? 'DISTINCT meta_value' : 'meta_value';
1109
1110 $query = $wpdb->prepare(
1111 "
1112 SELECT COUNT({$distinct})
1113 FROM {$donation_meta_table}
1114 WHERE meta_key=%s
1115 AND {$donation_id_col_name} IN(
1116 SELECT {$donation_id_col_name}
1117 FROM {$donation_meta_table} as pm
1118 INNER JOIN {$wpdb->posts} as p
1119 ON pm.{$donation_id_col_name}=p.ID
1120 WHERE pm.meta_key=%s
1121 AND pm.meta_value=%s
1122 AND p.post_status=%s
1123 )
1124 ",
1125 '_give_payment_donor_id',
1126 '_give_payment_form_id',
1127 $form_id,
1128 'publish'
1129 );
1130
1131 $donor_count = absint( $wpdb->get_var( $query ) );
1132 }
1133
1134
1135 /**
1136 * Filter the donor count
1137 *
1138 * @since 2.1.0
1139 */
1140 $donor_count = apply_filters( 'give_get_form_donor_count', $donor_count, $form_id, $args );
1141
1142 return $donor_count;
1143 }
1144
1145 /**
1146 * Verify the form status.
1147 *
1148 * @param int $form_id Donation Form ID.
1149 *
1150 * @since 2.1
1151 *
1152 * @return void
1153 */
1154 function give_set_form_closed_status( $form_id ) {
1155
1156 // Bailout.
1157 if ( empty( $form_id ) ) {
1158 return;
1159 }
1160
1161 $open_form = false;
1162 $is_goal_enabled = give_is_setting_enabled( give_get_meta( $form_id, '_give_goal_option', true, 'disabled' ) );
1163
1164 // Proceed, if the form goal is enabled.
1165 if ( $is_goal_enabled ) {
1166
1167 $close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $form_id, '_give_close_form_when_goal_achieved', true, 'disabled' ) );
1168
1169 // Proceed, if close form when goal achieved option is enabled.
1170 if ( $close_form_when_goal_achieved ) {
1171
1172 $form = new Give_Donate_Form( $form_id );
1173 $goal_progress_stats = give_goal_progress_stats( $form );
1174
1175 // Verify whether the form is closed or not after processing data.
1176 $closed = $goal_progress_stats['raw_goal'] <= $goal_progress_stats['raw_actual'];
1177
1178 // Update form meta if verified that the form is closed.
1179 if ( $closed ) {
1180 give_update_meta( $form_id, '_give_form_status', 'closed' );
1181 } else {
1182 $open_form = true;
1183 }
1184 } else {
1185 $open_form = true;
1186 }
1187 } else {
1188 $open_form = true;
1189 }
1190
1191 // If $open_form is true, then update form status to open.
1192 if ( $open_form ) {
1193 give_update_meta( $form_id, '_give_form_status', 'open' );
1194 }
1195 }
1196
1197 /**
1198 * Show Form Goal Stats in Admin ( Listing and Detail page )
1199 *
1200 * @param int $form_id Form ID.
1201 *
1202 * @since 2.1.0
1203 *
1204 * @return string
1205 */
1206 function give_admin_form_goal_stats( $form_id ) {
1207
1208 $html = '';
1209 $goal_stats = give_goal_progress_stats( $form_id );
1210 $percent_complete = round( ( $goal_stats['raw_actual'] / $goal_stats['raw_goal'] ), 3 ) * 100;
1211
1212 $html .= sprintf(
1213 '<div class="give-admin-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="%1$s">
1214 <span style="width:%1$s%%;"></span>
1215 </div>',
1216 esc_attr( $goal_stats['progress'] )
1217 );
1218
1219 $html .= sprintf(
1220 ( 'percentage' !== $goal_stats['format'] ) ?
1221 '<div class="give-goal-text"><span>%1$s</span> %2$s <a href="%3$s">%4$s</a> %5$s ' :
1222 '<div class="give-goal-text"><a href="%3$s">%1$s </a>',
1223 ( 'percentage' !== $goal_stats['format'] ) ? $goal_stats['actual'] : $percent_complete . '%',
1224 ( 'percentage' !== $goal_stats['format'] ) ? __( 'of', 'give' ) : '',
1225 esc_url( admin_url( "post.php?post={$form_id}&action=edit&give_tab=donation_goal_options" ) ),
1226 $goal_stats['goal'],
1227 ( 'donors' === $goal_stats['format'] ? __( 'Donors', 'give' ) : ( 'donation' === $goal_stats['format'] ? __( 'Donations', 'give' ) : '' ) )
1228 );
1229
1230 if ( $goal_stats['raw_actual'] >= $goal_stats['raw_goal'] ) {
1231 $html .= sprintf( '<span class="give-admin-goal-achieved"><span class="dashicons dashicons-star-filled"></span> %s</span>', __( 'Goal achieved', 'give' ) );
1232 }
1233
1234 $html .= '</div>';
1235
1236
1237 return $html;
1238 }
1239
1240 /**
1241 * Get the default donation form's level id.
1242 *
1243 * @since 2.2.0
1244 *
1245 * @param integer $form_id Donation Form ID.
1246 *
1247 * @return null | array
1248 */
1249 function give_form_get_default_level( $form_id ) {
1250 $default_level = null;
1251
1252 // If donation form has variable prices.
1253 if ( give_has_variable_prices( $form_id ) ) {
1254 /**
1255 * Filter the variable pricing
1256 *
1257 *
1258 * @since 1.0
1259 * @deprecated 2.2 Use give_get_donation_levels filter instead of give_form_variable_prices.
1260 * Check Give_Donate_Form::get_prices().
1261 *
1262 * @param array $prices Array of variable prices.
1263 * @param int $form Form ID.
1264 */
1265 $prices = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
1266
1267 // Go through each of the level and get the default level id.
1268 foreach ( $prices as $level ) {
1269 if (
1270 isset( $level['_give_default'] )
1271 && $level['_give_default'] === 'default'
1272 ) {
1273 $default_level = $level;
1274 }
1275 }
1276 }
1277
1278 /**
1279 * Filter the default donation level id.
1280 *
1281 * @since 2.2.0
1282 *
1283 * @param array $default_level Default level price data.
1284 * @param integer $form_id Donation form ID.
1285 */
1286 return apply_filters( 'give_form_get_default_level', $default_level, $form_id );
1287 }
1288
1289 /**
1290 * Get the default level id.
1291 *
1292 * @since 2.2.0
1293 *
1294 * @param array|integer $price_or_level_id Price level data.
1295 * @param boolean|integer $form_id Donation Form ID.
1296 *
1297 * @return boolean
1298 */
1299 function give_is_default_level_id( $price_or_level_id, $form_id = 0 ) {
1300 $is_default = false;
1301
1302 if (
1303 ! empty( $form_id )
1304 && is_numeric( $price_or_level_id )
1305 ) {
1306 // Get default level id.
1307 $form_price_data = give_form_get_default_level( $form_id );
1308
1309 $is_default = ! is_null( $form_price_data ) && ( $price_or_level_id === absint( $form_price_data['_give_id']['level_id'] ) );
1310 }
1311
1312 $is_default = false === $is_default && is_array( $price_or_level_id ) ?
1313 ( isset( $price_or_level_id['_give_default'] ) && $price_or_level_id['_give_default'] === 'default' )
1314 : $is_default;
1315
1316 /**
1317 * Allow developers to modify the default level id checks.
1318 *
1319 * @since 2.2.0
1320 *
1321 * @param bool $is_default True if it is default price level id otherwise false.
1322 * @param array|integer $price_or_level_id Price Data.
1323 */
1324 return apply_filters( 'give_is_default_level_id', $is_default, $price_or_level_id );
1325 }
1326
1327
1328 /**
1329 * Get Name Title Prefixes (a.k.a. Salutation) value.
1330 *
1331 * @param int $form_id Donation Form ID.
1332 *
1333 * @since 2.2.0
1334 *
1335 * @return mixed
1336 */
1337 function give_get_name_title_prefixes( $form_id = 0 ) {
1338
1339 $name_title_prefix = give_is_name_title_prefix_enabled( $form_id );
1340 $title_prefixes = give_get_option( 'title_prefixes', give_get_default_title_prefixes() );
1341
1342 // If form id exists, then fetch form specific title prefixes.
1343 if ( intval( $form_id ) > 0 && $name_title_prefix ) {
1344
1345 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1346 if ( 'global' !== $form_title_prefix ) {
1347 $form_title_prefixes = give_get_meta( $form_id, '_give_title_prefixes', true, give_get_default_title_prefixes() );
1348
1349 // Check whether the form based title prefixes exists or not.
1350 if ( is_array( $form_title_prefixes ) && count( $form_title_prefixes ) > 0 ) {
1351 $title_prefixes = $form_title_prefixes;
1352 }
1353 }
1354 }
1355
1356 return $title_prefixes;
1357 }
1358
1359 /**
1360 * Check whether the name title prefix is enabled or not.
1361 *
1362 * @param int $form_id Donation Form ID.
1363 * @param string $status Status to set status based on option value.
1364 *
1365 * @since 2.2.0
1366 *
1367 * @return bool
1368 */
1369 function give_is_name_title_prefix_enabled( $form_id = 0, $status = '' ) {
1370 if ( empty( $status ) ) {
1371 $status = array( 'required', 'optional' );
1372 } else {
1373 $status = array( $status );
1374 }
1375
1376 $title_prefix_status = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status );
1377
1378 if ( intval( $form_id ) > 0 ) {
1379 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1380
1381 if ( 'disabled' === $form_title_prefix ) {
1382 $title_prefix_status = false;
1383 } elseif ( in_array( $form_title_prefix, $status, true ) ) {
1384 $title_prefix_status = give_is_setting_enabled( $form_title_prefix, $status );
1385 }
1386 }
1387
1388 return $title_prefix_status;
1389
1390 }
1391
1392 /**
1393 * Get Donor Name with Title Prefix
1394 *
1395 * @param int|Give_Donor $donor Donor Information.
1396 *
1397 * @since 2.2.0
1398 *
1399 * @return object
1400 */
1401 function give_get_name_with_title_prefixes( $donor ) {
1402
1403 // Prepare Give_Donor object, if $donor is numeric.
1404 if ( is_numeric( $donor ) ) {
1405 $donor = new Give_Donor( $donor );
1406 }
1407
1408 $title_prefix = Give()->donor_meta->get_meta( $donor->id, '_give_donor_title_prefix', true );
1409
1410 // Update Donor name, if non empty title prefix.
1411 if ( ! empty( $title_prefix ) ) {
1412 $donor->name = give_get_donor_name_with_title_prefixes( $title_prefix, $donor->name );
1413 }
1414
1415 return $donor;
1416 }
1417
1418 /**
1419 * This function will generate donor name with title prefix if it is required.
1420 *
1421 * @param string $title_prefix Title Prefix of Donor
1422 * @param string $name Donor Name.
1423 *
1424 * @since 2.2.0
1425 *
1426 * @return string
1427 */
1428 function give_get_donor_name_with_title_prefixes( $title_prefix, $name ) {
1429
1430 $donor_name = $name;
1431
1432 if ( ! empty( $title_prefix ) && ! empty( $name ) ) {
1433 $donor_name = "{$title_prefix} {$name}";
1434 }
1435
1436 return trim( $donor_name );
1437 }
1438
1439 /**
1440 * This function will fetch the default list of title prefixes.
1441 *
1442 * @since 2.2.0
1443 *
1444 * @return array
1445 */
1446 function give_get_default_title_prefixes() {
1447 /**
1448 * Filter the data
1449 * Set default title prefixes.
1450 *
1451 * @since 2.2.0
1452 */
1453 return apply_filters(
1454 'give_get_default_title_prefixes',
1455 array(
1456 'Mr.' => __( 'Mr.', 'give' ),
1457 'Mrs.' => __( 'Mrs.', 'give' ),
1458 'Ms.' => __( 'Ms.', 'give' ),
1459 )
1460 );
1461 }
1462
1463 /**
1464 * This function will check whether the name title prefix field is required or not.
1465 *
1466 * @param int $form_id Donation Form ID.
1467 *
1468 * @since 2.2.0
1469 *
1470 * @return bool
1471 */
1472 function give_is_name_title_prefix_required( $form_id = 0 ) {
1473
1474 // Bail out, if name title prefix is not enabled.
1475 if ( ! give_is_name_title_prefix_enabled( $form_id ) ) {
1476 return false;
1477 }
1478
1479 $status = array( 'optional' );
1480 $is_optional = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status );
1481
1482 if ( intval( $form_id ) > 0 ) {
1483 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1484
1485 if ( 'required' === $form_title_prefix ) {
1486 $is_optional = false;
1487 } elseif ( 'optional' === $form_title_prefix ) {
1488 $is_optional = true;
1489 }
1490 }
1491
1492 return ( ! $is_optional );
1493 }