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