function bogo_admin_bar_menu($wp_admin_bar)
{
    $current_locale = bogo_get_user_locale();
    $current_language = bogo_get_language($current_locale);
    if (!$current_language) {
        $current_language = $current_locale;
    }
    $wp_admin_bar->add_menu(array('parent' => 'top-secondary', 'id' => 'bogo-user-locale', 'title' => '✔ ' . $current_language));
    $available_languages = bogo_available_languages(array('exclude' => array($current_locale), 'current_user_can_access' => true));
    foreach ($available_languages as $locale => $lang) {
        $url = admin_url('profile.php?action=bogo-switch-locale&locale=' . $locale);
        $url = add_query_arg(array('redirect_to' => urlencode($_SERVER['REQUEST_URI'])), $url);
        $url = wp_nonce_url($url, 'bogo-switch-locale');
        $wp_admin_bar->add_menu(array('parent' => 'bogo-user-locale', 'id' => 'bogo-user-locale-' . $locale, 'title' => $lang, 'href' => $url));
    }
}
function bogo_metabox_add_translation($post)
{
    if ('auto-draft' == $post->post_status) {
        return;
    }
    $post_locale = bogo_get_post_locale($post->ID);
    $user_locale = bogo_get_user_locale();
    if ($post_locale == $user_locale) {
        return;
    }
    $locale = $user_locale;
    $translations = bogo_get_post_translations($post->ID);
    if (isset($translations[$locale])) {
        return;
    }
    $lang = bogo_get_language($locale);
    if (empty($lang)) {
        $lang = $locale;
    }
    $edit_link = admin_url('post-new.php?post_type=' . $post->post_type . '&locale=' . $locale . '&original_post=' . $post->ID);
    ?>
<p class="textright"><a href="<?php 
    echo $edit_link;
    ?>
" target="_blank" class="button"><?php 
    echo esc_html(sprintf(__('Add %s translation', 'bogo'), $lang));
    ?>
</a></p>
<?php 
}
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_available_languages($args = '')
{
    $defaults = array('exclude' => array(), 'orderby' => 'key', 'order' => 'ASC', 'current_user_can_access' => false);
    $args = wp_parse_args($args, $defaults);
    $langs = array();
    $available_locales = bogo_available_locales($args);
    foreach ($available_locales as $locale) {
        $lang = bogo_get_language($locale);
        $langs[$locale] = empty($lang) ? "[{$locale}]" : $lang;
    }
    if ('value' == $args['orderby']) {
        natcasesort($langs);
        if ('DESC' == $args['order']) {
            $langs = array_reverse($langs);
        }
    } else {
        if ('DESC' == $args['order']) {
            krsort($langs);
        } else {
            ksort($langs);
        }
    }
    $langs = apply_filters('bogo_available_languages', $langs, $args);
    return $langs;
}