currency-switcher
4 weeks ago
class-wcml-custom-prices.php
4 weeks ago
class-wcml-multi-currency-configuration.php
4 weeks ago
class-wcml-multi-currency-coupons.php
1 year ago
class-wcml-multi-currency-install.php
4 weeks ago
class-wcml-multi-currency-orders.php
4 weeks ago
class-wcml-multi-currency-prices.php
4 weeks ago
class-wcml-multi-currency-reports.php
4 weeks ago
class-wcml-multi-currency-resources.php
4 weeks ago
class-wcml-multi-currency-shipping.php
4 weeks ago
class-wcml-multi-currency-table-rate-shipping.php
4 weeks ago
class-wcml-multi-currency.php
4 weeks ago
class-wcml-price-filter.php
4 weeks ago
class-wcml-w3tc-multi-currency.php
4 weeks ago
class-wcml-multi-currency.php
463 lines
| 1 | <?php |
| 2 | |
| 3 | use WCML\MultiCurrency\Resolver\Factory as ResolverFactory; |
| 4 | use WCML\MultiCurrency\Resolver\HelperByLanguage as ResolverHelperByLang; |
| 5 | use WCML\Rest\Functions; |
| 6 | use WPML\FP\Obj; |
| 7 | use function WCML\functions\getSitePress; |
| 8 | use function WPML\Container\make; |
| 9 | use WCML\MultiCurrency\ExchangeRateServices\Service; |
| 10 | use WCML\MultiCurrency\ExchangeRateServices\Fixerio; |
| 11 | use WCML\MultiCurrency\ExchangeRateServices\CurrencyLayer; |
| 12 | use WCML\MultiCurrency\ExchangeRateServices\ExchangeRatesApi; |
| 13 | use WCML\MultiCurrency\ExchangeRateServices\OpenExchangeRates; |
| 14 | use WPML\API\Sanitize; |
| 15 | |
| 16 | class WCML_Multi_Currency { |
| 17 | |
| 18 | const CURRENCY_STORAGE_KEY = 'client_currency'; |
| 19 | const CURRENCY_LANGUAGE_STORAGE_KEY = 'client_currency_language'; |
| 20 | |
| 21 | public $currencies = []; |
| 22 | public $currency_codes = []; |
| 23 | |
| 24 | private $default_currency; |
| 25 | private $client_currency; |
| 26 | private $exchange_rates = []; |
| 27 | public $currencies_without_cents = [ 'JPY', 'TWD', 'KRW', 'BIF', 'BYR', 'CLP', 'GNF', 'ISK', 'KMF', 'PYG', 'RWF', 'VUV', 'XAF', 'XOF', 'XPF' ]; |
| 28 | |
| 29 | public $prices; |
| 30 | public $coupons; |
| 31 | public $shipping; |
| 32 | |
| 33 | public $reports; |
| 34 | public $orders; |
| 35 | public $admin_currency_selector; |
| 36 | public $custom_prices; |
| 37 | public $currency_switcher; |
| 38 | public $currency_switcher_ajax; |
| 39 | public $install; |
| 40 | public $W3TC = null; |
| 41 | |
| 42 | public $woocommerce_wpml; |
| 43 | |
| 44 | public $woocommerce; |
| 45 | |
| 46 | public $exchange_rate_services; |
| 47 | |
| 48 | public $currencies_payment_gateways; |
| 49 | |
| 50 | public $load_filters; |
| 51 | |
| 52 | public $switching_currency_html; |
| 53 | |
| 54 | private $rest_currency; |
| 55 | |
| 56 | public function __construct() { |
| 57 | global $woocommerce_wpml, $woocommerce, $wpdb, $wp; |
| 58 | |
| 59 | $sitepress = getSitePress(); |
| 60 | |
| 61 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 62 | $this->woocommerce = $woocommerce; |
| 63 | |
| 64 | $this->install = new WCML_Multi_Currency_Install( $this, $woocommerce_wpml ); |
| 65 | |
| 66 | $this->init_currencies(); |
| 67 | |
| 68 | $this->load_filters = $this->are_filters_need_loading(); |
| 69 | $this->prices = new WCML_Multi_Currency_Prices( $this, $woocommerce_wpml->get_setting( 'currency_options' ) ); |
| 70 | $this->prices->add_hooks(); |
| 71 | if ( $this->load_filters ) { |
| 72 | $table_rate_shipping_multi_currency = new WCML_Multi_Currency_Table_Rate_Shipping(); |
| 73 | $table_rate_shipping_multi_currency->add_hooks(); |
| 74 | |
| 75 | $this->coupons = new WCML_Multi_Currency_Coupons(); |
| 76 | $this->shipping = new WCML_Multi_Currency_Shipping( $this, $wpdb ); |
| 77 | $this->shipping->add_hooks(); |
| 78 | } |
| 79 | $this->reports = new WCML_Multi_Currency_Reports( $woocommerce_wpml, $wpdb ); |
| 80 | $this->reports->add_hooks(); |
| 81 | $this->orders = new WCML_Multi_Currency_Orders( $this, $woocommerce_wpml, $wp ); |
| 82 | $this->admin_currency_selector = new WCML_Admin_Currency_Selector( |
| 83 | $woocommerce_wpml, |
| 84 | new WCML_Admin_Cookie( '_wcml_dashboard_currency' ) |
| 85 | ); |
| 86 | $this->admin_currency_selector->add_hooks(); |
| 87 | $this->custom_prices = new WCML_Custom_Prices( $woocommerce_wpml, $wpdb ); |
| 88 | $this->custom_prices->add_hooks(); |
| 89 | $this->currency_switcher = new WCML_Currency_Switcher( $woocommerce_wpml, $sitepress ); |
| 90 | $this->currency_switcher->add_hooks(); |
| 91 | $this->currency_switcher_ajax = new WCML_Currency_Switcher_Ajax( $woocommerce_wpml ); |
| 92 | |
| 93 | $this->exchange_rate_services = make( \WCML_Exchange_Rates::class ); |
| 94 | $this->exchange_rate_services->initialize_settings(); |
| 95 | $this->exchange_rate_services->add_actions(); |
| 96 | |
| 97 | wpml_collect( |
| 98 | [ |
| 99 | new CurrencyLayer(), |
| 100 | new ExchangeRatesApi(), |
| 101 | new Fixerio(), |
| 102 | new OpenExchangeRates(), |
| 103 | ] |
| 104 | )->each( |
| 105 | function( Service $service ) { |
| 106 | $this->exchange_rate_services->add_service( $service->getId(), $service ); |
| 107 | } |
| 108 | ); |
| 109 | |
| 110 | $this->currencies_payment_gateways = make( WCML_Currencies_Payment_Gateways::class ); |
| 111 | $this->currencies_payment_gateways->add_hooks(); |
| 112 | |
| 113 | if ( defined( 'W3TC' ) ) { |
| 114 | $this->W3TC = new WCML_W3TC_Multi_Currency(); |
| 115 | } |
| 116 | |
| 117 | WCML_Multi_Currency_Resources::set_up( $this, $this->woocommerce_wpml ); |
| 118 | WCML_Multi_Currency_Configuration::set_up( $this, $woocommerce_wpml ); |
| 119 | |
| 120 | add_filter( 'init', [ $this, 'init' ], 5 ); |
| 121 | |
| 122 | if ( wp_doing_ajax() ) { |
| 123 | add_action( 'wp_ajax_nopriv_wcml_switch_currency', [ $this, 'switch_currency' ] ); |
| 124 | add_action( 'wp_ajax_wcml_switch_currency', [ $this, 'switch_currency' ] ); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | public function are_filters_need_loading() { |
| 130 | $load = false; |
| 131 | |
| 132 | if ( ! is_admin() && $this->get_client_currency() !== wcml_get_woocommerce_currency_option() ) { |
| 133 | $load = true; |
| 134 | } else { |
| 135 | if ( wp_doing_ajax() && $this->get_client_currency() !== wcml_get_woocommerce_currency_option() ) { |
| 136 | |
| 137 | $ajax_actions = apply_filters( |
| 138 | 'wcml_multi_currency_ajax_actions', |
| 139 | [ |
| 140 | 'woocommerce_get_refreshed_fragments', |
| 141 | 'woocommerce_update_order_review', |
| 142 | 'woocommerce_checkout', |
| 143 | 'woocommerce_add_to_cart', |
| 144 | 'woocommerce_update_shipping_method', |
| 145 | 'woocommerce_json_search_products_and_variations', |
| 146 | 'woocommerce_add_coupon_discount', |
| 147 | |
| 148 | ] |
| 149 | ); |
| 150 | |
| 151 | if ( ( isset( $_POST['action'] ) && in_array( $_POST['action'], $ajax_actions ) ) || |
| 152 | ( isset( $_GET['action'] ) && in_array( $_GET['action'], $ajax_actions ) ) ) { |
| 153 | $load = true; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | $load = apply_filters( 'wcml_load_multi_currency', $load ); |
| 159 | |
| 160 | $load = apply_filters( 'wcml_load_multi_currency_in_ajax', $load ); |
| 161 | |
| 162 | return $load; |
| 163 | } |
| 164 | |
| 165 | public function init() { |
| 166 | |
| 167 | add_filter( 'wcml_get_client_currency', [ $this, 'get_client_currency' ] ); |
| 168 | add_action( 'wcml_multi_currency_set_switching_currency_html', function( $html ) { |
| 169 | $this->switching_currency_html = $html; |
| 170 | } ); |
| 171 | add_action( 'wp_footer', [ $this, 'maybe_show_switching_currency_prompt_dialog' ] ); |
| 172 | add_action( 'wp_footer', [ $this, 'maybe_reset_cart_fragments' ] ); |
| 173 | |
| 174 | if ( WCML\Rest\Functions::isRestApiRequest() ) { |
| 175 | add_filter( 'rest_request_before_callbacks', [ $this, 'set_request_currency' ], 10, 3 ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function enable() { |
| 180 | $this->woocommerce_wpml->settings['enable_multi_currency'] = WCML_MULTI_CURRENCIES_INDEPENDENT; |
| 181 | $this->woocommerce_wpml->update_settings(); |
| 182 | } |
| 183 | |
| 184 | public function disable() { |
| 185 | $this->woocommerce_wpml->settings['enable_multi_currency'] = WCML_MULTI_CURRENCIES_DISABLED; |
| 186 | $this->woocommerce_wpml->update_settings(); |
| 187 | } |
| 188 | |
| 189 | public function init_currencies() { |
| 190 | $sitepress = getSitePress(); |
| 191 | |
| 192 | $this->default_currency = wcml_get_woocommerce_currency_option(); |
| 193 | $this->currencies = $this->woocommerce_wpml->settings['currency_options']; |
| 194 | |
| 195 | if ( ! empty( $this->default_currency ) && ! isset( $this->currencies[ $this->default_currency ] ) ) { |
| 196 | $this->currencies[ $this->default_currency ] = []; |
| 197 | } |
| 198 | |
| 199 | $save_to_db = false; |
| 200 | |
| 201 | $active_languages = $sitepress->get_active_languages(); |
| 202 | |
| 203 | $currency_defaults = [ |
| 204 | 'rate' => 0, |
| 205 | 'position' => 'left', |
| 206 | 'thousand_sep' => ',', |
| 207 | 'decimal_sep' => '.', |
| 208 | 'num_decimals' => 2, |
| 209 | 'rounding' => 'disabled', |
| 210 | 'rounding_increment' => 1, |
| 211 | 'auto_subtract' => 0, |
| 212 | 'location_mode' => 'all', |
| 213 | 'countries' => [], |
| 214 | ]; |
| 215 | |
| 216 | foreach ( $this->currencies as $code => $currency ) { |
| 217 | foreach ( $currency_defaults as $key => $val ) { |
| 218 | if ( ! isset( $currency[ $key ] ) ) { |
| 219 | $this->currencies[ $code ][ $key ] = $val; |
| 220 | $save_to_db = true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | foreach ( $active_languages as $language ) { |
| 225 | if ( ! isset( $currency['languages'][ $language['code'] ] ) ) { |
| 226 | $this->currencies[ $code ]['languages'][ $language['code'] ] = 1; |
| 227 | $save_to_db = true; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | $this->currency_codes = array_keys( $this->currencies ); |
| 233 | |
| 234 | foreach ( $active_languages as $language ) { |
| 235 | if ( ! isset( $this->woocommerce_wpml->settings['default_currencies'][ $language['code'] ] ) ) { |
| 236 | $this->woocommerce_wpml->settings['default_currencies'][ $language['code'] ] = 0; |
| 237 | $save_to_db = true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if ( isset( $this->woocommerce_wpml->settings['default_currencies'] ) ) { |
| 242 | foreach ( $this->woocommerce_wpml->settings['default_currencies'] as $language => $value ) { |
| 243 | if ( ! isset( $active_languages[ $language ] ) ) { |
| 244 | unset( $this->woocommerce_wpml->settings['default_currencies'][ $language ] ); |
| 245 | $save_to_db = true; |
| 246 | } |
| 247 | if ( ! empty( $value ) && ! in_array( $value, $this->currency_codes ) && $value !== 'location' ) { |
| 248 | $this->woocommerce_wpml->settings['default_currencies'][ $language ] = 0; |
| 249 | $save_to_db = true; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if ( isset( $this->woocommerce_wpml->settings['currencies_order'] ) ) { |
| 255 | foreach ( $this->currency_codes as $currency ) { |
| 256 | if ( ! in_array( $currency, $this->woocommerce_wpml->settings['currencies_order'] ) ) { |
| 257 | $this->woocommerce_wpml->settings['currencies_order'][] = $currency; |
| 258 | $save_to_db = true; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if ( $save_to_db && is_admin() ) { |
| 264 | $this->woocommerce_wpml->settings['currency_options'] = $this->currencies; |
| 265 | $this->woocommerce_wpml->update_settings(); |
| 266 | } |
| 267 | |
| 268 | if ( empty( $this->default_currency ) ) { |
| 269 | $this->woocommerce_wpml->settings['enable_multi_currency'] = WCML_MULTI_CURRENCIES_DISABLED; |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | |
| 274 | public function get_default_currency() { |
| 275 | return $this->default_currency; |
| 276 | } |
| 277 | |
| 278 | public function get_currencies( $include_default = false ) { |
| 279 | |
| 280 | $currencies = []; |
| 281 | $default_currency = wcml_get_woocommerce_currency_option(); |
| 282 | |
| 283 | foreach ( $this->currencies as $key => $value ) { |
| 284 | if ( $default_currency != $key || $include_default ) { |
| 285 | $currencies[ $key ] = $value; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return $currencies; |
| 290 | } |
| 291 | |
| 292 | public function get_currency_codes() { |
| 293 | return $this->currency_codes; |
| 294 | } |
| 295 | |
| 296 | public function is_currency_active( $code ) { |
| 297 | return in_array( $code, $this->get_currency_codes(), true ); |
| 298 | } |
| 299 | |
| 300 | public function get_currency_code() { |
| 301 | $currency_code = wcml_get_woocommerce_currency_option(); |
| 302 | $currency_codes = $this->get_currency_codes(); |
| 303 | if ( ! in_array( $currency_code, $currency_codes, true ) ) { |
| 304 | $currency_code = $this->woocommerce_wpml->multi_currency->get_default_currency(); |
| 305 | } |
| 306 | |
| 307 | return $currency_code; |
| 308 | } |
| 309 | |
| 310 | public function get_currency_details_by_code( $code ) { |
| 311 | |
| 312 | if ( isset( $this->currencies[ $code ] ) ) { |
| 313 | return $this->currencies[ $code ]; |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | public function delete_currency_by_code( $code, $settings = false, $update = true ) { |
| 320 | $settings = $settings ?: $this->woocommerce_wpml->get_settings(); |
| 321 | unset( $settings['currency_options'][ $code ] ); |
| 322 | |
| 323 | if ( isset( $settings['currencies_order'] ) ) { |
| 324 | foreach ( $settings['currencies_order'] as $key => $cur_code ) { |
| 325 | if ( $cur_code == $code ) { |
| 326 | unset( $settings['currencies_order'][ $key ] ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if ( $update ) { |
| 332 | $this->woocommerce_wpml->update_settings( $settings ); |
| 333 | } |
| 334 | |
| 335 | return $settings; |
| 336 | } |
| 337 | |
| 338 | public function get_exchange_rates() { |
| 339 | |
| 340 | if ( empty( $this->exchange_rates ) ) { |
| 341 | |
| 342 | $this->exchange_rates = [ wcml_get_woocommerce_currency_option() => 1 ]; |
| 343 | $woo_currencies = get_woocommerce_currencies(); |
| 344 | |
| 345 | $currencies = $this->get_currencies(); |
| 346 | foreach ( $currencies as $code => $currency ) { |
| 347 | if ( ! empty( $woo_currencies[ $code ] ) ) { |
| 348 | $this->exchange_rates[ $code ] = $currency['rate']; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return apply_filters( 'wcml_exchange_rates', $this->exchange_rates ); |
| 354 | } |
| 355 | |
| 356 | public function get_client_currency() { |
| 357 | if ( Functions::isRestApiRequest() ) { |
| 358 | return $this->get_rest_currency(); |
| 359 | } elseif ( ! wcml_is_multi_currency_on() ) { |
| 360 | return apply_filters( 'wcml_client_currency', wcml_get_woocommerce_currency_option() ); |
| 361 | } elseif ( null === $this->client_currency ) { |
| 362 | $this->client_currency = ResolverFactory::create()->getClientCurrency(); |
| 363 | |
| 364 | if ( $this->client_currency ) { |
| 365 | wcml_user_store_set( self::CURRENCY_STORAGE_KEY, $this->client_currency ); |
| 366 | wcml_user_store_set( self::CURRENCY_LANGUAGE_STORAGE_KEY, ResolverHelperByLang::getCurrentLanguage() ); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | $filteredCurrency = apply_filters( 'wcml_client_currency', $this->client_currency ); |
| 371 | |
| 372 | if ( $filteredCurrency !== $this->client_currency ) { |
| 373 | $this->client_currency = $filteredCurrency; |
| 374 | wcml_user_store_set( self::CURRENCY_STORAGE_KEY, $this->client_currency ); |
| 375 | } |
| 376 | |
| 377 | return $this->client_currency; |
| 378 | } |
| 379 | |
| 380 | public function maybe_show_switching_currency_prompt_dialog() { |
| 381 | if ( $this->switching_currency_html ) { |
| 382 | echo $this->switching_currency_html; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | public function maybe_reset_cart_fragments() { |
| 387 | if ( wcml_user_store_get( 'client_currency_switched' ) ) { |
| 388 | ?> |
| 389 | <script type="text/javascript"> |
| 390 | jQuery(function () { |
| 391 | wcml_reset_cart_fragments(); |
| 392 | }); |
| 393 | </script> |
| 394 | <?php |
| 395 | wcml_user_store_set( 'client_currency_switched', false ); |
| 396 | } |
| 397 | |
| 398 | } |
| 399 | |
| 400 | public function set_client_currency( $currency ) { |
| 401 | $this->client_currency = $currency; |
| 402 | |
| 403 | wcml_user_store_set( self::CURRENCY_STORAGE_KEY, $currency ); |
| 404 | wcml_user_store_set( self::CURRENCY_LANGUAGE_STORAGE_KEY, getSitePress()->get_current_language() ); |
| 405 | |
| 406 | do_action( 'wcml_set_client_currency', $currency ); |
| 407 | |
| 408 | } |
| 409 | |
| 410 | public function switch_currency() { |
| 411 | $sitepress = getSitePress(); |
| 412 | |
| 413 | $currency = filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 414 | $force_switch = filter_input( INPUT_POST, 'force_switch', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 415 | parse_str( Sanitize::stringProp( 'params', $_POST ), $params ); |
| 416 | $from_currency = $this->client_currency; |
| 417 | |
| 418 | do_action( 'wcml_before_switch_currency', $currency, $force_switch ); |
| 419 | |
| 420 | if ( ! $force_switch && apply_filters( 'wcml_switch_currency_exception', false, $from_currency, $currency ) ) { |
| 421 | die(); |
| 422 | } |
| 423 | |
| 424 | $lang = Obj::prop( 'lang', $params ); |
| 425 | |
| 426 | if ( $lang ) { |
| 427 | $sitepress->switch_lang( $lang ); |
| 428 | } |
| 429 | |
| 430 | $this->set_client_currency( $currency ); |
| 431 | |
| 432 | global $woocommerce, $current_user; |
| 433 | if ( empty( $woocommerce->session->data ) && empty( $current_user->ID ) ) { |
| 434 | $woocommerce->session->set_customer_session_cookie( true ); |
| 435 | } |
| 436 | |
| 437 | wcml_user_store_set( 'client_currency_switched', true ); |
| 438 | |
| 439 | do_action( 'wcml_switch_currency', $currency, $from_currency ); |
| 440 | |
| 441 | $response = $this->prices->filter_pre_selected_widget_prices_in_new_currency( [], $currency, $from_currency, $params ); |
| 442 | |
| 443 | wp_send_json_success( $response ); |
| 444 | } |
| 445 | |
| 446 | public function get_currencies_without_cents() { |
| 447 | |
| 448 | return apply_filters( 'wcml_currencies_without_cents', $this->currencies_without_cents ); |
| 449 | } |
| 450 | |
| 451 | public function set_request_currency( $response, $handler, $request ) { |
| 452 | $this->rest_currency = Obj::prop( 'currency', $request->get_params() ) ?: wcml_get_woocommerce_currency_option(); |
| 453 | $this->client_currency = $this->rest_currency; |
| 454 | |
| 455 | return $response; |
| 456 | } |
| 457 | |
| 458 | public function get_rest_currency() { |
| 459 | return $this->rest_currency ?: wcml_get_woocommerce_currency_option(); |
| 460 | } |
| 461 | |
| 462 | } |
| 463 |