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'], '');
                }
            }
        }
    }
}