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