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