Exemplo n.º 1
0
function qtranxf_get_url_info($url)
{
    $urlinfo = qtranxf_parseURL($url);
    qtranxf_complete_url_info($urlinfo);
    qtranxf_complete_url_info_path($urlinfo);
    return $urlinfo;
}
Exemplo n.º 2
0
/**
 * Expects to be set before call:
 * - $url_info['host']
 * - $url_info['path']
 * - $url_info['query']
 * - $url_info['path-base']
 * - $url_info['path-base-length']
*/
function qtranxf_parse_language_info(&$url_info, $link = false)
{
    global $q_config;
    qtranxf_complete_url_info_path($url_info);
    if (!isset($url_info['wp-path'])) {
        return false;
    }
    // url is not from this WP installation
    $doredirect = false;
    if (!defined('WP_ADMIN') || $link) {
        $lang = null;
        $url_mode = $q_config['url_mode'];
        switch ($url_mode) {
            case QTX_URL_PATH:
                // pre path
                if (!empty($url_info['wp-path']) && preg_match('!^/([a-z]{2})(/|$)!i', $url_info['wp-path'], $match)) {
                    $lang = qtranxf_resolveLangCase($match[1], $doredirect);
                    if ($lang) {
                        $url_info['lang_url'] = $lang;
                        $url_info['wp-path'] = substr($url_info['wp-path'], 3);
                        $url_info['doing_front_end'] = true;
                        if (WP_DEBUG) {
                            $url_info['url_mode'] = 'pre-path';
                        }
                    }
                }
                //}
                break;
            case QTX_URL_DOMAIN:
                // pre domain
                if (!empty($url_info['host'])) {
                    if (preg_match('#^([a-z]{2})\\.#i', $url_info['host'], $match)) {
                        $lang = qtranxf_resolveLangCase($match[1], $doredirect);
                        if ($lang) {
                            $url_info['lang_url'] = $lang;
                            $url_info['host'] = substr($url_info['host'], 3);
                            $url_info['doing_front_end'] = true;
                            if (WP_DEBUG) {
                                $url_info['url_mode'] = 'pre-domain';
                            }
                        }
                    }
                }
                break;
            case QTX_URL_DOMAINS:
                // domain per language
                if (!empty($url_info['host'])) {
                    foreach ($q_config['enabled_languages'] as $lang) {
                        //todo should have hash host->lang
                        if (!isset($q_config['domains'][$lang])) {
                            continue;
                        }
                        if ($q_config['domains'][$lang] != $url_info['host']) {
                            continue;
                        }
                        $url_info['lang_url'] = $lang;
                        if ($lang != $q_config['default_language'] || strpos(get_option('siteurl'), $url_info['host']) === FALSE) {
                            $url_info['doing_front_end'] = true;
                        }
                        if (WP_DEBUG) {
                            $url_info['url_mode'] = 'per-domain';
                        }
                        break;
                    }
                }
                break;
            default:
                /**
                 * Hook for possible other methods
                 * Set, if applicable:
                 * $url_info['lang_url']
                 * $url_info['doing_front_end']
                 * $url_info['path'] - convert to language neutral or default
                 */
                $url_info = apply_filters('qtranslate_parse_language_info_mode', $url_info, $q_config['url_mode']);
                if (isset($url_info['lang_url'])) {
                    $lang = $url_info['lang_url'];
                }
                break;
        }
    }
    $lang = null;
    if (!$link) {
        if (isset($_GET['lang'])) {
            $lang = qtranxf_resolveLangCase($_GET['lang'], $doredirect);
            if ($lang) {
                $url_info['lang_query_get'] = $lang;
            }
        } else {
            if (isset($_POST['lang'])) {
                $lang = qtranxf_resolveLangCase($_POST['lang'], $doredirect);
                if ($lang) {
                    $url_info['lang_query_post'] = $lang;
                }
                //todo excessive?
            }
        }
    } elseif (!empty($url_info['query']) && preg_match('/(^|&|&|&|\\?)lang=([a-z]{2})/i', $url_info['query'], $match)) {
        //rx is changed in 3.4.6.5: https://github.com/qTranslate-Team/qtranslate-x/issues/288
        $lang = qtranxf_resolveLangCase($match[2], $doredirect);
        if ($lang) {
            $url_info['lang_query_link'] = $lang;
        }
        //todo excessive?
    }
    if ($lang) {
        $url_info['lang_query'] = $lang;
        qtranxf_del_query_arg($url_info['query'], 'lang');
        //if(isset($url_info['lang_url'])){
        //	if($lang !== $url_info['lang_url']) $doredirect=true;
        //}elseif(!defined('WP_ADMIN')){
        if ($q_config['url_mode'] != QTX_URL_QUERY) {
            $doredirect = true;
        }
        //}
    } else {
        if (isset($url_info['lang_url'])) {
            $lang = $url_info['lang_url'];
            if ($q_config['hide_default_language'] && $lang == $q_config['default_language']) {
                $doredirect = true;
            }
        }
    }
    if ($lang) {
        $url_info['language'] = $lang;
    }
    if ($doredirect) {
        $url_info['doredirect'] = 'detected in parse_language_info';
    }
    if (!isset($url_info['doing_front_end'])) {
        $language_neutral_path = qtranxf_language_neutral_path($url_info['wp-path']);
        $url_info['language_neutral_path'] = $language_neutral_path;
        if (!$language_neutral_path) {
            $url_info['doing_front_end'] = true;
        }
    }
    /**
     * Hook for possible other methods
     * Set $url_info['language'] with the result
     */
    $url_info = apply_filters('qtranslate_parse_language_info', $url_info, $link);
    //slug?
    if (isset($url_info['language'])) {
        $lang = $url_info['language'];
    }
    return $lang;
}