Function Reference: geodir_expire_check

Summary

This function has not been documented yet.

Actions

‘geodir_downgrade_price’ [Line: 3645]

Source Code

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.post_status != 'trash' AND 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.post_status != 'trash' AND 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');

                        /**
                         * Fires when a post is downgraded by the expire process.
                         *
                         * @param int $post_id The post id that is being downgraded.
                         * @param int $package_id The price package the post was on.
                         * @param int $downgrade_pkg The price package the post is being downgraded to.
                         */
                        do_action('geodir_downgrade_price',$post_id,$package_id,$downgrade_pkg);
						
						$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
						}
					}					
				}				
			}			
		}
	}
}