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
HelperByLanguage.php
47 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\MultiCurrency\Resolver; |
| 4 | |
| 5 | use WCML\MultiCurrency\Geolocation; |
| 6 | use WCML\MultiCurrency\Settings; |
| 7 | use WCML\StandAlone\NullSitePress; |
| 8 | use WPML\FP\Fns; |
| 9 | use function WCML\functions\getSitePress; |
| 10 | |
| 11 | class HelperByLanguage { |
| 12 | |
| 13 | private static $getCurrency; |
| 14 | |
| 15 | public static function getCurrencyByUserCountry( $currentLang ) { |
| 16 | if ( ! self::$getCurrency ) { |
| 17 | self::$getCurrency = Fns::memorize( function() use ( $currentLang ) { |
| 18 | $clientCountry = Geolocation::getUserCountry(); |
| 19 | $currency = Geolocation::getOfficialCurrencyCodeByCountry( $clientCountry ); |
| 20 | |
| 21 | if ( ! Settings::isValidCurrencyForLang( $currency, $currentLang ) ) { |
| 22 | $currency = Settings::getFirstAvailableCurrencyForLang( $currentLang ); |
| 23 | } |
| 24 | |
| 25 | return $currency ?: null; |
| 26 | } ); |
| 27 | } |
| 28 | |
| 29 | return call_user_func( self::$getCurrency ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | public static function getCurrentLanguage() { |
| 34 | $currentLang = getSitePress()->get_current_language(); |
| 35 | |
| 36 | if ( in_array( $currentLang, [ 'all', null, false ], true ) ) { |
| 37 | $currentLang = getSitePress()->get_default_language(); |
| 38 | } |
| 39 | |
| 40 | if ( ! is_string( $currentLang ) ) { |
| 41 | $currentLang = ( new NullSitePress() )->get_current_language(); |
| 42 | } |
| 43 | |
| 44 | return $currentLang; |
| 45 | } |
| 46 | } |
| 47 |