/**
     * Custom taxonomy walker function.
     *
     * @since 1.0.0
     * @package GeoDirectory
     * @global object $post WordPress Post object.
     * @param string $cat_taxonomy The taxonomy name.
     * @param string $cat_limit Number of categories to display.
     */
    function geodir_custom_taxonomy_walker2($cat_taxonomy, $cat_limit = '')
    {
        $post_category = '';
        $post_category_str = '';
        global $exclude_cats;
        $cat_exclude = '';
        if (is_array($exclude_cats) && !empty($exclude_cats)) {
            $cat_exclude = serialize($exclude_cats);
        }
        if (isset($_REQUEST['backandedit'])) {
            $post = (object) unserialize($_SESSION['listing']);
            if (!is_array($post->post_category[$cat_taxonomy])) {
                $post_category = $post->post_category[$cat_taxonomy];
            }
            $post_categories = $post->post_category_str;
            if (!empty($post_categories) && array_key_exists($cat_taxonomy, $post_categories)) {
                $post_category_str = $post_categories[$cat_taxonomy];
            }
        } elseif (geodir_is_page('add-listing') && isset($_REQUEST['pid']) && $_REQUEST['pid'] != '' || is_admin()) {
            global $post;
            $post_category = geodir_get_post_meta($post->ID, $cat_taxonomy, true);
            if (empty($post_category) && isset($post->{$cat_taxonomy})) {
                $post_category = $post->{$cat_taxonomy};
            }
            $post_categories = get_post_meta($post->ID, 'post_categories', true);
            if (empty($post_category) && !empty($post_categories) && !empty($post_categories[$cat_taxonomy])) {
                foreach (explode(",", $post_categories[$cat_taxonomy]) as $cat_part) {
                    if (is_numeric($cat_part)) {
                        $cat_part_arr[] = $cat_part;
                    }
                }
                if (is_array($cat_part_arr)) {
                    $post_category = implode(',', $cat_part_arr);
                }
            }
            if (!empty($post_category)) {
                $cat1 = array_filter(explode(',', $post_category));
                $post_category = ',' . implode(',', $cat1) . ',';
            }
            if ($post_category != '' && is_array($exclude_cats) && !empty($exclude_cats)) {
                $post_category_upd = explode(',', $post_category);
                $post_category_change = '';
                foreach ($post_category_upd as $cat) {
                    if (!in_array($cat, $exclude_cats) && $cat != '') {
                        $post_category_change .= ',' . $cat;
                    }
                }
                $post_category = $post_category_change;
            }
            if (!empty($post_categories) && array_key_exists($cat_taxonomy, $post_categories)) {
                $post_category_str = $post_categories[$cat_taxonomy];
            }
        }
        echo '<input type="hidden" id="cat_limit" value="' . $cat_limit . '" name="cat_limit[' . $cat_taxonomy . ']"  />';
        echo '<input type="hidden" id="post_category" value="' . $post_category . '" name="post_category[' . $cat_taxonomy . ']"  />';
        echo '<input type="hidden" id="post_category_str" value="' . $post_category_str . '" name="post_category_str[' . $cat_taxonomy . ']"  />';
        ?>
        <div class="cat_sublist">
            <?php 
        $post_id = isset($post->ID) ? $post->ID : '';
        if ((geodir_is_page('add-listing') || is_admin()) && !empty($post_categories[$cat_taxonomy])) {
            geodir_editpost_categories_html($cat_taxonomy, $post_id, $post_categories);
        }
        ?>
        </div>
        <script type="text/javascript">

            function show_subcatlist(main_cat, catObj) {
                if (main_cat != '') {
					var url = '<?php 
        echo geodir_get_ajax_url();
        ?>
';
                    var cat_taxonomy = '<?php 
        echo $cat_taxonomy;
        ?>
';
                    var cat_exclude = '<?php 
        echo base64_encode($cat_exclude);
        ?>
';
                    var cat_limit = jQuery('#' + cat_taxonomy).find('#cat_limit').val();
					<?php 
        if ((int) $cat_limit > 0) {
            ?>
					var selected = parseInt(jQuery('#' + cat_taxonomy).find('.cat_sublist > div.post_catlist_item').length);
					if (cat_limit != '' && selected > 0 && selected >= cat_limit && cat_limit != 0) {
						alert("<?php 
            echo esc_attr(wp_sprintf(__('You have reached category limit of %d categories.', 'geodirectory'), (int) $cat_limit));
            ?>
");
						return false;
					}
					<?php 
        }
        ?>
                    jQuery.post(url, {
                        geodir_ajax: 'category_ajax',
                        cat_tax: cat_taxonomy,
                        main_catid: main_cat,
                        exclude: cat_exclude
                    }, function (data) {
                        if (data != '') {
                            jQuery('#' + cat_taxonomy).find('.cat_sublist').append(data);

                            setTimeout(function () {
                                jQuery('#' + cat_taxonomy).find('.cat_sublist').find('.chosen_select').chosen();
                            }, 200);


                        }
                        maincat_obj = jQuery('#' + cat_taxonomy).find('.main_cat_list');

                        if (cat_limit != '' && jQuery('#' + cat_taxonomy).find('.cat_sublist .chosen_select').length >= cat_limit) {
                            maincat_obj.find('.chosen_select').chosen('destroy');
                            maincat_obj.hide();
                        } else {
                            maincat_obj.show();
                            maincat_obj.find('.chosen_select').chosen('destroy');
                            maincat_obj.find('.chosen_select').prop('selectedIndex', 0);
                            maincat_obj.find('.chosen_select').chosen();
                        }

                        update_listing_cat();

                    });
                }
                update_listing_cat();
            }

            function update_listing_cat(el) {
                var cat_taxonomy = '<?php 
        echo $cat_taxonomy;
        ?>
';
                var cat_ids = '';
                var main_cat = '';
                var sub_cat = '';
                var post_cat_str = '';
                var cat_limit = jQuery('#' + cat_taxonomy).find('#cat_limit').val();
				
				var delEl = jQuery(el).closest('.post_catlist_item').find('input.listing_main_cat');
				if (typeof el != 'undefined' && jQuery(delEl).val()) {
					jQuery('.geodir_taxonomy_field').find('select > option[_hc="f"][value="'+jQuery(delEl).val()+'"]').attr('disabled', false);
				}
				jQuery('.geodir_taxonomy_field').find('input.listing_main_cat:checked').each(function() {
					var cV = jQuery(this).val();
					if (parseInt(cV) > 0) {
						jQuery('.geodir_taxonomy_field').find('select > option[_hc="f"][value="'+cV+'"]').attr('disabled', true);
					}
				});

                jQuery('#' + cat_taxonomy).find('.cat_sublist > div').each(function () {
                    main_cat = jQuery(this).find('.listing_main_cat').val();

                    if (jQuery(this).find('.chosen_select').length > 0)
                        sub_cat = jQuery(this).find('.chosen_select').val()

                    if (post_cat_str != '')
                        post_cat_str = post_cat_str + '#';

                    post_cat_str = post_cat_str + main_cat;

                    if (jQuery(this).find('.listing_main_cat').is(':checked')) {
                        cat_ids = cat_ids + ',' + main_cat;
                        post_cat_str = post_cat_str + ',y';

                        if (jQuery(this).find('.post_default_category input').is(':checked'))
                            post_cat_str = post_cat_str + ',d';

                    } else {
                        post_cat_str = post_cat_str + ',n';
                    }

                    if (sub_cat != '' && sub_cat) {
                        cat_ids = cat_ids + ',' + sub_cat;
                        post_cat_str = post_cat_str + ':' + sub_cat;
                    } else {
                        post_cat_str = post_cat_str + ':';
                    }

                });

                maincat_obj = jQuery('#' + cat_taxonomy).find('.main_cat_list');
                if (cat_limit != '' && jQuery('#' + cat_taxonomy).find('.cat_sublist > div.post_catlist_item').length >= cat_limit && cat_limit != 0) {
                    maincat_obj.find('.chosen_select').chosen('destroy');
                    maincat_obj.hide();
                } else {
                    maincat_obj.show();
                    maincat_obj.find('.chosen_select').chosen('destroy');
                    maincat_obj.find('.chosen_select').prop('selectedIndex', 0);
                    maincat_obj.find('.chosen_select').chosen();
                }

                maincat_obj.find('.chosen_select').trigger("chosen:updated");
                jQuery('#' + cat_taxonomy).find('#post_category').val(cat_ids);
                jQuery('#' + cat_taxonomy).find('#post_category_str').val(post_cat_str);
            }
            jQuery(function () {
                update_listing_cat();
            })


        </script>
        <?php 
        if (!empty($post_categories) && array_key_exists($cat_taxonomy, $post_categories)) {
            $post_cat_str = $post_categories[$cat_taxonomy];
            $post_cat_array = explode("#", $post_cat_str);
            if (count($post_cat_array) >= $cat_limit && $cat_limit != 0) {
                $style = "display:none;";
            }
        }
        ?>
        <div class="main_cat_list" style=" <?php 
        if (isset($style)) {
            echo $style;
        }
        ?>
 ">
            <?php 
        geodir_get_catlist($cat_taxonomy, 0);
        // print main categories list
        ?>
        </div>
    <?php 
    }
/**
 * Processes GeoDirectory ajax url calls.
 *
 * @see geodir_get_ajax_url()
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @global object $current_user Current user object.
 * @todo check if nonce is required here and if so add one.
 */
function geodir_ajax_handler()
{
    global $wpdb;
    if (isset($_REQUEST['gd_listing_view']) && $_REQUEST['gd_listing_view'] != '') {
        $_SESSION['gd_listing_view'] = $_REQUEST['gd_listing_view'];
        echo '1';
    }
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'category_ajax') {
        if (isset($_REQUEST['main_catid']) && isset($_REQUEST['cat_tax']) && isset($_REQUEST['exclude'])) {
            geodir_addpost_categories_html($_REQUEST['cat_tax'], $_REQUEST['main_catid'], '', '', '', $_REQUEST['exclude']);
        } elseif (isset($_REQUEST['catpid']) && isset($_REQUEST['cat_tax'])) {
            geodir_editpost_categories_html($_REQUEST['cat_tax'], $_REQUEST['catpid']);
        }
    }
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'admin_ajax' || isset($_REQUEST['create_field']) || isset($_REQUEST['sort_create_field'])) {
        if (current_user_can('manage_options')) {
            /**
             * Contains admin ajax handling functions.
             *
             * @since 1.0.0
             * @package GeoDirectory
             */
            include_once geodir_plugin_path() . '/geodirectory-admin/geodir_admin_ajax.php';
        } else {
            wp_redirect(home_url() . '/?geodir_signup=true');
            exit;
        }
    }
    if (isset($_REQUEST['geodir_autofill']) && $_REQUEST['geodir_autofill'] != '' && isset($_REQUEST['_wpnonce'])) {
        if (current_user_can('manage_options')) {
            switch ($_REQUEST['geodir_autofill']) {
                case "geodir_dummy_delete":
                    if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'geodir_dummy_posts_delete_noncename')) {
                        return;
                    }
                    if (isset($_REQUEST['posttype'])) {
                        /**
                         * Used to delete the dummy post data per post type.
                         *
                         * Uses dynamic hook, geodir_delete_dummy_posts_$_REQUEST['posttype'].
                         *
                         * @since 1.0.0
                         */
                        do_action('geodir_delete_dummy_posts_' . $_REQUEST['posttype']);
                    }
                    break;
                case "geodir_dummy_insert":
                    if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'geodir_dummy_posts_insert_noncename')) {
                        return;
                    }
                    global $dummy_post_index, $city_bound_lat1, $city_bound_lng1, $city_bound_lat2, $city_bound_lng2;
                    $dummy_post_index = $_REQUEST['insert_dummy_post_index'];
                    $city_bound_lat1 = $_REQUEST['city_bound_lat1'];
                    $city_bound_lng1 = $_REQUEST['city_bound_lng1'];
                    $city_bound_lat2 = $_REQUEST['city_bound_lat2'];
                    $city_bound_lng2 = $_REQUEST['city_bound_lng2'];
                    if (isset($_REQUEST['posttype'])) {
                        /**
                         * Used to insert the dummy post data per post type.
                         *
                         * Uses dynamic hook, geodir_insert_dummy_posts_$_REQUEST['posttype'].
                         *
                         * @since 1.0.0
                         */
                        do_action('geodir_insert_dummy_posts_' . $_REQUEST['posttype']);
                    }
                    break;
            }
        } else {
            wp_redirect(home_url() . '/?geodir_signup=true');
            exit;
        }
    }
    if (isset($_REQUEST['geodir_import_data']) && $_REQUEST['geodir_import_data'] != '') {
        if (current_user_can('manage_options')) {
            geodir_import_data();
        } else {
            wp_redirect(home_url() . '/?geodir_signup=true');
            exit;
        }
    }
    if (isset($_REQUEST['popuptype']) && $_REQUEST['popuptype'] != '' && isset($_REQUEST['post_id']) && $_REQUEST['post_id'] != '') {
        if ($_REQUEST['popuptype'] == 'b_send_inquiry' || $_REQUEST['popuptype'] == 'b_sendtofriend') {
            require_once geodir_plugin_path() . '/geodirectory-templates/popup-forms.php';
        }
        exit;
    }
    /*if(isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'filter_ajax'){
          include_once ( geodir_plugin_path() . '/geodirectory-templates/advance-search-form.php');
      }*/
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'map_ajax') {
        /**
         * Contains map marker functions.
         *
         * @since 1.0.0
         * @package GeoDirectory
         */
        include_once geodir_plugin_path() . '/geodirectory-functions/map-functions/get_markers.php';
    }
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'favorite') {
        if (is_user_logged_in()) {
            switch ($_REQUEST['ajax_action']) {
                case "add":
                    geodir_add_to_favorite($_REQUEST['pid']);
                    break;
                case "remove":
                    geodir_remove_from_favorite($_REQUEST['pid']);
                    break;
            }
        } else {
            wp_redirect(home_url() . '/?geodir_signup=true');
            exit;
        }
    }
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'add_listing') {
        $is_current_user_owner = true;
        if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
            $is_current_user_owner = geodir_listing_belong_to_current_user($_REQUEST['pid']);
        }
        $request = isset($_SESSION['listing']) ? unserialize($_SESSION['listing']) : '';
        if (is_user_logged_in() && $is_current_user_owner) {
            switch ($_REQUEST['ajax_action']) {
                case "add":
                case "update":
                    if (isset($request['geodir_spamblocker']) && $request['geodir_spamblocker'] == '64' && isset($request['geodir_filled_by_spam_bot']) && $request['geodir_filled_by_spam_bot'] == '') {
                        $last_id = geodir_save_listing();
                        if ($last_id) {
                            //$redirect_to = get_permalink( $last_id );
                            $redirect_to = geodir_getlink(get_permalink(geodir_success_page_id()), array('pid' => $last_id));
                        } elseif (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
                            $redirect_to = get_permalink(geodir_add_listing_page_id());
                            $redirect_to = geodir_getlink($redirect_to, array('pid' => $post->pid), false);
                        } else {
                            $redirect_to = get_permalink(geodir_add_listing_page_id());
                        }
                        wp_redirect($redirect_to);
                    } else {
                        if (isset($_SESSION['listing'])) {
                            unset($_SESSION['listing']);
                        }
                        wp_redirect(home_url());
                    }
                    break;
                case "cancel":
                    unset($_SESSION['listing']);
                    if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '' && get_permalink($_REQUEST['pid'])) {
                        wp_redirect(get_permalink($_REQUEST['pid']));
                    } else {
                        geodir_remove_temp_images();
                        wp_redirect(geodir_getlink(get_permalink(geodir_add_listing_page_id()), array('listing_type' => $_REQUEST['listing_type'])));
                    }
                    break;
                case "publish":
                    if (isset($request['geodir_spamblocker']) && $request['geodir_spamblocker'] == '64' && isset($request['geodir_filled_by_spam_bot']) && $request['geodir_filled_by_spam_bot'] == '') {
                        if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
                            $new_post = array();
                            $new_post['ID'] = $_REQUEST['pid'];
                            //$new_post['post_status'] = 'publish';
                            $lastid = wp_update_post($new_post);
                            if (isset($_SESSION['listing'])) {
                                unset($_SESSION['listing']);
                            }
                            wp_redirect(get_permalink($lastid));
                        } else {
                            $last_id = geodir_save_listing();
                            if ($last_id) {
                                //$redirect_to = get_permalink( $last_id );
                                $redirect_to = geodir_getlink(get_permalink(geodir_success_page_id()), array('pid' => $last_id));
                            } elseif (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
                                $redirect_to = get_permalink(geodir_add_listing_page_id());
                                $redirect_to = geodir_getlink($redirect_to, array('pid' => $post->pid), false);
                            } else {
                                $redirect_to = get_permalink(geodir_add_listing_page_id());
                            }
                            if (isset($_SESSION['listing'])) {
                                unset($_SESSION['listing']);
                            }
                            wp_redirect($redirect_to);
                        }
                    } else {
                        if (isset($_SESSION['listing'])) {
                            unset($_SESSION['listing']);
                        }
                        wp_redirect(home_url());
                    }
                    break;
                case "delete":
                    if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
                        global $current_user;
                        get_currentuserinfo();
                        $post_type = get_post_type($_REQUEST['pid']);
                        $lastid = wp_delete_post($_REQUEST['pid']);
                        if ($lastid && !is_wp_error($lastid)) {
                            wp_redirect($_SERVER['HTTP_REFERER']);
                        }
                        //wp_redirect( geodir_getlink(get_author_posts_url($current_user->ID),array('geodir_dashbord'=>'true','stype'=>$post_type ),false) );
                    }
                    break;
            }
            if (isset($_SESSION['listing'])) {
                unset($_SESSION['listing']);
            }
        } else {
            if (isset($_SESSION['listing'])) {
                unset($_SESSION['listing']);
            }
            wp_redirect(home_url() . '/?geodir_signup=true');
            exit;
        }
    }
    if (isset($_REQUEST['geodir_ajax']) && $_REQUEST['geodir_ajax'] == 'user_login') {
        /**
         * Contains registration and login functions.
         * @todo Fix the file path.
         *
         * @since 1.0.0
         * @package GeoDirectory
         */
        include_once geodir_plugin_path() . '/geodirectory-functions/geodirectory_reg.php';
    }
    if (isset($_REQUEST['ajax_action']) && $_REQUEST['ajax_action'] == 'geodir_get_term_list') {
        $terms_o = get_terms(sanitize_text_field($_REQUEST['term']));
        // Skip terms which has no listing
        if (!empty($terms_o)) {
            $filter_terms = array();
            foreach ($terms_o as $term) {
                if ($term->count > 0) {
                    $filter_terms[] = $term;
                }
            }
            $terms_o = $filter_terms;
        }
        $terms = geodir_sort_terms($terms_o, 'count');
        geodir_helper_cat_list_output($terms, intval($_REQUEST['limit']));
        exit;
    }
    die;
}