/**
 * Adds meta boxes to the GD post types.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $post WordPress Post object.
 */
function geodir_meta_box_add()
{
    global $post;
    $geodir_post_types = geodir_get_posttypes('array');
    $geodir_posttypes = array_keys($geodir_post_types);
    if (isset($post->post_type) && in_array($post->post_type, $geodir_posttypes)) {
        $geodir_posttype = $post->post_type;
        $post_typename = geodir_ucwords($geodir_post_types[$geodir_posttype]['labels']['singular_name']);
        // Filter-Payment-Manager
        add_meta_box('geodir_post_images', $post_typename . ' ' . __('Attachments', 'geodirectory'), 'geodir_post_attachments', $geodir_posttype, 'side');
        add_meta_box('geodir_post_info', $post_typename . ' ' . __('Information', 'geodirectory'), 'geodir_post_info_setting', $geodir_posttype, 'normal', 'high');
        // no need of this box as all fields moved to main informain box
        //add_meta_box( 'geodir_post_addinfo', $post_typename. ' ' .__('Additional Information' , 'geodirectory'), 'geodir_post_addinfo_setting', $geodir_posttype,'normal', 'high' );
    }
}
/**
 * Category Selection Interface in add/edit listing form.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @param string $request_taxonomy The taxonomy name.
 * @param int $parrent The parent term ID.
 * @param bool|string $selected The selected value.
 * @param bool $main_selected Not yet implemented.
 * @param bool $default Is this the default category? Default: false.
 * @param string $exclude Excluded terms list. Serialized base64 encoded string.
 */
function geodir_addpost_categories_html($request_taxonomy, $parrent, $selected = false, $main_selected = true, $default = false, $exclude = '')
{
    global $exclude_cats;
    if ($exclude != '') {
        $exclude_cats = unserialize(base64_decode($exclude));
    }
    if (is_array($exclude_cats) && !empty($exclude_cats) && !in_array($parrent, $exclude_cats) || (!is_array($exclude_cats) || empty($exclude_cats))) {
        ?>

        <?php 
        $main_cat = get_term($parrent, $request_taxonomy);
        ?>

        <div class="post_catlist_item" style="border:1px solid #CCCCCC; margin:5px auto; padding:5px;">
            <img alt="move icon" src="<?php 
        echo geodir_plugin_url() . '/geodirectory-assets/images/move.png';
        ?>
"
                 onclick="jQuery(this).closest('div').remove();update_listing_cat(this);" align="right"/>
            <?php 
        /* ?>
        		<img src="<?php echo geodir_plugin_url().'/geodirectory-assets/images/move.png';?>" onclick="jQuery(this).closest('div').remove();show_subcatlist();" align="right" /> 
        		<?php */
        ?>

            <input type="checkbox" value="<?php 
        echo $main_cat->term_id;
        ?>
" class="listing_main_cat"
                   onchange="if(jQuery(this).is(':checked')){jQuery(this).closest('div').find('.post_default_category').prop('checked',false).show();}else{jQuery(this).closest('div').find('.post_default_category').prop('checked',false).hide();};update_listing_cat()"
                   checked="checked" disabled="disabled"/>
       <span> 
        <?php 
        printf(__('Add listing in %s category', 'geodirectory'), geodir_ucwords($main_cat->name));
        ?>
        </span>
            <br/>

            <div class="post_default_category">
                <input type="radio" name="post_default_category" value="<?php 
        echo $main_cat->term_id;
        ?>
"
                       onchange="update_listing_cat()" <?php 
        if ($default) {
            echo ' checked="checked" ';
        }
        ?>
   />
        <span> 
        <?php 
        printf(__('Set %s as default category', 'geodirectory'), geodir_ucwords($main_cat->name));
        ?>
        </span>
            </div>

            <br/>
            <?php 
        $cat_terms = get_terms($request_taxonomy, array('parent' => $main_cat->term_id, 'hide_empty' => false, 'exclude' => $exclude_cats));
        if (!empty($cat_terms)) {
            ?>
                <span> <?php 
            printf(__('Add listing in category', 'geodirectory'));
            ?>
</span>
                <?php 
            geodir_get_catlist($request_taxonomy, $parrent, $selected);
            ?>
            <?php 
        }
        ?>
        </div>

    <?php 
    }
}
/**
 * Adds the filetrs and gets the query.
 *
 * @since 1.0.0
 *
 * @global object $wp_query WordPress Query object.
 * @todo $wp_query declared twice - fix it.
 * @global string $geodir_post_type Post type.
 * @global string $table Listing table name.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global string $term Term object.
 *
 * @param string $query Database query.
 * @return string Query.
 */
function gdsc_listing_loop_filter($query)
{
    global $wp_query, $geodir_post_type, $table, $plugin_prefix, $term;
    $geodir_post_type = geodir_get_current_posttype();
    if (isset($wp_query->tax_query->queries) && $wp_query->tax_query->queries) {
        $taxonomies = wp_list_pluck($wp_query->tax_query->queries, 'taxonomy');
        if (isset($wp_query->query[$taxonomies[0]])) {
            $request_term = explode("/", $wp_query->query[$taxonomies[0]]);
            $request_term = end($request_term);
            if (!term_exists($request_term)) {
                $args = array('number' => '1');
                $terms_arr = get_terms($taxonomies[0], $args);
                foreach ($terms_arr as $location_term) {
                    $term_arr = $location_term;
                    $term_arr->name = geodir_ucwords(str_replace('-', ' ', $request_term));
                }
                $wp_query->queried_object_id = 1;
                $wp_query->queried_object = $term_arr;
            }
        }
    }
    if (isset($query->query_vars['is_geodir_loop']) && $query->query_vars['is_geodir_loop']) {
        $table = $plugin_prefix . $geodir_post_type . '_detail';
        add_filter('posts_fields', 'geodir_posts_fields', 1);
        add_filter('posts_join', 'geodir_posts_join', 1);
        geodir_post_where();
        if (!is_admin()) {
            add_filter('posts_orderby', 'geodir_posts_orderby', 1);
        }
        // advanced filter for popular post view widget
        global $wp_query;
        if (!is_admin()) {
            if (!empty($wp_query->query['with_pics_only'])) {
                add_filter('posts_join', 'geodir_filter_widget_join', 1000);
            }
            add_filter('posts_where', 'geodir_filter_widget_where', 1000);
        }
    }
    return $query;
}
function geodir_filter_title_variables($title, $gd_page, $sep = '')
{
    if (!$gd_page || !$title) {
        return $title;
    }
    // if no a GD page then bail.
    global $post;
    //print_r($post);
    /*
        %%date%%	                Replaced with the date of the post/page
        %%title%%	                Replaced with the title of the post/page
        %%sitename%%	            The site's name
        %%sitedesc%%	            The site's tagline / description
        %%excerpt%%	                Replaced with the post/page excerpt (or auto-generated if it does not exist)
        %%tag%%	                    Replaced with the current tag/tags
        %%category%%	            Replaced with the post categories (comma separated)
        %%category_description%%	Replaced with the category description
        %%tag_description%%	        Replaced with the tag description
        %%term_description%%	    Replaced with the term description
        %%term_title%%	            Replaced with the term name
        %%searchphrase%%	        Replaced with the current search phrase
        %%sep%%	                    The separator defined in your theme's wp_title() tag.
    
        ADVANCED
        %%pt_single%%	            Replaced with the post type single label
        %%pt_plural%%	            Replaced with the post type plural label
        %%modified%%	            Replaced with the post/page modified time
        %%id%%	                    Replaced with the post/page ID
        %%name%%	                Replaced with the post/page author's 'nicename'
        %%userid%%	                Replaced with the post/page author's userid
        %%page%%	                Replaced with the current page number (i.e. page 2 of 4)
        %%pagetotal%%	            Replaced with the current page total
        %%pagenumber%%	            Replaced with the current page number
    */
    if ($sep == '') {
        /**
         * Filter the page title separator.
         *
         * @since 1.0.0
         * @package GeoDirectory
         * @param string $sep The separator, default: `|`.
         */
        $sep = apply_filters('geodir_page_title_separator', '|');
    }
    if (strpos($title, '%%title%%') !== false) {
        $title = str_replace("%%title%%", $post->post_title, $title);
    }
    if (strpos($title, '%%sitename%%') !== false) {
        $title = str_replace("%%sitename%%", get_bloginfo('name'), $title);
    }
    if (strpos($title, '%%sitedesc%%') !== false) {
        $title = str_replace("%%sitedesc%%", get_bloginfo('description'), $title);
    }
    if (strpos($title, '%%excerpt%%') !== false) {
        $title = str_replace("%%excerpt%%", strip_tags(get_the_excerpt()), $title);
    }
    if (strpos($title, '%%pt_single%%') !== false) {
        $single_name = '';
        if ($gd_page == 'search' || $gd_page == 'author') {
            $geodir_post_types = get_option('geodir_post_types');
            $spt = esc_attr($_REQUEST['stype']);
            if (isset($geodir_post_types[$spt]['labels']['singular_name'])) {
                $single_name = __($geodir_post_types[$spt]['labels']['singular_name'], 'geodirectory');
            }
        } elseif ($gd_page == 'add-listing') {
            $geodir_post_types = get_option('geodir_post_types');
            $spt = isset($_REQUEST['listing_type']) ? esc_attr($_REQUEST['listing_type']) : '';
            if (!$spt && isset($_REQUEST['pid'])) {
                $spt = get_post_type($_REQUEST['pid']);
            }
            if (!$spt) {
                $spt = 'gd_place';
            }
            if (isset($geodir_post_types[$spt]['labels']['singular_name'])) {
                $single_name = __($geodir_post_types[$spt]['labels']['singular_name'], 'geodirectory');
            }
        } elseif ($post->post_type) {
            $geodir_post_types = get_option('geodir_post_types');
            if (isset($geodir_post_types[$post->post_type]['labels']['singular_name'])) {
                $single_name = __($geodir_post_types[$post->post_type]['labels']['singular_name'], 'geodirectory');
            }
        }
        $title = str_replace("%%pt_single%%", $single_name, $title);
    }
    if (strpos($title, '%%pt_plural%%') !== false) {
        $plural_name = '';
        if ($gd_page == 'search' || $gd_page == 'author') {
            $geodir_post_types = get_option('geodir_post_types');
            $spt = esc_attr($_REQUEST['stype']);
            if (isset($geodir_post_types[$spt]['labels']['name'])) {
                $plural_name = __($geodir_post_types[$spt]['labels']['name'], 'geodirectory');
            }
        } elseif ($gd_page == 'add-listing') {
            $geodir_post_types = get_option('geodir_post_types');
            $spt = esc_attr($_REQUEST['listing_type']);
            if (!$spt) {
                $spt = 'gd_place';
            }
            if (isset($geodir_post_types[$spt]['labels']['name'])) {
                $plural_name = __($geodir_post_types[$spt]['labels']['name'], 'geodirectory');
            }
        } elseif (isset($post->post_type) && $post->post_type) {
            $geodir_post_types = get_option('geodir_post_types');
            if (isset($geodir_post_types[$post->post_type]['labels']['name'])) {
                $plural_name = __($geodir_post_types[$post->post_type]['labels']['name'], 'geodirectory');
            }
        }
        $title = str_replace("%%pt_plural%%", $plural_name, $title);
    }
    if (strpos($title, '%%category%%') !== false) {
        $cat_name = '';
        if ($gd_page == 'detail') {
            if ($post->default_category) {
                $cat = get_term($post->default_category, $post->post_type . 'category');
                $cat_name = isset($cat->name) ? $cat->name : '';
            }
        } elseif ($gd_page == 'listing') {
            $queried_object = get_queried_object();
            if (isset($queried_object->name)) {
                $cat_name = $queried_object->name;
            }
        }
        $title = str_replace("%%category%%", $cat_name, $title);
    }
    if (strpos($title, '%%tag%%') !== false) {
        $cat_name = '';
        if ($gd_page == 'detail') {
            if ($post->default_category) {
                $cat = get_term($post->default_category, $post->post_type . 'category');
                $cat_name = isset($cat->name) ? $cat->name : '';
            }
        } elseif ($gd_page == 'listing') {
            $queried_object = get_queried_object();
            if (isset($queried_object->name)) {
                $cat_name = $queried_object->name;
            }
        }
        $title = str_replace("%%tag%%", $cat_name, $title);
    }
    if (strpos($title, '%%id%%') !== false) {
        $ID = isset($post->ID) ? $post->ID : '';
        $title = str_replace("%%id%%", $ID, $title);
    }
    /*
        if(strpos($title,'') !== false){
            $title = str_replace("",,$title);
        }
    
        if(strpos($title,'') !== false){
            $title = str_replace("",,$title);
        }
    */
    if (strpos($title, '%%sep%%') !== false) {
        $title = str_replace("%%sep%%", $sep, $title);
    }
    global $wp;
    // location variables
    $gd_post_type = geodir_get_current_posttype();
    $location_array = geodir_get_current_location_terms('query_vars', $gd_post_type);
    /**
     * Filter the title variables location variables array
     *
     * @since 1.5.5
     * @package GeoDirectory
     * @param array $location_array The array of location variables.
     * @param string $title The title with variables..
     * @param string $gd_page The page being filtered.
     * @param string $sep The separator, default: `|`.
     */
    $location_array = apply_filters('geodir_filter_title_variables_location_arr', $location_array, $title, $gd_page, $sep);
    $location_titles = array();
    if ($gd_page == 'location' && get_query_var('gd_country_full')) {
        if (get_query_var('gd_country_full')) {
            $location_array['gd_country'] = get_query_var('gd_country_full');
        }
        if (get_query_var('gd_region_full')) {
            $location_array['gd_region'] = get_query_var('gd_region_full');
        }
        if (get_query_var('gd_city_full')) {
            $location_array['gd_city'] = get_query_var('gd_city_full');
        }
    }
    $location_single = '';
    $gd_country = isset($wp->query_vars['gd_country']) && $wp->query_vars['gd_country'] != '' ? $wp->query_vars['gd_country'] : '';
    $gd_region = isset($wp->query_vars['gd_region']) && $wp->query_vars['gd_region'] != '' ? $wp->query_vars['gd_region'] : '';
    $gd_city = isset($wp->query_vars['gd_city']) && $wp->query_vars['gd_city'] != '' ? $wp->query_vars['gd_city'] : '';
    $gd_country_actual = $gd_region_actual = $gd_city_actual = '';
    if (function_exists('get_actual_location_name')) {
        $gd_country_actual = $gd_country != '' ? get_actual_location_name('country', $gd_country, true) : $gd_country;
        $gd_region_actual = $gd_region != '' ? get_actual_location_name('region', $gd_region) : $gd_region;
        $gd_city_actual = $gd_city != '' ? get_actual_location_name('city', $gd_city) : $gd_city;
    }
    if ($gd_city != '') {
        if ($gd_city_actual != '') {
            $gd_city = $gd_city_actual;
        } else {
            $gd_city = preg_replace('/-(\\d+)$/', '', $gd_city);
            $gd_city = preg_replace('/[_-]/', ' ', $gd_city);
            $gd_city = __(geodir_ucwords($gd_city), 'geodirectory');
        }
        $location_single = $gd_city;
    } else {
        if ($gd_region != '') {
            if ($gd_region_actual != '') {
                $gd_region = $gd_region_actual;
            } else {
                $gd_region = preg_replace('/-(\\d+)$/', '', $gd_region);
                $gd_region = preg_replace('/[_-]/', ' ', $gd_region);
                $gd_region = __(geodir_ucwords($gd_region), 'geodirectory');
            }
            $location_single = $gd_region;
        } else {
            if ($gd_country != '') {
                if ($gd_country_actual != '') {
                    $gd_country = $gd_country_actual;
                } else {
                    $gd_country = preg_replace('/-(\\d+)$/', '', $gd_country);
                    $gd_country = preg_replace('/[_-]/', ' ', $gd_country);
                    $gd_country = __(geodir_ucwords($gd_country), 'geodirectory');
                }
                $location_single = $gd_country;
            }
        }
    }
    if (!empty($location_array)) {
        $actual_location_name = function_exists('get_actual_location_name') ? true : false;
        $location_array = array_reverse($location_array);
        foreach ($location_array as $location_type => $location) {
            $gd_location_link_text = preg_replace('/-(\\d+)$/', '', $location);
            $gd_location_link_text = preg_replace('/[_-]/', ' ', $gd_location_link_text);
            $location_name = geodir_ucwords($gd_location_link_text);
            $location_name = __($location_name, 'geodirectory');
            if ($actual_location_name) {
                $location_type = strpos($location_type, 'gd_') === 0 ? substr($location_type, 3) : $location_type;
                $location_name = get_actual_location_name($location_type, $location, true);
            }
            $location_titles[] = $location_name;
        }
        if (!empty($location_titles)) {
            $location_titles = array_unique($location_titles);
        }
    }
    if (strpos($title, '%%location%%') !== false) {
        $location = '';
        if ($location_titles) {
            $location = implode(", ", $location_titles);
        }
        $title = str_replace("%%location%%", $location, $title);
    }
    if (strpos($title, '%%in_location%%') !== false) {
        $location = '';
        if ($location_titles) {
            $location = __('in ', 'geodirectory') . implode(", ", $location_titles);
        }
        $title = str_replace("%%in_location%%", $location, $title);
    }
    if (strpos($title, '%%in_location_single%%') !== false) {
        if ($location_single) {
            $location_single = __('in', 'geodirectory') . ' ' . $location_single;
        }
        $title = str_replace("%%in_location_single%%", $location_single, $title);
    }
    if (strpos($title, '%%location_single%%') !== false) {
        $title = str_replace("%%location_single%%", $location_single, $title);
    }
    if (strpos($title, '%%search_term%%') !== false) {
        $search_term = '';
        if (isset($_REQUEST['s'])) {
            $search_term = esc_attr($_REQUEST['s']);
        }
        $title = str_replace("%%search_term%%", $search_term, $title);
    }
    if (strpos($title, '%%search_near%%') !== false) {
        $search_term = '';
        if (isset($_REQUEST['snear'])) {
            $search_term = esc_attr($_REQUEST['snear']);
        }
        $title = str_replace("%%search_near%%", $search_term, $title);
    }
    if (strpos($title, '%%name%%') !== false) {
        $author_name = '';
        if ($author_name = get_the_author()) {
        } else {
            $queried_object = get_queried_object();
            if (isset($queried_object->data->user_nicename)) {
                $author_name = $queried_object->data->user_nicename;
            }
        }
        $title = str_replace("%%name%%", $author_name, $title);
    }
    $title = wptexturize($title);
    $title = convert_chars($title);
    $title = esc_html($title);
    /**
     * Filter the title variables after standard ones have been filtered.
     *
     * @since 1.5.7
     * @package GeoDirectory
     * @param string $title The title with variables.
     * @param array $location_array The array of location variables.
     * @param string $gd_page The page being filtered.
     * @param string $sep The separator, default: `|`.
     */
    return apply_filters('geodir_filter_title_variables_vars', $title, $location_array, $gd_page, $sep);
}
               onclick="show_hide('field_frm<?php 
    echo $result_str;
    ?>
')"><?php 
    echo geodir_ucwords(__('Fieldset:', 'geodirectory') . ' ' . $field_admin_title);
    ?>
</b>
        <?php 
} else {
    ?>
            <b style="cursor:pointer;"
               onclick="show_hide('field_frm<?php 
    echo $result_str;
    ?>
')"><?php 
    echo geodir_ucwords(__('Field:', 'geodirectory') . ' ' . $field_admin_title . ' (' . $field_type . ')');
    ?>
</b>
        <?php 
}
?>
    </div>

    <div id="field_frm<?php 
echo $result_str;
?>
" class="field_frm"
         style="display:<?php 
if ($field_ins_upd == 'submit') {
    echo 'block;';
} else {
    /**
     * Front-end display content for best of widget.
     *
     * @since 1.3.9
     * @since 1.5.1 Added filter to view all link.
     * @since 1.5.1 Declare function public.
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget($args, $instance)
    {
        extract($args);
        /**
         * Filter the best of widget tab layout.
         *
         * @since 1.3.9
         *
         * @param string $instance['tab_layout'] Best of widget tab layout name.
         */
        $tab_layout = empty($instance['tab_layout']) ? 'bestof-tabs-on-top' : apply_filters('bestof_widget_tab_layout', $instance['tab_layout']);
        echo '<div class="bestof-widget-tab-layout ' . $tab_layout . '">';
        echo $before_widget;
        $loc_terms = geodir_get_current_location_terms();
        if ($loc_terms) {
            $cur_location = ' : ' . geodir_ucwords(str_replace('-', ' ', end($loc_terms)));
        } else {
            $cur_location = '';
        }
        /**
         * Filter the current location name.
         *
         * @since 1.3.9
         *
         * @param string $cur_location Current location name.
         */
        $cur_location = apply_filters('bestof_widget_cur_location', $cur_location);
        /**
         * Filter the widget title.
         *
         * @since 1.3.9
         *
         * @param string $instance['title'] The widget title.
         */
        $title = empty($instance['title']) ? wp_sprintf(__('Best of %s', 'geodirectory'), get_bloginfo('name') . $cur_location) : apply_filters('bestof_widget_title', __($instance['title'], 'geodirectory'));
        /**
         * Filter the post type.
         *
         * @since 1.3.9
         *
         * @param string $instance['post_type'] The post type.
         */
        $post_type = empty($instance['post_type']) ? 'gd_place' : apply_filters('bestof_widget_post_type', $instance['post_type']);
        /**
         * Filter the excerpt type.
         *
         * @since 1.5.4
         *
         * @param string $instance['excerpt_type'] The excerpt type.
         */
        $excerpt_type = empty($instance['excerpt_type']) ? 'show-desc' : apply_filters('bestof_widget_excerpt_type', $instance['excerpt_type']);
        /**
         * Filter the listing limit.
         *
         * @since 1.3.9
         *
         * @param int $instance['post_limit'] No. of posts to display.
         */
        $post_limit = empty($instance['post_limit']) ? '5' : apply_filters('bestof_widget_post_limit', $instance['post_limit']);
        /**
         * Filter the category limit.
         *
         * @since 1.3.9
         *
         * @param int $instance['categ_limit'] No. of categories to display.
         */
        $categ_limit = empty($instance['categ_limit']) ? '3' : apply_filters('bestof_widget_categ_limit', $instance['categ_limit']);
        $use_viewing_post_type = !empty($instance['use_viewing_post_type']) ? true : false;
        /**
         * Filter the use of location filter.
         *
         * @since 1.3.9
         *
         * @param int|bool $instance['add_location_filter'] Filter listings using current location.
         */
        $add_location_filter = empty($instance['add_location_filter']) ? '1' : apply_filters('bestof_widget_location_filter', $instance['add_location_filter']);
        // set post type to current viewing post type
        if ($use_viewing_post_type) {
            $current_post_type = geodir_get_current_posttype();
            if ($current_post_type != '' && $current_post_type != $post_type) {
                $post_type = $current_post_type;
            }
        }
        if (isset($instance['character_count'])) {
            /**
             * Filter the widget's excerpt character count.
             *
             * @since 1.3.9
             *
             * @param int $instance['character_count'] Excerpt character count.
             */
            $character_count = apply_filters('bestof_widget_list_character_count', $instance['character_count']);
        } else {
            $character_count = '';
        }
        $category_taxonomy = geodir_get_taxonomies($post_type);
        $term_args = array('hide_empty' => true, 'parent' => 0);
        if (is_tax()) {
            $taxonomy = get_query_var('taxonomy');
            $cur_term = get_query_var('term');
            $term_data = get_term_by('name', $cur_term, $taxonomy);
            $term_args['parent'] = $term_data->term_id;
        }
        $terms = get_terms($category_taxonomy[0], $term_args);
        $term_reviews = geodir_count_reviews_by_terms();
        $a_terms = array();
        foreach ($terms as $term) {
            if ($term->count > 0) {
                if (isset($term_reviews[$term->term_id])) {
                    $term->review_count = $term_reviews[$term->term_id];
                } else {
                    $term->review_count = '0';
                }
                $a_terms[] = $term;
            }
        }
        $terms = geodir_sort_terms($a_terms, 'review_count');
        $query_args = array('posts_per_page' => $post_limit, 'is_geodir_loop' => true, 'post_type' => $post_type, 'gd_location' => $add_location_filter ? true : false, 'order_by' => 'high_review');
        if ($character_count >= 0) {
            $query_args['excerpt_length'] = $character_count;
        }
        $layout = array();
        if ($tab_layout == 'bestof-tabs-as-dropdown') {
            $layout[] = $tab_layout;
        } else {
            $layout[] = 'bestof-tabs-as-dropdown';
            $layout[] = $tab_layout;
        }
        echo $before_title . __($title) . $after_title;
        //term navigation - start
        echo '<div class="geodir-tabs gd-bestof-tabs" id="gd-bestof-tabs" style="position:relative;">';
        $final_html = '';
        foreach ($layout as $tab_layout) {
            $nav_html = '';
            $is_dropdown = $tab_layout == 'bestof-tabs-as-dropdown' ? true : false;
            if ($is_dropdown) {
                $nav_html .= '<select id="geodir_bestof_tab_dd" class="chosen_select" name="geodir_bestof_tab_dd" data-placeholder="' . esc_attr(__('Select Category', 'geodirectory')) . '">';
            } else {
                $nav_html .= '<dl class="geodir-tab-head geodir-bestof-cat-list">';
                $nav_html .= '<dt></dt>';
            }
            $term_icon = geodir_get_term_icon();
            $cat_count = 0;
            foreach ($terms as $cat) {
                $cat_count++;
                if ($cat_count > $categ_limit) {
                    break;
                }
                if ($is_dropdown) {
                    $selected = $cat_count == 1 ? 'selected="selected"' : '';
                    $nav_html .= '<option ' . $selected . ' value="' . $cat->term_id . '">' . geodir_ucwords($cat->name) . '</option>';
                } else {
                    if ($cat_count == 1) {
                        $nav_html .= '<dd class="geodir-tab-active">';
                    } else {
                        $nav_html .= '<dd class="">';
                    }
                    $term_icon_url = !empty($term_icon) && isset($term_icon[$cat->term_id]) ? $term_icon[$cat->term_id] : '';
                    $nav_html .= '<a data-termid="' . $cat->term_id . '" href="' . get_term_link($cat, $cat->taxonomy) . '">';
                    $nav_html .= '<img alt="' . $cat->name . ' icon" class="bestof-cat-icon" src="' . $term_icon_url . '"/>';
                    $nav_html .= '<span>';
                    $nav_html .= geodir_ucwords($cat->name);
                    $nav_html .= '<small>';
                    if (isset($cat->review_count)) {
                        $num_reviews = $cat->review_count;
                        if ($num_reviews == 0) {
                            $reviews = __('No Reviews', 'geodirectory');
                        } elseif ($num_reviews > 1) {
                            $reviews = $num_reviews . __(' Reviews', 'geodirectory');
                        } else {
                            $reviews = __('1 Review', 'geodirectory');
                        }
                        $nav_html .= $reviews;
                    }
                    $nav_html .= '</small>';
                    $nav_html .= '</span>';
                    $nav_html .= '</a>';
                    $nav_html .= '</dd>';
                }
            }
            if ($is_dropdown) {
                $nav_html .= '</select>';
            } else {
                $nav_html .= '</dl>';
            }
            $final_html .= $nav_html;
        }
        if ($terms) {
            echo $final_html;
        }
        echo '</div>';
        //term navigation - end
        //first term listings by default - start
        $first_term = '';
        if ($terms) {
            $first_term = $terms[0];
            $tax_query = array('taxonomy' => $category_taxonomy[0], 'field' => 'id', 'terms' => $first_term->term_id);
            $query_args['tax_query'] = array($tax_query);
        }
        ?>
        <input type="hidden" id="bestof_widget_post_type" name="bestof_widget_post_type"
               value="<?php 
        echo $post_type;
        ?>
">
        <input type="hidden" id="bestof_widget_excerpt_type" name="bestof_widget_excerpt_type"
               value="<?php 
        echo $excerpt_type;
        ?>
">
        <input type="hidden" id="bestof_widget_post_limit" name="bestof_widget_post_limit"
               value="<?php 
        echo $post_limit;
        ?>
">
        <input type="hidden" id="bestof_widget_taxonomy" name="bestof_widget_taxonomy"
               value="<?php 
        echo $category_taxonomy[0];
        ?>
">
        <input type="hidden" id="bestof_widget_location_filter" name="bestof_widget_location_filter"
               value="<?php 
        if ($add_location_filter) {
            echo 1;
        } else {
            echo 0;
        }
        ?>
">
        <input type="hidden" id="bestof_widget_char_count" name="bestof_widget_char_count"
               value="<?php 
        echo $character_count;
        ?>
">
        <div class="geo-bestof-contentwrap geodir-tabs-content" style="position: relative; z-index: 0;">
            <p id="geodir-bestof-loading" class="geodir-bestof-loading"><i class="fa fa-cog fa-spin"></i></p>
            <?php 
        echo '<div id="geodir-bestof-places">';
        if ($terms) {
            $view_all_link = add_query_arg(array('sort_by' => 'rating_count_desc'), get_term_link($first_term, $first_term->taxonomy));
            /**
             * Filter the page link to view all lisitngs.
             *
             * @since 1.5.1
             *
             * @param array $view_all_link View all listings page link.
             * @param array $post_type The Post type.
             * @param array $first_term The category term object.
             */
            $view_all_link = apply_filters('geodir_bestof_widget_view_all_link', $view_all_link, $post_type, $first_term);
            echo '<h3 class="bestof-cat-title">' . wp_sprintf(__('Best of %s', 'geodirectory'), $first_term->name) . '<a href="' . esc_url($view_all_link) . '">' . __("View all", 'geodirectory') . '</a></h3>';
        }
        if ($excerpt_type == 'show-reviews') {
            add_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
        }
        geodir_bestof_places_by_term($query_args);
        if ($excerpt_type == 'show-reviews') {
            remove_filter('get_the_excerpt', 'best_of_show_review_in_excerpt');
        }
        echo "</div>";
        ?>
        </div>
        <?php 
        //first term listings by default - end
        ?>
        <?php 
        echo $after_widget;
        echo "</div>";
    }
    /**
     * Custom sort field admin html.
     *
     * @since 1.0.0
     * @package GeoDirectory
     * @global object $wpdb WordPress Database object.
     * @param string $field_type The form field type.
     * @param object|int $result_str The custom field results object or row id.
     * @param string $field_ins_upd When set to "submit" displays form.
     * @param bool $default when set to true field will be for admin use only.
     */
    function geodir_custom_sort_field_adminhtml($field_type, $result_str, $field_ins_upd = '', $default = false)
    {
        global $wpdb;
        $cf = $result_str;
        if (!is_object($cf)) {
            $field_info = $wpdb->get_row($wpdb->prepare("select * from " . GEODIR_CUSTOM_SORT_FIELDS_TABLE . " where id= %d", array($cf)));
        } else {
            $field_info = $cf;
            $result_str = $cf->id;
        }
        $field_info = stripslashes_deep($field_info);
        // strip slashes
        if (!isset($field_info->post_type)) {
            $post_type = $_REQUEST['listing_type'];
        } else {
            $post_type = $field_info->post_type;
        }
        $field_types = explode('-_-', $field_type);
        $field_type = $field_types[0];
        $htmlvar_name = isset($field_types[1]) ? $field_types[1] : '';
        $site_title = '';
        if ($site_title == '') {
            $site_title = isset($field_info->site_title) ? $field_info->site_title : '';
        }
        if ($site_title == '') {
            $fields = geodir_get_custom_sort_options($post_type);
            foreach ($fields as $val) {
                $val = stripslashes_deep($val);
                // strip slashes
                if ($val['field_type'] == $field_type && $val['htmlvar_name'] == $htmlvar_name) {
                    $site_title = isset($val['site_title']) ? $val['site_title'] : '';
                }
            }
        }
        if ($htmlvar_name == '') {
            $htmlvar_name = isset($field_info->htmlvar_name) ? $field_info->htmlvar_name : '';
        }
        $nonce = wp_create_nonce('custom_fields_' . $result_str);
        ?>
        <li class="text" id="licontainer_<?php 
        echo $result_str;
        ?>
">
            <div class="title title<?php 
        echo $result_str;
        ?>
 gt-fieldset"
                 title="<?php 
        _e('Double Click to toggle and drag-drop to sort', 'geodirectory');
        ?>
"
                 ondblclick="show_hide('field_frm<?php 
        echo $result_str;
        ?>
')">
                <?php 
        $nonce = wp_create_nonce('custom_fields_' . $result_str);
        ?>

                <div title="<?php 
        _e('Click to remove field', 'geodirectory');
        ?>
"
                     onclick="delete_sort_field('<?php 
        echo $result_str;
        ?>
', '<?php 
        echo $nonce;
        ?>
', this)"
                     class="handlediv close"></div>

                <b style="cursor:pointer;"
                   onclick="show_hide('field_frm<?php 
        echo $result_str;
        ?>
')"><?php 
        echo geodir_ucwords(__('Field:', 'geodirectory') . ' (' . $site_title . ')');
        ?>
</b>

            </div>

            <div id="field_frm<?php 
        echo $result_str;
        ?>
" class="field_frm"
                 style="display:<?php 
        if ($field_ins_upd == 'submit') {
            echo 'block;';
        } else {
            echo 'none;';
        }
        ?>
">
                <input type="hidden" name="_wpnonce" value="<?php 
        echo $nonce;
        ?>
"/>
                <input type="hidden" name="listing_type" id="listing_type" value="<?php 
        echo $post_type;
        ?>
"/>
                <input type="hidden" name="field_type" id="field_type" value="<?php 
        echo $field_type;
        ?>
"/>
                <input type="hidden" name="field_id" id="field_id" value="<?php 
        echo $result_str;
        ?>
"/>
                <input type="hidden" name="data_type" id="data_type" value="<?php 
        if (isset($field_info->data_type)) {
            echo $field_info->data_type;
        }
        ?>
"/>
                <input type="hidden" name="htmlvar_name" id="htmlvar_name" value="<?php 
        echo $htmlvar_name;
        ?>
"/>


                <table class="widefat post fixed" border="0" style="width:100%;">

                    <?php 
        if ($field_type != 'random') {
            ?>

                        <input type="hidden" name="site_title" id="site_title" value="<?php 
            echo esc_attr($site_title);
            ?>
"/>

                        <tr>
                            <td>Select Ascending</td>
                            <td>
                                <input type="checkbox" name="asc" id="asc"
                                       value="1" <?php 
            if (isset($field_info->sort_asc) && $field_info->sort_asc == '1') {
                echo 'checked="checked"';
            }
            ?>
/>

                                <input type="text" name="asc_title" id="asc_title"
                                       placeholder="<?php 
            esc_attr_e('Ascending title', 'geodirectory');
            ?>
"
                                       value="<?php 
            if (isset($field_info->asc_title)) {
                echo esc_attr($field_info->asc_title);
            }
            ?>
" style="width:45%;"/>

                                <input type="radio" name="is_default"
                                       value="<?php 
            echo $htmlvar_name;
            ?>
_asc" <?php 
            if (isset($field_info->default_order) && $field_info->default_order == $htmlvar_name . '_asc') {
                echo 'checked="checked"';
            }
            ?>
/><span><?php 
            _e('Set as default sort.', 'geodirectory');
            ?>
</span>

                                <br/>
                                <span><?php 
            _e('Select if you want to show option in sort.', 'geodirectory');
            ?>
</span>
                            </td>
                        </tr>

                        <tr>
                            <td>Select Descending</td>
                            <td>
                                <input type="checkbox" name="desc" id="desc"
                                       value="1" <?php 
            if (isset($field_info->sort_desc) && $field_info->sort_desc == '1') {
                echo 'checked="checked"';
            }
            ?>
/>

                                <input type="text" name="desc_title" id="desc_title"
                                       placeholder="<?php 
            esc_attr_e('Descending title', 'geodirectory');
            ?>
"
                                       value="<?php 
            if (isset($field_info->desc_title)) {
                echo esc_attr($field_info->desc_title);
            }
            ?>
" style="width:45%;"/>
                                <input type="radio" name="is_default"
                                       value="<?php 
            echo $htmlvar_name;
            ?>
_desc" <?php 
            if (isset($field_info->default_order) && $field_info->default_order == $htmlvar_name . '_desc') {
                echo 'checked="checked"';
            }
            ?>
/><span><?php 
            _e('Set as default sort.', 'geodirectory');
            ?>
</span>
                                <br/>
                                <span><?php 
            _e('Select if you want to show option in sort.', 'geodirectory');
            ?>
</span>
                            </td>
                        </tr>

                    <?php 
        } else {
            ?>


                        <tr>
                            <td><strong><?php 
            _e('Frontend title :', 'geodirectory');
            ?>
</strong></td>
                            <td align="left">
                                <input type="text" name="site_title" id="site_title" value="<?php 
            echo esc_attr($site_title);
            ?>
"
                                       size="50"/>
                                <br/><span><?php 
            _e('Section title which you wish to display in frontend', 'geodirectory');
            ?>
</span>
                            </td>
                        </tr>

                        <tr>
                            <td><strong><?php 
            _e('Default sort option :', 'geodirectory');
            ?>
</strong></td>
                            <td align="left">
                                <input type="checkbox" name="is_default"
                                       value="<?php 
            echo $field_type;
            ?>
"  <?php 
            if (isset($field_info->is_default) && $field_info->is_default == '1') {
                echo 'checked="checked"';
            }
            ?>
/>
                                <br/>
                                <span><?php 
            _e('If field is checked then the field will be use as default sort.', 'geodirectory');
            ?>
</span>
                            </td>
                        </tr>

                    <?php 
        }
        ?>

                    <tr>
                        <td><strong><?php 
        _e('Is active :', 'geodirectory');
        ?>
</strong></td>
                        <td align="left">
                            <select name="is_active" id="is_active">
                                <option
                                    value="1" <?php 
        if (isset($field_info->is_active) && $field_info->is_active == '1') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('Yes', 'geodirectory');
        ?>
</option>
                                <option
                                    value="0" <?php 
        if (isset($field_info->is_active) && $field_info->is_active == '0') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('No', 'geodirectory');
        ?>
</option>
                            </select>
                            <br/>
                            <span><?php 
        _e('Select yes or no. If no is selected then the field will not be displayed anywhere.', 'geodirectory');
        ?>
</span>
                        </td>
                    </tr>

                    <tr>
                        <td><strong><?php 
        _e('Display order :', 'geodirectory');
        ?>
</strong></td>
                        <td align="left"><input type="text" readonly="readonly" name="sort_order" id="sort_order"
                                                value="<?php 
        if (isset($field_info->sort_order)) {
            echo esc_attr($field_info->sort_order);
        }
        ?>
" size="50"/>
                            <br/>
                            <span><?php 
        _e('Enter the display order of this field in backend. e.g. 5', 'geodirectory');
        ?>
</span>
                        </td>
                    </tr>

                    <tr>
                        <td>&nbsp;</td>
                        <td align="left">
                            <input type="button" class="button" name="save" id="save" value="<?php 
        esc_attr_e('Save', 'geodirectory');
        ?>
"
                                   onclick="save_sort_field('<?php 
        echo $result_str;
        ?>
')"/>

                            <a href="javascript:void(0)"><input type="button" name="delete" value="<?php 
        esc_attr_e('Delete', 'geodirectory');
        ?>
"
                                                                onclick="delete_sort_field('<?php 
        echo $result_str;
        ?>
', '<?php 
        echo $nonce;
        ?>
', this)"
                                                                class="button_n"/></a>

                        </td>
                    </tr>
                </table>

            </div>
        </li> <?php 
    }
 /**
  * Sanitize popular posts widget form values as they are saved.
  *
  * @since 1.0.0
  * @since 1.5.1 Declare function public.
  *
  * @param array $new_instance Values just sent to be saved.
  * @param array $old_instance Previously saved values from database.
  *
  * @return array Updated safe values to be saved.
  */
 public function update($new_instance, $old_instance)
 {
     //save the widget
     $instance = $old_instance;
     if ($new_instance['title'] == '') {
         $title = geodir_ucwords(strip_tags($new_instance['category_title']));
         //$instance['title'] = $title;
     }
     $instance['title'] = strip_tags($new_instance['title']);
     $instance['post_type'] = strip_tags($new_instance['post_type']);
     //$instance['category'] = strip_tags($new_instance['category']);
     $instance['category'] = isset($new_instance['category']) ? $new_instance['category'] : '';
     $instance['category_title'] = strip_tags($new_instance['category_title']);
     $instance['post_number'] = strip_tags($new_instance['post_number']);
     $instance['layout'] = strip_tags($new_instance['layout']);
     $instance['listing_width'] = strip_tags($new_instance['listing_width']);
     $instance['list_sort'] = strip_tags($new_instance['list_sort']);
     $instance['character_count'] = $new_instance['character_count'];
     if (isset($new_instance['add_location_filter']) && $new_instance['add_location_filter'] != '') {
         $instance['add_location_filter'] = strip_tags($new_instance['add_location_filter']);
     } else {
         $instance['add_location_filter'] = '0';
     }
     $instance['show_featured_only'] = isset($new_instance['show_featured_only']) && $new_instance['show_featured_only'] ? 1 : 0;
     $instance['show_special_only'] = isset($new_instance['show_special_only']) && $new_instance['show_special_only'] ? 1 : 0;
     $instance['with_pics_only'] = isset($new_instance['with_pics_only']) && $new_instance['with_pics_only'] ? 1 : 0;
     $instance['with_videos_only'] = isset($new_instance['with_videos_only']) && $new_instance['with_videos_only'] ? 1 : 0;
     $instance['use_viewing_post_type'] = isset($new_instance['use_viewing_post_type']) && $new_instance['use_viewing_post_type'] ? 1 : 0;
     return $instance;
 }
/**
 * Output the author page title including HTML wrappers.
 *
 * @global string $term Current term slug.
 * @since 1.0.0
 * @package GeoDirectory
 */
function geodir_action_author_page_title()
{
    global $term;
    $gd_post_type = geodir_get_current_posttype();
    $post_type_info = get_post_type_object($gd_post_type);
    $add_string_in_title = __('All', 'geodirectory') . ' ';
    if (isset($_REQUEST['list']) && $_REQUEST['list'] == 'favourite') {
        $add_string_in_title = __('My Favorite', 'geodirectory') . ' ';
    }
    $list_title = $add_string_in_title . $post_type_info->labels->name;
    $single_name = $post_type_info->labels->singular_name;
    $taxonomy = geodir_get_taxonomies($gd_post_type);
    if (!empty($term)) {
        $current_term = get_term_by('slug', $term, $taxonomy[0]);
        if (!empty($current_term)) {
            $list_title .= __(' in', 'geodirectory') . " '" . geodir_ucwords($current_term->name) . "'";
        }
    }
    if (is_search()) {
        $list_title = __('Search', 'geodirectory') . ' ' . __($post_type_info->labels->name, 'geodirectory') . __(' For :', 'geodirectory') . " '" . get_search_query() . "'";
    }
    /** This action is documented in geodirectory_template_actions.php */
    $class = apply_filters('geodir_page_title_class', 'entry-title fn');
    /** This action is documented in geodirectory_template_actions.php */
    $class_header = apply_filters('geodir_page_title_header_class', 'entry-header');
    $title = $list_title;
    if (geodir_is_page('author')) {
        $gd_page = 'author';
        if (isset($_REQUEST['list']) && $_REQUEST['list'] == 'favourite') {
            $title = get_option('geodir_page_title_favorite') ? get_option('geodir_page_title_favorite') : $title;
        } else {
            $title = get_option('geodir_page_title_author') ? get_option('geodir_page_title_author') : $title;
        }
    }
    /**
     * Filter page title to replace variables.
     *
     * @since 1.5.4
     * @param string $title The page title including variables.
     * @param string $gd_page The GeoDirectory page type if any.
     */
    $title = apply_filters('geodir_seo_page_title', __($title, 'geodirectory'), $gd_page);
    echo '<header class="' . $class_header . '"><h1 class="' . $class . '">' . apply_filters('geodir_author_page_title_text', $title) . '</h1></header>';
}