Function Reference: geodir_merge_location

Summary

Merge locations.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None

Package

GeoDirectory_Location_Manager

Change Log

Since: 1.0.0

Source File

geodir_merge_location() is located in geodir_location_manager/geodir_location_functions.php [Line: 1338]

Source Code

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
										post_location_id=%d,
										post_city	= %s,
										post_region	= %s,
										post_country	= %s,
										post_locations = %s
										WHERE  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
				country=%s,
				region=%s,
				city=%s,
				city_latitude=%s,
				city_longitude=%s,
				country_slug = %s,
				region_slug = %s,
				city_slug = %s
				".$setdefault."
				WHERE 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();
		}
}