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