示例#1
0
 /**
  * Set post Categories.
  *
  * @since 1.0.0
  * @package GeoDirectory
  * @global object $wpdb WordPress Database object.
  * @global string $plugin_prefix Geodirectory plugin table prefix.
  * @param int $post_id The post ID.
  * @param array $terms An array of term objects.
  * @param array $tt_ids An array of term taxonomy IDs.
  * @param string $taxonomy Taxonomy slug.
  */
 function geodir_set_post_terms($post_id, $terms, $tt_ids, $taxonomy)
 {
     global $wpdb, $plugin_prefix;
     $post_type = get_post_type($post_id);
     $table = $plugin_prefix . $post_type . '_detail';
     if (in_array($post_type, geodir_get_posttypes()) && !wp_is_post_revision($post_id)) {
         if (strstr($taxonomy, 'tag')) {
             if (isset($_POST['action']) && $_POST['action'] == 'inline-save') {
                 geodir_save_post_meta($post_id, 'post_tags', $terms);
             }
         } elseif ($taxonomy == $post_type . 'category') {
             $srcharr = array('"', '\\');
             $replarr = array(""", '');
             $post_obj = get_post($post_id);
             $cat_ids = array('0');
             if (is_array($tt_ids)) {
                 $cat_ids = $tt_ids;
             }
             if (!empty($cat_ids)) {
                 $cat_ids_array = $cat_ids;
                 $cat_ids_length = count($cat_ids_array);
                 $cat_ids_format = array_fill(0, $cat_ids_length, '%d');
                 $format = implode(',', $cat_ids_format);
                 $cat_ids_array_del = $cat_ids_array;
                 $cat_ids_array_del[] = $post_id;
                 $wpdb->get_var($wpdb->prepare("DELETE from " . GEODIR_ICON_TABLE . " WHERE cat_id NOT IN ({$format}) AND post_id = %d ", $cat_ids_array_del));
                 $post_term = $wpdb->get_col($wpdb->prepare("SELECT term_id FROM " . $wpdb->term_taxonomy . " WHERE term_taxonomy_id IN({$format}) GROUP BY term_id", $cat_ids_array));
             }
             $post_marker_json = '';
             if (!empty($post_term)) {
                 foreach ($post_term as $cat_id) {
                     $term_icon_url = get_tax_meta($cat_id, 'ct_cat_icon', false, $post_type);
                     $term_icon = isset($term_icon_url['src']) ? $term_icon_url['src'] : '';
                     $post_title = $post_obj->title;
                     $title = str_replace($srcharr, $replarr, $post_title);
                     $lat = geodir_get_post_meta($post_id, 'post_latitude', true);
                     $lng = geodir_get_post_meta($post_id, 'post_longitude', true);
                     $timing = ' - ' . date('D M j, Y', strtotime(geodir_get_post_meta($post_id, 'st_date', true)));
                     $timing .= ' - ' . geodir_get_post_meta($post_id, 'st_time', true);
                     $json = '{';
                     $json .= '"id":"' . $post_id . '",';
                     $json .= '"lat_pos": "' . $lat . '",';
                     $json .= '"long_pos": "' . $lng . '",';
                     $json .= '"marker_id":"' . $post_id . '_' . $cat_id . '",';
                     $json .= '"icon":"' . $term_icon . '",';
                     $json .= '"group":"catgroup' . $cat_id . '"';
                     $json .= '}';
                     if ($cat_id == geodir_get_post_meta($post_id, 'default_category', true)) {
                         $post_marker_json = $json;
                     }
                     if ($wpdb->get_var($wpdb->prepare("SELECT post_id from " . GEODIR_ICON_TABLE . " WHERE post_id = %d AND cat_id = %d", array($post_id, $cat_id)))) {
                         $json_query = $wpdb->prepare("UPDATE " . GEODIR_ICON_TABLE . " SET \r\n\t\t\t\t\t\t\t\t\t\tpost_title = %s, \r\n\t\t\t\t\t\t\t\t\t\tjson = %s \r\n\t\t\t\t\t\t\t\t\t\tWHERE post_id = %d AND cat_id = %d ", array($post_title, $json, $post_id, $cat_id));
                     } else {
                         $json_query = $wpdb->prepare("INSERT INTO " . GEODIR_ICON_TABLE . " SET \r\n\t\t\t\t\t\t\t\t\t\tpost_id = %d, \r\n\t\t\t\t\t\t\t\t\t\tpost_title = %s, \r\n\t\t\t\t\t\t\t\t\t\tcat_id = %d,\r\n\t\t\t\t\t\t\t\t\t\tjson = %s", array($post_id, $post_title, $cat_id, $json));
                     }
                     $wpdb->query($json_query);
                 }
             }
             if (!empty($post_term) && is_array($post_term)) {
                 $categories = implode(',', $post_term);
                 if ($categories != '' && $categories != 0) {
                     $categories = ',' . $categories . ',';
                 }
                 if (empty($post_marker_json)) {
                     $post_marker_json = isset($json) ? $json : '';
                 }
                 if ($wpdb->get_var($wpdb->prepare("SELECT post_id from " . $table . " where post_id = %d", array($post_id)))) {
                     $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET \r\n\t\t\t\t\t\t\t\t" . $taxonomy . " = %s, \r\n\t\t\t\t\t\t\t\tmarker_json = %s\r\n\t\t\t\t\t\t\t\twhere post_id = %d", array($categories, $post_marker_json, $post_id)));
                     if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'inline-save') {
                         $categories = trim($categories, ',');
                         if ($categories) {
                             $categories = explode(',', $categories);
                             $default_category = geodir_get_post_meta($post_id, 'default_category', true);
                             if (!in_array($default_category, $categories)) {
                                 $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET \r\n\t\t\t\t\t\t\t\t\t\t\tdefault_category = %s\r\n\t\t\t\t\t\t\t\t\t\t\twhere post_id = %d", array($categories[0], $post_id)));
                                 $default_category = $categories[0];
                             }
                             if ($default_category == '') {
                                 $default_category = $categories[0];
                             }
                             geodir_set_postcat_structure($post_id, $taxonomy, $default_category, '');
                         }
                     }
                 } else {
                     $wpdb->query($wpdb->prepare("INSERT INTO " . $table . " SET \r\n\t\t\t\t\t\t\t\tpost_id = %d, \r\n\t\t\t\t\t\t\t\t" . $taxonomy . " = %s,\r\n\t\t\t\t\t\t\t\tmarker_json = %s ", array($post_id, $categories, $post_marker_json)));
                 }
             }
         }
     }
 }
function geodir_manage_claim_listing_actions()
{
    global $wpdb, $plugin_prefix;
    if (isset($_REQUEST['_wpnonce']) && isset($_REQUEST['id']) && $_REQUEST['id'] != '' && current_user_can('manage_options')) {
        if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'claim_action_' . $_REQUEST['id'])) {
            return;
        }
        if (isset($_REQUEST['pagetype']) && $_REQUEST['pagetype'] == 'delete') {
            $pid = $_REQUEST['id'];
            $approvesql = $wpdb->prepare("select * from " . GEODIR_CLAIM_TABLE . " where pid=%d", array($pid));
            $approveinfo = $wpdb->get_results($approvesql);
            $post_id = $approveinfo[0]->list_id;
            $author_id = $approveinfo[0]->user_id;
            $post_id = $approveinfo[0]->list_id;
            $wpdb->query($wpdb->prepare("delete from " . GEODIR_CLAIM_TABLE . " where pid=%d", array($pid)));
            $change_clamed = $wpdb->get_row($wpdb->prepare("select pid from " . GEODIR_CLAIM_TABLE . " where list_id=%s and status='1'", array($post_id)));
            if (!$change_clamed) {
                geodir_save_post_meta($post_id, 'claimed', '0');
            }
            $msg = CLAIM_DELETED_SUCCESS;
            $msg = urlencode($msg);
            $location = admin_url('admin.php?page=geodirectory&tab=claimlisting_fields&subtab=manage_geodir_claim_listing&claim_success=' . $msg);
            wp_redirect($location);
            exit;
        }
        if (isset($_REQUEST['pagetype']) && $_REQUEST['pagetype'] == 'approve') {
            $pid = $_REQUEST['id'];
            $approvesql = $wpdb->prepare("select * from " . GEODIR_CLAIM_TABLE . " where pid=%d", array($pid));
            $approveinfo = $wpdb->get_results($approvesql);
            $post_id = $approveinfo[0]->list_id;
            $author_id = $approveinfo[0]->user_id;
            $wpdb->query($wpdb->prepare("update " . GEODIR_CLAIM_TABLE . " set status='2' where list_id=%s and user_id!=%s and status='1'", array($post_id, $author_id)));
            geodir_save_post_meta($post_id, 'claimed', '1');
            $wpdb->query($wpdb->prepare("update {$wpdb->posts} set post_author=%d where ID=%d", array($author_id, $post_id)));
            $wpdb->query($wpdb->prepare("update " . GEODIR_CLAIM_TABLE . " set status='1' where pid=%d", array($pid)));
            // Force to upgrade to complete claim listing
            $force_upgrade = geodir_claim_force_upgrade();
            $package_list = geodir_claim_payment_package_list(get_post_type($post_id));
            if ($force_upgrade && !empty($package_list) && !empty($approveinfo) && isset($approveinfo[0]->upgrade_pkg_id)) {
                $geodir_upgrade_pkg = $approveinfo[0]->upgrade_pkg_id;
                $package_info = geodir_get_package_info_by_id($geodir_upgrade_pkg);
                if (!empty($package_info)) {
                    $claim_post_info = array();
                    $claim_post_info['package_id'] = $geodir_upgrade_pkg;
                    geodir_save_listing_payment($post_id, $claim_post_info);
                    $wpdb->query($wpdb->prepare("UPDATE `" . GEODIR_CLAIM_TABLE . "` SET `upgrade_pkg_id`='' WHERE `pid`=%d", array($pid)));
                }
            }
            geodir_clientEmail($post_id, $author_id, 'claim_approved');
            /* email to client*/
            $msg = CLAIM_APPROVE_SUCCESS;
            $msg = urlencode($msg);
            $location = admin_url('admin.php?page=geodirectory&tab=claimlisting_fields&subtab=manage_geodir_claim_listing&claim_success=' . $msg);
            wp_redirect($location);
            exit;
        }
        if (isset($_REQUEST['pagetype']) && $_REQUEST['pagetype'] == 'reject') {
            $pid = $_REQUEST['id'];
            $wpdb->query($wpdb->prepare("update " . GEODIR_CLAIM_TABLE . " set status='2' where pid=%d", array($pid)));
            $approvesql = $wpdb->prepare("select * from " . GEODIR_CLAIM_TABLE . " where pid=%d", array($pid));
            $approveinfo = $wpdb->get_results($approvesql);
            $post_id = $approveinfo[0]->list_id;
            $author_id = $approveinfo[0]->user_id;
            geodir_clientEmail($post_id, $author_id, 'claim_rejected');
            /* email to client*/
            $msg = CLAIM_REJECT_SUCCESS;
            $msg = urlencode($msg);
            $location = admin_url('admin.php?page=geodirectory&tab=claimlisting_fields&subtab=manage_geodir_claim_listing&claim_success=' . $msg);
            wp_redirect($location);
            exit;
        }
        if (isset($_REQUEST['pagetype']) && $_REQUEST['pagetype'] == 'undo') {
            $pid = $_REQUEST['id'];
            $approvesql = $wpdb->prepare("select * from " . GEODIR_CLAIM_TABLE . " where pid=%d", array($pid));
            $approveinfo = $wpdb->get_results($approvesql);
            $post_id = $approveinfo[0]->list_id;
            $author_id = $approveinfo[0]->org_authorid;
            $wpdb->query($wpdb->prepare("update {$wpdb->posts} set post_author=%d where ID=%d", array($author_id, $post_id)));
            $wpdb->query($wpdb->prepare("update " . GEODIR_CLAIM_TABLE . " set status='2' where pid=%d", array($pid)));
            $change_clamed = $wpdb->get_row($wpdb->prepare("select pid from " . GEODIR_CLAIM_TABLE . " where list_id=%s and status='1'", array($post_id)));
            if (!$change_clamed) {
                geodir_save_post_meta($post_id, 'claimed', '0');
                /*update claimed post data.*/
            }
            $location = admin_url('admin.php?page=geodirectory&tab=claimlisting_fields&subtab=manage_geodir_claim_listing&msg=reject');
            wp_redirect($location);
            exit;
        }
    } else {
        wp_redirect(home_url() . '/?geodir_signup=true');
        exit;
    }
}
function geodir_expire_check()
{
    global $wpdb, $plugin_prefix;
    $current_date = date('Y-m-d');
    $geodir_postypes = geodir_get_posttypes();
    $upload_dir = wp_upload_dir();
    $upload_basedir = $upload_dir['basedir'];
    if (get_option('geodir_listing_expiry')) {
        foreach ($geodir_postypes as $post) {
            $table = $plugin_prefix . $post . '_detail';
            if (get_option('geodir_listing_preexpiry_notice_disable')) {
                $number_of_grace_days = get_option('geodir_listing_preexpiry_notice_days');
                if ($number_of_grace_days == '') {
                    $number_of_grace_days = 1;
                }
                $today = date('Y-m-d', strtotime(date('Y-m-d') . "+" . (int) $number_of_grace_days . " days"));
                $strtoday = $wpdb->get_var("SELECT UNIX_TIMESTAMP( STR_TO_DATE( '" . $today . "','%Y-%m-%d'))");
                $postid_str = $wpdb->get_results($wpdb->prepare("SELECT p.ID, p.post_author, p.post_title from " . $table . " detail, " . $wpdb->posts . " p WHERE p.ID=detail.post_id AND detail.expire_date!='Never' AND detail.expire_date!='' AND detail.expire_notification='false' AND unix_timestamp(detail.expire_date)<=%s", array($strtoday)));
                if (!empty($postid_str)) {
                    foreach ($postid_str as $postid_str_obj) {
                        geodir_payment_clientEmail($postid_str_obj->ID, $postid_str_obj->post_author, 'expiration');
                        $wpdb->query($wpdb->prepare("update " . $table . " set expire_notification='true' where post_id=%d", array($postid_str_obj->ID)));
                    }
                }
            }
            $strcurrent = $wpdb->get_var("SELECT UNIX_TIMESTAMP( STR_TO_DATE( '" . $current_date . "','%Y-%m-%d'))");
            $postid_str = $wpdb->get_results($wpdb->prepare("SELECT p.ID, p.post_author, p.post_title, detail.package_id from " . $table . " detail, " . $wpdb->posts . " p WHERE p.ID=detail.post_id AND detail.expire_date!='Never' AND detail.expire_date!='' AND unix_timestamp(detail.expire_date)<=%s", array($strcurrent)));
            if (!empty($postid_str)) {
                foreach ($postid_str as $postid_str_obj) {
                    $post_id = $postid_str_obj->ID;
                    $package_id = $postid_str_obj->package_id;
                    $old_package_info = geodir_get_package_info($package_id);
                    $old_image_limit = empty($old_package_info->image_limit) ? 0 : $old_package_info->image_limit;
                    $old_cat_limit = empty($old_package_info->cat_limit) ? 0 : $old_package_info->cat_limit;
                    $downgrade_pkg = $old_package_info->downgrade_pkg;
                    $package_info = (int) $downgrade_pkg > 0 ? geodir_get_package_info($downgrade_pkg) : array();
                    if ((int) $downgrade_pkg > 0 && $downgrade_pkg != '' && !empty($package_info)) {
                        $featured = $package_info->is_featured;
                        $image_limit = empty($package_info->image_limit) ? 0 : $package_info->image_limit;
                        $cat_limit = empty($package_info->cat_limit) ? 0 : $package_info->cat_limit;
                        $days = $package_info->days;
                        $exclude_cats = $package_info->cat;
                        if ($cat_limit != 0 && $cat_limit < $old_cat_limit) {
                            $terms = wp_get_post_terms($post_id, $post . 'category', array("fields" => "all"));
                            $term_ids = array();
                            foreach ($terms as $termsObj) {
                                if ($termsObj->parent == 0) {
                                    $term_ids[] = $termsObj->term_id;
                                }
                            }
                            $cat_arr = array_slice($term_ids, 0, $cat_limit);
                            $term_ids = implode(",", $cat_arr);
                            wp_set_object_terms($post_id, $cat_arr, $post . 'category');
                            $post_default_category = geodir_get_post_meta($post_id, 'default_category');
                            if ($post_default_category != '' && !in_array($post_default_category, $cat_arr)) {
                                $post_default_category = $cat_arr[0];
                                geodir_save_post_meta($post_id, 'default_category', $post_default_category);
                            }
                            geodir_set_postcat_structure($post_id, $post . 'category', $post_default_category, '');
                        }
                        $post_images = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE `post_id`=%d order by menu_order asc", array($post_id)));
                        $count_post_images = count($post_images);
                        if ($image_limit != 0 && $image_limit < $old_image_limit && $count_post_images > $image_limit) {
                            $post_images_arr = array_slice($post_images, $image_limit, $image_limit);
                            foreach ($post_images_arr as $perimage) {
                                if (file_exists($upload_basedir . $perimage->file)) {
                                    unlink($upload_basedir . $perimage->file);
                                }
                                $wpdb->query($wpdb->prepare("DELETE FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE ID=%d", array($perimage->ID)));
                            }
                        }
                        $expire_date = 'Never';
                        if ((int) $days > 0 && $days != '') {
                            $expire_date = date('Y-m-d', strtotime(date('Y-m-d') . "+" . (int) $days . " days"));
                        }
                        geodir_save_post_meta($post_id, 'is_featured', $featured);
                        geodir_save_post_meta($post_id, 'package_id', $downgrade_pkg);
                        geodir_save_post_meta($post_id, 'paid_amount', '');
                        geodir_save_post_meta($post_id, 'paymentmethod', '');
                        geodir_save_post_meta($post_id, 'alive_days', $days);
                        geodir_save_post_meta($post_id, 'expire_date', $expire_date);
                        geodir_save_post_meta($post_id, 'expire_notification', 'false');
                        $post_info = get_post($post_id);
                        if (!empty($post_info) && isset($post_info->post_status) && $post_info->post_status != 'publish') {
                            $update_post = array();
                            $update_post['post_status'] = 'publish';
                            $update_post['ID'] = $post_id;
                            wp_update_post($update_post);
                        }
                    } else {
                        $post_info = get_post($post_id);
                        $post_ex_status = get_option('geodir_listing_ex_status');
                        if (!empty($post_info) && isset($post_info->post_status) && $post_info->post_status != $post_ex_status) {
                            $expire_post = array();
                            $expire_post['post_status'] = $post_ex_status;
                            $expire_post['ID'] = $post_id;
                            wp_update_post($expire_post);
                            // update post expiry status
                        }
                    }
                }
            }
        }
    }
}
function geodir_save_listing_package_fields($post_id = '', $request_info)
{
    global $wpdb;
    $package_id = isset($request_info['package_id']) ? $request_info['package_id'] : '';
    if (!$post_id || !$package_id) {
        return;
    }
    $package_info = (array) geodir_get_package_info($package_id);
    $post_info = geodir_get_post_info($post_id);
    $post_type = $post_info->post_type;
    $post_category = $post_type . 'category';
    //print_r($package_info);
    //print_r($post_info);
    // check for excluded cats
    if ($package_info['cat']) {
        // only run if there are excluded cats
        $cur_cats = array_unique(array_filter(explode(",", $post_info->{$post_category})));
        $ex_cats = array_filter(explode(",", $package_info['cat']));
        foreach ($cur_cats as $key => $value) {
            if (in_array($value, $ex_cats)) {
                unset($cur_cats[$key]);
            }
        }
        //print_r($cur_cats);
        $cur_cats = array_map('intval', $cur_cats);
        // this was being treated as a string so we convert to int.
        $cur_cats_str = !empty($cur_cats) ? implode(',', $cur_cats) : '';
        $term_taxonomy_ids = wp_set_object_terms($post_id, $cur_cats, $post_category);
        geodir_save_post_meta($post_id, $post_category, $cur_cats_str);
        // check if defualt cat is excluded and if so chane it
        $default_cat = $post_info->default_category;
        if ($default_cat && in_array($default_cat, $ex_cats)) {
            geodir_save_post_meta($post_id, 'default_category', $cur_cats[0]);
        }
    }
    // check if featured only if not in admin
    if (!is_admin()) {
        if ($package_info['is_featured'] != $post_info->is_featured) {
            geodir_save_post_meta($post_id, 'is_featured', $package_info['is_featured']);
        }
    }
    // check image limit
    if ($package_info['image_limit'] != '') {
        $image_limit = $package_info['image_limit'];
        $post_images = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE `post_id`=%d order by menu_order asc", array($post_id)));
        //print_r($post_images);
        $count_post_images = count($post_images);
        if ($count_post_images > $image_limit) {
            if ($image_limit >= '1') {
                foreach ($post_images as $key => $perimage) {
                    // move featured image to the end of the arr so it's not removed
                    if ($post_info->featured_image == $perimage->file) {
                        unset($post_images[$key]);
                        $post_images[] = $perimage;
                    }
                }
            }
            $post_images_arr = array_slice($post_images, 0, $count_post_images - $image_limit);
            $upload_dir = wp_upload_dir();
            $upload_basedir = $upload_dir['basedir'];
            foreach ($post_images_arr as $perimage) {
                if (file_exists($upload_basedir . $perimage->file)) {
                    unlink($upload_basedir . $perimage->file);
                }
                $wpdb->query($wpdb->prepare("DELETE FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE ID=%d", array($perimage->ID)));
                if ($post_info->featured_image == $perimage->file) {
                    geodir_save_post_meta($post_id, 'featured_image', '');
                }
            }
        }
    }
    // check if there is a category limit
    if ($package_info['cat_limit'] != '') {
        $cur_cats = array_unique(array_filter(explode(",", $post_info->{$post_category})));
        $cat_limit = (int) $package_info['cat_limit'];
        if (count($cur_cats) > $cat_limit) {
            $default_category = (int) $post_info->default_category > 0 ? (int) $post_info->default_category : $cur_cats[0];
            $count = 0;
            $new_cur_cats = array();
            foreach ($cur_cats as $cat_id) {
                $new_cur_cats[] = (int) $cat_id;
                $count++;
                if ($count >= $cat_limit) {
                    break;
                }
            }
            if ($default_category && !in_array($default_category, $new_cur_cats)) {
                $new_cur_cats[$cat_limit - 1] = $default_category;
            }
            $cur_cats_str = !empty($new_cur_cats) ? implode(',', $new_cur_cats) : '';
            $term_taxonomy_ids = wp_set_object_terms($post_id, $new_cur_cats, $post_category);
            geodir_save_post_meta($post_id, $post_category, $cur_cats_str);
            $post_cat_str = '';
            if (!empty($new_cur_cats)) {
                $post_cat_str = '#' . implode(",y:#", $new_cur_cats) . ',y:';
                $post_cat_str = str_replace('#' . $default_category . ',y', '#' . $default_category . ',y,d', $post_cat_str);
                $post_cat_str = ltrim($post_cat_str, '#');
                $post_cat_str = array($post_category => $post_cat_str);
            }
            geodir_set_postcat_structure($post_id, $post_category, $default_category, $post_cat_str);
        }
    }
    // check custom fields
    $custom_fields = geodir_post_custom_fields('', 'all', $post_type);
    //print_r($custom_fields);
    if (!empty($custom_fields)) {
        foreach ($custom_fields as $key => $val) {
            $id = $val['id'];
            $label = $val['label'];
            $is_default = $val['is_default'];
            $is_admin = $val['is_admin'];
            $field_type = $val['field_type'];
            $packages = array();
            $packages = array_unique(array_filter(explode(",", $val['packages'])));
            if (!($field_type == 'address' && $is_admin == '1') && !($field_type == 'taxonomy' && $is_admin == '1') && $val['for_admin_use'] != '1') {
                if (in_array($package_id, $packages)) {
                    // if active for this package then dont change
                } else {
                    // if not active in this package then blank
                    geodir_save_post_meta($post_id, $val['name'], '');
                }
            }
        }
    }
}
function geodir_claim_template_loader($template)
{
    global $wp_query, $current_user, $plugin_prefix, $wpdb;
    if (isset($_REQUEST['geodir_ptype'])) {
        global $information;
        if ($current_user->ID) {
            if (get_option('geodir_claim_auto_approve') == 'yes') {
                if (!isset($_REQUEST['rs']) || isset($_REQUEST['rs']) && $_REQUEST['rs'] == '') {
                    $information .= CLAIM_VARIFY_CODE_NOT_EXIST;
                } else {
                    $rand_string = isset($_REQUEST['rs']) ? $_REQUEST['rs'] : '';
                    $approvesql = $wpdb->prepare("select * from " . GEODIR_CLAIM_TABLE . " where rand_string=%s", array($rand_string));
                    $approveinfo = $wpdb->get_results($approvesql);
                    if ($approveinfo) {
                        $pid = $approveinfo[0]->pid;
                        $post_id = $approveinfo[0]->list_id;
                        $author_id = $approveinfo[0]->user_id;
                        $user_id = $current_user->ID;
                        $status = $approveinfo[0]->status;
                        if ($author_id == $user_id) {
                            if ($status == 1) {
                                $information .= CLAIM_LISTING_ALREADY_VARIFIED;
                            } elseif ($status == 2) {
                                $information .= CLAIM_LISTING_VERIFICATION_REJECTED;
                            } else {
                                $wpdb->query($wpdb->prepare("update {$wpdb->posts} set post_author=%d where ID=%d", array($author_id, $post_id)));
                                $wpdb->query($wpdb->prepare("update " . GEODIR_CLAIM_TABLE . " set status='1' where pid=%d", array($pid)));
                                geodir_save_post_meta($post_id, 'claimed', '1');
                                geodir_clientEmail($post_id, $author_id, 'claim_approved');
                                /* email to client */
                                $information .= CLAIM_LISTING_SUCCESS_VERIFIED . ' <a href="' . get_option('siteurl') . '/?p=' . $post_id . '">' . $approveinfo[0]->list_title . '</a>';
                            }
                        } else {
                            $information .= CLAIM_VARIFY_CODE_NOT_MATCH;
                        }
                    } else {
                        $information .= CLAIM_VARIFY_CODE_NOT_EXIST;
                    }
                }
            } else {
                $information .= CLAIM_AUTO_VARIFY_DISABLE;
            }
        } else {
            $site_login_url = get_option('siteurl') . '?geodir_signup=true';
            $information .= sprintf(CLAIM_LOGIN_REQUIRED, '<a href="' . $site_login_url . '" >');
        }
        $template = geodir_locate_template('information');
        if (!$template) {
            $template = geodir_plugin_path() . '/geodirectory-templates/geodir-information.php';
        }
        $template = apply_filters('geodir_template_information', $template);
    }
    return $template;
}
示例#6
0
 /**
  * Save post file fields
  *
  * @since 1.0.0
  * @since 1.4.7 Added `$extra_fields` parameter.
  * @package GeoDirectory
  * @global object $wpdb WordPress Database object.
  * @global string $plugin_prefix Geodirectory plugin table prefix.
  * @global object $current_user Current user object.
  * @param int $post_id
  * @param string $field_id
  * @param array $post_image
  * @param array $extra_fields Array of extra fields.
  */
 function geodir_save_post_file_fields($post_id = 0, $field_id = '', $post_image = array(), $extra_fields = array())
 {
     global $wpdb, $plugin_prefix, $current_user;
     $post_type = get_post_type($post_id);
     //echo $field_id; exit;
     $table = $plugin_prefix . $post_type . '_detail';
     $postcurr_images = array();
     $postcurr_images = geodir_get_post_meta($post_id, $field_id, true);
     $file_urls = '';
     if (!empty($post_image)) {
         $invalid_files = array();
         //Get and remove all old images of post from database to set by new order
         $geodir_uploaddir = '';
         $uploads = wp_upload_dir();
         $uploads_dir = $uploads['path'];
         $geodir_uploadpath = $uploads['path'];
         $geodir_uploadurl = $uploads['url'];
         $sub_dir = $uploads['subdir'];
         $allowed_file_types = !empty($extra_fields['gd_file_types']) && is_array($extra_fields['gd_file_types']) && !in_array("*", $extra_fields['gd_file_types']) ? $extra_fields['gd_file_types'] : '';
         for ($m = 0; $m < count($post_image); $m++) {
             /* --------- start ------- */
             if (!($find_image = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM " . $table . " WHERE {$field_id} = %s AND post_id = %d", array($post_image[$m], $post_id))))) {
                 $curr_img_url = $post_image[$m];
                 $image_name_arr = explode('/', $curr_img_url);
                 $curr_img_dir = $image_name_arr[count($image_name_arr) - 2];
                 $filename = end($image_name_arr);
                 $img_name_arr = explode('.', $filename);
                 $arr_file_type = wp_check_filetype($filename);
                 if (empty($arr_file_type['ext']) || empty($arr_file_type['type'])) {
                     continue;
                 }
                 $uploaded_file_type = $arr_file_type['type'];
                 $uploaded_file_ext = $arr_file_type['ext'];
                 if (!empty($allowed_file_types) && !in_array($uploaded_file_ext, $allowed_file_types)) {
                     continue;
                     // Invalid file type.
                 }
                 // Set an array containing a list of acceptable formats
                 //$allowed_file_types = array('image/jpg', 'image/jpeg', 'image/gif', 'image/png', 'application/pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv', 'text/plain');
                 if (!function_exists('wp_handle_upload')) {
                     require_once ABSPATH . 'wp-admin/includes/file.php';
                 }
                 if (!is_dir($geodir_uploadpath)) {
                     mkdir($geodir_uploadpath);
                 }
                 $new_name = $post_id . '_' . $field_id . '_' . $img_name_arr[0] . '.' . $img_name_arr[1];
                 $explode_sub_dir = explode("/", $sub_dir);
                 if ($curr_img_dir == end($explode_sub_dir)) {
                     $img_path = $geodir_uploadpath . '/' . $filename;
                     $img_url = $geodir_uploadurl . '/' . $filename;
                 } else {
                     $img_path = $uploads_dir . '/temp_' . $current_user->data->ID . '/' . $filename;
                     $img_url = $uploads['url'] . '/temp_' . $current_user->data->ID . '/' . $filename;
                 }
                 $uploaded_file = '';
                 if (file_exists($img_path)) {
                     $uploaded_file = copy($img_path, $geodir_uploadpath . '/' . $new_name);
                 }
                 if ($curr_img_dir != $geodir_uploaddir) {
                     if (file_exists($img_path)) {
                         unlink($img_path);
                     }
                 }
                 if (!empty($uploaded_file)) {
                     $file_urls = $geodir_uploadurl . '/' . $new_name;
                 }
             } else {
                 $file_urls = $post_image[$m];
             }
         }
     }
     //Remove all old attachments and temp images
     if (!empty($postcurr_images)) {
         if ($file_urls != $postcurr_images) {
             $invalid_files[] = (object) array('src' => $postcurr_images);
             $invalid_files = (object) $invalid_files;
         }
     }
     geodir_save_post_meta($post_id, $field_id, $file_urls);
     if (!empty($invalid_files)) {
         geodir_remove_attachments($invalid_files);
     }
 }
/**
 * Handle import/export for categories & listings.
 *
 * @since 1.4.6
 * @package GeoDirectory
 *
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global object $current_user Current user object.
 * @global null|object $wp_filesystem WP_Filesystem object.
 * @return string Json data.
 */
function geodir_ajax_import_export()
{
    global $wpdb, $plugin_prefix, $current_user, $wp_filesystem;
    // try to set higher limits for import
    @ini_set('max_input_time', 3000);
    @ini_set('max_execution_time', 3000);
    @ini_set('memory_limit', '256M');
    error_reporting(0);
    $json = array();
    if (!current_user_can('manage_options')) {
        wp_send_json($json);
    }
    $task = isset($_REQUEST['task']) ? $_REQUEST['task'] : NULL;
    $nonce = isset($_REQUEST['_nonce']) ? $_REQUEST['_nonce'] : NULL;
    $stat = isset($_REQUEST['_st']) ? $_REQUEST['_st'] : false;
    if (!wp_verify_nonce($nonce, 'geodir_import_export_nonce')) {
        wp_send_json($json);
    }
    $post_type = isset($_REQUEST['_pt']) ? $_REQUEST['_pt'] : NULL;
    /*if( empty( $wp_filesystem ) ) {
    		require_once( ABSPATH . '/wp-admin/includes/file.php' );
    		WP_Filesystem();
    		global $wp_filesystem;
    	}*/
    $wp_filesystem = geodir_init_filesystem();
    if (!$wp_filesystem) {
        $json['error'] = __('Fail, something wrong to create csv file.', GEODIRECTORY_TEXTDOMAIN);
        wp_send_json($json);
        exit;
    }
    $csv_file_dir = geodir_path_import_export(false);
    if (!$wp_filesystem->is_dir($csv_file_dir)) {
        $wp_filesystem->mkdir($csv_file_dir, FS_CHMOD_DIR);
    }
    switch ($task) {
        case 'export_posts':
            // WPML
            $is_wpml = geodir_is_wpml();
            if ($is_wpml) {
                global $sitepress;
                $active_lang = ICL_LANGUAGE_CODE;
                $sitepress->switch_lang('all', true);
            }
            // WPML
            if ($post_type == 'gd_event') {
                //add_filter( 'geodir_imex_count_posts', 'geodir_imex_count_events', 10, 2 );
                add_filter('geodir_imex_export_posts_query', 'geodir_imex_get_events_query', 10, 2);
            }
            $file_name = $post_type . '_' . date('j_n_y');
            $posts_count = geodir_get_posts_count($post_type);
            $file_url = geodir_path_import_export() . '/' . $file_name . '.csv';
            $file_path = $csv_file_dir . '/' . $file_name . '.csv';
            $file_path_temp = $csv_file_dir . '/' . $post_type . '_' . $nonce . '.csv';
            if (isset($_REQUEST['_st'])) {
                $line_count = (int) geodir_import_export_line_count($file_path_temp);
                $percentage = count($posts_count) > 0 && $line_count > 0 ? ceil($line_count / $posts_count) * 100 : 0;
                $percentage = min($percentage, 100);
                $json['percentage'] = $percentage;
                // WPML
                if ($is_wpml) {
                    $sitepress->switch_lang($active_lang, true);
                }
                // WPML
                wp_send_json($json);
            } else {
                if ($wp_filesystem->exists($file_path)) {
                    $wp_filesystem->delete($file_path);
                }
                if (!$posts_count > 0) {
                    $json['error'] = __('No records to export.', GEODIRECTORY_TEXTDOMAIN);
                } else {
                    $args = array('hide_empty' => 0);
                    $posts = geodir_imex_get_posts($post_type);
                    if (!empty($posts)) {
                        $total_posts = count($posts);
                        $per_page = 100;
                        $total_pages = ceil($total_posts / $per_page);
                        for ($i = 0; $i <= $total_pages; $i++) {
                            $save_posts = array_slice($posts, $i * $per_page, $per_page);
                            $clear = $i == 0 ? true : false;
                            geodir_save_csv_data($file_path_temp, $save_posts, $clear);
                        }
                        if ($wp_filesystem->exists($file_path_temp)) {
                            $wp_filesystem->move($file_path_temp, $file_path, true);
                        }
                        if ($wp_filesystem->exists($file_path)) {
                            $json['total'] = $posts_count;
                            $json['csv_file'] = $file_url;
                        } else {
                            $json['error'] = __('Fail, something wrong to create csv file.', GEODIRECTORY_TEXTDOMAIN);
                        }
                    } else {
                        $json['error'] = __('No records to export.', GEODIRECTORY_TEXTDOMAIN);
                    }
                }
                // WPML
                if ($is_wpml) {
                    $sitepress->switch_lang($active_lang, true);
                }
                // WPML
                wp_send_json($json);
            }
            break;
        case 'export_cats':
            // WPML
            $is_wpml = geodir_is_wpml();
            if ($is_wpml) {
                global $sitepress;
                $active_lang = ICL_LANGUAGE_CODE;
                $sitepress->switch_lang('all', true);
            }
            // WPML
            $file_name = $post_type . 'category_' . date('j_n_y');
            $terms_count = geodir_get_terms_count($post_type);
            $file_url = geodir_path_import_export() . '/' . $file_name . '.csv';
            $file_path = $csv_file_dir . '/' . $file_name . '.csv';
            $file_path_temp = $csv_file_dir . '/' . $post_type . 'category_' . $nonce . '.csv';
            if (isset($_REQUEST['_st'])) {
                $line_count = (int) geodir_import_export_line_count($file_path_temp);
                $percentage = count($terms_count) > 0 && $line_count > 0 ? ceil($line_count / $terms_count) * 100 : 0;
                $percentage = min($percentage, 100);
                $json['percentage'] = $percentage;
                // WPML
                if ($is_wpml) {
                    $sitepress->switch_lang($active_lang, true);
                }
                // WPML
                wp_send_json($json);
            } else {
                if ($wp_filesystem->exists($file_path)) {
                    $wp_filesystem->delete($file_path);
                }
                if (!$terms_count > 0) {
                    $json['error'] = __('No records to export.', GEODIRECTORY_TEXTDOMAIN);
                } else {
                    $args = array('hide_empty' => 0);
                    $terms = geodir_imex_get_terms($post_type);
                    if (!empty($terms)) {
                        $total_terms = count($terms);
                        $per_page = 50;
                        $total_pages = ceil($total_terms / $per_page);
                        for ($i = 0; $i <= $total_pages; $i++) {
                            $save_terms = array_slice($terms, $i * $per_page, $per_page);
                            $clear = $i == 0 ? true : false;
                            geodir_save_csv_data($file_path_temp, $save_terms, $clear);
                        }
                        if ($wp_filesystem->exists($file_path_temp)) {
                            $wp_filesystem->move($file_path_temp, $file_path, true);
                        }
                        if ($wp_filesystem->exists($file_path)) {
                            $json['total'] = $terms_count;
                            $json['csv_file'] = $file_url;
                        } else {
                            $json['error'] = __('Fail, something wrong to create csv file.', GEODIRECTORY_TEXTDOMAIN);
                        }
                    } else {
                        $json['error'] = __('No records to export.', GEODIRECTORY_TEXTDOMAIN);
                    }
                }
                // WPML
                if ($is_wpml) {
                    $sitepress->switch_lang($active_lang, true);
                }
                // WPML
                wp_send_json($json);
            }
            break;
        case 'prepare_import':
        case 'import_cat':
        case 'import_post':
            // WPML
            $is_wpml = geodir_is_wpml();
            if ($is_wpml) {
                global $sitepress;
                $active_lang = ICL_LANGUAGE_CODE;
            }
            // WPML
            ini_set('auto_detect_line_endings', true);
            $uploads = wp_upload_dir();
            $uploads_dir = $uploads['path'];
            $uploads_subdir = $uploads['subdir'];
            $csv_file = isset($_POST['_file']) ? $_POST['_file'] : NULL;
            $import_choice = isset($_REQUEST['_ch']) ? $_REQUEST['_ch'] : 'skip';
            $csv_file_arr = explode('/', $csv_file);
            $csv_filename = end($csv_file_arr);
            $target_path = $uploads_dir . '/temp_' . $current_user->data->ID . '/' . $csv_filename;
            $json['file'] = $csv_file;
            $json['error'] = __('The uploaded file is not a valid csv file. Please try again.', GEODIRECTORY_TEXTDOMAIN);
            if ($csv_file && $wp_filesystem->is_file($target_path) && $wp_filesystem->exists($target_path)) {
                $wp_filetype = wp_check_filetype_and_ext($target_path, $csv_filename);
                if (!empty($wp_filetype) && isset($wp_filetype['ext']) && strtolower($wp_filetype['ext']) == 'csv') {
                    $json['error'] = NULL;
                    $json['rows'] = 0;
                    if (($handle = fopen($target_path, "r")) !== FALSE) {
                        while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
                            if (!empty($data)) {
                                $file[] = $data;
                            }
                        }
                        fclose($handle);
                    }
                    $json['rows'] = !empty($file) && count($file) > 1 ? count($file) - 1 : 0;
                    if (!$json['rows'] > 0) {
                        $json['error'] = __('No data found in csv file.', GEODIRECTORY_TEXTDOMAIN);
                    }
                } else {
                    wp_send_json($json);
                }
            } else {
                wp_send_json($json);
            }
            if ($task == 'prepare_import' || !empty($json['error'])) {
                wp_send_json($json);
            }
            $total = $json['rows'];
            $limit = isset($_POST['limit']) ? (int) $_POST['limit'] : 1;
            $processed = isset($_POST['processed']) ? (int) $_POST['processed'] : 0;
            $count = $limit;
            $requested_limit = $limit;
            if ($count < $total) {
                $count = $processed + $count;
                if ($count > $total) {
                    $count = $total;
                }
            } else {
                $count = $total;
            }
            $created = 0;
            $updated = 0;
            $skipped = 0;
            $invalid = 0;
            $invalid_addr = 0;
            $images = 0;
            $invalid_title = 0;
            $customKeyarray = array();
            $gd_post_info = array();
            $post_location = array();
            $countpost = 0;
            $post_types = geodir_get_posttypes();
            if ($task == 'import_cat') {
                if (!empty($file)) {
                    $columns = isset($file[0]) ? $file[0] : NULL;
                    if (empty($columns) || !empty($columns) && $columns[0] == '') {
                        $json['error'] = CSV_INVAILD_FILE;
                        wp_send_json($json);
                    }
                    for ($i = 1; $i <= $limit; $i++) {
                        $index = $processed + $i;
                        if (isset($file[$index])) {
                            $row = $file[$index];
                            $row = array_map('trim', $row);
                            $row = array_map('utf8_encode', $row);
                            $cat_id = '';
                            $cat_name = '';
                            $cat_slug = '';
                            $cat_posttype = '';
                            $cat_parent = '';
                            $cat_description = '';
                            $cat_top_description = '';
                            $cat_image = '';
                            $cat_icon = '';
                            $cat_language = '';
                            $c = 0;
                            foreach ($columns as $column) {
                                if ($column == 'cat_id') {
                                    $cat_id = (int) $row[$c];
                                } else {
                                    if ($column == 'cat_name') {
                                        $cat_name = $row[$c];
                                    } else {
                                        if ($column == 'cat_slug') {
                                            $cat_slug = $row[$c];
                                        } else {
                                            if ($column == 'cat_posttype') {
                                                $cat_posttype = $row[$c];
                                            } else {
                                                if ($column == 'cat_parent') {
                                                    $cat_parent = trim($row[$c]);
                                                } else {
                                                    if ($column == 'cat_description') {
                                                        $cat_description = $row[$c];
                                                    } else {
                                                        if ($column == 'cat_top_description') {
                                                            $cat_top_description = $row[$c];
                                                        } else {
                                                            if ($column == 'cat_image') {
                                                                $cat_image = $row[$c];
                                                            } else {
                                                                if ($column == 'cat_icon') {
                                                                    $cat_icon = $row[$c];
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                // WPML
                                if ($is_wpml && $column == 'cat_language') {
                                    $cat_language = strtolower(trim($row[$c]));
                                }
                                // WPML
                                $c++;
                            }
                            if ($cat_name == '' || !in_array($cat_posttype, $post_types)) {
                                $invalid++;
                                continue;
                            }
                            // WPML
                            if ($is_wpml && $cat_language != '') {
                                $sitepress->switch_lang($cat_language, true);
                            }
                            // WPML
                            $term_data = array();
                            $term_data['name'] = $cat_name;
                            $term_data['slug'] = $cat_slug;
                            $term_data['description'] = $cat_description;
                            $term_data['top_description'] = $cat_top_description;
                            $term_data['image'] = $cat_image != '' ? basename($cat_image) : '';
                            $term_data['icon'] = $cat_icon != '' ? basename($cat_icon) : '';
                            $term_data = array_map('utf8_encode', $term_data);
                            $taxonomy = $cat_posttype . 'category';
                            $term_data['taxonomy'] = $taxonomy;
                            $term_parent_id = 0;
                            if ($cat_parent != "" || (int) $cat_parent > 0) {
                                $term_parent = '';
                                if ($term_parent = get_term_by('name', $cat_parent, $taxonomy)) {
                                    $term_parent = $term_parent;
                                } else {
                                    if ($term_parent = get_term_by('slug', $cat_parent, $taxonomy)) {
                                        $term_parent = $term_parent;
                                    } else {
                                        if ($term_parent = get_term_by('id', $cat_parent, $taxonomy)) {
                                            $term_parent = $term_parent;
                                        } else {
                                            $term_parent_data = array();
                                            $term_parent_data['name'] = $cat_parent;
                                            $term_parent_data = array_map('utf8_encode', $term_parent_data);
                                            $term_parent_data['taxonomy'] = $taxonomy;
                                            $term_parent_id = (int) geodir_imex_insert_term($taxonomy, $term_parent_data);
                                        }
                                    }
                                }
                                if (!empty($term_parent) && !is_wp_error($term_parent)) {
                                    $term_parent_id = (int) $term_parent->term_id;
                                }
                            }
                            $term_data['parent'] = (int) $term_parent_id;
                            $term_id = NULL;
                            if ($import_choice == 'update') {
                                if ($cat_id > 0 && ($term = (array) term_exists($cat_id, $taxonomy))) {
                                    $term_data['term_id'] = $term['term_id'];
                                    if ($term_id = geodir_imex_update_term($taxonomy, $term_data)) {
                                        $updated++;
                                    } else {
                                        $invalid++;
                                    }
                                } else {
                                    if ($term_data['slug'] != '' && ($term = (array) term_exists($term_data['slug'], $taxonomy))) {
                                        $term_data['term_id'] = $term['term_id'];
                                        if ($term_id = geodir_imex_update_term($taxonomy, $term_data)) {
                                            $updated++;
                                        } else {
                                            $invalid++;
                                        }
                                    } else {
                                        if ($term_id = geodir_imex_insert_term($taxonomy, $term_data)) {
                                            $created++;
                                        } else {
                                            $invalid++;
                                        }
                                    }
                                }
                            } else {
                                if ($import_choice == 'skip') {
                                    if ($cat_id > 0 && ($term = (array) term_exists($cat_id, $taxonomy))) {
                                        $skipped++;
                                    } else {
                                        if ($term_data['slug'] != '' && ($term = (array) term_exists($term_data['slug'], $taxonomy))) {
                                            $skipped++;
                                        } else {
                                            if ($term_id = geodir_imex_insert_term($taxonomy, $term_data)) {
                                                $created++;
                                            } else {
                                                $invalid++;
                                            }
                                        }
                                    }
                                } else {
                                    $invalid++;
                                }
                            }
                            if ($term_id) {
                                if (isset($term_data['top_description'])) {
                                    update_tax_meta($term_id, 'ct_cat_top_desc', $term_data['top_description'], $cat_posttype);
                                }
                                $attachment = false;
                                if (isset($term_data['image']) && $term_data['image'] != '') {
                                    $cat_image = geodir_get_default_catimage($term_id, $cat_posttype);
                                    $cat_image = !empty($cat_image) && isset($cat_image['src']) ? $cat_image['src'] : '';
                                    if (basename($cat_image) != $term_data['image']) {
                                        $attachment = true;
                                        update_tax_meta($term_id, 'ct_cat_default_img', array('id' => 'image', 'src' => $uploads['url'] . '/' . $term_data['image']), $cat_posttype);
                                    }
                                }
                                if (isset($term_data['icon']) && $term_data['icon'] != '') {
                                    $cat_icon = get_tax_meta($term_id, 'ct_cat_icon', false, $cat_posttype);
                                    $cat_icon = !empty($cat_icon) && isset($cat_icon['src']) ? $cat_icon['src'] : '';
                                    if (basename($cat_icon) != $term_data['icon']) {
                                        $attachment = true;
                                        update_tax_meta($term_id, 'ct_cat_icon', array('id' => 'icon', 'src' => $uploads['url'] . '/' . $term_data['icon']), $cat_posttype);
                                    }
                                }
                                if ($attachment) {
                                    $images++;
                                }
                            }
                            // WPML
                            if ($is_wpml && $cat_language != '') {
                                $sitepress->switch_lang($active_lang, true);
                            }
                            // WPML
                        }
                    }
                }
                $json = array();
                $json['processed'] = $limit;
                $json['created'] = $created;
                $json['updated'] = $updated;
                $json['skipped'] = $skipped;
                $json['invalid'] = $invalid;
                $json['images'] = $images;
                wp_send_json($json);
            } else {
                if ($task == 'import_post') {
                    if (!empty($file)) {
                        $wp_post_statuses = get_post_statuses();
                        // All of the WordPress supported post statuses.
                        $default_status = 'publish';
                        $current_date = date_i18n('Y-m-d', time());
                        $columns = isset($file[0]) ? $file[0] : NULL;
                        if (empty($columns) || !empty($columns) && $columns[0] == '') {
                            $json['error'] = CSV_INVAILD_FILE;
                            wp_send_json($json);
                        }
                        for ($i = 1; $i <= $limit; $i++) {
                            $index = $processed + $i;
                            $gd_post = array();
                            if (isset($file[$index])) {
                                $row = $file[$index];
                                $row = array_map('trim', $row);
                                $row = array_map('utf8_encode', $row);
                                $row = array_map('addslashes_gpc', $row);
                                $post_id = '';
                                $post_title = '';
                                $post_author = '';
                                $post_content = '';
                                $post_category_arr = array();
                                $post_tags = array();
                                $post_type = '';
                                $post_status = '';
                                $geodir_video = '';
                                $post_address = '';
                                $post_city = '';
                                $post_region = '';
                                $post_country = '';
                                $post_zip = '';
                                $post_latitude = '';
                                $post_longitude = '';
                                $geodir_timing = '';
                                $geodir_contact = '';
                                $geodir_email = '';
                                $geodir_website = '';
                                $geodir_twitter = '';
                                $geodir_facebook = '';
                                $geodir_twitter = '';
                                $post_images = array();
                                $expire_date = 'Never';
                                $language = '';
                                $original_post_id = '';
                                $c = 0;
                                foreach ($columns as $column) {
                                    $gd_post[$column] = $row[$c];
                                    if ($column == 'post_id') {
                                        $post_id = $row[$c];
                                    } else {
                                        if ($column == 'post_title') {
                                            $post_title = $row[$c];
                                        } else {
                                            if ($column == 'post_author') {
                                                $post_author = $row[$c];
                                            } else {
                                                if ($column == 'post_content') {
                                                    $post_content = $row[$c];
                                                } else {
                                                    if ($column == 'post_category' && $row[$c] != '') {
                                                        $post_category_arr = explode(',', $row[$c]);
                                                    } else {
                                                        if ($column == 'post_tags' && $row[$c] != '') {
                                                            $post_tags = explode(',', $row[$c]);
                                                        } else {
                                                            if ($column == 'post_type') {
                                                                $post_type = $row[$c];
                                                            } else {
                                                                if ($column == 'post_status') {
                                                                    $post_status = sanitize_key($row[$c]);
                                                                } else {
                                                                    if ($column == 'is_featured') {
                                                                        $is_featured = (int) $row[$c];
                                                                    } else {
                                                                        if ($column == 'geodir_video') {
                                                                            $geodir_video = $row[$c];
                                                                        } else {
                                                                            if ($column == 'post_address') {
                                                                                $post_address = $row[$c];
                                                                            } else {
                                                                                if ($column == 'post_city') {
                                                                                    $post_city = $row[$c];
                                                                                } else {
                                                                                    if ($column == 'post_region') {
                                                                                        $post_region = $row[$c];
                                                                                    } else {
                                                                                        if ($column == 'post_country') {
                                                                                            $post_country = $row[$c];
                                                                                        } else {
                                                                                            if ($column == 'post_zip') {
                                                                                                $post_zip = $row[$c];
                                                                                            } else {
                                                                                                if ($column == 'post_latitude') {
                                                                                                    $post_latitude = $row[$c];
                                                                                                } else {
                                                                                                    if ($column == 'post_longitude') {
                                                                                                        $post_longitude = $row[$c];
                                                                                                    } else {
                                                                                                        if ($column == 'geodir_timing') {
                                                                                                            $geodir_timing = $row[$c];
                                                                                                        } else {
                                                                                                            if ($column == 'geodir_contact') {
                                                                                                                $geodir_contact = $row[$c];
                                                                                                            } else {
                                                                                                                if ($column == 'geodir_email') {
                                                                                                                    $geodir_email = $row[$c];
                                                                                                                } else {
                                                                                                                    if ($column == 'geodir_website') {
                                                                                                                        $geodir_website = $row[$c];
                                                                                                                    } else {
                                                                                                                        if ($column == 'geodir_twitter') {
                                                                                                                            $geodir_twitter = $row[$c];
                                                                                                                        } else {
                                                                                                                            if ($column == 'geodir_facebook') {
                                                                                                                                $geodir_facebook = $row[$c];
                                                                                                                            } else {
                                                                                                                                if ($column == 'geodir_twitter') {
                                                                                                                                    $geodir_twitter = $row[$c];
                                                                                                                                } else {
                                                                                                                                    if ($column == 'IMAGE' && !empty($row[$c]) && $row[$c] != '') {
                                                                                                                                        $post_images[] = $row[$c];
                                                                                                                                    } else {
                                                                                                                                        if ($column == 'alive_days' && (int) $row[$c] > 0) {
                                                                                                                                            $expire_date = date_i18n('Y-m-d', strtotime($current_date . '+' . (int) $row[$c] . ' days'));
                                                                                                                                        } else {
                                                                                                                                            if ($column == 'expire_date' && $row[$c] != '' && strtolower($row[$c]) != 'never') {
                                                                                                                                                $row[$c] = str_replace('/', '-', $row[$c]);
                                                                                                                                                $expire_date = date_i18n('Y-m-d', strtotime($row[$c]));
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    // WPML
                                    if ($is_wpml) {
                                        if ($column == 'language') {
                                            $language = strtolower(trim($row[$c]));
                                        } else {
                                            if ($column == 'original_post_id') {
                                                $original_post_id = (int) $row[$c];
                                            }
                                        }
                                    }
                                    // WPML
                                    $c++;
                                }
                                // WPML
                                if ($is_wpml && $language != '') {
                                    $sitepress->switch_lang($language, true);
                                }
                                // WPML
                                $gd_post['IMAGE'] = $post_images;
                                $post_status = !empty($post_status) ? sanitize_key($post_status) : $default_status;
                                $post_status = !empty($wp_post_statuses) && !isset($wp_post_statuses[$post_status]) ? $default_status : $post_status;
                                $valid = true;
                                if ($post_title == '' || !in_array($post_type, $post_types)) {
                                    $invalid++;
                                    $valid = false;
                                }
                                $location_allowed = function_exists('geodir_cpt_no_location') && geodir_cpt_no_location($post_type) ? false : true;
                                if ($location_allowed) {
                                    $location_result = geodir_get_default_location();
                                    if ($post_address == '' || $post_city == '' || $post_region == '' || $post_country == '' || $post_latitude == '' || $post_longitude == '') {
                                        $invalid_addr++;
                                        $valid = false;
                                    } else {
                                        if (!empty($location_result) && $location_result->location_id == 0) {
                                            if (strtolower($post_city) != strtolower($location_result->city) || strtolower($post_region) != strtolower($location_result->region) || strtolower($post_country) != strtolower($location_result->country)) {
                                                $invalid_addr++;
                                                $valid = false;
                                            } else {
                                                if (!function_exists('geodir_location_plugin_activated')) {
                                                    $gd_post['post_locations'] = '[' . $location_result->city_slug . '],[' . $location_result->region_slug . '],[' . $location_result->country_slug . ']';
                                                    // Set the default location when location manager not activated.
                                                }
                                            }
                                        }
                                    }
                                }
                                if (!$valid) {
                                    continue;
                                }
                                $cat_taxonomy = $post_type . 'category';
                                $tags_taxonomy = $post_type . '_tags';
                                $post_category = array();
                                if (!empty($post_category_arr)) {
                                    foreach ($post_category_arr as $value) {
                                        $category_name = wp_kses_normalize_entities(trim($value));
                                        if ($category_name != '') {
                                            $term_category = array();
                                            if ($term = get_term_by('name', $category_name, $cat_taxonomy)) {
                                                $term_category = $term;
                                            } else {
                                                if ($term = get_term_by('slug', $category_name, $cat_taxonomy)) {
                                                    $term_category = $term;
                                                } else {
                                                    $term_data = array();
                                                    $term_data['name'] = $category_name;
                                                    $term_data = array_map('utf8_encode', $term_data);
                                                    $term_data['taxonomy'] = $cat_taxonomy;
                                                    $term_id = geodir_imex_insert_term($cat_taxonomy, $term_data);
                                                    if ($term_id) {
                                                        $term_category = get_term($term_id, $cat_taxonomy);
                                                    }
                                                }
                                            }
                                            if (!empty($term_category) && !is_wp_error($term_category)) {
                                                //$post_category[] = $term_category->slug;
                                                $post_category[] = intval($term_category->term_id);
                                            }
                                        }
                                    }
                                }
                                $save_post = array();
                                $save_post['post_title'] = $post_title;
                                $save_post['post_content'] = $post_content;
                                $save_post['post_type'] = $post_type;
                                $save_post['post_author'] = $post_author;
                                $save_post['post_status'] = $post_status;
                                $save_post['post_category'] = $post_category;
                                $save_post['post_tags'] = $post_tags;
                                $saved_post_id = NULL;
                                if ($import_choice == 'update') {
                                    if ($post_id > 0 && get_post($post_id)) {
                                        $save_post['ID'] = $post_id;
                                        if (wp_update_post($save_post)) {
                                            $saved_post_id = $post_id;
                                            $updated++;
                                        }
                                    } else {
                                        if ($saved_post_id = wp_insert_post($save_post)) {
                                            $created++;
                                        }
                                    }
                                    if (!$saved_post_id > 0) {
                                        $invalid++;
                                    }
                                } else {
                                    if ($import_choice == 'skip') {
                                        if ($post_id > 0 && get_post($post_id)) {
                                            $skipped++;
                                        } else {
                                            if ($saved_post_id = wp_insert_post($save_post)) {
                                                $created++;
                                            } else {
                                                $invalid++;
                                            }
                                        }
                                    } else {
                                        $invalid++;
                                    }
                                }
                                if ((int) $saved_post_id > 0) {
                                    // WPML
                                    if ($is_wpml && $original_post_id > 0 && $language != '') {
                                        $wpml_post_type = 'post_' . $post_type;
                                        $source_language = geodir_get_language_for_element($original_post_id, $wpml_post_type);
                                        $source_language = $source_language != '' ? $source_language : $sitepress->get_default_language();
                                        $trid = $sitepress->get_element_trid($original_post_id, $wpml_post_type);
                                        $sitepress->set_element_language_details($saved_post_id, $wpml_post_type, $trid, $language, $source_language);
                                    }
                                    // WPML
                                    $gd_post_info = geodir_get_post_info($saved_post_id);
                                    $gd_post['post_id'] = $saved_post_id;
                                    $gd_post['ID'] = $saved_post_id;
                                    $gd_post['post_tags'] = $post_tags;
                                    $gd_post['post_title'] = $post_title;
                                    $gd_post['post_status'] = $post_status;
                                    $gd_post['submit_time'] = time();
                                    $gd_post['submit_ip'] = $_SERVER['REMOTE_ADDR'];
                                    // post location
                                    $post_location_id = 0;
                                    if ($location_allowed && !empty($location_result) && $location_result->location_id > 0) {
                                        $post_location_info = array('city' => $post_city, 'region' => $post_region, 'country' => $post_country, 'geo_lat' => $post_latitude, 'geo_lng' => $post_longitude);
                                        if ($location_id = (int) geodir_add_new_location($post_location_info)) {
                                            $post_location_id = $location_id;
                                        }
                                    }
                                    $gd_post['post_location_id'] = $post_location_id;
                                    // post package info
                                    $package_id = isset($gd_post['package_id']) && !empty($gd_post['package_id']) ? (int) $gd_post['package_id'] : 0;
                                    if (!$package_id && !empty($gd_post_info) && isset($gd_post_info->package_id) && $gd_post_info->package_id) {
                                        $package_id = $gd_post_info->package_id;
                                    }
                                    $package_info = array();
                                    if ($package_id && function_exists('geodir_get_package_info_by_id')) {
                                        $package_info = (array) geodir_get_package_info_by_id($package_id);
                                        if (!(!empty($package_info) && isset($package_info['post_type']) && $package_info['post_type'] == $post_type)) {
                                            $package_info = array();
                                        }
                                    }
                                    if (empty($package_info)) {
                                        $package_info = (array) geodir_post_package_info(array(), '', $post_type);
                                    }
                                    if (!empty($package_info)) {
                                        $package_id = $package_info['pid'];
                                        if (isset($gd_post['alive_days']) || isset($gd_post['expire_date'])) {
                                            $gd_post['expire_date'] = $expire_date;
                                        } else {
                                            if (isset($package_info['days']) && (int) $package_info['days'] > 0) {
                                                $gd_post['alive_days'] = (int) $package_info['days'];
                                                $gd_post['expire_date'] = date_i18n('Y-m-d', strtotime($current_date . '+' . (int) $package_info['days'] . ' days'));
                                            } else {
                                                $gd_post['expire_date'] = 'Never';
                                            }
                                        }
                                        $gd_post['package_id'] = $package_id;
                                    }
                                    $table = $plugin_prefix . $post_type . '_detail';
                                    if ($post_type == 'gd_event') {
                                        $gd_post = geodir_imex_process_event_data($gd_post);
                                    }
                                    if (isset($gd_post['post_id'])) {
                                        unset($gd_post['post_id']);
                                    }
                                    // Export franchise fields
                                    $is_franchise_active = is_plugin_active('geodir_franchise/geodir_franchise.php') && geodir_franchise_enabled($post_type) ? true : false;
                                    if ($is_franchise_active) {
                                        if (isset($gd_post['gd_is_franchise']) && (int) $gd_post['gd_is_franchise'] == 1) {
                                            $gd_franchise_lock = array();
                                            if (isset($gd_post['gd_franchise_lock'])) {
                                                $gd_franchise_lock = str_replace(" ", "", $gd_post['gd_franchise_lock']);
                                                $gd_franchise_lock = trim($gd_franchise_lock);
                                                $gd_franchise_lock = explode(",", $gd_franchise_lock);
                                            }
                                            update_post_meta($saved_post_id, 'gd_is_franchise', 1);
                                            update_post_meta($saved_post_id, 'gd_franchise_lock', $gd_franchise_lock);
                                        } else {
                                            if (isset($gd_post['franchise']) && (int) $gd_post['franchise'] > 0 && geodir_franchise_check((int) $gd_post['franchise'])) {
                                                geodir_save_post_meta($saved_post_id, 'franchise', (int) $gd_post['franchise']);
                                            }
                                        }
                                    }
                                    if (!empty($save_post['post_category']) && is_array($save_post['post_category'])) {
                                        $save_post['post_category'] = array_unique(array_map('intval', $save_post['post_category']));
                                        $gd_post[$cat_taxonomy] = $save_post['post_category'];
                                    }
                                    // Save post info
                                    geodir_save_post_info($saved_post_id, $gd_post);
                                    // post taxonomies
                                    if (!empty($save_post['post_category'])) {
                                        wp_set_object_terms($saved_post_id, $save_post['post_category'], $cat_taxonomy);
                                        $post_default_category = isset($save_post['post_default_category']) ? $save_post['post_default_category'] : '';
                                        $post_category_str = isset($save_post['post_category_str']) ? $save_post['post_category_str'] : '';
                                        geodir_set_postcat_structure($saved_post_id, $cat_taxonomy, $post_default_category, $post_category_str);
                                    }
                                    if (!empty($save_post['post_tags'])) {
                                        wp_set_object_terms($saved_post_id, $save_post['post_tags'], $tags_taxonomy);
                                    }
                                    // Post images
                                    if (!empty($post_images)) {
                                        $post_images = array_unique($post_images);
                                        $old_post_images_arr = array();
                                        $saved_post_images_arr = array();
                                        $order = 1;
                                        $old_post_images = geodir_get_images($saved_post_id);
                                        if (!empty($old_post_images)) {
                                            foreach ($old_post_images as $old_post_image) {
                                                if (!empty($old_post_image) && isset($old_post_image->file) && $old_post_image->file != '') {
                                                    $old_post_images_arr[] = $old_post_image->file;
                                                }
                                            }
                                        }
                                        foreach ($post_images as $post_image) {
                                            $image_name = basename($post_image);
                                            $saved_post_images_arr[] = $image_name;
                                            if (!empty($old_post_images_arr) && in_array($image_name, $old_post_images_arr)) {
                                                continue;
                                                // Skip if image already exists.
                                            }
                                            $image_name_parts = explode('.', $image_name);
                                            array_pop($image_name_parts);
                                            $proper_image_name = implode('.', $image_name_parts);
                                            $arr_file_type = wp_check_filetype($image_name);
                                            if (!empty($arr_file_type)) {
                                                $uploaded_file_type = $arr_file_type['type'];
                                                $attachment = array();
                                                $attachment['post_id'] = $saved_post_id;
                                                $attachment['title'] = $proper_image_name;
                                                $attachment['content'] = '';
                                                $attachment['file'] = $uploads_subdir . '/' . $image_name;
                                                $attachment['mime_type'] = $uploaded_file_type;
                                                $attachment['menu_order'] = $order;
                                                $attachment['is_featured'] = 0;
                                                $attachment_set = '';
                                                foreach ($attachment as $key => $val) {
                                                    if ($val != '') {
                                                        $attachment_set .= $key . " = '" . $val . "', ";
                                                    }
                                                }
                                                $attachment_set = trim($attachment_set, ", ");
                                                // Add new attachment
                                                $wpdb->query("INSERT INTO " . GEODIR_ATTACHMENT_TABLE . " SET " . $attachment_set);
                                                $order++;
                                            }
                                        }
                                        $saved_post_images_sql = !empty($saved_post_images_arr) ? " AND ( file NOT LIKE '%/" . implode("' AND file NOT LIKE '%/", $saved_post_images_arr) . "' )" : '';
                                        // Remove previous attachment
                                        $wpdb->query("DELETE FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE post_id = " . (int) $saved_post_id . " " . $saved_post_images_sql);
                                        if (!empty($saved_post_images_arr)) {
                                            $menu_order = 1;
                                            foreach ($saved_post_images_arr as $img_name) {
                                                $wpdb->query($wpdb->prepare("UPDATE " . GEODIR_ATTACHMENT_TABLE . " SET menu_order = %d WHERE post_id =%d AND file LIKE %s", array($menu_order, $saved_post_id, '%/' . $img_name)));
                                                if ($menu_order == 1) {
                                                    if ($featured_image = $wpdb->get_var($wpdb->prepare("SELECT file FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE post_id =%d AND file LIKE %s", array($saved_post_id, '%/' . $img_name)))) {
                                                        $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET featured_image = %s WHERE post_id =%d", array($featured_image, $saved_post_id)));
                                                    }
                                                }
                                                $menu_order++;
                                            }
                                        }
                                        if ($order > 1) {
                                            $images++;
                                        }
                                    }
                                    /** This action is documented in geodirectory-functions/post-functions.php */
                                    do_action('geodir_after_save_listing', $saved_post_id, $gd_post);
                                    if (isset($is_featured)) {
                                        geodir_save_post_meta($saved_post_id, 'is_featured', $is_featured);
                                    }
                                    if (isset($gd_post['expire_date'])) {
                                        geodir_save_post_meta($saved_post_id, 'expire_date', $gd_post['expire_date']);
                                    }
                                }
                                // WPML
                                if ($is_wpml && $language != '') {
                                    $sitepress->switch_lang($active_lang, true);
                                }
                                // WPML
                            }
                        }
                    }
                    $json = array();
                    $json['processed'] = $limit;
                    $json['created'] = $created;
                    $json['updated'] = $updated;
                    $json['skipped'] = $skipped;
                    $json['invalid'] = $invalid;
                    $json['invalid_addr'] = $invalid_addr;
                    $json['images'] = $images;
                    wp_send_json($json);
                }
            }
            break;
    }
    echo '0';
    wp_die();
}