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