-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheurofxref.php
376 lines (327 loc) · 13.9 KB
/
eurofxref.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Plugin Name: Euro FxRef Currency Converter
* Plugin URI: https://wordpress.org/plugins/euro-fxref-currency-converter/
* Description: Adds the [currency] and [currency_legal] shortcodes to convert currencies based on the ECB reference exchange rates. Please visit <a href="https://wordpress.org/plugins/euro-fxref-currency-converter/" target="_blank">the plugin page on WordPress.org</a> for help and options.
* Text Domain: euro-fxref-currency-converter
* Version: 2.0.2
* Author: Joost de Keijzer
* Author URI: https://dkzr.nl/
* Requires at least: 3.3
* Requires PHP: 7.0
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Development code at https://github.com/joostdekeijzer/wp_eurofxref
*
* This plugin is based on the Xclamation Currency Converter Shortcode plugin.
* See http://www.xclamationdesign.co.uk/free-currency-converter-shortcode-plugin-for-wordpress/
* for more information.
*/
if ( !function_exists( 'add_action' ) ) {
echo "Hi there! I'm just a plugin, not much I can do when called directly.";
exit;
}
class EuroFxRef {
static protected $euroFxRef;
const TRANSIENT_LABEL = 'EuroFxRefRates';
public $space = ' ';
public function __construct() {
// for testing
//delete_transient( self::TRANSIENT_LABEL );
add_shortcode( 'currency', array( $this, 'currency_shortcode' ) );
add_shortcode( 'currency_legal', array( $this, 'currency_legal_shortcode' ) );
add_action( 'admin_head', array( $this, 'insert_help_tab' ) );
}
/**
* Available for static calling.
*/
public static function legal_string( $prepend = '* ' ) {
return self::currency_legal_shortcode( array( 'prepend' => $prepend ) );
}
public static function convert( $amount = 0, $from = 'EUR', $to = 'USD' ) {
$from = strtoupper( $from );
$to = strtoupper( $to );
if( ( 'EUR' != $from && null === self::getEuroFxRef( $from ) ) || ( 'EUR' != $to && null === self::getEuroFxRef( $to ) ) ) {
return false;
}
if( 'EUR' != $from && 'EUR' != $to ) {
// normalize on Euro
$amount = self::convert( $amount, $from, 'EUR' );
$from = 'EUR';
}
if( 'EUR' == $from ) {
// from Euro to ...
return $amount * self::getEuroFxRef( $to );
} else {
// from ... to Euro
return $amount / self::getEuroFxRef( $from );
}
}
/**
* Available for static calling.
*/
public function currency_converter( $atts ) {
_deprecated_function( __METHOD__, 'euro-fxref-currency-converter-2.0.1', 'EuroFxRef::currency_shortcode' );
return $this->currency_shortcode( $atts );
}
public function currency_shortcode( $atts ) {
extract( shortcode_atts( array(
'from' => 'EUR',
'to' => 'USD',
'amount' => 1,
'iso' => false,
'show_from' => true,
'between' => ' / ',
'append' => ' *',
'round' => true,
'round_append' => '=',
'no_from_show_rate' => true,
'to_style' => 'cursor:help;border-bottom:1px dotted gray;',
//'flag' => '',
), $atts, 'currency' ) );
// fix booleans
foreach( array( 'iso', 'show_from', 'no_from_show_rate', 'round' ) as $var ) {
$$var = $this->_bool_from_string( $$var );
}
$currency = $this->get_currency_symbols();
$number_format = $this->get_number_formats();
if( !isset( $currency[ $from ] ) || !isset( $currency[ $to ] ) ) {
$currency[ $from ] = $currency[ $to ] = '';
$number_format[ $from ] = $number_format[ $to ] = array( 'dp' => ',', 'ts' => '.' );
$iso = true;
}
$cAmount = self::convert( floatval( $amount ), $from, $to );
if( false !== $cAmount ) {
$cAmount = number_format( $cAmount, ( $round ? 0 : 2 ), $number_format[$to]['dp'], $number_format[$to]['ts'] );
if( $round && '' != $round_append ) $cAmount .= $number_format[$to]['dp'] . $round_append;
}
$amount = number_format( $amount, ( $round ? 0 : 2 ), $number_format[$from]['dp'], $number_format[$from]['ts'] );
if( $round && '' != $round_append ) $amount .= $number_format[$from]['dp'] . $round_append;
$s = $this->space;
if( $show_from || false === $cAmount ) {
if( $iso ) {
$output = $amount . $s . $from;
if( false !== $cAmount )
$output .= $between . $cAmount . $s . $to;
} else {
$output = $currency[$from] . $s . $amount;
if( false !== $cAmount )
$output .= $between . $currency[$to] . $s . $cAmount;
}
} else {
if( $iso ) {
$output = $cAmount . $s . $to;
} else {
$output = $currency[$to] . $s . $cAmount;
}
if ( $no_from_show_rate ) {
$cOne = number_format( self::convert( 1, strtoupper( $from ), strtoupper( $to ) ), 4, $number_format[$to]['dp'], $number_format[$to]['ts'] );
$output = sprintf( '<span class="eurofxref-conversion-rate" style="%s" title="%s">%s</span>', esc_attr( $to_style ), esc_attr( "1 {$from} = {$cOne} {$to}" ), $output );
}
}
if ( $append ) {
$output .= sprintf( '<span class="eurofxref-append-string">%s</span>', $append );
}
return $output;
}
public static function currency_legal_shortcode( $atts ) {
$atts = shortcode_atts( array(
'prepend' => '* ',
), $atts, 'currency_legal' );
$output = '';
if ( $atts['prepend'] ) {
$output = sprintf( '<span class="eurofxref-prepend-string">%s</span>', $atts['prepend'] );
}
return $output . sprintf( __( 'For informational purposes only. Exchange rates may vary. Based on <a href="%s" target="_blank">ECB reference rates</a>.', 'euro-fxref-currency-converter' ), 'https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/' );
}
public function insert_help_tab() {
global $post_type;
$screen = get_current_screen();
if( 'post' != $screen->base || !post_type_supports($post_type, 'editor') ) return;
$id = __CLASS__ . '_help_id';
$title = __( "Using the [currency] shortcodes", __CLASS__ );
$help = <<<EOH
<p><strong>currency</strong> shortcode examples:<br/>
<code>[currency from="EUR" to="GBP" amount="10"]</code><br/>
<code>[currency from="JPY" to="CHF" amount="750"]</code></p>
<p>Full example with default values:<br/>
<code>[currency from="EUR" to="USD" amount="1" iso=false show_from=true between="&nbsp;/&nbsp;" append="&nbsp;*" round=true round_append="=" to_style="cursor:help;border-bottom:1px dotted gray;"]</code></p>
<p><strong>currency_legal</strong> shortcode examples:<br/>
<code>[currency_legal]</code><br/>
<code>[currency_legal prepend='* ']</code> (full example with default value)</p>
<p>The legal text is:<br/>
'For informational purposes only. Exchange rates may vary. Based on <a href="https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/" target="_blank">ECB reference rates</a>.'</p>
<p><strong>Need more help?</strong><br/>
Please visit <a href="https://wordpress.org/plugins/euro-fxref-currency-converter/" target="_blank">https://wordpress.org/plugins/euro-fxref-currency-converter/</a> for more examples and a full list of supported currencies.</p>
EOH;
$screen->add_help_tab( array(
'id' => $id,
'title' => $title,
'content' => $help,
) );
}
/**
* First copied from xclam_currency_converter, modified with WooCommerce `WooCommerce\Functions wc-core-functions.php` and `WooCommerce\i18n currency-info.php` input.
*
* @see https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html
*
* $symbols['LTL'] = 'Lt'; // Lithuanian Litas - not published since 2020-01-02
* $symbols['LVL'] = 'Ls'; // Latvian Lats - not published since 2020-01-02
* $symbols['RUB'] = '₽'; // Russian Rouble - not published since 2022-06-01
* $symbols['HRK'] = 'kn'; // Croatian Kuna - not published since 2023-01-02
*/
public function get_currency_symbols() {
$symbols = apply_filters(
'eurofxref_currency_symbols',
array(
'AUD' => '$', // Australian dollar
'BGN' => 'лв.', // Bulgarian lev
'BRL' => 'R$', // Brazilian real
'CAD' => '$', // Canadian dollar
'CHF' => 'CHF', // Swiss franc
'CNY' => '¥', // Chinese yuan
'CZK' => 'Kč', // Czech koruna
'DKK' => 'kr.', // Danish krone
'EUR' => '€', // Euro
'GBP' => '£', // Pound sterling
'HKD' => '$', // Hong Kong dollar
'HUF' => 'Ft', // Hungarian forint
'IDR' => 'Rp', // Indonesian rupiah
'ILS' => '₪', // Israeli new shekel
'INR' => '₹', // Indian rupee
'ISK' => 'kr.', // Icelandic króna
'JPY' => '¥', // Japanese yen
'KRW' => '₩', // South Korean won
'MXN' => '$', // Mexican peso
'MYR' => 'RM', // Malaysian ringgit
'NOK' => 'kr', // Norwegian krone
'NZD' => '$', // New Zealand dollar
'PHP' => '₱', // Philippine peso
'PLN' => 'zł', // Polish złoty
'RON' => 'lei', // Romanian leu
'SEK' => 'kr', // Swedish krona
'SGD' => '$', // Singapore dollar
'THB' => '฿', // Thai baht
'TRY' => '₺', // Turkish lira
'USD' => '$', // United States (US) dollar
'ZAR' => 'R', // South African rand
)
);
return apply_filters( 'woocommerce_currency_symbols', $symbols );
}
/**
* Copied from xclam_currency_converter
*/
public function get_number_formats() {
$formats = apply_filters(
'eurofxref_number_formats',
array(
'AUD' => array( 'dp' => '.', 'ts' => ',' ),
'BGN' => array( 'dp' => ',', 'ts' => ' ', 'after' => true ),
'BRL' => array( 'dp' => ',', 'ts' => '.' ),
'CAD' => array( 'dp' => '.', 'ts' => ',' ),
'CHF' => array( 'dp' => '.', 'ts' => '’', 'after' => true ),
'CNY' => array( 'dp' => '.', 'ts' => ',' ),
'CZK' => array( 'dp' => '.', 'ts' => ' ', 'after' => true ),
'DKK' => array( 'dp' => ',', 'ts' => '.', 'after' => true ),
'EUR' => array( 'dp' => ',', 'ts' => '.' , 'after' => false ),
'GBP' => array( 'dp' => '.', 'ts' => ',' ),
'HKD' => array( 'dp' => '.', 'ts' => ',' ),
'HUF' => array( 'dp' => ',', 'ts' => ' ', 'after' => true, 'round' => true ),
'IDR' => array( 'dp' => ',', 'ts' => '.', 'round' => true ),
'ILS' => array( 'dp' => '.', 'ts' => ',', 'after' => true ),
'INR' => array( 'dp' => '.', 'ts' => ',' ),
'ISK' => array( 'dp' => '.', 'ts' => ',' ),
'JPY' => array( 'dp' => '.', 'ts' => ',', 'round' => true ),
'KRW' => array( 'dp' => '.', 'ts' => ',', 'round' => true ),
'MXN' => array( 'dp' => '.', 'ts' => ',' ),
'MYR' => array( 'dp' => '.', 'ts' => ',' ),
'NOK' => array( 'dp' => ',', 'ts' => ' ' ),
'NZD' => array( 'dp' => '.', 'ts' => ',' ),
'PHP' => array( 'dp' => '.', 'ts' => ',' ),
'PLN' => array( 'dp' => ',', 'ts' => ' ', 'after' => true ),
'RON' => array( 'dp' => ',', 'ts' => '.', 'after' => true ),
'SEK' => array( 'dp' => ',', 'ts' => ' ', 'after' => true ),
'SGD' => array( 'dp' => '.', 'ts' => ',' ),
'THB' => array( 'dp' => '.', 'ts' => ',' ),
'TRY' => array( 'dp' => ',', 'ts' => '.', 'after' => true ),
'USD' => array( 'dp' => '.', 'ts' => ',' ),
'ZAR' => array( 'dp' => ',', 'ts' => ' ' ),
)
);
return $formats;
}
protected static function getEuroFxRef( $currency = null ) {
global $wp_version;
if( !isset(self::$euroFxRef) || false == self::$euroFxRef || 0 == count(self::$euroFxRef) ) {
self::$euroFxRef = get_transient( self::TRANSIENT_LABEL );
if( false == self::$euroFxRef || 0 == count(self::$euroFxRef) ) {
//This is a PHP(5)script example on how eurofxref-daily.xml can be parsed
//the file is updated daily at 16:00 CET
//Read eurofxref-daily.xml file in memory
//For the next command you will need the config option allow_url_fopen=On (default)
$response = wp_remote_get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', array('user-agent' => 'Euro FxRef Currency Converter plugin on WordPress/' . $wp_version . '; ' . home_url()));
self::$euroFxRef = array();
if( !is_wp_error( $response ) ) {
libxml_use_internal_errors(true);
$fxRefXml = simplexml_load_string( $response['body'] );
if ( libxml_get_errors() ) {
return null;
}
$fxRefDateString = (string) $fxRefXml->Cube->Cube['time'];
foreach($fxRefXml->Cube->Cube->Cube as $rate) {
self::$euroFxRef[ (string) $rate['currency'] ] = (float) $rate['rate'];
}
/**
* Calculate transient expiration to try update around 17h00 daily
* with a minimum of 15 minutes and a maximum of 6 hours.
* ECB publishes around 16h00 daily so around 17h00 the new rates should be published.
*
* All calculated in Seconds since the Unix Epoch to support PHP 5.2
*/
$pubEpoch = date_format( new DateTime( $fxRefDateString, new DateTimeZone('CET') ), 'U' );
$pubEpoch += 60 * 60 * 17; // add 17h to be around 1 (one) hour past actual publication date
$pubEpoch += 60 * 60 * 24; // add 24h for NEXT publication date
$transient_expiration = min( 60 * 60 * 6, max( 60 * 15, $pubEpoch - date_format( new DateTime( 'now', new DateTimeZone('CET') ), 'U' ) ) );
set_transient( self::TRANSIENT_LABEL, self::$euroFxRef, $transient_expiration );
}
}
}
if( isset($currency) ) {
if( isset( self::$euroFxRef[$currency] ) ) {
return self::$euroFxRef[$currency];
} else {
return null;
}
} else {
return self::$euroFxRef;
}
}
/**
* converts strings and integers to boolean values.
* 0, "0", false, "FALSE", "no", 'n' etc. becomes (bool) false
* all other becomes (bool) true.
*
* The function itself defaults to (bool) false
*
* Also see http://php.net/manual/en/function.is-bool.php#93165
*/
private function _bool_from_string( $val = false ) {
if( is_bool( $val ) ) return $val;
$val = strtolower( trim( $val ) );
if(
'false' == $val ||
'null' == $val ||
'off' == $val ||
'no' == $val ||
'n' == $val ||
'0' == $val
) {
return false;
} else {
return true;
}
}
}
$EuroFxRef = new EuroFxRef();