WCML_Currency_Switcher_Ajax

Class WCML_Currency_Switcher_Ajax.

Defined (1)

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

/inc/currencies/currency-switcher/class-wcml-currency-switcher-ajax.php  
  1. class WCML_Currency_Switcher_Ajax{ 
  2.  
  3. private $woocommerce_wpml; 
  4.  
  5. public function __construct( &$woocommerce_wpml ) { 
  6.  
  7. $this->woocommerce_wpml = $woocommerce_wpml; 
  8.  
  9. add_action( 'wp_ajax_wcml_currencies_order', array($this, 'wcml_currencies_order') ); 
  10. add_action( 'wp_ajax_wcml_currencies_switcher_preview', array($this, 'wcml_currencies_switcher_preview') ); 
  11. add_action( 'wp_ajax_wcml_currencies_switcher_save_settings', array($this, 'wcml_currencies_switcher_save_settings') ); 
  12. add_action( 'wp_ajax_wcml_delete_currency_switcher', array($this, 'wcml_delete_currency_switcher') ); 
  13.  
  14.  
  15. public function wcml_currencies_order() { 
  16. $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; 
  17. if ( !$nonce || !wp_verify_nonce( $nonce, 'set_currencies_order_nonce' ) ) { 
  18. wp_send_json_error('Invalid nonce'); 
  19.  
  20. $this->woocommerce_wpml->settings['currencies_order'] = explode( ';', $_POST['order'] ); 
  21. $this->woocommerce_wpml->update_settings( $this->woocommerce_wpml->settings ); 
  22. wp_send_json_success( array( 'message' => esc_html__( 'Currencies order updated', 'woocommerce-multilingual' ) ) ); 
  23.  
  24. public function wcml_currencies_switcher_save_settings() { 
  25. $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; 
  26. if ( !$nonce || !wp_verify_nonce( $nonce, 'wcml_currencies_switcher_save_settings' ) ) { 
  27. wp_send_json_error('Invalid nonce'); 
  28. $wcml_settings =& $this->woocommerce_wpml->settings; 
  29. $switcher_settings = array(); 
  30.  
  31. // Allow some HTML in the currency switcher 
  32. $currency_switcher_format = strip_tags( stripslashes_deep( $_POST[ 'template' ] ), '<img><span><u><strong><em>'); 
  33. $currency_switcher_format = htmlentities( $currency_switcher_format ); 
  34. $currency_switcher_format = sanitize_text_field( $currency_switcher_format ); 
  35. $currency_switcher_format = html_entity_decode( $currency_switcher_format ); 
  36.  
  37. $switcher_id = sanitize_text_field( $_POST[ 'switcher_id' ] ); 
  38. if( $switcher_id == 'new_widget' ) { 
  39. $switcher_id = sanitize_text_field( $_POST[ 'widget_id' ] ); 
  40.  
  41.  
  42. $switcher_settings[ 'widget_title' ] = isset( $_POST[ 'widget_title' ] ) ? sanitize_text_field( $_POST[ 'widget_title' ] ) : ''; 
  43. $switcher_settings[ 'switcher_style' ] = sanitize_text_field( $_POST[ 'switcher_style' ] ); 
  44. $switcher_settings[ 'template' ] = $currency_switcher_format; 
  45.  
  46. foreach( $_POST[ 'color_scheme' ] as $color_id => $color ) { 
  47. $switcher_settings[ 'color_scheme' ][ sanitize_text_field( $color_id ) ] = sanitize_hex_color( $color ); 
  48.  
  49. $wcml_settings[ 'currency_switchers' ][ $switcher_id ] = $switcher_settings; 
  50.  
  51. //update widget settings 
  52. if( $switcher_id != 'product' ) { 
  53. $widget_settings = get_option('widget_currency_sel_widget'); 
  54. $setting_match = false; 
  55. foreach( $widget_settings as $key => $widget_setting ) { 
  56. if( isset( $widget_setting['id'] ) && $switcher_id == $widget_setting['id'] ) { 
  57. $setting_match = true; 
  58. $widget_settings[ $key ][ 'settings' ] = $switcher_settings; 
  59.  
  60. if( !$setting_match ) { 
  61. $widget_settings[] = array( 
  62. 'id' => $switcher_id,  
  63. 'settings' => $switcher_settings 
  64. ); 
  65.  
  66. update_option( 'widget_currency_sel_widget', $widget_settings ); 
  67.  
  68. $this->synchronize_widget_instances( $widget_settings ); 
  69.  
  70. $this->woocommerce_wpml->update_settings( $wcml_settings ); 
  71.  
  72. wp_send_json_success(); 
  73.  
  74. private function synchronize_widget_instances( $widget_settings ) { 
  75.  
  76. $sidebars_widgets = $this->get_sidebars_widgets(); 
  77.  
  78. foreach ( $sidebars_widgets as $sidebar => $widgets ) { 
  79. if ( 'wp_inactive_widgets' === $sidebar ) { 
  80. continue; 
  81.  
  82. $found = false; 
  83. if ( is_array( $widgets ) ) { 
  84. foreach ( $widgets as $key => $widget_id ) { 
  85. if ( strpos( $widget_id, WCML_Currency_Switcher_Widget::SLUG ) === 0 ) { 
  86.  
  87. if ( $found ) { // Only synchronize the first CS widget instance per sidebar 
  88. unset( $sidebars_widgets[ $sidebar ][ $key ] ); 
  89. continue; 
  90.  
  91. $found = true; 
  92.  
  93.  
  94. if ( ! $found ) { 
  95.  
  96. foreach( $widget_settings as $key => $widget_setting ) { 
  97. if( $widget_setting['id'] == $sidebar ) { 
  98. array_unshift( $sidebars_widgets[ $sidebar ], WCML_Currency_Switcher_Widget::SLUG.'-'.$key ); 
  99.  
  100.  
  101. $this->update_sidebars_widgets( $sidebars_widgets ); 
  102.  
  103.  
  104. public function wcml_delete_currency_switcher() { 
  105. $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; 
  106. if ( !$nonce || !wp_verify_nonce( $nonce, 'delete_currency_switcher' ) ) { 
  107. wp_send_json_error(); 
  108.  
  109. $switcher_id = sanitize_text_field( $_POST[ 'switcher_id' ] ); 
  110.  
  111. $wcml_settings = $this->woocommerce_wpml->get_settings(); 
  112.  
  113. unset( $wcml_settings[ 'currency_switchers' ][ $switcher_id ] ); 
  114.  
  115. $this->woocommerce_wpml->update_settings( $wcml_settings ); 
  116.  
  117. $sidebars_widgets = $this->get_sidebars_widgets(); 
  118.  
  119. foreach ($sidebars_widgets as $sidebar => $widgets) { 
  120. if ($sidebar != $switcher_id) { 
  121. continue; 
  122.  
  123. if (is_array($widgets)) { 
  124. foreach ($widgets as $key => $widget_id) { 
  125. if (strpos($widget_id, WCML_Currency_Switcher_Widget::SLUG) === 0) { 
  126. unset($sidebars_widgets[$sidebar][$key]); 
  127.  
  128. $this->update_sidebars_widgets( $sidebars_widgets ); 
  129.  
  130. wp_send_json_success(); 
  131.  
  132. public function wcml_currencies_switcher_preview() { 
  133. $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; 
  134. if ( !$nonce || !wp_verify_nonce( $nonce, 'wcml_currencies_switcher_preview' ) ) { 
  135. wp_send_json_error( 'Invalid nonce' ); 
  136. $return= array(); 
  137.  
  138. $inline_css = $this->woocommerce_wpml->cs_templates->get_color_picket_css( $_POST['switcher_id'], array( 'switcher_style' => $_POST['switcher_style'], 'color_scheme' => $_POST['color_scheme'] ) ); 
  139.  
  140. $template = $this->woocommerce_wpml->cs_templates->get_template( $_POST['switcher_style'] ); 
  141.  
  142. if ( $template->has_styles() ) { 
  143. $return['inline_styles_id'] = $template->get_inline_style_handler().'-inline-css'; 
  144. }else{ 
  145. $return['inline_styles_id'] = 'wcml-cs-inline-styles-' . $_POST['switcher_id'].'-'.$_POST['switcher_style']; 
  146.  
  147. $return['inline_css'] = $inline_css; 
  148.  
  149. ob_start(); 
  150. $this->woocommerce_wpml->multi_currency->currency_switcher->wcml_currency_switcher( 
  151. array( 
  152. 'switcher_id' => $_POST['switcher_id'],  
  153. 'format' => isset( $_POST['template'] ) ? stripslashes_deep( $_POST['template'] ) : '%name% (%symbol%) - %code%',  
  154. 'switcher_style' => $_POST['switcher_style'],  
  155. 'color_scheme' => $_POST['color_scheme'],  
  156. 'preview' => true 
  157. ); 
  158. $switcher_preview = ob_get_contents(); 
  159. ob_end_clean(); 
  160.  
  161. $return['preview'] = $switcher_preview; 
  162.  
  163. wp_send_json_success( $return ); 
  164.  
  165. public function get_sidebars_widgets() { 
  166. if ( ! function_exists( 'wp_get_sidebars_widgets' ) ) { 
  167. require_once( ABSPATH . '/wp-admin/includes/widgets.php' ); 
  168. $sidebars_widgets = wp_get_sidebars_widgets(); 
  169.  
  170. return is_array( $sidebars_widgets ) ? $sidebars_widgets : array(); 
  171. public function update_sidebars_widgets( $sidebars_widgets ) { 
  172. remove_action( 'pre_update_option_sidebars_widgets', array( $this->woocommerce_wpml->multi_currency->currency_switcher, 'update_option_sidebars_widgets' ), 10, 2 ); 
  173.  
  174. wp_set_sidebars_widgets( $sidebars_widgets ); 
  175.  
  176. add_action( 'pre_update_option_sidebars_widgets', array( $this->woocommerce_wpml->multi_currency->currency_switcher, 'update_option_sidebars_widgets' ), 10, 2 );