WCML_Currency_Switcher

Class WCML_Currency_Switcher.

Defined (1)

The class is defined in the following location(s).

/inc/currencies/currency-switcher/class-wcml-currency-switcher.php  
  1. class WCML_Currency_Switcher{ 
  2.  
  3. private $woocommerce_wpml; 
  4. private $sitepress; 
  5. private $is_touch_screen; 
  6.  
  7. public function __construct( &$woocommerce_wpml, &$sitepress ) { 
  8.  
  9. $this->woocommerce_wpml = $woocommerce_wpml; 
  10. $this->sitepress = $sitepress; 
  11.  
  12. add_action( 'init', array($this, 'init'), 5 ); 
  13.  
  14. public function init() { 
  15.  
  16. add_action( 'wcml_currency_switcher', array($this, 'wcml_currency_switcher') ); 
  17. //@deprecated 3.9 
  18. add_action( 'currency_switcher', array($this, 'currency_switcher') ); 
  19.  
  20. add_shortcode( 'currency_switcher', array($this, 'currency_switcher_shortcode') ); 
  21.  
  22. // Built in currency switcher 
  23. add_action( 'woocommerce_product_meta_start', array($this, 'show_currency_switcher') ); 
  24.  
  25. add_action( 'pre_update_option_sidebars_widgets', array( $this, 'update_option_sidebars_widgets' ), 10, 2 ); 
  26.  
  27. public static function get_settings( $switcher_id ) { 
  28. global $woocommerce_wpml; 
  29.  
  30. $wcml_settings = $woocommerce_wpml->get_settings(); 
  31.  
  32. return isset( $wcml_settings[ 'currency_switchers' ][ $switcher_id ] ) ? $wcml_settings[ 'currency_switchers' ][ $switcher_id ] : array(); 
  33.  
  34. public function currency_switcher_shortcode( $atts ) { 
  35.  
  36. $atts = (array) $atts; 
  37.  
  38. ob_start(); 
  39. $this->wcml_currency_switcher( $atts ); 
  40. $html = ob_get_contents(); 
  41. ob_end_clean(); 
  42.  
  43. return $html; 
  44.  
  45. public function wcml_currency_switcher( $args = array() ) { 
  46.  
  47. if ( is_page( wc_get_page_id( 'myaccount' ) ) ) { 
  48. return ''; 
  49.  
  50. if( !is_array( $args ) ) { 
  51. $args = array(); 
  52.  
  53. if( !isset( $args[ 'switcher_id' ] ) ) { 
  54. $args[ 'switcher_id' ] = 'product'; 
  55.  
  56. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  57. $multi_currency_object =& $this->woocommerce_wpml->multi_currency; 
  58. $currency_switcher_settings = array(); 
  59.  
  60. if( isset( $wcml_settings[ 'currency_switchers' ][ $args[ 'switcher_id' ] ] ) ) { 
  61. $currency_switcher_settings = $wcml_settings[ 'currency_switchers' ][ $args[ 'switcher_id' ] ]; 
  62.  
  63. $args = $this->check_and_convert_switcher_style( $args ); 
  64.  
  65. $switcher_style_not_available = !isset( $args[ 'switcher_style' ] ) || !$this->woocommerce_wpml->cs_templates->check_is_active( $args[ 'switcher_style' ] ); 
  66. if ( 
  67. !isset( $args[ 'preview' ] ) && 
  68. $switcher_style_not_available 
  69. ) { 
  70. $args[ 'switcher_style' ] = isset( $currency_switcher_settings[ 'switcher_style' ] ) ? $currency_switcher_settings[ 'switcher_style' ] : $this->woocommerce_wpml->cs_templates->get_first_active(); 
  71.  
  72. if ( !isset( $args[ 'format' ] ) ) { 
  73. $args[ 'format' ] = isset( $currency_switcher_settings[ 'template' ] ) && '' !== $currency_switcher_settings[ 'template' ] ? 
  74. $currency_switcher_settings[ 'template' ] : '%name% (%symbol%) - %code%'; 
  75.  
  76. if ( !isset( $args[ 'color_scheme' ] ) ) { 
  77. $args[ 'color_scheme' ] = isset($currency_switcher_settings['color_scheme']) ? $currency_switcher_settings['color_scheme'] : array(); 
  78.  
  79. $preview = ''; 
  80. $show_currency_switcher = true; 
  81.  
  82. $display_custom_prices = isset( $wcml_settings[ 'display_custom_prices' ] ) && $wcml_settings[ 'display_custom_prices' ]; 
  83. $is_cart_or_checkout = is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ); 
  84.  
  85. if ( $display_custom_prices ) { 
  86. if( $is_cart_or_checkout ) { 
  87. $show_currency_switcher = false; 
  88. }elseif( is_product() ) { 
  89. $current_product_id = get_post()->ID; 
  90. $original_product_id = $this->woocommerce_wpml->products->get_original_product_id( $current_product_id ); 
  91. $use_custom_prices = get_post_meta( 
  92. $original_product_id,  
  93. '_wcml_custom_prices_status',  
  94. true 
  95. ); 
  96.  
  97. if ( !$use_custom_prices ) $show_currency_switcher = false; 
  98.  
  99. if ( $show_currency_switcher ) { 
  100.  
  101. $currencies = isset($wcml_settings['currencies_order']) ? 
  102. $wcml_settings['currencies_order'] : 
  103. $multi_currency_object->get_currency_codes(); 
  104.  
  105. if ( count($currencies) > 1) { 
  106. if ( !is_admin() ) { 
  107. foreach ( $currencies as $k => $currency ) { 
  108. if ( $wcml_settings['currency_options'][$currency]['languages'][$this->sitepress->get_current_language()] != 1 ) { 
  109. unset( $currencies[$k] ); 
  110.  
  111. $template = $this->woocommerce_wpml->cs_templates->get_template( $args[ 'switcher_style' ] ); 
  112. $template->set_model( $this->get_model_data( $args, $currencies ) ); 
  113. $preview = $template->get_view(); 
  114. } else{ 
  115.  
  116. if( is_admin() ) { 
  117. $preview = '<i>' . esc_html__("You haven't added any secondary currencies.", 'woocommerce-multilingual') . '</i>'; 
  118. }else{ 
  119. $preview = ''; 
  120.  
  121. if ( !isset($args['echo']) || $args['echo'] ) { 
  122. echo $preview; 
  123. } else { 
  124. return $preview; 
  125.  
  126. public function get_model_data( $args, $currencies ) { 
  127.  
  128. $css_classes = $this->get_css_classes( array( $args[ 'switcher_style' ], $args[ 'switcher_id' ], 'wcml_currency_switcher' ) ); 
  129.  
  130. $model = array( 
  131. 'css_classes' => $css_classes,  
  132. 'format' => isset( $args['format'] ) ? $args['format'] : '%name% (%symbol%) - %code%',  
  133. 'currencies' => $currencies,  
  134. 'selected_currency' => $this->woocommerce_wpml->multi_currency->get_client_currency() 
  135. ); 
  136.  
  137. return $model; 
  138.  
  139. public function get_css_classes( $classes = array() ) { 
  140.  
  141. if ( $this->sitepress->is_rtl( $this->sitepress->get_current_language() ) ) { 
  142. $classes[] = 'wcml-cs-rtl'; 
  143.  
  144. $classes = $this->add_user_agent_touch_device_classes( $classes ); 
  145. $classes = apply_filters( 'wcml_cs_template_css_classes', $classes ); 
  146.  
  147. return implode( ' ', $classes ); 
  148.  
  149. public function add_user_agent_touch_device_classes( $classes ) { 
  150.  
  151. if ( is_null( $this->is_touch_screen ) ) { 
  152. if ( ! class_exists( 'WPML_Mobile_Detect' ) ) { 
  153. require_once ICL_PLUGIN_PATH . '/lib/mobile-detect.php'; 
  154. $mobile_detect = new WPML_Mobile_Detect(); 
  155. $this->is_touch_screen = $mobile_detect->isMobile() || $mobile_detect->isTablet(); 
  156.  
  157. if ( $this->is_touch_screen ) { 
  158. $classes[] = 'wcml-cs-touch-device'; 
  159.  
  160. return $classes; 
  161.  
  162. public function show_currency_switcher() { 
  163. $settings = $this->woocommerce_wpml->get_settings(); 
  164.  
  165. if ( is_product() && isset($settings['currency_switcher_product_visibility']) && $settings['currency_switcher_product_visibility'] === 1 ) { 
  166. echo( do_shortcode( '[currency_switcher]' ) ); 
  167.  
  168. /** 
  169. * @deprecated 3.9 
  170. */ 
  171. public function currency_switcher( $args = array() ) { 
  172.  
  173. $this->wcml_currency_switcher( $args ); 
  174.  
  175. /** 
  176. * @return array 
  177. */ 
  178. public function get_registered_sidebars() { 
  179. global $wp_registered_sidebars; 
  180.  
  181. return is_array( $wp_registered_sidebars ) ? $wp_registered_sidebars : array(); 
  182.  
  183. public function get_available_sidebars() { 
  184. $sidebars = $this->get_registered_sidebars(); 
  185. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  186.  
  187. foreach( $sidebars as $key => $sidebar ) { 
  188. if ( isset( $wcml_settings[ 'currency_switchers' ][ $sidebar[ 'id' ] ] ) ) { 
  189. unset( $sidebars[ $key ] ); 
  190.  
  191. return $sidebars; 
  192.  
  193. public function update_option_sidebars_widgets( $sidebars, $old_sidebars ) { 
  194.  
  195. foreach ( $sidebars as $sidebar => $widgets ) { 
  196. if ( 'wp_inactive_widgets' === $sidebar ) { 
  197. continue; 
  198. $found = false; 
  199. if ( is_array( $widgets ) ) { 
  200. foreach ( $widgets as $key => $widget_id ) { 
  201. if ( strpos($widget_id, WCML_Currency_Switcher_Widget::SLUG ) === 0 ) { 
  202. if ( $found ) { //Only one CS widget instance per sidebar 
  203. unset($sidebars[$sidebar][$key]); 
  204. continue; 
  205. $found = true; 
  206.  
  207. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  208. if( $found && empty( $wcml_settings[ 'currency_switchers' ][ $sidebar ] ) ) { 
  209. $wcml_settings[ 'currency_switchers' ][ $sidebar ] = $this->get_switcher_default_settings(); 
  210. }elseif( !$found && isset( $wcml_settings[ 'currency_switchers' ][ $sidebar ] ) ) { 
  211. unset( $wcml_settings[ 'currency_switchers' ][ $sidebar ] ); 
  212.  
  213. $this->woocommerce_wpml->update_settings( $wcml_settings ); 
  214.  
  215. return $sidebars; 
  216.  
  217. public function get_switcher_default_settings() { 
  218. return array( 
  219. 'switcher_style' => 'wcml-dropdown',  
  220. 'widget_title' => '',  
  221. 'template' => '%name% (%symbol%) - %code%',  
  222. 'color_scheme' => array( 
  223. 'font_current_normal' => '',  
  224. 'font_current_hover' => '',  
  225. 'background_current_normal' => '',  
  226. 'background_current_hover' => '',  
  227. 'font_other_normal' => '',  
  228. 'font_other_hover' => '',  
  229. 'background_other_normal' => '',  
  230. 'background_other_hover' => '',  
  231. 'border_normal' => '' 
  232. ); 
  233.  
  234. //backward compatibility to convert switcher style for users who uses old parameters wcml-1874 
  235. public function check_and_convert_switcher_style( $args ) { 
  236.  
  237. if( isset( $args[ 'switcher_style' ] ) ) { 
  238. if( 
  239. 'list' === $args[ 'switcher_style' ] && 
  240. isset( $args[ 'orientation' ] ) 
  241. ) { 
  242. if( 'horizontal' === $args[ 'orientation' ] ) { 
  243. $args[ 'switcher_style' ] = 'wcml-horizontal-list'; 
  244. }else{ 
  245. $args[ 'switcher_style' ] = 'wcml-vertical-list'; 
  246. unset( $args[ 'orientation' ] ); 
  247. }elseif( 'dropdown' === $args[ 'switcher_style' ] ) { 
  248. $args[ 'switcher_style' ] = 'wcml-dropdown'; 
  249.  
  250. return $args; 
  251.