* @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 */
global $wpdb, $table_prefix;
$rows = get_post_location_countries();
$nonce = wp_create_nonce('update_location_translate');
$post_action = isset($_POST['action']) ? $_POST['action'] : '';
$post_wpnonce = isset($_POST['update_location_translate']) ? $_POST['update_location_translate'] : '';
$post_country_slug = isset($_POST['country_slug']) && !empty($_POST['country_slug']) ? $_POST['country_slug'] : '';
$success = 0;
$submit = false;
if ($post_action == 'update_location_translate' && $post_wpnonce && $post_country_slug && is_array($post_country_slug)) {
    if (wp_verify_nonce($post_wpnonce, 'update_location_translate')) {
        foreach ($post_country_slug as $post_slug) {
            $post_country = get_post_country_by_slug($post_slug);
            $return = geodir_update_location_translate($post_slug);
            if ($return) {
                $success++;
            }
        }
        $submit = true;
    }
}
$msg = '';
if ($submit) {
    if ($success > 0) {
        $msg = MSG_MANAGE_LOCATION_TRANSLATE_SUCCESS;
    } else {
        $msg = MSG_MANAGE_LOCATION_TRANSLATE_FAIL;
    }
/**
 * Update location with translated string.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 *
 * @param $country_slug
 * @return bool
 */
function geodir_update_location_translate($country_slug)
{
    global $wpdb, $plugin_prefix;
    if ($country_slug == '') {
        return false;
    }
    $country = get_post_country_by_slug($country_slug);
    if ($country == '') {
        return false;
    }
    $geodir_posttypes = geodir_get_posttypes();
    $country_translated = __($country, GEODIRECTORY_TEXTDOMAIN);
    $country_translated = trim(wp_unslash($country_translated));
    $country_slug_translated = sanitize_title($country_translated);
    $country_slug = apply_filters('geodir_filter_update_location_translate', $country_slug, $country, $country_translated, $country_slug_translated);
    do_action('geodir_action_update_location_translate', $country_slug, $country, $country_translated, $country_slug_translated);
    if ($country_slug == $country_slug_translated) {
        return false;
    }
    $sql = $wpdb->prepare("SELECT location_id FROM " . POST_LOCATION_TABLE . " WHERE country_slug=%s", array($country_slug));
    $location_ids = $wpdb->get_col($sql);
    /* update in post locations table */
    $update_locations = false;
    //$sql = $wpdb->prepare( "UPDATE " . POST_LOCATION_TABLE . " SET country=%s, country_slug=%s WHERE country_slug=%s", array( $country_translated, $country_slug_translated, $country_slug ) );
    $sql = $wpdb->prepare("UPDATE " . POST_LOCATION_TABLE . " SET country_slug=%s WHERE country_slug=%s", array($country_slug_translated, $country_slug));
    $update_locations = $wpdb->query($sql);
    /* update in post listings table */
    $update_listings = false;
    if (!empty($location_ids)) {
        $location_ids = implode(",", $location_ids);
        foreach ($geodir_posttypes as $geodir_posttype) {
            $table = $plugin_prefix . $geodir_posttype . '_detail';
            $sql = "SELECT post_id, post_locations, post_location_id FROM " . $table . " WHERE post_location_id IN(" . $location_ids . ")";
            $listings = $wpdb->get_results($sql);
            if (!empty($listings)) {
                foreach ($listings as $listing) {
                    $post_id = $listing->post_id;
                    $location_id = $listing->post_location_id;
                    $post_locations = $listing->post_locations;
                    if ($post_locations != '') {
                        $post_locations_arr = explode(",", $post_locations);
                        if (isset($post_locations_arr[2]) && trim($post_locations_arr[2]) != '[]') {
                            $post_locations_arr[2] = '[' . $country_slug_translated . ']';
                            $post_locations = implode(",", $post_locations_arr);
                        } else {
                            $post_locations = '';
                        }
                    }
                    if ($post_locations == '') {
                        $location_info = geodir_get_location_by_id('', $location_id);
                        if (!empty($location_info) && isset($location_info->location_id)) {
                            $post_locations = '[' . $location_info->city_slug . '],[' . $location_info->region_slug . '],[' . $country_slug_translated . ']';
                        }
                    }
                    $sql = $wpdb->prepare("UPDATE " . $table . " SET post_locations=%s, post_country=%s WHERE post_id=%d", array($post_locations, $country_translated, $post_id));
                    $update_locations = $wpdb->query($sql);
                }
            }
        }
        $update_locations = true;
    }
    /* update in location seo table */
    $update_location_seo = false;
    $sql = $wpdb->prepare("UPDATE " . LOCATION_SEO_TABLE . " SET country_slug=%s WHERE country_slug=%s", array($country_slug_translated, $country_slug));
    $update_location_seo = $wpdb->query($sql);
    if ($update_locations || $update_listings || $update_location_seo) {
        return true;
    }
    return false;
}