| @@ -3,14 +3,18 @@ | ||
| 3 | 3 | * Payment Functions |
| 4 | 4 | * |
| 5 | 5 | * @package Give |
| 6 | 6 | * @subpackage Payments |
| 7 | - * @copyright Copyright (c) 2016, WordImpress | |
| 7 | + * @copyright Copyright (c) 2016, GiveWP | |
| 8 | 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 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,8 +258,15 @@ | ||
| 198 | 258 | |
| 199 | 259 | // Setup donor id. |
| 200 | 260 | $payment_data['user_info']['donor_id'] = $payment->donor_id; |
| 201 | 261 | |
| 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 | + } | |
| 268 | + | |
| 202 | 269 | /** |
| 203 | 270 | * Fires while inserting payments. |
| 204 | 271 | * |
| 205 | 272 | * @since 1.0 |
| @@ -231,9 +298,9 @@ | ||
| 231 | 298 | $form_id = intval( $payment_data['post_data']['give-form-id'] ); |
| 232 | 299 | $price_id = isset( $payment_data['post_data']['give-price-id'] ) ? $payment_data['post_data']['give-price-id'] : ''; |
| 233 | 300 | |
| 234 | 301 | // Collect payment data. |
| 235 | - $insert_payment_data = array( | |
| 302 | + $insert_payment_data = [ | |
| 236 | 303 | 'price' => $payment_data['price'], |
| 237 | 304 | 'give_form_title' => $payment_data['post_data']['give-form-title'], |
| 238 | 305 | 'give_form_id' => $form_id, |
| 239 | 306 | 'give_price_id' => $price_id, |
| @@ -243,9 +310,9 @@ | ||
| 243 | 310 | 'currency' => give_get_currency( $form_id, $payment_data ), |
| 244 | 311 | 'user_info' => $payment_data['user_info'], |
| 245 | 312 | 'status' => 'pending', |
| 246 | 313 | 'gateway' => 'paypal', |
| 247 | - ); | |
| 314 | + ]; | |
| 248 | 315 | |
| 249 | 316 | /** |
| 250 | 317 | * Filter the payment params. |
| 251 | 318 | * |
| @@ -308,12 +375,13 @@ | ||
| 308 | 375 | $donor = new Give_Donor( $payment->donor_id ); |
| 309 | 376 | |
| 310 | 377 | // Only undo donations that aren't these statuses. |
| 311 | 378 | $dont_undo_statuses = apply_filters( |
| 312 | - 'give_undo_donation_statuses', array( | |
| 379 | + 'give_undo_donation_statuses', | |
| 380 | + [ | |
| 313 | 381 | 'pending', |
| 314 | 382 | 'cancelled', |
| 315 | - ) | |
| 383 | + ] | |
| 316 | 384 | ); |
| 317 | 385 | |
| 318 | 386 | if ( ! in_array( $status, $dont_undo_statuses ) ) { |
| 319 | 387 | give_undo_donation( $payment_id ); |
| @@ -319,9 +387,9 @@ | ||
| 319 | 387 | give_undo_donation( $payment_id ); |
| 320 | 388 | } |
| 321 | 389 | |
| 322 | 390 | // Only undo donations that aren't these statuses. |
| 323 | - $status_to_decrease_stats = apply_filters( 'give_decrease_donor_statuses', array( 'publish' ) ); | |
| 391 | + $status_to_decrease_stats = apply_filters( 'give_decrease_donor_statuses', [ 'publish' ] ); | |
| 324 | 392 | |
| 325 | 393 | if ( in_array( $status, $status_to_decrease_stats ) ) { |
| 326 | 394 | |
| 327 | 395 | // Only decrease earnings if they haven't already been decreased (or were never increased for this payment). |
| @@ -352,16 +420,13 @@ | ||
| 352 | 420 | // Remove the payment ID from the donor. |
| 353 | 421 | $donor->remove_payment( $payment_id ); |
| 354 | 422 | } |
| 355 | 423 | |
| 356 | - // Remove the payment. | |
| 357 | - wp_delete_post( $payment_id, true ); | |
| 424 | + // Remove the payment. | |
| 425 | + wp_delete_post( $payment_id, true ); | |
| 358 | 426 | |
| 359 | - Give()->payment_meta->delete_all_meta( $payment_id ); | |
| 427 | + Give()->payment_meta->delete_all_meta( $payment_id ); | |
| 360 | 428 | |
| 361 | - // Remove related sale log entries. | |
| 362 | - Give()->logs->delete_logs( $payment_id ); | |
| 363 | - | |
| 364 | 429 | /** |
| 365 | 430 | * Fires after payment deleted. |
| 366 | 431 | * |
| 367 | 432 | * @param int $payment_id Payment ID. |
| @@ -412,9 +477,9 @@ | ||
| 412 | 477 | * @since 1.0 |
| 413 | 478 | * |
| 414 | 479 | * @return object $stats Contains the number of payments per payment status. |
| 415 | 480 | */ |
| 416 | -function give_count_payments( $args = array() ) { | |
| 481 | +function give_count_payments( $args = [] ) { | |
| 417 | 482 | // Backward compatibility. |
| 418 | 483 | if ( ! empty( $args['start-date'] ) ) { |
| 419 | 484 | $args['start_date'] = $args['start-date']; |
| 420 | 485 | unset( $args['start-date'] ); |
| @@ -487,9 +552,9 @@ | ||
| 487 | 552 | function give_get_payment_status( $payment_id, $return_label = false ) { |
| 488 | 553 | |
| 489 | 554 | if ( ! is_numeric( $payment_id ) ) { |
| 490 | 555 | if ( |
| 491 | - $payment_id instanceof Give_Payment | |
| 556 | + $payment_id instanceof Give_Payment | |
| 492 | 557 | || $payment_id instanceof WP_Post |
| 493 | 558 | ) { |
| 494 | 559 | $payment_id = $payment_id->ID; |
| 495 | 560 | } |
| @@ -530,9 +595,9 @@ | ||
| 530 | 595 | * |
| 531 | 596 | * @return array $payment_status All the available payment statuses. |
| 532 | 597 | */ |
| 533 | 598 | function give_get_payment_statuses() { |
| 534 | - $payment_statuses = array( | |
| 599 | + $payment_statuses = [ | |
| 535 | 600 | 'pending' => __( 'Pending', 'give' ), |
| 536 | 601 | 'publish' => __( 'Complete', 'give' ), |
| 537 | 602 | 'refunded' => __( 'Refunded', 'give' ), |
| 538 | 603 | 'failed' => __( 'Failed', 'give' ), |
| @@ -540,9 +605,9 @@ | ||
| 540 | 605 | 'abandoned' => __( 'Abandoned', 'give' ), |
| 541 | 606 | 'preapproval' => __( 'Pre-Approved', 'give' ), |
| 542 | 607 | 'processing' => __( 'Processing', 'give' ), |
| 543 | 608 | 'revoked' => __( 'Revoked', 'give' ), |
| 544 | - ); | |
| 609 | + ]; | |
| 545 | 610 | |
| 546 | 611 | return apply_filters( 'give_payment_statuses', $payment_statuses ); |
| 547 | 612 | } |
| 548 | 613 | |
| @@ -571,23 +636,25 @@ | ||
| 571 | 636 | * @param int $hour Hour number. Default is null. |
| 572 | 637 | * |
| 573 | 638 | * @since 1.0 |
| 574 | 639 | * |
| 640 | + * @since 2.12.0 default value for the $day parameter is removed to prevent PHP8 warnings. | |
| 641 | + * | |
| 575 | 642 | * @return int $earnings Earnings |
| 576 | 643 | */ |
| 577 | -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 ) { | |
| 578 | 645 | // This is getting deprecated soon. Use Give_Payment_Stats with the get_earnings() method instead. |
| 579 | 646 | global $wpdb; |
| 580 | 647 | |
| 581 | - $args = array( | |
| 648 | + $args = [ | |
| 582 | 649 | 'post_type' => 'give_payment', |
| 583 | 650 | 'nopaging' => true, |
| 584 | 651 | 'year' => $year, |
| 585 | 652 | 'monthnum' => $month_num, |
| 586 | - 'post_status' => array( 'publish' ), | |
| 653 | + 'post_status' => [ 'publish' ], | |
| 587 | 654 | 'fields' => 'ids', |
| 588 | 655 | 'update_post_term_cache' => false, |
| 589 | - ); | |
| 656 | + ]; | |
| 590 | 657 | if ( ! empty( $day ) ) { |
| 591 | 658 | $args['day'] = $day; |
| 592 | 659 | } |
| 593 | 660 | |
| @@ -647,29 +714,29 @@ | ||
| 647 | 714 | */ |
| 648 | 715 | function give_get_sales_by_date( $day = null, $month_num = null, $year = null, $hour = null ) { |
| 649 | 716 | |
| 650 | 717 | // This is getting deprecated soon. Use Give_Payment_Stats with the get_sales() method instead. |
| 651 | - $args = array( | |
| 718 | + $args = [ | |
| 652 | 719 | 'post_type' => 'give_payment', |
| 653 | 720 | 'nopaging' => true, |
| 654 | 721 | 'year' => $year, |
| 655 | 722 | 'fields' => 'ids', |
| 656 | - 'post_status' => array( 'publish' ), | |
| 723 | + 'post_status' => [ 'publish' ], | |
| 657 | 724 | 'update_post_meta_cache' => false, |
| 658 | 725 | 'update_post_term_cache' => false, |
| 659 | - ); | |
| 726 | + ]; | |
| 660 | 727 | |
| 661 | 728 | $show_free = apply_filters( 'give_sales_by_date_show_free', true, $args ); |
| 662 | 729 | |
| 663 | 730 | if ( false === $show_free ) { |
| 664 | - $args['meta_query'] = array( | |
| 665 | - array( | |
| 731 | + $args['meta_query'] = [ | |
| 732 | + [ | |
| 666 | 733 | 'key' => '_give_payment_total', |
| 667 | 734 | 'value' => 0, |
| 668 | 735 | 'compare' => '>', |
| 669 | 736 | 'type' => 'NUMERIC', |
| 670 | - ), | |
| 671 | - ); | |
| 737 | + ], | |
| 738 | + ]; | |
| 672 | 739 | } |
| 673 | 740 | |
| 674 | 741 | if ( ! empty( $month_num ) ) { |
| 675 | 742 | $args['monthnum'] = $month_num; |
| @@ -757,9 +824,9 @@ | ||
| 757 | 824 | */ |
| 758 | 825 | function give_get_total_earnings( $recalculate = false ) { |
| 759 | 826 | |
| 760 | 827 | $total = get_option( 'give_earnings_total', 0 ); |
| 761 | - $meta_table = __give_v20_bc_table_details( 'payment' ); | |
| 828 | + $meta_table = give_v20_bc_table_details( 'payment' ); | |
| 762 | 829 | |
| 763 | 830 | // Calculate total earnings. |
| 764 | 831 | if ( ! $total || $recalculate ) { |
| 765 | 832 | global $wpdb; |
| @@ -766,14 +833,15 @@ | ||
| 766 | 833 | |
| 767 | 834 | $total = (float) 0; |
| 768 | 835 | |
| 769 | 836 | $args = apply_filters( |
| 770 | - 'give_get_total_earnings_args', array( | |
| 837 | + 'give_get_total_earnings_args', | |
| 838 | + [ | |
| 771 | 839 | 'offset' => 0, |
| 772 | 840 | 'number' => - 1, |
| 773 | - 'status' => array( 'publish' ), | |
| 841 | + 'status' => [ 'publish' ], | |
| 774 | 842 | 'fields' => 'ids', |
| 775 | - ) | |
| 843 | + ] | |
| 776 | 844 | ); |
| 777 | 845 | |
| 778 | 846 | $payments = give_get_payments( $args ); |
| 779 | 847 | if ( $payments ) { |
| @@ -879,13 +947,13 @@ | ||
| 879 | 947 | * @return array $user_info User Info Meta Values. |
| 880 | 948 | */ |
| 881 | 949 | function give_get_payment_meta_user_info( $payment_id ) { |
| 882 | 950 | $donor_id = 0; |
| 883 | - $donor_info = array( | |
| 951 | + $donor_info = [ | |
| 884 | 952 | 'first_name' => give_get_meta( $payment_id, '_give_donor_billing_first_name', true ), |
| 885 | 953 | 'last_name' => give_get_meta( $payment_id, '_give_donor_billing_last_name', true ), |
| 886 | 954 | 'email' => give_get_meta( $payment_id, '_give_donor_billing_donor_email', true ), |
| 887 | - ); | |
| 955 | + ]; | |
| 888 | 956 | |
| 889 | 957 | if ( empty( $donor_info['first_name'] ) ) { |
| 890 | 958 | $donor_id = give_get_payment_donor_id( $payment_id ); |
| 891 | 959 | $donor_info['first_name'] = Give()->donor_meta->get_meta( $donor_id, '_give_donor_first_name', true ); |
| @@ -970,9 +1038,9 @@ | ||
| 970 | 1038 | * @return int $user_id User ID. |
| 971 | 1039 | */ |
| 972 | 1040 | function give_get_payment_user_id( $payment_id ) { |
| 973 | 1041 | global $wpdb; |
| 974 | - $paymentmeta_table = Give()->payment_meta->table_name; | |
| 1042 | + $paymentmeta_table = Give()->payment_meta->table_name; | |
| 975 | 1043 | $donationmeta_primary_key = Give()->payment_meta->get_meta_type() . '_id'; |
| 976 | 1044 | |
| 977 | 1045 | return (int) $wpdb->get_var( |
| 978 | 1046 | $wpdb->prepare( |
| @@ -1145,9 +1213,9 @@ | ||
| 1145 | 1213 | * @since 1.8.17 Added filter and internally use functions. |
| 1146 | 1214 | * |
| 1147 | 1215 | * @return string $amount Fully formatted donation amount. |
| 1148 | 1216 | */ |
| 1149 | -function give_donation_amount( $donation_id, $format_args = array() ) { | |
| 1217 | +function give_donation_amount( $donation_id, $format_args = [] ) { | |
| 1150 | 1218 | if ( ! $donation_id ) { |
| 1151 | 1219 | return ''; |
| 1152 | 1220 | } elseif ( ! is_numeric( $donation_id ) && ( $donation_id instanceof Give_Payment ) ) { |
| 1153 | 1221 | $donation_id = $donation_id->ID; |
| @@ -1156,17 +1224,17 @@ | ||
| 1156 | 1224 | $amount = $formatted_amount = give_get_payment_total( $donation_id ); |
| 1157 | 1225 | $currency_code = give_get_payment_currency_code( $donation_id ); |
| 1158 | 1226 | |
| 1159 | 1227 | if ( is_bool( $format_args ) ) { |
| 1160 | - $format_args = array( | |
| 1228 | + $format_args = [ | |
| 1161 | 1229 | 'currency' => (bool) $format_args, |
| 1162 | 1230 | 'amount' => (bool) $format_args, |
| 1163 | - ); | |
| 1231 | + ]; | |
| 1164 | 1232 | } |
| 1165 | 1233 | |
| 1166 | 1234 | $format_args = wp_parse_args( |
| 1167 | 1235 | $format_args, |
| 1168 | - array( | |
| 1236 | + [ | |
| 1169 | 1237 | 'currency' => false, |
| 1170 | 1238 | 'amount' => false, |
| 1171 | 1239 | |
| 1172 | 1240 | // Define context of donation amount, by default keep $type as blank. |
| @@ -1175,9 +1243,9 @@ | ||
| 1175 | 1243 | // different currency other than base currency, in that case for correct |
| 1176 | 1244 | // report calculation based on base currency we will need to return donation |
| 1177 | 1245 | // base amount and not the converted amount . |
| 1178 | 1246 | 'type' => '', |
| 1179 | - ) | |
| 1247 | + ] | |
| 1180 | 1248 | ); |
| 1181 | 1249 | |
| 1182 | 1250 | if ( $format_args['amount'] || $format_args['currency'] ) { |
| 1183 | 1251 | |
| @@ -1185,12 +1253,12 @@ | ||
| 1185 | 1253 | |
| 1186 | 1254 | $formatted_amount = give_format_amount( |
| 1187 | 1255 | $amount, |
| 1188 | 1256 | ! is_array( $format_args['amount'] ) ? |
| 1189 | - array( | |
| 1257 | + [ | |
| 1190 | 1258 | 'sanitize' => false, |
| 1191 | 1259 | 'currency' => $currency_code, |
| 1192 | - ) : | |
| 1260 | + ] : | |
| 1193 | 1261 | $format_args['amount'] |
| 1194 | 1262 | ); |
| 1195 | 1263 | } |
| 1196 | 1264 | |
| @@ -1197,9 +1265,9 @@ | ||
| 1197 | 1265 | if ( $format_args['currency'] ) { |
| 1198 | 1266 | $formatted_amount = give_currency_filter( |
| 1199 | 1267 | $formatted_amount, |
| 1200 | 1268 | ! is_array( $format_args['currency'] ) ? |
| 1201 | - array( 'currency_code' => $currency_code ) : | |
| 1269 | + [ 'currency_code' => $currency_code ] : | |
| 1202 | 1270 | $format_args['currency'] |
| 1203 | 1271 | ); |
| 1204 | 1272 | } |
| 1205 | 1273 | } |
| @@ -1233,9 +1301,9 @@ | ||
| 1233 | 1301 | */ |
| 1234 | 1302 | function give_payment_subtotal( $payment_id = 0 ) { |
| 1235 | 1303 | $subtotal = give_get_payment_subtotal( $payment_id ); |
| 1236 | 1304 | |
| 1237 | - 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 ) ] ); | |
| 1238 | 1306 | } |
| 1239 | 1307 | |
| 1240 | 1308 | /** |
| 1241 | 1309 | * Get Payment Subtotal |
| @@ -1307,9 +1375,9 @@ | ||
| 1307 | 1375 | */ |
| 1308 | 1376 | function give_get_donation_id_by_key( $key ) { |
| 1309 | 1377 | global $wpdb; |
| 1310 | 1378 | |
| 1311 | - $meta_table = __give_v20_bc_table_details( 'payment' ); | |
| 1379 | + $meta_table = give_v20_bc_table_details( 'payment' ); | |
| 1312 | 1380 | |
| 1313 | 1381 | $purchase = $wpdb->get_var( |
| 1314 | 1382 | $wpdb->prepare( |
| 1315 | 1383 | " |
| @@ -1343,9 +1411,9 @@ | ||
| 1343 | 1411 | * @return int $purchase Donation ID. |
| 1344 | 1412 | */ |
| 1345 | 1413 | function give_get_purchase_id_by_transaction_id( $key ) { |
| 1346 | 1414 | global $wpdb; |
| 1347 | - $meta_table = __give_v20_bc_table_details( 'payment' ); | |
| 1415 | + $meta_table = give_v20_bc_table_details( 'payment' ); | |
| 1348 | 1416 | |
| 1349 | 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 ) ); |
| 1350 | 1418 | |
| 1351 | 1419 | if ( $purchase != null ) { |
| @@ -1365,9 +1433,9 @@ | ||
| 1365 | 1433 | * |
| 1366 | 1434 | * @return array $notes Donation Notes |
| 1367 | 1435 | */ |
| 1368 | 1436 | function give_get_payment_notes( $payment_id = 0, $search = '' ) { |
| 1369 | - return Give_Comment::get( $payment_id,'payment', array(), $search ); | |
| 1437 | + return Give_Comment::get( $payment_id, 'payment', [], $search ); | |
| 1370 | 1438 | } |
| 1371 | 1439 | |
| 1372 | 1440 | |
| 1373 | 1441 | /** |
| @@ -1412,9 +1480,9 @@ | ||
| 1412 | 1480 | |
| 1413 | 1481 | if ( is_numeric( $note ) ) { |
| 1414 | 1482 | if ( ! give_has_upgrade_completed( 'v230_move_donor_note' ) ) { |
| 1415 | 1483 | $note = get_comment( $note ); |
| 1416 | - } else{ | |
| 1484 | + } else { | |
| 1417 | 1485 | $note = Give()->comment->db->get( $note ); |
| 1418 | 1486 | } |
| 1419 | 1487 | } |
| 1420 | 1488 | |
| @@ -1428,14 +1496,15 @@ | ||
| 1428 | 1496 | $date_format = give_date_format() . ', ' . get_option( 'time_format' ); |
| 1429 | 1497 | |
| 1430 | 1498 | $delete_note_url = wp_nonce_url( |
| 1431 | 1499 | add_query_arg( |
| 1432 | - array( | |
| 1500 | + [ | |
| 1433 | 1501 | 'give-action' => 'delete_payment_note', |
| 1434 | 1502 | 'note_id' => $note->comment_ID, |
| 1435 | 1503 | 'payment_id' => $payment_id, |
| 1436 | - ) | |
| 1437 | - ), 'give_delete_payment_note_' . $note->comment_ID | |
| 1504 | + ] | |
| 1505 | + ), | |
| 1506 | + 'give_delete_payment_note_' . $note->comment_ID | |
| 1438 | 1507 | ); |
| 1439 | 1508 | |
| 1440 | 1509 | $note_html = '<div class="give-payment-note" id="give-payment-note-' . $note->comment_ID . '">'; |
| 1441 | 1510 | $note_html .= '<p>'; |
| @@ -1478,13 +1547,16 @@ | ||
| 1478 | 1547 | * @param array $args a. only_level = If set to true will only return the level name if multi-level |
| 1479 | 1548 | * enabled. b. separator = The separator between the Form Title and the Donation |
| 1480 | 1549 | * Level. |
| 1481 | 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 | |
| 1482 | 1554 | * @since 1.5 |
| 1483 | 1555 | * |
| 1484 | 1556 | * @return string $form_title Returns the full title if $only_level is false, otherwise returns the levels title. |
| 1485 | 1557 | */ |
| 1486 | -function give_get_donation_form_title( $donation_id, $args = array() ) { | |
| 1558 | +function give_get_donation_form_title( $donation_id, $args = [] ) { | |
| 1487 | 1559 | // Backward compatibility. |
| 1488 | 1560 | if ( ! is_numeric( $donation_id ) && $donation_id instanceof Give_Payment ) { |
| 1489 | 1561 | $donation_id = $donation_id->ID; |
| 1490 | 1562 | } |
| @@ -1492,18 +1564,63 @@ | ||
| 1492 | 1564 | if ( ! $donation_id ) { |
| 1493 | 1565 | return ''; |
| 1494 | 1566 | } |
| 1495 | 1567 | |
| 1496 | - $defaults = array( | |
| 1568 | + $defaults = [ | |
| 1497 | 1569 | 'only_level' => false, |
| 1498 | 1570 | 'separator' => '', |
| 1499 | - ); | |
| 1571 | + ]; | |
| 1500 | 1572 | |
| 1501 | 1573 | $args = wp_parse_args( $args, $defaults ); |
| 1502 | 1574 | |
| 1503 | - $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 | + | |
| 1504 | 1621 | $price_id = give_get_meta( $donation_id, '_give_payment_price_id', true ); |
| 1505 | - $form_title = give_get_meta( $donation_id, '_give_payment_form_title', true ); | |
| 1622 | + | |
| 1506 | 1623 | $only_level = $args['only_level']; |
| 1507 | 1624 | $separator = $args['separator']; |
| 1508 | 1625 | $level_label = ''; |
| 1509 | 1626 | |
| @@ -1508,15 +1625,16 @@ | ||
| 1508 | 1625 | $level_label = ''; |
| 1509 | 1626 | |
| 1510 | 1627 | $cache_key = Give_Cache::get_key( |
| 1511 | 1628 | 'give_forms', |
| 1512 | - array( | |
| 1629 | + [ | |
| 1513 | 1630 | $form_id, |
| 1514 | 1631 | $price_id, |
| 1515 | 1632 | $form_title, |
| 1516 | 1633 | $only_level, |
| 1517 | 1634 | $separator, |
| 1518 | - ), false | |
| 1635 | + ], | |
| 1636 | + false | |
| 1519 | 1637 | ); |
| 1520 | 1638 | |
| 1521 | 1639 | $form_title_html = Give_Cache::get_db_query( $cache_key ); |
| 1522 | 1640 | |
| @@ -1622,9 +1740,9 @@ | ||
| 1622 | 1740 | * @since 1.6 |
| 1623 | 1741 | * |
| 1624 | 1742 | * @return string |
| 1625 | 1743 | */ |
| 1626 | -function give_get_form_dropdown( $args = array(), $echo = false ) { | |
| 1744 | +function give_get_form_dropdown( $args = [], $echo = false ) { | |
| 1627 | 1745 | $form_dropdown_html = Give()->html->forms_dropdown( $args ); |
| 1628 | 1746 | |
| 1629 | 1747 | if ( ! $echo ) { |
| 1630 | 1748 | return $form_dropdown_html; |
| @@ -1639,12 +1757,13 @@ | ||
| 1639 | 1757 | * @param array $args Arguments for form dropdown. |
| 1640 | 1758 | * @param bool $echo This parameter decide if print form dropdown html output or not. |
| 1641 | 1759 | * |
| 1642 | 1760 | * @since 1.6 |
| 1761 | + * @since 2.12.0 Show "Custom" choice in select field if donation created with cusotm amount | |
| 1643 | 1762 | * |
| 1644 | 1763 | * @return string|bool |
| 1645 | 1764 | */ |
| 1646 | -function give_get_form_variable_price_dropdown( $args = array(), $echo = false ) { | |
| 1765 | +function give_get_form_variable_price_dropdown( $args = [], $echo = false ) { | |
| 1647 | 1766 | |
| 1648 | 1767 | // Check for give form id. |
| 1649 | 1768 | if ( empty( $args['id'] ) ) { |
| 1650 | 1769 | return false; |
| @@ -1657,25 +1776,30 @@ | ||
| 1657 | 1776 | return false; |
| 1658 | 1777 | } |
| 1659 | 1778 | |
| 1660 | 1779 | $variable_prices = $form->get_prices(); |
| 1661 | - $variable_price_options = array(); | |
| 1780 | + $variable_price_options = []; | |
| 1662 | 1781 | |
| 1663 | 1782 | // Check if multi donation form support custom donation or not. |
| 1664 | - 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 | + ) { | |
| 1665 | 1788 | $variable_price_options['custom'] = _x( 'Custom', 'custom donation dropdown item', 'give' ); |
| 1666 | 1789 | } |
| 1667 | 1790 | |
| 1668 | 1791 | // Get variable price and ID from variable price array. |
| 1669 | 1792 | foreach ( $variable_prices as $variable_price ) { |
| 1670 | - $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 ] ) ); | |
| 1671 | 1794 | } |
| 1672 | 1795 | |
| 1673 | 1796 | // Update options. |
| 1674 | 1797 | $args = array_merge( |
| 1675 | - $args, array( | |
| 1798 | + $args, | |
| 1799 | + [ | |
| 1676 | 1800 | 'options' => $variable_price_options, |
| 1677 | - ) | |
| 1801 | + ] | |
| 1678 | 1802 | ); |
| 1679 | 1803 | |
| 1680 | 1804 | // Generate select html. |
| 1681 | 1805 | $form_dropdown_html = Give()->html->select( $args ); |
| @@ -1779,9 +1903,10 @@ | ||
| 1779 | 1903 | * @param bool |
| 1780 | 1904 | * @param int $donation_id |
| 1781 | 1905 | */ |
| 1782 | 1906 | return apply_filters( |
| 1783 | - 'give_is_donation_completed', (bool) $wpdb->get_var( | |
| 1907 | + 'give_is_donation_completed', | |
| 1908 | + (bool) $wpdb->get_var( | |
| 1784 | 1909 | $wpdb->prepare( |
| 1785 | 1910 | " |
| 1786 | 1911 | SELECT meta_value |
| 1787 | 1912 | FROM {$wpdb->donationmeta} |
| @@ -1796,9 +1921,10 @@ | ||
| 1796 | 1921 | 'publish', |
| 1797 | 1922 | $donation_id, |
| 1798 | 1923 | '_give_completed_date' |
| 1799 | 1924 | ) |
| 1800 | - ), $donation_id | |
| 1925 | + ), | |
| 1926 | + $donation_id | |
| 1801 | 1927 | ); |
| 1802 | 1928 | } |
| 1803 | 1929 | |
| 1804 | 1930 | /** |
| @@ -1811,9 +1937,9 @@ | ||
| 1811 | 1937 | */ |
| 1812 | 1938 | function give_is_anonymous_donation( $donation_id ) { |
| 1813 | 1939 | $value = false; |
| 1814 | 1940 | |
| 1815 | - if( (int) give_get_meta( $donation_id, '_give_anonymous_donation', true ) ){ | |
| 1941 | + if ( (int) give_get_meta( $donation_id, '_give_anonymous_donation', true ) ) { | |
| 1816 | 1942 | $value = true; |
| 1817 | 1943 | } |
| 1818 | 1944 | |
| 1819 | 1945 | return $value; |