| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | * Formatting functions for taking care of proper number formats and such |
| 4 | 4 | * |
| 5 | 5 | * @package Give |
| 6 | 6 | * @subpackage Functions/Formatting |
| 7 | - * @copyright Copyright (c) 2016, GiveWP | |
| 7 | + * @copyright Copyright (c) 2016, WordImpress | |
| 8 | 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | 9 | * @since 1.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| @@ -38,15 +38,15 @@ | ||
| 38 | 38 | array_key_exists( $id_or_currency_code, $currencies ) |
| 39 | 39 | ) { |
| 40 | 40 | $setting = $currencies[ $id_or_currency_code ]['setting']; |
| 41 | 41 | } elseif ( is_numeric( $id_or_currency_code ) && 'give_payment' === get_post_type( $id_or_currency_code ) ) { |
| 42 | - $currency = give_get_meta( $id_or_currency_code, '_give_payment_currency', true ); | |
| 42 | + $donation_meta = give_get_meta( $id_or_currency_code, '_give_payment_meta', true ); | |
| 43 | 43 | |
| 44 | 44 | if ( |
| 45 | - ! empty( $currency) && | |
| 46 | - $give_options['currency'] !== $currency | |
| 45 | + ! empty( $donation_meta['currency'] ) && | |
| 46 | + $give_options['currency'] !== $donation_meta['currency'] | |
| 47 | 47 | ) { |
| 48 | - $setting = $currencies[ $currency ]['setting']; | |
| 48 | + $setting = $currencies[ $donation_meta['currency'] ]['setting']; | |
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
| @@ -152,8 +152,9 @@ | ||
| 152 | 152 | */ |
| 153 | 153 | return apply_filters( 'give_get_price_decimal_separator', $setting['decimal_separator'], $id_or_currency_code ); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | + | |
| 156 | 157 | /** |
| 157 | 158 | * Sanitize Amount before saving to database |
| 158 | 159 | * |
| 159 | 160 | * @since 1.8.12 |
| @@ -158,23 +159,13 @@ | ||
| 158 | 159 | * |
| 159 | 160 | * @since 1.8.12 |
| 160 | 161 | * |
| 161 | 162 | * @param int|float|string $number Expects either a float or a string with a decimal separator only (no thousands) |
| 162 | - * @param array|bool $args It accepts 'number_decimals', 'trim_zeros', 'currency'. | |
| 163 | 163 | * |
| 164 | 164 | * @return string $amount Newly sanitized amount |
| 165 | 165 | */ |
| 166 | -function give_sanitize_amount_for_db( $number, $args = array() ) { | |
| 167 | - $args['number_decimals'] = 6; | |
| 168 | - | |
| 169 | - if ( | |
| 170 | - ( isset( $args['currency'] ) && 'BTC' === $args['currency'] ) | |
| 171 | - || 'BTC' === give_get_currency() | |
| 172 | - ) { | |
| 173 | - $args['number_decimals'] = 10; | |
| 174 | - } | |
| 175 | - | |
| 176 | - return give_maybe_sanitize_amount( $number, $args ); | |
| 166 | +function give_sanitize_amount_for_db( $number ) { | |
| 167 | + return give_maybe_sanitize_amount( $number, array( 'number_decimals' => 6 ) ); | |
| 177 | 168 | } |
| 178 | 169 | |
| 179 | 170 | /** |
| 180 | 171 | * Sanitize Amount before saving to database |
| @@ -213,9 +204,9 @@ | ||
| 213 | 204 | |
| 214 | 205 | $thousand_separator = give_get_price_thousand_separator( $args['currency'] ); |
| 215 | 206 | $decimal_separator = give_get_price_decimal_separator( $args['currency'] ); |
| 216 | 207 | $number_decimals = is_bool( $args['number_decimals'] ) ? |
| 217 | - give_get_price_decimals( $args['currency'] ) : | |
| 208 | + give_get_price_decimals() : | |
| 218 | 209 | $args['number_decimals']; |
| 219 | 210 | |
| 220 | 211 | // Explode number by . decimal separator. |
| 221 | 212 | $number_parts = explode( '.', $number ); |
| @@ -224,23 +215,25 @@ | ||
| 224 | 215 | $number = trim( str_replace( give_currency_symbols( true ), '', $number ) ); |
| 225 | 216 | |
| 226 | 217 | if ( |
| 227 | 218 | // Non formatted number. |
| 228 | - false === strpos( $number, $thousand_separator ) | |
| 229 | - && false === strpos( $number, $decimal_separator ) | |
| 230 | - ) { | |
| 231 | - return number_format( $number, $number_decimals, '.', '' ); | |
| 232 | - } elseif ( | |
| 219 | + ( | |
| 220 | + ( false === strpos( $number, $thousand_separator ) ) && | |
| 221 | + ( false === strpos( $number, $decimal_separator ) ) | |
| 222 | + ) || | |
| 223 | + | |
| 233 | 224 | // Decimal formatted number. |
| 234 | 225 | // If number of decimal place set to non zero and |
| 235 | 226 | // number only contains `.` as separator, precision set to less then or equal to number of decimal |
| 236 | 227 | // then number will be consider as decimal formatted which means number is already sanitized. |
| 237 | - $number_decimals | |
| 238 | - && '.' === $thousand_separator | |
| 239 | - && false !== strpos( $number, $thousand_separator ) | |
| 240 | - && false === strpos( $number, $decimal_separator ) | |
| 241 | - && 2 === count( $number_parts ) | |
| 242 | - && ( $number_decimals >= strlen( $number_parts[1] ) ) | |
| 228 | + ( | |
| 229 | + $number_decimals && | |
| 230 | + '.' === $thousand_separator && | |
| 231 | + false !== strpos( $number, $thousand_separator ) && | |
| 232 | + false === strpos( $number, $decimal_separator ) && | |
| 233 | + 2 === count( $number_parts ) && | |
| 234 | + ( $number_decimals >= strlen( $number_parts[1] ) ) | |
| 235 | + ) | |
| 243 | 236 | ) { |
| 244 | 237 | return number_format( $number, $number_decimals, '.', '' ); |
| 245 | 238 | } |
| 246 | 239 | |
| @@ -246,11 +239,11 @@ | ||
| 246 | 239 | |
| 247 | 240 | // Handle thousand separator as '.' |
| 248 | 241 | // Handle sanitize database values. |
| 249 | 242 | $is_db_sanitize_val = ( 2 === count( $number_parts ) && |
| 250 | - is_numeric( $number_parts[0] ) && | |
| 251 | - is_numeric( $number_parts[1] ) && | |
| 252 | - ( in_array( strlen( $number_parts[1] ), array( 6, 10 ) ) ) ); | |
| 243 | + is_numeric( $number_parts[0] ) && | |
| 244 | + is_numeric( $number_parts[1] ) && | |
| 245 | + ( 6 === strlen( $number_parts[1] ) ) ); | |
| 253 | 246 | |
| 254 | 247 | if ( $is_db_sanitize_val ) { |
| 255 | 248 | // Sanitize database value. |
| 256 | 249 | return number_format( $number, $number_decimals, '.', '' ); |
| @@ -400,9 +393,10 @@ | ||
| 400 | 393 | $args = wp_parse_args( $args, $default_args ); |
| 401 | 394 | |
| 402 | 395 | // Set Currency based on donation id, if required. |
| 403 | 396 | if ( $args['donation_id'] && empty( $args['currency'] ) ) { |
| 404 | - $args['currency'] = give_get_meta( $args['donation_id'], '_give_payment_currency', true ); | |
| 397 | + $donation_meta = give_get_meta( $args['donation_id'], '_give_payment_meta', true ); | |
| 398 | + $args['currency'] = $donation_meta['currency']; | |
| 405 | 399 | } |
| 406 | 400 | |
| 407 | 401 | $formatted = 0; |
| 408 | 402 | $currency = ! empty( $args['currency'] ) ? $args['currency'] : give_get_currency( $args['donation_id'] ); |
| @@ -532,64 +526,25 @@ | ||
| 532 | 526 | * Returns a nicely formatted amount with custom decimal separator. |
| 533 | 527 | * |
| 534 | 528 | * @since 1.0 |
| 535 | 529 | * |
| 536 | - * @param array $args { | |
| 530 | + * @param int|float|string $amount Formatted or sanitized price | |
| 531 | + * @param int|bool $dp number of decimals | |
| 532 | + * @param bool $sanitize Whether or not sanitize number | |
| 537 | 533 | * |
| 538 | - * @type int|float|string $amount Formatted or sanitized price. (optional if donation id set) | |
| 539 | - * @type int $donation_id donation amount (optional if set amount, but provide it for better result if formatting decimal amount of donation). | |
| 540 | - * @type string $currency donation amount (optional if set donation id). Provide either amount or donation id | |
| 541 | - * @type int|bool $dp number of decimals | |
| 542 | - * @type bool $sanitize Whether or not sanitize number | |
| 543 | - * } | |
| 544 | - * | |
| 545 | 534 | * @return string $amount Newly formatted amount or Price Not Available |
| 546 | 535 | */ |
| 547 | -function give_format_decimal( $args ) { | |
| 548 | - // Backward compatibility. | |
| 549 | - if ( ! is_array( $args ) ) { | |
| 550 | - $func_args = func_get_args(); | |
| 551 | - $args = array( | |
| 552 | - 'amount' => $func_args[0], | |
| 553 | - 'dp' => isset( $func_args[1] ) ? $func_args[1] : false, | |
| 554 | - 'sanitize' => isset( $func_args[2] ) ? $func_args[2] : true, | |
| 555 | - ); | |
| 556 | - } | |
| 557 | - | |
| 558 | - $args = wp_parse_args( | |
| 559 | - $args, | |
| 560 | - array( | |
| 561 | - 'amount' => '', | |
| 562 | - 'donation_id' => 0, | |
| 563 | - 'currency' => '', | |
| 564 | - 'dp' => false, | |
| 565 | - 'sanitize' => false, | |
| 566 | - ) | |
| 567 | - ); | |
| 568 | - | |
| 569 | - if( ! empty( $args['donation_id'] ) ) { | |
| 570 | - | |
| 571 | - // Set currency if not already done. | |
| 572 | - if( empty( $args['currency'] ) ) { | |
| 573 | - $args['currency'] = give_get_payment_currency_code( $args['donation_id'] ); | |
| 574 | - } | |
| 575 | - | |
| 576 | - // Set amount if not already done. | |
| 577 | - if( empty( $args['amount'] ) ) { | |
| 578 | - $args['amount'] = give_donation_amount( $args['donation_id'] ); | |
| 579 | - } | |
| 580 | - } | |
| 581 | - | |
| 536 | +function give_format_decimal( $amount, $dp = false, $sanitize = true ) { | |
| 582 | 537 | $decimal_separator = give_get_price_decimal_separator(); |
| 583 | - $formatted_amount = $args['sanitize'] ? | |
| 584 | - give_maybe_sanitize_amount( $args['amount'], array( 'number_decimals' => $args['dp'], 'currency' => $args['currency'] ) ) : | |
| 585 | - number_format( $args['amount'], ( is_bool( $args['dp'] ) ? give_get_price_decimals( $args['currency'] ) : $args['dp'] ), '.', '' ); | |
| 538 | + $formatted_amount = $sanitize ? | |
| 539 | + give_maybe_sanitize_amount( $amount, array( 'number_decimals' => $dp ) ) : | |
| 540 | + number_format( $amount, ( is_bool( $dp ) ? give_get_price_decimals() : $dp ), '.', '' ); | |
| 586 | 541 | |
| 587 | 542 | if ( false !== strpos( $formatted_amount, '.' ) ) { |
| 588 | 543 | $formatted_amount = str_replace( '.', $decimal_separator, $formatted_amount ); |
| 589 | 544 | } |
| 590 | 545 | |
| 591 | - return apply_filters( 'give_format_decimal', $formatted_amount, $args['amount'], $decimal_separator, $args ); | |
| 546 | + return apply_filters( 'give_format_decimal', $formatted_amount, $amount, $decimal_separator ); | |
| 592 | 547 | } |
| 593 | 548 | |
| 594 | 549 | /** |
| 595 | 550 | * Get date format string on basis of given context. |
| @@ -664,9 +619,9 @@ | ||
| 664 | 619 | function give_clean( $var ) { |
| 665 | 620 | if ( is_array( $var ) ) { |
| 666 | 621 | return array_map( 'give_clean', $var ); |
| 667 | 622 | } else { |
| 668 | - return is_scalar( $var ) ? sanitize_text_field( wp_unslash( $var ) ) : $var; | |
| 623 | + return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; | |
| 669 | 624 | } |
| 670 | 625 | } |
| 671 | 626 | |
| 672 | 627 | /** |
| @@ -701,11 +656,11 @@ | ||
| 701 | 656 | * Verify nonce. |
| 702 | 657 | * |
| 703 | 658 | * @since 1.8 |
| 704 | 659 | * |
| 705 | - * @param string $nonce Nonce Hash. | |
| 706 | - * @param int $action Nonce verification action. | |
| 707 | - * @param array $wp_die_args Nonce fail arguments. | |
| 660 | + * @param $nonce | |
| 661 | + * @param int $action | |
| 662 | + * @param array $wp_die_args | |
| 708 | 663 | * |
| 709 | 664 | * @return bool |
| 710 | 665 | */ |
| 711 | 666 | function give_validate_nonce( $nonce, $action = - 1, $wp_die_args = array() ) { |
| @@ -725,9 +680,9 @@ | ||
| 725 | 680 | 'message' => __( 'Nonce verification has failed.', 'give' ), |
| 726 | 681 | 'title' => __( 'Error', 'give' ), |
| 727 | 682 | 'args' => array( |
| 728 | 683 | 'response' => 403, |
| 729 | - ), | |
| 684 | + ) | |
| 730 | 685 | ) |
| 731 | 686 | ); |
| 732 | 687 | |
| 733 | 688 | wp_die( |
| @@ -744,17 +699,19 @@ | ||
| 744 | 699 | * Verify nonce while processing donation form. |
| 745 | 700 | * |
| 746 | 701 | * @since 2.0 |
| 747 | 702 | * |
| 748 | - * @param string $nonce Nonce value. | |
| 749 | - * @param int $form_id Donation Form ID. | |
| 703 | + * @param string $nonce Pass nonce value. | |
| 750 | 704 | * |
| 751 | 705 | * @return bool |
| 752 | 706 | */ |
| 753 | -function give_verify_donation_form_nonce( $nonce = '', $form_id ) { | |
| 707 | +function give_verify_donation_form_nonce( $nonce = '' ) { | |
| 708 | + // Get nonce key from donation. | |
| 709 | + $nonce = empty( $nonce ) ? give_clean( $_POST['_wpnonce'] ) : $nonce; | |
| 710 | + $form_id = isset( $_POST['give-form-id'] ) ? absint( $_POST['give-form-id'] ) : 0; | |
| 754 | 711 | |
| 755 | 712 | // Form nonce action. |
| 756 | - $nonce_action = "give_donation_form_nonce_{$form_id}"; | |
| 713 | + $nonce_action = "donation_form_nonce_{$form_id}"; | |
| 757 | 714 | |
| 758 | 715 | // Nonce validation. |
| 759 | 716 | $verify_nonce = give_validate_nonce( $nonce, $nonce_action ); |
| 760 | 717 | |
| @@ -773,9 +730,9 @@ | ||
| 773 | 730 | * @since 1.8 |
| 774 | 731 | * |
| 775 | 732 | * @param $variable |
| 776 | 733 | * @param string (optional) $conditional default value: isset |
| 777 | - * @param mixed (optional) $default default value: false | |
| 734 | + * @param bool (optional) $default default value: false | |
| 778 | 735 | * @param string (optional) $array_key_name default value: false |
| 779 | 736 | * |
| 780 | 737 | * @return mixed |
| 781 | 738 | */ |