Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_currency_decimal_filter
  • edd_currency_filter
  • edd_format_amount
  • edd_sanitize_amount
  1 <?php
  2 /**
  3  * Formatting functions for taking care of proper number formats and such
  4  *
  5  * @package     EDD
  6  * @subpackage  Functions/Formatting
  7  * @copyright   Copyright (c) 2013, Pippin Williamson
  8  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9  * @since       1.2
 10 */
 11 
 12 // Exit if accessed directly
 13 if ( ! defined( 'ABSPATH' ) ) exit;
 14 
 15 /**
 16  * Sanitize Amount
 17  *
 18  * Returns a sanitized amount by stripping out thousands separators.
 19  *
 20  * @since 1.0
 21  * @param string $amount Price amount to format
 22  * @return string $amount Newly sanitized amount
 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     // Sanitize the amount
 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  * Returns a nicely formatted amount.
 44  *
 45  * @since 1.0
 46  * @param string $amount Price amount to format
 47  * @return string $amount Newly formatted amount or Price Not Available
 48  */
 49 function edd_format_amount( $amount ) {
 50     global $edd_options;
 51 
 52     // If no price was given for the download
 53     $amount= "$amount"; // The Anti-Geczy check
 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     // Format the amount
 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     // Strip , from the amount (if set as the thousands separator)
 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  * Formats the currency display
 83  *
 84  * @since 1.0
 85  * @param string $price Price
 86  * @return array $currency Currencies displayed correctly
 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 '&pound;' . $price; break;
 97             case "BRL" : return 'R&#36;' . $price; break;
 98             case "USD" :
 99             case "AUD" :
100             case "CAD" :
101             case "HKD" :
102             case "MXN" :
103             case "SGD" :
104                 return '&#36;' . $price;
105             break;
106             case "JPY" : return '&yen;' . $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 . '&pound;'; break;
115             case "BRL" : return $price . 'R&#36;'; 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 . '&#36;';
124             break;
125             case "JPY" : return $price . '&yen;'; 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  * Set the number of decimal places per currency
136  *
137  * @since 1.4.2
138  * @param int $decimals Number of decimal places
139  * @return int $decimals
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' );
Easy Digital Downloads API documentation generated by ApiGen 2.8.0