function bogo_get_user_locale($user_id = 0)
{
    global $current_user;
    $default_locale = bogo_get_default_locale();
    $user_id = absint($user_id);
    if (!$user_id) {
        if (function_exists('wp_get_current_user') && !empty($current_user)) {
            $user_id = get_current_user_id();
        } elseif (!($user_id = apply_filters('determine_current_user', false))) {
            return $default_locale;
        }
    }
    $locale = get_user_option('locale', $user_id);
    if (bogo_is_available_locale($locale) && user_can($user_id, 'bogo_access_locale', $locale)) {
        return $locale;
    }
    if (user_can($user_id, 'bogo_access_locale', $default_locale)) {
        return $default_locale;
    }
    foreach ((array) bogo_available_locales() as $locale) {
        if (user_can($user_id, 'bogo_access_locale', $locale)) {
            return $locale;
        }
    }
    return $default_locale;
}
function bogo_setup_nav_menu_item($menu_item)
{
    if (isset($menu_item->bogo_locales)) {
        return $menu_item;
    }
    $menu_item->bogo_locales = array();
    if (isset($menu_item->post_type) && 'nav_menu_item' == $menu_item->post_type) {
        $menu_item->bogo_locales = get_post_meta($menu_item->ID, '_locale');
    }
    if ($menu_item->bogo_locales) {
        $menu_item->bogo_locales = bogo_filter_locales($menu_item->bogo_locales);
    } else {
        $menu_item->bogo_locales = bogo_available_locales();
    }
    return $menu_item;
}
function bogo_flag_css()
{
    $flags = array();
    if (apply_filters('bogo_use_flags', true)) {
        $locales = bogo_available_locales();
        foreach ($locales as $locale) {
            if ($flag = bogo_get_flag($locale)) {
                $flags[$locale] = $flag;
            }
        }
    }
    if (!$flags) {
        return;
    }
    $side = is_rtl() ? 'right' : 'left';
    echo '<style type="text/css">' . "\n";
    foreach ($flags as $locale => $flag) {
        echo '.bogo-language-switcher .' . bogo_language_tag($locale) . ' {';
        echo ' background: url("' . $flag . '") no-repeat ' . $side . ' center;';
        echo ' }' . "\n";
    }
    echo '</style>' . "\n";
}
function bogo_tools_page()
{
    $message = "";
    if (isset($_GET['message'])) {
        if ('success' == $_GET['message']) {
            $message = __("Translation installed successfully.", 'bogo');
        } elseif ('failed' == $_GET['message']) {
            $message = __("Translation install failed.", 'bogo');
        }
    }
    $default_locale = bogo_get_default_locale();
    $available_locales = bogo_available_locales();
    ?>
<div class="wrap">

<h2><?php 
    echo esc_html(__('Bogo Tools', 'bogo'));
    ?>
</h2>

<?php 
    if (!empty($message)) {
        ?>
<div id="message" class="updated"><p><?php 
        echo esc_html($message);
        ?>
</p></div>
<?php 
    }
    ?>

<h3 class="title"><?php 
    echo esc_html(__('Available Languages', 'bogo'));
    ?>
</h3>

<table id="bogo-languages-table" class="widefat">
<thead>
	<tr><th></th><th><?php 
    echo esc_html(__('Language', 'bogo'));
    ?>
</th></tr>
</thead>
<tfoot>
	<tr><th></th><th><?php 
    echo esc_html(__('Language', 'bogo'));
    ?>
</th></tr>
</tfoot>
<tbody id="translations">
	<tr><th>1</th><td><p>
		<strong><?php 
    echo esc_html(bogo_get_language($default_locale));
    ?>
</strong>
		[<?php 
    echo esc_html($default_locale);
    ?>
]
		<br /><?php 
    echo esc_html(__('Site Default Language', 'bogo'));
    ?>
	</p></td></tr>

<?php 
    $count = 1;
    foreach ($available_locales as $locale) {
        if ($locale == $default_locale) {
            continue;
        }
        $count += 1;
        ?>
	<tr><th><?php 
        echo $count;
        ?>
</th><td><p>
		<strong><?php 
        echo esc_html(bogo_get_language($locale));
        ?>
</strong>
		[<?php 
        echo esc_html($locale);
        ?>
]
		<br /><?php 
        echo esc_html(__('Installed', 'bogo'));
        ?>
	</p></td></tr>
<?php 
    }
    $can_install = wp_can_install_language_pack();
    foreach (wp_get_available_translations() as $locale => $translation) {
        if (in_array($locale, $available_locales)) {
            continue;
        }
        $count += 1;
        $install_link = '';
        if ($can_install) {
            $install_link = menu_page_url('bogo-tools', false);
            $install_link = add_query_arg(array('action' => 'install_translation', 'locale' => $locale), $install_link);
            $install_link = wp_nonce_url($install_link, 'bogo-tools');
            $install_link = sprintf('<a href="%1$s" class="install">%2$s</a>', $install_link, esc_html(__('Install', 'bogo')));
        }
        ?>
	<tr><th><?php 
        echo $count;
        ?>
</th><td><p>
		<strong><?php 
        echo esc_html(bogo_get_language($locale));
        ?>
</strong>
		[<?php 
        echo esc_html($locale);
        ?>
]
		<?php 
        echo $install_link;
        ?>
	</p></td></tr>
<?php 
    }
    ?>

</tbody>
</table>

</div>
<?php 
}
function bogo_m17n_headers()
{
    $languages = array();
    if (is_singular()) {
        $post_id = get_queried_object_id();
        if ($post_id && ($translations = bogo_get_post_translations($post_id))) {
            $locale = get_locale();
            $translations[$locale] = get_post($post_id);
            foreach ($translations as $lang => $translation) {
                $languages[] = array('hreflang' => bogo_language_tag($lang), 'href' => get_permalink($translation));
            }
        }
    } else {
        $available_locales = bogo_available_locales();
        if (1 < count($available_locales)) {
            foreach ($available_locales as $locale) {
                $languages[] = array('hreflang' => bogo_language_tag($locale), 'href' => bogo_url(null, $locale));
            }
        }
    }
    $languages = apply_filters('bogo_rel_alternate_hreflang', $languages);
    foreach ((array) $languages as $language) {
        $hreflang = isset($language['hreflang']) ? $language['hreflang'] : '';
        $href = isset($language['href']) ? $language['href'] : '';
        if ($hreflang && $href) {
            $link = sprintf('<link rel="alternate" hreflang="%1$s" href="%2$s" />', esc_attr($hreflang), esc_url($href));
            echo $link . "\n";
        }
    }
}
function bogo_get_lang_from_url($url = '')
{
    if (!$url) {
        $url = is_ssl() ? 'https://' : 'http://';
        $url .= $_SERVER['HTTP_HOST'];
        $url .= $_SERVER['REQUEST_URI'];
    }
    if ($frag = strstr($url, '#')) {
        $url = substr($url, 0, -strlen($frag));
    }
    $home = set_url_scheme(get_option('home'));
    $home = trailingslashit($home);
    $available_languages = array_map('bogo_lang_slug', bogo_available_locales());
    $regex = '#^' . preg_quote($home) . '(' . implode('|', $available_languages) . ')/#';
    if (preg_match($regex, trailingslashit($url), $matches)) {
        return $matches[1];
    }
    if ($query = @parse_url($url, PHP_URL_QUERY)) {
        parse_str($query, $query_vars);
        if (isset($query_vars['lang']) && in_array($query_vars['lang'], $available_languages)) {
            return $query_vars['lang'];
        }
    }
    return false;
}