/**
 * Merge locations.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 */
function geodir_merge_location()
{
    global $wpdb, $plugin_prefix;
    if (isset($_REQUEST['location_merge_nonce']) && current_user_can('manage_options')) {
        if (!wp_verify_nonce($_REQUEST['location_merge_nonce'], 'location_merge_wpnonce')) {
            exit;
        }
        $geodir_location_merge_ids = trim($_REQUEST['geodir_location_merge_ids'], ',');
        $gd_merge = $_REQUEST['gd_merge'];
        $gd_city = $_REQUEST['gd_city'];
        $gd_region = $_REQUEST['gd_region'];
        $gd_country = $_REQUEST['gd_country'];
        $gd_lat = $_REQUEST['gd_lat'];
        $gd_log = $_REQUEST['gd_log'];
        $geodir_postlocation_merge_ids = array();
        $geodir_merge_ids_array = explode(',', $geodir_location_merge_ids);
        $geodir_merge_ids_length = count($geodir_merge_ids_array);
        $format = array_fill(0, $geodir_merge_ids_length, '%d');
        $format = implode(',', $format);
        $geodir_postlocation_merge_ids = $geodir_merge_ids_array;
        $geodir_postlocation_merge_ids[] = $gd_merge;
        $gd_location_sql = $wpdb->prepare("select * from " . POST_LOCATION_TABLE . " WHERE location_id IN ({$format}) AND location_id!=%d", $geodir_postlocation_merge_ids);
        $gd_locationinfo = $wpdb->get_results($gd_location_sql);
        $check_default = '';
        foreach ($gd_locationinfo as $gd_locationinfo_obj) {
            $locationid = $gd_locationinfo_obj->location_id;
            if (!$check_default) {
                $check_default = $wpdb->get_var($wpdb->prepare("SELECT location_id FROM " . POST_LOCATION_TABLE . " WHERE is_default='1' AND location_id = %d", array($locationid)));
            }
            /*$location_hood = $wpdb->get_results(
            				$wpdb->prepare(
            					"SELECT hood_id FROM ".POST_NEIGHBOURHOOD_TABLE." WHERE hood_location_id=%d",
            					array($locationid)
            				)
            			);
            
            			if(!empty($location_hood)){
            				foreach($location_hood as $hood_del){
            
            					geodir_neighbourhood_delete($hood_del->hood_id);
            
            				}
            			}*/
            $gd_location_del = $wpdb->prepare("DELETE FROM " . POST_LOCATION_TABLE . " WHERE  location_id = %d", array($locationid));
            $wpdb->query($gd_location_del);
        }
        $country_slug = create_location_slug($gd_country);
        $region_slug = create_location_slug($gd_region);
        $city_slug = create_location_slug($gd_city);
        //FILL SELECTED CITY IN MERGE LOCATIONS POST
        $geodir_posttypes = geodir_get_posttypes();
        foreach ($geodir_posttypes as $geodir_posttype) {
            $table = $plugin_prefix . $geodir_posttype . '_detail';
            $gd_placedetail_sql = $wpdb->prepare("select * from " . $table . " WHERE post_location_id IN ({$format})", $geodir_merge_ids_array);
            $gd_placedetailinfo = $wpdb->get_results($gd_placedetail_sql);
            foreach ($gd_placedetailinfo as $gd_placedetailinfo_obj) {
                $postid = $gd_placedetailinfo_obj->post_id;
                $post_locations = '[' . $city_slug . '],[' . $region_slug . '],[' . $country_slug . ']';
                // set all overall post location
                $gd_rep_locationid = $wpdb->prepare("UPDATE " . $table . " SET\n\t\t\t\t\t\t\t\t\t\tpost_location_id=%d,\n\t\t\t\t\t\t\t\t\t\tpost_city\t= %s,\n\t\t\t\t\t\t\t\t\t\tpost_region\t= %s,\n\t\t\t\t\t\t\t\t\t\tpost_country\t= %s,\n\t\t\t\t\t\t\t\t\t\tpost_locations = %s\n\t\t\t\t\t\t\t\t\t\tWHERE  post_id = %d", array($gd_merge, $gd_city, $gd_region, $gd_country, $post_locations, $postid));
                $wpdb->query($gd_rep_locationid);
            }
        }
        $setdefault = '';
        if (isset($check_default) && $check_default != '') {
            $setdefault = ", is_default='1'";
        }
        //UPDATE SELECTED LOCATION
        $sql = $wpdb->prepare("UPDATE " . POST_LOCATION_TABLE . " SET\n\t\t\t\tcountry=%s,\n\t\t\t\tregion=%s,\n\t\t\t\tcity=%s,\n\t\t\t\tcity_latitude=%s,\n\t\t\t\tcity_longitude=%s,\n\t\t\t\tcountry_slug = %s,\n\t\t\t\tregion_slug = %s,\n\t\t\t\tcity_slug = %s\n\t\t\t\t" . $setdefault . "\n\t\t\t\tWHERE location_id = %d", array($gd_country, $gd_region, $gd_city, $gd_lat, $gd_log, $country_slug, $region_slug, $city_slug, $gd_merge));
        $wpdb->query($sql);
        if ($setdefault != '') {
            geodir_location_set_default($gd_merge);
        }
        /* ----- update hooks table ---- */
        $location_hood_info = $wpdb->query($wpdb->prepare("UPDATE " . POST_NEIGHBOURHOOD_TABLE . " SET hood_location_id=" . $gd_merge . " WHERE hood_location_id IN ({$format})", $geodir_merge_ids_array));
        $msg = MSG_LOCATION_MERGE_SUCCESS;
        $msg = urlencode($msg);
        $location = admin_url() . "admin.php?page=geodirectory&tab=managelocation_fields&subtab=geodir_location_manager&location_success=" . $msg;
        wp_redirect($location);
        exit;
    } else {
        wp_redirect(home_url() . '/?geodir_signup=true');
        exit;
    }
}
/**
 * Adds new location using location info.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @param array $location_info {
 *     Array of location info arguments.
 *
 *     @type string         $city               The city string.
 *     @type string         $region             The region string.
 *     @type string         $country            The country string.
 *     @type string         $geo_lat            The latitude string.
 *     @type string         $geo_lng            The longitude string.
 *     @type string|bool    $is_default         Is this the default location?.
 * }
 * @return string|bool Location ID on success. False when Fail.
 */
function geodir_add_new_location($location_info = array())
{
    global $wpdb;
    if (!empty($location_info)) {
        $location_city = $location_info['city'] != '' ? $location_info['city'] : 'all';
        $location_region = $location_info['region'] != '' ? $location_info['region'] : 'all';
        $location_country = $location_info['country'] != '' ? $location_info['country'] : 'all';
        $location_lat = $location_info['geo_lat'] != '' ? $location_info['geo_lat'] : '';
        $location_lng = $location_info['geo_lng'] != '' ? $location_info['geo_lng'] : '';
        $is_default = isset($location_info['is_default']) ? $location_info['is_default'] : '';
        $country_slug = create_location_slug(__($location_country, 'geodirectory'));
        $region_slug = create_location_slug($location_region);
        $city_slug = create_location_slug($location_city);
        /**
         * Filter add new location data.
         *
         * @since 1.0.0
         */
        $geodir_location = (object) apply_filters('geodir_add_new_location', array('location_id' => 0, 'country' => $location_country, 'region' => $location_region, 'city' => $location_city, 'country_slug' => $country_slug, 'region_slug' => $region_slug, 'city_slug' => $city_slug, 'city_latitude' => $location_lat, 'city_longitude' => $location_lng, 'is_default' => $is_default));
        if ($geodir_location->country) {
            $get_country = $wpdb->get_var($wpdb->prepare("SELECT Country FROM " . GEODIR_COUNTRIES_TABLE . " WHERE Country=%s", array($geodir_location->country)));
            if (empty($get_country)) {
                $wpdb->query($wpdb->prepare("INSERT INTO " . GEODIR_COUNTRIES_TABLE . " (Country, Title) VALUES (%s,%s)", array($geodir_location->country, $geodir_location->country)));
            }
        }
        if ($geodir_location->is_default) {
            update_option('geodir_default_location', $geodir_location);
        }
        return $geodir_location->location_id;
    } else {
        return false;
    }
}
/**
 * Update post term count for the given post id.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @param int $post_id The post ID.
 * @param array $post {
 *    Attributes of the location array.
 *
 *    @type string $post_type The post type.
 *    @type string $post_country The country name.
 *    @type string $post_region The region name.
 *    @type string $post_city The city name.
 *
 * }
 */
function geodir_term_post_count_update($post_id, $post)
{
    $geodir_posttypes = geodir_get_posttypes();
    //print_r($post);exit;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    /*print_r($post);
      echo '###';
      print_r($_REQUEST);
      exit;*/
    if (!isset($post['post_type'])) {
        $post['post_type'] = get_post_type($post_id);
    }
    if (!wp_is_post_revision($post_id) && isset($post['post_type']) && in_array($post['post_type'], $geodir_posttypes)) {
        //if( !wp_is_post_revision( $post_id ) && isset($post->post_type) && in_array($post->post_type,$geodir_posttypes )) {
        //if ( !wp_verify_nonce( $_POST['geodir_post_info_noncename'], 'geodirectory/geodirectory-admin/admin_functions.php' ) )
        //    return;
        $country = isset($post['post_country']) ? $post['post_country'] : '';
        $region = isset($post['post_region']) ? $post['post_region'] : '';
        $city = isset($post['post_city']) ? $post['post_city'] : '';
        $country_slug = create_location_slug($country);
        $region_slug = create_location_slug($region);
        $city_slug = create_location_slug($city);
        $loc = array();
        $loc['gd_city'] = $city_slug;
        $loc['gd_region'] = $region_slug;
        $loc['gd_country'] = $country_slug;
        foreach ($loc as $key => $value) {
            if ($value != '') {
                geodir_get_loc_term_count('term_count', $value, $key, true, $loc);
            }
        }
        //exit;
    }
}