settings = get_option( 'woocommerce_gateways_country_limiter' ); add_action( 'init', array( $this, 'init' ), 10 ); } public static function woocommerce_inactive_notice() { if ( current_user_can( 'activate_plugins' ) ) : ?>

', '', '', '', '', ' »' ); ?>

payment_gateways->payment_gateways(); if ( !empty( $_GET['section'] ) ) { $this->current_section = sanitize_title( $_GET['section'] ); } foreach ( $payment_gateways as $id => $gateway ) { $title = empty( $gateway->method_title ) ? ucfirst( $gateway->id ) : $gateway->method_title; $section_id = version_compare( WC()->version, '2.6.0', '>=' ) ? $id : 'wc_gateway_' . $id; $this->sections[$section_id] = array( 'title' => esc_html( $title ), 'id' => $id ); } if ( is_admin() && !empty( $this->current_section ) ) { add_action( 'woocommerce_settings_checkout', array( $this, 'country_options_output' ), 1000 ); add_action( 'woocommerce_update_options_checkout', array( $this, 'country_options_update' ), 1000 ); } add_filter( 'woocommerce_available_payment_gateways', array( $this, 'filter_by_country' ), 1000 ); //defaults foreach ( $payment_gateways as $gateway_id => $gateway ) { if ( !isset( $this->settings[$gateway_id]['option'] ) ) { $this->settings[$gateway_id]['option'] = 'all'; } if ( !isset( $this->settings[$gateway_id]['countries'] ) || !is_array( $this->settings[$gateway_id]['countries'] ) ) { $this->settings[$gateway_id]['countries'] = array(); } } if ( is_admin() && !empty( $this->current_section ) ) { if ( isset( $this->sections[$this->current_section] ) ) { $gateway_id = $this->sections[$this->current_section]['id']; wc_enqueue_js( " jQuery(document).ready(function(){ var current_option = jQuery('input[name={$gateway_id}_option]:checked'); if(current_option.val() == 'all_except' || current_option.val() == 'selected'){ jQuery('#pgcl_countries_list').show(); current_option.parent().parent().append(jQuery('#pgcl_countries_list')); } jQuery('input[name={$gateway_id}_option]').change(function(){ if(jQuery(this).val() == 'all_except' || jQuery(this).val() == 'selected'){ jQuery(this).parent().parent().append(jQuery('#pgcl_countries_list')); jQuery('#pgcl_countries_list').show(); }else{ jQuery('#pgcl_countries_list').hide(); } }) }) " ); } } } public function load_plugin_textdomain() { $locale = get_locale(); load_textdomain( 'woocommerce-gateways-country-limiter', dirname( __FILE__ ) . "/i18n/languages/woocommerce-gateways-country-limiter-$locale.mo" ); } function country_options_output() { if ( !empty( $this->current_section ) && !empty( $this->sections[$this->current_section] ) ): $gateway_id = $this->sections[$this->current_section]['id']; ?>
settings[$gateway]['option'] = $_POST[$gateway . '_option']; } $this->settings[$gateway]['countries'] = isset( $_POST[$gateway . '_countries'] ) ? array_map( 'esc_attr', (array)$_POST[$gateway . '_countries'] ) : array(); update_option( 'woocommerce_gateways_country_limiter', $this->settings ); } } function filter_by_country( $payment_gateways ) { $customer_country = WC()->customer ? WC()->customer->get_country() : false; foreach ( $payment_gateways as $gateway_id => $gateway ) { if ( $this->settings[$gateway_id]['option'] == 'all_except' && $customer_country && in_array( $customer_country, $this->settings[$gateway_id]['countries'] ) || $this->settings[$gateway_id]['option'] == 'selected' && $customer_country && !in_array( $customer_country, $this->settings[$gateway_id]['countries'] ) ) { unset( $payment_gateways[$gateway_id] ); } } return $payment_gateways; } } function WooCommerce_Gateways_Country_Limiter() { return WooCommerce_Gateways_Country_Limiter::instance(); } WooCommerce_Gateways_Country_Limiter(); // WooCommerce 2.0.x backward compatibility // wc_enqueue_js add_action( 'admin_head', 'wc_gcl_wc_backwards_compatibility', 100 ); function wc_gcl_wc_backwards_compatibility() { if ( !function_exists( 'wc_enqueue_js' ) ) { function wc_enqueue_js( $code ) { global $woocommerce; return $woocommerce->add_inline_js( $code ); } } } endif;