ComposedResolver.php
1 week ago
Factory.php
1 week ago
HelperByLanguage.php
1 week ago
HelperByLocation.php
1 week ago
Resolver.php
1 week ago
ResolverForContext.php
1 week ago
ResolverForDefault.php
1 week ago
ResolverForModeLanguage.php
1 week ago
ResolverForModeLocation.php
1 week ago
ResolverForModeLanguage.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\MultiCurrency\Resolver; |
| 4 | |
| 5 | use WCML\MultiCurrency\Settings; |
| 6 | use WCML_Multi_Currency; |
| 7 | use WPML\FP\Logic; |
| 8 | |
| 9 | class ResolverForModeLanguage implements Resolver { |
| 10 | |
| 11 | public function getClientCurrency() { |
| 12 | $currentLang = HelperByLanguage::getCurrentLanguage(); |
| 13 | $storedLang = wcml_user_store_get( WCML_Multi_Currency::CURRENCY_LANGUAGE_STORAGE_KEY ); |
| 14 | $storedCurrency = wcml_user_store_get( WCML_Multi_Currency::CURRENCY_STORAGE_KEY ); |
| 15 | |
| 16 | $getInitialCurrencyForLang = function() use ( $currentLang ) { |
| 17 | return Settings::isDefaultCurrencyByLocationForLang( $currentLang ) |
| 18 | ? HelperByLanguage::getCurrencyByUserCountry( $currentLang ) |
| 19 | : Settings::getDefaultCurrencyForLang( $currentLang ); |
| 20 | }; |
| 21 | |
| 22 | $reInitCurrencyIfLangHasChanged = function() use ( $currentLang, $storedLang, $storedCurrency, $getInitialCurrencyForLang ) { |
| 23 | $hasChangedLang = $storedLang && $currentLang !== $storedLang; |
| 24 | |
| 25 | if ( $hasChangedLang ) { |
| 26 | $initialCurrencyForLang = $getInitialCurrencyForLang(); |
| 27 | |
| 28 | if ( $initialCurrencyForLang ) { |
| 29 | $preventSwitching = apply_filters( 'wcml_switch_currency_exception', false, $storedCurrency, $initialCurrencyForLang, true ); |
| 30 | |
| 31 | if ( ! array_key_exists( 'force_switch', $_POST ) && $preventSwitching ) { |
| 32 | do_action( 'wcml_multi_currency_set_switching_currency_html', $preventSwitching['prevent_switching'] ); |
| 33 | } |
| 34 | |
| 35 | return $initialCurrencyForLang; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return null; |
| 40 | }; |
| 41 | |
| 42 | $getStoredCurrencyIfValid = function() use ( $currentLang, $storedCurrency ) { |
| 43 | return Settings::isValidCurrencyForLang( $storedCurrency, $currentLang ) |
| 44 | ? $storedCurrency |
| 45 | : null; |
| 46 | }; |
| 47 | |
| 48 | $getDefaultCurrencyIfValid = function() use ( $currentLang ) { |
| 49 | $defaultCurrency = wcml_get_woocommerce_currency_option(); |
| 50 | |
| 51 | return Settings::isValidCurrencyForLang( $defaultCurrency, $currentLang ) |
| 52 | ? $defaultCurrency |
| 53 | : null; |
| 54 | }; |
| 55 | |
| 56 | $getFirstAvailableCurrencyForLang = function() use ( $currentLang ) { |
| 57 | return Settings::getFirstAvailableCurrencyForLang( $currentLang ); |
| 58 | }; |
| 59 | |
| 60 | $resolve = Logic::firstSatisfying( |
| 61 | Logic::isTruthy(), |
| 62 | [ |
| 63 | $reInitCurrencyIfLangHasChanged, |
| 64 | $getStoredCurrencyIfValid, |
| 65 | $getInitialCurrencyForLang, |
| 66 | $getDefaultCurrencyIfValid, |
| 67 | $getFirstAvailableCurrencyForLang, |
| 68 | ] |
| 69 | ); |
| 70 | |
| 71 | return $resolve( null ); |
| 72 | } |
| 73 | } |
| 74 |