function uls_link_filter($post_url, $post = null)
{
    //check flag to avoid infinite recursion
    global $uls_link_filter_flag;
    if ($uls_link_filter_flag) {
        return $post_url;
    }
    //if global change is enabled
    global $uls_permalink_convertion;
    if ($uls_permalink_convertion) {
        return $post_url;
    }
    //if user is in backend
    if (is_admin()) {
        return $post_url;
    }
    //init flag to avoid infinite recursion
    $uls_link_filter_flag = true;
    //get language from URL
    $url_language = uls_get_user_language_from_url();
    //get the general options
    $options = uls_get_options();
    //check if page donesn't require a post to do translation, it only uses URL
    if (null == $post || is_object($post) && empty($post->ID)) {
        $post_url = uls_get_url_translated($post_url, $url_language, $options["url_type"]);
        //clean flag to control infinite recursion
        $uls_link_filter_flag = false;
        return $post_url;
    }
    //check if the URL is an special URL of WordPress and doesn't require changes
    /*$query_string_start = strpos($post_url, '?');
      if(false !== $query_string_start){
         //check special folders of WordPress
         $exclude_urls = array('wp-content', 'wp-includes', 'wp-admin');
         $start_url = substr($post_url, 0, $query_string_start);
         foreach($exclude_urls as $special_folder)
            if(false !== strpos($start_url, $special_folder))
               return $post_url;
      }*/
    //check post ID
    $post_id = $post;
    if (is_object($post)) {
        $post_id = $post->ID;
    }
    //get post language
    $post_language = uls_get_post_language($post_id);
    //if there is a language in the URL, then append the language in the link
    if (false !== $url_language) {
        //get the translation of the post
        $translation_id = uls_get_post_translation_id($post_id, $url_language);
        if ($translation_id == $post_id) {
            $post_url = uls_get_url_translated($post_url, $url_language, $options["url_type"]);
        } elseif (false !== $translation_id) {
            $post_url = uls_get_url_translated(get_permalink($translation_id), $url_language);
        } else {
            $post_url = uls_get_url_translated($post_url, $url_language, $options["url_type"]);
        }
    } else {
        //check if language is the same to the user saved language
        $saved_language = uls_get_user_saved_language();
        if (false === $saved_language) {
            $saved_language = uls_get_site_language();
        }
        //if languages are not the same
        if ($post_language != $saved_language) {
            //get the translation of the post
            $translation_id = uls_get_post_translation_id($post_id, $url_language);
            if ($translation_id != $post_id && false !== $translation_id) {
                $post_url = uls_get_url_translated(get_permalink($translation_id), $url_language);
            }
        }
    }
    //clean flag to control infinite recursion
    $uls_link_filter_flag = false;
    return $post_url;
}
function uls_link_filter($post_url, $post = null)
{
    //if global change is enabled
    global $uls_permalink_convertion;
    if (!$uls_permalink_convertion) {
        return $post_url;
    }
    //TO-DO: what happen if user is in backend? see next line
    //if user is in backend
    if (is_admin()) {
        return $post_url;
    }
    //echo "enter: " . $post_url . "<br/>";
    if (null == $post) {
        return $post_url;
    }
    $post_id = $post;
    if (is_object($post)) {
        $post_id = $post->ID;
    }
    //echo "post_id: " . $post_id . "<br/>";
    //get post language
    $post_language = uls_get_post_language($post_id);
    //get language from URL
    $language = uls_get_user_language_from_url();
    //get the general options
    $options = uls_get_options();
    //echo "lang: " . $language . "<br/>";
    //if there is a language in the URL, then append the language in the link
    if (false !== $language) {
        //get the translation of the post
        $translation_id = uls_get_post_translation_id($post_id, $language);
        //echo "Trans2: $translation_id <br/>";
        if ($translation_id == $post_id) {
            return uls_get_url_translated($post_url, $language, $options["url_type"]);
        } elseif (false !== $translation_id) {
            return get_permalink($translation_id);
        } else {
            return uls_get_url_translated($post_url, $language, $options["url_type"]);
        }
    }
    //if language is the same to the user saved language
    $saved_language = uls_get_user_saved_language();
    //echo "saved: $saved_language<br/>";
    if (false === $saved_language) {
        $saved_language = uls_get_site_language();
    }
    if ($post_language == $saved_language) {
        return $post_url;
    } else {
        //get the translation of the post
        $translation_id = uls_get_post_translation_id($post_id, $language);
        //echo "Trans: $translation_id <br/>";
        if ($translation_id == $post_id) {
            return $post_url;
        } elseif (false !== $translation_id) {
            return get_permalink($translation_id);
        }
    }
    //add language to the url
    return uls_get_url_translated($post_url, $saved_language, $options["url_type"]);
}