/**
 * Loads template based on the current page.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wp_query WordPress Query object.
 * @todo $wp_query declared twice - fix it.
 * @global object $post The current post object.
 * @global object $current_user Current user object.
 * @param string $template The template path.
 * @return bool|string The template path.
 */
function geodir_template_loader($template)
{
    global $wp_query;
    /**
     * Filter the custom page list.
     *
     * @since 1.0.0
     */
    $geodir_custom_page_list = apply_filters('geodir_set_custom_pages', array('geodir_signup_page' => apply_filters('geodir_set_custom_signup_page', false), 'geodir_add_listing_page' => apply_filters('geodir_set_custom_add_listing_page', false), 'geodir_preview_page' => apply_filters('geodir_set_custom_preview_page', false), 'geodir_listing_success_page' => apply_filters('geodir_set_custom_listing_success_page', false), 'geodir_listing_detail_page' => apply_filters('geodir_set_custom_listing_detail_page', false), 'geodir_listing_page' => apply_filters('geodir_set_custom_listing_page', false), 'geodir_search_page' => apply_filters('geodir_set_custom_search_page', false), 'geodir_author_page' => apply_filters('geodir_set_custom_author_page', false), 'geodir_home_map_page' => apply_filters('geodir_set_custom_home_map_page', false)));
    if (geodir_is_page('login') || $geodir_custom_page_list['geodir_signup_page']) {
        $template = geodir_locate_template('signup');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/geodir-signup.php';
        }
        /**
         * Filter the signup template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_signup', $template);
    }
    if (geodir_is_page('add-listing') || $geodir_custom_page_list['geodir_add_listing_page']) {
        if (!geodir_is_default_location_set()) {
            global $information;
            $information = sprintf(__('Please %sclick here%s to set a default location, this will make the plugin work properly.', 'geodirectory'), '<a href=\'' . admin_url('admin.php?page=geodirectory&tab=default_location_settings') . '\'>', '</a>');
            $template = geodir_locate_template('information');
            if (!$template) {
                $template = geodir_plugin_path() . '/geodirectory-templates/geodir-information.php';
            }
            /**
             * Filter the information template path.
             *
             * @since 1.0.0
             * @param string $template The template path.
             */
            return $template = apply_filters('geodir_template_information', $template);
        }
        // check if pid exists in the record if yes then check if this post belongs to the user who is logged in.
        if (isset($_REQUEST['pid']) && $_REQUEST['pid'] != '') {
            global $information;
            $information = __('This listing does not belong to your account, please check the listing id carefully.', 'geodirectory');
            $is_current_user_owner = geodir_listing_belong_to_current_user();
            if (!$is_current_user_owner) {
                $template = geodir_locate_template('information');
                if (!$template) {
                    $template = geodir_plugin_path() . '/geodirectory-templates/geodir-information.php';
                }
                /**
                 * Filter the information template path.
                 *
                 * @since 1.0.0
                 * @param string $template The template path.
                 */
                return $template = apply_filters('geodir_template_information', $template);
            }
        }
        //geodir_is_login(true);
        global $current_user;
        if (!$current_user->ID) {
            wp_redirect(geodir_login_url(array('redirect_add_listing' => urlencode(geodir_curPageURL()))), 302);
            exit;
        }
        $template = geodir_locate_template('add-listing');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/add-listing.php';
        }
        /**
         * Filter the add listing template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_add_listing', $template);
    }
    if (geodir_is_page('preview') || $geodir_custom_page_list['geodir_preview_page']) {
        global $preview;
        $preview = true;
        $template = geodir_locate_template('preview');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/listing-detail.php';
        }
        /**
         * Filter the preview template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_preview', $template);
    }
    if (geodir_is_page('listing-success') || $geodir_custom_page_list['geodir_listing_success_page']) {
        $template = geodir_locate_template('success');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/listing-success.php';
        }
        /**
         * Filter the success template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_success', $template);
    }
    if (geodir_is_page('detail') || $geodir_custom_page_list['geodir_listing_detail_page']) {
        $template = geodir_locate_template('detail');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/listing-detail.php';
        }
        /**
         * Filter the detail template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_detail', $template);
    }
    if (geodir_is_page('listing') || $geodir_custom_page_list['geodir_listing_page']) {
        $template = geodir_locate_template('listing');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/geodir-listing.php';
        }
        /**
         * Filter the listing template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_listing', $template);
    }
    if (geodir_is_page('search') || $geodir_custom_page_list['geodir_search_page']) {
        $template = geodir_locate_template('search');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/geodir-search.php';
        }
        /**
         * Filter the search template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_search', $template);
    }
    if (geodir_is_page('author') || $geodir_custom_page_list['geodir_author_page']) {
        $template = geodir_locate_template('author');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/geodir-author.php';
        }
        /**
         * Filter the author template path.
         *
         * @since 1.0.0
         * @param string $template The template path.
         */
        return $template = apply_filters('geodir_template_author', $template);
    }
    if (get_option('geodir_set_as_home') || geodir_is_page('home') || geodir_is_page('location')) {
        global $post, $wp_query;
        if (geodir_is_page('home') || 'page' == get_option('show_on_front') && isset($post->ID) && $post->ID == get_option('page_on_front') || is_home() && !$wp_query->is_posts_page) {
            $template = geodir_locate_template('geodir-home');
            if (!$template) {
                $template = geodir_plugin_path() . '/geodirectory-templates/geodir-home.php';
            }
            /**
             * Filter the home page template path.
             *
             * @since 1.0.0
             * @param string $template The template path.
             */
            return $template = apply_filters('geodir_template_homepage', $template);
        } elseif (geodir_is_page('location')) {
            $template = geodir_locate_template('location');
            if (!$template) {
                $template = geodir_plugin_path() . '/geodirectory-templates/geodir-location.php';
            }
            /**
             * Filter the location template path.
             *
             * @since 1.0.0
             * @param string $template The template path.
             */
            return $template = apply_filters('geodir_template_location', $template);
        } else {
            return $template;
        }
    }
    return $template;
}
    /**
     * GeoDirectory dummy data installation.
     *
     * @since 1.0.0
     * @package GeoDirectory
     * @global object $wpdb WordPress Database object.
     * @global string $plugin_prefix Geodirectory plugin table prefix.
     * @param string $post_type The post type.
     */
    function geodir_autoinstall_admin_header($post_type = 'gd_place')
    {
        global $wpdb, $plugin_prefix;
        if (!geodir_is_default_location_set()) {
            echo '<div class="updated fade"><p><strong>' . sprintf(__('Please %sclick here%s to set a default location, this will help to set location of all dummy data.', GEODIRECTORY_TEXTDOMAIN), '<a href=\'' . admin_url('admin.php?page=geodirectory&tab=default_location_settings') . '\'>', '</a>') . '</strong></p></div>';
        } else {
            $geodir_url = admin_url() . 'admin.php?page=geodirectory&tab=general_settings&active_tab=';
            $post_counts = $wpdb->get_var("SELECT count(post_id) FROM " . $plugin_prefix . $post_type . "_detail WHERE post_dummy='1'");
            if ($post_counts > 0) {
                $nonce = wp_create_nonce('geodir_dummy_posts_delete_noncename');
                $dummy_msg = '<div id="" class="geodir_auto_install updated highlight fade"><p><b>' . SAMPLE_DATA_SHOW_MSG . '</b><br /><a id="geodir_dummy_delete" class="button_delete" onclick="geodir_autoinstall(this,\'geodir_dummy_delete\',\'' . $nonce . '\',\'' . $post_type . '\')" href="javascript:void(0);" redirect_to="' . $geodir_url . '"  >' . DELETE_BTN_SAMPLE_MSG . '</a></p></div>';
                $dummy_msg .= '<div id="" style="display:none;" class="geodir_show_progress updated highlight fade"><p><b>' . GEODIR_SAMPLE_DATA_DELETE_MSG . '</b><br><img src="' . geodir_plugin_url() . '/geodirectory-assets/images/loadingAnimation.gif" /></p></div>';
            } else {
                $options_list = '';
                for ($option = 1; $option <= 30; $option++) {
                    $selected = '';
                    if ($option == 10) {
                        $selected = 'selected="selected"';
                    }
                    $options_list .= '<option ' . $selected . ' value="' . $option . '">' . $option . '</option>';
                }
                $nonce = wp_create_nonce('geodir_dummy_posts_insert_noncename');
                $dummy_msg = '<div id="" class="geodir_auto_install updated highlight fade"><p><b>' . AUTO_INSATALL_MSG . '</b><br /><select class="selected_sample_data">' . $options_list . '</select><a id="geodir_dummy_insert" class="button_insert" href="javascript:void(0);" onclick="geodir_autoinstall(this,\'geodir_dummy_insert\',\'' . $nonce . '\',\'' . $post_type . '\')"   redirect_to="' . $geodir_url . '" >' . INSERT_BTN_SAMPLE_MSG . '</a></p></div>';
                $dummy_msg .= '<div id="" style="display:none;" class="geodir_show_progress updated highlight fade"><p><b>' . GEODIR_SAMPLE_DATA_IMPORT_MSG . '</b><br><img src="' . geodir_plugin_url() . '/geodirectory-assets/images/loadingAnimation.gif" /><br><span class="dummy_post_inserted"></span></div>';
            }
            echo $dummy_msg;
            ?>
            <script>
                <?php 
            $default_location = geodir_get_default_location();
            $city = isset($default_location->city) ? $default_location->city : '';
            $region = isset($default_location->region) ? $default_location->region : '';
            $country = isset($default_location->country) ? $default_location->country : '';
            $city_latitude = isset($default_location->city_latitude) ? $default_location->city_latitude : '';
            $city_longitude = isset($default_location->city_longitude) ? $default_location->city_longitude : '';
            ?>
                var geocoder = new google.maps.Geocoder();
                var CITY_ADDRESS = '<?php 
            echo $city . ',' . $region . ',' . $country;
            ?>
';
                var bound_lat_lng;
                var lat = <?php 
            echo $city_latitude;
            ?>
;
                var lng = <?php 
            echo $city_longitude;
            ?>
;
                var latlng = new google.maps.LatLng(lat, lng);
                geocoder.geocode({'address': CITY_ADDRESS},
                    function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            // Bounds for North America
                            //	 alert((results[0].geometry.bounds==null))
                            if (results[0].geometry.bounds == null) {
                                bound_lat_lng1 = String(results[0].geometry.viewport.getSouthWest());
                                bound_lat_lng1 = bound_lat_lng1.replace(/[()]/g, "");

                                bound_lat_lng2 = String(results[0].geometry.viewport.getNorthEast());
                                bound_lat_lng2 = bound_lat_lng2.replace(/[()]/g, "");
                                bound_lat_lng2 = bound_lat_lng1 + "," + bound_lat_lng2;
                                bound_lat_lng = bound_lat_lng2.split(',');
                            }
                            else {
                                bound_lat_lng = String(results[0].geometry.bounds);

                                bound_lat_lng = bound_lat_lng.replace(/[()]/g, "");

                                bound_lat_lng = bound_lat_lng.split(',');
                            }

                            strictBounds = new google.maps.LatLngBounds(
                                new google.maps.LatLng(bound_lat_lng[0], bound_lat_lng[1]),
                                new google.maps.LatLng(bound_lat_lng[2], bound_lat_lng[3])
                            );

                        } else {
                            alert("<?php 
            _e('Geocode was not successful for the following reason:', GEODIRECTORY_TEXTDOMAIN);
            ?>
 " + status);
                        }
                    });


                var dummy_post_index = 1;
                function geodir_autoinstall(obj, id, nonce, posttype) {

                    var active_tab = jQuery(obj).closest('form').find('dl dd.gd-tab-active').attr('id');

                    var total_dummy_post_count = jQuery('#sub_' + active_tab).find('.selected_sample_data').val();

                    if (id == 'geodir_dummy_delete') {
                        if (confirm('<?php 
            _e('Are you sure you want to delete dummy data?', GEODIRECTORY_TEXTDOMAIN);
            ?>
')) {
                            jQuery('#sub_' + active_tab).find('.geodir_auto_install').hide();
                            jQuery('#sub_' + active_tab).find('.geodir_show_progress').show();
                            jQuery.post('<?php 
            echo geodir_get_ajax_url();
            ?>
&geodir_autofill=' + id + '&posttype=' + posttype + '&_wpnonce=' + nonce,
                                function (data) {
                                    window.location.href = jQuery('#' + id).attr('redirect_to') + active_tab;
                                });
                            return true;
                        } else {
                            return false;
                        }
                    }
                    else {

                        jQuery('#sub_' + active_tab).find('.geodir_auto_install').hide();
                        jQuery('#sub_' + active_tab).find('.geodir_show_progress').show();
                        jQuery.post('<?php 
            echo geodir_get_ajax_url();
            ?>
&geodir_autofill=' + id + '&posttype=' + posttype + '&insert_dummy_post_index=' + dummy_post_index + '&city_bound_lat1=' + bound_lat_lng[0] + '&city_bound_lng1=' + bound_lat_lng[1] + '&city_bound_lat2=' + bound_lat_lng[2] + '&city_bound_lng2=' + bound_lat_lng[3] + '&_wpnonce=' + nonce,
                            function (data) {

                                jQuery(obj).closest('form').find('.dummy_post_inserted').html('<?php 
            _e('Dummy post(s) inserted:', GEODIRECTORY_TEXTDOMAIN);
            ?>
 ' + dummy_post_index + ' <?php 
            _e('of', GEODIRECTORY_TEXTDOMAIN);
            ?>
 ' + total_dummy_post_count + '');
                                dummy_post_index++;
                                if (dummy_post_index <= total_dummy_post_count)
                                    geodir_autoinstall(obj, id, nonce, posttype);
                                else {
                                    window.location.href = jQuery('#' + id).attr('redirect_to') + active_tab;
                                }

                            });
                    }

                }
            </script>
        <?php 
        }
    }