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