1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 23
24 function edd_sanitize_amount( $amount ) {
25 global $edd_options;
26
27 $thousands_sep = ! empty( $edd_options['thousands_separator'] ) ? $edd_options['thousands_separator'] : ',';
28 $decimal_sep = ! empty( $edd_options['decimal_separator'] ) ? $edd_options['decimal_separator'] : '.';
29
30
31 if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) {
32 if ( $thousands_sep == '.' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
33 $amount = str_replace( $thousands_sep, '', $amount );
34 }
35
36 $amount = str_replace( $decimal_sep, '.', $amount );
37 }
38
39 return apply_filters( 'edd_sanitize_amount', $amount );
40 }
41
42 43 44 45 46 47 48
49 function edd_format_amount( $amount ) {
50 global $edd_options;
51
52
53 $amount= "$amount";
54
55 if ( '' == $amount ) {
56 $label = edd_get_label_singular();
57 $string = sprintf( __('%1$s Not Available', 'edd' ), $label );
58 return apply_filters( 'edd_price_not_available_text', $string );
59 }
60
61 $thousands_sep = ! empty( $edd_options['thousands_separator'] ) ? $edd_options['thousands_separator'] : ',';
62 $decimal_sep = ! empty( $edd_options['decimal_separator'] ) ? $edd_options['decimal_separator'] : '.';
63
64
65 if ( $decimal_sep == ',' && false !== ( $found = strpos( $amount, $decimal_sep ) ) ) {
66 $whole = substr( $amount, 0, $sep_found );
67 $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) );
68 $amount = $whole . '.' . $part;
69 }
70
71
72 if ( $thousands_sep == ',' && false !== ( $found = strpos( $amount, $thousands_sep ) ) ) {
73 $amount = str_replace( ',', '', $amount );
74 }
75
76 $decimals = apply_filters( 'edd_format_amount_decimals', 2 );
77
78 return number_format( $amount, $decimals, $decimal_sep, $thousands_sep );
79 }
80
81 82 83 84 85 86 87
88 function edd_currency_filter( $price ) {
89 global $edd_options;
90
91 $currency = isset( $edd_options['currency'] ) ? $edd_options['currency'] : 'USD';
92 $position = isset( $edd_options['currency_position'] ) ? $edd_options['currency_position'] : 'before';
93
94 if ( $position == 'before' ):
95 switch ( $currency ):
96 case "GBP" : return '£' . $price; break;
97 case "BRL" : return 'R$' . $price; break;
98 case "USD" :
99 case "AUD" :
100 case "CAD" :
101 case "HKD" :
102 case "MXN" :
103 case "SGD" :
104 return '$' . $price;
105 break;
106 case "JPY" : return '¥' . $price; break;
107 default :
108 $formatted = $currency . ' ' . $price;
109 return apply_filters( 'edd_' . strtolower( $currency ) . '_currency_filter_before', $formatted, $currency, $price );
110 break;
111 endswitch;
112 else :
113 switch ( $currency ) :
114 case "GBP" : return $price . '£'; break;
115 case "BRL" : return $price . 'R$'; break;
116 case "USD" :
117 case "AUD" :
118 case "BRL" :
119 case "CAD" :
120 case "HKD" :
121 case "MXN" :
122 case "SGD" :
123 return $price . '$';
124 break;
125 case "JPY" : return $price . '¥'; break;
126 default :
127 $formatted = $price . ' ' . $currency;
128 return apply_filters( 'edd_' . strtolower( $currency ) . '_currency_filter_after', $formatted, $currency, $price );
129 break;
130 endswitch;
131 endif;
132 }
133
134 135 136 137 138 139 140
141 function edd_currency_decimal_filter( $decimals = 2 ) {
142 global $edd_options;
143
144 $currency = isset( $edd_options['currency'] ) ? $edd_options['currency'] : 'USD';
145
146 switch ( $currency ) {
147 case 'RIAL' :
148 $decimals = 0;
149 break;
150
151 case 'JPY' :
152 $decimals = 0;
153 break;
154 }
155
156 return $decimals;
157 }
158 add_filter( 'edd_format_amount_decimals', 'edd_currency_decimal_filter' );