WCML_Ajax_Setup

The WooCommerce Multilingual WCML Ajax Setup class.

Defined (1)

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

/inc/class-wcml-ajax-setup.php  
  1. class WCML_Ajax_Setup{ 
  2.  
  3. /** 
  4. * @var SitePress 
  5. */ 
  6. private $sitepress; 
  7.  
  8. public function __construct( $sitepres ) { 
  9.  
  10. $this->sitepress =& $sitepres; 
  11.  
  12. add_action( 'init', array( $this, 'init' ) ); 
  13. add_action( 'wcml_localize_woocommerce_on_ajax', array( $this, 'wcml_localize_woocommerce_on_ajax' ) ); 
  14.  
  15. //@deprecated 3.9 
  16. add_action( 'localize_woocommerce_on_ajax', array( $this, 'localize_woocommerce_on_ajax' ) ); 
  17.  
  18. add_action( 'woocommerce_ajax_get_endpoint', array( $this, 'add_language_to_endpoint' ) ); 
  19.  
  20. public function init() { 
  21. if (wpml_is_ajax()) { 
  22. do_action('wcml_localize_woocommerce_on_ajax'); 
  23.  
  24. add_filter('woocommerce_params', array($this, 'filter_woocommerce_ajax_params')); 
  25.  
  26. add_filter('wc_checkout_params', array($this, 'add_language_parameter_to_ajax_url')); 
  27. add_filter('wc_cart', array($this, 'add_language_parameter_to_ajax_url')); 
  28. add_filter('wc_cart_fragments_params', array($this, 'add_language_parameter_to_ajax_url')); 
  29. add_filter('wc_add_to_cart_params', array($this, 'add_language_parameter_to_ajax_url')); 
  30.  
  31. add_action( 'woocommerce_checkout_order_review', array($this, 'filter_woocommerce_order_review'), 9 ); 
  32. add_action( 'woocommerce_checkout_order_review', array($this, 'add_hidden_language_field') ); 
  33. add_action( 'woocommerce_checkout_update_order_review', array($this, 'filter_woocommerce_order_review'), 9 ); 
  34.  
  35.  
  36. function filter_woocommerce_order_review() {  
  37. global $woocommerce; 
  38. unload_textdomain('woocommerce'); 
  39. $woocommerce->load_plugin_textdomain(); 
  40.  
  41. function add_hidden_language_field() { 
  42. if( function_exists('wpml_the_language_input_field') ) { 
  43. wpml_the_language_input_field(); 
  44. }else{ 
  45. global $sitepress; 
  46. if (isset($sitepress) ) { 
  47. return "<input type='hidden' name='lang' value='" . $sitepress->get_current_language() . "' />"; 
  48. return null; 
  49.  
  50. function add_language_parameter_to_ajax_url($woocommerce_params) { 
  51. global $sitepress; 
  52.  
  53. if($sitepress->get_current_language() !== $sitepress->get_default_language()) { 
  54. $woocommerce_params['ajax_url'] = add_query_arg('lang', ICL_LANGUAGE_CODE, $woocommerce_params['ajax_url']); 
  55.  
  56. return $woocommerce_params; 
  57.  
  58. function filter_woocommerce_ajax_params($woocommerce_params) { 
  59. global $sitepress, $post; 
  60. $value = array(); 
  61. $value = $woocommerce_params; 
  62.  
  63. if($sitepress->get_current_language() !== $sitepress->get_default_language()) { 
  64. $value['ajax_url'] = add_query_arg('lang', ICL_LANGUAGE_CODE, $woocommerce_params['ajax_url']); 
  65. $value['checkout_url'] = add_query_arg('action', 'woocommerce-checkout', $value['ajax_url']); 
  66.  
  67. if(!isset($post->ID)) { 
  68. return $value;  
  69.  
  70. $ch_pages = wp_cache_get('ch_pages', 'wcml_ch_pages'); 
  71.  
  72. if(empty($ch_pages)) { 
  73.  
  74. $ch_pages = array( 
  75.  
  76. 'checkout_page_id' => get_option('woocommerce_checkout_page_id'),  
  77. 'pay_page_id' => get_option('woocommerce_pay_page_id'),  
  78. 'cart_page_id' => get_option('woocommerce_cart_page_id')); 
  79.  
  80. $ch_pages['translated_checkout_page_id'] = apply_filters( 'translate_object_id', $ch_pages['checkout_page_id'], 'page', false); 
  81. $ch_pages['translated_pay_page_id'] = apply_filters( 'translate_object_id', $ch_pages['pay_page_id'], 'page', false); 
  82. $ch_pages['translated_cart_page_id'] = apply_filters( 'translate_object_id', $ch_pages['cart_page_id'], 'page', false); 
  83.  
  84.  
  85. wp_cache_set( 'ch_pages', $ch_pages, 'wcml_ch_pages' ); 
  86.  
  87. if($ch_pages['translated_cart_page_id'] == $post->ID) { 
  88. $value['is_cart'] = 1; 
  89. $value['cart_url'] = get_permalink($ch_pages['translated_cart_page_id']); 
  90. } else if($ch_pages['translated_checkout_page_id'] == $post->ID || $ch_pages['checkout_page_id'] == $post->ID) { 
  91. $value['is_checkout'] = 1; 
  92.  
  93. $_SESSION['wpml_globalcart_language'] = $sitepress->get_current_language(); 
  94.  
  95. } else if($ch_pages['translated_pay_page_id'] == $post->ID) { 
  96. $value['is_pay_page'] = 1; 
  97.  
  98. return $value;  
  99.  
  100.  
  101. function wcml_localize_woocommerce_on_ajax() { 
  102. if( isset($_POST['action']) && in_array( $_POST['action'], array( 'wcml_product_data', 'wpml_translation_dialog_save_job' ) ) ) { 
  103. return; 
  104.  
  105. global $sitepress; 
  106.  
  107. $current_language = $sitepress->get_current_language(); 
  108.  
  109. $sitepress->switch_lang($current_language, true); 
  110.  
  111. /** 
  112. * @param $endpoint string 
  113. * Adds a language parameter to the url when different domains for each language are used 
  114. * @return string 
  115. */ 
  116. public function add_language_to_endpoint( $endpoint ) { 
  117.  
  118. $is_per_domain = WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $this->sitepress->get_setting( 'language_negotiation_type' ); 
  119. if( $is_per_domain && $this->sitepress->get_current_language() != $this->sitepress->get_default_language() ) { 
  120.  
  121. $endpoint = add_query_arg('lang', $this->sitepress->get_current_language(), remove_query_arg( 'lang', $endpoint ) ); 
  122. $endpoint = urldecode($endpoint); 
  123.  
  124.  
  125. return $endpoint; 
  126.  
  127.  
  128. /** 
  129. * @deprecated 3.9 
  130. */ 
  131. function localize_woocommerce_on_ajax() { 
  132. $this->wcml_localize_woocommerce_on_ajax(); 
  133.  
  134.