/**
 * Set parent categories to fix categoires tree structure.
 *
 * @since 1.5.7
 * @package GeoDirectory
 *
 * @param array $request_info Listing request info.
 * @return array Modified listing request info.
 */
function geodir_attach_parent_categories($request_info)
{
    if (!empty($request_info['post_category']) && is_array($request_info['post_category'])) {
        foreach ($request_info['post_category'] as $taxomony => $term_ids) {
            $attach_term_ids = array();
            if (!empty($term_ids) && is_array($term_ids) && taxonomy_exists($taxomony) && strpos($taxomony, 'category') !== false) {
                $attach_term_ids = geodir_add_parent_terms($term_ids, $taxomony);
                if (!empty($attach_term_ids)) {
                    if (!isset($request_info['post_default_category'])) {
                        $request_info['post_default_category'] = $attach_term_ids[0];
                    }
                    $request_info['post_category'][$taxomony] = $attach_term_ids;
                }
            }
        }
    }
    return $request_info;
}
/**
 * Output link to the posts categories and tags.
 *
 * @global bool $preview True of on a preview page. False if not.
 * @global object $post The current post object.
 * @since 1.0.0
 * @since 1.5.7 Modified to add parent categories if only sub category selected.
 * @package GeoDirectory
 */
function geodir_action_details_taxonomies()
{
    global $preview, $post;
    ?>
    <p class="geodir_post_taxomomies clearfix">
    <?php 
    $taxonomies = array();
    $is_backend_preview = is_single() && !empty($_REQUEST['post_type']) && !empty($_REQUEST['preview']) && !empty($_REQUEST['p']) && is_super_admin() ? true : false;
    // skip if preview from backend
    if ($preview && !$is_backend_preview) {
        $post_type = $post->listing_type;
        $post_taxonomy = $post_type . 'category';
        $post->{$post_taxonomy} = $post->post_category[$post_taxonomy];
    } else {
        $post_type = $post->post_type;
        $post_taxonomy = $post_type . 'category';
    }
    //{
    $post_type_info = get_post_type_object($post_type);
    $listing_label = $post_type_info->labels->singular_name;
    if (!empty($post->post_tags)) {
        if (taxonomy_exists($post_type . '_tags')) {
            $links = array();
            $terms = array();
            // to limit post tags
            $post_tags = trim($post->post_tags, ",");
            $post_id = isset($post->ID) ? $post->ID : '';
            /**
             * Filter the post tags.
             *
             * Allows you to filter the post tags output on the details page of a post.
             *
             * @since 1.0.0
             * @param string $post_tags A comma seperated list of tags.
             * @param int $post_id The current post id.
             */
            $post_tags = apply_filters('geodir_action_details_post_tags', $post_tags, $post_id);
            $post->post_tags = $post_tags;
            $post_tags = explode(",", trim($post->post_tags, ","));
            foreach ($post_tags as $post_term) {
                // fix slug creation order for tags & location
                $post_term = trim($post_term);
                $priority_location = false;
                if ($insert_term = term_exists($post_term, $post_type . '_tags')) {
                    $term = get_term_by('name', $post_term, $post_type . '_tags');
                } else {
                    $post_country = isset($_REQUEST['post_country']) && $_REQUEST['post_country'] != '' ? $_REQUEST['post_country'] : NULL;
                    $post_region = isset($_REQUEST['post_region']) && $_REQUEST['post_region'] != '' ? $_REQUEST['post_region'] : NULL;
                    $post_city = isset($_REQUEST['post_city']) && $_REQUEST['post_city'] != '' ? $_REQUEST['post_city'] : NULL;
                    $match_country = $post_country && sanitize_title($post_term) == sanitize_title($post_country) ? true : false;
                    $match_region = $post_region && sanitize_title($post_term) == sanitize_title($post_region) ? true : false;
                    $match_city = $post_city && sanitize_title($post_term) == sanitize_title($post_city) ? true : false;
                    if ($match_country || $match_region || $match_city) {
                        $priority_location = true;
                        $term = get_term_by('name', $post_term, $post_type . '_tags');
                    } else {
                        $insert_term = wp_insert_term($post_term, $post_type . '_tags');
                        $term = get_term_by('name', $post_term, $post_type . '_tags');
                    }
                }
                if (!is_wp_error($term) && is_object($term)) {
                    // fix tag link on detail page
                    if ($priority_location) {
                        $tag_link = "<a href=''>{$post_term}</a>";
                        /**
                         * Filter the tag name on the details page.
                         *
                         * @since 1.5.6
                         * @param string $tag_link The tag link html.
                         * @param object $term The tag term object.
                         */
                        $tag_link = apply_filters('geodir_details_taxonomies_tag_link', $tag_link, $term);
                        $links[] = $tag_link;
                    } else {
                        $tag_link = "<a href='" . esc_attr(get_term_link($term->term_id, $term->taxonomy)) . "'>{$term->name}</a>";
                        /** This action is documented in geodirectory-template_actions.php */
                        $tag_link = apply_filters('geodir_details_taxonomies_tag_link', $tag_link, $term);
                        $links[] = $tag_link;
                    }
                    $terms[] = $term;
                }
                //
            }
            if (!isset($listing_label)) {
                $listing_label = '';
            }
            $taxonomies[$post_type . '_tags'] = wp_sprintf('%s: %l', geodir_ucwords($listing_label . ' ' . __('Tags', 'geodirectory')), $links, (object) $terms);
        }
    }
    if (!empty($post->{$post_taxonomy})) {
        $links = array();
        $terms = array();
        $termsOrdered = array();
        if (!is_array($post->{$post_taxonomy})) {
            $post_term = explode(",", trim($post->{$post_taxonomy}, ","));
        } else {
            $post_term = $post->{$post_taxonomy};
            if ($preview && !$is_backend_preview) {
                $post_term = geodir_add_parent_terms($post_term, $post_taxonomy);
            }
        }
        $post_term = array_unique($post_term);
        if (!empty($post_term)) {
            foreach ($post_term as $post_term) {
                $post_term = trim($post_term);
                if ($post_term != '') {
                    $term = get_term_by('id', $post_term, $post_taxonomy);
                    if (is_object($term)) {
                        $term_link = "<a href='" . esc_attr(get_term_link($term, $post_taxonomy)) . "'>{$term->name}</a>";
                        /**
                         * Filter the category name on the details page.
                         *
                         * @since 1.5.6
                         * @param string $term_link The link html to the category.
                         * @param object $term The category term object.
                         */
                        $term_link = apply_filters('geodir_details_taxonomies_cat_link', $term_link, $term);
                        $links[] = $term_link;
                        $terms[] = $term;
                    }
                }
            }
            // order alphabetically
            asort($links);
            foreach (array_keys($links) as $key) {
                $termsOrdered[$key] = $terms[$key];
            }
            $terms = $termsOrdered;
        }
        if (!isset($listing_label)) {
            $listing_label = '';
        }
        $taxonomies[$post_taxonomy] = wp_sprintf('%s: %l', geodir_ucwords($listing_label . ' ' . __('Category', 'geodirectory')), $links, (object) $terms);
    }
    if (isset($taxonomies[$post_taxonomy])) {
        echo '<span class="geodir-category">' . $taxonomies[$post_taxonomy] . '</span>';
    }
    if (isset($taxonomies[$post_type . '_tags'])) {
        echo '<span class="geodir-tags">' . $taxonomies[$post_type . '_tags'] . '</span>';
    }
    ?>
    </p><?php 
}