/**
 * Action to save location related information in post type detail table on add/edit new listing action.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 *
 * @param int $last_post_id The saved post ID.
 * @param array $request_info The post details in an array.
 */
function geodir_save_listing_location($last_post_id, $request_info)
{
    global $wpdb;
    $location_info = array();
    if (isset($request_info['post_neighbourhood'])) {
        $location_info['post_neighbourhood'] = $request_info['post_neighbourhood'];
    }
    if (isset($request_info['post_city']) && isset($request_info['post_region'])) {
        $post_location_id = geodir_get_post_meta($last_post_id, 'post_location_id', true);
        $post_location = geodir_get_location_by_id('', $post_location_id);
        $location_info['post_locations'] = '[' . $post_location->city_slug . '],[' . $post_location->region_slug . '],[' . $post_location->country_slug . ']';
        // set all overall post location
    }
    if (!empty($location_info)) {
        geodir_save_post_info($last_post_id, $location_info);
    }
}
/**
 * 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;
}