| @@ -29,8 +29,9 @@ | ||
| 29 | 29 | * @property $sales |
| 30 | 30 | * @property $earnings |
| 31 | 31 | * @property $post_type |
| 32 | 32 | */ |
| 33 | +#[\AllowDynamicProperties] | |
| 33 | 34 | class Give_Donate_Form { |
| 34 | 35 | |
| 35 | 36 | /** |
| 36 | 37 | * The donation ID. |
| @@ -347,13 +348,12 @@ | ||
| 347 | 348 | * |
| 348 | 349 | * @param int|bool $_id Post id. Default is false. |
| 349 | 350 | * @param array $_args Arguments passed. |
| 350 | 351 | */ |
| 351 | - public function __construct( $_id = false, $_args = array() ) { | |
| 352 | - | |
| 352 | + public function __construct( $_id = false, $_args = [] ) { | |
| 353 | 353 | $donation_form = WP_Post::get_instance( $_id ); |
| 354 | 354 | |
| 355 | - return $this->setup_donation_form( $donation_form ); | |
| 355 | + $this->setup_donation_form( $donation_form ); | |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | /** |
| 359 | 359 | * Given the donation form data, let's set the variables |
| @@ -366,33 +366,23 @@ | ||
| 366 | 366 | * @return bool If the setup was successful or not. |
| 367 | 367 | */ |
| 368 | 368 | private function setup_donation_form( $donation_form ) { |
| 369 | 369 | |
| 370 | - if ( ! is_object( $donation_form ) ) { | |
| 370 | + // Bailout. | |
| 371 | + if ( | |
| 372 | + ! ( $donation_form instanceof WP_Post ) | |
| 373 | + || 'give_forms' !== $donation_form->post_type | |
| 374 | + ) { | |
| 371 | 375 | return false; |
| 372 | 376 | } |
| 373 | 377 | |
| 374 | - if ( ! is_a( $donation_form, 'WP_Post' ) ) { | |
| 375 | - return false; | |
| 376 | - } | |
| 378 | + Give_Forms_Query::update_meta_cache( [ $donation_form->ID ] ); | |
| 377 | 379 | |
| 378 | - if ( 'give_forms' !== $donation_form->post_type ) { | |
| 379 | - return false; | |
| 380 | - } | |
| 381 | - | |
| 382 | 380 | foreach ( $donation_form as $key => $value ) { |
| 383 | - | |
| 384 | - switch ( $key ) { | |
| 385 | - | |
| 386 | - default: | |
| 387 | - $this->$key = $value; | |
| 388 | - break; | |
| 389 | - | |
| 390 | - } | |
| 391 | - | |
| 381 | + $this->$key = $value; | |
| 392 | 382 | } |
| 393 | 383 | |
| 394 | - return true; | |
| 384 | + return $donation_form->ID; | |
| 395 | 385 | |
| 396 | 386 | } |
| 397 | 387 | |
| 398 | 388 | /** |
| @@ -406,19 +396,15 @@ | ||
| 406 | 396 | * @return mixed |
| 407 | 397 | */ |
| 408 | 398 | public function __get( $key ) { |
| 409 | 399 | |
| 410 | - if ( method_exists( $this, 'get_' . $key ) ) { | |
| 400 | + if ( method_exists( $this, "get_{$key}" ) ) { | |
| 401 | + return $this->{"get_{$key}"}(); | |
| 402 | + } | |
| 411 | 403 | |
| 412 | - return call_user_func( array( $this, 'get_' . $key ) ); | |
| 404 | + /* translators: %s: property key */ | |
| 405 | + return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) ); | |
| 413 | 406 | |
| 414 | - } else { | |
| 415 | - | |
| 416 | - /* translators: %s: property key */ | |
| 417 | - return new WP_Error( 'give-form-invalid-property', sprintf( esc_html__( 'Can\'t get property %s.', 'give' ), $key ) ); | |
| 418 | - | |
| 419 | - } | |
| 420 | - | |
| 421 | 407 | } |
| 422 | 408 | |
| 423 | 409 | /** |
| 424 | 410 | * Creates a donation form |
| @@ -429,19 +415,19 @@ | ||
| 429 | 415 | * @param array $data Array of attributes for a donation form. |
| 430 | 416 | * |
| 431 | 417 | * @return bool|int False if data isn't passed and class not instantiated for creation, or New Form ID. |
| 432 | 418 | */ |
| 433 | - public function create( $data = array() ) { | |
| 419 | + public function create( $data = [] ) { | |
| 434 | 420 | |
| 435 | 421 | if ( $this->id != 0 ) { |
| 436 | 422 | return false; |
| 437 | 423 | } |
| 438 | 424 | |
| 439 | - $defaults = array( | |
| 425 | + $defaults = [ | |
| 440 | 426 | 'post_type' => 'give_forms', |
| 441 | 427 | 'post_status' => 'draft', |
| 442 | 428 | 'post_title' => __( 'New Donation Form', 'give' ), |
| 443 | - ); | |
| 429 | + ]; | |
| 444 | 430 | |
| 445 | 431 | $args = wp_parse_args( $data, $defaults ); |
| 446 | 432 | |
| 447 | 433 | /** |
| @@ -513,9 +499,8 @@ | ||
| 513 | 499 | |
| 514 | 500 | if ( ! $this->price ) { |
| 515 | 501 | $this->price = 0; |
| 516 | 502 | } |
| 517 | - | |
| 518 | 503 | } |
| 519 | 504 | |
| 520 | 505 | /** |
| 521 | 506 | * Override the donation form set price. |
| @@ -549,9 +534,8 @@ | ||
| 549 | 534 | |
| 550 | 535 | if ( ! $this->is_custom_price_mode() ) { |
| 551 | 536 | $this->minimum_price = give_get_lowest_price_option( $this->ID ); |
| 552 | 537 | } |
| 553 | - | |
| 554 | 538 | } |
| 555 | 539 | |
| 556 | 540 | return apply_filters( 'give_get_set_minimum_price', $this->minimum_price, $this->ID ); |
| 557 | 541 | } |
| @@ -610,14 +594,14 @@ | ||
| 610 | 594 | * |
| 611 | 595 | * @since 2.0.6 |
| 612 | 596 | * @access public |
| 613 | 597 | * |
| 614 | - * @param $price_id | |
| 598 | + * @param string $price_id | |
| 615 | 599 | * |
| 616 | 600 | * @return array|null |
| 617 | 601 | */ |
| 618 | 602 | public function get_level_info( $price_id ) { |
| 619 | - $level_info = array(); | |
| 603 | + $level_info = []; | |
| 620 | 604 | |
| 621 | 605 | // Bailout. |
| 622 | 606 | if ( 'multi' !== $this->get_type() ) { |
| 623 | 607 | return null; |
| @@ -639,8 +623,9 @@ | ||
| 639 | 623 | /** |
| 640 | 624 | * Retrieve the goal |
| 641 | 625 | * |
| 642 | 626 | * @since 1.0 |
| 627 | + * @since 2.19.0 Set default goal value to zero to prevent fatal error on PHP8.0. | |
| 643 | 628 | * @access public |
| 644 | 629 | * |
| 645 | 630 | * @return float Goal. |
| 646 | 631 | */ |
| @@ -649,23 +634,23 @@ | ||
| 649 | 634 | if ( ! isset( $this->goal ) ) { |
| 650 | 635 | |
| 651 | 636 | $goal_format = give_get_form_goal_format( $this->ID ); |
| 652 | 637 | |
| 653 | - if ( 'donation' === $goal_format ) { | |
| 638 | + if ( ! $this->has_goal() ) { | |
| 639 | + $this->goal = ''; | |
| 640 | + | |
| 641 | + } elseif ( 'donation' === $goal_format ) { | |
| 654 | 642 | $this->goal = give_get_meta( $this->ID, '_give_number_of_donation_goal', true ); |
| 643 | + | |
| 655 | 644 | } elseif ( 'donors' === $goal_format ) { |
| 656 | 645 | $this->goal = give_get_meta( $this->ID, '_give_number_of_donor_goal', true ); |
| 657 | - } else { | |
| 646 | + | |
| 647 | + } elseif ( in_array( $goal_format, [ 'amount', 'percentage' ] ) ) { | |
| 658 | 648 | $this->goal = give_get_meta( $this->ID, '_give_set_goal', true ); |
| 659 | 649 | } |
| 660 | - | |
| 661 | - if ( ! $this->goal ) { | |
| 662 | - $this->goal = 0; | |
| 663 | - } | |
| 664 | - | |
| 665 | 650 | } |
| 666 | 651 | |
| 667 | - return apply_filters( 'give_get_set_goal', $this->goal, $this->ID ); | |
| 652 | + return apply_filters( 'give_get_set_goal', $this->goal ?: 0, $this->ID ); | |
| 668 | 653 | |
| 669 | 654 | } |
| 670 | 655 | |
| 671 | 656 | /** |
| @@ -745,9 +730,8 @@ | ||
| 745 | 730 | if ( 'set' === $this->get_type() ) { |
| 746 | 731 | if ( $amount !== $this->get_price() ) { |
| 747 | 732 | $result = true; |
| 748 | 733 | } |
| 749 | - | |
| 750 | 734 | } elseif ( 'multi' === $this->get_type() ) { |
| 751 | 735 | $level_amounts = array_map( 'give_maybe_sanitize_amount', wp_list_pluck( $this->get_prices(), '_give_amount' ) ); |
| 752 | 736 | $result = ! in_array( $amount, $level_amounts ); |
| 753 | 737 | } |
| @@ -810,9 +794,8 @@ | ||
| 810 | 794 | |
| 811 | 795 | if ( empty( $this->type ) ) { |
| 812 | 796 | $this->type = 'set'; |
| 813 | 797 | } |
| 814 | - | |
| 815 | 798 | } |
| 816 | 799 | |
| 817 | 800 | return apply_filters( 'give_get_form_type', $this->type, $this->ID ); |
| 818 | 801 | |
| @@ -830,23 +813,37 @@ | ||
| 830 | 813 | * |
| 831 | 814 | * @return string |
| 832 | 815 | */ |
| 833 | 816 | public function get_form_classes( $args ) { |
| 817 | + /** | |
| 818 | + * @since 3.11.0 sanitize $args | |
| 819 | + */ | |
| 820 | + $args = give_clean($args); | |
| 834 | 821 | |
| 835 | 822 | $float_labels_option = give_is_float_labels_enabled( $args ) |
| 836 | 823 | ? 'float-labels-enabled' |
| 837 | 824 | : ''; |
| 838 | 825 | |
| 839 | - $form_classes_array = apply_filters( 'give_form_classes', array( | |
| 840 | - 'give-form', | |
| 841 | - 'give-form-' . $this->ID, | |
| 842 | - 'give-form-type-' . $this->get_type(), | |
| 843 | - $float_labels_option, | |
| 844 | - ), $this->ID, $args ); | |
| 826 | + $form_classes_array = apply_filters( | |
| 827 | + 'give_form_classes', | |
| 828 | + [ | |
| 829 | + 'give-form', | |
| 830 | + 'give-form-' . $this->ID, | |
| 831 | + 'give-form-type-' . $this->get_type(), | |
| 832 | + $float_labels_option, | |
| 833 | + ], | |
| 834 | + $this->ID, | |
| 835 | + $args | |
| 836 | + ); | |
| 845 | 837 | |
| 846 | 838 | // Remove empty class names. |
| 847 | 839 | $form_classes_array = array_filter( $form_classes_array ); |
| 848 | 840 | |
| 841 | + /** | |
| 842 | + * @since 3.11.0 sanitize attributes | |
| 843 | + */ | |
| 844 | + $form_classes_array = array_map('esc_attr', $form_classes_array); | |
| 845 | + | |
| 849 | 846 | return implode( ' ', $form_classes_array ); |
| 850 | 847 | |
| 851 | 848 | } |
| 852 | 849 | |
| @@ -861,11 +858,16 @@ | ||
| 861 | 858 | * |
| 862 | 859 | * @return string |
| 863 | 860 | */ |
| 864 | 861 | public function get_form_wrap_classes( $args ) { |
| 865 | - $custom_class = array( | |
| 862 | + /** | |
| 863 | + * @since 3.11.0 sanitize $args | |
| 864 | + */ | |
| 865 | + $args = give_clean($args); | |
| 866 | + | |
| 867 | + $custom_class = [ | |
| 866 | 868 | 'give-form-wrap', |
| 867 | - ); | |
| 869 | + ]; | |
| 868 | 870 | |
| 869 | 871 | if ( $this->is_close_donation_form() ) { |
| 870 | 872 | $custom_class[] = 'give-form-closed'; |
| 871 | 873 | } else { |
| @@ -880,9 +882,8 @@ | ||
| 880 | 882 | $custom_class[] = 'give-display-button-only'; |
| 881 | 883 | } |
| 882 | 884 | } |
| 883 | 885 | |
| 884 | - | |
| 885 | 886 | /** |
| 886 | 887 | * Filter the donation form classes. |
| 887 | 888 | * |
| 888 | 889 | * @since 1.0 |
| @@ -888,8 +889,12 @@ | ||
| 888 | 889 | * @since 1.0 |
| 889 | 890 | */ |
| 890 | 891 | $form_wrap_classes_array = (array) apply_filters( 'give_form_wrap_classes', $custom_class, $this->ID, $args ); |
| 891 | 892 | |
| 893 | + /** | |
| 894 | + * @since 3.11.0 sanitize attributes | |
| 895 | + */ | |
| 896 | + $form_wrap_classes_array = array_map('esc_attr', $form_wrap_classes_array); | |
| 892 | 897 | |
| 893 | 898 | return implode( ' ', $form_wrap_classes_array ); |
| 894 | 899 | |
| 895 | 900 | } |
| @@ -935,9 +940,9 @@ | ||
| 935 | 940 | |
| 936 | 941 | if ( ! isset( $this->sales ) ) { |
| 937 | 942 | |
| 938 | 943 | if ( '' == give_get_meta( $this->ID, '_give_form_sales', true ) ) { |
| 939 | - add_post_meta( $this->ID, '_give_form_sales', 0 ); | |
| 944 | + give_update_meta( $this->ID, '_give_form_sales', 0 ); | |
| 940 | 945 | } // End if |
| 941 | 946 | |
| 942 | 947 | $this->sales = give_get_meta( $this->ID, '_give_form_sales', true ); |
| 943 | 948 | |
| @@ -944,13 +949,14 @@ | ||
| 944 | 949 | if ( $this->sales < 0 ) { |
| 945 | 950 | // Never let sales be less than zero. |
| 946 | 951 | $this->sales = 0; |
| 947 | 952 | } |
| 948 | - | |
| 949 | 953 | } |
| 950 | 954 | |
| 951 | - return $this->sales; | |
| 952 | - | |
| 955 | + /** | |
| 956 | + * @since 3.14.0 | |
| 957 | + */ | |
| 958 | + return apply_filters('give_donate_form_get_sales', $this->sales, $this->ID); | |
| 953 | 959 | } |
| 954 | 960 | |
| 955 | 961 | /** |
| 956 | 962 | * Increment the sale count by one |
| @@ -1005,9 +1011,8 @@ | ||
| 1005 | 1011 | |
| 1006 | 1012 | return $sales; |
| 1007 | 1013 | |
| 1008 | 1014 | } |
| 1009 | - | |
| 1010 | 1015 | } |
| 1011 | 1016 | |
| 1012 | 1017 | return false; |
| 1013 | 1018 | |
| @@ -1025,9 +1030,9 @@ | ||
| 1025 | 1030 | |
| 1026 | 1031 | if ( ! isset( $this->earnings ) ) { |
| 1027 | 1032 | |
| 1028 | 1033 | if ( '' == give_get_meta( $this->ID, '_give_form_earnings', true ) ) { |
| 1029 | - add_post_meta( $this->ID, '_give_form_earnings', 0 ); | |
| 1034 | + give_update_meta( $this->ID, '_give_form_earnings', 0 ); | |
| 1030 | 1035 | } |
| 1031 | 1036 | |
| 1032 | 1037 | $this->earnings = give_get_meta( $this->ID, '_give_form_earnings', true ); |
| 1033 | 1038 | |
| @@ -1034,9 +1039,8 @@ | ||
| 1034 | 1039 | if ( $this->earnings < 0 ) { |
| 1035 | 1040 | // Never let earnings be less than zero |
| 1036 | 1041 | $this->earnings = 0; |
| 1037 | 1042 | } |
| 1038 | - | |
| 1039 | 1043 | } |
| 1040 | 1044 | |
| 1041 | 1045 | return $this->earnings; |
| 1042 | 1046 | |
| @@ -1050,15 +1054,15 @@ | ||
| 1050 | 1054 | * |
| 1051 | 1055 | * @access public |
| 1052 | 1056 | * |
| 1053 | 1057 | * @param int $amount Amount of donation. Default is 0. |
| 1054 | - * @param int $payment_id Donation ID. | |
| 1058 | + * @param int $payment_id Donation ID. | |
| 1055 | 1059 | * |
| 1056 | 1060 | * @return float|false |
| 1057 | 1061 | */ |
| 1058 | 1062 | public function increase_earnings( $amount = 0, $payment_id = 0 ) { |
| 1059 | 1063 | |
| 1060 | - $earnings = give_get_form_earnings_stats( $this->ID ); | |
| 1064 | + $earnings = give_get_form_earnings_stats( $this->ID ); | |
| 1061 | 1065 | |
| 1062 | 1066 | /** |
| 1063 | 1067 | * Modify the earning amount when increasing. |
| 1064 | 1068 | * |
| @@ -1090,9 +1094,9 @@ | ||
| 1090 | 1094 | * @since 1.0 |
| 1091 | 1095 | * @access public |
| 1092 | 1096 | * |
| 1093 | 1097 | * @param int $amount Amount of donation. |
| 1094 | - * @param int $payment_id Donation ID. | |
| 1098 | + * @param int $payment_id Donation ID. | |
| 1095 | 1099 | * |
| 1096 | 1100 | * @return float|false |
| 1097 | 1101 | */ |
| 1098 | 1102 | public function decrease_earnings( $amount, $payment_id = 0 ) { |
| @@ -1119,9 +1123,8 @@ | ||
| 1119 | 1123 | $this->earnings = $new_amount; |
| 1120 | 1124 | |
| 1121 | 1125 | return $this->earnings; |
| 1122 | 1126 | } |
| 1123 | - | |
| 1124 | 1127 | } |
| 1125 | 1128 | |
| 1126 | 1129 | return false; |
| 1127 | 1130 | |
| @@ -1162,9 +1165,28 @@ | ||
| 1162 | 1165 | ); |
| 1163 | 1166 | |
| 1164 | 1167 | } |
| 1165 | 1168 | |
| 1169 | + | |
| 1166 | 1170 | /** |
| 1171 | + * Check whether donation form has goal or not | |
| 1172 | + * | |
| 1173 | + * @since 2.4.2 | |
| 1174 | + * @access public | |
| 1175 | + * | |
| 1176 | + * @return bool | |
| 1177 | + */ | |
| 1178 | + public function has_goal() { | |
| 1179 | + return give_is_setting_enabled( | |
| 1180 | + Give()->form_meta->get_meta( | |
| 1181 | + $this->ID, | |
| 1182 | + '_give_goal_option', | |
| 1183 | + true | |
| 1184 | + ) | |
| 1185 | + ); | |
| 1186 | + } | |
| 1187 | + | |
| 1188 | + /** | |
| 1167 | 1189 | * Updates a single meta entry for the donation form |
| 1168 | 1190 | * |
| 1169 | 1191 | * @since 1.5 |
| 1170 | 1192 | * @access private |
| @@ -1221,9 +1243,9 @@ | ||
| 1221 | 1243 | break; |
| 1222 | 1244 | case 'donors': |
| 1223 | 1245 | $closed = $form->get_goal() <= give_get_form_donor_count( $this->ID ); |
| 1224 | 1246 | break; |
| 1225 | - default : | |
| 1247 | + default: | |
| 1226 | 1248 | $closed = $form->get_goal() <= $form->get_earnings(); |
| 1227 | 1249 | break; |
| 1228 | 1250 | } |
| 1229 | 1251 | |
| @@ -1229,9 +1251,8 @@ | ||
| 1229 | 1251 | |
| 1230 | 1252 | if ( $closed ) { |
| 1231 | 1253 | $close_form = true; |
| 1232 | 1254 | } |
| 1233 | - | |
| 1234 | 1255 | } |
| 1235 | 1256 | } |
| 1236 | 1257 | |
| 1237 | 1258 | return $close_form; |