/**
 * This function check if the redirection based on the browser language is enabled. If it is and the user is in the home page, then the user is redirected to the home page with the specified language.
 *
 * @return mixed it returns false if the redirection is not possible, due to some of the restriction mentioned above. Otherwise, it just redirects the user.
 */
function uls_redirect_by_browser_language()
{
    $options = uls_get_options();
    if (!isset($options['use_browser_language_to_redirect_visitors']) || !$options['use_browser_language_to_redirect_visitors']) {
        return false;
    }
    $type = 'frontend';
    $url = uls_get_browser_url();
    $homeUrl = get_bloginfo('url') . '/';
    //if user is in the home page
    if ($homeUrl == $url) {
        //if the redirection is enabled
        if ((!isset($options['user_browser_language_detection']) || $options['user_browser_language_detection']) && "no" != get_user_meta(get_current_user_id(), "uls_{$type}_browser_language", true)) {
            $language = uls_get_user_language_from_browser();
            //if the browser language is different to the site language
            if ("" != $language && $language != uls_get_site_language()) {
                $frontpage_id = get_option('page_on_front');
                // get front page id
                $post_id_translation = uls_get_post_translation_id($frontpage_id, $language);
                // get page id translation
                $redirectUrl = $homeUrl;
                // save this atribute to after check the iformation
                // check the post translation if it exits redirect the page
                if ($post_id_translation) {
                    $homeUrl = get_page_link($post_id_translation);
                    // get url page translation
                    //redirect to the browser language
                    $redirectUrl = uls_get_url_translated($homeUrl, $language);
                } else {
                    $redirectUrl = uls_get_url_translated($url, $language);
                }
                //check if it is the first redirection
                if (!session_id()) {
                    session_start();
                }
                if ($url != $redirectUrl && empty($_SESSION['uls_home_redirection']) && empty($_COOKIE['uls_home_redirection'])) {
                    //save temporal vars to avoid redirection in the home page again
                    $time = date_format(new DateTime(), 'Y-m-d H:i');
                    setcookie('uls_home_redirection', $time, time() + 2 * 60 * 60);
                    //set a cookie for 2 hour
                    $_SESSION['uls_home_redirection'] = $time;
                    //save temporal var in session
                } else {
                    return false;
                }
                //redirect
                if ($url != $redirectUrl) {
                    wp_redirect($redirectUrl);
                    exit;
                }
            }
            //browser language different to site language
        }
        //redirection enabled
    }
    //is in home
    return false;
}
예제 #2
0
/**
 * This function check if the redirection based on the browser language is enabled. If it is and the user is in the home page, then is redirected to the home page with the specified language.
 *
 * @return mixed it returns false if the redirection is not possible, due to some of the restriction mentioned above. Otherwise, it just redirects the user.
 */
function uls_redirect_by_browser_language()
{
    $type = 'frontend';
    $url = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https://" : "http://";
    $url .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    $homeUrl = get_bloginfo('url') . '/';
    //if user is in the home page
    if ($homeUrl == $url) {
        $options = uls_get_options();
        //if the redirection is enabled
        if ((!isset($options['user_browser_language_detection']) || $options['user_browser_language_detection']) && "no" != get_user_meta(get_current_user_id(), "uls_{$type}_browser_language", true)) {
            $language = uls_get_user_language_from_browser();
            //if the browser language is different to the site language
            if ("" != $language && $language != uls_get_site_language()) {
                //redirect to the browser language
                $redirectUrl = uls_get_url_translated($homeUrl, $language);
                //check if it is the first redirection
                if (!session_id()) {
                    session_start();
                }
                if (empty($_SESSION['uls_home_redirection']) && empty($_COOKIE['uls_home_redirection'])) {
                    //save temporal vars to avoid redirection in the home page again
                    $time = date_format(new DateTime(), 'Y-m-d H:i');
                    setcookie('uls_home_redirection', $time, time() + 2 * 60 * 60);
                    //set a cookie for 2 hour
                    $_SESSION['uls_home_redirection'] = $time;
                    //save temporal var in session
                } else {
                    return false;
                }
                //redirect
                if ($url != $redirectUrl) {
                    wp_redirect($redirectUrl);
                    exit;
                }
            }
            //browser language different to site language
        }
        //redirection enabled
    }
    //is in home
    return false;
}