et_id ) { if ( strpos( $widget_id, WCML_Currency_Switcher_Widget::SLUG ) === 0 ) { if ( $found ) { // Only synchronize the first CS widget instance per sidebar unset( $sidebars_widgets[ $sidebar ][ $key ] ); continue; } $found = true; } } } if ( ! $found ) { foreach ( $widget_settings as $key => $widget_setting ) { if ( is_array( $widget_setting ) && Obj::prop( 'id', $widget_setting ) == $sidebar ) { array_unshift( $sidebars_widgets[ $sidebar ], WCML_Currency_Switcher_Widget::SLUG . '-' . $key ); } } } } $this->update_sidebars_widgets( $sidebars_widgets ); } public function wcml_delete_currency_switcher() { $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; if ( ! $nonce || ! wp_verify_nonce( $nonce, 'delete_currency_switcher' ) ) { wp_send_json_error(); } $switcher_id = sanitize_text_field( $_POST['switcher_id'] ); $wcml_settings = $this->woocommerce_wpml->get_settings(); unset( $wcml_settings['currency_switchers'][ $switcher_id ] ); $this->woocommerce_wpml->update_settings( $wcml_settings ); $sidebars_widgets = $this->get_sidebars_widgets(); foreach ( $sidebars_widgets as $sidebar => $widgets ) { if ( $sidebar != $switcher_id ) { continue; } if ( is_array( $widgets ) ) { foreach ( $widgets as $key => $widget_id ) { if ( strpos( $widget_id, WCML_Currency_Switcher_Widget::SLUG ) === 0 ) { unset( $sidebars_widgets[ $sidebar ][ $key ] ); } } } } $this->update_sidebars_widgets( $sidebars_widgets ); wp_send_json_success(); } public function wcml_currencies_switcher_preview() { $nonce = array_key_exists( 'wcml_nonce', $_POST ) ? sanitize_text_field( $_POST['wcml_nonce'] ) : false; if ( ! $nonce || ! wp_verify_nonce( $nonce, 'wcml_currencies_switcher_preview' ) ) { wp_send_json_error( 'Invalid nonce' ); } $return = []; $inline_css = $this->woocommerce_wpml->cs_templates->get_color_picket_css( $_POST['switcher_id'], [ 'switcher_style' => $_POST['switcher_style'], 'color_scheme' => $_POST['color_scheme'], ] ); $template = $this->woocommerce_wpml->cs_templates->get_template( $_POST['switcher_style'] ); if ( $template->has_styles() ) { $return['inline_styles_id'] = $template->get_inline_style_handler() . '-inline-css'; } else { $return['inline_styles_id'] = 'wcml-cs-inline-styles-' . $_POST['switcher_id'] . '-' . $_POST['switcher_style']; } $return['inline_css'] = $inline_css; ob_start(); $this->woocommerce_wpml->multi_currency->currency_switcher->do_currency_switcher( [ 'switcher_id' => $_POST['switcher_id'], 'format' => isset( $_POST['template'] ) ? stripslashes_deep( $_POST['template'] ) : '%name% (%symbol%) - %code%', 'switcher_style' => $_POST['switcher_style'], 'color_scheme' => $_POST['color_scheme'], 'preview' => true, ] ); $switcher_preview = ob_get_contents(); ob_end_clean(); $return['preview'] = $switcher_preview; wp_send_json_success( $return ); } public function get_sidebars_widgets() { if ( ! function_exists( 'wp_get_sidebars_widgets' ) ) { require_once ABSPATH . '/wp-admin/includes/widgets.php'; } $sidebars_widgets = wp_get_sidebars_widgets(); return is_array( $sidebars_widgets ) ? $sidebars_widgets : []; } public function update_sidebars_widgets( $sidebars_widgets ) { remove_action( 'pre_update_option_sidebars_widgets', [ $this->woocommerce_wpml->multi_currency->currency_switcher, 'update_option_sidebars_widgets' ], 10 ); wp_set_sidebars_widgets( $sidebars_widgets ); add_action( 'pre_update_option_sidebars_widgets', [ $this->woocommerce_wpml->multi_currency->currency_switcher, 'update_option_sidebars_widgets' ], 10, 2 ); } }