PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.2.4
GiveWP – Donation Plugin and Fundraising Platform v2.2.4
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
1491 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 return apply_filters( 'give_is_success_page', is_page( give_get_success_page_uri() ) );
122 }
123
124 /**
125 * Send To Success Page
126 *
127 * Sends the user to the success page.
128 *
129 * @param string $query_string
130 *
131 * @access public
132 * @since 1.0
133 * @return void
134 */
135 function give_send_to_success_page( $query_string = null ) {
136
137 $redirect = give_get_success_page_uri();
138
139 if ( $query_string ) {
140 $redirect .= $query_string;
141 }
142
143 $gateway = isset( $_REQUEST['give-gateway'] ) ? $_REQUEST['give-gateway'] : '';
144
145 wp_redirect( apply_filters( 'give_success_page_redirect', $redirect, $gateway, $query_string ) );
146 give_die();
147 }
148
149
150 /**
151 * Send back to donation form.
152 *
153 * Used to redirect a user back to the donation form if there are errors present.
154 *
155 * @param array|string $args
156 *
157 * @access public
158 * @since 1.0
159 * @return Void
160 */
161 function give_send_back_to_checkout( $args = array() ) {
162
163 $url = isset( $_POST['give-current-url'] ) ? sanitize_text_field( $_POST['give-current-url'] ) : '';
164 $form_id = 0;
165
166 // Set the form_id.
167 if ( isset( $_POST['give-form-id'] ) ) {
168 $form_id = sanitize_text_field( $_POST['give-form-id'] );
169 }
170
171 // Need a URL to continue. If none, redirect back to single form.
172 if ( empty( $url ) ) {
173 wp_safe_redirect( get_permalink( $form_id ) );
174 give_die();
175 }
176
177 $defaults = array(
178 'form-id' => (int) $form_id,
179 );
180
181 // Set the $level_id.
182 if ( isset( $_POST['give-price-id'] ) ) {
183 $defaults['level-id'] = sanitize_text_field( $_POST['give-price-id'] );
184 }
185
186 // Check for backward compatibility.
187 if ( is_string( $args ) ) {
188 $args = str_replace( '?', '', $args );
189 }
190
191 $args = wp_parse_args( $args, $defaults );
192
193 // Merge URL query with $args to maintain third-party URL parameters after redirect.
194 $url_data = wp_parse_url( $url );
195
196 // Check if an array to prevent notices before parsing.
197 if ( isset( $url_data['query'] ) && ! empty( $url_data['query'] ) ) {
198 parse_str( $url_data['query'], $query );
199
200 // Precaution: don't allow any CC info.
201 unset( $query['card_number'] );
202 unset( $query['card_cvc'] );
203
204 } else {
205 // No $url_data so pass empty array.
206 $query = array();
207 }
208
209 $new_query = array_merge( $args, $query );
210 $new_query_string = http_build_query( $new_query );
211
212 // Assemble URL parts.
213 $redirect = home_url( '/' . $url_data['path'] . '?' . $new_query_string . '#give-form-' . $form_id . '-wrap' );
214
215 // Redirect them.
216 wp_safe_redirect( apply_filters( 'give_send_back_to_checkout', $redirect, $args ) );
217 give_die();
218
219 }
220
221 /**
222 * Get Success Page URL
223 *
224 * Gets the success page URL.
225 *
226 * @param string $query_string
227 *
228 * @access public
229 * @since 1.0
230 * @return string
231 */
232 function give_get_success_page_url( $query_string = null ) {
233
234 $success_page = give_get_option( 'success_page', 0 );
235 $success_page = get_permalink( $success_page );
236
237 if ( $query_string ) {
238 $success_page .= $query_string;
239 }
240
241 return apply_filters( 'give_success_page_url', $success_page );
242
243 }
244
245 /**
246 * Get the URL of the Failed Donation Page.
247 *
248 * @since 1.0
249 *
250 * @param bool $extras Extras to append to the URL.
251 *
252 * @return mixed Full URL to the Failed Donation Page, if present, home page if it doesn't exist.
253 */
254 function give_get_failed_transaction_uri( $extras = false ) {
255 $give_options = give_get_settings();
256
257 // Remove question mark.
258 if ( 0 === strpos( $extras, '?' ) ) {
259 $extras = substr( $extras, 1 );
260 }
261
262 $extras_args = wp_parse_args( $extras );
263
264 // Set nonce if payment id exist in extra params.
265 if ( array_key_exists( 'payment-id', $extras_args ) ) {
266 $extras_args['_wpnonce'] = wp_create_nonce( "give-failed-donation-{$extras_args['payment-id']}" );
267 $extras = http_build_query( $extras_args );
268 }
269
270 $uri = ! empty( $give_options['failure_page'] ) ?
271 trailingslashit( get_permalink( $give_options['failure_page'] ) ) :
272 home_url();
273
274
275 if ( $extras ) {
276 $uri .= "?{$extras}";
277 }
278
279 return apply_filters( 'give_get_failed_transaction_uri', $uri );
280 }
281
282 /**
283 * Determines if we're currently on the Failed Donation Page.
284 *
285 * @since 1.0
286 * @return bool True if on the Failed Donation Page, false otherwise.
287 */
288 function give_is_failed_transaction_page() {
289 $give_options = give_get_settings();
290 $ret = isset( $give_options['failure_page'] ) ? is_page( $give_options['failure_page'] ) : false;
291
292 return apply_filters( 'give_is_failure_page', $ret );
293 }
294
295 /**
296 * Mark payments as Failed when returning to the Failed Donation Page
297 *
298 * @since 1.0
299 * @since 1.8.16 Add security check
300 *
301 * @return bool
302 */
303 function give_listen_for_failed_payments() {
304
305 $failed_page = give_get_option( 'failure_page', 0 );
306 $payment_id = ! empty( $_GET['payment-id'] ) ? absint( $_GET['payment-id'] ) : 0;
307 $nonce = ! empty( $_GET['_wpnonce'] ) ? give_clean( $_GET['_wpnonce'] ) : false;
308
309 // Bailout.
310 if ( ! $failed_page || ! is_page( $failed_page ) || ! $payment_id || ! $nonce ) {
311 return false;
312 }
313
314 // Security check.
315 if ( ! wp_verify_nonce( $nonce, "give-failed-donation-{$payment_id}" ) ) {
316 wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ) );
317 }
318
319 // Set payment status to failure
320 give_update_payment_status( $payment_id, 'failed' );
321 }
322
323 add_action( 'template_redirect', 'give_listen_for_failed_payments' );
324
325 /**
326 * Retrieve the Donation History page URI
327 *
328 * @access public
329 * @since 1.7
330 *
331 * @return string
332 */
333 function give_get_history_page_uri() {
334 $give_options = give_get_settings();
335
336 $history_page = isset( $give_options['history_page'] ) ? get_permalink( absint( $give_options['history_page'] ) ) : get_bloginfo( 'url' );
337
338 return apply_filters( 'give_get_history_page_uri', $history_page );
339 }
340
341 /**
342 * Check if a field is required
343 *
344 * @param string $field
345 * @param int $form_id
346 *
347 * @access public
348 * @since 1.0
349 * @return bool
350 */
351 function give_field_is_required( $field = '', $form_id ) {
352
353 $required_fields = give_get_required_fields( $form_id );
354
355 return array_key_exists( $field, $required_fields );
356 }
357
358 /**
359 * Record Donation In Log
360 *
361 * Stores log information for a donation.
362 *
363 * @since 1.0
364 *
365 * @param int $give_form_id Give Form ID.
366 * @param int $payment_id Payment ID.
367 * @param bool|int $price_id Price ID, if any.
368 * @param string|null $donation_date The date of the donation.
369 *
370 * @return void
371 */
372 function give_record_donation_in_log( $give_form_id = 0, $payment_id, $price_id = false, $donation_date = null ) {
373 $log_data = array(
374 'log_parent' => $payment_id,
375 'log_type' => 'sale',
376 'log_date' => isset( $donation_date ) ? $donation_date : null,
377 'log_date_gmt' => isset( $donation_date ) ? $donation_date : null,
378 );
379
380 $log_meta = array(
381 'form_id' => $give_form_id,
382 'price_id' => (int) $price_id,
383 );
384
385 Give()->logs->insert_log( $log_data, $log_meta );
386 }
387
388
389 /**
390 * Increases the donation total count of a donation form.
391 *
392 * @since 1.0
393 *
394 * @param int $form_id Give Form ID
395 * @param int $quantity Quantity to increase donation count by
396 *
397 * @return bool|int
398 */
399 function give_increase_donation_count( $form_id = 0, $quantity = 1 ) {
400 $quantity = (int) $quantity;
401
402 /** @var \Give_Donate_Form $form */
403 $form = new Give_Donate_Form( $form_id );
404
405 return $form->increase_sales( $quantity );
406 }
407
408 /**
409 * Decreases the sale count of a form. Primarily for when a donation is refunded.
410 *
411 * @since 1.0
412 *
413 * @param int $form_id Give Form ID
414 * @param int $quantity Quantity to increase donation count by
415 *
416 * @return bool|int
417 */
418 function give_decrease_donation_count( $form_id = 0, $quantity = 1 ) {
419 $quantity = (int) $quantity;
420
421 /** @var \Give_Donate_Form $form */
422 $form = new Give_Donate_Form( $form_id );
423
424 return $form->decrease_sales( $quantity );
425 }
426
427 /**
428 * Increases the total earnings of a form.
429 *
430 * @since 1.0
431 *
432 * @since 2.1 Pass donation id.
433 *
434 * @param int $give_form_id Give Form ID
435 * @param int $amount Earnings
436 * @param int $payment_id Donation ID.
437 *
438 * @return bool|int
439 */
440 function give_increase_earnings( $give_form_id = 0, $amount, $payment_id = 0 ) {
441 /** @var \Give_Donate_Form $form */
442 $form = new Give_Donate_Form( $give_form_id );
443
444 return $form->increase_earnings( $amount, $payment_id );
445 }
446
447 /**
448 * Decreases the total earnings of a form.
449 *
450 * Primarily for when a donation is refunded.
451 *
452 * @since 1.0
453 *
454 * @since 2.1 Pass donation id.
455 *
456 * @param int $form_id Give Form ID
457 * @param int $amount Earnings
458 * @param int $payment_id Donation ID.
459 *
460 * @return bool|int
461 */
462 function give_decrease_form_earnings( $form_id = 0, $amount, $payment_id = 0 ) {
463 /** @var \Give_Donate_Form $form */
464 $form = new Give_Donate_Form( $form_id );
465
466 return $form->decrease_earnings( $amount, $payment_id );
467 }
468
469
470 /**
471 * Returns the total earnings for a form.
472 *
473 * @since 1.0
474 *
475 * @param int $form_id Give Form ID
476 *
477 * @return int $earnings Earnings for a certain form
478 */
479 function give_get_form_earnings_stats( $form_id = 0 ) {
480 $give_form = new Give_Donate_Form( $form_id );
481
482 /**
483 * Filter the form earnings
484 *
485 * @since 1.8.17
486 */
487 return apply_filters( 'give_get_form_earnings_stats', $give_form->earnings, $form_id, $give_form );
488 }
489
490
491 /**
492 * Return the sales number for a form.
493 *
494 * @since 1.0
495 *
496 * @param int $give_form_id Give Form ID
497 *
498 * @return int $sales Amount of sales for a certain form
499 */
500 function give_get_form_sales_stats( $give_form_id = 0 ) {
501 $give_form = new Give_Donate_Form( $give_form_id );
502
503 return $give_form->sales;
504 }
505
506
507 /**
508 * Retrieves the average monthly sales for a specific donation form
509 *
510 * @since 1.0
511 *
512 * @param int $form_id Form ID
513 *
514 * @return float $sales Average monthly sales
515 */
516 function give_get_average_monthly_form_sales( $form_id = 0 ) {
517 $sales = give_get_form_sales_stats( $form_id );
518 $release_date = get_post_field( 'post_date', $form_id );
519
520 $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
521
522 $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
523
524 if ( $months > 0 ) {
525 $sales = ( $sales / $months );
526 }
527
528 return $sales;
529 }
530
531
532 /**
533 * Retrieves the average monthly earnings for a specific form
534 *
535 * @since 1.0
536 *
537 * @param int $form_id Form ID
538 *
539 * @return float $earnings Average monthly earnings
540 */
541 function give_get_average_monthly_form_earnings( $form_id = 0 ) {
542 $earnings = give_get_form_earnings_stats( $form_id );
543 $release_date = get_post_field( 'post_date', $form_id );
544
545 $diff = abs( current_time( 'timestamp' ) - strtotime( $release_date ) );
546
547 $months = floor( $diff / ( 30 * 60 * 60 * 24 ) ); // Number of months since publication
548
549 if ( $months > 0 ) {
550 $earnings = ( $earnings / $months );
551 }
552
553 return $earnings < 0 ? 0 : $earnings;
554 }
555
556
557 /**
558 * Get Price Option Name (Text)
559 *
560 * Retrieves the name of a variable price option.
561 *
562 * @since 1.0
563 *
564 * @param int $form_id ID of the donation form.
565 * @param int $price_id ID of the price option.
566 * @param int $payment_id payment ID for use in filters ( optional ).
567 * @param bool $use_fallback Outputs the level amount if no level text is provided.
568 *
569 * @return string $price_name Name of the price option
570 */
571 function give_get_price_option_name( $form_id = 0, $price_id = 0, $payment_id = 0, $use_fallback = true ) {
572
573 $prices = give_get_variable_prices( $form_id );
574 $price_name = '';
575
576 if ( false === $prices ) {
577 return $price_name;
578 }
579
580 foreach ( $prices as $price ) {
581
582 if ( intval( $price['_give_id']['level_id'] ) === intval( $price_id ) ) {
583
584 $price_text = isset( $price['_give_text'] ) ? $price['_give_text'] : '';
585 $price_fallback = $use_fallback ?
586 give_currency_filter(
587 give_format_amount(
588 $price['_give_amount'],
589 array( 'sanitize' => false )
590 ),
591 array( 'decode_currency' => true )
592 ) : '';
593 $price_name = ! empty( $price_text ) ? $price_text : $price_fallback;
594
595 }
596 }
597
598 return apply_filters( 'give_get_price_option_name', $price_name, $form_id, $payment_id, $price_id );
599 }
600
601
602 /**
603 * Retrieves a price from from low to high of a variable priced form
604 *
605 * @since 1.0
606 *
607 * @param int $form_id ID of the form
608 * @param bool $formatted Flag to decide which type of price range string return
609 *
610 * @return string $range A fully formatted price range
611 */
612 function give_price_range( $form_id = 0, $formatted = true ) {
613 $low = give_get_lowest_price_option( $form_id );
614 $high = give_get_highest_price_option( $form_id );
615 $order_type = ! empty( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'asc';
616
617 $range = sprintf(
618 '<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>',
619 'asc' === $order_type ? 'low' : 'high',
620 'asc' === $order_type ? give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ),
621 'asc' === $order_type ? 'high' : 'low',
622 'asc' === $order_type ? give_currency_filter( give_format_amount( $high, array( 'sanitize' => false ) ) ) : give_currency_filter( give_format_amount( $low, array( 'sanitize' => false ) ) )
623
624 );
625
626 if ( ! $formatted ) {
627 $range = wp_strip_all_tags( $range );
628 }
629
630 return apply_filters( 'give_price_range', $range, $form_id, $low, $high );
631 }
632
633
634 /**
635 * Get Lowest Price ID
636 *
637 * Retrieves the ID for the cheapest price option of a variable donation form
638 *
639 * @since 1.5
640 *
641 * @param int $form_id ID of the donation
642 *
643 * @return int ID of the lowest price
644 */
645 function give_get_lowest_price_id( $form_id = 0 ) {
646
647 if ( empty( $form_id ) ) {
648 $form_id = get_the_ID();
649 }
650
651 if ( ! give_has_variable_prices( $form_id ) ) {
652 return give_get_form_price( $form_id );
653 }
654
655 $prices = give_get_variable_prices( $form_id );
656
657 $min = $min_id = 0;
658
659 if ( ! empty( $prices ) ) {
660
661 foreach ( $prices as $key => $price ) {
662
663 if ( empty( $price['_give_amount'] ) ) {
664 continue;
665 }
666
667 if ( ! isset( $min ) ) {
668 $min = $price['_give_amount'];
669 } else {
670 $min = min( $min, $price['_give_amount'] );
671 }
672
673 if ( $price['_give_amount'] == $min ) {
674 $min_id = $price['_give_id']['level_id'];
675 }
676 }
677 }
678
679 return (int) $min_id;
680 }
681
682 /**
683 * Retrieves cheapest price option of a variable priced form
684 *
685 * @since 1.0
686 *
687 * @param int $form_id ID of the form
688 *
689 * @return float Amount of the lowest price
690 */
691 function give_get_lowest_price_option( $form_id = 0 ) {
692 if ( empty( $form_id ) ) {
693 $form_id = get_the_ID();
694 }
695
696 if ( ! give_has_variable_prices( $form_id ) ) {
697 return give_get_form_price( $form_id );
698 }
699
700 if ( ! ( $low = get_post_meta( $form_id, '_give_levels_minimum_amount', true ) ) ) {
701 // Backward compatibility.
702 $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
703 $low = ! empty( $prices ) ? min( $prices ) : 0;
704 }
705
706 return give_maybe_sanitize_amount( $low );
707 }
708
709 /**
710 * Retrieves most expensive price option of a variable priced form
711 *
712 * @since 1.0
713 *
714 * @param int $form_id ID of the form
715 *
716 * @return float Amount of the highest price
717 */
718 function give_get_highest_price_option( $form_id = 0 ) {
719
720 if ( empty( $form_id ) ) {
721 $form_id = get_the_ID();
722 }
723
724 if ( ! give_has_variable_prices( $form_id ) ) {
725 return give_get_form_price( $form_id );
726 }
727
728 if ( ! ( $high = get_post_meta( $form_id, '_give_levels_maximum_amount', true ) ) ) {
729 // Backward compatibility.
730 $prices = wp_list_pluck( give_get_variable_prices( $form_id ), '_give_amount' );
731 $high = ! empty( $prices ) ? max( $prices ) : 0;
732 }
733
734 return give_maybe_sanitize_amount( $high );
735 }
736
737 /**
738 * Returns the price of a form, but only for non-variable priced forms.
739 *
740 * @since 1.0
741 *
742 * @param int $form_id ID number of the form to retrieve a price for
743 *
744 * @return mixed string|int Price of the form
745 */
746 function give_get_form_price( $form_id = 0 ) {
747
748 if ( empty( $form_id ) ) {
749 return false;
750 }
751
752 $form = new Give_Donate_Form( $form_id );
753
754 return $form->__get( 'price' );
755 }
756
757 /**
758 * Returns the minimum price amount of a form, only enforced for the custom amount input.
759 *
760 * @since 1.3.6
761 *
762 * @param int $form_id ID number of the form to retrieve the minimum price for
763 *
764 * @return mixed string|int Minimum price of the form
765 */
766 function give_get_form_minimum_price( $form_id = 0 ) {
767
768 if ( empty( $form_id ) ) {
769 return false;
770 }
771
772 $form = new Give_Donate_Form( $form_id );
773
774 return $form->get_minimum_price();
775
776 }
777
778 /**
779 * Return the maximum price amount of form.
780 *
781 * @since 2.1
782 *
783 * @param int $form_id Donate Form ID
784 *
785 * @return bool|float
786 */
787 function give_get_form_maximum_price( $form_id = 0 ) {
788
789 if ( empty( $form_id ) ) {
790 return false;
791 }
792
793 $form = new Give_Donate_Form( $form_id );
794
795 return $form->get_maximum_price();
796 }
797
798 /**
799 * Displays a formatted price for a donation form
800 *
801 * @since 1.0
802 *
803 * @param int $form_id ID of the form price to show
804 * @param bool $echo Whether to echo or return the results
805 * @param bool|int $price_id Optional price id for variable pricing
806 *
807 * @return int $formatted_price
808 */
809 function give_price( $form_id = 0, $echo = true, $price_id = false ) {
810 $price = 0;
811
812 if ( empty( $form_id ) ) {
813 $form_id = get_the_ID();
814 }
815
816 if ( give_has_variable_prices( $form_id ) ) {
817
818 $prices = give_get_variable_prices( $form_id );
819
820 if ( false !== $price_id ) {
821
822 // loop through multi-prices to see which is default
823 foreach ( $prices as $price ) {
824 // this is the default price
825 if ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) {
826 $price = (float) $price['_give_amount'];
827 };
828 }
829 } else {
830
831 $price = give_get_lowest_price_option( $form_id );
832 }
833 } else {
834
835 $price = give_get_form_price( $form_id );
836 }
837
838 $price = apply_filters( 'give_form_price', give_maybe_sanitize_amount( $price ), $form_id );
839 $formatted_price = '<span class="give_price" id="give_price_' . $form_id . '">' . $price . '</span>';
840 $formatted_price = apply_filters( 'give_form_price_after_html', $formatted_price, $form_id, $price );
841
842 if ( $echo ) {
843 echo $formatted_price;
844 } else {
845 return $formatted_price;
846 }
847 }
848
849 add_filter( 'give_form_price', 'give_format_amount', 10 );
850 add_filter( 'give_form_price', 'give_currency_filter', 20 );
851
852
853 /**
854 * Retrieves the amount of a variable price option
855 *
856 * @since 1.0
857 *
858 * @param int $form_id ID of the form
859 * @param int $price_id ID of the price option
860 *
861 * @return float $amount Amount of the price option
862 */
863 function give_get_price_option_amount( $form_id = 0, $price_id = 0 ) {
864 $prices = give_get_variable_prices( $form_id );
865
866 $amount = 0.00;
867
868 foreach ( $prices as $price ) {
869 if ( isset( $price['_give_id']['level_id'] ) && $price['_give_id']['level_id'] == $price_id ) {
870 $amount = isset( $price['_give_amount'] ) ? $price['_give_amount'] : 0.00;
871 break;
872 };
873 }
874
875 return apply_filters( 'give_get_price_option_amount', give_maybe_sanitize_amount( $amount ), $form_id, $price_id );
876 }
877
878 /**
879 * Returns the goal of a form
880 *
881 * @since 1.0
882 *
883 * @param int $form_id ID number of the form to retrieve a goal for
884 *
885 * @return mixed string|int Goal of the form
886 */
887 function give_get_form_goal( $form_id = 0 ) {
888
889 if ( empty( $form_id ) ) {
890 return false;
891 }
892
893 $form = new Give_Donate_Form( $form_id );
894
895 return $form->goal;
896
897 }
898
899 /**
900 * Returns the goal format of a form
901 *
902 * @since 2.0
903 *
904 * @param int $form_id ID number of the form to retrieve a goal for
905 *
906 * @return mixed string|int Goal of the form
907 */
908 function give_get_form_goal_format( $form_id = 0 ) {
909
910 if ( empty( $form_id ) ) {
911 return false;
912 }
913
914 return give_get_meta( $form_id, '_give_goal_format', true );
915
916 }
917
918 /**
919 * Display/Return a formatted goal for a donation form
920 *
921 * @since 1.0
922 *
923 * @param int $form_id ID of the form price to show
924 * @param bool $echo Whether to echo or return the results
925 *
926 * @return string $formatted_goal
927 */
928 function give_goal( $form_id = 0, $echo = true ) {
929
930 if ( empty( $form_id ) ) {
931 $form_id = get_the_ID();
932 }
933
934 $goal = give_get_form_goal( $form_id );
935 $goal_format = give_get_form_goal_format( $form_id );
936
937 if ( 'donation' === $goal_format ) {
938 $goal = "{$goal} donations";
939 } else {
940 $goal = apply_filters( 'give_form_goal', give_maybe_sanitize_amount( $goal ), $form_id );
941 }
942
943 $formatted_goal = sprintf(
944 '<span class="give_price" id="give_price_%1$s">%2$s</span>',
945 $form_id,
946 $goal
947 );
948 $formatted_goal = apply_filters( 'give_form_price_after_html', $formatted_goal, $form_id, $goal );
949
950 if ( $echo ) {
951 echo $formatted_goal;
952 } else {
953 return $formatted_goal;
954 }
955 }
956
957 add_filter( 'give_form_goal', 'give_format_amount', 10 );
958 add_filter( 'give_form_goal', 'give_currency_filter', 20 );
959
960
961 /**
962 * Checks if users can only donate when logged in
963 *
964 * @since 1.0
965 *
966 * @param int $form_id Give form ID
967 *
968 * @return bool $ret Whether or not the logged_in_only setting is set
969 */
970 function give_logged_in_only( $form_id ) {
971 // If _give_logged_in_only is set to enable then guest can donate from that specific form.
972 // Otherwise it is member only donation form.
973 $val = give_get_meta( $form_id, '_give_logged_in_only', true );
974 $val = ! empty( $val ) ? $val : 'enabled';
975
976 $ret = ! give_is_setting_enabled( $val );
977
978 return (bool) apply_filters( 'give_logged_in_only', $ret, $form_id );
979 }
980
981
982 /**
983 * Checks the option for the "Register / Login Option"
984 *
985 * @since 1.4.1
986 *
987 * @param int $form_id
988 *
989 * @return string
990 */
991 function give_show_login_register_option( $form_id ) {
992
993 $show_register_form = give_get_meta( $form_id, '_give_show_register_form', true );
994
995 return apply_filters( 'give_show_register_form', $show_register_form, $form_id );
996
997 }
998
999
1000 /**
1001 * Get pre fill form field values.
1002 *
1003 * Note: this function will extract form field values from give_purchase session data.
1004 *
1005 * @since 1.8
1006 *
1007 * @param int $form_id Form ID.
1008 *
1009 * @return array
1010 */
1011 function _give_get_prefill_form_field_values( $form_id ) {
1012 $logged_in_donor_info = array();
1013
1014 if ( is_user_logged_in() ) :
1015 $donor_data = get_userdata( get_current_user_id() );
1016 $donor = new Give_Donor( get_current_user_id(), true );
1017 $donor_address = $donor->get_donor_address();
1018 $company_name = $donor->get_company_name();
1019
1020 $logged_in_donor_info = array(
1021 // First name.
1022 'give_first' => $donor_data->first_name,
1023
1024 // Last name.
1025 'give_last' => $donor_data->last_name,
1026
1027 // Title Prefix.
1028 'give_title' => $donor->get_meta( '_give_donor_title_prefix', true ),
1029
1030 // Company name.
1031 'company_name' => $company_name,
1032
1033 // Email.
1034 'give_email' => $donor_data->user_email,
1035
1036 // Street address 1.
1037 'card_address' => $donor_address['line1'],
1038
1039 // Street address 2.
1040 'card_address_2' => $donor_address['line2'],
1041
1042 // Country.
1043 'billing_country' => $donor_address['country'],
1044
1045 // State.
1046 'card_state' => $donor_address['state'],
1047
1048 // City.
1049 'card_city' => $donor_address['city'],
1050
1051 // Zipcode
1052 'card_zip' => $donor_address['zip'],
1053 );
1054 endif;
1055
1056 // Bailout: Auto fill form field values only form form which donor is donating.
1057 if (
1058 empty( $_GET['form-id'] )
1059 || ! $form_id
1060 || ( $form_id !== absint( $_GET['form-id'] ) )
1061 ) {
1062 return $logged_in_donor_info;
1063 }
1064
1065 // Get purchase data.
1066 $give_purchase_data = Give()->session->get( 'give_purchase' );
1067
1068 // Get donor info from form data.
1069 $give_donor_info_in_session = empty( $give_purchase_data['post_data'] )
1070 ? array()
1071 : $give_purchase_data['post_data'];
1072
1073 // Output.
1074 return wp_parse_args( $give_donor_info_in_session, $logged_in_donor_info );
1075 }
1076
1077 /**
1078 * Get donor count of form
1079 *
1080 * @since 2.1.0
1081 *
1082 * @param int $form_id
1083 * @param array $args
1084 *
1085 * @return int
1086 */
1087 function give_get_form_donor_count( $form_id, $args = array() ) {
1088 global $wpdb;
1089
1090 $cache_key = Give_Cache::get_key( "form_donor_count_{$form_id}", $args, false );
1091 $donor_count = absint( Give_Cache::get_db_query( $cache_key ) );
1092
1093 if ( $form_id && ! $donor_count ) {
1094 // Set arguments.
1095 $args = wp_parse_args(
1096 $args,
1097 array(
1098 'unique' => true,
1099 )
1100 );
1101
1102 $donation_meta_table = Give()->payment_meta->table_name;
1103 $donation_id_col_name = Give()->payment_meta->get_meta_type() . '_id';
1104
1105 $distinct = $args['unique'] ? 'DISTINCT meta_value' : 'meta_value';
1106
1107 $query = $wpdb->prepare(
1108 "
1109 SELECT COUNT({$distinct})
1110 FROM {$donation_meta_table}
1111 WHERE meta_key=%s
1112 AND {$donation_id_col_name} IN(
1113 SELECT {$donation_id_col_name}
1114 FROM {$donation_meta_table} as pm
1115 INNER JOIN {$wpdb->posts} as p
1116 ON pm.{$donation_id_col_name}=p.ID
1117 WHERE pm.meta_key=%s
1118 AND pm.meta_value=%s
1119 AND p.post_status=%s
1120 )
1121 ",
1122 '_give_payment_donor_id',
1123 '_give_payment_form_id',
1124 $form_id,
1125 'publish'
1126 );
1127
1128 $donor_count = absint( $wpdb->get_var( $query ) );
1129 }
1130
1131
1132 /**
1133 * Filter the donor count
1134 *
1135 * @since 2.1.0
1136 */
1137 $donor_count = apply_filters( 'give_get_form_donor_count', $donor_count, $form_id, $args );
1138
1139 return $donor_count;
1140 }
1141
1142 /**
1143 * Verify the form status.
1144 *
1145 * @param int $form_id Donation Form ID.
1146 *
1147 * @since 2.1
1148 *
1149 * @return void
1150 */
1151 function give_set_form_closed_status( $form_id ) {
1152
1153 // Bailout.
1154 if ( empty( $form_id ) ) {
1155 return;
1156 }
1157
1158 $open_form = false;
1159 $is_goal_enabled = give_is_setting_enabled( give_get_meta( $form_id, '_give_goal_option', true, 'disabled' ) );
1160
1161 // Proceed, if the form goal is enabled.
1162 if ( $is_goal_enabled ) {
1163
1164 $close_form_when_goal_achieved = give_is_setting_enabled( give_get_meta( $form_id, '_give_close_form_when_goal_achieved', true, 'disabled' ) );
1165
1166 // Proceed, if close form when goal achieved option is enabled.
1167 if ( $close_form_when_goal_achieved ) {
1168
1169 $form = new Give_Donate_Form( $form_id );
1170 $goal_progress_stats = give_goal_progress_stats( $form );
1171
1172 // Verify whether the form is closed or not after processing data.
1173 $closed = $goal_progress_stats['raw_goal'] <= $goal_progress_stats['raw_actual'];
1174
1175 // Update form meta if verified that the form is closed.
1176 if ( $closed ) {
1177 give_update_meta( $form_id, '_give_form_status', 'closed' );
1178 } else {
1179 $open_form = true;
1180 }
1181 } else {
1182 $open_form = true;
1183 }
1184 } else {
1185 $open_form = true;
1186 }
1187
1188 // If $open_form is true, then update form status to open.
1189 if ( $open_form ) {
1190 give_update_meta( $form_id, '_give_form_status', 'open' );
1191 }
1192 }
1193
1194 /**
1195 * Show Form Goal Stats in Admin ( Listing and Detail page )
1196 *
1197 * @param int $form_id Form ID.
1198 *
1199 * @since 2.1.0
1200 *
1201 * @return string
1202 */
1203 function give_admin_form_goal_stats( $form_id ) {
1204
1205 $html = '';
1206 $goal_stats = give_goal_progress_stats( $form_id );
1207 $percent_complete = round( ( $goal_stats['raw_actual'] / $goal_stats['raw_goal'] ), 3 ) * 100;
1208
1209 $html .= sprintf(
1210 '<div class="give-admin-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="%1$s">
1211 <span style="width:%1$s%%;"></span>
1212 </div>',
1213 esc_attr( $goal_stats['progress'] )
1214 );
1215
1216 $html .= sprintf(
1217 ( 'percentage' !== $goal_stats['format'] ) ?
1218 '<div class="give-goal-text"><span>%1$s</span> %2$s <a href="%3$s">%4$s</a> %5$s ' :
1219 '<div class="give-goal-text"><a href="%3$s">%1$s </a>',
1220 ( 'percentage' !== $goal_stats['format'] ) ? $goal_stats['actual'] : $percent_complete . '%',
1221 ( 'percentage' !== $goal_stats['format'] ) ? __( 'of', 'give' ) : '',
1222 esc_url( admin_url( "post.php?post={$form_id}&action=edit&give_tab=donation_goal_options" ) ),
1223 $goal_stats['goal'],
1224 ( 'donors' === $goal_stats['format'] ? __( 'Donors', 'give' ) : ( 'donation' === $goal_stats['format'] ? __( 'Donations', 'give' ) : '' ) )
1225 );
1226
1227 if ( $goal_stats['raw_actual'] >= $goal_stats['raw_goal'] ) {
1228 $html .= sprintf( '<span class="give-admin-goal-achieved"><span class="dashicons dashicons-star-filled"></span> %s</span>', __( 'Goal achieved', 'give' ) );
1229 }
1230
1231 $html .= '</div>';
1232
1233
1234 return $html;
1235 }
1236
1237 /**
1238 * Get the default donation form's level id.
1239 *
1240 * @since 2.2.0
1241 *
1242 * @param integer $form_id Donation Form ID.
1243 *
1244 * @return null | array
1245 */
1246 function give_form_get_default_level( $form_id ) {
1247 $default_level = null;
1248
1249 // If donation form has variable prices.
1250 if ( give_has_variable_prices( $form_id ) ) {
1251 /**
1252 * Filter the variable pricing
1253 *
1254 *
1255 * @since 1.0
1256 * @deprecated 2.2 Use give_get_donation_levels filter instead of give_form_variable_prices.
1257 * Check Give_Donate_Form::get_prices().
1258 *
1259 * @param array $prices Array of variable prices.
1260 * @param int $form Form ID.
1261 */
1262 $prices = apply_filters( 'give_form_variable_prices', give_get_variable_prices( $form_id ), $form_id );
1263
1264 // Go through each of the level and get the default level id.
1265 foreach ( $prices as $level ) {
1266 if (
1267 isset( $level['_give_default'] )
1268 && $level['_give_default'] === 'default'
1269 ) {
1270 $default_level = $level;
1271 }
1272 }
1273 }
1274
1275 /**
1276 * Filter the default donation level id.
1277 *
1278 * @since 2.2.0
1279 *
1280 * @param array $default_level Default level price data.
1281 * @param integer $form_id Donation form ID.
1282 */
1283 return apply_filters( 'give_form_get_default_level', $default_level, $form_id );
1284 }
1285
1286 /**
1287 * Get the default level id.
1288 *
1289 * @since 2.2.0
1290 *
1291 * @param array|integer $price_or_level_id Price level data.
1292 * @param boolean|integer $form_id Donation Form ID.
1293 *
1294 * @return boolean
1295 */
1296 function give_is_default_level_id( $price_or_level_id, $form_id = 0 ) {
1297 $is_default = false;
1298
1299 if (
1300 ! empty( $form_id )
1301 && is_numeric( $price_or_level_id )
1302 ) {
1303 // Get default level id.
1304 $form_price_data = give_form_get_default_level( $form_id );
1305
1306 $is_default = ! is_null( $form_price_data ) && ( $price_or_level_id === absint( $form_price_data['_give_id']['level_id'] ) );
1307 }
1308
1309 $is_default = false === $is_default && is_array( $price_or_level_id ) ?
1310 ( isset( $price_or_level_id['_give_default'] ) && $price_or_level_id['_give_default'] === 'default' )
1311 : $is_default;
1312
1313 /**
1314 * Allow developers to modify the default level id checks.
1315 *
1316 * @since 2.2.0
1317 *
1318 * @param bool $is_default True if it is default price level id otherwise false.
1319 * @param array|integer $price_or_level_id Price Data.
1320 */
1321 return apply_filters( 'give_is_default_level_id', $is_default, $price_or_level_id );
1322 }
1323
1324
1325 /**
1326 * Get Name Title Prefixes (a.k.a. Salutation) value.
1327 *
1328 * @param int $form_id Donation Form ID.
1329 *
1330 * @since 2.2.0
1331 *
1332 * @return mixed
1333 */
1334 function give_get_name_title_prefixes( $form_id = 0 ) {
1335
1336 $name_title_prefix = give_is_name_title_prefix_enabled( $form_id );
1337 $title_prefixes = give_get_option( 'title_prefixes', give_get_default_title_prefixes() );
1338
1339 // If form id exists, then fetch form specific title prefixes.
1340 if ( intval( $form_id ) > 0 && $name_title_prefix ) {
1341
1342 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1343 if ( 'global' !== $form_title_prefix ) {
1344 $form_title_prefixes = give_get_meta( $form_id, '_give_title_prefixes', true, give_get_default_title_prefixes() );
1345
1346 // Check whether the form based title prefixes exists or not.
1347 if ( is_array( $form_title_prefixes ) && count( $form_title_prefixes ) > 0 ) {
1348 $title_prefixes = $form_title_prefixes;
1349 }
1350 }
1351 }
1352
1353 return $title_prefixes;
1354 }
1355
1356 /**
1357 * Check whether the name title prefix is enabled or not.
1358 *
1359 * @param int $form_id Donation Form ID.
1360 * @param string $status Status to set status based on option value.
1361 *
1362 * @since 2.2.0
1363 *
1364 * @return bool
1365 */
1366 function give_is_name_title_prefix_enabled( $form_id = 0, $status = '' ) {
1367 if ( empty( $status ) ) {
1368 $status = array( 'required', 'optional' );
1369 } else {
1370 $status = array( $status );
1371 }
1372
1373 $title_prefix_status = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status );
1374
1375 if ( intval( $form_id ) > 0 ) {
1376 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1377
1378 if ( 'disabled' === $form_title_prefix ) {
1379 $title_prefix_status = false;
1380 } elseif ( in_array( $form_title_prefix, $status, true ) ) {
1381 $title_prefix_status = give_is_setting_enabled( $form_title_prefix, $status );
1382 }
1383 }
1384
1385 return $title_prefix_status;
1386
1387 }
1388
1389 /**
1390 * Get Donor Name with Title Prefix
1391 *
1392 * @param int|Give_Donor $donor Donor Information.
1393 *
1394 * @since 2.2.0
1395 *
1396 * @return object
1397 */
1398 function give_get_name_with_title_prefixes( $donor ) {
1399
1400 // Prepare Give_Donor object, if $donor is numeric.
1401 if ( is_numeric( $donor ) ) {
1402 $donor = new Give_Donor( $donor );
1403 }
1404
1405 $title_prefix = Give()->donor_meta->get_meta( $donor->id, '_give_donor_title_prefix', true );
1406
1407 // Update Donor name, if non empty title prefix.
1408 if ( ! empty( $title_prefix ) ) {
1409 $donor->name = give_get_donor_name_with_title_prefixes( $title_prefix, $donor->name );
1410 }
1411
1412 return $donor;
1413 }
1414
1415 /**
1416 * This function will generate donor name with title prefix if it is required.
1417 *
1418 * @param string $title_prefix Title Prefix of Donor
1419 * @param string $name Donor Name.
1420 *
1421 * @since 2.2.0
1422 *
1423 * @return string
1424 */
1425 function give_get_donor_name_with_title_prefixes( $title_prefix, $name ) {
1426
1427 $donor_name = $name;
1428
1429 if ( ! empty( $title_prefix ) && ! empty( $name ) ) {
1430 $donor_name = "{$title_prefix} {$name}";
1431 }
1432
1433 return trim( $donor_name );
1434 }
1435
1436 /**
1437 * This function will fetch the default list of title prefixes.
1438 *
1439 * @since 2.2.0
1440 *
1441 * @return array
1442 */
1443 function give_get_default_title_prefixes() {
1444 /**
1445 * Filter the data
1446 * Set default title prefixes.
1447 *
1448 * @since 2.2.0
1449 */
1450 return apply_filters(
1451 'give_get_default_title_prefixes',
1452 array(
1453 'Mr.' => __( 'Mr.', 'give' ),
1454 'Mrs.' => __( 'Mrs.', 'give' ),
1455 'Ms.' => __( 'Ms.', 'give' ),
1456 )
1457 );
1458 }
1459
1460 /**
1461 * This function will check whether the name title prefix field is required or not.
1462 *
1463 * @param int $form_id Donation Form ID.
1464 *
1465 * @since 2.2.0
1466 *
1467 * @return bool
1468 */
1469 function give_is_name_title_prefix_required( $form_id = 0 ) {
1470
1471 // Bail out, if name title prefix is not enabled.
1472 if ( ! give_is_name_title_prefix_enabled( $form_id ) ) {
1473 return false;
1474 }
1475
1476 $status = array( 'optional' );
1477 $is_optional = give_is_setting_enabled( give_get_option( 'name_title_prefix' ), $status );
1478
1479 if ( intval( $form_id ) > 0 ) {
1480 $form_title_prefix = give_get_meta( $form_id, '_give_name_title_prefix', true );
1481
1482 if ( 'required' === $form_title_prefix ) {
1483 $is_optional = false;
1484 } elseif ( 'optional' === $form_title_prefix ) {
1485 $is_optional = true;
1486 }
1487 }
1488
1489 return ( ! $is_optional );
1490 }
1491