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