PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
← All changes | includes/payments/functions.php +200 -79 2.3.1 → 4.18.0 View file →
@@ -9,8 +9,12 @@
9 9 * @since 1.0
10 10 */
11 11
12 12 // Exit if accessed directly.
13 +use Give\Donations\Models\Donation;
14 +use Give\Helpers\Form\Utils;
15 +use Give\ValueObjects\Money;
16 +
13 17 if ( ! defined( 'ABSPATH' ) ) {
14 18 exit;
15 19 }
16 20
@@ -43,9 +47,9 @@
43 47 * }
44 48 *
45 49 * @return array $payments Payments retrieved from the database
46 50 */
47 -function give_get_payments( $args = array() ) {
51 +function give_get_payments( $args = [] ) {
48 52
49 53 // Fallback to post objects to ensure backwards compatibility.
50 54 if ( ! isset( $args['output'] ) ) {
51 55 $args['output'] = 'posts';
@@ -86,14 +90,14 @@
86 90 break;
87 91
88 92 case 'key':
89 93 $payment = give_get_payments(
90 - array(
94 + [
91 95 'meta_key' => '_give_payment_purchase_key',
92 96 'meta_value' => $value,
93 97 'posts_per_page' => 1,
94 98 'fields' => 'ids',
95 - )
99 + ]
96 100 );
97 101
98 102 if ( $payment ) {
99 103 $payment = new Give_Payment( $payment[0] );
@@ -102,14 +106,14 @@
102 106 break;
103 107
104 108 case 'payment_number':
105 109 $payment = give_get_payments(
106 - array(
110 + [
107 111 'meta_key' => '_give_payment_number',
108 112 'meta_value' => $value,
109 113 'posts_per_page' => 1,
110 114 'fields' => 'ids',
111 - )
115 + ]
112 116 );
113 117
114 118 if ( $payment ) {
115 119 $payment = new Give_Payment( $payment[0] );
@@ -128,8 +132,55 @@
128 132 return false;
129 133 }
130 134
131 135 /**
136 + * Derive Campaign ID from Form ID
137 + *
138 + * Automatically derives a campaign ID from a form ID when a campaign exists for that form.
139 + * This function provides a centralized way to handle campaign ID derivation across different
140 + * parts of the codebase.
141 + *
142 + * @since 4.3.2
143 + *
144 + * @param int $form_id The form ID to derive the campaign ID from.
145 + *
146 + * @return int The derived campaign ID, or 0 if no campaign is found.
147 + */
148 +function give_derive_campaign_id_from_form_id( $form_id ) {
149 + $derived_campaign_id = 0;
150 +
151 + // Method 1: Use modern Campaign model (if available)
152 + if ( class_exists( 'Give\\Campaigns\\Models\\Campaign' ) ) {
153 + $campaign = \Give\Campaigns\Models\Campaign::findByFormId( $form_id );
154 + if ( $campaign ) {
155 + $derived_campaign_id = $campaign->id;
156 + }
157 + }
158 +
159 + // Method 2: Fallback to direct database query
160 + if ( ! $derived_campaign_id && class_exists( 'Give\\Framework\\Database\\DB' ) ) {
161 + $campaign_id_from_db = \Give\Framework\Database\DB::table( 'give_campaign_forms' )
162 + ->where( 'form_id', $form_id )
163 + ->value( 'campaign_id' );
164 +
165 + if ( $campaign_id_from_db ) {
166 + $derived_campaign_id = (int) $campaign_id_from_db;
167 + }
168 + }
169 +
170 + /**
171 + * Filter the derived campaign ID.
172 + *
173 + * @since 4.3.2
174 + *
175 + * @param int $derived_campaign_id The derived campaign ID.
176 + * @param int $form_id The form ID used for derivation.
177 + * @return int The derived campaign ID.
178 + */
179 + return apply_filters( 'give_derive_campaign_id_from_form_id', $derived_campaign_id, $form_id );
180 +}
181 +
182 +/**
132 183 * Insert Payment
133 184 *
134 185 * @since 1.0
135 186 *
@@ -136,9 +187,9 @@
136 187 * @param array $payment_data Arguments passed.
137 188 *
138 189 * @return int|bool Payment ID if payment is inserted, false otherwise.
139 190 */
140 -function give_insert_payment( $payment_data = array() ) {
191 +function give_insert_payment( $payment_data = [] ) {
141 192
142 193 if ( empty( $payment_data ) ) {
143 194 return false;
144 195 }
@@ -166,12 +217,21 @@
166 217 $payment->user_info = $payment_data['user_info'];
167 218 $payment->gateway = $gateway;
168 219 $payment->form_title = $form_title;
169 220 $payment->form_id = $form_id;
221 +
222 + // Set campaign_id: Use explicit value if provided, otherwise derive from form_id
223 + if ( ! empty( $payment_data['campaign_id'] ) ) {
224 + $payment->campaign_id = $payment_data['campaign_id'];
225 + } else {
226 + // Try to automatically derive campaign_id from form_id
227 + $derived_campaign_id = give_derive_campaign_id_from_form_id( $form_id );
228 + $payment->campaign_id = $derived_campaign_id;
229 + }
230 +
170 231 $payment->price_id = $price_id;
171 232 $payment->donor_id = ( ! empty( $payment_data['donor_id'] ) ? $payment_data['donor_id'] : '' );
172 233 $payment->user_id = $payment_data['user_info']['id'];
173 - $payment->email = $payment_data['user_email'];
174 234 $payment->first_name = $payment_data['user_info']['first_name'];
175 235 $payment->last_name = $payment_data['user_info']['last_name'];
176 236 $payment->title_prefix = ! empty( $payment_data['user_info']['title'] ) ? $payment_data['user_info']['title'] : '';
177 237 $payment->email = $payment_data['user_info']['email'];
@@ -180,12 +240,12 @@
180 240 $payment->mode = ( ! empty( $payment_data['mode'] ) ? (string) $payment_data['mode'] : ( give_is_test_mode() ? 'test' : 'live' ) );
181 241 $payment->parent_payment = ! empty( $payment_data['parent'] ) ? absint( $payment_data['parent'] ) : '';
182 242
183 243 // Add the donation.
184 - $args = array(
244 + $args = [
185 245 'price' => $payment->total,
186 246 'price_id' => $payment->price_id,
187 - );
247 + ];
188 248
189 249 $payment->add_donation( $payment->form_id, $args );
190 250
191 251 // Set date if present.
@@ -198,12 +258,14 @@
198 258
199 259 // Setup donor id.
200 260 $payment_data['user_info']['donor_id'] = $payment->donor_id;
201 261
202 - // Set donation id to purchase session.
203 - $purchase_session = Give()->session->get( 'give_purchase' );
204 - $purchase_session['donation_id'] = $payment->ID;
205 - Give()->session->set( 'give_purchase', $purchase_session );
262 + // Set donation id to purchase session only donor session for donation exist.
263 + $purchase_session = (array) Give()->session->get( 'give_purchase' );
264 + if ( $purchase_session && array_key_exists( 'purchase_key', $purchase_session ) ) {
265 + $purchase_session['donation_id'] = $payment->ID;
266 + Give()->session->set( 'give_purchase', $purchase_session );
267 + }
206 268
207 269 /**
208 270 * Fires while inserting payments.
209 271 *
@@ -236,9 +298,9 @@
236 298 $form_id = intval( $payment_data['post_data']['give-form-id'] );
237 299 $price_id = isset( $payment_data['post_data']['give-price-id'] ) ? $payment_data['post_data']['give-price-id'] : '';
238 300
239 301 // Collect payment data.
240 - $insert_payment_data = array(
302 + $insert_payment_data = [
241 303 'price' => $payment_data['price'],
242 304 'give_form_title' => $payment_data['post_data']['give-form-title'],
243 305 'give_form_id' => $form_id,
244 306 'give_price_id' => $price_id,
@@ -248,9 +310,9 @@
248 310 'currency' => give_get_currency( $form_id, $payment_data ),
249 311 'user_info' => $payment_data['user_info'],
250 312 'status' => 'pending',
251 313 'gateway' => 'paypal',
252 - );
314 + ];
253 315
254 316 /**
255 317 * Filter the payment params.
256 318 *
@@ -313,12 +375,13 @@
313 375 $donor = new Give_Donor( $payment->donor_id );
314 376
315 377 // Only undo donations that aren't these statuses.
316 378 $dont_undo_statuses = apply_filters(
317 - 'give_undo_donation_statuses', array(
379 + 'give_undo_donation_statuses',
380 + [
318 381 'pending',
319 382 'cancelled',
320 - )
383 + ]
321 384 );
322 385
323 386 if ( ! in_array( $status, $dont_undo_statuses ) ) {
324 387 give_undo_donation( $payment_id );
@@ -324,9 +387,9 @@
324 387 give_undo_donation( $payment_id );
325 388 }
326 389
327 390 // Only undo donations that aren't these statuses.
328 - $status_to_decrease_stats = apply_filters( 'give_decrease_donor_statuses', array( 'publish' ) );
391 + $status_to_decrease_stats = apply_filters( 'give_decrease_donor_statuses', [ 'publish' ] );
329 392
330 393 if ( in_array( $status, $status_to_decrease_stats ) ) {
331 394
332 395 // Only decrease earnings if they haven't already been decreased (or were never increased for this payment).
@@ -357,16 +420,13 @@
357 420 // Remove the payment ID from the donor.
358 421 $donor->remove_payment( $payment_id );
359 422 }
360 423
361 - // Remove the payment.
362 - wp_delete_post( $payment_id, true );
424 + // Remove the payment.
425 + wp_delete_post( $payment_id, true );
363 426
364 - Give()->payment_meta->delete_all_meta( $payment_id );
427 + Give()->payment_meta->delete_all_meta( $payment_id );
365 428
366 - // Remove related sale log entries.
367 - Give()->logs->delete_logs( $payment_id );
368 -
369 429 /**
370 430 * Fires after payment deleted.
371 431 *
372 432 * @param int $payment_id Payment ID.
@@ -417,9 +477,9 @@
417 477 * @since 1.0
418 478 *
419 479 * @return object $stats Contains the number of payments per payment status.
420 480 */
421 -function give_count_payments( $args = array() ) {
481 +function give_count_payments( $args = [] ) {
422 482 // Backward compatibility.
423 483 if ( ! empty( $args['start-date'] ) ) {
424 484 $args['start_date'] = $args['start-date'];
425 485 unset( $args['start-date'] );
@@ -492,9 +552,9 @@
492 552 function give_get_payment_status( $payment_id, $return_label = false ) {
493 553
494 554 if ( ! is_numeric( $payment_id ) ) {
495 555 if (
496 - $payment_id instanceof Give_Payment
556 + $payment_id instanceof Give_Payment
497 557 || $payment_id instanceof WP_Post
498 558 ) {
499 559 $payment_id = $payment_id->ID;
500 560 }
@@ -535,9 +595,9 @@
535 595 *
536 596 * @return array $payment_status All the available payment statuses.
537 597 */
538 598 function give_get_payment_statuses() {
539 - $payment_statuses = array(
599 + $payment_statuses = [
540 600 'pending' => __( 'Pending', 'give' ),
541 601 'publish' => __( 'Complete', 'give' ),
542 602 'refunded' => __( 'Refunded', 'give' ),
543 603 'failed' => __( 'Failed', 'give' ),
@@ -545,9 +605,9 @@
545 605 'abandoned' => __( 'Abandoned', 'give' ),
546 606 'preapproval' => __( 'Pre-Approved', 'give' ),
547 607 'processing' => __( 'Processing', 'give' ),
548 608 'revoked' => __( 'Revoked', 'give' ),
549 - );
609 + ];
550 610
551 611 return apply_filters( 'give_payment_statuses', $payment_statuses );
552 612 }
553 613
@@ -576,23 +636,25 @@
576 636 * @param int $hour Hour number. Default is null.
577 637 *
578 638 * @since 1.0
579 639 *
640 + * @since 2.12.0 default value for the $day parameter is removed to prevent PHP8 warnings.
641 + *
580 642 * @return int $earnings Earnings
581 643 */
582 -function give_get_earnings_by_date( $day = null, $month_num, $year = null, $hour = null ) {
644 +function give_get_earnings_by_date( $day, $month_num, $year = null, $hour = null ) {
583 645 // This is getting deprecated soon. Use Give_Payment_Stats with the get_earnings() method instead.
584 646 global $wpdb;
585 647
586 - $args = array(
648 + $args = [
587 649 'post_type' => 'give_payment',
588 650 'nopaging' => true,
589 651 'year' => $year,
590 652 'monthnum' => $month_num,
591 - 'post_status' => array( 'publish' ),
653 + 'post_status' => [ 'publish' ],
592 654 'fields' => 'ids',
593 655 'update_post_term_cache' => false,
594 - );
656 + ];
595 657 if ( ! empty( $day ) ) {
596 658 $args['day'] = $day;
597 659 }
598 660
@@ -652,29 +714,29 @@
652 714 */
653 715 function give_get_sales_by_date( $day = null, $month_num = null, $year = null, $hour = null ) {
654 716
655 717 // This is getting deprecated soon. Use Give_Payment_Stats with the get_sales() method instead.
656 - $args = array(
718 + $args = [
657 719 'post_type' => 'give_payment',
658 720 'nopaging' => true,
659 721 'year' => $year,
660 722 'fields' => 'ids',
661 - 'post_status' => array( 'publish' ),
723 + 'post_status' => [ 'publish' ],
662 724 'update_post_meta_cache' => false,
663 725 'update_post_term_cache' => false,
664 - );
726 + ];
665 727
666 728 $show_free = apply_filters( 'give_sales_by_date_show_free', true, $args );
667 729
668 730 if ( false === $show_free ) {
669 - $args['meta_query'] = array(
670 - array(
731 + $args['meta_query'] = [
732 + [
671 733 'key' => '_give_payment_total',
672 734 'value' => 0,
673 735 'compare' => '>',
674 736 'type' => 'NUMERIC',
675 - ),
676 - );
737 + ],
738 + ];
677 739 }
678 740
679 741 if ( ! empty( $month_num ) ) {
680 742 $args['monthnum'] = $month_num;
@@ -762,9 +824,9 @@
762 824 */
763 825 function give_get_total_earnings( $recalculate = false ) {
764 826
765 827 $total = get_option( 'give_earnings_total', 0 );
766 - $meta_table = __give_v20_bc_table_details( 'payment' );
828 + $meta_table = give_v20_bc_table_details( 'payment' );
767 829
768 830 // Calculate total earnings.
769 831 if ( ! $total || $recalculate ) {
770 832 global $wpdb;
@@ -771,14 +833,15 @@
771 833
772 834 $total = (float) 0;
773 835
774 836 $args = apply_filters(
775 - 'give_get_total_earnings_args', array(
837 + 'give_get_total_earnings_args',
838 + [
776 839 'offset' => 0,
777 840 'number' => - 1,
778 - 'status' => array( 'publish' ),
841 + 'status' => [ 'publish' ],
779 842 'fields' => 'ids',
780 - )
843 + ]
781 844 );
782 845
783 846 $payments = give_get_payments( $args );
784 847 if ( $payments ) {
@@ -884,13 +947,13 @@
884 947 * @return array $user_info User Info Meta Values.
885 948 */
886 949 function give_get_payment_meta_user_info( $payment_id ) {
887 950 $donor_id = 0;
888 - $donor_info = array(
951 + $donor_info = [
889 952 'first_name' => give_get_meta( $payment_id, '_give_donor_billing_first_name', true ),
890 953 'last_name' => give_get_meta( $payment_id, '_give_donor_billing_last_name', true ),
891 954 'email' => give_get_meta( $payment_id, '_give_donor_billing_donor_email', true ),
892 - );
955 + ];
893 956
894 957 if ( empty( $donor_info['first_name'] ) ) {
895 958 $donor_id = give_get_payment_donor_id( $payment_id );
896 959 $donor_info['first_name'] = Give()->donor_meta->get_meta( $donor_id, '_give_donor_first_name', true );
@@ -975,9 +1038,9 @@
975 1038 * @return int $user_id User ID.
976 1039 */
977 1040 function give_get_payment_user_id( $payment_id ) {
978 1041 global $wpdb;
979 - $paymentmeta_table = Give()->payment_meta->table_name;
1042 + $paymentmeta_table = Give()->payment_meta->table_name;
980 1043 $donationmeta_primary_key = Give()->payment_meta->get_meta_type() . '_id';
981 1044
982 1045 return (int) $wpdb->get_var(
983 1046 $wpdb->prepare(
@@ -1150,9 +1213,9 @@
1150 1213 * @since 1.8.17 Added filter and internally use functions.
1151 1214 *
1152 1215 * @return string $amount Fully formatted donation amount.
1153 1216 */
1154 -function give_donation_amount( $donation_id, $format_args = array() ) {
1217 +function give_donation_amount( $donation_id, $format_args = [] ) {
1155 1218 if ( ! $donation_id ) {
1156 1219 return '';
1157 1220 } elseif ( ! is_numeric( $donation_id ) && ( $donation_id instanceof Give_Payment ) ) {
1158 1221 $donation_id = $donation_id->ID;
@@ -1161,17 +1224,17 @@
1161 1224 $amount = $formatted_amount = give_get_payment_total( $donation_id );
1162 1225 $currency_code = give_get_payment_currency_code( $donation_id );
1163 1226
1164 1227 if ( is_bool( $format_args ) ) {
1165 - $format_args = array(
1228 + $format_args = [
1166 1229 'currency' => (bool) $format_args,
1167 1230 'amount' => (bool) $format_args,
1168 - );
1231 + ];
1169 1232 }
1170 1233
1171 1234 $format_args = wp_parse_args(
1172 1235 $format_args,
1173 - array(
1236 + [
1174 1237 'currency' => false,
1175 1238 'amount' => false,
1176 1239
1177 1240 // Define context of donation amount, by default keep $type as blank.
@@ -1180,9 +1243,9 @@
1180 1243 // different currency other than base currency, in that case for correct
1181 1244 // report calculation based on base currency we will need to return donation
1182 1245 // base amount and not the converted amount .
1183 1246 'type' => '',
1184 - )
1247 + ]
1185 1248 );
1186 1249
1187 1250 if ( $format_args['amount'] || $format_args['currency'] ) {
1188 1251
@@ -1190,12 +1253,12 @@
1190 1253
1191 1254 $formatted_amount = give_format_amount(
1192 1255 $amount,
1193 1256 ! is_array( $format_args['amount'] ) ?
1194 - array(
1257 + [
1195 1258 'sanitize' => false,
1196 1259 'currency' => $currency_code,
1197 - ) :
1260 + ] :
1198 1261 $format_args['amount']
1199 1262 );
1200 1263 }
1201 1264
@@ -1202,9 +1265,9 @@
1202 1265 if ( $format_args['currency'] ) {
1203 1266 $formatted_amount = give_currency_filter(
1204 1267 $formatted_amount,
1205 1268 ! is_array( $format_args['currency'] ) ?
1206 - array( 'currency_code' => $currency_code ) :
1269 + [ 'currency_code' => $currency_code ] :
1207 1270 $format_args['currency']
1208 1271 );
1209 1272 }
1210 1273 }
@@ -1238,9 +1301,9 @@
1238 1301 */
1239 1302 function give_payment_subtotal( $payment_id = 0 ) {
1240 1303 $subtotal = give_get_payment_subtotal( $payment_id );
1241 1304
1242 - return give_currency_filter( give_format_amount( $subtotal, array( 'sanitize' => false ) ), array( 'currency_code' => give_get_payment_currency_code( $payment_id ) ) );
1305 + return give_currency_filter( give_format_amount( $subtotal, [ 'sanitize' => false ] ), [ 'currency_code' => give_get_payment_currency_code( $payment_id ) ] );
1243 1306 }
1244 1307
1245 1308 /**
1246 1309 * Get Payment Subtotal
@@ -1312,9 +1375,9 @@
1312 1375 */
1313 1376 function give_get_donation_id_by_key( $key ) {
1314 1377 global $wpdb;
1315 1378
1316 - $meta_table = __give_v20_bc_table_details( 'payment' );
1379 + $meta_table = give_v20_bc_table_details( 'payment' );
1317 1380
1318 1381 $purchase = $wpdb->get_var(
1319 1382 $wpdb->prepare(
1320 1383 "
@@ -1348,9 +1411,9 @@
1348 1411 * @return int $purchase Donation ID.
1349 1412 */
1350 1413 function give_get_purchase_id_by_transaction_id( $key ) {
1351 1414 global $wpdb;
1352 - $meta_table = __give_v20_bc_table_details( 'payment' );
1415 + $meta_table = give_v20_bc_table_details( 'payment' );
1353 1416
1354 1417 $purchase = $wpdb->get_var( $wpdb->prepare( "SELECT {$meta_table['column']['id']} FROM {$meta_table['name']} WHERE meta_key = '_give_payment_transaction_id' AND meta_value = %s LIMIT 1", $key ) );
1355 1418
1356 1419 if ( $purchase != null ) {
@@ -1370,9 +1433,9 @@
1370 1433 *
1371 1434 * @return array $notes Donation Notes
1372 1435 */
1373 1436 function give_get_payment_notes( $payment_id = 0, $search = '' ) {
1374 - return Give_Comment::get( $payment_id,'payment', array(), $search );
1437 + return Give_Comment::get( $payment_id, 'payment', [], $search );
1375 1438 }
1376 1439
1377 1440
1378 1441 /**
@@ -1417,9 +1480,9 @@
1417 1480
1418 1481 if ( is_numeric( $note ) ) {
1419 1482 if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) {
1420 1483 $note = get_comment( $note );
1421 - } else{
1484 + } else {
1422 1485 $note = Give()->comment->db->get( $note );
1423 1486 }
1424 1487 }
1425 1488
@@ -1433,14 +1496,15 @@
1433 1496 $date_format = give_date_format() . ', ' . get_option( 'time_format' );
1434 1497
1435 1498 $delete_note_url = wp_nonce_url(
1436 1499 add_query_arg(
1437 - array(
1500 + [
1438 1501 'give-action' => 'delete_payment_note',
1439 1502 'note_id' => $note->comment_ID,
1440 1503 'payment_id' => $payment_id,
1441 - )
1442 - ), 'give_delete_payment_note_' . $note->comment_ID
1504 + ]
1505 + ),
1506 + 'give_delete_payment_note_' . $note->comment_ID
1443 1507 );
1444 1508
1445 1509 $note_html = '<div class="give-payment-note" id="give-payment-note-' . $note->comment_ID . '">';
1446 1510 $note_html .= '<p>';
@@ -1483,13 +1547,16 @@
1483 1547 * @param array $args a. only_level = If set to true will only return the level name if multi-level
1484 1548 * enabled. b. separator = The separator between the Form Title and the Donation
1485 1549 * Level.
1486 1550 *
1551 + * @since 4.16.5 lookup option values by level ID (price_id) first before falling back to amount-based matching
1552 + * @since 4.14.5 add currency compatibility to determine whether the level is same as donation amount
1553 + * @since 3.18.0 check if donation form is V3 form
1487 1554 * @since 1.5
1488 1555 *
1489 1556 * @return string $form_title Returns the full title if $only_level is false, otherwise returns the levels title.
1490 1557 */
1491 -function give_get_donation_form_title( $donation_id, $args = array() ) {
1558 +function give_get_donation_form_title( $donation_id, $args = [] ) {
1492 1559 // Backward compatibility.
1493 1560 if ( ! is_numeric( $donation_id ) && $donation_id instanceof Give_Payment ) {
1494 1561 $donation_id = $donation_id->ID;
1495 1562 }
@@ -1497,18 +1564,63 @@
1497 1564 if ( ! $donation_id ) {
1498 1565 return '';
1499 1566 }
1500 1567
1501 - $defaults = array(
1568 + $defaults = [
1502 1569 'only_level' => false,
1503 1570 'separator' => '',
1504 - );
1571 + ];
1505 1572
1506 1573 $args = wp_parse_args( $args, $defaults );
1507 1574
1508 - $form_id = give_get_payment_form_id( $donation_id );
1575 + $form_id = give_get_payment_form_id( $donation_id );
1576 + $form_title = give_get_meta( $donation_id, '_give_payment_form_title', true );
1577 +
1578 + // Check if the donation form is V3 form
1579 + if (Utils::isV3Form($form_id)) {
1580 + $default_currency = give_get_option('currency'); // default currency
1581 + $payment_currency = give_get_payment_currency_code($donation_id); // donation currency
1582 + $options = give()->form_meta->get_meta($form_id, '_give_donation_levels', true) ?? [];
1583 + $donation = Donation::find($donation_id);
1584 + $price_id = give_get_meta( $donation_id, '_give_payment_price_id', true );
1585 +
1586 + if ( $price_id !== '' && $price_id !== 'custom' && ! is_null( $price_id ) ) {
1587 + foreach ( $options as $option ) {
1588 + if ( isset( $option['_give_id']['level_id'] ) && (string) $option['_give_id']['level_id'] === (string) $price_id ) {
1589 + $form_title = sprintf('%s %s %s', $form_title, $args['separator'], $option['_give_text']);
1590 + return apply_filters('give_get_donation_form_title', $form_title, $donation_id);
1591 + }
1592 + }
1593 + }
1594 +
1595 + // Different currencies - use exchange rate from currency switcher
1596 + $exchange_rate = give_get_meta($donation_id, '_give_cs_exchange_rate', true);
1597 +
1598 + foreach ( $options as $option ) {
1599 + if (!isset($option['_give_amount'], $option['_give_text'])) {
1600 + continue;
1601 + }
1602 +
1603 + $matched = false;
1604 +
1605 + if ($default_currency === $payment_currency) {
1606 + $matched = Money::of($option['_give_amount'], $default_currency)->getMinorAmount() == $donation->amount->getAmount();
1607 + } elseif (!empty($exchange_rate) && is_numeric($exchange_rate)) {
1608 + $option_amount_converted = $option['_give_amount'] * $exchange_rate;
1609 + $option_money = Money::of($option_amount_converted, $payment_currency);
1610 + $diff = abs($option_money->getMinorAmount() - $donation->amount->getAmount());
1611 + $matched = ($diff < 2);
1612 + }
1613 +
1614 + if ($matched) {
1615 + $form_title = sprintf('%s %s %s', $form_title, $args['separator'], $option['_give_text']);
1616 + return apply_filters('give_get_donation_form_title', $form_title, $donation_id);
1617 + }
1618 + }
1619 + }
1620 +
1509 1621 $price_id = give_get_meta( $donation_id, '_give_payment_price_id', true );
1510 - $form_title = give_get_meta( $donation_id, '_give_payment_form_title', true );
1622 +
1511 1623 $only_level = $args['only_level'];
1512 1624 $separator = $args['separator'];
1513 1625 $level_label = '';
1514 1626
@@ -1513,15 +1625,16 @@
1513 1625 $level_label = '';
1514 1626
1515 1627 $cache_key = Give_Cache::get_key(
1516 1628 'give_forms',
1517 - array(
1629 + [
1518 1630 $form_id,
1519 1631 $price_id,
1520 1632 $form_title,
1521 1633 $only_level,
1522 1634 $separator,
1523 - ), false
1635 + ],
1636 + false
1524 1637 );
1525 1638
1526 1639 $form_title_html = Give_Cache::get_db_query( $cache_key );
1527 1640
@@ -1627,9 +1740,9 @@
1627 1740 * @since 1.6
1628 1741 *
1629 1742 * @return string
1630 1743 */
1631 -function give_get_form_dropdown( $args = array(), $echo = false ) {
1744 +function give_get_form_dropdown( $args = [], $echo = false ) {
1632 1745 $form_dropdown_html = Give()->html->forms_dropdown( $args );
1633 1746
1634 1747 if ( ! $echo ) {
1635 1748 return $form_dropdown_html;
@@ -1644,12 +1757,13 @@
1644 1757 * @param array $args Arguments for form dropdown.
1645 1758 * @param bool $echo This parameter decide if print form dropdown html output or not.
1646 1759 *
1647 1760 * @since 1.6
1761 + * @since 2.12.0 Show "Custom" choice in select field if donation created with cusotm amount
1648 1762 *
1649 1763 * @return string|bool
1650 1764 */
1651 -function give_get_form_variable_price_dropdown( $args = array(), $echo = false ) {
1765 +function give_get_form_variable_price_dropdown( $args = [], $echo = false ) {
1652 1766
1653 1767 // Check for give form id.
1654 1768 if ( empty( $args['id'] ) ) {
1655 1769 return false;
@@ -1662,25 +1776,30 @@
1662 1776 return false;
1663 1777 }
1664 1778
1665 1779 $variable_prices = $form->get_prices();
1666 - $variable_price_options = array();
1780 + $variable_price_options = [];
1667 1781
1668 1782 // Check if multi donation form support custom donation or not.
1669 - if ( $form->is_custom_price_mode() ) {
1783 + // Check if donation amount is a custom or not.
1784 + if (
1785 + $form->is_custom_price_mode() ||
1786 + 'custom' === $args['selected']
1787 + ) {
1670 1788 $variable_price_options['custom'] = _x( 'Custom', 'custom donation dropdown item', 'give' );
1671 1789 }
1672 1790
1673 1791 // Get variable price and ID from variable price array.
1674 1792 foreach ( $variable_prices as $variable_price ) {
1675 - $variable_price_options[ $variable_price['_give_id']['level_id'] ] = ! empty( $variable_price['_give_text'] ) ? $variable_price['_give_text'] : give_currency_filter( give_format_amount( $variable_price['_give_amount'], array( 'sanitize' => false ) ) );
1793 + $variable_price_options[ $variable_price['_give_id']['level_id'] ] = ! empty( $variable_price['_give_text'] ) ? $variable_price['_give_text'] : give_currency_filter( give_format_amount( $variable_price['_give_amount'], [ 'sanitize' => false ] ) );
1676 1794 }
1677 1795
1678 1796 // Update options.
1679 1797 $args = array_merge(
1680 - $args, array(
1798 + $args,
1799 + [
1681 1800 'options' => $variable_price_options,
1682 - )
1801 + ]
1683 1802 );
1684 1803
1685 1804 // Generate select html.
1686 1805 $form_dropdown_html = Give()->html->select( $args );
@@ -1784,9 +1903,10 @@
1784 1903 * @param bool
1785 1904 * @param int $donation_id
1786 1905 */
1787 1906 return apply_filters(
1788 - 'give_is_donation_completed', (bool) $wpdb->get_var(
1907 + 'give_is_donation_completed',
1908 + (bool) $wpdb->get_var(
1789 1909 $wpdb->prepare(
1790 1910 "
1791 1911 SELECT meta_value
1792 1912 FROM {$wpdb->donationmeta}
@@ -1801,9 +1921,10 @@
1801 1921 'publish',
1802 1922 $donation_id,
1803 1923 '_give_completed_date'
1804 1924 )
1805 - ), $donation_id
1925 + ),
1926 + $donation_id
1806 1927 );
1807 1928 }
1808 1929
1809 1930 /**
@@ -1816,9 +1937,9 @@
1816 1937 */
1817 1938 function give_is_anonymous_donation( $donation_id ) {
1818 1939 $value = false;
1819 1940
1820 - if( (int) give_get_meta( $donation_id, '_give_anonymous_donation', true ) ){
1941 + if ( (int) give_get_meta( $donation_id, '_give_anonymous_donation', true ) ) {
1821 1942 $value = true;
1822 1943 }
1823 1944
1824 1945 return $value;