/**
 * Imports data from CSV file using ajax.
 *
 * @since 1.0.0
 * @package GeoDirectory
 * @global object $wpdb WordPress Database object.
 * @global string $plugin_prefix Geodirectory plugin table prefix.
 * @global object $current_user Current user object.
 */
function geodir_ajax_import_csv()
{
    error_reporting(0);
    // hide error to get clean json response
    global $wpdb, $plugin_prefix, $current_user;
    $uploads = wp_upload_dir();
    ini_set('auto_detect_line_endings', true);
    $wp_post_statuses = get_post_statuses();
    // All of the WordPress supported post statuses.
    $task = isset($_POST['task']) ? $_POST['task'] : '';
    $uploadedFile = isset($_POST['gddata']['uploadedFile']) ? $_POST['gddata']['uploadedFile'] : NULL;
    $filename = $uploadedFile;
    $uploads = wp_upload_dir();
    $uploads_dir = $uploads['path'];
    $image_name_arr = explode('/', $filename);
    $filename = end($image_name_arr);
    $target_path = $uploads_dir . '/temp_' . $current_user->data->ID . '/' . $filename;
    $return = array();
    $return['file'] = $uploadedFile;
    $return['error'] = __('The uploaded file is not a valid csv file. Please try again.', 'geodirectory');
    if (is_file($target_path) && file_exists($target_path) && $uploadedFile) {
        $wp_filetype = wp_check_filetype_and_ext($target_path, $filename);
        if (!empty($wp_filetype) && isset($wp_filetype['ext']) && geodir_strtolower($wp_filetype['ext']) == 'csv') {
            $return['error'] = NULL;
            $return['rows'] = 0;
            if (($handle = fopen($target_path, "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    if (is_array($data) && !empty($data)) {
                        $file[] = '"' . implode('","', $data) . '"';
                    }
                }
                fclose($handle);
                $file = $file;
            }
            $return['rows'] = !empty($file) && count($file) > 1 ? count($file) - 1 : 0;
            if (!$return['rows'] > 0) {
                $return['error'] = __('No data found in csv file.', 'geodirectory');
            }
        }
    }
    if ($task == 'prepare' || !empty($return['error'])) {
        echo json_encode($return);
        exit;
    }
    $totRecords = isset($_POST['gddata']['totRecords']) ? $_POST['gddata']['totRecords'] : NULL;
    $importlimit = isset($_POST['gddata']['importlimit']) ? $_POST['gddata']['importlimit'] : 1;
    $count = $importlimit;
    $requested_limit = $importlimit;
    $tmpCnt = isset($_POST['gddata']['tmpcount']) ? $_POST['gddata']['tmpcount'] : 0;
    if ($count < $totRecords) {
        $count = $tmpCnt + $count;
        if ($count > $totRecords) {
            $count = $totRecords;
        }
    } else {
        $count = $totRecords;
    }
    $total_records = 0;
    $rowcount = 0;
    $address_invalid = 0;
    $blank_address = 0;
    $upload_files = 0;
    $invalid_post_type = 0;
    $invalid_title = 0;
    $customKeyarray = array();
    $gd_post_info = array();
    $post_location = array();
    $countpost = 0;
    if (!empty($file)) {
        $columns = isset($file[0]) ? geodir_str_getcsv($file[0]) : NULL;
        $customKeyarray = $columns;
        if (empty($columns) || !empty($columns) && $columns[0] == '') {
            $return['error'] = CSV_INVAILD_FILE;
            echo json_encode($return);
            exit;
        }
        for ($i = 1; $i <= $importlimit; $i++) {
            $current_index = $tmpCnt + $i;
            if (isset($file[$current_index])) {
                $total_records++;
                $buffer = geodir_str_getcsv($file[$current_index]);
                $post_title = addslashes($buffer[0]);
                $current_post_author = $buffer[1];
                $post_desc = addslashes($buffer[2]);
                $post_cat = array();
                $catids_arr = array();
                $post_cat = trim($buffer[3]);
                // comma seperated category name
                if ($post_cat) {
                    $post_cat_arr = explode(',', $post_cat);
                    for ($c = 0; $c < count($post_cat_arr); $c++) {
                        $catid = wp_kses_normalize_entities(trim($post_cat_arr[$c]));
                        if (!empty($buffer[5])) {
                            if (in_array($buffer[5], geodir_get_posttypes())) {
                                $p_taxonomy = geodir_get_taxonomies(addslashes($buffer[5]));
                                if (get_term_by('name', $catid, $p_taxonomy[0])) {
                                    $cat = get_term_by('name', $catid, $p_taxonomy[0]);
                                    $catids_arr[] = $cat->slug;
                                } else {
                                    if (get_term_by('slug', $catid, $p_taxonomy[0])) {
                                        $cat = get_term_by('slug', $catid, $p_taxonomy[0]);
                                        $catids_arr[] = $cat->slug;
                                    } else {
                                        $ret = wp_insert_term($catid, $p_taxonomy[0]);
                                        if ($ret && !is_wp_error($ret)) {
                                            if (get_term_by('name', $catid, $p_taxonomy[0])) {
                                                $cat = get_term_by('name', $catid, $p_taxonomy[0]);
                                                $catids_arr[] = $cat->slug;
                                            } elseif (get_term_by('slug', $catid, $p_taxonomy[0])) {
                                                $cat = get_term_by('slug', $catid, $p_taxonomy[0]);
                                                $catids_arr[] = $cat->slug;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (!$catids_arr) {
                    $catids_arr[] = 1;
                }
                $post_tags = trim($buffer[4]);
                // comma seperated tags
                $tag_arr = '';
                if ($post_tags) {
                    $tag_arr = explode(',', $post_tags);
                }
                $table = $plugin_prefix . $buffer[5] . '_detail';
                // check table in database
                $error = '';
                if ($wpdb->get_var("SHOW TABLES LIKE '" . $table . "'") != $table) {
                    $invalid_post_type++;
                    continue;
                }
                if ($post_title != '') {
                    $menu_order = 0;
                    $image_folder_name = 'uplaod/';
                    $image_names = array();
                    for ($c = 5; $c < count($customKeyarray); $c++) {
                        $gd_post_info[$customKeyarray[$c]] = addslashes($buffer[$c]);
                        if ($customKeyarray[$c] == 'IMAGE') {
                            $buffer[$c] = trim($buffer[$c]);
                            if (!empty($buffer[$c])) {
                                $image_names[] = $buffer[$c];
                            }
                        }
                        if ($customKeyarray[$c] == 'alive_days') {
                            if ($buffer[$c] != '0' && $buffer[$c] != '') {
                                $submitdata = date('Y-m-d');
                                $gd_post_info['expire_date'] = date('Y-m-d', strtotime($submitdata . "+" . addslashes($buffer[$c]) . " days"));
                            } else {
                                $gd_post_info['expire_date'] = 'Never';
                            }
                        }
                        if ($customKeyarray[$c] == 'post_city') {
                            $post_city = addslashes($buffer[$c]);
                        }
                        if ($customKeyarray[$c] == 'post_region') {
                            $post_region = addslashes($buffer[$c]);
                        }
                        if ($customKeyarray[$c] == 'post_country') {
                            $post_country = addslashes($buffer[$c]);
                        }
                        if ($customKeyarray[$c] == 'post_latitude') {
                            $post_latitude = addslashes($buffer[$c]);
                        }
                        if ($customKeyarray[$c] == 'post_longitude') {
                            $post_longitude = addslashes($buffer[$c]);
                        }
                        // Post status
                        if ($customKeyarray[$c] == 'post_status') {
                            $post_status = sanitize_key($buffer[$c]);
                        }
                    }
                    /* ================ before array create ============== */
                    $location_result = geodir_get_default_location();
                    if (!isset($gd_post_info['post_city']) || $gd_post_info['post_city'] == '' || (!isset($gd_post_info['post_region']) || $gd_post_info['post_region'] == '') || (!isset($gd_post_info['post_country']) || $gd_post_info['post_country'] == '') || (!isset($gd_post_info['post_address']) || $gd_post_info['post_address'] == '') || (!isset($gd_post_info['post_latitude']) || $gd_post_info['post_latitude'] == '') || (!isset($gd_post_info['post_longitude']) || $gd_post_info['post_longitude'] == '')) {
                        $blank_address++;
                        continue;
                    } else {
                        if ($location_result->location_id == 0) {
                            if (geodir_strtolower($gd_post_info['post_city']) != geodir_strtolower($location_result->city) || geodir_strtolower($gd_post_info['post_region']) != geodir_strtolower($location_result->region) || geodir_strtolower($gd_post_info['post_country']) != geodir_strtolower($location_result->country)) {
                                $address_invalid++;
                                continue;
                            }
                        }
                    }
                    // Default post status
                    $default_status = 'publish';
                    $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;
                    $my_post['post_title'] = $post_title;
                    $my_post['post_content'] = $post_desc;
                    $my_post['post_type'] = addslashes($buffer[5]);
                    $my_post['post_author'] = $current_post_author;
                    $my_post['post_status'] = $post_status;
                    $my_post['post_category'] = $catids_arr;
                    $my_post['post_tags'] = $tag_arr;
                    $gd_post_info['post_tags'] = $tag_arr;
                    $gd_post_info['post_title'] = $post_title;
                    $gd_post_info['post_status'] = $post_status;
                    $gd_post_info['submit_time'] = time();
                    $gd_post_info['submit_ip'] = $_SERVER['REMOTE_ADDR'];
                    $last_postid = wp_insert_post($my_post);
                    $countpost++;
                    // Check if we need to save post location as new location
                    if ($location_result->location_id > 0) {
                        if (isset($post_city) && isset($post_region)) {
                            $request_info['post_location'] = array('city' => $post_city, 'region' => $post_region, 'country' => $post_country, 'geo_lat' => $post_latitude, 'geo_lng' => $post_longitude);
                            $post_location_info = $request_info['post_location'];
                            if ($location_id = geodir_add_new_location($post_location_info)) {
                                $post_location_id = $location_id;
                            }
                        } else {
                            $post_location_id = 0;
                        }
                    } else {
                        $post_location_id = 0;
                    }
                    /* ------- get default package info ----- */
                    $payment_info = array();
                    $package_info = array();
                    $package_info = (array) geodir_post_package_info($package_info, '', $buffer[5]);
                    $package_id = '';
                    if (isset($gd_post_info['package_id']) && $gd_post_info['package_id'] != '') {
                        $package_id = $gd_post_info['package_id'];
                    }
                    if (!empty($package_info)) {
                        $payment_info['package_id'] = $package_info['pid'];
                        if (isset($package_info['alive_days']) && $package_info['alive_days'] != 0) {
                            $payment_info['expire_date'] = date('Y-m-d', strtotime("+" . $package_info['alive_days'] . " days"));
                        } else {
                            $payment_info['expire_date'] = 'Never';
                        }
                        $gd_post_info = array_merge($gd_post_info, $payment_info);
                    }
                    $gd_post_info['post_location_id'] = $post_location_id;
                    $post_type = get_post_type($last_postid);
                    $table = $plugin_prefix . $post_type . '_detail';
                    geodir_save_post_info($last_postid, $gd_post_info);
                    if (!empty($image_names)) {
                        $upload_files++;
                        $menu_order = 1;
                        foreach ($image_names as $image_name) {
                            $img_name_arr = explode('.', $image_name);
                            $uploads = wp_upload_dir();
                            $sub_dir = $uploads['subdir'];
                            $arr_file_type = wp_check_filetype($image_name);
                            $uploaded_file_type = $arr_file_type['type'];
                            $attachment = array();
                            $attachment['post_id'] = $last_postid;
                            $attachment['title'] = $img_name_arr[0];
                            $attachment['content'] = '';
                            $attachment['file'] = $sub_dir . '/' . $image_name;
                            $attachment['mime_type'] = $uploaded_file_type;
                            $attachment['menu_order'] = $menu_order;
                            $attachment['is_featured'] = 0;
                            $attachment_set = '';
                            foreach ($attachment as $key => $val) {
                                if ($val != '') {
                                    $attachment_set .= $key . " = '" . $val . "', ";
                                }
                            }
                            $attachment_set = trim($attachment_set, ", ");
                            $wpdb->query("INSERT INTO " . GEODIR_ATTACHMENT_TABLE . " SET " . $attachment_set);
                            if ($menu_order == 1) {
                                $post_type = get_post_type($last_postid);
                                $wpdb->query($wpdb->prepare("UPDATE " . $table . " SET featured_image = %s where post_id =%d", array($sub_dir . '/' . $image_name, $last_postid)));
                            }
                            $menu_order++;
                        }
                    }
                    $gd_post_info['package_id'] = $package_id;
                    /** This action is documented in geodirectory-functions/post-functions.php */
                    do_action('geodir_after_save_listing', $last_postid, $gd_post_info);
                    if (!empty($buffer[5])) {
                        if (in_array($buffer[5], geodir_get_posttypes())) {
                            $taxonomies = geodir_get_posttype_info(addslashes($buffer[5]));
                            wp_set_object_terms($last_postid, $my_post['post_tags'], $taxonomy = $taxonomies['taxonomies'][1]);
                            wp_set_object_terms($last_postid, $my_post['post_category'], $taxonomy = $taxonomies['taxonomies'][0]);
                            $post_default_category = isset($my_post['post_default_category']) ? $my_post['post_default_category'] : '';
                            $post_category_str = isset($my_post['post_category_str']) ? $my_post['post_category_str'] : '';
                            geodir_set_postcat_structure($last_postid, $taxonomy, $post_default_category, $post_category_str);
                        }
                    }
                } else {
                    $invalid_title++;
                }
            }
        }
    }
    $return['rowcount'] = $countpost;
    $return['invalidcount'] = $address_invalid;
    $return['blank_address'] = $blank_address;
    $return['upload_files'] = $upload_files;
    $return['invalid_post_type'] = $invalid_post_type;
    $return['invalid_title'] = $invalid_title;
    $return['total_records'] = $total_records;
    echo json_encode($return);
    exit;
}
Esempio n. 2
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("&quot;", '');
             $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_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_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
                        }
                    }
                }
            }
        }
    }
}
Esempio n. 5
0
/**
 * Duplicate post taxonomies for WPML translation post.
 *
 * @since 1.5.0
 *
 * @global object $sitepress Sitepress WPML object.
 * @global object $wpdb WordPress Database object.
 *
 * @param int $master_post_id Original Post ID.
 * @param int $tr_post_id Translation Post ID.
 * @param string $lang Language code for translating post.
 * @return bool True for success, False for fail.
 */
function geodir_icl_duplicate_taxonomies($master_post_id, $tr_post_id, $lang)
{
    global $sitepress, $wpdb;
    $post_type = get_post_type($master_post_id);
    remove_filter('get_term', array($sitepress, 'get_term_adjust_id'));
    // AVOID filtering to current language
    $taxonomies = get_object_taxonomies($post_type);
    foreach ($taxonomies as $taxonomy) {
        $terms = get_the_terms($master_post_id, $taxonomy);
        $terms_array = array();
        if ($terms) {
            foreach ($terms as $term) {
                $tr_id = apply_filters('translate_object_id', $term->term_id, $taxonomy, false, $lang);
                if (!is_null($tr_id)) {
                    // not using get_term - unfiltered get_term
                    $translated_term = $wpdb->get_row($wpdb->prepare("\r\n\t\t\t\t\t\tSELECT * FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id = t.term_id WHERE t.term_id = %d AND x.taxonomy = %s", $tr_id, $taxonomy));
                    $terms_array[] = $translated_term->term_id;
                }
            }
            if (!is_taxonomy_hierarchical($taxonomy)) {
                $terms_array = array_unique(array_map('intval', $terms_array));
            }
            wp_set_post_terms($tr_post_id, $terms_array, $taxonomy);
            if ($taxonomy == $post_type . 'category') {
                geodir_set_postcat_structure($tr_post_id, $post_type . 'category');
            }
        }
    }
}