1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  //Add all the necessary filters. There are a LOT of WordPress functions, and you may need to add more filters for your site.
  if(!function_exists('avia_wpml_correct_domain_in_url'))
  {
    add_filter ('site_url', 'avia_wpml_correct_domain_in_url');
    add_filter ('get_option_siteurl', 'avia_wpml_correct_domain_in_url');
    add_filter ('stylesheet_directory_uri', 'avia_wpml_correct_domain_in_url');
    add_filter ('template_directory_uri', 'avia_wpml_correct_domain_in_url');
    add_filter ('post_thumbnail_html', 'avia_wpml_correct_domain_in_url');
    add_filter ('plugins_url', 'avia_wpml_correct_domain_in_url');
    add_filter ('admin_url', 'avia_wpml_correct_domain_in_url');
    add_filter ('wp_get_attachment_url', 'avia_wpml_correct_domain_in_url');
    
     
    /**
    * Changes the domain for a URL so it has the correct domain for the current language
    * Designed to be used by various filters
    * 
    * @param string $url
    * @return string
    */
    function avia_wpml_correct_domain_in_url($url)
    {
        if (function_exists('icl_get_home_url')) 
        {
            // Use the language switcher object, because that contains WPML settings, and it's available globally
            global $icl_language_switcher, $avia_config;

            // Only make the change if we're using the languages-per-domain option
            if (isset($icl_language_switcher->settings['language_negotiation_type']) && $icl_language_switcher->settings['language_negotiation_type'] == 2)
            {
          if(!avia_wpml_is_default_language())
          {
                  return str_replace(untrailingslashit(get_option('home')), untrailingslashit(icl_get_home_url()), $url);
            }
          }
        }
        return $url;
    }
  }
  
  /*check if we are using the default language*/
  if(!function_exists('avia_wpml_is_default_language'))
  {
    function avia_wpml_is_default_language()
    {
      global $avia_config;
      $wpml_options = $avia_config['wpml']['settings'];
      
      if((isset($wpml_options['default_language']) && $wpml_options['default_language'] != ICL_LANGUAGE_CODE) && 'all' != ICL_LANGUAGE_CODE && "" != ICL_LANGUAGE_CODE)
      {
        return false;
      }
      else
      {
        return true;
      }
    }
  }
  
  
  if(!function_exists('avia_append_language_code_to_ajax_url'))
  {
    add_filter ('avia_ajax_url_filter', 'avia_append_language_code_to_ajax_url');
  
    function avia_append_language_code_to_ajax_url($url)
    {
      //conert url in case we are using different domain
      $url = avia_wpml_correct_domain_in_url($url);
        
      //after converting the url in case it was necessary also append the language code
      $url .= '?lang='.ICL_LANGUAGE_CODE;

      return $url;
    }
  }
  
  /*fetch some default data necessary for the framework*/
  if(!function_exists('avia_wpml_get_languages'))
  {
    function avia_wpml_get_languages()
    {
      global $sitepress, $avia_config;
      $avia_config['wpml']['lang']     = $sitepress->get_active_languages();
      $avia_config['wpml']['settings']   = get_option('icl_sitepress_settings');
    }
  }