/**
 * Returns the term link with parameters.
 *
 * @since 1.0.0
 * @since 1.5.7 Changes for the neighbourhood system improvement.
 * @package GeoDirectory
 * @param string $termlink The term link
 * @param object $term Not yet implemented.
 * @param string $taxonomy The taxonomy name.
 * @return string The term link.
 */
function geodir_term_link($termlink, $term, $taxonomy)
{
    $geodir_taxonomies = geodir_get_taxonomies('', true);
    if (isset($taxonomy) && !empty($geodir_taxonomies) && in_array($taxonomy, $geodir_taxonomies)) {
        global $geodir_add_location_url;
        $include_location = false;
        $request_term = array();
        $listing_slug = geodir_get_listing_slug($taxonomy);
        if ($geodir_add_location_url != NULL && $geodir_add_location_url != '') {
            if ($geodir_add_location_url && get_option('geodir_add_location_url')) {
                $include_location = true;
            }
        } elseif (get_option('geodir_add_location_url') && isset($_SESSION['gd_multi_location']) && $_SESSION['gd_multi_location'] == 1) {
            $include_location = true;
        }
        if ($include_location) {
            global $post;
            $location_manager = defined('POST_LOCATION_TABLE') ? true : false;
            $neighbourhood_active = $location_manager && get_option('location_neighbourhoods') ? true : false;
            if (geodir_is_page('detail') && isset($post->country_slug)) {
                $location_terms = array('gd_country' => $post->country_slug, 'gd_region' => $post->region_slug, 'gd_city' => $post->city_slug);
                if ($neighbourhood_active && !empty($location_terms['gd_city']) && !empty($_SESSION['gd_neighbourhood'])) {
                    $location_terms['gd_neighbourhood'] = $_SESSION['gd_neighbourhood'];
                }
            } else {
                $location_terms = geodir_get_current_location_terms('query_vars');
            }
            $geodir_show_location_url = get_option('geodir_show_location_url');
            $location_terms = geodir_remove_location_terms($location_terms);
            if (!empty($location_terms)) {
                $url_separator = '';
                //get_option('geodir_listingurl_separator');
                if (get_option('permalink_structure') != '') {
                    $old_listing_slug = '/' . $listing_slug . '/';
                    $request_term = implode("/", $location_terms);
                    $new_listing_slug = '/' . $listing_slug . '/' . $request_term . '/';
                    $termlink = substr_replace($termlink, $new_listing_slug, strpos($termlink, $old_listing_slug), strlen($old_listing_slug));
                } else {
                    $termlink = geodir_getlink($termlink, $request_term);
                }
            }
        }
        // Alter the CPT slug is WPML is set to do so
        /* we can replace this with the below function
           if(function_exists('icl_object_id')){
               global $sitepress;
               $post_type = str_replace("category","",$taxonomy);
               $termlink = $sitepress->post_type_archive_link_filter( $termlink, $post_type);
           }*/
        // Alter the CPT slug if WPML is set to do so
        if (function_exists('icl_object_id')) {
            $post_types = get_option('geodir_post_types');
            $post_type = str_replace("category", "", $taxonomy);
            $post_type = str_replace("_tags", "", $post_type);
            $slug = $post_types[$post_type]['rewrite']['slug'];
            if (gd_wpml_slug_translation_turned_on($post_type)) {
                global $sitepress;
                $default_lang = $sitepress->get_default_language();
                $language_code = gd_wpml_get_lang_from_url($termlink);
                if (!$language_code) {
                    $language_code = $default_lang;
                }
                $org_slug = $slug;
                $slug = apply_filters('wpml_translate_single_string', $slug, 'WordPress', 'URL slug: ' . $slug, $language_code);
                if (!$slug) {
                    $slug = $org_slug;
                }
                $termlink = trailingslashit(preg_replace("/" . preg_quote($org_slug, "/") . "/", $slug, $termlink, 1));
            }
        }
    }
    return $termlink;
}
/**
 * Returns permalink structure using post link.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global object $wp_query WordPress Query object.
 * @global object $post WordPress Post object.
 * @param string $post_link The post link.
 * @param object $post_obj The post object.
 * @param string $leavename Not yet implemented.
 * @param bool $sample Is this a sample post?.
 * @return string The post link.
 */
function geodir_listing_permalink_structure($post_link, $post_obj, $leavename, $sample)
{
    //echo $post_link."<br />".$sample ;
    global $wpdb, $wp_query, $plugin_prefix, $post, $comment_post_cache, $gd_permalink_cache;
    if (isset($post_obj->ID) && isset($post->ID) && $post_obj->ID == $post->ID) {
    } elseif (isset($post_obj->post_status) && $post_obj->post_status == 'auto-draft') {
        return $post_link;
    } else {
        $orig_post = $post;
        $post = $post_obj;
    }
    if (in_array($post->post_type, geodir_get_posttypes())) {
        $post_types = get_option('geodir_post_types');
        $slug = $post_types[$post->post_type]['rewrite']['slug'];
        // Alter the CPT slug if WPML is set to do so
        if (function_exists('icl_object_id')) {
            if (gd_wpml_slug_translation_turned_on($post->post_type) && ($language_code = gd_wpml_get_lang_from_url($post_link))) {
                $slug = apply_filters('wpml_translate_single_string', $slug, 'WordPress', 'URL slug: ' . $slug, $language_code);
            }
        }
        // Fix slug problem when slug matches part of host or base url/ Ex: url -> www.abcxyz.com & slug -> xyz.
        $site_url = trailingslashit(get_home_url());
        $fix_url = strpos($post_link, $site_url) == 0 ? true : false;
        if ($fix_url) {
            $post_link = str_replace($site_url, '', $post_link);
        }
        $post_link = trailingslashit(preg_replace("/" . preg_quote($slug, "/") . "/", $slug . "/%gd_taxonomy%", $post_link, 1));
        if ($fix_url) {
            $post_link = $site_url . $post_link;
        }
        if (isset($comment_post_cache[$post->ID])) {
            $post = $comment_post_cache[$post->ID];
        }
        if (isset($gd_permalink_cache[$post->ID]) && $gd_permalink_cache[$post->ID] && !$sample) {
            $post_id = $post->ID;
            if (isset($orig_post)) {
                $post = $orig_post;
            }
            return $gd_permalink_cache[$post_id];
        }
        if (!isset($post->post_locations)) {
            $post_type = $post->post_type;
            $ID = $post->ID;
            $post2 = $wpdb->get_row($wpdb->prepare("SELECT * from " . $plugin_prefix . $post->post_type . "_detail WHERE post_id = %d ", array($post->ID)));
            $post = (object) array_merge((array) $post, (array) $post2);
            $comment_post_cache[$post->ID] = $post;
        }
        if (false !== strpos($post_link, '%gd_taxonomy%')) {
            if (get_option('geodir_add_location_url')) {
                $location_request = '';
                if (!empty($post->post_locations)) {
                    $geodir_arr_locations = explode(',', $post->post_locations);
                    if (count($geodir_arr_locations) == 3) {
                        $post->city_slug = str_replace('[', '', $geodir_arr_locations[0]);
                        $post->city_slug = str_replace(']', '', $post->city_slug);
                        $post->region_slug = str_replace('[', '', $geodir_arr_locations[1]);
                        $post->region_slug = str_replace(']', '', $post->region_slug);
                        $post->country_slug = str_replace('[', '', $geodir_arr_locations[2]);
                        $post->country_slug = str_replace(']', '', $post->country_slug);
                        $post_location = (object) array('country_slug' => $post->country_slug, 'region_slug' => $post->region_slug, 'city_slug' => $post->city_slug);
                    } else {
                        $post_location = geodir_get_location();
                    }
                } else {
                    $post_location_sql = $wpdb->get_results($wpdb->prepare("SELECT post_locations from " . $plugin_prefix . $post->post_type . "_detail WHERE post_id = %d ", array($post->ID)));
                    if (!empty($post_location_sql) && is_array($post_location_sql) && !empty($post_location_sql[0]->post_locations)) {
                        $geodir_arr_locations = explode(',', $post_location_sql[0]->post_locations);
                        if (count($geodir_arr_locations) == 3) {
                            $post->city_slug = str_replace('[', '', $geodir_arr_locations[0]);
                            $post->city_slug = str_replace(']', '', $post->city_slug);
                            $post->region_slug = str_replace('[', '', $geodir_arr_locations[1]);
                            $post->region_slug = str_replace(']', '', $post->region_slug);
                            $post->country_slug = str_replace('[', '', $geodir_arr_locations[2]);
                            $post->country_slug = str_replace(']', '', $post->country_slug);
                            $post_location = (object) array('country_slug' => $post->country_slug, 'region_slug' => $post->region_slug, 'city_slug' => $post->city_slug);
                        }
                    } else {
                        $post_location = geodir_get_location();
                    }
                }
                if (!empty($post_location)) {
                    if (get_option('geodir_show_location_url') == 'all') {
                        $location_request .= $post_location->country_slug . '/';
                        $location_request .= $post_location->region_slug . '/';
                        $location_request .= $post_location->city_slug . '/';
                    } else {
                        $location_request .= $post_location->city_slug . '/';
                    }
                }
            }
            if (get_option('geodir_add_categories_url')) {
                $term_request = '';
                $taxonomies = geodir_get_taxonomies($post->post_type);
                $taxonomies = end($taxonomies);
                if (!empty($post->default_category)) {
                    $post_terms = $post->default_category;
                } else {
                    $post_terms = '';
                    if (isset($post->{$taxonomies})) {
                        $post_terms = explode(",", trim($post->{$taxonomies}, ","));
                        $post_terms = $post_terms[0];
                    }
                    if (!$post_terms) {
                        $post_terms = geodir_get_post_meta($post->ID, 'default_category', true);
                    }
                    if (!$post_terms) {
                        $post_terms = geodir_get_post_meta($post->ID, $taxonomies, true);
                        if ($post_terms) {
                            $post_terms = explode(",", trim($post_terms, ","));
                            $post_terms = $post_terms[0];
                        }
                    }
                }
                $term = get_term_by('id', $post_terms, $taxonomies);
                if (!empty($term)) {
                    $term_request = $term->slug;
                }
                //$term_request = $term->slug.'/';
            }
            $request_term = '';
            $listingurl_separator = '';
            //$detailurl_separator = get_option('geodir_detailurl_separator');
            $detailurl_separator = '';
            if (isset($location_request) && $location_request != '' && isset($term_request) && $term_request != '') {
                $request_term = $location_request;
                //$listingurl_separator = get_option('geodir_listingurl_separator');
                //$request_term .= $listingurl_separator.'/'.$term_request;
                $request_term .= $term_request;
            } else {
                if (isset($location_request) && $location_request != '') {
                    $request_term = $location_request;
                }
                if (isset($term_request) && $term_request != '') {
                    $request_term .= $term_request;
                }
            }
            $request_term = trim($request_term, '/');
            if (!empty($request_term)) {
                $post_link = str_replace('%gd_taxonomy%', $request_term . $detailurl_separator, $post_link);
            } else {
                $post_link = str_replace('/%gd_taxonomy%', $request_term . $detailurl_separator, $post_link);
            }
            //echo $post_link ;
        }
        // temp cache the permalink
        if (!$sample && (!isset($_REQUEST['geodir_ajax']) || isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] != 'add_listing')) {
            $gd_permalink_cache[$post->ID] = $post_link;
        }
    }
    if (isset($orig_post)) {
        $post = $orig_post;
    }
    //echo $post_link ;
    return $post_link;
}