function bogo_update_user_option($user_id)
{
    global $wpdb;
    $meta_key = $wpdb->get_blog_prefix() . 'accessible_locale';
    if (!empty($_POST['setting_bogo_accessible_locales'])) {
        delete_user_meta($user_id, $meta_key);
        if (isset($_POST['bogo_accessible_locales'])) {
            $locales = (array) $_POST['bogo_accessible_locales'];
            $locales = bogo_filter_locales($locales);
            foreach ($locales as $locale) {
                add_user_meta($user_id, $meta_key, $locale);
            }
        }
        if (!metadata_exists('user', $user_id, $meta_key)) {
            add_user_meta($user_id, $meta_key, 'zxx');
            // zxx is a special code in ISO 639-2
        }
    }
    if (isset($_POST['bogo_own_locale'])) {
        $locale = trim($_POST['bogo_own_locale']);
        if (bogo_is_available_locale($locale)) {
            update_user_option($user_id, 'locale', $locale, true);
        }
    }
}
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_save_post($post_id, $post)
{
    if (did_action('import_start') && !did_action('import_end')) {
        // Importing
        return;
    }
    if (!bogo_is_localizable_post_type($post->post_type)) {
        return;
    }
    $old_locale = get_post_meta($post_id, '_locale', true);
    if (empty($old_locale)) {
        if (!empty($_REQUEST['locale']) && bogo_is_available_locale($_REQUEST['locale'])) {
            $locale = $_REQUEST['locale'];
        } elseif ('auto-draft' == get_post_status($post_id)) {
            $locale = bogo_get_user_locale();
        } else {
            $locale = bogo_get_default_locale();
        }
    }
    if (!empty($locale) && $locale != $old_locale) {
        update_post_meta($post_id, '_locale', $locale);
    } else {
        $locale = $old_locale;
    }
    if ($original = get_post_meta($post_id, '_original_post', true)) {
        return;
    }
    if (!empty($_REQUEST['original_post'])) {
        $original = get_post_meta($_REQUEST['original_post'], '_original_post', true);
        if (empty($original)) {
            $original = (int) $_REQUEST['original_post'];
        }
        update_post_meta($post_id, '_original_post', $original);
        return;
    }
    $original = $post_id;
    while (1) {
        $q = new WP_Query();
        $posts = $q->query(array('bogo_suppress_locale_query' => true, 'posts_per_page' => 1, 'post_status' => 'any', 'post_type' => $post->post_type, 'meta_key' => '_original_post', 'meta_value' => $original));
        if (empty($posts)) {
            update_post_meta($post_id, '_original_post', $original);
            return;
        }
        $original += 1;
    }
}
function bogo_get_page_by_path($page_path, $locale = null, $post_type = 'page')
{
    global $wpdb;
    if (!bogo_is_available_locale($locale)) {
        $locale = bogo_get_default_locale();
    }
    $page_path = rawurlencode(urldecode($page_path));
    $page_path = str_replace('%2F', '/', $page_path);
    $page_path = str_replace('%20', ' ', $page_path);
    $parts = explode('/', trim($page_path, '/'));
    $parts = array_map('esc_sql', $parts);
    $parts = array_map('sanitize_title_for_query', $parts);
    $in_string = "'" . implode("','", $parts) . "'";
    $post_type_sql = $post_type;
    $wpdb->escape_by_ref($post_type_sql);
    $q = "SELECT ID, post_name, post_parent FROM {$wpdb->posts}";
    $q .= " LEFT JOIN {$wpdb->postmeta} ON ID = {$wpdb->postmeta}.post_id AND meta_key = '_locale'";
    $q .= " WHERE 1=1";
    $q .= " AND post_name IN ({$in_string})";
    $q .= " AND (post_type = '{$post_type_sql}' OR post_type = 'attachment')";
    $q .= " AND (1=0";
    $q .= $wpdb->prepare(" OR meta_value LIKE %s", $locale);
    $q .= bogo_is_default_locale($locale) ? " OR meta_id IS NULL" : "";
    $q .= ")";
    $pages = $wpdb->get_results($q, OBJECT_K);
    $revparts = array_reverse($parts);
    $foundid = 0;
    foreach ((array) $pages as $page) {
        if ($page->post_name != $revparts[0]) {
            continue;
        }
        $count = 0;
        $p = $page;
        while ($p->post_parent != 0 && isset($pages[$p->post_parent])) {
            $count++;
            $parent = $pages[$p->post_parent];
            if (!isset($revparts[$count]) || $parent->post_name != $revparts[$count]) {
                break;
            }
            $p = $parent;
        }
        if ($p->post_parent == 0 && $count + 1 == count($revparts) && $p->post_name == $revparts[$count]) {
            $foundid = $page->ID;
            break;
        }
    }
    if ($foundid) {
        return get_page($foundid);
    }
    return null;
}
function bogo_posts_where($where, $query)
{
    global $wpdb;
    $qv =& $query->query_vars;
    if (!empty($qv['bogo_suppress_locale_query'])) {
        return $where;
    }
    $locale = empty($qv['lang']) ? '' : $qv['lang'];
    if (!bogo_is_available_locale($locale)) {
        return $where;
    }
    if (!($meta_table = _get_meta_table('post'))) {
        return $where;
    }
    $where .= " AND (1=0";
    $where .= $wpdb->prepare(" OR postmeta_bogo.meta_value LIKE %s", $locale);
    if (bogo_is_default_locale($locale)) {
        $where .= " OR postmeta_bogo.meta_id IS NULL";
    }
    $where .= ")";
    return $where;
}