/**
 * 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;
}
Example #2
0
/**
 * Sets a key and value in $wp object if the current page is a geodir page.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @param object $wp WordPress object.
 */
function geodir_set_is_geodir_page($wp)
{
    if (!is_admin()) {
        //$wp->query_vars['gd_is_geodir_page'] = false;
        //print_r()
        if (empty($wp->query_vars) || !array_diff(array_keys($wp->query_vars), array('preview', 'page', 'paged', 'cpage'))) {
            if (get_option('geodir_set_as_home')) {
                $wp->query_vars['gd_is_geodir_page'] = true;
            }
        }
        if (!isset($wp->query_vars['gd_is_geodir_page']) && isset($wp->query_vars['page_id'])) {
            if ($wp->query_vars['page_id'] == geodir_add_listing_page_id() || $wp->query_vars['page_id'] == geodir_preview_page_id() || $wp->query_vars['page_id'] == geodir_success_page_id() || $wp->query_vars['page_id'] == geodir_location_page_id()) {
                $wp->query_vars['gd_is_geodir_page'] = true;
            }
        }
        if (!isset($wp->query_vars['gd_is_geodir_page']) && isset($wp->query_vars['pagename'])) {
            $page = get_page_by_path($wp->query_vars['pagename']);
            if (!empty($page) && ($page->ID == geodir_add_listing_page_id() || $page->ID == geodir_preview_page_id() || $page->ID == geodir_success_page_id() || $page->ID == geodir_location_page_id())) {
                $wp->query_vars['gd_is_geodir_page'] = true;
            }
        }
        if (!isset($wp->query_vars['gd_is_geodir_page']) && isset($wp->query_vars['post_type']) && $wp->query_vars['post_type'] != '') {
            $requested_post_type = $wp->query_vars['post_type'];
            // check if this post type is geodirectory post types
            $post_type_array = geodir_get_posttypes();
            if (in_array($requested_post_type, $post_type_array)) {
                $wp->query_vars['gd_is_geodir_page'] = true;
            }
        }
        if (!isset($wp->query_vars['gd_is_geodir_page'])) {
            $geodir_taxonomis = geodir_get_taxonomies('', true);
            foreach ($geodir_taxonomis as $taxonomy) {
                if (array_key_exists($taxonomy, $wp->query_vars)) {
                    $wp->query_vars['gd_is_geodir_page'] = true;
                    break;
                }
            }
        }
        if (!isset($wp->query_vars['gd_is_geodir_page']) && isset($wp->query_vars['author_name']) && isset($_REQUEST['geodir_dashbord'])) {
            $wp->query_vars['gd_is_geodir_page'] = true;
        }
        if (!isset($wp->query_vars['gd_is_geodir_page']) && isset($_REQUEST['geodir_search'])) {
            $wp->query_vars['gd_is_geodir_page'] = true;
        }
        //echo $wp->query_vars['gd_is_geodir_page'] ;
        /*echo "<pre>" ;
        		print_r($wp) ;
        		echo "</pre>" ;
        	//	exit();
        			*/
    }
    // end of is admin
}
/**
 * Locates template based on the template type.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global string $post_type The post type.
 * @global object $wp WordPress object.
 * @global object $post WordPress post object.
 * @param string $template The template type.
 * @return bool|string The template path.
 */
function geodir_locate_template($template = '')
{
    global $post_type, $wp, $post;
    $fields = array();
    switch ($template) {
        case 'signup':
            return $template = locate_template(array("geodirectory/geodir-signup.php"));
            break;
        case 'add-listing':
            $sc_post_type = '';
            if (is_page() && isset($post->post_content) && has_shortcode($post->post_content, 'gd_add_listing')) {
                $listing_page_id = $post->ID;
                $regex_pattern = get_shortcode_regex();
                preg_match('/' . $regex_pattern . '/s', $post->post_content, $regex_matches);
                if (!empty($regex_matches) && isset($regex_matches[2]) == 'gd_add_listing' && isset($regex_matches[3])) {
                    $shortcode_atts = shortcode_parse_atts($regex_matches[3]);
                    $sc_post_type = !empty($shortcode_atts) && isset($shortcode_atts['listing_type']) && !empty($shortcode_atts['listing_type']) ? $shortcode_atts['listing_type'] : '';
                }
            } else {
                $listing_page_id = geodir_add_listing_page_id();
            }
            $is_wpml = function_exists('icl_object_id') ? true : false;
            if ($listing_page_id != '' && (is_page($listing_page_id) || $is_wpml && !empty($wp->query_vars['page_id'])) && isset($_REQUEST['listing_type']) && in_array($_REQUEST['listing_type'], geodir_get_posttypes())) {
                $post_type = $_REQUEST['listing_type'];
            }
            if (empty($post_type) && !isset($_REQUEST['pid'])) {
                $pagename = $wp->query_vars['pagename'];
                $post_types = geodir_get_posttypes();
                if (!empty($post_types)) {
                    $post_type = $post_types[0];
                }
                if ($sc_post_type != '') {
                    $post_type = $sc_post_type;
                }
                if ($is_wpml && !empty($wp->query_vars['page_id'])) {
                    wp_redirect(geodir_getlink(get_permalink($wp->query_vars['page_id']), array('listing_type' => $post_type)));
                } else {
                    wp_redirect(trailingslashit(get_site_url()) . $pagename . '/?listing_type=' . $post_type);
                }
                exit;
            }
            return $template = locate_template(array("geodirectory/add-{$post_type}.php", "geodirectory/add-listing.php"));
            break;
        case 'success':
            $success_page_id = geodir_success_page_id();
            if ($success_page_id != '' && is_page($success_page_id) && isset($_REQUEST['listing_type']) && in_array($_REQUEST['listing_type'], geodir_get_posttypes())) {
                $post_type = $_REQUEST['listing_type'];
            }
            return $template = locate_template(array("geodirectory/{$post_type}-success.php", "geodirectory/listing-success.php"));
            break;
        case 'detail':
        case 'preview':
            if (in_array(get_post_type(), geodir_get_posttypes())) {
                $post_type = get_post_type();
            }
            return $template = locate_template(array("geodirectory/single-{$post_type}.php", "geodirectory/listing-detail.php"));
            break;
        case 'listing':
            $templates = array();
            if (is_post_type_archive() && in_array(get_post_type(), geodir_get_posttypes())) {
                $post_type = get_post_type();
                $templates[] = "geodirectory/archive-{$post_type}.php";
            }
            if (is_tax() && geodir_get_taxonomy_posttype()) {
                $query_obj = get_queried_object();
                $curr_taxonomy = isset($query_obj->taxonomy) ? $query_obj->taxonomy : '';
                $curr_term = isset($query_obj->slug) ? $query_obj->slug : '';
                $templates[] = "geodirectory/taxonomy-{$curr_taxonomy}-{$curr_term}.php";
                $templates[] = "geodirectory/taxonomy-{$curr_taxonomy}.php";
            }
            $templates[] = "geodirectory/geodir-listing.php";
            return $template = locate_template($templates);
            break;
        case 'information':
            return $template = locate_template(array("geodirectory/geodir-information.php"));
            break;
        case 'author':
            return $template = locate_template(array("geodirectory/geodir-author.php"));
            break;
        case 'search':
            return $template = locate_template(array("geodirectory/geodir-search.php"));
            break;
        case 'location':
            return $template = locate_template(array("geodirectory/geodir-location.php"));
            break;
        case 'geodir-home':
            return $template = locate_template(array("geodirectory/geodir-home.php"));
            break;
        case 'listing-listview':
            $template = locate_template(array("geodirectory/listing-listview.php"));
            if (!$template) {
                $template = geodir_plugin_path() . '/geodirectory-templates/listing-listview.php';
            }
            return $template;
            break;
        case 'widget-listing-listview':
            $template = locate_template(array("geodirectory/widget-listing-listview.php"));
            if (!$template) {
                $template = geodir_plugin_path() . '/geodirectory-templates/widget-listing-listview.php';
            }
            return $template;
            break;
    }
    return false;
}