woocommerce-multilingual
/
inc
/
template-classes
/
multi-currency
/
class-wcml-multi-currency-ui.php
class-wcml-custom-prices-ui.php
1 month ago
class-wcml-exchange-rates-ui.php
1 month ago
class-wcml-multi-currency-ui.php
1 month ago
class-wcml-multi-currency-ui.php
254 lines
| 1 | <?php |
| 2 | |
| 3 | use WPML\Core\Twig_SimpleFunction; |
| 4 | |
| 5 | class WCML_Multi_Currency_UI extends WCML_Templates_Factory { |
| 6 | |
| 7 | private $woocommerce_wpml; |
| 8 | private $sitepress; |
| 9 | private $currencies; |
| 10 | private $wc_currencies; |
| 11 | private $wc_currency; |
| 12 | |
| 13 | private $tracking_link; |
| 14 | |
| 15 | public function __construct( $woocommerce_wpml, $sitepress ) { |
| 16 | $functions = [ |
| 17 | new Twig_SimpleFunction( 'get_flag_url', [ $this, 'get_flag_url' ] ), |
| 18 | new Twig_SimpleFunction( 'get_currency_symbol', [ $this, 'get_currency_symbol' ] ), |
| 19 | new Twig_SimpleFunction( 'wp_do_action', [ $this, 'wp_do_action' ] ), |
| 20 | new Twig_SimpleFunction( 'get_weekday', [ $this, 'get_weekday' ] ), |
| 21 | ]; |
| 22 | |
| 23 | parent::__construct( $functions ); |
| 24 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 25 | $this->sitepress = $sitepress; |
| 26 | |
| 27 | $this->currencies = $this->woocommerce_wpml->get_multi_currency()->get_currencies( true ); |
| 28 | $this->wc_currencies = get_woocommerce_currencies(); |
| 29 | $this->wc_currency = wcml_get_woocommerce_currency_option(); |
| 30 | |
| 31 | $this->load_curency_switcher_option_boxes(); |
| 32 | |
| 33 | $this->tracking_link = new WCML_Tracking_Link(); |
| 34 | } |
| 35 | |
| 36 | public function get_model() { |
| 37 | $exchange_rates_ui = new WCML_Exchange_Rates_UI( $this->woocommerce_wpml ); |
| 38 | |
| 39 | $model = [ |
| 40 | 'strings' => [ |
| 41 | 'headers' => [ |
| 42 | 'enable_disable' => __( 'Enable/disable', 'woocommerce-multilingual' ), |
| 43 | 'currencies' => __( 'Currencies', 'woocommerce-multilingual' ), |
| 44 | ], |
| 45 | 'settings' => __( 'Settings', 'woocommerce-multilingual' ), |
| 46 | ], |
| 47 | 'currencies' => $this->currencies, |
| 48 | 'wc_currency' => $this->wc_currency, |
| 49 | 'wc_currencies' => $this->wc_currencies, |
| 50 | |
| 51 | 'active_languages' => $this->sitepress->get_active_languages(), |
| 52 | |
| 53 | 'multi_currency_on' => WCML_MULTI_CURRENCIES_INDEPENDENT === (int) $this->woocommerce_wpml->settings['enable_multi_currency'], |
| 54 | |
| 55 | 'wc_currency_empty_warn' => sprintf( |
| 56 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 57 | __( |
| 58 | 'The multi-currency mode cannot be enabled as a specific currency was not set. Go to the %1$sWooCommerce settings%2$s page and select the default currency for your store.', |
| 59 | 'woocommerce-multilingual' |
| 60 | ), |
| 61 | '<a href="' . \WCML\Utilities\AdminUrl::getWooSettings() . '">', |
| 62 | '</a>' |
| 63 | ), |
| 64 | 'wcml_settings' => $this->woocommerce_wpml->settings, |
| 65 | 'form' => [ |
| 66 | 'action' => $_SERVER['REQUEST_URI'], |
| 67 | 'nonce' => wp_nonce_field( 'wcml_mc_options', 'wcml_nonce', true, false ), |
| 68 | 'multi_currency_option' => WCML_MULTI_CURRENCIES_INDEPENDENT, |
| 69 | 'mco_disabled' => empty( $this->wc_currency ), |
| 70 | 'label_mco' => __( 'Enable the multicurrency mode', 'woocommerce-multilingual' ), |
| 71 | 'label_mco_learn_url' => $this->tracking_link->getWcmlMultiCurrencyDoc(), |
| 72 | 'label_mco_learn_txt' => __( 'Multicurrency documentation', 'woocommerce-multilingual' ), |
| 73 | 'custom_prices_select' => [ |
| 74 | 'checked' => 1 === (int) $this->woocommerce_wpml->settings['display_custom_prices'], |
| 75 | 'label' => sprintf( |
| 76 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 77 | __( 'Show only products with %1$scustom prices%2$s in secondary currencies', 'woocommerce-multilingual' ), |
| 78 | '<a href="' . WCML_Tracking_Link::getWcmlMultiCurrencyDoc( '#configure-distinct-prices-for-different-currencies' ) . '" target="_blank" class="wpml-external-link">', |
| 79 | '</a>' |
| 80 | ), |
| 81 | 'tip' => __( 'When enabled, products with prices based on the exchange rate are hidden', 'woocommerce-multilingual' ), |
| 82 | ], |
| 83 | 'submit' => __( 'Save changes', 'woocommerce-multilingual' ), |
| 84 | 'navigate_warn' => __( 'The changes you made will be lost if you navigate away from this page.', 'woocommerce-multilingual' ), |
| 85 | 'cur_lang_warn' => __( 'At least one currency must be enabled for this language!', 'woocommerce-multilingual' ), |
| 86 | |
| 87 | ], |
| 88 | |
| 89 | 'currency_switcher' => [ |
| 90 | 'headers' => [ |
| 91 | 'main' => __( 'Currency switcher options', 'woocommerce-multilingual' ), |
| 92 | 'main_desc' => __( 'All currency switchers in your site are affected by the settings in this section.', 'woocommerce-multilingual' ), |
| 93 | 'order' => __( 'Order of currencies', 'woocommerce-multilingual' ), |
| 94 | 'additional_css' => __( 'Additional CSS', 'woocommerce-multilingual' ), |
| 95 | 'widget' => __( 'Widget Currency Switcher', 'woocommerce-multilingual' ), |
| 96 | 'product_page' => __( 'Product page Currency Switcher', 'woocommerce-multilingual' ), |
| 97 | 'preview' => __( 'Preview', 'woocommerce-multilingual' ), |
| 98 | 'position' => __( 'Position', 'woocommerce-multilingual' ), |
| 99 | 'actions' => __( 'Actions', 'woocommerce-multilingual' ), |
| 100 | 'action' => __( 'Action', 'woocommerce-multilingual' ), |
| 101 | 'delete' => __( 'Delete', 'woocommerce-multilingual' ), |
| 102 | 'edit' => __( 'Edit currency switcher', 'woocommerce-multilingual' ), |
| 103 | 'add_widget' => __( 'Add a new currency switcher to a widget area', 'woocommerce-multilingual' ), |
| 104 | 'no_sidebar' => sprintf( |
| 105 | /* translators: %1$s and %2$s are opening and closing HTML link tags */ |
| 106 | esc_html__( 'Your theme does not support a currency switcher widget. Please %1$sadd your currency switcher%2$s using a shortcode or PHP.', 'woocommerce-multilingual' ), |
| 107 | '<a href="' . WCML_Tracking_Link::getWcmlMultiCurrencyDoc( '#adding-a-currency-switcher-to-the-front-end' ) . '" class="wpml-external-link" target="_blank">', |
| 108 | '</a>' |
| 109 | ), |
| 110 | ], |
| 111 | 'preview' => $this->get_currency_switchers_preview(), |
| 112 | 'widget_currency_switchers' => $this->widget_currency_switchers(), |
| 113 | 'available_sidebars' => $this->woocommerce_wpml->multi_currency->currency_switcher->get_available_sidebars(), |
| 114 | 'preview_text' => __( 'Currency switcher preview', 'woocommerce-multilingual' ), |
| 115 | 'order' => ! isset( $this->woocommerce_wpml->settings['currencies_order'] ) ? |
| 116 | $this->woocommerce_wpml->multi_currency->get_currency_codes() : |
| 117 | $this->woocommerce_wpml->settings['currencies_order'], |
| 118 | 'order_nonce' => wp_create_nonce( 'set_currencies_order_nonce' ), |
| 119 | 'delete_nonce' => wp_create_nonce( 'delete_currency_switcher' ), |
| 120 | 'order_tip' => __( 'Drag and drop the currencies to change their order', 'woocommerce-multilingual' ), |
| 121 | 'visibility_label' => __( 'Show a currency switcher on the product page template', 'woocommerce-multilingual' ), |
| 122 | 'visibility_on' => $this->woocommerce_wpml->settings['currency_switcher_product_visibility'] ?? 1, |
| 123 | 'additional_css' => $this->woocommerce_wpml->settings['currency_switcher_additional_css'] ?? '', |
| 124 | ], |
| 125 | 'exchange_rates' => $exchange_rates_ui->get_model(), |
| 126 | ]; |
| 127 | |
| 128 | return $model; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | protected function init_template_base_dir() { |
| 133 | $this->template_paths = [ |
| 134 | WCML_PLUGIN_PATH . '/templates/multi-currency/', |
| 135 | ]; |
| 136 | } |
| 137 | |
| 138 | public function get_template() { |
| 139 | return 'multi-currency.twig'; |
| 140 | } |
| 141 | |
| 142 | public function get_flag_url( $code ) { |
| 143 | return $this->sitepress->get_flag_url( $code ); |
| 144 | } |
| 145 | |
| 146 | public function get_currency_symbol( $code ) { |
| 147 | return get_woocommerce_currency_symbol( $code ); |
| 148 | } |
| 149 | |
| 150 | public function load_curency_switcher_option_boxes() { |
| 151 | |
| 152 | $wcml_settings = $this->woocommerce_wpml->get_settings(); |
| 153 | $currency_switchers = $wcml_settings['currency_switchers'] ?? []; |
| 154 | |
| 155 | $currency_switchers['new_widget'] = [ |
| 156 | 'switcher_style' => 'wcml-dropdown', |
| 157 | 'widget_title' => '', |
| 158 | 'switcher_templates' => $this->woocommerce_wpml->cs_templates->get_templates(), |
| 159 | 'template' => '%name% (%symbol%) - %code%', |
| 160 | 'color_scheme' => [ |
| 161 | 'font_current_normal' => '', |
| 162 | 'font_current_hover' => '', |
| 163 | 'background_current_normal' => '', |
| 164 | 'background_current_hover' => '', |
| 165 | 'font_other_normal' => '', |
| 166 | 'font_other_hover' => '', |
| 167 | 'background_other_normal' => '', |
| 168 | 'background_other_hover' => '', |
| 169 | 'border_normal' => '', |
| 170 | ], |
| 171 | ]; |
| 172 | |
| 173 | if ( ! isset( $currency_switchers['product'] ) ) { |
| 174 | $currency_switchers['product'] = $currency_switchers['new_widget']; |
| 175 | } |
| 176 | |
| 177 | $widget_currency_switchers = $this->widget_currency_switchers(); |
| 178 | |
| 179 | foreach ( $currency_switchers as $switcher_id => $currency_switcher ) { |
| 180 | |
| 181 | if ( 'new_widget' !== $switcher_id && ! $this->woocommerce_wpml->cs_properties->is_currency_switcher_active( $switcher_id, $wcml_settings ) ) { |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | if ( 'product' === $switcher_id ) { |
| 186 | $dialog_title = __( 'Edit Product Currency Switcher', 'woocommerce-multilingual' ); |
| 187 | } elseif ( 'new_widget' === $switcher_id ) { |
| 188 | $dialog_title = __( 'New Widget Area Currency Switcher', 'woocommerce-multilingual' ); |
| 189 | } else { |
| 190 | /* translators: %s is sidebar name */ |
| 191 | $dialog_title = sprintf( __( 'Edit %s Currency Switcher', 'woocommerce-multilingual' ), $widget_currency_switchers[ $switcher_id ]['name'] ); |
| 192 | } |
| 193 | |
| 194 | $args = [ |
| 195 | 'title' => $dialog_title, |
| 196 | 'currency_switcher' => $switcher_id, |
| 197 | 'switcher_style' => $currency_switcher['switcher_style'], |
| 198 | 'widget_title' => $currency_switcher['widget_title'], |
| 199 | 'switcher_templates' => $this->woocommerce_wpml->cs_templates->get_templates(), |
| 200 | 'template' => $currency_switcher['template'], |
| 201 | 'template_default' => '%name% (%symbol%) - %code%', |
| 202 | 'options' => $currency_switcher['color_scheme'], |
| 203 | ]; |
| 204 | |
| 205 | new WCML_Currency_Switcher_Options_Dialog( $args, $this->woocommerce_wpml ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | public function get_currency_switchers_preview() { |
| 210 | $preview = [ |
| 211 | 'product' => $this->woocommerce_wpml->multi_currency->currency_switcher->do_currency_switcher( |
| 212 | [ |
| 213 | 'switcher_id' => 'product', |
| 214 | 'echo' => false, |
| 215 | ] |
| 216 | ), |
| 217 | ]; |
| 218 | |
| 219 | foreach ( $this->widget_currency_switchers() as $switcher ) { |
| 220 | $preview[ $switcher['id'] ] = $this->woocommerce_wpml->multi_currency->currency_switcher->do_currency_switcher( |
| 221 | [ |
| 222 | 'switcher_id' => $switcher['id'], |
| 223 | 'echo' => false, |
| 224 | ] |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | return $preview; |
| 229 | } |
| 230 | |
| 231 | public function wp_do_action( $hook ) { |
| 232 | do_action( $hook ); |
| 233 | } |
| 234 | |
| 235 | public function get_weekday( $day_index ) { |
| 236 | global $wp_locale; |
| 237 | return $wp_locale->get_weekday( $day_index ); |
| 238 | } |
| 239 | |
| 240 | public function widget_currency_switchers() { |
| 241 | $wcml_settings = $this->woocommerce_wpml->get_settings(); |
| 242 | $currency_switchers = $wcml_settings['currency_switchers'] ?? []; |
| 243 | $sidebars = $this->woocommerce_wpml->multi_currency->currency_switcher->get_registered_sidebars(); |
| 244 | foreach ( $sidebars as $key => $sidebar ) { |
| 245 | if ( ! isset( $currency_switchers[ $key ] ) ) { |
| 246 | unset( $sidebars[ $key ] ); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return $sidebars; |
| 251 | } |
| 252 | |
| 253 | } |
| 254 |