WCML_WC_Gateways

The WooCommerce Multilingual WCML WC Gateways class.

Defined (1)

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

/inc/class-wcml-wc-gateways.php  
  1. class WCML_WC_Gateways{ 
  2.  
  3. private $current_language; 
  4. private $sitepress; 
  5.  
  6. function __construct( &$woocommerce_wpml, &$sitepress ) { 
  7.  
  8. $this->sitepress = $sitepress; 
  9. $this->woocommerce_wpml = $woocommerce_wpml; 
  10.  
  11. add_action( 'init', array( $this, 'init' ), 11 ); 
  12.  
  13. $this->current_language = $sitepress->get_current_language(); 
  14. if( $this->current_language == 'all' ) { 
  15. $this->current_language = $sitepress->get_default_language(); 
  16.  
  17. add_filter( 'woocommerce_payment_gateways', array( $this, 'loaded_woocommerce_payment_gateways' ) ); 
  18.  
  19.  
  20. function init() { 
  21. global $pagenow; 
  22.  
  23. add_filter('woocommerce_gateway_title', array($this, 'translate_gateway_title'), 10, 2); 
  24. add_filter('woocommerce_gateway_description', array($this, 'translate_gateway_description'), 10, 2); 
  25.  
  26. if( is_admin() && $pagenow == 'admin.php' && isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] == 'wc-settings' && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'checkout' ) { 
  27. add_action( 'admin_footer', array($this, 'show_language_links_for_gateways' ) ); 
  28. $this->register_and_set_gateway_strings_language(); 
  29.  
  30.  
  31.  
  32. function loaded_woocommerce_payment_gateways( $load_gateways ) { 
  33.  
  34. foreach( $load_gateways as $key => $gateway ) { 
  35.  
  36. $load_gateway = is_string( $gateway ) ? new $gateway() : $gateway; 
  37. $this->payment_gateways_filters( $load_gateway ); 
  38. $load_gateways[ $key ] = $load_gateway; 
  39.  
  40.  
  41. return $load_gateways; 
  42.  
  43. function payment_gateways_filters( $gateway ) { 
  44.  
  45. if( isset( $gateway->id ) ) { 
  46. $gateway_id = $gateway->id; 
  47. $this->translate_gateway_strings( $gateway ); 
  48.  
  49.  
  50. function translate_gateway_strings( $gateway ) { 
  51.  
  52. if( isset( $gateway->enabled ) && $gateway->enabled != 'no' ) { 
  53.  
  54. if( isset( $gateway->instructions ) ) { 
  55. $gateway->instructions = $this->translate_gateway_instructions( $gateway->instructions, $gateway->id ); 
  56.  
  57. if( isset( $gateway->description ) ) { 
  58. $gateway->description = $this->translate_gateway_description( $gateway->description, $gateway->id ); 
  59.  
  60. if( isset( $gateway->title ) ) { 
  61. $gateway->title = $this->translate_gateway_title( $gateway->title, $gateway->id ); 
  62.  
  63. return $gateway; 
  64.  
  65.  
  66. function translate_gateway_title( $title, $gateway_id, $language = false ) { 
  67. $title = apply_filters( 'wpml_translate_single_string', $title, 'woocommerce', $gateway_id .'_gateway_title', $language ? $language : $this->current_language ); 
  68. return $title; 
  69.  
  70. function translate_gateway_description( $description, $gateway_id) { 
  71. $description = apply_filters( 'wpml_translate_single_string', $description, 'woocommerce', $gateway_id . '_gateway_description', $this->current_language ); 
  72. return $description; 
  73.  
  74. function translate_gateway_instructions( $instructions, $gateway_id ) { 
  75. $instructions = apply_filters( 'wpml_translate_single_string', $instructions, 'woocommerce', $gateway_id . '_gateway_instructions', $this->current_language ); 
  76. return $instructions; 
  77.  
  78. function show_language_links_for_gateways() { 
  79.  
  80. $text_keys = array( 
  81. 'title',  
  82. 'description',  
  83. 'instructions' 
  84. ); 
  85. $wc_payment_gateways = WC_Payment_Gateways::instance(); 
  86.  
  87. foreach( $wc_payment_gateways->payment_gateways() as $payment_gateway ) { 
  88.  
  89. if( isset( $_GET['section'] ) && $_GET['section'] == $payment_gateway->id ) { 
  90.  
  91. foreach( $text_keys as $text_key ) { 
  92.  
  93. if ( isset( $payment_gateway->settings[ $text_key ] ) ) { 
  94. $setting_value = $payment_gateway->settings[ $text_key ]; 
  95. }elseif( $text_key === 'instructions' ) { 
  96. $setting_value = $payment_gateway->description; 
  97. }else{ 
  98. $setting_value = $payment_gateway->$text_key; 
  99.  
  100. $input_name = $payment_gateway->plugin_id.$payment_gateway->id.'_'.$text_key; 
  101. $gateway_option = $payment_gateway->plugin_id.$payment_gateway->id.'_settings'; 
  102.  
  103. $lang_selector = new WPML_Simple_Language_Selector( $this->sitepress ); 
  104. $language = $this->woocommerce_wpml->strings->get_string_language( $setting_value, 'woocommerce', $payment_gateway->id .'_gateway_'. $text_key ); 
  105. if( is_null( $language ) ) { 
  106. $language = $this->sitepress->get_default_language(); 
  107.  
  108. $lang_selector->render( array( 
  109. 'id' => $gateway_option.'_'.$text_key.'_language_selector',  
  110. 'name' => 'wcml_lang-'.$gateway_option.'-'.$text_key,  
  111. 'selected' => $language,  
  112. 'show_please_select' => false,  
  113. 'echo' => true,  
  114. 'style' => 'width: 18%;float: left;margin-top: 3px;' 
  115. ); 
  116.  
  117. $st_page = admin_url( 'admin.php?page=' . WPML_ST_FOLDER . '/menu/string-translation.php&context=woocommerce&search='.esc_attr( preg_replace("/[\n\r]/", "", $setting_value) ) ); 
  118. ?> 
  119. <script> 
  120. var input = jQuery('#<?php echo esc_js( $input_name ); ?>'); 
  121. if ( input.length > 0 ) { 
  122. input.parent().append('<div class="translation_controls"></div>'); 
  123. input.parent().find('.translation_controls').append('<a href="<?php echo $st_page ?>" style="margin-left: 10px"><?php _e('translations', 'woocommerce-multilingual') ?></a>'); 
  124. jQuery('#<?php echo $gateway_option.'_'.$text_key.'_language_selector' ?>').prependTo( input.parent().find('.translation_controls') ); 
  125. }else{ 
  126. jQuery('#<?php echo $gateway_option.'_'.$text_key.'_language_selector' ?>').remove(); 
  127. </script> 
  128. <?php } 
  129.  
  130. function register_and_set_gateway_strings_language() { 
  131.  
  132. foreach( $_POST as $key => $language ) { 
  133.  
  134. if( substr( $key, 0, 9 ) == 'wcml_lang' ) { 
  135.  
  136. $gateway_string = explode( '-', $key ); 
  137.  
  138. if( isset( $gateway_string[2] ) ) { 
  139.  
  140. $gateway_key = str_replace( '_settings', '', $gateway_string[1] ); 
  141. $gateway_string_name = str_replace( 'woocommerce_', '', $gateway_key ) .'_gateway_'. $gateway_string[2]; 
  142. $gateway_key .= '_'.$gateway_string[2]; 
  143.  
  144. $gateway_settings = get_option( $gateway_string[1], true ); 
  145.  
  146. $string_value = isset( $_POST[ $gateway_key ] ) ? $_POST[ $gateway_key ] : ''; 
  147. $opt_string_value = isset( $gateway_settings[ $gateway_string[2] ] ) ? $gateway_settings[ $gateway_string[2] ] : $string_value; 
  148.  
  149. $context = 'woocommerce'; 
  150.  
  151. do_action( 'wpml_register_single_string', $context, $gateway_string_name, $string_value, false, $this->woocommerce_wpml->strings->get_string_language( $opt_string_value, $context ) ); 
  152.  
  153. $this->woocommerce_wpml->strings->set_string_language( $string_value, $context, $gateway_string_name, $language ); 
  154.