first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
{
"name": "wpdesk\/wc-currency-switchers-integrations",
"authors": [
{
"name": "Grzegorz",
"email": "grola@wpdesk.pl"
}
],
"license": "MIT",
"require": {
"php": ">=7.0",
"psr\/container": "^1.0",
"wpdesk\/wp-builder": "^2.0",
"psr\/log": "^1.1"
},
"require-dev": {
"phpunit\/phpunit": "^5",
"wp-coding-standards\/wpcs": "^0.14.1",
"squizlabs\/php_codesniffer": "^3.0.2",
"wimg\/php-compatibility": "^8",
"mockery\/mockery": "*",
"10up\/wp_mock": "*"
},
"autoload": {
"psr-4": {
"FSVendor\\WPDesk\\WooCommerce\\CurrencySwitchers\\": "src\/"
}
},
"autoload-dev": {},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}

View File

@@ -0,0 +1,34 @@
<?php
/**
* Abstract converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
/**
* Abstract class for converters.
*/
abstract class AbstractConverter implements \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\SwitcherConverter, \Psr\Log\LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @inheritDoc
*/
abstract function convert($value);
/**
* @inheritDoc
*/
public function convert_array($values)
{
foreach ($values as $key => $value) {
if ($value) {
$values[$key] = $this->convert($value);
}
}
return $values;
}
}

View File

@@ -0,0 +1,51 @@
<?php
/**
* Filter converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers;
use Psr\Log\LoggerInterface;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Can add filter to convert currency.
*/
class FilterConverter implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
/**
* @var AbstractConverter
*/
private $converter;
/**
* @var string
*/
private $shipping_method_id;
/**
* @param SwitcherConverter $converter
* @param string $shipping_method_id
*/
public function __construct(\FSVendor\WPDesk\WooCommerce\CurrencySwitchers\SwitcherConverter $converter, $shipping_method_id)
{
$this->converter = $converter;
$this->shipping_method_id = $shipping_method_id;
}
public function hooks()
{
\add_filter($this->shipping_method_id . '/currency-switchers/amount', [$this, 'convert'], 10, 2);
}
/**
* @param float $amount_in_shop_currency
* @param LoggerInterface $logger
*
* @return float
*/
public function convert($amount_in_shop_currency, $logger = null)
{
if ($logger instanceof \Psr\Log\LoggerInterface) {
$this->converter->setLogger($logger);
}
return $this->converter->convert($amount_in_shop_currency);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/**
* Filter converters.
*
* @package WPDesk\WooCommerce\CurrencySwitchers
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher;
/**
* Can create filter converters.
*/
class FilterConvertersFactory implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
const PRIORITY_AFTER_DEFAULT = 100;
/**
* @var string
*/
private $shipping_method_id;
/**
* @param string $shipping_method_id .
*/
public function __construct($shipping_method_id)
{
$this->shipping_method_id = $shipping_method_id;
}
/**
* Hooks.
*/
public function hooks()
{
\add_action('woocommerce_multicurrency_loaded', array($this, 'create_woocommerce_multicurrency_filter_converter'));
\add_action('woocommerce_init', [$this, 'create_currency_switcher_woocommerce_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
\add_action('woocommerce_init', [$this, 'create_wcml_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
\add_action('woocommerce_init', [$this, 'create_aelia_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
\add_action('woocommerce_init', [$this, 'create_fox_currency_switcher_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
\add_action('woocommerce_init', [$this, 'create_wmcs_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
\add_action('woocommerce_init', [$this, 'create_curcy_filter_converter'], self::PRIORITY_AFTER_DEFAULT);
}
public function create_woocommerce_multicurrency_filter_converter()
{
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency\Converter(), $this->shipping_method_id))->hooks();
}
public function create_currency_switcher_woocommerce_filter_converter()
{
$alg_get_current_currency_code = 'alg_get_current_currency_code';
// php scoper faker.
$alg_wc_cs_get_currency_exchange_rate = 'alg_wc_cs_get_currency_exchange_rate';
// php scoper faker.
if (\function_exists($alg_get_current_currency_code) && \function_exists($alg_wc_cs_get_currency_exchange_rate)) {
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CurrencySwitcherWoocommerce\Converter(), $this->shipping_method_id))->hooks();
}
}
public function create_wcml_filter_converter()
{
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WCML\Converter(), $this->shipping_method_id))->hooks();
}
public function create_aelia_filter_converter()
{
$class = 'Aelia' . '\\WC\\CurrencySwitcher\\WC_Aelia_CurrencySwitcher';
// php scoper faker
if (\class_exists($class)) {
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\Aelia\Converter(), $this->shipping_method_id))->hooks();
}
}
public function create_fox_currency_switcher_filter_converter()
{
if (isset($GLOBALS['WOOCS'])) {
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\FoxCurrencySwitcher\Converter(), $this->shipping_method_id))->hooks();
}
}
public function create_wmcs_filter_converter()
{
$wmcs_convert_price = 'wmcs_convert_price';
// php scoper faker.
if (\function_exists($wmcs_convert_price)) {
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WMCS\Converter(), $this->shipping_method_id))->hooks();
}
}
public function create_curcy_filter_converter()
{
$wmc_get_price = 'wmc_get_price';
// php scoper faker.
if (\function_exists($wmc_get_price)) {
(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\FilterConverter(new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CURCY\Converter(), $this->shipping_method_id))->hooks();
}
}
}

View File

@@ -0,0 +1,45 @@
<?php
/**
* Shipping integrations.
*
* @package WPDesk\WooCommerce\CurrencySwitchers
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency\ShippingMethodIntegration;
/**
* Can create integrations for shipping methods.
* Creates integrations for plugins which do not works by default, ie. WooCommerce MultiCurrency.
* @see https://woocommerce.com/products/multi-currency/
*/
class ShippingIntegrations implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
/**
* @var string
*/
private $shipping_method_id;
/**
* @param string $shipping_method_id .
*/
public function __construct($shipping_method_id)
{
$this->shipping_method_id = $shipping_method_id;
}
/**
* Hooks.
*/
public function hooks()
{
\add_action('woocommerce_loaded', array($this, 'add_integrations_for_shipping_method'));
}
/**
* Add integration to WooCommerce MultiCurrency for shipping method.
*/
public function add_integrations_for_shipping_method()
{
$woocommerce_multicurrency_integration = new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency\ShippingMethodIntegration($this->shipping_method_id);
$woocommerce_multicurrency_integration->hooks();
}
}

View File

@@ -0,0 +1,37 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\Aelia
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\Aelia;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using Aelia Currency Switcher plugin.
* @see https://aelia.co/shop/currency-switcher-woocommerce/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
$class = 'Aelia' . '\\WC\\CurrencySwitcher\\WC_Aelia_CurrencySwitcher';
// php scoper faker
$aelia = $class::instance();
$aelia_settings = $class::settings();
$from_currency = $aelia_settings->base_currency();
$to_currency = $aelia->get_selected_currency();
return $aelia->convert($value, $from_currency, $to_currency);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\CURCY
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CURCY;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using CURCY Multi Currency for WooCommerce plugin.
* @see https://wordpress.org/plugins/woo-multi-currency/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
return wmc_get_price($value);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\CurrencySwitcherWoocommerce
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CurrencySwitcherWoocommerce;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using Currency Switcher for WooCommerce plugin.
* @see https://wordpress.org/plugins/currency-switcher-woocommerce/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
$currency_exchange_rate = alg_wc_cs_get_currency_exchange_rate(alg_get_current_currency_code());
return $value * $currency_exchange_rate;
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,33 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\FoxCurrencySwitcher
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\FoxCurrencySwitcher;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
use function FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CurrencySwitcherWoocommerce\alg_get_current_currency_code;
use function FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\CurrencySwitcherWoocommerce\alg_wc_cs_get_currency_exchange_rate;
/**
* Can convert currency using FOX Currency Switcher Professional for WooCommerce plugin.
* @see https://wordpress.org/plugins/woocommerce-currency-switcher/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
return $GLOBALS['WOOCS'] ? $GLOBALS['WOOCS']->woocs_exchange_value($value) : $value;
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\WCML
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WCML;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using Currency Switcher for WooCommerce plugin.
* @see https://wordpress.org/plugins/woocommerce-multilingual/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
return \apply_filters('wcml_raw_price_amount', $value);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\WMCS
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WMCS;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using WMCS plugin.
* Unknown plugin URL. This is legacy from Flexible Shipping
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
return wmcs_convert_price($value);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/**
* Currency converter.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency;
use FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter;
/**
* Can convert currency using WooCommerce MultiCurrency plugin.
* @see https://woocommerce.com/products/multi-currency/
*/
class Converter extends \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\AbstractConverter
{
/**
* @inheritDoc
*/
public function convert($value)
{
try {
$rate_storage = new \WOOMC\Rate\Storage();
$price_rounder = new \WOOMC\Price\Rounder();
$currency_detector = new \WOOMC\Currency\Detector();
$price_calculator = new \WOOMC\Price\Calculator($rate_storage, $price_rounder);
$price_controller = new \WOOMC\Price\Controller($price_calculator, $currency_detector);
return $price_controller->convert($value);
} catch (\Throwable $e) {
if ($this->logger) {
$this->logger->debug($e->getMessage());
}
}
return $value;
}
}

View File

@@ -0,0 +1,86 @@
<?php
/**
* Shipping method controler.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency;
use FSVendor\WP_Mock\Hook;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Converts shipping costs and taxes from shop currency to current currency.
*/
class ShippingMethodController implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
/**
* @var string
*/
private $shipping_method_id;
/**
*
* @var Converter
*/
protected $converter;
/**
* @param string $shipping_method_id .
* @param Converter $converter .
*/
public function __construct($shipping_method_id, $converter)
{
$this->shipping_method_id = $shipping_method_id;
$this->converter = $converter;
}
/**
* Hooks.
*/
public function hooks()
{
\add_filter('woocommerce_shipping_rate_cost', array($this, 'convert_shipping_rate_costs_for_shipping_method'), 10, 2);
\add_filter('woocommerce_shipping_rate_taxes', array($this, 'convert_shipping_rate_taxes_for_shipping_method'), 10, 2);
}
/**
* Filter the rate cost.
*
* @param float|int|string $cost The shipping rate cost.
* @param \WC_Shipping_Rate $shipping_rate_object The shipping rate object.
*
* @return float|int|string
* @internal filter.
*/
public function convert_shipping_rate_costs_for_shipping_method($cost, $shipping_rate_object)
{
if ($this->is_shipping_method($shipping_rate_object)) {
$cost = $this->converter->convert($cost);
}
return $cost;
}
/**
* Filter the taxes.
*
* @param float[]|int[]|string[] $taxes The shipping rate taxes array.
* @param \WC_Shipping_Rate $shipping_rate_object The shipping rate object.
*
* @return float[]|int[]|string[]
* @internal filter.
*/
public function convert_shipping_rate_taxes_for_shipping_method($taxes, $shipping_rate_object)
{
if ($this->is_shipping_method($shipping_rate_object)) {
$taxes = $this->converter->convert_array($taxes);
}
return $taxes;
}
/**
* Check if the shipping rate object's method ID is relevant to this class.
*
* @param \WC_Shipping_Rate $shipping_rate_object The shipping rate object.
*
* @return bool
*/
private function is_shipping_method($shipping_rate_object)
{
return $shipping_rate_object instanceof \WC_Shipping_Rate && $this->shipping_method_id === $shipping_rate_object->get_method_id();
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* Shipping method integration.
*
* @package WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency;
use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable;
/**
* Integrates shipping method (by method_id, ie.: flexible_shipping ) with WooCommerce Multicurrency plugin.
* @see https://woocommerce.com/products/multi-currency/
*/
class ShippingMethodIntegration implements \FSVendor\WPDesk\PluginBuilder\Plugin\Hookable
{
/**
* @var string
*/
private $shipping_method_id;
/**
* @var \WOOMC\Integration\Shipping\AbstractController
*/
private $woocommerce_multicurrency_controller;
/**
* @param string $shipping_method_id .
*/
public function __construct($shipping_method_id)
{
$this->shipping_method_id = $shipping_method_id;
}
/**
* Hooks.
*/
public function hooks()
{
\add_action('woocommerce_multicurrency_loaded', array($this, 'add_integration_for_shipping_method'));
}
/**
* Add integration to WooCommerce MultiCurrency for shipping method.
*/
public function add_integration_for_shipping_method()
{
$this->woocommerce_multicurrency_controller = new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency\ShippingMethodController($this->shipping_method_id, new \FSVendor\WPDesk\WooCommerce\CurrencySwitchers\Switcher\WooCommerceMultiCurrency\Converter());
$this->woocommerce_multicurrency_controller->hooks();
}
}

View File

@@ -0,0 +1,29 @@
<?php
/**
* Interface Converter
* @package WPDesk\WooCommerce\CurrencySwitchers
*/
namespace FSVendor\WPDesk\WooCommerce\CurrencySwitchers;
/**
* Interface for currency switchers converters.
*/
interface SwitcherConverter
{
/**
* Convert value from shop currency to current currency.
*
* @param float $value Value in shop currency.
*
* @return float
*/
public function convert($value);
/**
* Convert an array of prices.
*
* @return array
* @example convert_array( ['price' => '10', 'sale_price' => 5] ) --> ['price' => '12.4', 'sale_price' => 6.2]
*/
public function convert_array($values);
}