| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Currency utilities. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\Utilities |
| 16 |
* @since 110531 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_utils_cur')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Currency utilities. |
| 25 |
* |
| 26 |
* @package s2Member\Utilities |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_utils_cur |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Currency converter. |
| 33 |
* |
| 34 |
* Uses the Google currency conversion API. |
| 35 |
* |
| 36 |
* @package s2Member\Utilities |
| 37 |
* @since 3.5 |
| 38 |
* |
| 39 |
* @param int|float|string $a The amount, in ``$from``. |
| 40 |
* @param string $from Three character currency code. |
| 41 |
* @param string $to Three character currency code. |
| 42 |
* |
| 43 |
* @return string A numeric amount in ``$to``, after having been converted. Else false. |
| 44 |
*/ |
| 45 |
public static function convert($a = 0, $from = '', $to = '') |
| 46 |
{ |
| 47 |
if(is_numeric($a) && strlen($from) === 3 && strlen($to) === 3) |
| 48 |
{ |
| 49 |
$regex = '/\<span\s+class\s*\=\s*(?:["\'])?bld(?:["\'])?\s*\>(?P<conversion>[0-9.]+)\s+'.preg_quote($to, '/').'\s*\<\/span\>/i'; |
| 50 |
$endpoint = 'http://www.google.com/finance/converter?a='.urlencode($a).'&from='.urlencode($from).'&to='.urlencode($to); |
| 51 |
|
| 52 |
$prefix = 's2m_cur'; // Transient prefix. |
| 53 |
$transient = $prefix.md5('s2member_cur_convert_'.$endpoint); |
| 54 |
|
| 55 |
if(!($response = get_transient($transient))) |
| 56 |
{ |
| 57 |
$response = c_ws_plugin__s2member_utils_urls::remote($endpoint); |
| 58 |
set_transient($transient, $response, DAY_IN_SECONDS / 2); |
| 59 |
} |
| 60 |
if($response && preg_match($regex, $response, $m)) |
| 61 |
return number_format((float)$m['conversion'], 2, '.', ''); |
| 62 |
} |
| 63 |
return ''; // Default return value. |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Converts Currency Codes to Currency Symbols. |
| 68 |
* |
| 69 |
* Defaults to the `$` dollar sign. |
| 70 |
* |
| 71 |
* @package s2Member\Utilities |
| 72 |
* @since 110531 |
| 73 |
* |
| 74 |
* @param string $currency Expects a 3 character Currency Code. |
| 75 |
* |
| 76 |
* @return string A Currency Symbol. Defaults to the `$` sign. |
| 77 |
*/ |
| 78 |
public static function symbol($currency = '') |
| 79 |
{ |
| 80 |
$symbols['AUD'] = '$'; // Australian Dollar |
| 81 |
$symbols['BRL'] = 'R$'; // Brazilian Real |
| 82 |
$symbols['CAD'] = '$'; // Canadian Dollar |
| 83 |
$symbols['CZK'] = 'Kč'; // Czech Koruna |
| 84 |
$symbols['DKK'] = 'kr'; // Danish Krone |
| 85 |
$symbols['EUR'] = '€'; // Euro |
| 86 |
$symbols['HKD'] = '$'; // Hong Kong Dollar |
| 87 |
$symbols['HUF'] = 'Ft'; // Hungarian Forint |
| 88 |
$symbols['ILS'] = '₪'; // Israeli New Sheqel |
| 89 |
$symbols['INR'] = 'Rs'; // Indian Rupee |
| 90 |
$symbols['JPY'] = '¥'; // Japanese Yen |
| 91 |
$symbols['MYR'] = 'RM'; // Malaysian Ringgit |
| 92 |
$symbols['MXN'] = '$'; // Mexican Peso |
| 93 |
$symbols['NOK'] = 'kr'; // Norwegian Krone |
| 94 |
$symbols['NZD'] = '$'; // New Zealand Dollar |
| 95 |
$symbols['PHP'] = 'Php'; // Philippine Peso |
| 96 |
$symbols['PLN'] = 'zł'; // Polish Zloty |
| 97 |
$symbols['GBP'] = '£'; // Pound Sterling |
| 98 |
$symbols['SGD'] = '$'; // Singapore Dollar |
| 99 |
$symbols['SEK'] = 'kr'; // Swedish Krona |
| 100 |
$symbols['CHF'] = 'CHF'; // Swiss Franc |
| 101 |
$symbols['TWD'] = 'NT$'; // Taiwan New Dollar |
| 102 |
$symbols['THB'] = '฿'; // Thai Baht |
| 103 |
$symbols['USD'] = '$'; // U.S. Dollar |
| 104 |
|
| 105 |
if(($currency = strtoupper($currency)) && !empty($symbols[$currency])) |
| 106 |
return $symbols[$currency]; |
| 107 |
return '$'; // Else `$` sign (default value). |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|