RGCurrency

The Gravity Forms RGCurrency class.

Defined (1)

The class is defined in the following location(s).

/currency.php  
  1. class RGCurrency { 
  2. private $currency; 
  3.  
  4. public function __construct( $currency ) { 
  5. if ( is_array( $currency ) ) { 
  6. $this->currency = $currency; 
  7. } else { 
  8. $this->currency = self::get_currency( $currency ); 
  9.  
  10. public function to_number( $text ) { 
  11. $text = strval( $text ); 
  12.  
  13. if ( is_numeric( $text ) ) { 
  14. return floatval( $text ); 
  15.  
  16. //Making sure symbol is in unicode format (i.e. ᅜ) 
  17. $text = preg_replace( '/&.*?;/', '', $text ); 
  18.  
  19. //Removing symbol from text 
  20. $text = str_replace( $this->currency['symbol_right'], '', $text ); 
  21. $text = str_replace( $this->currency['symbol_left'], '', $text ); 
  22. if ( ! empty( $this->currency['symbol_old'] ) ) { 
  23. $text = str_replace( $this->currency['symbol_old'], '', $text ); 
  24.  
  25. //Removing all non-numeric characters 
  26. $array = str_split( $text ); 
  27. $is_negative = false; 
  28. $clean_number = ''; 
  29. foreach ( $array as $char ) { 
  30.  
  31. if ( ( $char >= '0' && $char <= '9' ) || $char == $this->currency['decimal_separator'] ) { 
  32. $clean_number .= $char; 
  33. } else if ( $char == '-' ) { 
  34. $is_negative = true; 
  35.  
  36. $decimal_separator = $this->currency && $this->currency['decimal_separator'] ? $this->currency['decimal_separator'] : '.'; 
  37.  
  38. //Removing thousand separators but keeping decimal point 
  39. $array = str_split( $clean_number ); 
  40. $float_number = ''; 
  41. for ( $i = 0, $count = sizeof( $array ); $i < $count; $i ++ ) { 
  42. $char = $array[ $i ]; 
  43.  
  44. if ( $char >= '0' && $char <= '9' ) { 
  45. $float_number .= $char; 
  46. } else if ( $char == $decimal_separator ) { 
  47. $float_number .= '.'; 
  48.  
  49. if ( $is_negative ) { 
  50. $float_number = '-' . $float_number; 
  51.  
  52. return is_numeric( $float_number ) ? floatval( $float_number ) : false; 
  53.  
  54. public function to_money( $number, $do_encode = false ) { 
  55.  
  56. if ( ! is_numeric( $number ) ) { 
  57. $number = $this->to_number( $number ); 
  58.  
  59. if ( $number === false ) { 
  60. return ''; 
  61.  
  62. $negative = ''; 
  63. if ( strpos( strval( $number ), '-' ) !== false ) { 
  64. $negative = '-'; 
  65. $number = floatval( substr( $number, 1 ) ); 
  66.  
  67. $money = number_format( $number, $this->currency['decimals'], $this->currency['decimal_separator'], $this->currency['thousand_separator'] ); 
  68.  
  69. if ( $money == '0.00' ) { 
  70. $negative = ''; 
  71.  
  72. $symbol_left = ! empty( $this->currency['symbol_left'] ) ? $this->currency['symbol_left'] . $this->currency['symbol_padding'] : ''; 
  73. $symbol_right = ! empty( $this->currency['symbol_right'] ) ? $this->currency['symbol_padding'] . $this->currency['symbol_right'] : ''; 
  74.  
  75. if ( $do_encode ) { 
  76. $symbol_left = html_entity_decode( $symbol_left ); 
  77. $symbol_right = html_entity_decode( $symbol_right ); 
  78.  
  79. return $negative . $symbol_left . $money . $symbol_right; 
  80.  
  81. public static function get_currency( $code ) { 
  82. $currencies = self::get_currencies(); 
  83.  
  84. return $currencies[ $code ]; 
  85.  
  86. public function is_zero_decimal() { 
  87.  
  88. return empty( $this->currency['decimals'] ); 
  89.  
  90. public static function get_currencies() { 
  91. $currencies = array( 
  92. 'USD' => array( 'name' => esc_html__( 'U.S. Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => '', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  93. 'GBP' => array( 'name' => esc_html__( 'Pound Sterling', 'gravityforms' ), 'symbol_left' => '£', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  94. 'EUR' => array( 'name' => esc_html__( 'Euro', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => '€', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  95. 'AUD' => array( 'name' => esc_html__( 'Australian Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  96. 'BRL' => array( 'name' => esc_html__( 'Brazilian Real', 'gravityforms' ), 'symbol_left' => 'R$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  97. 'CAD' => array( 'name' => esc_html__( 'Canadian Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => 'CAD', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  98. 'CZK' => array( 'name' => esc_html__( 'Czech Koruna', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => 'Kč', 'symbol_padding' => ' ', 'thousand_separator' => ' ', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  99. 'DKK' => array( 'name' => esc_html__( 'Danish Krone', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => 'kr.', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  100. 'HKD' => array( 'name' => esc_html__( 'Hong Kong Dollar', 'gravityforms' ), 'symbol_left' => 'HK$', 'symbol_right' => '', 'symbol_padding' => '', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  101. 'HUF' => array( 'name' => esc_html__( 'Hungarian Forint', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => 'Ft', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  102. 'ILS' => array( 'name' => esc_html__( 'Israeli New Sheqel', 'gravityforms' ), 'symbol_left' => '₪', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  103. 'JPY' => array( 'name' => esc_html__( 'Japanese Yen', 'gravityforms' ), 'symbol_left' => '¥', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '', 'decimals' => 0 ),  
  104. 'MYR' => array( 'name' => esc_html__( 'Malaysian Ringgit', 'gravityforms' ), 'symbol_left' => 'RM', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  105. 'MXN' => array( 'name' => esc_html__( 'Mexican Peso', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  106. 'NOK' => array( 'name' => esc_html__( 'Norwegian Krone', 'gravityforms' ), 'symbol_left' => 'Kr', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  107. 'NZD' => array( 'name' => esc_html__( 'New Zealand Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  108. 'PHP' => array( 'name' => esc_html__( 'Philippine Peso', 'gravityforms' ), 'symbol_left' => 'Php', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  109. 'PLN' => array( 'name' => esc_html__( 'Polish Zloty', 'gravityforms' ), 'symbol_left' => 'zł', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => '.', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  110. 'RUB' => array( 'name' => esc_html__( 'Russian Ruble', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => 'py*', 'symbol_padding' => ' ', 'thousand_separator' => ' ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  111. 'SGD' => array( 'name' => esc_html__( 'Singapore Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  112. 'ZAR' => array( 'name' => esc_html__( 'South African Rand', 'gravityforms' ), 'symbol_left' => 'R', 'symbol_right' => '', 'symbol_padding' => '', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  113. 'SEK' => array( 'name' => esc_html__( 'Swedish Krona', 'gravityforms' ), 'symbol_left' => '', 'symbol_right' => 'Kr', 'symbol_padding' => ' ', 'thousand_separator' => ' ', 'decimal_separator' => ', ', 'decimals' => 2 ),  
  114. 'CHF' => array( 'name' => esc_html__( 'Swiss Franc', 'gravityforms' ), 'symbol_left' => 'CHF', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => "'", 'decimal_separator' => '.', 'decimals' => 2, 'symbol_old' => 'Fr.' ),  
  115. 'TWD' => array( 'name' => esc_html__( 'Taiwan New Dollar', 'gravityforms' ), 'symbol_left' => '$', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  116. 'THB' => array( 'name' => esc_html__( 'Thai Baht', 'gravityforms' ), 'symbol_left' => '฿', 'symbol_right' => '', 'symbol_padding' => ' ', 'thousand_separator' => ', ', 'decimal_separator' => '.', 'decimals' => 2 ),  
  117. ); 
  118.  
  119. return apply_filters( 'gform_currencies', $currencies );