/**
 * Set location data in session.
 *
 * @since 1.0.0
 * @package GeoDirectory_Location_Manager
 *
 * @global object $wpdb WordPress Database object.
 *
 * @param object $wp WordPress object.
 */
function geodir_set_location_var_in_session($wp)
{
    //unset($_SESSION['gd_location_shared']);
    /*$point1=array('latitude'=>'-22.5260060699' ,'longitude'=> '-43.7334400235' ) ;
    $point1=array('latitude'=>'28.635308' ,'longitude'=> '77.22496' ) ;
    $point2=array('latitude'=>'-22.7024218' ,'longitude'=> '-43.33662349999997' ) ;
    $point2=array('latitude'=>'-22.7356363' ,'longitude'=> '-43.44001100000003' ) ;
    
    echo geodir_calculateDistanceFromLatLong($point1, $point2);
    */
    // Avoide all the changes made by core, restore original queryvars ;
    //	$wp->query_vars=$wp->geodir_query_vars ;
    // this code will determine when a user wants to switch location
    // A location can be switched using 3 ways
    //1. usign location switcher, in this case the url will always have location prefix
    // Query Vars will have page_id parameter
    // check if query var has page_id and that page id is location page
    //print_r($_SESSION);
    // my location set start
    //Fix for WPML removing page_id query var:
    if (isset($wp->query_vars['page']) && !isset($wp->query_vars['page_id']) && isset($wp->query_vars['pagename'])) {
        global $wpdb;
        $real_page_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type='page' AND post_name=%s", $wp->query_vars['pagename']));
        if ($real_page_id) {
            $wp->query_vars['page_id'] = $real_page_id;
        }
    }
    if (isset($wp->query_vars['gd_country']) && $wp->query_vars['gd_country'] == 'me' && isset($_SESSION['user_lat']) && $_SESSION['user_lat'] && isset($_SESSION['user_lon']) && $_SESSION['user_lon'] || isset($_SESSION['all_near_me']) && is_admin()) {
        if (isset($_REQUEST['user_lat']) && $_REQUEST['user_lat']) {
            $_SESSION['user_lat'] = $_REQUEST['user_lat'];
        }
        if (isset($_REQUEST['user_lon']) && $_REQUEST['user_lon']) {
            $_SESSION['user_lon'] = $_REQUEST['user_lon'];
        }
        if (isset($_SESSION['near_me_range']) && $_SESSION['near_me_range']) {
            $_REQUEST['sdist'] = $_SESSION['near_me_range'];
        }
        $_SESSION['all_near_me'] = 1;
        $_REQUEST['sgeo_lat'] = $_SESSION['user_lat'];
        $_REQUEST['sgeo_lon'] = $_SESSION['user_lon'];
        $_REQUEST['snear'] = 1;
        $_SESSION['gd_multi_location'] = 0;
        //unset any locations
        unset($_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
        return;
    } elseif (isset($wp->query_vars['gd_country']) && $wp->query_vars['gd_country'] == 'me') {
        // at the near me page but with no location
        add_action('wp_head', 'geodir_set_user_location_near_me');
        return;
    } else {
        if (isset($_SESSION['all_near_me'])) {
            unset($_SESSION['all_near_me']);
        }
    }
    // my location set end
    geodir_set_is_geodir_page($wp);
    if (!get_option('geodir_set_as_home')) {
        if (empty($wp->query_vars) || !array_diff(array_keys($wp->query_vars), array('preview', 'page', 'paged', 'cpage'))) {
            if ('page' == get_option('show_on_front')) {
                $wp->query_vars['page_id'] = get_option('page_on_front');
            }
        }
    }
    if (isset($wp->query_vars['page_id']) && $wp->query_vars['page_id'] == geodir_location_page_id() || isset($_REQUEST['set_location_type']) && $_REQUEST['set_location_type'] && isset($_REQUEST['set_location_val']) && $_REQUEST['set_location_val']) {
        $gd_country = '';
        $gd_region = '';
        $gd_city = '';
        if (isset($wp->query_vars['gd_country']) && $wp->query_vars['gd_country'] != '') {
            $gd_country = urldecode($wp->query_vars['gd_country']);
        }
        if (isset($wp->query_vars['gd_region']) && $wp->query_vars['gd_region'] != '') {
            $gd_region = urldecode($wp->query_vars['gd_region']);
        }
        if (isset($wp->query_vars['gd_city']) && $wp->query_vars['gd_city'] != '') {
            $gd_city = urldecode($wp->query_vars['gd_city']);
        }
        if (!($gd_country == '' && $gd_region == '' && $gd_city == '')) {
            $default_location = geodir_get_default_location();
            if (get_option('geodir_add_location_url')) {
                if (get_option('geodir_show_location_url') != 'all') {
                    /*
                     * @todo i don't see the point in this code so i am removing it. (stiofan)
                     */
                    /*
                    					if($gd_region=='' )
                    					{
                    						if(isset($_SESSION['gd_region']))
                    							$gd_region = $_SESSION['gd_region'];
                    						else
                    							$gd_region =$default_location->region_slug;
                    					}
                    					
                    					if($gd_city=='' )
                    					{
                    						if(isset($_SESSION['gd_city']))
                    							$gd_city = $_SESSION['gd_city'];
                    						else
                    							$gd_city =$default_location->city_slug;
                    						
                    						$base_location_link = geodir_get_location_link('base') ;
                    						wp_redirect($base_location_link . '/' .$gd_country . '/' . $gd_region . '/' . $gd_city )	;
                    						exit();
                    					}*/
                }
            }
            $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '');
            $location_array = geodir_get_location_array($args);
            if (!empty($location_array)) {
                $_SESSION['gd_multi_location'] = 1;
                $_SESSION['gd_country'] = $gd_country;
                $_SESSION['gd_region'] = $gd_region;
                $_SESSION['gd_city'] = $gd_city;
                $wp->query_vars['gd_country'] = $gd_country;
                $wp->query_vars['gd_region'] = $gd_region;
                $wp->query_vars['gd_city'] = $gd_city;
            } else {
                unset($_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
            }
        } else {
            unset($_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
        }
    } else {
        if (isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] != '') {
            if (!is_admin()) {
                $requested_post_type = $wp->query_vars['post_type'];
                // check if this post type is geodirectory post types
                $post_type_array = geodir_get_posttypes();
                if (in_array($requested_post_type, $post_type_array)) {
                    // now u can apply geodirectory related manipulation.
                    //echo "good: it is geodirectory post type<br />" ;
                    //print_r($wp->query_vars) ;
                }
            }
        } else {
            // check if a geodirectory taxonomy is set
            $gd_country = '';
            $gd_region = '';
            $gd_city = '';
            $is_geodir_taxonomy = false;
            $is_geodir_taxonomy_term = false;
            // the last term is real geodirectory taxonomy term or not
            $is_geodir_location_found = false;
            $geodir_taxonomy = '';
            $geodir_post_type = '';
            $geodir_term = '';
            $geodir_set_location_session = true;
            $geodir_taxonomis = geodir_get_taxonomies('', true);
            foreach ($geodir_taxonomis as $taxonomy) {
                if (array_key_exists($taxonomy, $wp->query_vars)) {
                    $is_geodir_taxonomy = true;
                    $geodir_taxonomy = $taxonomy;
                    $geodir_post_type = str_replace('category', '', $taxonomy);
                    $geodir_post_type = str_replace('_tags', '', $geodir_post_type);
                    $geodir_term = $wp->query_vars[$geodir_taxonomy];
                    break;
                }
            }
            // now get an array of all terms seperated by '/'
            $geodir_terms = explode('/', $geodir_term);
            $geodir_last_term = end($geodir_terms);
            if ($is_geodir_taxonomy) {
                $wp->query_vars['post_type'] = $geodir_post_type;
                // now check if last term is a post of geodirectory post types
                $geodir_post = get_posts(array('name' => $geodir_last_term, 'posts_per_page' => 1, 'post_type' => $geodir_post_type));
                if (empty($geodir_post)) {
                    $geodir_post = get_posts(array('name' => $geodir_last_term, 'posts_per_page' => 1, 'post_type' => $geodir_post_type, 'post_status' => 'draft', 'suppress_filters' => false));
                }
                if (!empty($geodir_post)) {
                    if ($geodir_post[0]->post_status != 'publish') {
                        foreach ($wp->query_vars as $key => $vars) {
                            unset($wp->query_vars[$key]);
                        }
                        $wp->query_vars['error'] = '404';
                        // set it as 404 if post exists but its status is not published yet
                    } else {
                        //$wp->query_vars[$geodir_taxonomy] = str_replace( '/'.$geodir_last_term , ''  , $geodir_term);
                        $wp->query_vars[$geodir_post_type] = $geodir_last_term;
                        $wp->query_vars['name'] = $geodir_last_term;
                    }
                    $geodir_term = str_replace('/' . $geodir_last_term, '', $geodir_term, $post_title_replace_count);
                    if (!$post_title_replace_count) {
                        $geodir_term = str_replace($geodir_last_term, '', $geodir_term, $post_title_replace_count);
                    }
                    $geodir_terms = explode('/', $geodir_term);
                    $geodir_last_term = end($geodir_terms);
                    $geodir_set_location_session = false;
                    //return ;
                }
                $geodir_location_terms = '';
                // if last term is not a post then check if last term is a term of the specific texonomy or not
                if (geodir_term_exists($geodir_last_term, $geodir_taxonomy)) {
                    $is_geodir_taxonomy_term = true;
                    $geodir_set_location_session = false;
                }
                // now check if there is location parts in the url or not
                if (get_option('geodir_add_location_url')) {
                    if (get_option('geodir_show_location_url') == 'all') {
                        if (count($geodir_terms) >= 3) {
                            $gd_country = urldecode($geodir_terms[0]);
                            $gd_region = urldecode($geodir_terms[1]);
                            $gd_city = urldecode($geodir_terms[2]);
                        } else {
                            if (count($geodir_terms) >= 2) {
                                $gd_country = urldecode($geodir_terms[0]);
                                $gd_region = urldecode($geodir_terms[1]);
                            } else {
                                if (count($geodir_terms) >= 1) {
                                    $gd_country = urldecode($geodir_terms[0]);
                                }
                            }
                        }
                        $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                        $location_array = geodir_get_location_array($args);
                        if (!empty($location_array)) {
                            $is_geodir_location_found = true;
                        }
                        // if location has not been found for country , region and city then search for country and region only
                        if (!$is_geodir_location_found) {
                            $gd_city = '';
                            $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                            $location_array = geodir_get_location_array($args);
                            if (!empty($location_array)) {
                                $is_geodir_location_found = true;
                            }
                        }
                        // if location has not been found for country , region  then search for country only
                        if (!$is_geodir_location_found) {
                            $gd_city = '';
                            $gd_region = '';
                            $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                            $location_array = geodir_get_location_array($args);
                            if (!empty($location_array)) {
                                $is_geodir_location_found = true;
                            }
                        }
                    } else {
                        $gd_city = urldecode($geodir_terms[0]);
                        $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                        $location_array = geodir_get_location_array($args);
                        if (!empty($location_array)) {
                            $is_geodir_location_found = true;
                        }
                        $args = array('what' => 'region_slug', 'city_val' => $gd_city, 'region_val' => '', 'country_val' => '', 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                        $location_array = geodir_get_location_array($args);
                        if (!empty($location_array)) {
                            $gd_region = $location_array[0]->region_slug;
                        }
                        $args = array('what' => 'country_slug', 'city_val' => $gd_city, 'region_val' => '', 'country_val' => '', 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '', 'format' => array('type' => 'array'));
                        $location_array = geodir_get_location_array($args);
                        if (!empty($location_array)) {
                            $gd_country = $location_array[0]->country_slug;
                        }
                    }
                    // if locaton still not found then clear location related session variables
                    if ($is_geodir_location_found && $geodir_set_location_session) {
                        $_SESSION['gd_multi_location'] = 1;
                        $_SESSION['gd_country'] = $gd_country;
                        $_SESSION['gd_region'] = $gd_region;
                        $_SESSION['gd_city'] = $gd_city;
                    }
                    if (get_option('geodir_show_location_url') != 'all') {
                        $gd_country = '';
                        $gd_region = '';
                    }
                    if ($is_geodir_location_found) {
                        $wp->query_vars['gd_country'] = $gd_country;
                        $wp->query_vars['gd_region'] = $gd_region;
                        $wp->query_vars['gd_city'] = $gd_city;
                    } else {
                        $gd_country = '';
                        $gd_region = '';
                        $gd_city = '';
                    }
                }
                $wp->query_vars[$geodir_taxonomy] = $geodir_term;
                // eliminate location related terms from taxonomy term
                if ($gd_country != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_country) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                if ($gd_region != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_region) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                if ($gd_city != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_city) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                $wp->query_vars[$geodir_taxonomy] = str_replace('///', '', $wp->query_vars[$geodir_taxonomy]);
                $wp->query_vars[$geodir_taxonomy] = str_replace('//', '', $wp->query_vars[$geodir_taxonomy]);
                $wp->query_vars[$geodir_taxonomy] = trim($wp->query_vars[$geodir_taxonomy], '/');
                if ($wp->query_vars[$geodir_taxonomy] == '') {
                    unset($wp->query_vars[$geodir_taxonomy]);
                } else {
                    if (!$is_geodir_taxonomy_term) {
                        foreach ($wp->query_vars as $key => $vars) {
                            unset($wp->query_vars[$key]);
                        }
                        $wp->query_vars['error'] = '404';
                    }
                }
            }
        }
    }
    if (isset($wp->query_vars['gd_is_geodir_page']) && is_array($wp->query_vars) && count($wp->query_vars) == '1') {
        if (!isset($_SESSION['gd_location_filter_on_site_load'])) {
            $_SESSION['gd_location_filter_on_site_load'] = 1;
            if (get_option('geodir_result_by_location') == 'default') {
                $location_default = geodir_get_default_location();
                $_SESSION['gd_multi_location'] = 1;
                $_SESSION['gd_country'] = isset($location_default->country_slug) ? $location_default->country_slug : '';
                $_SESSION['gd_region'] = isset($location_default->region_slug) ? $location_default->region_slug : '';
                $_SESSION['gd_city'] = isset($location_default->city_slug) ? $location_default->city_slug : '';
                $wp->query_vars['gd_country'] = isset($location_default->country_slug) ? $location_default->country_slug : '';
                $wp->query_vars['gd_region'] = isset($location_default->region_slug) ? $location_default->region_slug : '';
                $wp->query_vars['gd_city'] = isset($location_default->city_slug) ? $location_default->city_slug : '';
            }
        }
    } else {
        $_SESSION['gd_location_filter_on_site_load'] = 1;
    }
    if (isset($_SESSION['gd_multi_location']) && $_SESSION['gd_multi_location'] == 1) {
        $wp->query_vars['gd_country'] = $_SESSION['gd_country'];
        $wp->query_vars['gd_region'] = $_SESSION['gd_region'];
        $wp->query_vars['gd_city'] = $_SESSION['gd_city'];
    }
    /**/
    // now check if there is location parts in the url or not
    if (get_option('geodir_add_location_url')) {
        if (get_option('geodir_show_location_url') != 'all') {
            if (isset($wp->query_vars['gd_country'])) {
                $wp->query_vars['gd_country'] = '';
            }
            if (isset($wp->query_vars['gd_region'])) {
                $wp->query_vars['gd_region'] = '';
            }
        }
    } else {
        if (isset($wp->query_vars['gd_country'])) {
            $wp->query_vars['gd_country'] = '';
        }
        if (isset($wp->query_vars['gd_region'])) {
            $wp->query_vars['gd_region'] = '';
        }
        if (isset($wp->query_vars['gd_city'])) {
            $wp->query_vars['gd_city'] = '';
        }
    }
    /**/
    //print_r($_SESSION);
    /*
    echo "<pre>" ;
    	print_r($wp) ;
    	echo "</pre>" ;
    	exit();
    */
}
/**
 * Add the location variables in session.
 *
 * @since 1.0.0
 *
 * @param object $wp The WordPress object.
 */
function geodir_set_location_var_in_session_in_core($wp)
{
    //$wp->geodir_query_vars = $wp->query_vars ;
    // this code will determine when a user wants to switch location
    // Query Vars will have page_id parameter
    // check if query var has page_id and that page id is location page
    geodir_set_is_geodir_page($wp);
    if (!get_option('geodir_set_as_home')) {
        if (empty($wp->query_vars) || !array_diff(array_keys($wp->query_vars), array('preview', 'page', 'paged', 'cpage'))) {
            if ('page' == get_option('show_on_front')) {
                $wp->query_vars['page_id'] = get_option('page_on_front');
            }
        }
    }
    if (isset($wp->query_vars['page_id']) && $wp->query_vars['page_id'] == geodir_location_page_id()) {
        $gd_country = '';
        $gd_region = '';
        $gd_city = '';
        if (isset($wp->query_vars['gd_country']) && $wp->query_vars['gd_country'] != '') {
            $gd_country = urldecode($wp->query_vars['gd_country']);
        }
        if (isset($wp->query_vars['gd_region']) && $wp->query_vars['gd_region'] != '') {
            $gd_region = urldecode($wp->query_vars['gd_region']);
        }
        if (isset($wp->query_vars['gd_city']) && $wp->query_vars['gd_city'] != '') {
            $gd_city = urldecode($wp->query_vars['gd_city']);
        }
        if (!($gd_country == '' && $gd_region == '' && $gd_city == '')) {
            $default_location = geodir_get_default_location();
            if (get_option('geodir_add_location_url')) {
                if (get_option('geodir_show_location_url') != 'all') {
                    if ($gd_region == '') {
                        if (isset($_SESSION['gd_region'])) {
                            $gd_region = $_SESSION['gd_region'];
                        } else {
                            $gd_region = $default_location->region_slug;
                        }
                    }
                    if ($gd_city == '') {
                        if (isset($_SESSION['gd_city'])) {
                            $gd_city = $_SESSION['gd_city'];
                        } else {
                            $gd_city = $default_location->city_slug;
                        }
                        $base_location_link = geodir_get_location_link('base');
                        wp_redirect($base_location_link . '/' . $gd_country . '/' . $gd_region . '/' . $gd_city);
                        exit;
                    }
                }
            }
            $args = array('what' => 'city', 'city_val' => $gd_city, 'region_val' => $gd_region, 'country_val' => $gd_country, 'country_column_name' => 'country_slug', 'region_column_name' => 'region_slug', 'city_column_name' => 'city_slug', 'location_link_part' => false, 'compare_operator' => '');
            $location_array = function_exists('geodir_get_location_array') ? geodir_get_location_array($args) : array();
            if (!empty($location_array)) {
                $_SESSION['gd_multi_location'] = 1;
                $_SESSION['gd_country'] = $gd_country;
                $_SESSION['gd_region'] = $gd_region;
                $_SESSION['gd_city'] = $gd_city;
                $wp->query_vars['gd_country'] = $gd_country;
                $wp->query_vars['gd_region'] = $gd_region;
                $wp->query_vars['gd_city'] = $gd_city;
            } else {
                unset($_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
            }
        } else {
            unset($_SESSION['gd_multi_location'], $_SESSION['gd_city'], $_SESSION['gd_region'], $_SESSION['gd_country']);
        }
    } else {
        if (isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] != '') {
            if (!is_admin()) {
                $requested_post_type = $wp->query_vars['post_type'];
                // check if this post type is geodirectory post types
                $post_type_array = geodir_get_posttypes();
                if (in_array($requested_post_type, $post_type_array)) {
                    // now u can apply geodirectory related manipulation.
                }
            }
        } else {
            // check if a geodirectory taxonomy is set
            $gd_country = '';
            $gd_region = '';
            $gd_city = '';
            $is_geodir_taxonomy = false;
            $is_geodir_taxonomy_term = false;
            // the last term is real geodirectory taxonomy term or not
            $is_geodir_location_found = false;
            $geodir_taxonomy = '';
            $geodir_post_type = '';
            $geodir_term = '';
            $geodir_set_location_session = true;
            $geodir_taxonomis = geodir_get_taxonomies('', true);
            foreach ($geodir_taxonomis as $taxonomy) {
                if (array_key_exists($taxonomy, $wp->query_vars)) {
                    $is_geodir_taxonomy = true;
                    $geodir_taxonomy = $taxonomy;
                    $geodir_post_type = str_replace('category', '', $taxonomy);
                    $geodir_post_type = str_replace('_tags', '', $geodir_post_type);
                    $geodir_term = $wp->query_vars[$geodir_taxonomy];
                    break;
                }
            }
            // now get an array of all terms seperated by '/'
            $geodir_terms = explode('/', $geodir_term);
            $geodir_last_term = end($geodir_terms);
            if ($is_geodir_taxonomy) {
                $wp->query_vars['post_type'] = $geodir_post_type;
                // now check if last term is a post of geodirectory post types
                $geodir_post = get_posts(array('name' => $geodir_last_term, 'posts_per_page' => 1, 'post_type' => $geodir_post_type));
                if (empty($geodir_post)) {
                    $geodir_post = get_posts(array('name' => $geodir_last_term, 'posts_per_page' => 1, 'post_type' => $geodir_post_type, 'post_status' => 'draft', 'suppress_filters' => false));
                }
                if (!empty($geodir_post)) {
                    if ($geodir_post[0]->post_status != 'publish') {
                        foreach ($wp->query_vars as $key => $vars) {
                            unset($wp->query_vars[$key]);
                        }
                        $wp->query_vars['error'] = '404';
                        // set it as 404 if post exists but its status is not published yet
                    } else {
                        //$wp->query_vars[$geodir_taxonomy] = str_replace( '/'.$geodir_last_term , ''  , $geodir_term);
                        $wp->query_vars[$geodir_post_type] = $geodir_last_term;
                        $wp->query_vars['name'] = $geodir_last_term;
                    }
                    $geodir_term = str_replace('/' . $geodir_last_term, '', $geodir_term, $post_title_replace_count);
                    if (!$post_title_replace_count) {
                        $geodir_term = str_replace($geodir_last_term, '', $geodir_term, $post_title_replace_count);
                    }
                    $geodir_terms = explode('/', $geodir_term);
                    $geodir_last_term = end($geodir_terms);
                    $geodir_set_location_session = false;
                    //return ;
                }
                $geodir_location_terms = '';
                // if last term is not a post then check if last term is a term of the specific texonomy or not
                if (geodir_term_exists($geodir_last_term, $geodir_taxonomy)) {
                    $is_geodir_taxonomy_term = true;
                    $geodir_set_location_session = false;
                }
                // now check if there is location parts in the url or not
                if (get_option('geodir_add_location_url')) {
                    $default_location = geodir_get_default_location();
                    if (get_option('geodir_show_location_url') == 'all') {
                        if (count($geodir_terms) >= 3) {
                            $gd_country = urldecode($geodir_terms[0]);
                            $gd_region = urldecode($geodir_terms[1]);
                            $gd_city = urldecode($geodir_terms[2]);
                        } else {
                            if (count($geodir_terms) >= 2) {
                                $gd_country = urldecode($geodir_terms[0]);
                                $gd_region = urldecode($geodir_terms[1]);
                            } else {
                                if (count($geodir_terms) >= 1) {
                                    $gd_country = urldecode($geodir_terms[0]);
                                }
                            }
                        }
                        if (strtolower($default_location->country_slug) == strtolower($gd_country) && strtolower($default_location->region_slug) == strtolower($gd_region) && strtolower($default_location->city_slug) == strtolower($gd_city)) {
                            $is_geodir_location_found = true;
                        }
                        // if location has not been found for country , region and city then search for country and region only
                        if (!$is_geodir_location_found) {
                            $gd_city = '';
                            if (strtolower($default_location->country_slug) == strtolower($gd_country) && strtolower($default_location->region_slug) == strtolower($gd_region)) {
                                $is_geodir_location_found = true;
                            }
                        }
                        // if location has not been found for country , region  then search for country only
                        if (!$is_geodir_location_found) {
                            $gd_city = '';
                            $gd_region = '';
                            if (strtolower($default_location->country_slug) == strtolower($gd_country)) {
                                $is_geodir_location_found = true;
                            }
                        }
                    } else {
                        $gd_city = $geodir_terms[0];
                        if (strtolower($default_location->city_slug) == strtolower($gd_city)) {
                            $is_geodir_location_found = true;
                            $gd_region = $default_location->region_slug;
                            $gd_country = $default_location->country_slug;
                        }
                    }
                    // if locaton still not found then clear location related session variables
                    if ($is_geodir_location_found && $geodir_set_location_session) {
                        $_SESSION['gd_multi_location'] = 1;
                        $_SESSION['gd_country'] = $gd_country;
                        $_SESSION['gd_region'] = $gd_region;
                        $_SESSION['gd_city'] = $gd_city;
                    }
                    if (get_option('geodir_show_location_url') != 'all') {
                        $gd_country = '';
                        $gd_region = '';
                    }
                    if ($is_geodir_location_found) {
                        $wp->query_vars['gd_country'] = $gd_country;
                        $wp->query_vars['gd_region'] = $gd_region;
                        $wp->query_vars['gd_city'] = $gd_city;
                    } else {
                        $gd_country = '';
                        $gd_region = '';
                        $gd_city = '';
                    }
                }
                $wp->query_vars[$geodir_taxonomy] = $geodir_term;
                // eliminate location related terms from taxonomy term
                if ($gd_country != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_country) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                if ($gd_region != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_region) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                if ($gd_city != '') {
                    $wp->query_vars[$geodir_taxonomy] = preg_replace('/' . urlencode($gd_city) . '/', '', $wp->query_vars[$geodir_taxonomy], 1);
                }
                $wp->query_vars[$geodir_taxonomy] = str_replace('///', '', $wp->query_vars[$geodir_taxonomy]);
                $wp->query_vars[$geodir_taxonomy] = str_replace('//', '', $wp->query_vars[$geodir_taxonomy]);
                $wp->query_vars[$geodir_taxonomy] = trim($wp->query_vars[$geodir_taxonomy], '/');
                if ($wp->query_vars[$geodir_taxonomy] == '') {
                    unset($wp->query_vars[$geodir_taxonomy]);
                } else {
                    if (!$is_geodir_taxonomy_term) {
                        foreach ($wp->query_vars as $key => $vars) {
                            unset($wp->query_vars[$key]);
                        }
                        $wp->query_vars['error'] = '404';
                    }
                }
            }
        }
    }
    if (isset($_SESSION['gd_multi_location']) && $_SESSION['gd_multi_location'] == 1) {
        $wp->query_vars['gd_country'] = $_SESSION['gd_country'];
        $wp->query_vars['gd_region'] = $_SESSION['gd_region'];
        $wp->query_vars['gd_city'] = $_SESSION['gd_city'];
    }
    // now check if there is location parts in the url or not
    if (get_option('geodir_add_location_url')) {
        if (get_option('geodir_show_location_url') != 'all') {
            if (isset($wp->query_vars['gd_country'])) {
                $wp->query_vars['gd_country'] = '';
            }
            if (isset($wp->query_vars['gd_region'])) {
                $wp->query_vars['gd_region'] = '';
            }
        }
    } else {
        if (isset($wp->query_vars['gd_country'])) {
            $wp->query_vars['gd_country'] = '';
        }
        if (isset($wp->query_vars['gd_region'])) {
            $wp->query_vars['gd_region'] = '';
        }
        if (isset($wp->query_vars['gd_city'])) {
            $wp->query_vars['gd_city'] = '';
        }
    }
}