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