_exchangeRate = new \Akky\Money\WordPressCachableExchangeRate(); $this->_taggers[] = new \Akky\Money\Usd( function($value) { if ($value === 0) { return; } return '[acc value="' . $value . '" currency="usd"]'; } ); $this->_taggers[] = new \Akky\Money\Jpy( function($value) { if ($value === 0) { return; } return '[acc value="' . $value . '" currency="jpy"]'; } ); } /** * pass-1: let converters to find money notations and add short codes * * @Filter(tag="the_content") */ public function tagifyConvertedMoney($text) { if (has_tag('acc_disable')) { return $text; } // if the post was modified before the begin date set by plugin, pass. $options = get_option(self::DB_OPTION_KEY); if (is_array($options) && array_key_exists('begin_date', $options)) { $begin_date = $options['begin_date']; $modified_date = get_the_modified_date( 'Y-m-d' ); // we can asssume that the both date format are the same in Y-m-d if ($begin_date > $modified_date) { return $text; } } foreach ($this->_taggers as $tagger) { $text = $tagger->apply($text); } return $text; } /** * pass-2: extract short code * * @Shortcode(tag="acc") */ public function shortCode( $params ) { // parameters check if (!array_key_exists('value', $params)) { return ''; } if (!array_key_exists('currency', $params)) { return ''; } $value = (float) $params['value']; static $supportedCurrencies = array( 'jpy', 'usd' ); $currency = $params['currency']; if (!in_array($currency, $supportedCurrencies)) { $currency = 'jpy'; } static $supportedLocales = array( 'ja', 'en' ); if (array_key_exists('locale', $params)) { $locale = $params['locale']; } else { $locale = substr(get_locale(), 0, 2); } if (!in_array($locale, $supportedLocales)) { $locale = 'en'; } try { switch ($currency) { case 'usd': $jpy = $this->_exchangeRate->convert($value, 'usd', 'jpy'); if ($jpy > 1000) { $jpy = floor($jpy); } return '(' . \Akky\Money\JpyFormatter::format($jpy, $locale) . ')'; case 'jpy': default: $usd = $this->_exchangeRate->convert($value, 'jpy', 'usd'); if ($usd > 1000) { $usd = floor($usd); } return '(' . \Akky\Money\UsdFormatter::format($usd, $locale) . ')'; } } catch (\RuntimeException $ex) { // for case rate info unavailable, do nothing return ''; } } /** * * @Action(tag="admin_menu") */ public function registerMenu() { add_options_page( __( self::DISPLAY_NAME . ' Setting', 'auto_currency_converter' ), __( self::DISPLAY_NAME, 'auto_currency_converter' ), 'administrator', self::PLUGIN_KEY, array( &$this, 'callbackRenderForm') ); } public function callbackRenderForm() { echo '
' . __( 'Your PHP is using intl extension. Auto conversion runs much faster.', 'auto_currency_converter' ) . '
'; } else { echo '' . __( 'Your PHP does not have intl extension, so this plugin is using PHP-based slower formatter, also only works well in English. If possible, I receommend to turn on php_intl extension.', 'auto_currency_converter' ) . '
'; } } public function callbackRenderUsage() { echo '' . __( 'Add exchanged ammount after the money notations in posts/pages.', 'auto_currency_converter' ) . '
'; echo '' . __( 'example:'; echo '
"$199,666"
in a post will be changed to
"$199,666(¥15,973,280)"
The real ammount will be different as the plugin to use the latest exchange rate."', 'auto_currency_converter' ) . '
' . __( 'At now, only US dollars and Japanese yen are supported.', 'auto_currency_converter' ) . '
'; echo '' . __( 'tag acc_disable suppress this plugin to convert only on that post.', 'auto_currency_converter' ) . '
'; } public function callbackRenderBeginDate() { echo '' . __( 'Set a date after which you make the conversion effective. Only on the pages/posts which modified after the date will have converted money info, so your past posts which you manually added similar info will not be affected.', 'auto_currency_converter' ) . '
'; } public function callbackRenderBeginDateField() { $options = get_option(self::DB_OPTION_KEY); $begin_date = ''; if (is_array($options) && array_key_exists('begin_date', $options)) { $begin_date = $options['begin_date']; } echo ''; } }